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);
34 // I have this feeling that this resulted in a fuckup once... no clue though right now and invalidateFilter isn't a slot -.-
35 //connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidate()));
36 connect(model, SIGNAL(invalidateFilter()), this, SLOT(invalidateFilter_()));
39 void BufferViewFilter::invalidateFilter_() {
40 QSortFilterProxyModel::invalidateFilter();
43 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
44 Qt::ItemFlags flags = mapToSource(index).flags();
45 if(mode & FullCustom) {
46 if(index == QModelIndex() || index.parent() == QModelIndex())
47 flags |= Qt::ItemIsDropEnabled;
52 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
53 // drops have to occur in the open field
54 if(parent != QModelIndex())
55 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
57 if(!BufferTreeModel::mimeContainsBufferList(data))
60 QList< QPair<uint, uint> > bufferList = BufferTreeModel::mimeDataToBufferList(data);
63 for(int i = 0; i < bufferList.count(); i++) {
64 netId = bufferList[i].first;
65 bufferId = bufferList[i].second;
72 void BufferViewFilter::addBuffer(const uint &bufferuid) {
73 if(!buffers.contains(bufferuid)) {
79 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
80 if(!(mode & FullCustom))
81 return; // only custom buffers can be customized... obviously... :)
83 if(index.parent() == QModelIndex())
84 return; // only child elements can be deleted
86 bool lastBuffer = (rowCount(index.parent()) == 1);
87 uint netId = index.data(BufferTreeModel::NetworkIdRole).toUInt();
88 uint bufferuid = index.data(BufferTreeModel::BufferUidRole).toUInt();
89 if(buffers.contains(bufferuid)) {
90 buffers.remove(bufferuid);
92 networks.remove(netId);
99 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
100 Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(BufferTreeModel::BufferTypeRole).toInt();
102 if((mode & NoChannels) && bufferType == Buffer::ChannelType)
104 if((mode & NoQueries) && bufferType == Buffer::QueryType)
106 if((mode & NoServers) && bufferType == Buffer::StatusType)
109 // bool isActive = source_bufferIndex.data(BufferTreeModel::BufferActiveRole).toBool();
110 // if((mode & NoActive) && isActive)
112 // if((mode & NoInactive) && !isActive)
115 // if((mode & FullCustom)) {
116 // uint bufferuid = source_bufferIndex.data(BufferTreeModel::BufferUidRole).toUInt();
117 // if(!buffers.contains(bufferuid))
124 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
125 uint net = source_index.data(BufferTreeModel::NetworkIdRole).toUInt();
126 return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
129 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
130 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
132 if(!child.isValid()) {
133 qDebug() << "filterAcceptsRow has been called with an invalid Child";
137 if(source_parent == QModelIndex())
138 return filterAcceptNetwork(child);
140 return filterAcceptBuffer(child);
143 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const {
144 int lefttype = left.data(BufferTreeModel::BufferTypeRole).toInt();
145 int righttype = right.data(BufferTreeModel::BufferTypeRole).toInt();
147 if(lefttype != righttype)
148 return lefttype < righttype;
150 return QSortFilterProxyModel::lessThan(left, right);