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"
|
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;
|
|
|
|
}
|
|
|
|
|
2007-09-26 09:44:06 +02:00
|
|
|
bool AbstractItem::matches(const QString & pattern) const
|
|
|
|
{
|
|
|
|
return name().contains(pattern, Qt::CaseInsensitive) || description().contains(pattern, Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultFilterModel
|
|
|
|
|
|
|
|
DefaultFilterModel::DefaultFilterModel(QObject * parent) :
|
|
|
|
QStandardItemModel(0, 1, parent)
|
|
|
|
{
|
|
|
|
setHeaderData(1, Qt::Horizontal, tr("Filters"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultFilterModel::addFilter(const QString & caption,
|
2008-08-23 19:22:26 +02:00
|
|
|
const Filter & filter, const KIcon & icon)
|
2007-09-26 09:44:06 +02:00
|
|
|
{
|
|
|
|
QList<QStandardItem *> newRow;
|
|
|
|
QStandardItem * item = new QStandardItem(caption);
|
|
|
|
item->setData(qVariantFromValue<Filter>(filter));
|
2008-08-23 19:22:26 +02:00
|
|
|
if (!icon.isNull()) item->setIcon(icon);
|
2007-09-26 09:44:06 +02:00
|
|
|
|
|
|
|
newRow << item;
|
|
|
|
appendRow(newRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultFilterModel::addSeparator(const QString & caption)
|
|
|
|
{
|
|
|
|
QList<QStandardItem *> newRow;
|
|
|
|
QStandardItem * item = new QStandardItem(caption);
|
|
|
|
item->setEnabled(false);
|
|
|
|
|
|
|
|
newRow << item;
|
|
|
|
appendRow(newRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultItemFilterProxyModel
|
|
|
|
|
|
|
|
DefaultItemFilterProxyModel::DefaultItemFilterProxyModel(QObject * parent) :
|
|
|
|
QSortFilterProxyModel(parent), m_innerModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-11-25 01:13:49 +01: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) {
|
|
|
|
kWarning() << "DefaultItemFilterProxyModel::setSourceModel expects a QStandardItemModel!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_innerModel.setSourceModel(model);
|
2007-09-26 09:44:06 +02:00
|
|
|
QSortFilterProxyModel::setSourceModel(&m_innerModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStandardItemModel * DefaultItemFilterProxyModel::sourceModel() const
|
|
|
|
{
|
|
|
|
return m_innerModel.sourceModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DefaultItemFilterProxyModel::columnCount(const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(index);
|
2007-12-10 02:18:57 +01:00
|
|
|
return 3;
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DefaultItemFilterProxyModel::data(const QModelIndex & index, int role) const
|
|
|
|
{
|
|
|
|
return m_innerModel.data(index, (index.column() == 1), role);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultItemFilterProxyModel::filterAcceptsRow(int sourceRow,
|
|
|
|
const QModelIndex &sourceParent) const
|
|
|
|
{
|
|
|
|
QStandardItemModel * model = (QStandardItemModel *) sourceModel();
|
|
|
|
|
|
|
|
QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return (m_filter.first.isEmpty() || item->passesFiltering(m_filter))
|
|
|
|
&&(m_searchPattern.isEmpty() || item->matches(m_searchPattern));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultItemFilterProxyModel::lessThan(const QModelIndex &left,
|
|
|
|
const QModelIndex &right) const
|
|
|
|
{
|
2008-08-05 15:03:12 +02:00
|
|
|
return sourceModel()->data(left).toString().localeAwareCompare(sourceModel()->data(right).toString()) < 0;
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultItemFilterProxyModel::setSearch(const QString & pattern)
|
|
|
|
{
|
|
|
|
m_searchPattern = pattern;
|
|
|
|
invalidateFilter();
|
2008-08-11 03:41:29 +02:00
|
|
|
emit searchTermChanged(pattern);
|
2007-09-26 09:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultItemFilterProxyModel::setFilter(const Filter & filter)
|
|
|
|
{
|
|
|
|
m_filter = filter;
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultItemFilterProxyModel::InnerProxyModel
|
|
|
|
|
|
|
|
DefaultItemFilterProxyModel::InnerProxyModel::InnerProxyModel(QObject * parent) :
|
|
|
|
QAbstractItemModel(parent), m_sourceModel(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags DefaultItemFilterProxyModel::InnerProxyModel::flags(
|
|
|
|
const QModelIndex & index) const
|
|
|
|
{
|
|
|
|
if (!m_sourceModel)
|
|
|
|
return 0;
|
|
|
|
return m_sourceModel->flags(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
|
|
|
|
const QModelIndex & index, bool favoriteColumn, int role) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(favoriteColumn);
|
|
|
|
return data(index, role);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
|
|
|
|
const QModelIndex & index, int role) const
|
|
|
|
{
|
|
|
|
if (!m_sourceModel)
|
|
|
|
return QVariant();
|
|
|
|
return m_sourceModel->data(index, role);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DefaultItemFilterProxyModel::InnerProxyModel::headerData(int section,
|
|
|
|
Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(orientation);
|
|
|
|
Q_UNUSED(role);
|
|
|
|
return QVariant(section);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DefaultItemFilterProxyModel::InnerProxyModel::rowCount(
|
|
|
|
const QModelIndex & parent) const
|
|
|
|
{
|
|
|
|
if (!m_sourceModel)
|
|
|
|
return 0;
|
|
|
|
return m_sourceModel->rowCount(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultItemFilterProxyModel::InnerProxyModel::setData(
|
|
|
|
const QModelIndex & index, const QVariant & value, int role)
|
|
|
|
{
|
|
|
|
if (!m_sourceModel)
|
|
|
|
return false;
|
|
|
|
return m_sourceModel->setData(index, value, role);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultItemFilterProxyModel::InnerProxyModel::setHeaderData(int section,
|
|
|
|
Qt::Orientation orientation, const QVariant & value, int role)
|
|
|
|
{
|
|
|
|
Q_UNUSED(section);
|
|
|
|
Q_UNUSED(value);
|
|
|
|
Q_UNUSED(orientation);
|
|
|
|
Q_UNUSED(role);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::index(int row,
|
|
|
|
int column, const QModelIndex & parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(column);
|
|
|
|
if (!m_sourceModel)
|
|
|
|
return QModelIndex();
|
|
|
|
return m_sourceModel->index(row, 0, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::parent(
|
|
|
|
const QModelIndex & index) const
|
|
|
|
{
|
|
|
|
if (!m_sourceModel)
|
|
|
|
return QModelIndex();
|
|
|
|
return m_sourceModel->parent(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData * DefaultItemFilterProxyModel::InnerProxyModel::mimeData(
|
|
|
|
const QModelIndexList & indexes) const
|
|
|
|
{
|
|
|
|
if (!m_sourceModel)
|
|
|
|
return NULL;
|
|
|
|
return m_sourceModel->mimeData(indexes);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DefaultItemFilterProxyModel::InnerProxyModel::columnCount(
|
|
|
|
const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultItemFilterProxyModel::InnerProxyModel::setSourceModel(
|
|
|
|
QStandardItemModel * sourceModel)
|
|
|
|
{
|
|
|
|
m_sourceModel = sourceModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStandardItemModel * DefaultItemFilterProxyModel::InnerProxyModel::sourceModel() const
|
|
|
|
{
|
|
|
|
return m_sourceModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultItemModel
|
|
|
|
|
|
|
|
DefaultItemModel::DefaultItemModel(QObject * parent) :
|
|
|
|
QStandardItemModel(parent) {}
|
|
|
|
}
|