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),
45 setSourceModel(model);
47 setDynamicSortFilter(true);
51 connect(this, SIGNAL(_dataChanged(const QModelIndex &, const QModelIndex &)),
52 this, SLOT(_q_sourceDataChanged(QModelIndex,QModelIndex)));
55 void BufferViewFilter::loadColors() {
56 UiSettings s("QtUi/Colors");
57 _FgColorInactiveActivity = s.value("inactiveActivityFG", QVariant(QColor(Qt::gray))).value<QColor>();
58 _FgColorNoActivity = s.value("noActivityFG", QVariant(QColor(Qt::black))).value<QColor>();
59 _FgColorHighlightActivity = s.value("highlightActivityFG", QVariant(QColor(Qt::magenta))).value<QColor>();
60 _FgColorNewMessageActivity = s.value("newMessageActivityFG", QVariant(QColor(Qt::green))).value<QColor>();
61 _FgColorOtherActivity = s.value("otherActivityFG", QVariant(QColor(Qt::darkGreen))).value<QColor>();
64 void BufferViewFilter::setConfig(BufferViewConfig *config) {
69 disconnect(_config, 0, this, 0);
73 if(config->isInitialized()) {
76 connect(config, SIGNAL(initDone()), this, SLOT(configInitialized()));
81 void BufferViewFilter::configInitialized() {
85 connect(config(), SIGNAL(bufferViewNameSet(const QString &)), this, SLOT(invalidate()));
86 connect(config(), SIGNAL(networkIdSet(const NetworkId &)), this, SLOT(invalidate()));
87 connect(config(), SIGNAL(addNewBuffersAutomaticallySet(bool)), this, SLOT(invalidate()));
88 connect(config(), SIGNAL(sortAlphabeticallySet(bool)), this, SLOT(invalidate()));
89 connect(config(), SIGNAL(hideInactiveBuffersSet(bool)), this, SLOT(invalidate()));
90 connect(config(), SIGNAL(allowedBufferTypesSet(int)), this, SLOT(invalidate()));
91 connect(config(), SIGNAL(minimumActivitySet(int)), this, SLOT(invalidate()));
92 connect(config(), SIGNAL(bufferListSet()), this, SLOT(invalidate()));
93 connect(config(), SIGNAL(bufferAdded(const BufferId &, int)), this, SLOT(invalidate()));
94 connect(config(), SIGNAL(bufferMoved(const BufferId &, int)), this, SLOT(invalidate()));
95 connect(config(), SIGNAL(bufferRemoved(const BufferId &)), this, SLOT(invalidate()));
97 disconnect(config(), SIGNAL(initDone()), this, SLOT(configInitialized()));
102 Qt::ItemFlags BufferViewFilter::flags(const QModelIndex &index) const {
103 Qt::ItemFlags flags = mapToSource(index).flags();
104 if(_config && index == QModelIndex() || index.parent() == QModelIndex())
105 flags |= Qt::ItemIsDropEnabled;
109 bool BufferViewFilter::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
110 if(!config() || !NetworkModel::mimeContainsBufferList(data))
111 return QSortFilterProxyModel::dropMimeData(data, action, row, column, parent);
113 NetworkId droppedNetworkId;
114 if(parent.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType)
115 droppedNetworkId = parent.data(NetworkModel::NetworkIdRole).value<NetworkId>();
117 QList< QPair<NetworkId, BufferId> > bufferList = NetworkModel::mimeDataToBufferList(data);
121 for(int i = 0; i < bufferList.count(); i++) {
122 networkId = bufferList[i].first;
123 bufferId = bufferList[i].second;
124 if(droppedNetworkId == networkId) {
127 if(row < rowCount(parent)) {
128 BufferId beforeBufferId = parent.child(row, 0).data(NetworkModel::BufferIdRole).value<BufferId>();
129 pos = config()->bufferList().indexOf(beforeBufferId);
131 pos = config()->bufferList().count();
134 if(config()->bufferList().contains(bufferId)) {
135 if(config()->bufferList().indexOf(bufferId) < pos)
137 config()->requestMoveBuffer(bufferId, pos);
139 config()->requestAddBuffer(bufferId, pos);
149 void BufferViewFilter::addBuffer(const BufferId &bufferId) const {
150 if(!config() || config()->bufferList().contains(bufferId))
153 int pos = config()->bufferList().count();
155 for(int i = 0; i < config()->bufferList().count(); i++) {
156 if(config() && config()->sortAlphabetically())
157 lt = bufferIdLessThan(bufferId, config()->bufferList()[i]);
159 lt = bufferId < config()->bufferList()[i];
166 config()->requestAddBuffer(bufferId, pos);
169 bool BufferViewFilter::filterAcceptBuffer(const QModelIndex &source_bufferIndex) const {
170 BufferId bufferId = sourceModel()->data(source_bufferIndex, NetworkModel::BufferIdRole).value<BufferId>();
171 Q_ASSERT(bufferId.isValid());
175 int activityLevel = source_bufferIndex.data(NetworkModel::BufferActivityRole).toInt();
176 if(config()->networkId().isValid() && config()->networkId() != sourceModel()->data(source_bufferIndex, NetworkModel::NetworkIdRole).value<NetworkId>())
179 if(!(config()->allowedBufferTypes() & (BufferInfo::Type)source_bufferIndex.data(NetworkModel::BufferTypeRole).toInt()))
182 if(config()->hideInactiveBuffers() && !source_bufferIndex.data(NetworkModel::ItemActiveRole).toBool())
185 if(config()->minimumActivity() > activityLevel) {
186 if(bufferId != Client::bufferModel()->standardSelectionModel()->currentIndex().data(NetworkModel::BufferIdRole).value<BufferId>())
190 if(config()->bufferList().contains(bufferId))
193 if(config()->isInitialized() && !config()->removedBuffers().contains(bufferId)
194 && (activityLevel > Buffer::OtherActivity || config()->addNewBuffersAutomatically())) {
201 bool BufferViewFilter::filterAcceptNetwork(const QModelIndex &source_index) const {
205 if(!config()->networkId().isValid()) {
208 return config()->networkId() == sourceModel()->data(source_index, NetworkModel::NetworkIdRole).value<NetworkId>();
212 bool BufferViewFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
213 QModelIndex child = sourceModel()->index(source_row, 0, source_parent);
215 if(!child.isValid()) {
216 qWarning() << "filterAcceptsRow has been called with an invalid Child";
220 if(!source_parent.isValid())
221 return filterAcceptNetwork(child);
223 return filterAcceptBuffer(child);
226 bool BufferViewFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
227 int itemType = sourceModel()->data(source_left, NetworkModel::ItemTypeRole).toInt();
229 case NetworkModel::NetworkItemType:
230 return networkLessThan(source_left, source_right);
231 case NetworkModel::BufferItemType:
232 return bufferLessThan(source_left, source_right);
234 return QSortFilterProxyModel::lessThan(source_left, source_right);
238 bool BufferViewFilter::bufferLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
239 BufferId leftBufferId = sourceModel()->data(source_left, NetworkModel::BufferIdRole).value<BufferId>();
240 BufferId rightBufferId = sourceModel()->data(source_right, NetworkModel::BufferIdRole).value<BufferId>();
242 return config()->bufferList().indexOf(leftBufferId) < config()->bufferList().indexOf(rightBufferId);
244 return bufferIdLessThan(leftBufferId, rightBufferId);
247 bool BufferViewFilter::networkLessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
248 NetworkId leftNetworkId = sourceModel()->data(source_left, NetworkModel::NetworkIdRole).value<NetworkId>();
249 NetworkId rightNetworkId = sourceModel()->data(source_right, NetworkModel::NetworkIdRole).value<NetworkId>();
251 if(config() && config()->sortAlphabetically())
252 return QSortFilterProxyModel::lessThan(source_left, source_right);
254 return leftNetworkId < rightNetworkId;
257 QVariant BufferViewFilter::data(const QModelIndex &index, int role) const {
258 if(role == Qt::ForegroundRole)
259 return foreground(index);
261 return QSortFilterProxyModel::data(index, role);
264 QVariant BufferViewFilter::foreground(const QModelIndex &index) const {
265 if(!index.data(NetworkModel::ItemActiveRole).toBool())
266 return _FgColorInactiveActivity;
268 Buffer::ActivityLevel activity = (Buffer::ActivityLevel)index.data(NetworkModel::BufferActivityRole).toInt();
270 if(activity & Buffer::Highlight)
271 return _FgColorHighlightActivity;
272 if(activity & Buffer::NewMessage)
273 return _FgColorNewMessageActivity;
274 if(activity & Buffer::OtherActivity)
275 return _FgColorOtherActivity;
277 return _FgColorNoActivity;
280 void BufferViewFilter::checkPreviousCurrentForRemoval(const QModelIndex ¤t, const QModelIndex &previous) {
282 if(previous.isValid())
283 QCoreApplication::postEvent(this, new CheckRemovalEvent(previous));
286 void BufferViewFilter::customEvent(QEvent *event) {
287 if(event->type() != QEvent::User)
290 CheckRemovalEvent *removalEvent = static_cast<CheckRemovalEvent *>(event);
291 checkItemForRemoval(removalEvent->index);
296 void BufferViewFilter::checkItemsForRemoval(const QModelIndex &topLeft, const QModelIndex &bottomRight) {
297 QModelIndex source_topLeft = mapToSource(topLeft);
298 QModelIndex source_bottomRight = mapToSource(bottomRight);
299 emit _dataChanged(source_topLeft, source_bottomRight);
302 // ******************************
304 // ******************************
305 bool bufferIdLessThan(const BufferId &left, const BufferId &right) {
306 Q_CHECK_PTR(Client::networkModel());
307 if(!Client::networkModel())
310 QModelIndex leftIndex = Client::networkModel()->bufferIndex(left);
311 QModelIndex rightIndex = Client::networkModel()->bufferIndex(right);
313 int leftType = Client::networkModel()->data(leftIndex, NetworkModel::BufferTypeRole).toInt();
314 int rightType = Client::networkModel()->data(rightIndex, NetworkModel::BufferTypeRole).toInt();
316 if(leftType != rightType)
317 return leftType < rightType;
319 return QString::compare(Client::networkModel()->data(leftIndex, Qt::DisplayRole).toString(), Client::networkModel()->data(rightIndex, Qt::DisplayRole).toString(), Qt::CaseInsensitive) < 0;