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

libplasma

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 
00022 namespace KCategorizedItemsViewModels {
00023 
00024 // AbstractItem
00025 
00026 QString AbstractItem::name() const
00027 {
00028     return text();
00029 }
00030 
00031 QString AbstractItem::description() const
00032 {
00033     return "";
00034 }
00035 
00036 bool AbstractItem::isFavorite() const
00037 {
00038     return passesFiltering(Filter("favorite", true));
00039 }
00040 
00041 int AbstractItem::running() const
00042 {
00043     return 0;
00044 }
00045 
00046 bool AbstractItem::matches(const QString & pattern) const
00047 {
00048     return name().contains(pattern, Qt::CaseInsensitive) || description().contains(pattern, Qt::CaseInsensitive);
00049 }
00050 
00051 // DefaultFilterModel
00052 
00053 DefaultFilterModel::DefaultFilterModel(QObject * parent) :
00054     QStandardItemModel(0, 1, parent)
00055 {
00056     setHeaderData(1, Qt::Horizontal, tr("Filters"));
00057 }
00058 
00059 void DefaultFilterModel::addFilter(const QString & caption,
00060         const Filter & filter, const KIcon * icon)
00061 {
00062     QList<QStandardItem *> newRow;
00063     QStandardItem * item = new QStandardItem(caption);
00064     item->setData(qVariantFromValue<Filter>(filter));
00065     if (icon) item->setIcon(*icon);
00066 
00067     newRow << item;
00068     appendRow(newRow);
00069 }
00070 
00071 void DefaultFilterModel::addSeparator(const QString & caption)
00072 {
00073     QList<QStandardItem *> newRow;
00074     QStandardItem * item = new QStandardItem(caption);
00075     item->setEnabled(false);
00076 
00077     newRow << item;
00078     appendRow(newRow);
00079 }
00080 
00081 // DefaultItemFilterProxyModel
00082 
00083 DefaultItemFilterProxyModel::DefaultItemFilterProxyModel(QObject * parent) :
00084     QSortFilterProxyModel(parent), m_innerModel(parent)
00085 {
00086 }
00087 
00088 void DefaultItemFilterProxyModel::setSourceModel(QAbstractItemModel * sourceModel)
00089 {
00090     QStandardItemModel *model = qobject_cast<QStandardItemModel*>(sourceModel);
00091 
00092     if (!model) {
00093         kWarning() << "DefaultItemFilterProxyModel::setSourceModel expects a QStandardItemModel!";
00094         return;
00095     }
00096 
00097     m_innerModel.setSourceModel(model);
00098     QSortFilterProxyModel::setSourceModel(&m_innerModel);
00099 }
00100 
00101 QStandardItemModel * DefaultItemFilterProxyModel::sourceModel() const
00102 {
00103     return m_innerModel.sourceModel();
00104 }
00105 
00106 int DefaultItemFilterProxyModel::columnCount(const QModelIndex& index) const
00107 {
00108     Q_UNUSED(index);
00109     return 3;
00110 }
00111 
00112 QVariant DefaultItemFilterProxyModel::data(const QModelIndex & index, int role) const
00113 {
00114     return m_innerModel.data(index, (index.column() == 1), role);
00115 }
00116 
00117 bool DefaultItemFilterProxyModel::filterAcceptsRow(int sourceRow,
00118         const QModelIndex &sourceParent) const
00119 {
00120     QStandardItemModel * model = (QStandardItemModel *) sourceModel();
00121 
00122     QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
00123 
00124     AbstractItem * item = (AbstractItem *) model->itemFromIndex(index);
00125     //kDebug() << "ITEM " << (item ? "IS NOT " : "IS") << " NULL\n";
00126 
00127     return (m_filter.first.isEmpty() || item->passesFiltering(m_filter))
00128             &&(m_searchPattern.isEmpty() || item->matches(m_searchPattern));
00129 }
00130 
00131 bool DefaultItemFilterProxyModel::lessThan(const QModelIndex &left,
00132         const QModelIndex &right) const
00133 {
00134     return sourceModel()->data(left).toString().localeAwareCompare(sourceModel()->data(right).toString()) < 0;
00135 }
00136 
00137 void DefaultItemFilterProxyModel::setSearch(const QString & pattern)
00138 {
00139     m_searchPattern = pattern;
00140     invalidateFilter();
00141 }
00142 
00143 void DefaultItemFilterProxyModel::setFilter(const Filter & filter)
00144 {
00145     m_filter = filter;
00146     invalidateFilter();
00147 }
00148 
00149 // DefaultItemFilterProxyModel::InnerProxyModel
00150 
00151 DefaultItemFilterProxyModel::InnerProxyModel::InnerProxyModel(QObject * parent) :
00152     QAbstractItemModel(parent), m_sourceModel(NULL)
00153 {
00154 }
00155 
00156 Qt::ItemFlags DefaultItemFilterProxyModel::InnerProxyModel::flags(
00157         const QModelIndex & index) const
00158 {
00159     if (!m_sourceModel)
00160         return 0;
00161     return m_sourceModel->flags(index);
00162 }
00163 
00164 QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
00165         const QModelIndex & index, bool favoriteColumn, int role) const
00166 {
00167     Q_UNUSED(favoriteColumn);
00168     return data(index, role);
00169 }
00170 
00171 QVariant DefaultItemFilterProxyModel::InnerProxyModel::data(
00172         const QModelIndex & index, int role) const
00173 {
00174     if (!m_sourceModel)
00175         return QVariant();
00176     return m_sourceModel->data(index, role);
00177 }
00178 
00179 QVariant DefaultItemFilterProxyModel::InnerProxyModel::headerData(int section,
00180         Qt::Orientation orientation, int role) const
00181 {
00182     Q_UNUSED(orientation);
00183     Q_UNUSED(role);
00184     return QVariant(section);
00185 }
00186 
00187 int DefaultItemFilterProxyModel::InnerProxyModel::rowCount(
00188         const QModelIndex & parent) const
00189 {
00190     if (!m_sourceModel)
00191         return 0;
00192     return m_sourceModel->rowCount(parent);
00193 }
00194 
00195 bool DefaultItemFilterProxyModel::InnerProxyModel::setData(
00196         const QModelIndex & index, const QVariant & value, int role)
00197 {
00198     if (!m_sourceModel)
00199         return false;
00200     return m_sourceModel->setData(index, value, role);
00201 }
00202 
00203 bool DefaultItemFilterProxyModel::InnerProxyModel::setHeaderData(int section,
00204         Qt::Orientation orientation, const QVariant & value, int role)
00205 {
00206     Q_UNUSED(section);
00207     Q_UNUSED(value);
00208     Q_UNUSED(orientation);
00209     Q_UNUSED(role);
00210     return false;
00211 }
00212 
00213 QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::index(int row,
00214         int column, const QModelIndex & parent) const
00215 {
00216     Q_UNUSED(column);
00217     if (!m_sourceModel)
00218         return QModelIndex();
00219     return m_sourceModel->index(row, 0, parent);
00220 }
00221 
00222 QModelIndex DefaultItemFilterProxyModel::InnerProxyModel::parent(
00223         const QModelIndex & index) const
00224 {
00225     if (!m_sourceModel)
00226         return QModelIndex();
00227     return m_sourceModel->parent(index);
00228 }
00229 
00230 QMimeData * DefaultItemFilterProxyModel::InnerProxyModel::mimeData(
00231         const QModelIndexList & indexes) const
00232 {
00233     if (!m_sourceModel)
00234         return NULL;
00235     return m_sourceModel->mimeData(indexes);
00236 }
00237 
00238 int DefaultItemFilterProxyModel::InnerProxyModel::columnCount(
00239         const QModelIndex& index) const
00240 {
00241     Q_UNUSED(index);
00242     return 3; //FIXME: a hardcoded magic number that appears in two places CANNOT be good
00243 }
00244 
00245 void DefaultItemFilterProxyModel::InnerProxyModel::setSourceModel(
00246         QStandardItemModel * sourceModel)
00247 {
00248     m_sourceModel = sourceModel;
00249 }
00250 
00251 QStandardItemModel * DefaultItemFilterProxyModel::InnerProxyModel::sourceModel() const
00252 {
00253     return m_sourceModel;
00254 }
00255 
00256 // DefaultItemModel
00257 
00258 DefaultItemModel::DefaultItemModel(QObject * parent) :
00259     QStandardItemModel(parent) {}
00260 }

libplasma

Skip menu "libplasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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