1 /***************************************************************************
2 * Copyright (C) 2005-2015 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "bufferhotlistfilter.h"
23 #include "networkmodel.h"
26 BufferHotListFilter::BufferHotListFilter(QAbstractItemModel *source, QObject *parent)
27 : QSortFilterProxyModel(parent)
29 setSourceModel(source);
30 setDynamicSortFilter(true);
31 sort(0, Qt::DescendingOrder); // enable sorting... this is "usually" triggered by a enabling setSortingEnabled(true) on a view;
34 BufferId BufferHotListFilter::hottestBuffer()
37 sort(0, Qt::DescendingOrder);
38 QModelIndex topIndex = index(0,0);
39 return data(topIndex, NetworkModel::BufferIdRole).value<BufferId>();
42 bool BufferHotListFilter::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
44 Q_ASSERT(sourceModel());
45 QModelIndex source_index = sourceModel()->index(source_row, 0, source_parent);
47 MsgId firstUnreadMsgId = sourceModel()->data(source_index, NetworkModel::BufferFirstUnreadMsgIdRole).value<MsgId>();
48 if (!firstUnreadMsgId.isValid())
51 // filter out statusbuffers (it's accessable as networkitem)
52 BufferInfo::Type bufferType = (BufferInfo::Type)sourceModel()->data(source_index, NetworkModel::BufferTypeRole).toInt();
53 if (bufferType == BufferInfo::StatusBuffer) {
54 NetworkModel::ItemType itemType = (NetworkModel::ItemType)sourceModel()->data(source_index, NetworkModel::ItemTypeRole).toInt();
55 return itemType == NetworkModel::NetworkItemType;
62 bool BufferHotListFilter::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
64 int leftActivity = sourceModel()->data(source_left, NetworkModel::BufferActivityRole).toInt();
65 int rightActivity = sourceModel()->data(source_right, NetworkModel::BufferActivityRole).toInt();
66 if (leftActivity != rightActivity)
67 return leftActivity < rightActivity;
69 MsgId leftUnreadMsgId = sourceModel()->data(source_left, NetworkModel::BufferFirstUnreadMsgIdRole).value<MsgId>();
70 MsgId rightUnreadMsgId = sourceModel()->data(source_right, NetworkModel::BufferFirstUnreadMsgIdRole).value<MsgId>();
71 return leftUnreadMsgId > rightUnreadMsgId; // newer messages are treated to be "less"
75 // QVariant BufferHotListFilter::data(const QModelIndex &index, int role) const {
76 // QVariant d = QSortFilterProxyModel::data(index, role);
78 // if(role == Qt::DisplayRole) {
79 // int activity = QSortFilterProxyModel::data(index, NetworkModel::BufferActivityRole).toInt();
80 // MsgId unreadMsgId = QSortFilterProxyModel::data(index, NetworkModel::BufferFirstUnreadMsgIdRole).value<MsgId>();
81 // return QString("%1 %2 %3").arg(d.toString()).arg(activity).arg(unreadMsgId.toInt());