not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / shells / common / kcategorizeditemsviewmodels.cpp
blob86a596718bcb4d4dce7430a508d584f9f06b8823
1 /*
2 * Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library/Lesser General Public License
6 * version 2, or (at your option) any later version, as published by the
7 * Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library/Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "kcategorizeditemsviewmodels_p.h"
21 #include <klocale.h>
23 #define COLUMN_COUNT 4
25 namespace KCategorizedItemsViewModels {
27 // AbstractItem
29 QString AbstractItem::name() const
31 return text();
34 QString AbstractItem::description() const
36 return "";
39 bool AbstractItem::isFavorite() const
41 return passesFiltering(Filter("favorite", true));
44 int AbstractItem::running() const
46 return 0;
49 bool AbstractItem::matches(const QString &pattern) const
51 return
52 name().contains(pattern, Qt::CaseInsensitive) ||
53 description().contains(pattern, Qt::CaseInsensitive);
56 // DefaultFilterModel
58 DefaultFilterModel::DefaultFilterModel(QObject *parent) :
59 QStandardItemModel(0, 1, parent)
61 setHeaderData(1, Qt::Horizontal, i18n("Filters"));
64 void DefaultFilterModel::addFilter(const QString &caption, const Filter &filter, const KIcon &icon)
66 QList<QStandardItem *> newRow;
67 QStandardItem *item = new QStandardItem(caption);
68 item->setData(qVariantFromValue<Filter>(filter));
69 if (!icon.isNull()) {
70 item->setIcon(icon);
73 newRow << item;
74 appendRow(newRow);
77 void DefaultFilterModel::addSeparator(const QString &caption)
79 QList<QStandardItem *> newRow;
80 QStandardItem *item = new QStandardItem(caption);
81 item->setEnabled(false);
83 newRow << item;
84 appendRow(newRow);
87 // DefaultItemFilterProxyModel
89 DefaultItemFilterProxyModel::DefaultItemFilterProxyModel(QObject *parent) :
90 QSortFilterProxyModel(parent), m_innerModel(parent)
94 void DefaultItemFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
96 QStandardItemModel *model = qobject_cast<QStandardItemModel*>(sourceModel);
98 if (!model) {
99 kWarning() << "Expecting a QStandardItemModel!";
100 return;
103 m_innerModel.setSourceModel(model);
104 QSortFilterProxyModel::setSourceModel(&m_innerModel);
107 QStandardItemModel *DefaultItemFilterProxyModel::sourceModel() const
109 return m_innerModel.sourceModel();
112 int DefaultItemFilterProxyModel::columnCount(const QModelIndex &index) const
114 Q_UNUSED(index);
115 return COLUMN_COUNT;
118 QVariant DefaultItemFilterProxyModel::data(const QModelIndex &index, int role) const
120 return m_innerModel.data(index, (index.column() == 1), role);
123 bool DefaultItemFilterProxyModel::filterAcceptsRow(int sourceRow,
124 const QModelIndex &sourceParent) const
126 QStandardItemModel *model = (QStandardItemModel *) sourceModel();
128 QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
130 AbstractItem *item = (AbstractItem *) model->itemFromIndex(index);
131 //kDebug() << "ITEM " << (item ? "IS NOT " : "IS") << " NULL\n";
133 return
134 (m_filter.first.isEmpty() || item->passesFiltering(m_filter)) &&
135 (m_searchPattern.isEmpty() || item->matches(m_searchPattern));
138 bool DefaultItemFilterProxyModel::lessThan(const QModelIndex &left,
139 const QModelIndex &right) const
141 return
142 sourceModel()->data(left).toString().localeAwareCompare(
143 sourceModel()->data(right).toString()) < 0;
146 void DefaultItemFilterProxyModel::setSearch(const QString &pattern)
148 m_searchPattern = pattern;
149 invalidateFilter();
150 emit searchTermChanged(pattern);
153 void DefaultItemFilterProxyModel::setFilter(const Filter &filter)
155 m_filter = filter;
156 invalidateFilter();
159 // DefaultItemFilterProxyModel::InnerProxyModel
161 DefaultItemFilterProxyModel::InnerProxyModel::InnerProxyModel(QObject *parent) :
162 QAbstractItemModel(parent), m_sourceModel(NULL)
166 Qt::ItemFlags DefaultItemFilterProxyModel::InnerProxyModel::flags(const QModelIndex &index) const
168 if (!m_sourceModel) {
169 return 0;
171 return m_sourceModel->flags(index);
174 QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
175 const QModelIndex &index, bool favoriteColumn, int role) const
177 Q_UNUSED(favoriteColumn);
178 return data(index, role);
181 QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
182 const QModelIndex &index, int role) const
184 if (!m_sourceModel) {
185 return QVariant();
187 return m_sourceModel->data(index, role);
190 QVariant DefaultItemFilterProxyModel::InnerProxyModel::headerData(
191 int section, Qt::Orientation orientation, int role) const
193 Q_UNUSED(orientation);
194 Q_UNUSED(role);
195 return QVariant(section);
198 int DefaultItemFilterProxyModel::InnerProxyModel::rowCount(const QModelIndex &parent) const
200 if (!m_sourceModel) {
201 return 0;
203 return m_sourceModel->rowCount(parent);
206 bool DefaultItemFilterProxyModel::InnerProxyModel::setData(
207 const QModelIndex &index, const QVariant &value, int role)
209 if (!m_sourceModel) {
210 return false;
212 return m_sourceModel->setData(index, value, role);
215 bool DefaultItemFilterProxyModel::InnerProxyModel::setHeaderData(
216 int section, Qt::Orientation orientation, const QVariant &value, int role)
218 Q_UNUSED(section);
219 Q_UNUSED(value);
220 Q_UNUSED(orientation);
221 Q_UNUSED(role);
222 return false;
225 QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::index(
226 int row, int column, const QModelIndex &parent) const
228 Q_UNUSED(column);
229 if (!m_sourceModel) {
230 return QModelIndex();
232 return m_sourceModel->index(row, 0, parent);
235 QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::parent(const QModelIndex &index) const
237 if (!m_sourceModel) {
238 return QModelIndex();
240 return m_sourceModel->parent(index);
243 QMimeData *DefaultItemFilterProxyModel::InnerProxyModel::mimeData(
244 const QModelIndexList &indexes) const
246 if (!m_sourceModel) {
247 return NULL;
249 return m_sourceModel->mimeData(indexes);
252 int DefaultItemFilterProxyModel::InnerProxyModel::columnCount(const QModelIndex &index) const
254 Q_UNUSED(index);
255 return COLUMN_COUNT;
258 void DefaultItemFilterProxyModel::InnerProxyModel::setSourceModel(QStandardItemModel *sourceModel)
260 m_sourceModel = sourceModel;
263 QStandardItemModel *DefaultItemFilterProxyModel::InnerProxyModel::sourceModel() const
265 return m_sourceModel;
268 // DefaultItemModel
270 DefaultItemModel::DefaultItemModel(QObject *parent) :
271 QStandardItemModel(parent) {}