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
|
|
|
|
|
|
|
FeedsModel *FeedsProxyModel::sourceModel() {
|
|
|
|
return m_sourceModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2013-12-14 11:45:43 +01:00
|
|
|
FeedsModelRootItem *left_item = static_cast<FeedsModelRootItem*>(left.internalPointer());
|
|
|
|
FeedsModelRootItem *right_item = static_cast<FeedsModelRootItem*>(right.internalPointer());
|
|
|
|
|
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.
|
2013-12-19 11:35:17 +01:00
|
|
|
// 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.
|
2013-12-19 11:35:17 +01:00
|
|
|
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.
|
2014-01-02 14:17:15 +01:00
|
|
|
// TODO: mozna pouzit QString::localeAwareCompare tady.
|
2013-12-19 11:35:17 +01:00
|
|
|
return left_item->title() < right_item->title();
|
|
|
|
}
|
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 {
|
2013-12-16 09:11:14 +01:00
|
|
|
// Left item is category, right item is feed.*/
|
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;
|
|
|
|
}
|