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 "graphicalui.h"
33 #include "iconloader.h"
34 #include "networkmodel.h"
37 class CheckRemovalEvent : public QEvent {
39 CheckRemovalEvent(const QModelIndex &source_index) : QEvent(QEvent::User), index(source_index) {};
40 QPersistentModelIndex index;
43 /*****************************************
44 * The Filter for the Tree View
45 *****************************************/
46 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *config)
47 : QSortFilterProxyModel(model),
50 _sortOrder(Qt::AscendingOrder),
51 _showServerQueries(false),
53 _enableEditMode(tr("Show / Hide Chats"), this)
56 setSourceModel(model);
58 setDynamicSortFilter(true);
60 connect(this, SIGNAL(_dataChanged(QModelIndex,QModelIndex)),
61 this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
63 _enableEditMode.setCheckable(true);
64 _enableEditMode.setChecked(_editMode);
65 connect(&_enableEditMode, SIGNAL(toggled(bool)), this, SLOT(enableEditMode(bool)));
67 BufferSettings defaultSettings;
68 defaultSettings.notify("ServerNoticesTarget", this, SLOT(showServerQueriesChanged()));
69 showServerQueriesChanged();
72 void BufferViewFilter::setConfig(BufferViewConfig *config) {
76 #if QT_VERSION >= 0x040600
80 _tmpConfig = config; // don't invalidate the old config before we're initialized
82 if(!config || config->isInitialized()) {
85 // we use a queued connection here since manipulating the connection list of a sending object
86 // doesn't seem to be such a good idea while executing a connected slots.
87 connect(config, SIGNAL(initDone()), this, SLOT(configInitialized()), Qt::QueuedConnection);
88 //invalidate(); // not needed as we still have the old config and will reset once init is done
92 void BufferViewFilter::configInitialized() {
94 disconnect(_config, 0, this, 0);
101 connect(config(), SIGNAL(configChanged()), this, SLOT(invalidate()));
102 disconnect(config(), SIGNAL(initDone()), this, SLOT(configInitialized()));
103 setObjectName(config()->bufferViewName());
108 // not resetting the model can trigger bug #663 for some reason I haven't understood yet
109 // we get invalid model indexes in attached views even if no source model has been set yet... wtf?
110 #if QT_VERSION >= 0x040600
115 emit configChanged();
118 void BufferViewFilter::showServerQueriesChanged() {
119 BufferSettings bufferSettings;
121 bool showQueries = (bufferSettings.serverNoticesTarget() & BufferSettings::DefaultBuffer);
122 if(_showServerQueries != showQueries) {
123 _showServerQueries = showQueries;
128 QList<QAction *> BufferViewFilter::actions(const QModelIndex &index) {
130 QList<QAction *> actionList;
131 actionList << &_enableEditMode;
135 void BufferViewFilter::enableEditMode(bool enable) {
136 if(_editMode == enable) {
144 if(enable == false) {
145 addBuffers(QList<BufferId>::fromSet(_toAdd));
146 QSet<BufferId>::const_iterator iter;
147 for(iter = _toTempRemove.constBegin(); iter != _toTempRemove.constEnd(); iter++) {
148 if(config()->temporarilyRemovedBuffers().contains(*iter))
150 config()->requestRemoveBuffer(*iter);
152 for(iter = _toRemove.constBegin(); iter != _toRemove.constEnd(); iter++) {
153 if(config()->removedBuffers().contains(*iter))
155 config()->requestRemoveBufferPermanently(*iter);
159 _toTempRemove.clear();
166 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
167 QModelIndex source_index = mapToSource(index);
168 Qt::ItemFlags flags = sourceModel()->flags(source_index);
170 NetworkModel::ItemType itemType = (NetworkModel::ItemType)sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt();
171 BufferInfo::Type bufferType = (BufferInfo::Type)sourceModel()->data(source_index, NetworkModel::BufferTypeRole).toInt();
172 if(source_index == QModelIndex() || itemType == NetworkModel::NetworkItemType) {
173 flags |= Qt::ItemIsDropEnabled;
174 } else if(_editMode) {
175 flags |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
178 // prohibit dragging of most items. and most drop places
179 // only query to query is allowed for merging
180 if(bufferType != BufferInfo::QueryBuffer) {
181 ClientBufferViewConfig *clientConf = qobject_cast<ClientBufferViewConfig *>(config());
182 if(clientConf && clientConf->isLocked()) {
183 flags &= ~(Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled);
190 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
191 if(!config() || !NetworkModel::mimeContainsBufferList(data))
192 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
194 NetworkId droppedNetworkId;
195 QModelIndex source_parent = mapToSource(parent);
196 if(sourceModel()->data(source_parent, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
197 droppedNetworkId = sourceModel()->data(source_parent, NetworkModel::NetworkIdRole).value<NetworkId>();
199 QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
203 for(int i = 0; i < bufferList.count(); i++) {
204 networkId = bufferList[i].first;
205 bufferId = bufferList[i].second;
206 if(droppedNetworkId == networkId) {
210 if(row < rowCount(parent)) {
211 QModelIndex source_child = mapToSource(index(row, 0, parent));
212 BufferId beforeBufferId = sourceModel()->data(source_child, NetworkModel::BufferIdRole).value<BufferId>();
213 pos = config()->bufferList().indexOf(beforeBufferId);
214 if(_sortOrder == Qt::DescendingOrder)
217 if(_sortOrder == Qt::AscendingOrder)
218 pos = config()->bufferList().count();
223 if(config()->bufferList().contains(bufferId) && !config()->sortAlphabetically()) {
224 if(config()->bufferList().indexOf(bufferId) < pos)
226 ClientBufferViewConfig *clientConf = qobject_cast<ClientBufferViewConfig *>(config());
227 if(!clientConf || !clientConf->isLocked())
228 config()->requestMoveBuffer(bufferId, pos);
230 config()->requestAddBuffer(bufferId, pos);
240 void BufferViewFilter::sort(int column, Qt::SortOrder order) {
242 QSortFilterProxyModel::sort(column, order);
245 void BufferViewFilter::addBuffer(const BufferId &bufferId) const {
246 if(!config() || config()->bufferList().contains(bufferId))
249 int pos = config()->bufferList().count();
251 for(int i = 0; i < config()->bufferList().count(); i++) {
252 if(config() && config()->sortAlphabetically())
253 lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
255 lt = bufferId < config()->bufferList()[i];
262 config()->requestAddBuffer(bufferId, pos);
265 void BufferViewFilter::addBuffers(const QList<BufferId> &bufferIds) const {
269 QList<BufferId> bufferList = config()->bufferList();
270 foreach(BufferId bufferId, bufferIds) {
271 if(bufferList.contains(bufferId))
274 int pos = bufferList.count();
276 for(int i = 0; i < bufferList.count(); i++) {
277 if(config() && config()->sortAlphabetically())
278 lt = bufferIdLessThan(bufferId, bufferList[i]);
280 lt = bufferId < config()->bufferList()[i];
284 bufferList.insert(pos, bufferId);
288 config()->requestAddBuffer(bufferId, pos);
292 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
293 // no config -> "all buffers" -> accept everything
297 BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
298 Q_ASSERT(bufferId.isValid());
300 int activityLevel = sourceModel()->data(source_bufferIndex, NetworkModel::BufferActivityRole).toInt();
302 if(!config()->bufferList().contains(bufferId) && !_editMode) {
303 // add the buffer if...
304 if(config()->isInitialized()
305 && !config()->removedBuffers().contains(bufferId) // it hasn't been manually removed and either
306 && ((config()->addNewBuffersAutomatically() && !config()->temporarilyRemovedBuffers().contains(bufferId)) // is totally unknown to us (a new buffer)...
307 || (config()->temporarilyRemovedBuffers().contains(bufferId) && activityLevel > BufferInfo::OtherActivity))) { // or was just temporarily hidden and has a new message waiting for us.
310 // note: adding the buffer to the valid list does not temper with the following filters ("show only channels" and stuff)
314 if(config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
317 int allowedBufferTypes = config()->allowedBufferTypes();
318 if(!config()->networkId().isValid())
319 allowedBufferTypes &= ~BufferInfo::StatusBuffer;
320 int bufferType = sourceModel()->data(source_bufferIndex, NetworkModel::BufferTypeRole).toInt();
321 if(!(allowedBufferTypes & bufferType))
324 if(bufferType & BufferInfo::QueryBuffer && !_showServerQueries && sourceModel()->data(source_bufferIndex, Qt::DisplayRole).toString().contains('.')) {
328 // the following dynamic filters may not trigger if the buffer is currently selected.
329 QModelIndex currentIndex = Client::bufferModel()->standardSelectionModel()->currentIndex();
330 if(bufferId == Client::bufferModel()->data(currentIndex, NetworkModel::BufferIdRole).value<BufferId>())
333 if(config()->hideInactiveBuffers() && !sourceModel()->data(source_bufferIndex, NetworkModel::ItemActiveRole).toBool() && activityLevel <= BufferInfo::OtherActivity)
336 if(config()->minimumActivity() > activityLevel)
342 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
346 if(!config()->networkId().isValid()) {
349 return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
353 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
354 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
356 if(!child.isValid()) {
357 qWarning() << "filterAcceptsRow has been called with an invalid Child";
361 NetworkModel::ItemType childType = (NetworkModel::ItemType)sourceModel()->data(child, NetworkModel::ItemTypeRole).toInt();
363 case NetworkModel::NetworkItemType:
364 return filterAcceptNetwork(child);
365 case NetworkModel::BufferItemType:
366 return filterAcceptBuffer(child);
372 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
373 int leftItemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
374 int rightItemType = sourceModel()->data(source_right, NetworkModel::ItemTypeRole).toInt();
375 int itemType = leftItemType & rightItemType;
377 case NetworkModel::NetworkItemType:
378 return networkLessThan(source_left, source_right);
379 case NetworkModel::BufferItemType:
380 return bufferLessThan(source_left, source_right);
382 return QSortFilterProxyModel::lessThan(source_left, source_right);
386 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
387 BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
388 BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
390 int leftPos = config()->bufferList().indexOf(leftBufferId);
391 int rightPos = config()->bufferList().indexOf(rightBufferId);
392 if(leftPos == -1 && rightPos == -1)
393 return QSortFilterProxyModel::lessThan(source_left, source_right);
394 if(leftPos == -1 || rightPos == -1)
395 return !(leftPos < rightPos);
396 return leftPos < rightPos;
398 return bufferIdLessThan(leftBufferId, rightBufferId);
401 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
402 NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
403 NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
405 return QSortFilterProxyModel::lessThan(source_left, source_right);
408 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
411 case Qt::ForegroundRole:
412 case Qt::BackgroundRole:
413 case Qt::DecorationRole:
414 if((config() && config()->disableDecoration()))
416 return GraphicalUi::uiStyle()->bufferViewItemData(mapToSource(index), role);
417 case Qt::CheckStateRole:
418 return checkedState(index);
420 return QSortFilterProxyModel::data(index, role);
424 QVariant BufferViewFilter::checkedState(const QModelIndex &index) const {
425 if(!_editMode || !config())
428 QModelIndex source_index = mapToSource(index);
429 if(source_index == QModelIndex() || sourceModel()->data(source_index, NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
432 BufferId bufferId = sourceModel()->data(source_index, NetworkModel::BufferIdRole).value<BufferId>();
433 if(_toAdd.contains(bufferId))
436 if(_toTempRemove.contains(bufferId))
437 return Qt::PartiallyChecked;
439 if(_toRemove.contains(bufferId))
440 return Qt::Unchecked;
442 if(config()->bufferList().contains(bufferId))
445 if(config()->temporarilyRemovedBuffers().contains(bufferId))
446 return Qt::PartiallyChecked;
448 return Qt::Unchecked;
451 bool BufferViewFilter::setData(const QModelIndex &index, const QVariant &value, int role) {
453 case Qt::CheckStateRole:
454 return setCheckedState(index, Qt::CheckState(value.toInt()));
456 return QSortFilterProxyModel::setData(index, value, role);
460 bool BufferViewFilter::setCheckedState(const QModelIndex &index, Qt::CheckState state) {
461 QModelIndex source_index = mapToSource(index);
462 BufferId bufferId = sourceModel()->data(source_index, NetworkModel::BufferIdRole).value<BufferId>();
463 if(!bufferId.isValid())
468 _toAdd.remove(bufferId);
469 _toTempRemove.remove(bufferId);
470 _toRemove << bufferId;
472 case Qt::PartiallyChecked:
473 _toAdd.remove(bufferId);
474 _toTempRemove << bufferId;
475 _toRemove.remove(bufferId);
479 _toTempRemove.remove(bufferId);
480 _toRemove.remove(bufferId);
485 emit dataChanged(index, index);
489 void BufferViewFilter::checkPreviousCurrentForRemoval(const QModelIndex ¤t, const QModelIndex &previous) {
491 if(previous.isValid())
492 QCoreApplication::postEvent(this, new CheckRemovalEvent(previous));
495 void BufferViewFilter::customEvent(QEvent *event) {
496 if(event->type() != QEvent::User)
499 CheckRemovalEvent *removalEvent = static_cast<CheckRemovalEvent *>(event);
500 checkItemForRemoval(removalEvent->index);
505 void BufferViewFilter::checkItemsForRemoval(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
506 QModelIndex source_topLeft = mapToSource(topLeft);
507 QModelIndex source_bottomRight = mapToSource(bottomRight);
508 emit _dataChanged(source_topLeft, source_bottomRight);
511 bool BufferViewFilter::bufferIdLessThan(const BufferId &left, const BufferId &right) {
512 Q_CHECK_PTR(Client::networkModel());
513 if(!Client::networkModel())
516 QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
517 QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
519 int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
520 int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
522 if(leftType != rightType)
523 return leftType < rightType;
525 return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;