- Improved the speed of IrcServerHandler (and other BasicHandler
[quassel.git] / src / uisupport / bufferviewfilter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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 <QColor>
24
25 #include "networkmodel.h"
26
27 /*****************************************
28 * The Filter for the Tree View
29 *****************************************/
30 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, const Modes &filtermode, const QList<uint> &nets)
31   : QSortFilterProxyModel(model),
32     mode(filtermode),
33     networks(QSet<uint>::fromList(nets))
34 {
35   setSourceModel(model);
36   setSortCaseSensitivity(Qt::CaseInsensitive);
37
38   // FIXME
39   // ok the following basically sucks. therfore it's commented out. Justice served.
40   // a better solution would use dataChanged()
41   
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_()));
45 }
46
47 void BufferViewFilter::invalidateFilter_() {
48   QSortFilterProxyModel::invalidateFilter();
49 }
50
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;
56   }
57   return flags;
58 }
59
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);
64
65   if(!NetworkModel::mimeContainsBufferList(data))
66     return false;
67
68   QList< QPair<uint, uint> > bufferList = NetworkModel::mimeDataToBufferList(data);
69
70   uint netId, bufferId;
71   for(int i = 0; i < bufferList.count(); i++) {
72     netId = bufferList[i].first;
73     bufferId = bufferList[i].second;
74     if(!networks.contains(netId)) {
75       networks << netId;
76     }
77     addBuffer(bufferId);
78   }
79   return true;
80 }
81
82 void BufferViewFilter::addBuffer(const uint &bufferuid) {
83   if(!buffers.contains(bufferuid)) {
84     buffers << bufferuid;
85     invalidateFilter();
86   }
87 }
88
89 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
90   if(!(mode & FullCustom))
91     return; // only custom buffers can be customized... obviously... :)
92   
93   if(index.parent() == QModelIndex())
94     return; // only child elements can be deleted
95
96   bool lastBuffer = (rowCount(index.parent()) == 1);
97   uint netId = index.data(NetworkModel::NetworkIdRole).toUInt();
98   uint bufferuid = index.data(NetworkModel::BufferIdRole).toUInt();
99
100   if(buffers.contains(bufferuid)) {
101     buffers.remove(bufferuid);
102     
103     if(lastBuffer) {
104       networks.remove(netId);
105       Q_ASSERT(!networks.contains(netId));
106     }
107
108     invalidateFilter();
109   }
110   
111 }
112
113
114 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
115   BufferItem::Type bufferType = (BufferItem::Type) source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt();
116   
117   if((mode & NoChannels) && bufferType == BufferItem::ChannelType)
118     return false;
119   if((mode & NoQueries) && bufferType == BufferItem::QueryType)
120     return false;
121   if((mode & NoServers) && bufferType == BufferItem::StatusType)
122     return false;
123
124 //   bool isActive = source_bufferIndex.data(NetworkModel::BufferActiveRole).toBool();
125 //   if((mode & NoActive) && isActive)
126 //     return false;
127 //   if((mode & NoInactive) && !isActive)
128 //     return false;
129
130   if((mode & FullCustom)) {
131     uint bufferuid = source_bufferIndex.data(NetworkModel::BufferIdRole).toUInt();
132     return buffers.contains(bufferuid);
133   }
134     
135   return true;
136 }
137
138 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
139   uint net = source_index.data(NetworkModel::NetworkIdRole).toUInt();
140   return !((mode & (SomeNets | FullCustom)) && !networks.contains(net));
141 }
142
143 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
144   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
145   
146   if(!child.isValid()) {
147     qDebug() << "filterAcceptsRow has been called with an invalid Child";
148     return false;
149   }
150
151   if(source_parent == QModelIndex())
152     return filterAcceptNetwork(child);
153   else
154     return filterAcceptBuffer(child);
155 }
156
157 bool BufferViewFilter::lessThan(const QModelIndex &left, const QModelIndex &right) const {
158   int lefttype = left.data(NetworkModel::BufferTypeRole).toInt();
159   int righttype = right.data(NetworkModel::BufferTypeRole).toInt();
160
161   if(lefttype != righttype)
162     return lefttype < righttype;
163   else
164     return QSortFilterProxyModel::lessThan(left, right);
165 }
166
167 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
168   if(role == Qt::ForegroundRole)
169     return foreground(index);
170   else
171     return QSortFilterProxyModel::data(index, role);
172 }
173
174 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
175   if(!index.data(NetworkModel::ItemActiveRole).toBool())
176     return QColor(Qt::gray);
177
178   // FIXME:: show colors depending on activity level
179   return QColor(Qt::black);
180 }