rssguard/src/core/feedsproxymodel.cpp

253 lines
8.2 KiB
C++
Raw Normal View History

2014-02-26 07:41:40 +01:00
// This file is part of RSS Guard.
//
2015-01-04 14:12:14 +01:00
// Copyright (C) 2011-2015 by Martin Rotter <rotter.martinos@gmail.com>
2014-02-26 07:41:40 +01:00
//
// 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"
2015-09-30 07:24:46 +02:00
#include "miscellaneous/application.h"
2013-12-11 18:54:09 +01:00
#include "core/feedsmodel.h"
2015-07-24 10:05:42 +02:00
#include "core/rootitem.h"
#include "services/standard/standardcategory.h"
#include "services/standard/standardfeed.h"
2013-11-12 20:24:19 +01:00
#include <QTimer>
2013-11-12 20:24:19 +01:00
FeedsProxyModel::FeedsProxyModel(QObject *parent)
2015-07-16 09:18:07 +02:00
: QSortFilterProxyModel(parent), m_selectedItem(NULL), m_showUnreadOnly(false) {
2013-12-11 18:54:09 +01:00
m_sourceModel = new FeedsModel(this);
2015-06-24 09:10:43 +02:00
setObjectName(QSL("FeedsProxyModel"));
2013-12-14 11:45:43 +01:00
setSortRole(Qt::EditRole);
setSortCaseSensitivity(Qt::CaseInsensitive);
setFilterCaseSensitivity(Qt::CaseInsensitive);
2014-10-02 20:34:21 +02:00
setFilterKeyColumn(-1);
2013-12-14 11:45:43 +01:00
setFilterRole(Qt::EditRole);
2015-07-16 09:18:07 +02:00
setDynamicSortFilter(false);
2013-12-11 18:54:09 +01:00
setSourceModel(m_sourceModel);
connect(m_sourceModel, SIGNAL(readFeedsFilterInvalidationRequested()), this, SLOT(invalidateReadFeedsFilter()));
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-10-29 16:38:23 +01:00
QModelIndexList FeedsProxyModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const {
QModelIndexList result;
uint match_type = flags & 0x0F;
2014-10-29 16:38:23 +01:00
Qt::CaseSensitivity cs = Qt::CaseInsensitive;
bool recurse = flags & Qt::MatchRecursive;
bool wrap = flags & Qt::MatchWrap;
bool all_hits = (hits == -1);
2014-10-29 16:38:23 +01:00
QString entered_text;
QModelIndex p = parent(start);
int from = start.row();
int to = rowCount(p);
for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
for (int r = from; (r < to) && (all_hits || result.count() < hits); ++r) {
2014-10-29 16:38:23 +01:00
QModelIndex idx = index(r, start.column(), p);
if (!idx.isValid()) {
continue;
}
QModelIndex mapped_idx = mapToSource(idx);
QVariant item_value = m_sourceModel->itemForIndex(mapped_idx)->title();
2014-10-29 16:38:23 +01:00
// QVariant based matching.
if (match_type == Qt::MatchExactly) {
2014-10-29 16:38:23 +01:00
if (value == item_value) {
result.append(idx);
}
}
// QString based matching.
else {
if (entered_text.isEmpty()) {
entered_text = value.toString();
}
QString item_text = item_value.toString();
switch (match_type) {
2014-10-29 16:38:23 +01:00
case Qt::MatchRegExp:
if (QRegExp(entered_text, cs).exactMatch(item_text)) {
result.append(idx);
}
break;
case Qt::MatchWildcard:
if (QRegExp(entered_text, cs, QRegExp::Wildcard).exactMatch(item_text)) {
result.append(idx);
}
break;
case Qt::MatchStartsWith:
if (item_text.startsWith(entered_text, cs)) {
result.append(idx);
}
break;
case Qt::MatchEndsWith:
if (item_text.endsWith(entered_text, cs)) {
result.append(idx);
}
break;
case Qt::MatchFixedString:
if (item_text.compare(entered_text, cs) == 0) {
result.append(idx);
}
break;
case Qt::MatchContains:
default:
if (item_text.contains(entered_text, cs)) {
result.append(idx);
}
break;
}
}
if (recurse && hasChildren(idx)) {
result += match(index(0, idx.column(), idx), role, (entered_text.isEmpty() ? value : entered_text), (all_hits ? -1 : hits - result.count()), flags);
2014-10-29 16:38:23 +01:00
}
}
from = 0;
to = start.row();
}
return result;
}
2014-10-02 20:34:21 +02: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.
2015-07-24 10:05:42 +02:00
RootItem *left_item = m_sourceModel->itemForIndex(left);
RootItem *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.
2014-10-02 20:34:21 +02:00
return QString::localeAwareCompare(left_item->title(), right_item->title()) < 0;
}
2013-12-14 11:45:43 +01:00
}
else if (left_item->kind() == RootItemKind::Bin) {
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;
}
else if (right_item->kind() == RootItemKind::Bin) {
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;
}
else if (left_item->kind() == RootItemKind::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"
2014-01-10 08:36:08 +01:00
// above the "smalles" feed when ascending sort is used.
// 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
2015-06-12 06:49:06 +02:00
bool FeedsProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
2015-07-16 09:18:07 +02:00
if (!m_showUnreadOnly) {
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
QModelIndex idx = m_sourceModel->index(source_row, 0, source_parent);
if (!idx.isValid()) {
return false;
}
2015-07-24 10:05:42 +02:00
RootItem *item = m_sourceModel->itemForIndex(idx);
2015-07-16 09:18:07 +02:00
if (item->kind() == RootItemKind::Bin || item->kind() == RootItemKind::ServiceRoot) {
2015-07-16 09:18:07 +02:00
// Recycle bin is always displayed.
return true;
}
2015-07-18 13:01:36 +02:00
else if (item->isParentOf(m_selectedItem)/* || item->isChildOf(m_selectedItem)*/ || m_selectedItem == item) {
2015-07-16 09:18:07 +02:00
// Currently selected item and all its parents and children must be displayed.
return true;
}
else {
2015-11-25 09:37:40 +01:00
// NOTE: If item has < 0 of unread message it may mean, that the count
// of unread messages is not (yet) known, display that item too.
return item->countOfUnreadMessages() != 0;
2015-07-16 09:18:07 +02:00
}
}
2015-07-24 10:05:42 +02:00
RootItem *FeedsProxyModel::selectedItem() const {
2015-07-16 09:18:07 +02:00
return m_selectedItem;
}
2015-07-24 10:05:42 +02:00
void FeedsProxyModel::setSelectedItem(RootItem *selected_item) {
2015-07-17 08:45:07 +02:00
m_selectedItem = selected_item;
2015-07-16 09:18:07 +02:00
}
bool FeedsProxyModel::showUnreadOnly() const {
return m_showUnreadOnly;
}
void FeedsProxyModel::invalidateReadFeedsFilter(bool set_new_value, bool show_unread_only) {
if (set_new_value) {
setShowUnreadOnly(show_unread_only);
}
QTimer::singleShot(0, this, SLOT(invalidateFilter()));
}
2015-07-17 08:45:07 +02:00
void FeedsProxyModel::setShowUnreadOnly(bool show_unread_only) {
m_showUnreadOnly = show_unread_only;
2015-09-30 07:24:46 +02:00
qApp->settings()->setValue(GROUP(Feeds), Feeds::ShowOnlyUnreadFeeds, show_unread_only);
2015-06-12 06:49:06 +02:00
}
2013-12-16 12:53:48 +01:00
QModelIndexList FeedsProxyModel::mapListToSource(const QModelIndexList &indexes) {
QModelIndexList source_indexes;
foreach (const QModelIndex &index, indexes) {
source_indexes << mapToSource(index);
}
return source_indexes;
}
2015-06-12 06:49:06 +02:00
void FeedsProxyModel::invalidateFilter() {
QSortFilterProxyModel::invalidateFilter();
}