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 "clientbufferviewconfig.h"
32 #include "iconloader.h"
33 #include "networkmodel.h"
35 class CheckRemovalEvent : public QEvent {
37 CheckRemovalEvent(const QModelIndex &source_index) : QEvent(QEvent::User), index(source_index) {};
38 QPersistentModelIndex index;
41 /*****************************************
42 * The Filter for the Tree View
43 *****************************************/
44 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *config)
45 : QSortFilterProxyModel(model),
47 _sortOrder(Qt::AscendingOrder),
48 _userOfflineIcon(SmallIcon("im-user-offline")),
49 _userAwayIcon(SmallIcon("im-user-away")),
50 _userOnlineIcon(SmallIcon("im-user")),
52 _enableEditMode(tr("Show / Hide buffers"), this)
55 setSourceModel(model);
57 setDynamicSortFilter(true);
59 connect(this, SIGNAL(_dataChanged(const QModelIndex &, const QModelIndex &)),
60 this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
62 _enableEditMode.setCheckable(true);
63 _enableEditMode.setChecked(_editMode);
64 connect(&_enableEditMode, SIGNAL(toggled(bool)), this, SLOT(enableEditMode(bool)));
66 BufferSettings bufferSettings;
67 _showUserStateIcons = bufferSettings.showUserStateIcons();
68 bufferSettings.notify("ShowUserStateIcons", this, SLOT(showUserStateIconsChanged()));
71 void BufferViewFilter::showUserStateIconsChanged() {
72 BufferSettings bufferSettings;
73 _showUserStateIcons = bufferSettings.showUserStateIcons();
76 void BufferViewFilter::setConfig(BufferViewConfig *config) {
81 disconnect(_config, 0, this, 0);
92 if(config->isInitialized()) {
95 connect(config, SIGNAL(initDone()), this, SLOT(configInitialized()));
100 void BufferViewFilter::configInitialized() {
104 connect(config(), SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(invalidate()));
105 connect(config(), SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(invalidate()));
106 connect(config(), SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(invalidate()));
107 connect(config(), SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(invalidate()));
108 connect(config(), SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(invalidate()));
109 connect(config(), SIGNAL(allowedBufferTypesSet(int)), this, SLOT(invalidate()));
110 connect(config(), SIGNAL(minimumActivitySet(int)), this, SLOT(invalidate()));
111 connect(config(), SIGNAL(bufferListSet()), this, SLOT(invalidate()));
112 connect(config(), SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(invalidate()));
113 connect(config(), SIGNAL(bufferMoved(const BufferId &, int)), this, SLOT(invalidate()));
114 connect(config(), SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(invalidate()));
115 connect(config(), SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(invalidate()));
117 disconnect(config(), SIGNAL(initDone()), this, SLOT(configInitialized()));
119 setObjectName(config()->bufferViewName());
122 emit configChanged();
125 QList<QAction *> BufferViewFilter::actions(const QModelIndex &index) {
127 QList<QAction *> actionList;
128 actionList << &_enableEditMode;
132 void BufferViewFilter::enableEditMode(bool enable) {
133 if(_editMode == enable) {
141 if(enable == false) {
142 int numBuffers = config()->bufferList().count();
143 QSet<BufferId>::const_iterator iter;
144 for(iter = _toAdd.constBegin(); iter != _toAdd.constEnd(); iter++) {
145 if(config()->bufferList().contains(*iter))
147 config()->requestAddBuffer(*iter, numBuffers);
149 for(iter = _toTempRemove.constBegin(); iter != _toTempRemove.constEnd(); iter++) {
150 if(config()->temporarilyRemovedBuffers().contains(*iter))
152 config()->requestRemoveBuffer(*iter);
154 for(iter = _toRemove.constBegin(); iter != _toRemove.constEnd(); iter++) {
155 if(config()->removedBuffers().contains(*iter))
157 config()->requestRemoveBufferPermanently(*iter);
161 _toTempRemove.clear();
168 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
169 QModelIndex source_index = mapToSource(index);
170 Qt::ItemFlags flags = sourceModel()->flags(source_index);
172 if(source_index == QModelIndex() || sourceModel()->data(source_index, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
173 flags |= Qt::ItemIsDropEnabled;
174 } else if(_editMode) {
175 flags |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
177 ClientBufferViewConfig *clientConf = qobject_cast<ClientBufferViewConfig *>(config());
178 if(clientConf && clientConf->isLocked()) {
179 flags &= ~(Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled);
185 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
186 if(!config() || !NetworkModel::mimeContainsBufferList(data))
187 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
189 NetworkId droppedNetworkId;
190 QModelIndex source_parent = mapToSource(parent);
191 if(sourceModel()->data(source_parent, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
192 droppedNetworkId = sourceModel()->data(source_parent, NetworkModel::NetworkIdRole).value<NetworkId>();
194 QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
198 for(int i = 0; i < bufferList.count(); i++) {
199 networkId = bufferList[i].first;
200 bufferId = bufferList[i].second;
201 if(droppedNetworkId == networkId) {
205 if(row < rowCount(parent)) {
206 QModelIndex source_child = mapToSource(index(row, 0, parent));
207 BufferId beforeBufferId = sourceModel()->data(source_child, NetworkModel::BufferIdRole).value<BufferId>();
208 pos = config()->bufferList().indexOf(beforeBufferId);
209 if(_sortOrder == Qt::DescendingOrder)
212 if(_sortOrder == Qt::AscendingOrder)
213 pos = config()->bufferList().count();
218 if(config()->bufferList().contains(bufferId)) {
219 if(config()->bufferList().indexOf(bufferId) < pos)
221 config()->requestMoveBuffer(bufferId, pos);
223 config()->requestAddBuffer(bufferId, pos);
233 void BufferViewFilter::sort(int column, Qt::SortOrder order) {
235 QSortFilterProxyModel::sort(column, order);
238 void BufferViewFilter::addBuffer(const BufferId &bufferId) const {
239 if(!config() || config()->bufferList().contains(bufferId))
242 int pos = config()->bufferList().count();
244 for(int i = 0; i < config()->bufferList().count(); i++) {
245 if(config() && config()->sortAlphabetically())
246 lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
248 lt = bufferId < config()->bufferList()[i];
255 config()->requestAddBuffer(bufferId, pos);
258 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
259 // no config -> "all buffers" -> accept everything
263 BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
264 Q_ASSERT(bufferId.isValid());
266 int activityLevel = sourceModel()->data(source_bufferIndex, NetworkModel::BufferActivityRole).toInt();
268 if(!config()->bufferList().contains(bufferId) && !_editMode) {
269 // add the buffer if...
270 if(config()->isInitialized()
271 && !config()->removedBuffers().contains(bufferId) // it hasn't been manually removed and either
272 && ((config()->addNewBuffersAutomatically() && !config()->temporarilyRemovedBuffers().contains(bufferId)) // is totally unknown to us (a new buffer)...
273 || (config()->temporarilyRemovedBuffers().contains(bufferId) && activityLevel > BufferInfo::OtherActivity))) { // or was just temporarily hidden and has a new message waiting for us.
276 // note: adding the buffer to the valid list does not temper with the following filters ("show only channels" and stuff)
280 if(config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
283 int allowedBufferTypes = config()->allowedBufferTypes();
284 if(!config()->networkId().isValid())
285 allowedBufferTypes &= ~BufferInfo::StatusBuffer;
286 if(!(allowedBufferTypes & sourceModel()->data(source_bufferIndex, NetworkModel::BufferTypeRole).toInt()))
289 // the following dynamic filters may not trigger if the buffer is currently selected.
290 QModelIndex currentIndex = Client::bufferModel()->standardSelectionModel()->currentIndex();
291 if(bufferId == Client::bufferModel()->data(currentIndex, NetworkModel::BufferIdRole).value<BufferId>())
294 if(config()->hideInactiveBuffers() && !sourceModel()->data(source_bufferIndex, NetworkModel::ItemActiveRole).toBool() && activityLevel <= BufferInfo::OtherActivity)
297 if(config()->minimumActivity() > activityLevel)
303 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
307 if(!config()->networkId().isValid()) {
310 return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
314 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
315 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
317 if(!child.isValid()) {
318 qWarning() << "filterAcceptsRow has been called with an invalid Child";
322 NetworkModel::ItemType childType = (NetworkModel::ItemType)sourceModel()->data(child, NetworkModel::ItemTypeRole).toInt();
324 case NetworkModel::NetworkItemType:
325 return filterAcceptNetwork(child);
326 case NetworkModel::BufferItemType:
327 return filterAcceptBuffer(child);
333 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
334 int leftItemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
335 int rightItemType = sourceModel()->data(source_right, NetworkModel::ItemTypeRole).toInt();
336 int itemType = leftItemType & rightItemType;
338 case NetworkModel::NetworkItemType:
339 return networkLessThan(source_left, source_right);
340 case NetworkModel::BufferItemType:
341 return bufferLessThan(source_left, source_right);
343 return QSortFilterProxyModel::lessThan(source_left, source_right);
347 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
348 BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
349 BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
351 int leftPos = config()->bufferList().indexOf(leftBufferId);
352 int rightPos = config()->bufferList().indexOf(rightBufferId);
353 if(leftPos == -1 && rightPos == -1)
354 return QSortFilterProxyModel::lessThan(source_left, source_right);
355 if(leftPos == -1 || rightPos == -1)
356 return !(leftPos < rightPos);
357 return leftPos < rightPos;
359 return bufferIdLessThan(leftBufferId, rightBufferId);
362 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
363 NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
364 NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
366 return QSortFilterProxyModel::lessThan(source_left, source_right);
369 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
371 case Qt::DecorationRole:
373 case Qt::CheckStateRole:
374 return checkedState(index);
376 return QSortFilterProxyModel::data(index, role);
380 QVariant BufferViewFilter::icon(const QModelIndex &index) const {
381 if(!_showUserStateIcons || (config() && config()->disableDecoration()))
384 if(index.column() != 0)
387 QModelIndex source_index = mapToSource(index);
388 if(sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt() != NetworkModel::BufferItemType)
391 if(sourceModel()->data(source_index, NetworkModel::BufferTypeRole).toInt() != BufferInfo::QueryBuffer)
394 if(!sourceModel()->data(source_index, NetworkModel::ItemActiveRole).toBool())
395 return _userOfflineIcon;
397 if(sourceModel()->data(source_index, NetworkModel::UserAwayRole).toBool())
398 return _userAwayIcon;
400 return _userOnlineIcon;
405 QVariant BufferViewFilter::checkedState(const QModelIndex &index) const {
406 if(!_editMode || !config())
409 QModelIndex source_index = mapToSource(index);
410 if(source_index == QModelIndex() || sourceModel()->data(source_index, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
413 BufferId bufferId = sourceModel()->data(source_index, NetworkModel::BufferIdRole).value<BufferId>();
414 if(_toAdd.contains(bufferId))
417 if(_toTempRemove.contains(bufferId))
418 return Qt::PartiallyChecked;
420 if(_toRemove.contains(bufferId))
421 return Qt::Unchecked;
423 if(config()->bufferList().contains(bufferId))
426 if(config()->temporarilyRemovedBuffers().contains(bufferId))
427 return Qt::PartiallyChecked;
429 return Qt::Unchecked;
432 bool BufferViewFilter::setData(const QModelIndex &index, const QVariant &value, int role) {
434 case Qt::CheckStateRole:
435 return setCheckedState(index, Qt::CheckState(value.toInt()));
437 return QSortFilterProxyModel::setData(index, value, role);
441 bool BufferViewFilter::setCheckedState(const QModelIndex &index, Qt::CheckState state) {
442 QModelIndex source_index = mapToSource(index);
443 BufferId bufferId = sourceModel()->data(source_index, NetworkModel::BufferIdRole).value<BufferId>();
444 if(!bufferId.isValid())
449 _toAdd.remove(bufferId);
450 _toTempRemove.remove(bufferId);
451 _toRemove << bufferId;
453 case Qt::PartiallyChecked:
454 _toAdd.remove(bufferId);
455 _toTempRemove << bufferId;
456 _toRemove.remove(bufferId);
460 _toTempRemove.remove(bufferId);
461 _toRemove.remove(bufferId);
466 emit dataChanged(index, index);
470 void BufferViewFilter::checkPreviousCurrentForRemoval(const QModelIndex ¤t, const QModelIndex &previous) {
472 if(previous.isValid())
473 QCoreApplication::postEvent(this, new CheckRemovalEvent(previous));
476 void BufferViewFilter::customEvent(QEvent *event) {
477 if(event->type() != QEvent::User)
480 CheckRemovalEvent *removalEvent = static_cast<CheckRemovalEvent *>(event);
481 checkItemForRemoval(removalEvent->index);
486 void BufferViewFilter::checkItemsForRemoval(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
487 QModelIndex source_topLeft = mapToSource(topLeft);
488 QModelIndex source_bottomRight = mapToSource(bottomRight);
489 emit _dataChanged(source_topLeft, source_bottomRight);
492 bool BufferViewFilter::bufferIdLessThan(const BufferId &left, const BufferId &right) {
493 Q_CHECK_PTR(Client::networkModel());
494 if(!Client::networkModel())
497 QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
498 QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
500 int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
501 int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
503 if(leftType != rightType)
504 return leftType < rightType;
506 return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;