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<NetworkId> &nets)
31 : QSortFilterProxyModel(model),
33 networks(QSet<NetworkId>::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<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
72 for(int i = 0; i < bufferList.count(); i++) {
73 netId = bufferList[i].first;
74 bufferId = bufferList[i].second;
75 if(!networks.contains(netId)) {
83 void BufferViewFilter::addBuffer(const BufferId &bufferuid) {
84 if(!buffers.contains(bufferuid)) {
90 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
91 if(!(mode & FullCustom))
92 return; // only custom buffers can be customized... obviously... :)
94 if(index.parent() == QModelIndex())
95 return; // only child elements can be deleted
97 bool lastBuffer = (rowCount(index.parent()) == 1);
98 NetworkId netId = index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
99 BufferId bufferuid = index.data(NetworkModel::BufferIdRole).value<BufferId>();
101 if(buffers.contains(bufferuid)) {
102 buffers.remove(bufferuid);
105 networks.remove(netId);
106 Q_ASSERT(!networks.contains(netId));
115 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
116 BufferItem::Type bufferType = (BufferItem::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
118 if((mode & NoChannels) && bufferType == BufferItem::ChannelType)
120 if((mode & NoQueries) && bufferType == BufferItem::QueryType)
122 if((mode & NoServers) && bufferType == BufferItem::StatusType)
125 // bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
126 // if((mode & NoActive) && isActive)
128 // if((mode & NoInactive) && !isActive)
131 if((mode & FullCustom)) {
132 BufferId bufferuid = source_bufferIndex.data(NetworkModel::BufferIdRole).value<BufferId>();
133 return buffers.contains(bufferuid);
139 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
140 NetworkId net = source_index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
141 return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
144 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
145 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
147 if(!child.isValid()) {
148 qDebug() << "filterAcceptsRow has been called with an invalid Child";
152 if(source_parent == QModelIndex())
153 return filterAcceptNetwork(child);
155 return filterAcceptBuffer(child);
158 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const {
159 int lefttype = left.data(NetworkModel::BufferTypeRole).toInt();
160 int righttype = right.data(NetworkModel::BufferTypeRole).toInt();
162 if(lefttype != righttype)
163 return lefttype < righttype;
165 return QSortFilterProxyModel::lessThan(left, right);
168 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
169 if(role == Qt::ForegroundRole)
170 return foreground(index);
172 return QSortFilterProxyModel::data(index, role);
175 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
176 if(!index.data(NetworkModel::ItemActiveRole).toBool())
177 return QColor(Qt::gray);
179 // FIXME:: show colors depending on activity level
180 return QColor(Qt::black);