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 /*****************************************
24 * The Filter for the Tree View
25 *****************************************/
26 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QList<uint> &nets)
27 : QSortFilterProxyModel(model),
29 networks(QSet<uint>::fromList(nets))
31 setSourceModel(model);
32 setSortCaseSensitivity(Qt::CaseInsensitive);
35 // ok the following basically sucks. therfore it's commented out. Justice served.
36 // a better solution would use dataChanged()
38 // I have this feeling that this resulted in a fuckup once... no clue though right now and invalidateFilter isn't a slot -.-
39 //connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidate()));
40 // connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateFilter_()));
43 void BufferViewFilter::invalidateFilter_() {
44 QSortFilterProxyModel::invalidateFilter();
47 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
48 Qt::ItemFlags flags = mapToSource(index).flags();
49 if(mode & FullCustom) {
50 if(index == QModelIndex() || index.parent() == QModelIndex())
51 flags |= Qt::ItemIsDropEnabled;
56 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
57 // drops have to occur in the open field
58 if(parent != QModelIndex())
59 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
61 if(!BufferTreeModel::mimeContainsBufferList(data))
64 QList< QPair<uint, uint> > bufferList = BufferTreeModel::mimeDataToBufferList(data);
67 for(int i = 0; i < bufferList.count(); i++) {
68 netId = bufferList[i].first;
69 bufferId = bufferList[i].second;
70 if(!networks.contains(netId)) {
78 void BufferViewFilter::addBuffer(const uint &bufferuid) {
79 if(!buffers.contains(bufferuid)) {
85 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
86 if(!(mode & FullCustom))
87 return; // only custom buffers can be customized... obviously... :)
89 if(index.parent() == QModelIndex())
90 return; // only child elements can be deleted
92 bool lastBuffer = (rowCount(index.parent()) == 1);
93 uint netId = index.data(BufferTreeModel::NetworkIdRole).toUInt();
94 uint bufferuid = index.data(BufferTreeModel::BufferUidRole).toUInt();
96 if(buffers.contains(bufferuid)) {
97 buffers.remove(bufferuid);
100 networks.remove(netId);
101 Q_ASSERT(!networks.contains(netId));
110 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
111 Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(BufferTreeModel::BufferTypeRole).toInt();
113 if((mode & NoChannels) && bufferType == Buffer::ChannelType)
115 if((mode & NoQueries) && bufferType == Buffer::QueryType)
117 if((mode & NoServers) && bufferType == Buffer::StatusType)
120 // bool isActive = source_bufferIndex.data(BufferTreeModel::BufferActiveRole).toBool();
121 // if((mode & NoActive) && isActive)
123 // if((mode & NoInactive) && !isActive)
126 if((mode & FullCustom)) {
127 uint bufferuid = source_bufferIndex.data(BufferTreeModel::BufferUidRole).toUInt();
128 return buffers.contains(bufferuid);
134 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
135 uint net = source_index.data(BufferTreeModel::NetworkIdRole).toUInt();
136 return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
139 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
140 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
142 if(!child.isValid()) {
143 qDebug() << "filterAcceptsRow has been called with an invalid Child";
147 if(source_parent == QModelIndex())
148 return filterAcceptNetwork(child);
150 return filterAcceptBuffer(child);
153 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const {
154 int lefttype = left.data(BufferTreeModel::BufferTypeRole).toInt();
155 int righttype = right.data(BufferTreeModel::BufferTypeRole).toInt();
157 if(lefttype != righttype)
158 return lefttype < righttype;
160 return QSortFilterProxyModel::lessThan(left, right);