1 /***************************************************************************
2 * Copyright (C) 2005-08 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 #include "uisettings.h"
36 class CheckRemovalEvent : public QEvent {
38 CheckRemovalEvent(const QModelIndex &source_index) : QEvent(QEvent::User), index(source_index) {};
39 QPersistentModelIndex index;
42 /*****************************************
43 * The Filter for the Tree View
44 *****************************************/
45 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *config)
46 : QSortFilterProxyModel(model),
48 _sortOrder(Qt::AscendingOrder),
49 _userOfflineIcon(SmallIcon("user-offline")),
50 _userAwayIcon(SmallIcon("user-away")),
51 _userOnlineIcon(SmallIcon("user-online"))
54 setSourceModel(model);
56 setDynamicSortFilter(true);
60 connect(this, SIGNAL(_dataChanged(const QModelIndex &, const QModelIndex &)),
61 this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
63 BufferSettings bufferSettings;
64 _showUserStateIcons = bufferSettings.showUserStateIcons();
65 bufferSettings.notify("ShowUserStateIcons", this, SLOT(showUserStateIconsChanged()));
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>();
77 void BufferViewFilter::showUserStateIconsChanged() {
78 BufferSettings bufferSettings;
79 _showUserStateIcons = bufferSettings.showUserStateIcons();
82 void BufferViewFilter::setConfig(BufferViewConfig *config) {
87 disconnect(_config, 0, this, 0);
97 if(config->isInitialized()) {
100 connect(config, SIGNAL(initDone()), this, SLOT(configInitialized()));
105 void BufferViewFilter::configInitialized() {
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()));
122 disconnect(config(), SIGNAL(initDone()), this, SLOT(configInitialized()));
125 emit configChanged();
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;
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);
139 NetworkId droppedNetworkId;
140 if(parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
141 droppedNetworkId = parent.data(NetworkModel::NetworkIdRole).value<NetworkId>();
143 QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
147 for(int i = 0; i < bufferList.count(); i++) {
148 networkId = bufferList[i].first;
149 bufferId = bufferList[i].second;
150 if(droppedNetworkId == networkId) {
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)
160 if(_sortOrder == Qt::AscendingOrder)
161 pos = config()->bufferList().count();
166 if(config()->bufferList().contains(bufferId)) {
167 if(config()->bufferList().indexOf(bufferId) < pos)
169 config()->requestMoveBuffer(bufferId, pos);
171 config()->requestAddBuffer(bufferId, pos);
181 void BufferViewFilter::sort(int column, Qt::SortOrder order) {
183 QSortFilterProxyModel::sort(column, order);
186 void BufferViewFilter::addBuffer(const BufferId &bufferId) const {
187 if(!config() || config()->bufferList().contains(bufferId))
190 int pos = config()->bufferList().count();
192 for(int i = 0; i < config()->bufferList().count(); i++) {
193 if(config() && config()->sortAlphabetically())
194 lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
196 lt = bufferId < config()->bufferList()[i];
203 config()->requestAddBuffer(bufferId, pos);
206 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
207 // no config -> "all buffers" -> accept everything
211 BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
212 Q_ASSERT(bufferId.isValid());
214 int activityLevel = source_bufferIndex.data(NetworkModel::BufferActivityRole).toInt();
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.
223 // note: adding the buffer to the valid list does not temper with the following filters ("show only channels" and stuff)
227 if(config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
230 if(!(config()->allowedBufferTypes() & (BufferInfo::Type)source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt()))
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>())
237 if(config()->hideInactiveBuffers() && !source_bufferIndex.data(NetworkModel::ItemActiveRole).toBool() && activityLevel <= BufferInfo::OtherActivity)
240 if(config()->minimumActivity() > activityLevel)
246 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
250 if(!config()->networkId().isValid()) {
253 return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
257 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
258 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
260 if(!child.isValid()) {
261 qWarning() << "filterAcceptsRow has been called with an invalid Child";
265 if(!source_parent.isValid())
266 return filterAcceptNetwork(child);
268 return filterAcceptBuffer(child);
271 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
272 int itemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
274 case NetworkModel::NetworkItemType:
275 return networkLessThan(source_left, source_right);
276 case NetworkModel::BufferItemType:
277 return bufferLessThan(source_left, source_right);
279 return QSortFilterProxyModel::lessThan(source_left, source_right);
283 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
284 BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
285 BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
287 return config()->bufferList().indexOf(leftBufferId) < config()->bufferList().indexOf(rightBufferId);
289 return bufferIdLessThan(leftBufferId, rightBufferId);
292 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
293 NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
294 NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
296 if(config() && config()->sortAlphabetically())
297 return QSortFilterProxyModel::lessThan(source_left, source_right);
299 return leftNetworkId < rightNetworkId;
302 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
304 case Qt::DecorationRole:
306 case Qt::ForegroundRole:
307 return foreground(index);
309 return QSortFilterProxyModel::data(index, role);
313 QVariant BufferViewFilter::icon(const QModelIndex &index) const {
314 if(!_showUserStateIcons)
317 if(index.column() != 0)
320 if(index.data(NetworkModel::BufferTypeRole).toInt() != BufferInfo::QueryBuffer)
323 if(!index.data(NetworkModel::ItemActiveRole).toBool())
324 return _userOfflineIcon;
326 if(index.data(NetworkModel::UserAwayRole).toBool())
327 return _userAwayIcon;
329 return _userOnlineIcon;
334 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
335 BufferInfo::ActivityLevel activity = (BufferInfo::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
337 if(activity & BufferInfo::Highlight)
338 return _FgColorHighlightActivity;
339 if(activity & BufferInfo::NewMessage)
340 return _FgColorNewMessageActivity;
341 if(activity & BufferInfo::OtherActivity)
342 return _FgColorOtherActivity;
344 if(!index.data(NetworkModel::ItemActiveRole).toBool() || index.data(NetworkModel::UserAwayRole).toBool())
345 return _FgColorInactiveActivity;
347 return _FgColorNoActivity;
350 void BufferViewFilter::checkPreviousCurrentForRemoval(const QModelIndex ¤t, const QModelIndex &previous) {
352 if(previous.isValid())
353 QCoreApplication::postEvent(this, new CheckRemovalEvent(previous));
356 void BufferViewFilter::customEvent(QEvent *event) {
357 if(event->type() != QEvent::User)
360 CheckRemovalEvent *removalEvent = static_cast<CheckRemovalEvent *>(event);
361 checkItemForRemoval(removalEvent->index);
366 void BufferViewFilter::checkItemsForRemoval(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
367 QModelIndex source_topLeft = mapToSource(topLeft);
368 QModelIndex source_bottomRight = mapToSource(bottomRight);
369 emit _dataChanged(source_topLeft, source_bottomRight);
372 // ******************************
374 // ******************************
375 bool bufferIdLessThan(const BufferId &left, const BufferId &right) {
376 Q_CHECK_PTR(Client::networkModel());
377 if(!Client::networkModel())
380 QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
381 QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
383 int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
384 int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
386 if(leftType != rightType)
387 return leftType < rightType;
389 return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;