• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

PlasmaCore

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • core
datamodel.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 by Marco MArtin <mart@kde.org>
3 
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
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
13  *
14  * You should have received a copy of the GNU Library 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.
18  */
19 
20 #ifndef DATAMODEL_H
21 #define DATAMODEL_H
22 
23 #include <QAbstractItemModel>
24 #include <QSortFilterProxyModel>
25 #include <QVector>
26 
27 #include <Plasma/DataEngine>
28 
29 class QTimer;
30 
31 namespace Plasma
32 {
33 
34 class DataSource;
35 class DataModel;
36 
37 
38 class SortFilterModel : public QSortFilterProxyModel
39 {
40  Q_OBJECT
44  Q_PROPERTY(QAbstractItemModel *sourceModel READ sourceModel WRITE setModel NOTIFY sourceModelChanged)
45 
46 
49  Q_PROPERTY(QString filterRegExp READ filterRegExp WRITE setFilterRegExp NOTIFY filterRegExpChanged)
50 
54  Q_PROPERTY(QString filterRole READ filterRole WRITE setFilterRole)
55 
59  Q_PROPERTY(QString sortRole READ sortRole WRITE setSortRole)
60 
64  Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
65 
69  Q_PROPERTY(Qt::CaseSensitivity sortCaseSensitivity READ sortCaseSensitivity WRITE setSortCaseSensitivity)
70 
74  Q_PROPERTY(int count READ count NOTIFY countChanged)
75 
76  friend class DataModel;
77 
78 public:
79  SortFilterModel(QObject* parent=0);
80  ~SortFilterModel();
81 
82  void setModel(QAbstractItemModel *source);
83 
84  void setFilterRegExp(const QString &exp);
85  QString filterRegExp() const;
86 
87  void setFilterRole(const QString &role);
88  QString filterRole() const;
89 
90  void setSortRole(const QString &role);
91  QString sortRole() const;
92 
93  void setSortOrder(const Qt::SortOrder order);
94 
95  int count() const {return QSortFilterProxyModel::rowCount();}
96 
104  Q_INVOKABLE QVariantHash get(int i) const;
105 
106  Q_INVOKABLE int mapRowToSource(int i) const;
107 
108  Q_INVOKABLE int mapRowFromSource(int i) const;
109 
110 Q_SIGNALS:
111  void countChanged();
112  void sourceModelChanged(QObject *);
113  void filterRegExpChanged(const QString &);
114  void sortOrderChanged(const Qt::SortOrder);
115 
116 protected:
117  int roleNameToId(const QString &name);
118 
119 protected Q_SLOTS:
120  void syncRoleNames();
121 
122 private:
123  QString m_filterRole;
124  QString m_sortRole;
125  QHash<QString, int> m_roleIds;
126 };
127 
128 class DataModel : public QAbstractItemModel
129 {
130  Q_OBJECT
131 
135  Q_PROPERTY(QObject *dataSource READ dataSource WRITE setDataSource)
136 
137 
140  Q_PROPERTY(QString keyRoleFilter READ keyRoleFilter WRITE setKeyRoleFilter)
141 
146  Q_PROPERTY(QString sourceFilter READ sourceFilter WRITE setSourceFilter)
147 
151  Q_PROPERTY(int count READ count NOTIFY countChanged)
152 
153 public:
154  DataModel(QObject* parent=0);
155  ~DataModel();
156 
157  void setDataSource(QObject *source);
158  QObject *dataSource() const;
159 
163  void setKeyRoleFilter(const QString& key);
164  QString keyRoleFilter() const;
165 
169  void setSourceFilter(const QString& key);
170  QString sourceFilter() const;
171 
172  int roleNameToId(const QString &name);
173 
174  //Reimplemented
175  QVariant data(const QModelIndex &index, int role) const;
176  QVariant headerData(int section, Qt::Orientation orientation,
177  int role = Qt::DisplayRole) const;
178  QModelIndex index(int row, int column,
179  const QModelIndex &parent = QModelIndex()) const;
180  QModelIndex parent(const QModelIndex &child) const;
181  int rowCount(const QModelIndex &parent = QModelIndex()) const;
182  int columnCount(const QModelIndex &parent = QModelIndex()) const;
183 
184  int count() const {return countItems();}
185 
193  Q_INVOKABLE QVariantHash get(int i) const;
194 
195 protected:
196  void setItems(const QString &sourceName, const QVariantList &list);
197  inline int countItems() const;
198 
199 Q_SIGNALS:
200  void countChanged();
201  void sourceModelChanged(QObject *);
202  void filterRegExpChanged(const QString &);
203 
204 private Q_SLOTS:
205  void dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data);
206  void removeSource(const QString &sourceName);
207 
208 private:
209  DataSource *m_dataSource;
210  QString m_keyRoleFilter;
211  QRegExp m_keyRoleFilterRE;
212  QString m_sourceFilter;
213  QRegExp m_sourceFilterRE;
214  QMap<QString, QVector<QVariant> > m_items;
215  QHash<int, QByteArray> m_roleNames;
216  QHash<QString, int> m_roleIds;
217  int m_maxRoleId;
218 };
219 
220 int DataModel::countItems() const
221 {
222  int count = 0;
223  foreach (const QVector<QVariant> &v, m_items) {
224  count += v.count();
225  }
226  return count;
227 }
228 
229 }
230 #endif
QObject::child
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
Plasma::SortFilterModel::setFilterRole
void setFilterRole(const QString &role)
Definition: datamodel.cpp:107
QModelIndex
Plasma::SortFilterModel
Definition: datamodel.h:38
Plasma::DataModel::setSourceFilter
void setSourceFilter(const QString &key)
Include only sources that matches this regexp in the model.
Definition: datamodel.cpp:299
Plasma::DataModel::dataSource
QObject * dataSource() const
Plasma::SortFilterModel::sortRole
QString sortRole() const
Plasma::SortFilterModel::filterRegExpChanged
void filterRegExpChanged(const QString &)
Plasma::DataModel::sourceModelChanged
void sourceModelChanged(QObject *)
Plasma::SortFilterModel::filterRegExp
QString filterRegExp() const
QMap
Plasma::SortFilterModel::countChanged
void countChanged()
QSortFilterProxyModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
Plasma::DataModel::keyRoleFilter
QString keyRoleFilter() const
Plasma::DataModel
Definition: datamodel.h:128
Plasma::DataModel::setKeyRoleFilter
void setKeyRoleFilter(const QString &key)
Include only items with a key that matches this regexp in the model.
Definition: datamodel.cpp:277
Plasma::DataModel::countChanged
void countChanged()
QRegExp
QObject::name
const char * name() const
Plasma::DataModel::roleNameToId
int roleNameToId(const QString &name)
Definition: datamodel.cpp:511
Plasma::DataModel::setDataSource
void setDataSource(QObject *source)
Definition: datamodel.cpp:242
QTimer
QHash< QString, int >
QObject
Plasma::SortFilterModel::mapRowToSource
Q_INVOKABLE int mapRowToSource(int i) const
Definition: datamodel.cpp:156
Plasma::DataModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: datamodel.cpp:478
Plasma::DataModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: datamodel.cpp:453
QString
Plasma::SortFilterModel::mapRowFromSource
Q_INVOKABLE int mapRowFromSource(int i) const
Definition: datamodel.cpp:162
QSortFilterProxyModel
Plasma::DataModel::countItems
int countItems() const
Definition: datamodel.h:220
Plasma::SortFilterModel::setSortOrder
void setSortOrder(const Qt::SortOrder order)
Definition: datamodel.cpp:134
Plasma::DataModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: datamodel.cpp:419
QAbstractProxyModel::sourceModel
QAbstractItemModel * sourceModel() const
QSortFilterProxyModel::sortCaseSensitivity
Qt::CaseSensitivity sortCaseSensitivity() const
QVector
Plasma::SortFilterModel::count
int count() const
Definition: datamodel.h:95
QSortFilterProxyModel::sortOrder
Qt::SortOrder sortOrder() const
Plasma::DataModel::setItems
void setItems(const QString &sourceName, const QVariantList &list)
Definition: datamodel.cpp:321
Plasma::SortFilterModel::syncRoleNames
void syncRoleNames()
Definition: datamodel.cpp:48
Plasma::SortFilterModel::filterRole
QString filterRole() const
Plasma::SortFilterModel::sourceModelChanged
void sourceModelChanged(QObject *)
QVector::count
int count(const T &value) const
Plasma::SortFilterModel::setSortRole
void setSortRole(const QString &role)
Definition: datamodel.cpp:118
Plasma::DataModel::sourceFilter
QString sourceFilter() const
QAbstractItemModel
Plasma::SortFilterModel::setModel
void setModel(QAbstractItemModel *source)
Definition: datamodel.cpp:73
Plasma::DataModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: datamodel.cpp:462
Plasma::DataModel::count
int count() const
Definition: datamodel.h:184
Plasma::DataSource
Definition: datasource.h:42
Plasma::SortFilterModel::sortOrderChanged
void sortOrderChanged(const Qt::SortOrder)
QObject::parent
QObject * parent() const
Plasma::SortFilterModel::setFilterRegExp
void setFilterRegExp(const QString &exp)
Definition: datamodel.cpp:93
Plasma::SortFilterModel::roleNameToId
int roleNameToId(const QString &name)
Definition: datamodel.cpp:65
Plasma::DataModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: datamodel.cpp:489
Plasma::DataModel::filterRegExpChanged
void filterRegExpChanged(const QString &)
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

PlasmaCore

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

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal