Add a property disableDecoration to BufferViewConfig
[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 <QApplication>
24 #include <QPalette>
25 #include <QBrush>
26
27 #include "bufferinfo.h"
28 #include "buffermodel.h"
29 #include "buffersettings.h"
30 #include "client.h"
31 #include "iconloader.h"
32 #include "networkmodel.h"
33
34 #include "uisettings.h"
35
36 class CheckRemovalEvent : public QEvent {
37 public:
38   CheckRemovalEvent(const QModelIndex &source_index) : QEvent(QEvent::User), index(source_index) {};
39   QPersistentModelIndex index;
40 };
41
42 /*****************************************
43 * The Filter for the Tree View
44 *****************************************/
45 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *config)
46   : QSortFilterProxyModel(model),
47     _config(0),
48     _sortOrder(Qt::AscendingOrder),
49     _userOfflineIcon(SmallIcon("user-offline")),
50     _userAwayIcon(SmallIcon("user-away")),
51     _userOnlineIcon(SmallIcon("user-online"))
52 {
53   setConfig(config);
54   setSourceModel(model);
55
56   setDynamicSortFilter(true);
57
58   loadColors();
59
60   connect(this, SIGNAL(_dataChanged(const QModelIndex &, const QModelIndex &)),
61           this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
62
63   BufferSettings bufferSettings;
64   _showUserStateIcons = bufferSettings.showUserStateIcons();
65   bufferSettings.notify("ShowUserStateIcons", this, SLOT(showUserStateIconsChanged()));
66 }
67
68 void BufferViewFilter::loadColors() {
69   UiSettings s("QtUiStyle/Colors");
70   _FgColorInactiveActivity = s.value("inactiveActivityFG", QVariant(QColor(Qt::gray))).value<QColor>();
71   _FgColorNoActivity = s.value("noActivityFG", QVariant(QColor(Qt::black))).value<QColor>();
72   _FgColorHighlightActivity = s.value("highlightActivityFG", QVariant(QColor(Qt::magenta))).value<QColor>();
73   _FgColorNewMessageActivity = s.value("newMessageActivityFG", QVariant(QColor(Qt::green))).value<QColor>();
74   _FgColorOtherActivity = s.value("otherActivityFG", QVariant(QColor(Qt::darkGreen))).value<QColor>();
75 }
76
77 void BufferViewFilter::showUserStateIconsChanged() {
78   BufferSettings bufferSettings;
79   _showUserStateIcons = bufferSettings.showUserStateIcons();
80 }
81
82 void BufferViewFilter::setConfig(BufferViewConfig *config) {
83   if(_config == config)
84     return;
85
86   if(_config) {
87     disconnect(_config, 0, this, 0);
88   }
89
90   _config = config;
91
92   if(!config) {
93     invalidate();
94     return;
95   }
96
97   if(config->isInitialized()) {
98     configInitialized();
99   } else {
100     connect(config, SIGNAL(initDone()), this, SLOT(configInitialized()));
101     invalidate();
102   }
103 }
104
105 void BufferViewFilter::configInitialized() {
106   if(!config())
107     return;
108
109   connect(config(), SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(invalidate()));
110   connect(config(), SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(invalidate()));
111   connect(config(), SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(invalidate()));
112   connect(config(), SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(invalidate()));
113   connect(config(), SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(invalidate()));
114   connect(config(), SIGNAL(allowedBufferTypesSet(int)), this, SLOT(invalidate()));
115   connect(config(), SIGNAL(minimumActivitySet(int)), this, SLOT(invalidate()));
116   connect(config(), SIGNAL(bufferListSet()), this, SLOT(invalidate()));
117   connect(config(), SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(invalidate()));
118   connect(config(), SIGNAL(bufferMoved(const BufferId &, int)), this, SLOT(invalidate()));
119   connect(config(), SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(invalidate()));
120   connect(config(), SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(invalidate()));
121
122   disconnect(config(), SIGNAL(initDone()), this, SLOT(configInitialized()));
123
124   invalidate();
125   emit configChanged();
126 }
127
128 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
129   Qt::ItemFlags flags = mapToSource(index).flags();
130   if(_config && (index == QModelIndex() || index.parent() == QModelIndex()))
131     flags |= Qt::ItemIsDropEnabled;
132   return flags;
133 }
134
135 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
136   if(!config() || !NetworkModel::mimeContainsBufferList(data))
137     return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
138
139   NetworkId droppedNetworkId;
140   if(parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
141     droppedNetworkId = parent.data(NetworkModel::NetworkIdRole).value<NetworkId>();
142
143   QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
144   BufferId bufferId;
145   NetworkId networkId;
146   int pos;
147   for(int i = 0; i < bufferList.count(); i++) {
148     networkId = bufferList[i].first;
149     bufferId = bufferList[i].second;
150     if(droppedNetworkId == networkId) {
151       if(row < 0)
152         row = 0;
153
154       if(row < rowCount(parent)) {
155         BufferId beforeBufferId = parent.child(row, 0).data(NetworkModel::BufferIdRole).value<BufferId>();
156         pos = config()->bufferList().indexOf(beforeBufferId);
157         if(_sortOrder == Qt::DescendingOrder)
158           pos++;
159       } else {
160         if(_sortOrder == Qt::AscendingOrder)
161           pos = config()->bufferList().count();
162         else
163           pos = 0;
164       }
165
166       if(config()->bufferList().contains(bufferId)) {
167         if(config()->bufferList().indexOf(bufferId) < pos)
168           pos--;
169         config()->requestMoveBuffer(bufferId, pos);
170       } else {
171         config()->requestAddBuffer(bufferId, pos);
172       }
173
174     } else {
175       addBuffer(bufferId);
176     }
177   }
178   return true;
179 }
180
181 void BufferViewFilter::sort(int column, Qt::SortOrder order) {
182   _sortOrder = order;
183   QSortFilterProxyModel::sort(column, order);
184 }
185
186 void BufferViewFilter::addBuffer(const BufferId &bufferId) const {
187   if(!config() || config()->bufferList().contains(bufferId))
188     return;
189
190   int pos = config()->bufferList().count();
191   bool lt;
192   for(int i = 0; i < config()->bufferList().count(); i++) {
193     if(config() && config()->sortAlphabetically())
194       lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
195     else
196       lt = bufferId < config()->bufferList()[i];
197
198     if(lt) {
199       pos = i;
200       break;
201     }
202   }
203   config()->requestAddBuffer(bufferId, pos);
204 }
205
206 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
207   // no config -> "all buffers" -> accept everything
208   if(!config())
209     return true;
210
211   BufferId bufferId = source_bufferIndex.data(NetworkModel::BufferIdRole).value<BufferId>();
212   Q_ASSERT(bufferId.isValid());
213
214   int activityLevel = source_bufferIndex.data(NetworkModel::BufferActivityRole).toInt();
215
216   if(!config()->bufferList().contains(bufferId)) {
217     // add the buffer if...
218     if(config()->isInitialized() && !config()->removedBuffers().contains(bufferId) // it hasn't been manually removed and either
219        && ((config()->addNewBuffersAutomatically() && !config()->temporarilyRemovedBuffers().contains(bufferId)) // is totally unknown to us (a new buffer)...
220            || (config()->temporarilyRemovedBuffers().contains(bufferId) && activityLevel > BufferInfo::OtherActivity))) { // or was just temporarily hidden and has a new message waiting for us.
221       addBuffer(bufferId);
222     }
223     // note: adding the buffer to the valid list does not temper with the following filters ("show only channels" and stuff)
224     return false;
225   }
226
227   if(config()->networkId().isValid() && config()->networkId() != source_bufferIndex.data(NetworkModel::NetworkIdRole).value<NetworkId>())
228     return false;
229
230   if(!(config()->allowedBufferTypes() & (BufferInfo::Type)source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt()))
231     return false;
232
233   // the following dynamic filters may not trigger if the buffer is currently selected.
234   if(bufferId == Client::bufferModel()->standardSelectionModel()->currentIndex().data(NetworkModel::BufferIdRole).value<BufferId>())
235     return true;
236
237   if(config()->hideInactiveBuffers() && !source_bufferIndex.data(NetworkModel::ItemActiveRole).toBool() && activityLevel <= BufferInfo::OtherActivity)
238     return false;
239
240   if(config()->minimumActivity() > activityLevel)
241     return false;
242
243   return true;
244 }
245
246 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
247   if(!config())
248     return true;
249
250   if(!config()->networkId().isValid()) {
251     return true;
252   } else {
253     return config()->networkId() == source_index.data(NetworkModel::NetworkIdRole).value<NetworkId>();
254   }
255 }
256
257 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
258   QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
259
260   if(!child.isValid()) {
261     qWarning() << "filterAcceptsRow has been called with an invalid Child";
262     return false;
263   }
264
265   if(!source_parent.isValid())
266     return filterAcceptNetwork(child);
267   else
268     return filterAcceptBuffer(child);
269 }
270
271 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
272   int itemType = source_left.data(NetworkModel::ItemTypeRole).toInt();
273   switch(itemType) {
274   case NetworkModel::NetworkItemType:
275     return networkLessThan(source_left, source_right);
276   case NetworkModel::BufferItemType:
277     return bufferLessThan(source_left, source_right);
278   default:
279     return QSortFilterProxyModel::lessThan(source_left, source_right);
280   }
281 }
282
283 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
284   BufferId leftBufferId = source_left.data(NetworkModel::BufferIdRole).value<BufferId>();
285   BufferId rightBufferId = source_right.data(NetworkModel::BufferIdRole).value<BufferId>();
286   if(config()) {
287     return config()->bufferList().indexOf(leftBufferId) < config()->bufferList().indexOf(rightBufferId);
288   } else
289     return bufferIdLessThan(leftBufferId, rightBufferId);
290 }
291
292 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
293   NetworkId leftNetworkId = source_left.data(NetworkModel::NetworkIdRole).value<NetworkId>();
294   NetworkId rightNetworkId = source_right.data(NetworkModel::NetworkIdRole).value<NetworkId>();
295
296   if(config() && config()->sortAlphabetically())
297     return QSortFilterProxyModel::lessThan(source_left, source_right);
298   else
299     return leftNetworkId < rightNetworkId;
300 }
301
302 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
303   switch(role) {
304   case Qt::DecorationRole:
305     return icon(index);
306   case Qt::ForegroundRole:
307     return foreground(index);
308   default:
309     return QSortFilterProxyModel::data(index, role);
310   }
311 }
312
313 QVariant BufferViewFilter::icon(const QModelIndex &index) const {
314   if(!_showUserStateIcons || config() && config()->disableDecoration())
315     return QVariant();
316
317   if(index.column() != 0)
318     return QVariant();
319
320   if(index.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::QueryBuffer)
321     return QVariant();
322
323   if(!index.data(NetworkModel::ItemActiveRole).toBool())
324     return _userOfflineIcon;
325
326   if(index.data(NetworkModel::UserAwayRole).toBool())
327     return _userAwayIcon;
328   else
329     return _userOnlineIcon;
330
331   return QVariant();
332 }
333
334 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
335   if(config() && config()->disableDecoration())
336     return _FgColorNoActivity;
337
338   BufferInfo::ActivityLevel activity = (BufferInfo::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
339
340   if(activity & BufferInfo::Highlight)
341     return _FgColorHighlightActivity;
342   if(activity & BufferInfo::NewMessage)
343     return _FgColorNewMessageActivity;
344   if(activity & BufferInfo::OtherActivity)
345     return _FgColorOtherActivity;
346
347   if(!index.data(NetworkModel::ItemActiveRole).toBool() || index.data(NetworkModel::UserAwayRole).toBool())
348     return _FgColorInactiveActivity;
349
350   return _FgColorNoActivity;
351 }
352
353 void BufferViewFilter::checkPreviousCurrentForRemoval(const QModelIndex &current, const QModelIndex &previous) {
354   Q_UNUSED(current);
355   if(previous.isValid())
356     QCoreApplication::postEvent(this, new CheckRemovalEvent(previous));
357 }
358
359 void BufferViewFilter::customEvent(QEvent *event) {
360   if(event->type() != QEvent::User)
361     return;
362
363   CheckRemovalEvent *removalEvent = static_cast<CheckRemovalEvent *>(event);
364   checkItemForRemoval(removalEvent->index);
365
366   event->accept();
367 }
368
369 void BufferViewFilter::checkItemsForRemoval(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
370   QModelIndex source_topLeft = mapToSource(topLeft);
371   QModelIndex source_bottomRight = mapToSource(bottomRight);
372   emit _dataChanged(source_topLeft, source_bottomRight);
373 }
374
375 // ******************************
376 //  Helper
377 // ******************************
378 bool bufferIdLessThan(const BufferId &left, const BufferId &right) {
379   Q_CHECK_PTR(Client::networkModel());
380   if(!Client::networkModel())
381     return true;
382
383   QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
384   QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
385
386   int leftType = leftIndex.data(NetworkModel::BufferTypeRole).toInt();
387   int rightType = rightIndex.data(NetworkModel::BufferTypeRole).toInt();
388
389   if(leftType != rightType)
390     return leftType < rightType;
391   else
392     return QString::compare(leftIndex.data(Qt::DisplayRole).toString(), rightIndex.data(Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;
393 }
394