a5c3d156af991fe79e2d3df74f1321771ba8e6a3
[quassel.git] / src / uisupport / bufferviewfilter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "bufferviewfilter.h"
22
23 #include "networkmodel.h"
24
25 /*****************************************
26 * The Filter for the Tree View
27 *****************************************/
28 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QList<uint> &nets)
29   : QSortFilterProxyModel(model),
30     mode(filtermode),
31     networks(QSet<uint>::fromList(nets))
32 {
33   setSourceModel(model);
34   setSortCaseSensitivity(Qt::CaseInsensitive);
35
36   // FIXME
37   // ok the following basically sucks. therfore it's commented out. Justice served.
38   // a better solution would use dataChanged()
39   
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_()));
43 }
44
45 void BufferViewFilter::invalidateFilter_() {
46   QSortFilterProxyModel::invalidateFilter();
47 }
48
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;
54   }
55   return flags;
56 }
57
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);
62
63   if(!NetworkModel::mimeContainsBufferList(data))
64     return false;
65
66   QList< QPair<uint, uint> > bufferList = NetworkModel::mimeDataToBufferList(data);
67
68   uint netId, bufferId;
69   for(int i = 0; i < bufferList.count(); i++) {
70     netId = bufferList[i].first;
71     bufferId = bufferList[i].second;
72     if(!networks.contains(netId)) {
73       networks << netId;
74     }
75     addBuffer(bufferId);
76   }
77   return true;
78 }
79
80 void BufferViewFilter::addBuffer(const uint &bufferuid) {
81   if(!buffers.contains(bufferuid)) {
82     buffers << bufferuid;
83     invalidateFilter();
84   }
85 }
86
87 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
88   if(!(mode & FullCustom))
89     return; // only custom buffers can be customized... obviously... :)
90   
91   if(index.parent() == QModelIndex())
92     return; // only child elements can be deleted
93
94   bool lastBuffer = (rowCount(index.parent()) == 1);
95   uint netId = index.data(NetworkModel::NetworkIdRole).toUInt();
96   uint bufferuid = index.data(NetworkModel::BufferUidRole).toUInt();
97
98   if(buffers.contains(bufferuid)) {
99     buffers.remove(bufferuid);
100     
101     if(lastBuffer) {
102       networks.remove(netId);
103       Q_ASSERT(!networks.contains(netId));
104     }
105
106     invalidateFilter();
107   }
108   
109 }
110
111
112 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
113   Buffer::Type bufferType = (Buffer::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
114   
115   if((mode & NoChannels) && bufferType == Buffer::ChannelType)
116     return false;
117   if((mode & NoQueries) && bufferType == Buffer::QueryType)
118     return false;
119   if((mode & NoServers) && bufferType == Buffer::StatusType)
120     return false;
121
122 //   bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
123 //   if((mode & NoActive) && isActive)
124 //     return false;
125 //   if((mode & NoInactive) && !isActive)
126 //     return false;
127
128   if((mode & FullCustom)) {
129     uint bufferuid = source_bufferIndex.data(NetworkModel::BufferUidRole).toUInt();
130     return buffers.contains(bufferuid);
131   }
132     
133   return true;
134 }
135
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));
139 }
140
141 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
142   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
143   
144   if(!child.isValid()) {
145     qDebug() << "filterAcceptsRow has been called with an invalid Child";
146     return false;
147   }
148
149   if(source_parent == QModelIndex())
150     return filterAcceptNetwork(child);
151   else
152     return filterAcceptBuffer(child);
153 }
154
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();
158
159   if(lefttype != righttype)
160     return lefttype < righttype;
161   else
162     return QSortFilterProxyModel::lessThan(left, right);
163 }
164