rssguard/src/core/messagesproxymodel.cpp

191 lines
5.7 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-09 19:04:00 +01:00
#include "core/messagesproxymodel.h"
2013-12-21 21:08:52 +01:00
2013-11-15 22:09:38 +01:00
#include "core/messagesmodel.h"
2013-11-09 19:04:00 +01:00
MessagesProxyModel::MessagesProxyModel(QObject *parent)
: QSortFilterProxyModel(parent) {
2013-11-15 22:09:38 +01:00
m_sourceModel = new MessagesModel(this);
2015-06-24 09:10:43 +02:00
setObjectName(QSL("MessagesProxyModel"));
2013-11-15 22:09:38 +01:00
setSortRole(Qt::EditRole);
setSortCaseSensitivity(Qt::CaseInsensitive);
setFilterCaseSensitivity(Qt::CaseInsensitive);
setFilterKeyColumn(-1);
2013-11-17 16:41:44 +01:00
setFilterRole(Qt::EditRole);
setDynamicSortFilter(false);
2013-11-15 22:09:38 +01:00
setSourceModel(m_sourceModel);
}
MessagesProxyModel::~MessagesProxyModel() {
qDebug("Destroying MessagesProxyModel instance.");
2013-11-09 19:04:00 +01:00
}
2013-11-15 22:24:38 +01:00
2015-11-29 09:22:17 +01:00
QModelIndex MessagesProxyModel::getNextPreviousUnreadItemIndex(int default_row) {
bool started_from_zero = default_row == 0;
QModelIndex next_index = getNextUnreadItemIndex(default_row, rowCount() - 1);
// There is no next message, check previous.
if (!next_index.isValid() && !started_from_zero) {
next_index = getNextUnreadItemIndex(0, default_row - 1);
}
return next_index;
}
QModelIndex MessagesProxyModel::getNextUnreadItemIndex(int default_row, int max_row) {
while (default_row <= max_row) {
// Get info if the message is read or not.
QModelIndex proxy_index = index(default_row, MSG_DB_READ_INDEX);
2015-11-29 09:22:17 +01:00
bool is_read = m_sourceModel->data(mapToSource(proxy_index).row(),
MSG_DB_READ_INDEX, Qt::EditRole).toInt() == 1;
if (!is_read) {
// We found unread message, mark it.
return proxy_index;
}
else {
default_row++;
}
}
return QModelIndex();
}
2013-11-15 22:24:38 +01:00
bool MessagesProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const {
2014-09-09 11:16:08 +02:00
if (left.column() == MSG_DB_TITLE_INDEX && right.column() == MSG_DB_TITLE_INDEX) {
return QString::localeAwareCompare(m_sourceModel->data(left).toString(),
m_sourceModel->data(right).toString()) < 0;
}
else {
return QSortFilterProxyModel::lessThan(left, right);
}
2013-11-15 22:24:38 +01:00
}
2013-12-04 21:03:09 +01:00
2013-12-08 14:02:28 +01:00
QModelIndexList MessagesProxyModel::mapListFromSource(const QModelIndexList &indexes, bool deep) {
2013-12-11 18:54:09 +01:00
QModelIndexList mapped_indexes;
2013-12-04 21:03:09 +01:00
2013-12-08 14:02:28 +01:00
foreach (const QModelIndex &index, indexes) {
if (deep) {
2014-01-06 08:54:20 +01:00
// Construct new source index.
2013-12-11 18:54:09 +01:00
mapped_indexes << mapFromSource(m_sourceModel->index(index.row(), index.column()));
2013-12-08 14:02:28 +01:00
}
else {
2013-12-11 18:54:09 +01:00
mapped_indexes << mapFromSource(index);
2013-12-08 14:02:28 +01:00
}
2013-12-04 21:03:09 +01:00
}
2013-12-11 18:54:09 +01:00
return mapped_indexes;
2013-12-04 21:03:09 +01:00
}
QModelIndexList MessagesProxyModel::match(const QModelIndex &start, int role,
2014-09-19 14:43:07 +02:00
const QVariant &entered_value, int hits, Qt::MatchFlags flags) const {
QModelIndexList result;
2014-09-19 14:43:07 +02:00
uint match_type = flags & 0x0F;
2014-10-29 16:38:23 +01:00
Qt::CaseSensitivity case_sensitivity = Qt::CaseInsensitive;
bool wrap = flags & Qt::MatchWrap;
2014-09-19 14:43:07 +02:00
bool all_hits = (hits == -1);
QString entered_text;
int from = start.row();
int to = rowCount();
2014-09-19 14:43:07 +02:00
for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); i++) {
for (int r = from; (r < to) && (all_hits || result.count() < hits); r++) {
QModelIndex idx = index(r, start.column());
2014-09-19 14:43:07 +02:00
if (!idx.isValid()) {
continue;
2014-09-19 14:43:07 +02:00
}
2014-10-29 16:38:23 +01:00
QVariant item_value = m_sourceModel->data(mapToSource(idx).row(), MSG_DB_TITLE_INDEX, role);
// QVariant based matching.
2014-09-19 14:43:07 +02:00
if (match_type == Qt::MatchExactly) {
if (entered_value == item_value) {
result.append(idx);
2014-09-19 14:43:07 +02:00
}
}
// QString based matching.
else {
if (entered_text.isEmpty()) {
entered_text = entered_value.toString();
}
QString item_text = item_value.toString();
switch (match_type) {
case Qt::MatchRegExp:
2014-09-19 14:43:07 +02:00
if (QRegExp(entered_text, case_sensitivity).exactMatch(item_text)) {
result.append(idx);
2014-09-19 14:43:07 +02:00
}
break;
2014-09-19 14:43:07 +02:00
case Qt::MatchWildcard:
2014-09-19 14:43:07 +02:00
if (QRegExp(entered_text, case_sensitivity, QRegExp::Wildcard).exactMatch(item_text)) {
result.append(idx);
2014-09-19 14:43:07 +02:00
}
break;
2014-09-19 14:43:07 +02:00
case Qt::MatchStartsWith:
2014-09-19 14:43:07 +02:00
if (item_text.startsWith(entered_text, case_sensitivity)) {
result.append(idx);
2014-09-19 14:43:07 +02:00
}
break;
2014-09-19 14:43:07 +02:00
case Qt::MatchEndsWith:
2014-09-19 14:43:07 +02:00
if (item_text.endsWith(entered_text, case_sensitivity)) {
result.append(idx);
2014-09-19 14:43:07 +02:00
}
break;
2014-09-19 14:43:07 +02:00
case Qt::MatchFixedString:
2014-09-19 14:43:07 +02:00
if (item_text.compare(entered_text, case_sensitivity) == 0) {
result.append(idx);
2014-09-19 14:43:07 +02:00
}
break;
2014-09-19 14:43:07 +02:00
case Qt::MatchContains:
default:
2014-09-19 14:43:07 +02:00
if (item_text.contains(entered_text, case_sensitivity)) {
result.append(idx);
2014-09-19 14:43:07 +02:00
}
break;
}
}
}
// Prepare for the next iteration.
from = 0;
to = start.row();
}
2014-09-19 14:43:07 +02:00
return result;
}
2013-12-08 14:02:28 +01:00
QModelIndexList MessagesProxyModel::mapListToSource(const QModelIndexList &indexes) {
2013-12-11 18:54:09 +01:00
QModelIndexList source_indexes;
2013-12-04 21:03:09 +01:00
2013-12-08 14:02:28 +01:00
foreach (const QModelIndex &index, indexes) {
2013-12-11 18:54:09 +01:00
source_indexes << mapToSource(index);
2013-12-04 21:03:09 +01:00
}
2013-12-11 18:54:09 +01:00
return source_indexes;
2013-12-04 21:03:09 +01:00
}