1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "bufferviewfilter.h"
25 #include "networkmodel.h"
27 /*****************************************
28 * The Filter for the Tree View
29 *****************************************/
30 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QList<uint> &nets)
31 : QSortFilterProxyModel(model),
33 networks(QSet<uint>::fromList(nets))
35 setSourceModel(model);
36 setSortCaseSensitivity(Qt::CaseInsensitive);
39 // ok the following basically sucks. therfore it's commented out. Justice served.
40 // a better solution would use dataChanged()
42 // I have this feeling that this resulted in a fuckup once... no clue though right now and invalidateFilter isn't a slot -.-
43 //connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidate()));
44 // connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateFilter_()));
47 void BufferViewFilter::invalidateFilter_() {
48 QSortFilterProxyModel::invalidateFilter();
51 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
52 Qt::ItemFlags flags = mapToSource(index).flags();
53 if(mode & FullCustom) {
54 if(index == QModelIndex() || index.parent() == QModelIndex())
55 flags |= Qt::ItemIsDropEnabled;
60 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
61 // drops have to occur in the open field
62 if(parent != QModelIndex())
63 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
65 if(!NetworkModel::mimeContainsBufferList(data))
68 QList< QPair<uint, uint> > bufferList = NetworkModel::mimeDataToBufferList(data);
71 for(int i = 0; i < bufferList.count(); i++) {
72 netId = bufferList[i].first;
73 bufferId = bufferList[i].second;
74 if(!networks.contains(netId)) {
82 void BufferViewFilter::addBuffer(const uint &bufferuid) {
83 if(!buffers.contains(bufferuid)) {
89 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
90 if(!(mode & FullCustom))
91 return; // only custom buffers can be customized... obviously... :)
93 if(index.parent() == QModelIndex())
94 return; // only child elements can be deleted
96 bool lastBuffer = (rowCount(index.parent()) == 1);
97 uint netId = index.data(NetworkModel::NetworkIdRole).toUInt();
98 uint bufferuid = index.data(NetworkModel::BufferUidRole).toUInt();
100 if(buffers.contains(bufferuid)) {
101 buffers.remove(bufferuid);
104 networks.remove(netId);
105 Q_ASSERT(!networks.contains(netId));
114 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
115 BufferItem::Type bufferType = (BufferItem::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
117 if((mode & NoChannels) && bufferType == BufferItem::ChannelType)
119 if((mode & NoQueries) && bufferType == BufferItem::QueryType)
121 if((mode & NoServers) && bufferType == BufferItem::StatusType)
124 // bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
125 // if((mode & NoActive) && isActive)
127 // if((mode & NoInactive) && !isActive)
130 if((mode & FullCustom)) {
131 uint bufferuid = source_bufferIndex.data(NetworkModel::BufferUidRole).toUInt();
132 return buffers.contains(bufferuid);
138 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
139 uint net = source_index.data(NetworkModel::NetworkIdRole).toUInt();
140 return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
143 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
144 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
146 if(!child.isValid()) {
147 qDebug() << "filterAcceptsRow has been called with an invalid Child";
151 if(source_parent == QModelIndex())
152 return filterAcceptNetwork(child);
154 return filterAcceptBuffer(child);
157 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const {
158 int lefttype = left.data(NetworkModel::BufferTypeRole).toInt();
159 int righttype = right.data(NetworkModel::BufferTypeRole).toInt();
161 if(lefttype != righttype)
162 return lefttype < righttype;
164 return QSortFilterProxyModel::lessThan(left, right);
167 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
168 if(role == Qt::ForegroundRole)
169 return foreground(index);
171 return QSortFilterProxyModel::data(index, role);
174 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
175 if(!index.data(NetworkModel::ItemActiveRole).toBool())
176 return QColor(Qt::gray);
178 // FIXME:: show colors depending on activity level
179 return QColor(Qt::black);