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);
|
2014-10-02 20:34:21 +02:00
|
|
|
setFilterKeyColumn(-1);
|
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-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 matchType = flags & 0x0F;
|
|
|
|
Qt::CaseSensitivity cs = Qt::CaseInsensitive;
|
|
|
|
bool recurse = flags & Qt::MatchRecursive;
|
|
|
|
bool wrap = flags & Qt::MatchWrap;
|
|
|
|
bool allHits = (hits == -1);
|
|
|
|
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) && (allHits || result.count() < hits); ++r) {
|
|
|
|
QModelIndex idx = index(r, start.column(), p);
|
|
|
|
|
|
|
|
if (!idx.isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex mapped_idx = mapToSource(idx);
|
|
|
|
QVariant item_value = m_sourceModel->data(m_sourceModel->index(mapped_idx.row(), FDS_MODEL_TITLE_INDEX, mapped_idx.parent()), role);
|
|
|
|
|
|
|
|
// QVariant based matching.
|
|
|
|
if (matchType == Qt::MatchExactly) {
|
|
|
|
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 (matchType) {
|
|
|
|
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), (allHits ? -1 : hits - result.count()), flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
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-10-02 20:34:21 +02: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::mapListToSource(const QModelIndexList &indexes) {
|
|
|
|
QModelIndexList source_indexes;
|
|
|
|
|
|
|
|
foreach (const QModelIndex &index, indexes) {
|
|
|
|
source_indexes << mapToSource(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return source_indexes;
|
|
|
|
}
|