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 <QCoreApplication>
25 #include "buffermodel.h"
27 #include "networkmodel.h"
29 #include "uisettings.h"
31 class CheckRemovalEvent : public QEvent {
33 CheckRemovalEvent(const QModelIndex &source_index) : QEvent(QEvent::User), index(source_index) {};
34 QPersistentModelIndex index;
37 /*****************************************
38 * The Filter for the Tree View
39 *****************************************/
40 BufferViewFilter::BufferViewFilter(QAbstractItemModel *model, BufferViewConfig *config)
41 : QSortFilterProxyModel(model),
43 _sortOrder(Qt::AscendingOrder)
46 setSourceModel(model);
48 setDynamicSortFilter(true);
52 connect(this, SIGNAL(_dataChanged(const QModelIndex &, const QModelIndex &)),
53 this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
56 void BufferViewFilter::loadColors() {
57 UiSettings s("QtUi/Colors");
58 _FgColorInactiveActivity = s.value("inactiveActivityFG", QVariant(QColor(Qt::gray))).value<QColor>();
59 _FgColorNoActivity = s.value("noActivityFG", QVariant(QColor(Qt::black))).value<QColor>();
60 _FgColorHighlightActivity = s.value("highlightActivityFG", QVariant(QColor(Qt::magenta))).value<QColor>();
61 _FgColorNewMessageActivity = s.value("newMessageActivityFG", QVariant(QColor(Qt::green))).value<QColor>();
62 _FgColorOtherActivity = s.value("otherActivityFG", QVariant(QColor(Qt::darkGreen))).value<QColor>();
65 void BufferViewFilter::setConfig(BufferViewConfig *config) {
70 disconnect(_config, 0, this, 0);
80 if(config->isInitialized()) {
83 connect(config, SIGNAL(initDone()), this, SLOT(configInitialized()));
88 void BufferViewFilter::configInitialized() {
92 connect(config(), SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(invalidate()));
93 connect(config(), SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(invalidate()));
94 connect(config(), SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(invalidate()));
95 connect(config(), SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(invalidate()));
96 connect(config(), SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(invalidate()));
97 connect(config(), SIGNAL(allowedBufferTypesSet(int)), this, SLOT(invalidate()));
98 connect(config(), SIGNAL(minimumActivitySet(int)), this, SLOT(invalidate()));
99 connect(config(), SIGNAL(bufferListSet()), this, SLOT(invalidate()));
100 connect(config(), SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(invalidate()));
101 connect(config(), SIGNAL(bufferMoved(const BufferId &, int)), this, SLOT(invalidate()));
102 connect(config(), SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(invalidate()));
103 connect(config(), SIGNAL(bufferPermanentlyRemoved(const BufferId &)), this, SLOT(invalidate()));
105 disconnect(config(), SIGNAL(initDone()), this, SLOT(configInitialized()));
108 emit configChanged();
111 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
112 Qt::ItemFlags flags = mapToSource(index).flags();
113 if(_config && (index == QModelIndex() || index.parent() == QModelIndex()))
114 flags |= Qt::ItemIsDropEnabled;
118 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
119 if(!config() || !NetworkModel::mimeContainsBufferList(data))
120 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
122 NetworkId droppedNetworkId;
123 if(parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
124 droppedNetworkId = parent.data(NetworkModel::NetworkIdRole).value<NetworkId>();
126 QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
130 for(int i = 0; i < bufferList.count(); i++) {
131 networkId = bufferList[i].first;
132 bufferId = bufferList[i].second;
133 if(droppedNetworkId == networkId) {
137 if(row < rowCount(parent)) {
138 BufferId beforeBufferId = parent.child(row, 0).data(NetworkModel::BufferIdRole).value<BufferId>();
139 pos = config()->bufferList().indexOf(beforeBufferId);
140 if(_sortOrder == Qt::DescendingOrder)
143 if(_sortOrder == Qt::AscendingOrder)
144 pos = config()->bufferList().count();
149 if(config()->bufferList().contains(bufferId)) {
150 if(config()->bufferList().indexOf(bufferId) < pos)
152 config()->requestMoveBuffer(bufferId, pos);
154 config()->requestAddBuffer(bufferId, pos);
164 void BufferViewFilter::sort(int column, Qt::SortOrder order) {
166 QSortFilterProxyModel::sort(column, order);
169 void BufferViewFilter::addBuffer(const BufferId &bufferId) const {
170 if(!config() || config()->bufferList().contains(bufferId))
173 int pos = config()->bufferList().count();
175 for(int i = 0; i < config()->bufferList().count(); i++) {
176 if(config() && config()->sortAlphabetically())
177 lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
179 lt = bufferId < config()->bufferList()[i];
186 config()->requestAddBuffer(bufferId, pos);
189 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
190 // no config -> "all buffers" -> accept everything
194 BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
195 Q_ASSERT(bufferId.isValid());
197 int activityLevel = source_bufferIndex.data(NetworkModel::BufferActivityRole).toInt();
199 if(!config()->bufferList().contains(bufferId)) {
200 // add the buffer if...
201 if(config()->isInitialized() && !config()->removedBuffers().contains(bufferId) // it hasn't been manually removed and either
202 && ((config()->addNewBuffersAutomatically() && !config()->temporarilyRemovedBuffers().contains(bufferId)) // is totally unknown to us (a new buffer)...
203 || (config()->temporarilyRemovedBuffers().contains(bufferId) && activityLevel > Buffer::OtherActivity))) { // or was just temporarily hidden and has a new message waiting for us.
206 // note: adding the buffer to the valid list does not temper with the following filters ("show only channels" and stuff)
210 if(config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
213 if(!(config()->allowedBufferTypes() & (BufferInfo::Type)source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt()))
216 if(config()->hideInactiveBuffers() && !source_bufferIndex.data(NetworkModel::ItemActiveRole).toBool())
219 if(config()->minimumActivity() > activityLevel) {
220 if(bufferId != Client::bufferModel()->standardSelectionModel()->currentIndex().data(NetworkModel::BufferIdRole).value<BufferId>())
227 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
231 if(!config()->networkId().isValid()) {
234 return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
238 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
239 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
241 if(!child.isValid()) {
242 qWarning() << "filterAcceptsRow has been called with an invalid Child";
246 if(!source_parent.isValid())
247 return filterAcceptNetwork(child);
249 return filterAcceptBuffer(child);
252 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
253 int itemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
255 case NetworkModel::NetworkItemType:
256 return networkLessThan(source_left, source_right);
257 case NetworkModel::BufferItemType:
258 return bufferLessThan(source_left, source_right);
260 return QSortFilterProxyModel::lessThan(source_left, source_right);
264 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
265 BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
266 BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
268 return config()->bufferList().indexOf(leftBufferId) < config()->bufferList().indexOf(rightBufferId);
270 return bufferIdLessThan(leftBufferId, rightBufferId);
273 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
274 NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
275 NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
277 if(config() && config()->sortAlphabetically())
278 return QSortFilterProxyModel::lessThan(source_left, source_right);
280 return leftNetworkId < rightNetworkId;
283 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
284 if(role == Qt::ForegroundRole)
285 return foreground(index);
287 return QSortFilterProxyModel::data(index, role);
290 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
291 if(!index.data(NetworkModel::ItemActiveRole).toBool())
292 return _FgColorInactiveActivity;
294 Buffer::ActivityLevel activity = (Buffer::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
296 if(activity & Buffer::Highlight)
297 return _FgColorHighlightActivity;
298 if(activity & Buffer::NewMessage)
299 return _FgColorNewMessageActivity;
300 if(activity & Buffer::OtherActivity)
301 return _FgColorOtherActivity;
303 return _FgColorNoActivity;
306 void BufferViewFilter::checkPreviousCurrentForRemoval(const QModelIndex ¤t, const QModelIndex &previous) {
308 if(previous.isValid())
309 QCoreApplication::postEvent(this, new CheckRemovalEvent(previous));
312 void BufferViewFilter::customEvent(QEvent *event) {
313 if(event->type() != QEvent::User)
316 CheckRemovalEvent *removalEvent = static_cast<CheckRemovalEvent *>(event);
317 checkItemForRemoval(removalEvent->index);
322 void BufferViewFilter::checkItemsForRemoval(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
323 QModelIndex source_topLeft = mapToSource(topLeft);
324 QModelIndex source_bottomRight = mapToSource(bottomRight);
325 emit _dataChanged(source_topLeft, source_bottomRight);
328 // ******************************
330 // ******************************
331 bool bufferIdLessThan(const BufferId &left, const BufferId &right) {
332 Q_CHECK_PTR(Client::networkModel());
333 if(!Client::networkModel())
336 QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
337 QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
339 int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
340 int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
342 if(leftType != rightType)
343 return leftType < rightType;
345 return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;