rssguard/src/core/feedsproxymodel.cpp

91 lines
2.8 KiB
C++
Raw Normal View History

2013-11-12 20:24:19 +01:00
#include "core/feedsproxymodel.h"
2013-12-21 21:08:52 +01:00
#include "core/defs.h"
2013-12-11 18:54:09 +01:00
#include "core/feedsmodel.h"
2013-12-14 11:45:43 +01:00
#include "core/feedsmodelcategory.h"
#include "core/feedsmodelfeed.h"
#include "core/feedsmodelrootitem.h"
2013-11-12 20:24:19 +01:00
FeedsProxyModel::FeedsProxyModel(QObject *parent)
: QSortFilterProxyModel(parent) {
2013-12-11 18:54:09 +01:00
m_sourceModel = new FeedsModel(this);
2013-12-14 11:45:43 +01:00
setObjectName("FeedsProxyModel");
setSortRole(Qt::EditRole);
setSortCaseSensitivity(Qt::CaseInsensitive);
setFilterCaseSensitivity(Qt::CaseInsensitive);
2013-12-16 09:11:14 +01:00
setFilterKeyColumn(0);
2013-12-14 11:45:43 +01:00
setFilterRole(Qt::EditRole);
setDynamicSortFilter(true);
2013-12-11 18:54:09 +01:00
setSourceModel(m_sourceModel);
2013-11-12 20:24:19 +01:00
}
2013-12-11 14:07:18 +01:00
FeedsProxyModel::~FeedsProxyModel() {
qDebug("Destroying FeedsProxyModel instance");
}
2013-12-11 18:54:09 +01:00
2014-01-14 14:42:20 +01:00
2013-12-11 18:54:09 +01:00
bool FeedsProxyModel::lessThan(const QModelIndex &left,
const QModelIndex &right) const {
2013-12-14 11:45:43 +01:00
if (left.isValid() && right.isValid()) {
2013-12-16 09:11:14 +01:00
// Make necessary castings.
2014-01-10 08:36:08 +01:00
FeedsModelRootItem *left_item = m_sourceModel->itemForIndex(left);
FeedsModelRootItem *right_item = m_sourceModel->itemForIndex(right);
2013-12-14 11:45:43 +01:00
2013-12-16 09:11:14 +01:00
// NOTE: Here we want to accomplish that ALL
// categories are queued one after another and all
// feeds are queued one after another too.
// Moreover, sort everything alphabetically or
// by item counts, depending on the sort column.
2013-12-14 11:45:43 +01:00
2013-12-16 09:11:14 +01:00
if (left_item->kind() == right_item->kind()) {
// Both items are feeds or both items are categories.
if (left.column() == FDS_MODEL_COUNTS_INDEX) {
// User wants to sort according to counts.
return left_item->countOfUnreadMessages() < right_item->countOfUnreadMessages();
}
else {
// In other cases, sort by title.
return QString::localeAwareCompare(left_item->title(),
right_item->title()) < 0;
}
2013-12-14 11:45:43 +01:00
}
2013-12-16 09:11:14 +01:00
else if (left_item->kind() == FeedsModelRootItem::Feed) {
2013-12-14 11:45:43 +01:00
// Left item is feed, right item is category.
return false;
}
else {
2014-01-10 08:36:08 +01:00
// Left item is category, right item is feed.
// NOTE: Category is in fact "more" than feed but
// we consider it to be "less" because it should be "placed"
// above the "smalles" feed when ascending sort is used.
2013-12-14 11:45:43 +01:00
return true;
}
}
else {
return false;
}
2013-12-11 18:54:09 +01:00
}
2013-12-16 12:53:48 +01:00
QModelIndexList FeedsProxyModel::mapListFromSource(const QModelIndexList &indexes) {
QModelIndexList mapped_indexes;
foreach (const QModelIndex &index, indexes) {
mapped_indexes << mapFromSource(index);
}
return mapped_indexes;
}
QModelIndexList FeedsProxyModel::mapListToSource(const QModelIndexList &indexes) {
QModelIndexList source_indexes;
foreach (const QModelIndex &index, indexes) {
source_indexes << mapToSource(index);
}
return source_indexes;
}