2007-09-26 09:44:06 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-10-12 10:44:02 +02:00
|
|
|
* it under the terms of the GNU Library/Lesser General Public License
|
|
|
|
* version 2, or (at your option) any later version, as published by the
|
|
|
|
* Free Software Foundation
|
2007-09-26 09:44:06 +02:00
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
*
|
2007-10-12 10:44:02 +02:00
|
|
|
* You should have received a copy of the GNU Library/Lesser General Public
|
2007-09-26 09:44:06 +02:00
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2007-09-27 20:51:31 +02:00
|
|
|
#include "kcategorizeditemsviewmodels_p.h"
|
2008-09-05 09:20:11 +02:00
|
|
|
#include <klocale.h>
|
2007-09-26 09:44:06 +02:00
|
|
|
|
|
|
|
namespace KCategorizedItemsViewModels {
|
|
|
|
|
|
|
|
// AbstractItem
|
|
|
|
|
|
|
|
QString AbstractItem::name() const
|
|
|
|
{
|
|
|
|
return text();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AbstractItem::description() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AbstractItem::isFavorite() const
|
|
|
|
{
|
|
|
|
return passesFiltering(Filter("favorite", true));
|
|
|
|
}
|
|
|
|
|
2007-12-10 02:18:57 +01:00
|
|
|
int AbstractItem::running() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
bool AbstractItem::matches(const QString &pattern) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
return
|
|
|
|
name().contains(pattern, Qt::CaseInsensitive) ||
|
|
|
|
description().contains(pattern, Qt::CaseInsensitive);
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultFilterModel
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
DefaultFilterModel::DefaultFilterModel(QObject *parent) :
|
2007-09-26 09:44:06 +02:00
|
|
|
QStandardItemModel(0, 1, parent)
|
|
|
|
{
|
2008-09-05 09:20:11 +02:00
|
|
|
setHeaderData(1, Qt::Horizontal, i18n("Filters"));
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void DefaultFilterModel::addFilter(const QString &caption, const Filter &filter, const KIcon &icon)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
QList<QStandardItem *> newRow;
|
2008-10-12 02:18:08 +02:00
|
|
|
QStandardItem *item = new QStandardItem(caption);
|
2007-09-26 09:44:06 +02:00
|
|
|
item->setData(qVariantFromValue<Filter>(filter));
|
2008-10-12 02:18:08 +02:00
|
|
|
if (!icon.isNull()) {
|
|
|
|
item->setIcon(icon);
|
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
|
|
|
|
newRow << item;
|
|
|
|
appendRow(newRow);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void DefaultFilterModel::addSeparator(const QString &caption)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
QList<QStandardItem *> newRow;
|
2008-10-12 02:18:08 +02:00
|
|
|
QStandardItem *item = new QStandardItem(caption);
|
2007-09-26 09:44:06 +02:00
|
|
|
item->setEnabled(false);
|
|
|
|
|
|
|
|
newRow << item;
|
|
|
|
appendRow(newRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultItemFilterProxyModel
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
DefaultItemFilterProxyModel::DefaultItemFilterProxyModel(QObject *parent) :
|
2007-09-26 09:44:06 +02:00
|
|
|
QSortFilterProxyModel(parent), m_innerModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void DefaultItemFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2007-11-25 01:13:49 +01:00
|
|
|
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(sourceModel);
|
|
|
|
|
|
|
|
if (!model) {
|
2008-10-12 12:20:02 +02:00
|
|
|
kWarning() << "Expecting a QStandardItemModel!";
|
2007-11-25 01:13:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_innerModel.setSourceModel(model);
|
2007-09-26 09:44:06 +02:00
|
|
|
QSortFilterProxyModel::setSourceModel(&m_innerModel);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
QStandardItemModel *DefaultItemFilterProxyModel::sourceModel() const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
return m_innerModel.sourceModel();
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
int DefaultItemFilterProxyModel::columnCount(const QModelIndex &index) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(index);
|
2007-12-10 02:18:57 +01:00
|
|
|
return 3;
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
QVariant DefaultItemFilterProxyModel::data(const QModelIndex &index, int role) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
return m_innerModel.data(index, (index.column() == 1), role);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultItemFilterProxyModel::filterAcceptsRow(int sourceRow,
|
|
|
|
const QModelIndex &sourceParent) const
|
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
QStandardItemModel *model = (QStandardItemModel *) sourceModel();
|
2007-09-26 09:44:06 +02:00
|
|
|
|
|
|
|
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
AbstractItem *item = (AbstractItem *) model->itemFromIndex(index);
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "ITEM " << (item ? "IS NOT " : "IS") << " NULL\n";
|
2007-09-26 09:44:06 +02:00
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
return
|
|
|
|
(m_filter.first.isEmpty() || item->passesFiltering(m_filter)) &&
|
|
|
|
(m_searchPattern.isEmpty() || item->matches(m_searchPattern));
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultItemFilterProxyModel::lessThan(const QModelIndex &left,
|
|
|
|
const QModelIndex &right) const
|
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
return
|
|
|
|
sourceModel()->data(left).toString().localeAwareCompare(
|
|
|
|
sourceModel()->data(right).toString()) < 0;
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void DefaultItemFilterProxyModel::setSearch(const QString &pattern)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
m_searchPattern = pattern;
|
|
|
|
invalidateFilter();
|
2008-08-11 03:41:29 +02:00
|
|
|
emit searchTermChanged(pattern);
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void DefaultItemFilterProxyModel::setFilter(const Filter &filter)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
m_filter = filter;
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultItemFilterProxyModel::InnerProxyModel
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
DefaultItemFilterProxyModel::InnerProxyModel::InnerProxyModel(QObject *parent) :
|
2007-09-26 09:44:06 +02:00
|
|
|
QAbstractItemModel(parent), m_sourceModel(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
Qt::ItemFlags DefaultItemFilterProxyModel::InnerProxyModel::flags(const QModelIndex &index) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
if (!m_sourceModel) {
|
2007-09-26 09:44:06 +02:00
|
|
|
return 0;
|
2008-10-12 02:18:08 +02:00
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
return m_sourceModel->flags(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
|
2008-10-12 02:18:08 +02:00
|
|
|
const QModelIndex &index, bool favoriteColumn, int role) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(favoriteColumn);
|
|
|
|
return data(index, role);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
|
2008-10-12 02:18:08 +02:00
|
|
|
const QModelIndex &index, int role) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
if (!m_sourceModel) {
|
2007-09-26 09:44:06 +02:00
|
|
|
return QVariant();
|
2008-10-12 02:18:08 +02:00
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
return m_sourceModel->data(index, role);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
QVariant DefaultItemFilterProxyModel::InnerProxyModel::headerData(
|
|
|
|
int section, Qt::Orientation orientation, int role) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(orientation);
|
|
|
|
Q_UNUSED(role);
|
|
|
|
return QVariant(section);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
int DefaultItemFilterProxyModel::InnerProxyModel::rowCount(const QModelIndex &parent) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
if (!m_sourceModel) {
|
2007-09-26 09:44:06 +02:00
|
|
|
return 0;
|
2008-10-12 02:18:08 +02:00
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
return m_sourceModel->rowCount(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultItemFilterProxyModel::InnerProxyModel::setData(
|
2008-10-12 02:18:08 +02:00
|
|
|
const QModelIndex &index, const QVariant &value, int role)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
if (!m_sourceModel) {
|
2007-09-26 09:44:06 +02:00
|
|
|
return false;
|
2008-10-12 02:18:08 +02:00
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
return m_sourceModel->setData(index, value, role);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
bool DefaultItemFilterProxyModel::InnerProxyModel::setHeaderData(
|
|
|
|
int section, Qt::Orientation orientation, const QVariant &value, int role)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(section);
|
|
|
|
Q_UNUSED(value);
|
|
|
|
Q_UNUSED(orientation);
|
|
|
|
Q_UNUSED(role);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::index(
|
|
|
|
int row, int column, const QModelIndex &parent) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(column);
|
2008-10-12 02:18:08 +02:00
|
|
|
if (!m_sourceModel) {
|
2007-09-26 09:44:06 +02:00
|
|
|
return QModelIndex();
|
2008-10-12 02:18:08 +02:00
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
return m_sourceModel->index(row, 0, parent);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::parent(const QModelIndex &index) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
if (!m_sourceModel) {
|
2007-09-26 09:44:06 +02:00
|
|
|
return QModelIndex();
|
2008-10-12 02:18:08 +02:00
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
return m_sourceModel->parent(index);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
QMimeData *DefaultItemFilterProxyModel::InnerProxyModel::mimeData(
|
|
|
|
const QModelIndexList &indexes) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
if (!m_sourceModel) {
|
2007-09-26 09:44:06 +02:00
|
|
|
return NULL;
|
2008-10-12 02:18:08 +02:00
|
|
|
}
|
2007-09-26 09:44:06 +02:00
|
|
|
return m_sourceModel->mimeData(indexes);
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
int DefaultItemFilterProxyModel::InnerProxyModel::columnCount(const QModelIndex &index) const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(index);
|
2007-12-10 02:18:57 +01:00
|
|
|
return 3; //FIXME: a hardcoded magic number that appears in two places CANNOT be good
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
void DefaultItemFilterProxyModel::InnerProxyModel::setSourceModel(QStandardItemModel *sourceModel)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
m_sourceModel = sourceModel;
|
|
|
|
}
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
QStandardItemModel *DefaultItemFilterProxyModel::InnerProxyModel::sourceModel() const
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
return m_sourceModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultItemModel
|
|
|
|
|
2008-10-12 02:18:08 +02:00
|
|
|
DefaultItemModel::DefaultItemModel(QObject *parent) :
|
2007-09-26 09:44:06 +02:00
|
|
|
QStandardItemModel(parent) {}
|
|
|
|
}
|