1 /***************************************************************************
2 * Copyright (C) 2005-07 by the Quassel IRC Team *
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"
23 #include "networkmodel.h"
25 /*****************************************
26 * The Filter for the Tree View
27 *****************************************/
28 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QList<uint> &nets)
29 : QSortFilterProxyModel(model),
31 networks(QSet<uint>::fromList(nets))
33 setSourceModel(model);
34 setSortCaseSensitivity(Qt::CaseInsensitive);
37 // ok the following basically sucks. therfore it's commented out. Justice served.
38 // a better solution would use dataChanged()
40 // I have this feeling that this resulted in a fuckup once... no clue though right now and invalidateFilter isn't a slot -.-
41 //connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidate()));
42 // connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateFilter_()));
45 void BufferViewFilter::invalidateFilter_() {
46 QSortFilterProxyModel::invalidateFilter();
49 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
50 Qt::ItemFlags flags = mapToSource(index).flags();
51 if(mode & FullCustom) {
52 if(index == QModelIndex() || index.parent() == QModelIndex())
53 flags |= Qt::ItemIsDropEnabled;
58 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
59 // drops have to occur in the open field
60 if(parent != QModelIndex())
61 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
63 if(!NetworkModel::mimeContainsBufferList(data))
66 QList< QPair<uint, uint> > bufferList = NetworkModel::mimeDataToBufferList(data);
69 for(int i = 0; i < bufferList.count(); i++) {
70 netId = bufferList[i].first;
71 bufferId = bufferList[i].second;
72 if(!networks.contains(netId)) {
80 void BufferViewFilter::addBuffer(const uint &bufferuid) {
81 if(!buffers.contains(bufferuid)) {
87 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
88 if(!(mode & FullCustom))
89 return; // only custom buffers can be customized... obviously... :)
91 if(index.parent() == QModelIndex())
92 return; // only child elements can be deleted
94 bool lastBuffer = (rowCount(index.parent()) == 1);
95 uint netId = index.data(NetworkModel::NetworkIdRole).toUInt();
96 uint bufferuid = index.data(NetworkModel::BufferUidRole).toUInt();
98 if(buffers.contains(bufferuid)) {
99 buffers.remove(bufferuid);
102 networks.remove(netId);
103 Q_ASSERT(!networks.contains(netId));
112 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
113 Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
115 if((mode & NoChannels) && bufferType == Buffer::ChannelType)
117 if((mode & NoQueries) && bufferType == Buffer::QueryType)
119 if((mode & NoServers) && bufferType == Buffer::StatusType)
122 // bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
123 // if((mode & NoActive) && isActive)
125 // if((mode & NoInactive) && !isActive)
128 if((mode & FullCustom)) {
129 uint bufferuid = source_bufferIndex.data(NetworkModel::BufferUidRole).toUInt();
130 return buffers.contains(bufferuid);
136 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
137 uint net = source_index.data(NetworkModel::NetworkIdRole).toUInt();
138 return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
141 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
142 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
144 if(!child.isValid()) {
145 qDebug() << "filterAcceptsRow has been called with an invalid Child";
149 if(source_parent == QModelIndex())
150 return filterAcceptNetwork(child);
152 return filterAcceptBuffer(child);
155 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const {
156 int lefttype = left.data(NetworkModel::BufferTypeRole).toInt();
157 int righttype = right.data(NetworkModel::BufferTypeRole).toInt();
159 if(lefttype != righttype)
160 return lefttype < righttype;
162 return QSortFilterProxyModel::lessThan(left, right);