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"
23 #define COLUMN_COUNT 4
25 namespace KCategorizedItemsViewModels
{
29 QString
AbstractItem::name() const
34 QString
AbstractItem::description() const
39 bool AbstractItem::isFavorite() const
41 return passesFiltering(Filter("favorite", true));
44 int AbstractItem::running() const
49 bool AbstractItem::matches(const QString
&pattern
) const
52 name().contains(pattern
, Qt::CaseInsensitive
) ||
53 description().contains(pattern
, Qt::CaseInsensitive
);
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
));
77 void DefaultFilterModel::addSeparator(const QString
&caption
)
79 QList
<QStandardItem
*> newRow
;
80 QStandardItem
*item
= new QStandardItem(caption
);
81 item
->setEnabled(false);
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
);
99 kWarning() << "Expecting a QStandardItemModel!";
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
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";
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
142 sourceModel()->data(left
).toString().localeAwareCompare(
143 sourceModel()->data(right
).toString()) < 0;
146 void DefaultItemFilterProxyModel::setSearch(const QString
&pattern
)
148 m_searchPattern
= pattern
;
150 emit
searchTermChanged(pattern
);
153 void DefaultItemFilterProxyModel::setFilter(const Filter
&filter
)
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
) {
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
) {
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
);
195 return QVariant(section
);
198 int DefaultItemFilterProxyModel::InnerProxyModel::rowCount(const QModelIndex
&parent
) const
200 if (!m_sourceModel
) {
203 return m_sourceModel
->rowCount(parent
);
206 bool DefaultItemFilterProxyModel::InnerProxyModel::setData(
207 const QModelIndex
&index
, const QVariant
&value
, int role
)
209 if (!m_sourceModel
) {
212 return m_sourceModel
->setData(index
, value
, role
);
215 bool DefaultItemFilterProxyModel::InnerProxyModel::setHeaderData(
216 int section
, Qt::Orientation orientation
, const QVariant
&value
, int role
)
220 Q_UNUSED(orientation
);
225 QModelIndex
DefaultItemFilterProxyModel::InnerProxyModel::index(
226 int row
, int column
, const QModelIndex
&parent
) const
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
) {
249 return m_sourceModel
->mimeData(indexes
);
252 int DefaultItemFilterProxyModel::InnerProxyModel::columnCount(const QModelIndex
&index
) const
258 void DefaultItemFilterProxyModel::InnerProxyModel::setSourceModel(QStandardItemModel
*sourceModel
)
260 m_sourceModel
= sourceModel
;
263 QStandardItemModel
*DefaultItemFilterProxyModel::InnerProxyModel::sourceModel() const
265 return m_sourceModel
;
270 DefaultItemModel::DefaultItemModel(QObject
*parent
) :
271 QStandardItemModel(parent
) {}