~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/plasmagenericshell/widgetsexplorer/kcategorizeditemsviewmodels_p.h

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
 
3
 *
 
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
 
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/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.
 
18
 */
 
19
 
 
20
#ifndef PLASMA_KCATEGORIZEDITEMSVIEWMODELS_P_H
 
21
#define PLASMA_KCATEGORIZEDITEMSVIEWMODELS_P_H
 
22
 
 
23
#include <QtGui/QtGui>
 
24
#include <QtCore/QtCore>
 
25
#include <KIcon>
 
26
#include <KDebug>
 
27
 
 
28
namespace KCategorizedItemsViewModels {
 
29
 
 
30
typedef QPair<QString, QVariant> Filter;
 
31
 
 
32
/**
 
33
 * Abstract class that needs to be implemented and used with the ItemModel
 
34
 */
 
35
class AbstractItem : public QStandardItem
 
36
{
 
37
public:
 
38
    /**
 
39
     * Returns a localized string - name of the item
 
40
     */
 
41
    virtual QString name() const;
 
42
 
 
43
    /**
 
44
     * Returns a unique id related to this item
 
45
     */
 
46
    virtual QString id() const;
 
47
 
 
48
    /**
 
49
     * Returns a localized string - description of the item
 
50
     */
 
51
    virtual QString description() const;
 
52
 
 
53
    /**
 
54
     * Returns if the item is flagged as favorite
 
55
     * Default implementation checks if the item passes the Filter("favorite", "1") filter
 
56
     */
 
57
    virtual bool isFavorite() const;
 
58
 
 
59
    /**
 
60
     * Returns the item's number of running applets
 
61
     * Default implementation just returns 0
 
62
     */
 
63
    virtual int running() const;
 
64
 
 
65
    /**
 
66
     * Returns if the item contains string specified by pattern.
 
67
     * Default implementation checks whether name or description contain the
 
68
     * string (not needed to be exactly that string)
 
69
     */
 
70
    virtual bool matches(const QString &pattern) const;
 
71
 
 
72
    /**
 
73
     * sets the favorite flag for the item
 
74
     */
 
75
    virtual void setFavorite(bool favorite) = 0;
 
76
    /**
 
77
     * sets the number of running applets for the item
 
78
     */
 
79
    virtual void setRunning(int count) = 0;
 
80
 
 
81
    /**
 
82
     * Returns if the item passes the filter specified
 
83
     */
 
84
    virtual bool passesFiltering(const Filter &filter) const = 0;
 
85
private:
 
86
};
 
87
 
 
88
/**
 
89
 * The default implementation of the model containing filters
 
90
 */
 
91
class DefaultFilterModel : public QStandardItemModel
 
92
{
 
93
public:
 
94
    DefaultFilterModel(QObject *parent = 0);
 
95
 
 
96
    /**
 
97
     * Adds a filter to the model
 
98
     * @param caption The localized string to be displayed as a name of the filter
 
99
     * @param filter The filter structure
 
100
     */
 
101
    void addFilter(const QString &caption, const Filter &filter, const KIcon &icon = KIcon());
 
102
 
 
103
    /**
 
104
     * Adds a separator to the model
 
105
     * @param caption The localized string to be displayed as a name of the separator
 
106
     */
 
107
    void addSeparator(const QString &caption);
 
108
};
 
109
 
 
110
/**
 
111
 * Default filter proxy model.
 
112
 */
 
113
class DefaultItemFilterProxyModel : public QSortFilterProxyModel
 
114
{
 
115
Q_OBJECT
 
116
 
 
117
public:
 
118
    DefaultItemFilterProxyModel(QObject *parent = 0);
 
119
 
 
120
    bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
 
121
    bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
 
122
 
 
123
    void setSearch(const QString &pattern);
 
124
    void setFilter(const Filter &filter);
 
125
 
 
126
    void setSourceModel(QAbstractItemModel *sourceModel);
 
127
 
 
128
    QStandardItemModel *sourceModel() const;
 
129
 
 
130
    int columnCount(const QModelIndex &index) const;
 
131
 
 
132
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
 
133
 
 
134
Q_SIGNALS:
 
135
    void searchTermChanged(const QString &term);
 
136
    void filterChanged();
 
137
 
 
138
private:
 
139
 
 
140
    class InnerProxyModel : public QAbstractItemModel
 
141
    {
 
142
    public:
 
143
        InnerProxyModel(QObject *parent = 0);
 
144
 
 
145
        Qt::ItemFlags flags(const QModelIndex &index) const;
 
146
 
 
147
        QVariant data(const QModelIndex &index, bool favoriteColumn,
 
148
                      int role = Qt::DisplayRole) const;
 
149
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
 
150
        bool setData(const QModelIndex &index, const QVariant &value,
 
151
                     int role = Qt::EditRole);
 
152
 
 
153
        QVariant headerData(int section, Qt::Orientation orientation,
 
154
                            int role = Qt::DisplayRole) const;
 
155
        bool setHeaderData(int section, Qt::Orientation orientation,
 
156
                           const QVariant &value, int role = Qt::EditRole);
 
157
 
 
158
        int rowCount(const QModelIndex &parent = QModelIndex()) const;
 
159
        int columnCount(const QModelIndex &index) const;
 
160
 
 
161
        QModelIndex index(int row, int column,
 
162
                          const QModelIndex &parent = QModelIndex()) const;
 
163
        QModelIndex parent(const QModelIndex &index) const;
 
164
 
 
165
        QMimeData *mimeData(const QModelIndexList &indexes) const;
 
166
 
 
167
        void setSourceModel(QStandardItemModel *sourceModel);
 
168
        QStandardItemModel *sourceModel() const;
 
169
 
 
170
    private:
 
171
        QStandardItemModel *m_sourceModel;
 
172
    };
 
173
 
 
174
    Filter m_filter;
 
175
    QString m_searchPattern;
 
176
    InnerProxyModel m_innerModel;
 
177
 
 
178
};
 
179
 
 
180
} //end of namespace
 
181
 
 
182
Q_DECLARE_METATYPE(KCategorizedItemsViewModels::Filter)
 
183
 
 
184
#endif /*KCATEGORIZEDITEMSVIEWMODELS_H_*/