1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
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 #include <QApplication>
27 #include "bufferinfo.h"
28 #include "buffermodel.h"
29 #include "buffersettings.h"
31 #include "iconloader.h"
32 #include "networkmodel.h"
34 class CheckRemovalEvent : public QEvent {
36 CheckRemovalEvent(const QModelIndex &source_index) : QEvent(QEvent::User), index(source_index) {};
37 QPersistentModelIndex index;
40 /*****************************************
41 * The Filter for the Tree View
42 *****************************************/
43 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *config)
44 : QSortFilterProxyModel(model),
46 _sortOrder(Qt::AscendingOrder),
47 _userOfflineIcon(SmallIcon("im-user-offline")),
48 _userAwayIcon(SmallIcon("im-user-away")),
49 _userOnlineIcon(SmallIcon("im-user")),
51 _enableEditMode(tr("Show / Hide buffers"), this)
54 setSourceModel(model);
56 setDynamicSortFilter(true);
58 connect(this, SIGNAL(_dataChanged(const QModelIndex &, const QModelIndex &)),
59 this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
61 _enableEditMode.setCheckable(true);
62 _enableEditMode.setChecked(_editMode);
63 connect(&_enableEditMode, SIGNAL(toggled(bool)), this, SLOT(enableEditMode(bool)));
65 BufferSettings bufferSettings;
66 _showUserStateIcons = bufferSettings.showUserStateIcons();
67 bufferSettings.notify("ShowUserStateIcons", this, SLOT(showUserStateIconsChanged()));
70 void BufferViewFilter::showUserStateIconsChanged() {
71 BufferSettings bufferSettings;
72 _showUserStateIcons = bufferSettings.showUserStateIcons();
75 void BufferViewFilter::setConfig(BufferViewConfig *config) {
80 disconnect(_config, 0, this, 0);
91 if(config->isInitialized()) {
94 connect(config, SIGNAL(initDone()), this, SLOT(configInitialized()));
99 void BufferViewFilter::configInitialized() {
103 connect(config(), SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(invalidate()));
104 connect(config(), SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(invalidate()));
105 connect(config(), SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(invalidate()));
106 connect(config(), SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(invalidate()));
107 connect(config(), SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(invalidate()));
108 connect(config(), SIGNAL(allowedBufferTypesSet(int)), this, SLOT(invalidate()));
109 connect(config(), SIGNAL(minimumActivitySet(int)), this, SLOT(invalidate()));
110 connect(config(), SIGNAL(bufferListSet()), this, SLOT(invalidate()));
111 connect(config(), SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(invalidate()));
112 connect(config(), SIGNAL(bufferMoved(const BufferId &, int)), this, SLOT(invalidate()));
113 connect(config(), SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(invalidate()));
114 connect(config(), SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(invalidate()));
116 disconnect(config(), SIGNAL(initDone()), this, SLOT(configInitialized()));
118 setObjectName(config()->bufferViewName());
121 emit configChanged();
124 QList<QAction *> BufferViewFilter::actions(const QModelIndex &index) {
126 QList<QAction *> actionList;
127 actionList << &_enableEditMode;
131 void BufferViewFilter::enableEditMode(bool enable) {
132 if(_editMode == enable) {
140 if(enable == false) {
141 int numBuffers = config()->bufferList().count();
142 QSet<BufferId>::const_iterator iter;
143 for(iter = _toAdd.constBegin(); iter != _toAdd.constEnd(); iter++) {
144 if(config()->bufferList().contains(*iter))
146 config()->requestAddBuffer(*iter, numBuffers);
148 for(iter = _toTempRemove.constBegin(); iter != _toTempRemove.constEnd(); iter++) {
149 if(config()->temporarilyRemovedBuffers().contains(*iter))
151 config()->requestRemoveBuffer(*iter);
153 for(iter = _toRemove.constBegin(); iter != _toRemove.constEnd(); iter++) {
154 if(config()->removedBuffers().contains(*iter))
156 config()->requestRemoveBufferPermanently(*iter);
160 _toTempRemove.clear();
167 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
168 QModelIndex source_index = mapToSource(index);
169 Qt::ItemFlags flags = sourceModel()->flags(source_index);
171 if(source_index == QModelIndex() || sourceModel()->data(source_index, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
172 flags |= Qt::ItemIsDropEnabled;
173 } else if(_editMode) {
174 flags |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
180 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
181 if(!config() || !NetworkModel::mimeContainsBufferList(data))
182 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
184 NetworkId droppedNetworkId;
185 QModelIndex source_parent = mapToSource(parent);
186 if(sourceModel()->data(source_parent, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
187 droppedNetworkId = sourceModel()->data(source_parent, NetworkModel::NetworkIdRole).value<NetworkId>();
189 QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
193 for(int i = 0; i < bufferList.count(); i++) {
194 networkId = bufferList[i].first;
195 bufferId = bufferList[i].second;
196 if(droppedNetworkId == networkId) {
200 if(row < rowCount(parent)) {
201 QModelIndex source_child = mapToSource(index(row, 0, parent));
202 BufferId beforeBufferId = sourceModel()->data(source_child, NetworkModel::BufferIdRole).value<BufferId>();
203 pos = config()->bufferList().indexOf(beforeBufferId);
204 if(_sortOrder == Qt::DescendingOrder)
207 if(_sortOrder == Qt::AscendingOrder)
208 pos = config()->bufferList().count();
213 if(config()->bufferList().contains(bufferId)) {
214 if(config()->bufferList().indexOf(bufferId) < pos)
216 config()->requestMoveBuffer(bufferId, pos);
218 config()->requestAddBuffer(bufferId, pos);
228 void BufferViewFilter::sort(int column, Qt::SortOrder order) {
230 QSortFilterProxyModel::sort(column, order);
233 void BufferViewFilter::addBuffer(const BufferId &bufferId) const {
234 if(!config() || config()->bufferList().contains(bufferId))
237 int pos = config()->bufferList().count();
239 for(int i = 0; i < config()->bufferList().count(); i++) {
240 if(config() && config()->sortAlphabetically())
241 lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
243 lt = bufferId < config()->bufferList()[i];
250 config()->requestAddBuffer(bufferId, pos);
253 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
254 // no config -> "all buffers" -> accept everything
258 BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
259 Q_ASSERT(bufferId.isValid());
261 int activityLevel = sourceModel()->data(source_bufferIndex, NetworkModel::BufferActivityRole).toInt();
263 if(!config()->bufferList().contains(bufferId) && !_editMode) {
264 // add the buffer if...
265 if(config()->isInitialized()
266 && !config()->removedBuffers().contains(bufferId) // it hasn't been manually removed and either
267 && ((config()->addNewBuffersAutomatically() && !config()->temporarilyRemovedBuffers().contains(bufferId)) // is totally unknown to us (a new buffer)...
268 || (config()->temporarilyRemovedBuffers().contains(bufferId) && activityLevel > BufferInfo::OtherActivity))) { // or was just temporarily hidden and has a new message waiting for us.
271 // note: adding the buffer to the valid list does not temper with the following filters ("show only channels" and stuff)
275 if(config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
278 int allowedBufferTypes = config()->allowedBufferTypes();
279 if(!config()->networkId().isValid())
280 allowedBufferTypes &= ~BufferInfo::StatusBuffer;
281 if(!(allowedBufferTypes & sourceModel()->data(source_bufferIndex, NetworkModel::BufferTypeRole).toInt()))
284 // the following dynamic filters may not trigger if the buffer is currently selected.
285 QModelIndex currentIndex = Client::bufferModel()->standardSelectionModel()->currentIndex();
286 if(bufferId == Client::bufferModel()->data(currentIndex, NetworkModel::BufferIdRole).value<BufferId>())
289 if(config()->hideInactiveBuffers() && !sourceModel()->data(source_bufferIndex, NetworkModel::ItemActiveRole).toBool() && activityLevel <= BufferInfo::OtherActivity)
292 if(config()->minimumActivity() > activityLevel)
298 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
302 if(!config()->networkId().isValid()) {
305 return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
309 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
310 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
312 if(!child.isValid()) {
313 qWarning() << "filterAcceptsRow has been called with an invalid Child";
317 NetworkModel::ItemType childType = (NetworkModel::ItemType)sourceModel()->data(child, NetworkModel::ItemTypeRole).toInt();
319 case NetworkModel::NetworkItemType:
320 return filterAcceptNetwork(child);
321 case NetworkModel::BufferItemType:
322 return filterAcceptBuffer(child);
328 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
329 int leftItemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
330 int rightItemType = sourceModel()->data(source_right, NetworkModel::ItemTypeRole).toInt();
331 int itemType = leftItemType & rightItemType;
333 case NetworkModel::NetworkItemType:
334 return networkLessThan(source_left, source_right);
335 case NetworkModel::BufferItemType:
336 return bufferLessThan(source_left, source_right);
338 return QSortFilterProxyModel::lessThan(source_left, source_right);
342 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
343 BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
344 BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
346 int leftPos = config()->bufferList().indexOf(leftBufferId);
347 int rightPos = config()->bufferList().indexOf(rightBufferId);
348 if(leftPos == -1 && rightPos == -1)
349 return QSortFilterProxyModel::lessThan(source_left, source_right);
350 if(leftPos == -1 || rightPos == -1)
351 return !(leftPos < rightPos);
352 return leftPos < rightPos;
354 return bufferIdLessThan(leftBufferId, rightBufferId);
357 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
358 NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
359 NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
361 if(config() && config()->sortAlphabetically())
362 return QSortFilterProxyModel::lessThan(source_left, source_right);
364 return leftNetworkId < rightNetworkId;
367 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
369 case Qt::DecorationRole:
371 case Qt::CheckStateRole:
372 return checkedState(index);
374 return QSortFilterProxyModel::data(index, role);
378 QVariant BufferViewFilter::icon(const QModelIndex &index) const {
379 if(!_showUserStateIcons || (config() && config()->disableDecoration()))
382 if(index.column() != 0)
385 QModelIndex source_index = mapToSource(index);
386 if(sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt() != NetworkModel::BufferItemType)
389 if(sourceModel()->data(source_index, NetworkModel::BufferTypeRole).toInt() != BufferInfo::QueryBuffer)
392 if(!sourceModel()->data(source_index, NetworkModel::ItemActiveRole).toBool())
393 return _userOfflineIcon;
395 if(sourceModel()->data(source_index, NetworkModel::UserAwayRole).toBool())
396 return _userAwayIcon;
398 return _userOnlineIcon;
403 QVariant BufferViewFilter::checkedState(const QModelIndex &index) const {
404 if(!_editMode || !config())
407 QModelIndex source_index = mapToSource(index);
408 if(source_index == QModelIndex() || sourceModel()->data(source_index, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
411 BufferId bufferId = sourceModel()->data(source_index, NetworkModel::BufferIdRole).value<BufferId>();
412 if(_toAdd.contains(bufferId))
415 if(_toTempRemove.contains(bufferId))
416 return Qt::PartiallyChecked;
418 if(_toRemove.contains(bufferId))
419 return Qt::Unchecked;
421 if(config()->bufferList().contains(bufferId))
424 if(config()->temporarilyRemovedBuffers().contains(bufferId))
425 return Qt::PartiallyChecked;
427 return Qt::Unchecked;
430 bool BufferViewFilter::setData(const QModelIndex &index, const QVariant &value, int role) {
432 case Qt::CheckStateRole:
433 return setCheckedState(index, Qt::CheckState(value.toInt()));
435 return QSortFilterProxyModel::setData(index, value, role);
439 bool BufferViewFilter::setCheckedState(const QModelIndex &index, Qt::CheckState state) {
440 QModelIndex source_index = mapToSource(index);
441 BufferId bufferId = sourceModel()->data(source_index, NetworkModel::BufferIdRole).value<BufferId>();
442 if(!bufferId.isValid())
447 _toAdd.remove(bufferId);
448 _toTempRemove.remove(bufferId);
449 _toRemove << bufferId;
451 case Qt::PartiallyChecked:
452 _toAdd.remove(bufferId);
453 _toTempRemove << bufferId;
454 _toRemove.remove(bufferId);
458 _toTempRemove.remove(bufferId);
459 _toRemove.remove(bufferId);
464 emit dataChanged(index, index);
468 void BufferViewFilter::checkPreviousCurrentForRemoval(const QModelIndex ¤t, const QModelIndex &previous) {
470 if(previous.isValid())
471 QCoreApplication::postEvent(this, new CheckRemovalEvent(previous));
474 void BufferViewFilter::customEvent(QEvent *event) {
475 if(event->type() != QEvent::User)
478 CheckRemovalEvent *removalEvent = static_cast<CheckRemovalEvent *>(event);
479 checkItemForRemoval(removalEvent->index);
484 void BufferViewFilter::checkItemsForRemoval(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
485 QModelIndex source_topLeft = mapToSource(topLeft);
486 QModelIndex source_bottomRight = mapToSource(bottomRight);
487 emit _dataChanged(source_topLeft, source_bottomRight);
490 bool BufferViewFilter::bufferIdLessThan(const BufferId &left, const BufferId &right) {
491 Q_CHECK_PTR(Client::networkModel());
492 if(!Client::networkModel())
495 QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
496 QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
498 int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
499 int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
501 if(leftType != rightType)
502 return leftType < rightType;
504 return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;