rssguard/src/core/messagesproxymodel.cpp

76 lines
2.4 KiB
C++
Raw Normal View History

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-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);
setObjectName("MessagesProxyModel");
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
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
}
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
}