• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

kcategorizeditemsviewmodels.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library/Lesser General Public License
00006  *   version 2, or (at your option) any later version, as published by the
00007  *   Free Software Foundation
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library/Lesser General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "kcategorizeditemsviewmodels_p.h"
00021 #include <klocale.h>
00022 
00023 #define COLUMN_COUNT 4
00024 
00025 namespace KCategorizedItemsViewModels {
00026 
00027 // AbstractItem
00028 
00029 QString AbstractItem::name() const
00030 {
00031     return text();
00032 }
00033 
00034 QString AbstractItem::description() const
00035 {
00036     return "";
00037 }
00038 
00039 bool AbstractItem::isFavorite() const
00040 {
00041     return passesFiltering(Filter("favorite", true));
00042 }
00043 
00044 int AbstractItem::running() const
00045 {
00046     return 0;
00047 }
00048 
00049 bool AbstractItem::matches(const QString &pattern) const
00050 {
00051     return
00052         name().contains(pattern, Qt::CaseInsensitive) ||
00053         description().contains(pattern, Qt::CaseInsensitive);
00054 }
00055 
00056 // DefaultFilterModel
00057 
00058 DefaultFilterModel::DefaultFilterModel(QObject *parent) :
00059     QStandardItemModel(0, 1, parent)
00060 {
00061     setHeaderData(1, Qt::Horizontal, i18n("Filters"));
00062 }
00063 
00064 void DefaultFilterModel::addFilter(const QString &caption, const Filter &filter, const KIcon &icon)
00065 {
00066     QList<QStandardItem *> newRow;
00067     QStandardItem *item = new QStandardItem(caption);
00068     item->setData(qVariantFromValue<Filter>(filter));
00069     if (!icon.isNull()) {
00070         item->setIcon(icon);
00071     }
00072 
00073     newRow << item;
00074     appendRow(newRow);
00075 }
00076 
00077 void DefaultFilterModel::addSeparator(const QString &caption)
00078 {
00079     QList<QStandardItem *> newRow;
00080     QStandardItem *item = new QStandardItem(caption);
00081     item->setEnabled(false);
00082 
00083     newRow << item;
00084     appendRow(newRow);
00085 }
00086 
00087 // DefaultItemFilterProxyModel
00088 
00089 DefaultItemFilterProxyModel::DefaultItemFilterProxyModel(QObject *parent) :
00090     QSortFilterProxyModel(parent), m_innerModel(parent)
00091 {
00092 }
00093 
00094 void DefaultItemFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
00095 {
00096     QStandardItemModel *model = qobject_cast<QStandardItemModel*>(sourceModel);
00097 
00098     if (!model) {
00099         kWarning() << "Expecting a QStandardItemModel!";
00100         return;
00101     }
00102 
00103     m_innerModel.setSourceModel(model);
00104     QSortFilterProxyModel::setSourceModel(&m_innerModel);
00105 }
00106 
00107 QStandardItemModel *DefaultItemFilterProxyModel::sourceModel() const
00108 {
00109     return m_innerModel.sourceModel();
00110 }
00111 
00112 int DefaultItemFilterProxyModel::columnCount(const QModelIndex &index) const
00113 {
00114     Q_UNUSED(index);
00115     return COLUMN_COUNT;
00116 }
00117 
00118 QVariant DefaultItemFilterProxyModel::data(const QModelIndex &index, int role) const
00119 {
00120     return m_innerModel.data(index, (index.column() == 1), role);
00121 }
00122 
00123 bool DefaultItemFilterProxyModel::filterAcceptsRow(int sourceRow,
00124         const QModelIndex &sourceParent) const
00125 {
00126     QStandardItemModel *model = (QStandardItemModel *) sourceModel();
00127 
00128     QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
00129 
00130     AbstractItem *item = (AbstractItem *) model->itemFromIndex(index);
00131     //kDebug() << "ITEM " << (item ? "IS NOT " : "IS") << " NULL\n";
00132 
00133     return
00134         (m_filter.first.isEmpty() || item->passesFiltering(m_filter)) &&
00135         (m_searchPattern.isEmpty() || item->matches(m_searchPattern));
00136 }
00137 
00138 bool DefaultItemFilterProxyModel::lessThan(const QModelIndex &left,
00139         const QModelIndex &right) const
00140 {
00141     return
00142         sourceModel()->data(left).toString().localeAwareCompare(
00143             sourceModel()->data(right).toString()) < 0;
00144 }
00145 
00146 void DefaultItemFilterProxyModel::setSearch(const QString &pattern)
00147 {
00148     m_searchPattern = pattern;
00149     invalidateFilter();
00150     emit searchTermChanged(pattern);
00151 }
00152 
00153 void DefaultItemFilterProxyModel::setFilter(const Filter &filter)
00154 {
00155     m_filter = filter;
00156     invalidateFilter();
00157 }
00158 
00159 // DefaultItemFilterProxyModel::InnerProxyModel
00160 
00161 DefaultItemFilterProxyModel::InnerProxyModel::InnerProxyModel(QObject *parent) :
00162     QAbstractItemModel(parent), m_sourceModel(NULL)
00163 {
00164 }
00165 
00166 Qt::ItemFlags DefaultItemFilterProxyModel::InnerProxyModel::flags(const QModelIndex &index) const
00167 {
00168     if (!m_sourceModel) {
00169         return 0;
00170     }
00171     return m_sourceModel->flags(index);
00172 }
00173 
00174 QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
00175     const QModelIndex &index, bool favoriteColumn, int role) const
00176 {
00177     Q_UNUSED(favoriteColumn);
00178     return data(index, role);
00179 }
00180 
00181 QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
00182         const QModelIndex &index, int role) const
00183 {
00184     if (!m_sourceModel) {
00185         return QVariant();
00186     }
00187     return m_sourceModel->data(index, role);
00188 }
00189 
00190 QVariant DefaultItemFilterProxyModel::InnerProxyModel::headerData(
00191     int section, Qt::Orientation orientation, int role) const
00192 {
00193     Q_UNUSED(orientation);
00194     Q_UNUSED(role);
00195     return QVariant(section);
00196 }
00197 
00198 int DefaultItemFilterProxyModel::InnerProxyModel::rowCount(const QModelIndex &parent) const
00199 {
00200     if (!m_sourceModel) {
00201         return 0;
00202     }
00203     return m_sourceModel->rowCount(parent);
00204 }
00205 
00206 bool DefaultItemFilterProxyModel::InnerProxyModel::setData(
00207     const QModelIndex &index, const QVariant &value, int role)
00208 {
00209     if (!m_sourceModel) {
00210         return false;
00211     }
00212     return m_sourceModel->setData(index, value, role);
00213 }
00214 
00215 bool DefaultItemFilterProxyModel::InnerProxyModel::setHeaderData(
00216     int section, Qt::Orientation orientation, const QVariant &value, int role)
00217 {
00218     Q_UNUSED(section);
00219     Q_UNUSED(value);
00220     Q_UNUSED(orientation);
00221     Q_UNUSED(role);
00222     return false;
00223 }
00224 
00225 QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::index(
00226     int row, int column, const QModelIndex &parent) const
00227 {
00228     Q_UNUSED(column);
00229     if (!m_sourceModel) {
00230         return QModelIndex();
00231     }
00232     return m_sourceModel->index(row, 0, parent);
00233 }
00234 
00235 QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::parent(const QModelIndex &index) const
00236 {
00237     if (!m_sourceModel) {
00238         return QModelIndex();
00239     }
00240     return m_sourceModel->parent(index);
00241 }
00242 
00243 QMimeData *DefaultItemFilterProxyModel::InnerProxyModel::mimeData(
00244     const QModelIndexList &indexes) const
00245 {
00246     if (!m_sourceModel) {
00247         return NULL;
00248     }
00249     return m_sourceModel->mimeData(indexes);
00250 }
00251 
00252 int DefaultItemFilterProxyModel::InnerProxyModel::columnCount(const QModelIndex &index) const
00253 {
00254     Q_UNUSED(index);
00255     return COLUMN_COUNT;
00256 }
00257 
00258 void DefaultItemFilterProxyModel::InnerProxyModel::setSourceModel(QStandardItemModel *sourceModel)
00259 {
00260     m_sourceModel = sourceModel;
00261 }
00262 
00263 QStandardItemModel *DefaultItemFilterProxyModel::InnerProxyModel::sourceModel() const
00264 {
00265     return m_sourceModel;
00266 }
00267 
00268 // DefaultItemModel
00269 
00270 DefaultItemModel::DefaultItemModel(QObject *parent) :
00271     QStandardItemModel(parent) {}
00272 }

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal