Merging r730:732 from trunk to branches/0.3. Forwardports fancy bufferviews.
[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 "client.h"
26 #include "networkmodel.h"
27
28 #include "uisettings.h"
29
30 /*****************************************
31 * The Filter for the Tree View
32 *****************************************/
33 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *config)
34   : QSortFilterProxyModel(model),
35     _config(0)
36 {
37   setConfig(config);
38   setSourceModel(model);
39   // setSortCaseSensitivity(Qt::CaseInsensitive);
40   setDynamicSortFilter(true);
41 }
42
43 void BufferViewFilter::setConfig(BufferViewConfig *config) {
44   if(_config == config)
45     return;
46   
47   if(_config) {
48     disconnect(_config, 0, this, 0);
49   }
50
51   _config = config;
52   if(config) {
53     connect(config, SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(invalidate()));
54     connect(config, SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(invalidate()));
55     connect(config, SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(invalidate()));
56     connect(config, SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(invalidate()));
57     connect(config, SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(invalidate()));
58     connect(config, SIGNAL(allowedBufferTypesSet(int)), this, SLOT(invalidate()));
59     connect(config, SIGNAL(minimumActivitySet(int)), this, SLOT(invalidate()));
60     connect(config, SIGNAL(bufferListSet()), this, SLOT(invalidate()));
61     connect(config, SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(invalidate()));
62     connect(config, SIGNAL(bufferMoved(const BufferId &, int)), this, SLOT(invalidate()));
63     connect(config, SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(invalidate()));
64   }
65   invalidate();
66 }
67
68 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
69   Qt::ItemFlags flags = mapToSource(index).flags();
70   if(_config && index == QModelIndex() || index.parent() == QModelIndex())
71     flags |= Qt::ItemIsDropEnabled;
72   return flags;
73 }
74
75 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
76   if(!config() || !NetworkModel::mimeContainsBufferList(data))
77     return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
78
79   NetworkId droppedNetworkId;
80   if(parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
81     droppedNetworkId = parent.data(NetworkModel::NetworkIdRole).value<NetworkId>();
82
83   QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
84   BufferId bufferId;
85   NetworkId networkId;
86   int pos;
87   for(int i = 0; i < bufferList.count(); i++) {
88     networkId = bufferList[i].first;
89     bufferId = bufferList[i].second;
90     if(droppedNetworkId == networkId) {
91       if(row < rowCount(parent)) {
92         BufferId beforeBufferId = parent.child(row, 0).data(NetworkModel::BufferIdRole).value<BufferId>();
93         pos = config()->bufferList().indexOf(beforeBufferId);
94       } else {
95         pos = config()->bufferList().count();
96       }
97
98       if(config()->bufferList().contains(bufferId)) {
99         if(config()->bufferList().indexOf(bufferId) < pos)
100           pos--;
101         config()->requestMoveBuffer(bufferId, pos);
102       } else {
103         config()->requestAddBuffer(bufferId, pos);
104       }
105
106     } else {
107       addBuffer(bufferId);
108     }
109   }
110   return true;
111 }
112
113 void BufferViewFilter::addBuffer(const BufferId &bufferId) {
114   if(config()->bufferList().contains(bufferId))
115     return;
116   
117   int pos = config()->bufferList().count();
118   bool lt;
119   for(int i = 0; i < config()->bufferList().count(); i++) {
120     if(config() && config()->sortAlphabetically())
121       lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
122     else
123       lt = bufferId < config()->bufferList()[i];
124     
125     if(lt) {
126       pos = i;
127       break;
128     }
129   }
130   config()->requestAddBuffer(bufferId, pos);
131 }
132
133 void BufferViewFilter::removeBuffer(const QModelIndex &index) {
134   if(!config())
135     return;
136   
137   BufferId bufferId = data(index, NetworkModel::BufferIdRole).value<BufferId>();
138   config()->requestRemoveBuffer(bufferId);
139 }
140
141
142 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
143   if(!_config)
144     return true;
145   
146   if(!(_config->allowedBufferTypes() & (BufferInfo::Type)source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt()))
147     return false;
148
149   if(_config->hideInactiveBuffers() && !source_bufferIndex.data(NetworkModel::ItemActiveRole).toBool())
150     return false;
151
152    if(_config->minimumActivity() > source_bufferIndex.data(NetworkModel::BufferActivityRole).toInt())
153     return false;
154
155    BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
156    return _config->bufferList().contains(bufferId);
157 }
158
159 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
160   if(!config())
161     return true;
162
163   if(!config()->networkId().isValid()) {
164     return true;
165   } else {
166     return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
167   }
168 }
169
170 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
171   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
172   
173   if(!child.isValid()) {
174     qWarning() << "filterAcceptsRow has been called with an invalid Child";
175     return false;
176   }
177
178   if(source_parent == QModelIndex())
179     return filterAcceptNetwork(child);
180   else
181     return filterAcceptBuffer(child);
182 }
183
184 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
185   int itemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
186   switch(itemType) {
187   case NetworkModel::NetworkItemType:
188     return networkLessThan(source_left, source_right);
189   case NetworkModel::BufferItemType:
190     return bufferLessThan(source_left, source_right);
191   default:
192     return QSortFilterProxyModel::lessThan(source_left, source_right);    
193   }
194 }
195
196 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
197   BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
198   BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
199   if(config()) {
200     return config()->bufferList().indexOf(leftBufferId) < config()->bufferList().indexOf(rightBufferId);
201   } else
202     return leftBufferId < rightBufferId;
203 }
204
205 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
206   NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
207   NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
208
209   if(config() && config()->sortAlphabetically())
210     return QSortFilterProxyModel::lessThan(source_left, source_right);
211   else
212     return leftNetworkId < rightNetworkId;
213 }
214
215 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
216   if(role == Qt::ForegroundRole)
217     return foreground(index);
218   else
219     return QSortFilterProxyModel::data(index, role);
220 }
221
222 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
223   UiSettings s("QtUi/Colors");
224   QVariant inactiveActivity = s.value("inactiveActivityFG", QVariant(QColor(Qt::gray)));
225   QVariant noActivity = s.value("noActivityFG", QVariant(QColor(Qt::black)));
226   QVariant highlightActivity = s.value("highlightActivityFG", QVariant(QColor(Qt::magenta)));
227   QVariant newMessageActivity = s.value("newMessageActivityFG", QVariant(QColor(Qt::green)));
228   QVariant otherActivity = s.value("otherActivityFG", QVariant(QColor(Qt::darkGreen)));
229
230   if(!index.data(NetworkModel::ItemActiveRole).toBool())
231     return inactiveActivity;
232
233   Buffer::ActivityLevel activity = (Buffer::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
234
235   if(activity & Buffer::Highlight)
236     return highlightActivity;
237   if(activity & Buffer::NewMessage)
238     return newMessageActivity;
239   if(activity & Buffer::OtherActivity)
240     return otherActivity;
241
242   return noActivity;
243
244 }
245
246
247 // ******************************
248 //  Helper
249 // ******************************
250 bool bufferIdLessThan(const BufferId &left, const BufferId &right) {
251   Q_CHECK_PTR(Client::networkModel());
252   if(!Client::networkModel())
253     return true;
254   
255   QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
256   QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
257
258   int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
259   int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
260
261   if(leftType != rightType)
262     return leftType < rightType;
263   else
264     return Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString() < Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString();
265 }
266