2014-02-26 07:41:40 +01:00
|
|
|
// This file is part of RSS Guard.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2011-2014 by Martin Rotter <rotter.martinos@gmail.com>
|
|
|
|
//
|
|
|
|
// RSS Guard is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// RSS Guard is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-11-12 20:24:19 +01:00
|
|
|
#include "core/feedsproxymodel.h"
|
2013-12-21 21:08:52 +01:00
|
|
|
|
2014-03-27 08:40:23 +01:00
|
|
|
#include "definitions/definitions.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
|
|
|
|
|
|
|
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.
|
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-18 20:38:51 +01:00
|
|
|
return QString::localeAwareCompare(left_item->title(),
|
|
|
|
right_item->title()) < 0;
|
2013-12-19 11:35:17 +01:00
|
|
|
}
|
2013-12-14 11:45:43 +01:00
|
|
|
}
|
2014-09-14 19:14:18 +02:00
|
|
|
else if (left_item->kind() == FeedsModelRootItem::RecycleBin) {
|
2014-09-16 07:39:11 +02:00
|
|
|
// Left item is recycle bin. Make sure it is "biggest" item if we have selected ascending order.
|
|
|
|
return sortOrder() == Qt::DescendingOrder;
|
2014-09-14 19:14:18 +02:00
|
|
|
}
|
|
|
|
else if (right_item->kind() == FeedsModelRootItem::RecycleBin) {
|
2014-09-16 07:39:11 +02:00
|
|
|
// Right item is recycle bin. Make sure it is "smallest" item if we have selected descending order.
|
|
|
|
return sortOrder() == Qt::AscendingOrder;
|
2014-09-14 19:14:18 +02: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.
|
2014-09-14 19:14:18 +02:00
|
|
|
// NOTE: Category is in fact "more" than feed but we consider it to be "less" because it should be "placed"
|
2014-01-10 08:36:08 +01:00
|
|
|
// above the "smalles" feed when ascending sort is used.
|
2014-09-14 19:14:18 +02:00
|
|
|
// NOTE: We need to keep recycle bin in first position.
|
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;
|
|
|
|
}
|