1/*
2 This file is part of Akonadi.
3
4 Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 USA.
20*/
21
22#ifndef KCHECKABLEPROXYMODEL_H
23#define KCHECKABLEPROXYMODEL_H
24
25#include "kidentityproxymodel.h"
26
27#include "kdeui_export.h"
28
29#include <QItemSelection>
30
31class KCheckableProxyModelPrivate;
32
33/**
34 * @brief Adds a checkable capability to a source model
35 *
36 * Items is standard Qt views such as QTreeView and QListView can have a
37 * checkable capability and draw checkboxes. Adding such a capability
38 * requires specific implementations of the data() and flags() virtual methods.
39 * This class implements those methods generically so that it is not necessary to
40 * implement them in your model.
41 *
42 * This can be combined with a KSelectionProxyModel showing the items currently selected
43 *
44 * @code
45 *
46 * QItemSelectionModel *checkModel = new QItemSelectionModel(rootModel, this);
47 * KCheckableProxyModel *checkable = new KCheckableProxyModel(this);
48 * checkable->setSourceModel(rootModel);
49 * checkable->setSelectionModel(checkModel);
50 *
51 * QTreeView *tree1 = new QTreeView(vSplitter);
52 * tree1->setModel(checkable);
53 * tree1->expandAll();
54 *
55 * KSelectionProxyModel *selectionProxy = new KSelectionProxyModel(checkModel, this);
56 * selectionProxy->setFilterBehavior(KSelectionProxyModel::ExactSelection);
57 * selectionProxy->setSourceModel(rootModel);
58 *
59 * QTreeView *tree2 = new QTreeView(vSplitter);
60 * tree2->setModel(selectionProxy);
61 * @endcode
62 *
63 * @image html kcheckableproxymodel.png "A KCheckableProxyModel and KSelectionProxyModel showing checked items"
64 *
65 * @since 4.6
66 * @author Stephen Kelly <steveire@gmail.com>
67 */
68class KDEUI_EXPORT KCheckableProxyModel : public KIdentityProxyModel
69{
70 Q_OBJECT
71public:
72 KCheckableProxyModel(QObject* parent = 0);
73 ~KCheckableProxyModel();
74
75 void setSelectionModel(QItemSelectionModel *itemSelectionModel);
76 QItemSelectionModel *selectionModel() const;
77
78 /* reimp */ Qt::ItemFlags flags(const QModelIndex& index) const;
79
80 /* reimp */ QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
81
82 /* reimp */ bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
83
84 /* reimp */ void setSourceModel(QAbstractItemModel* sourceModel);
85
86protected:
87 virtual bool select( const QItemSelection &selection, QItemSelectionModel::SelectionFlags command );
88
89private:
90 Q_DECLARE_PRIVATE(KCheckableProxyModel )
91 KCheckableProxyModelPrivate * const d_ptr;
92
93 Q_PRIVATE_SLOT(d_func(), void selectionChanged(const QItemSelection &, const QItemSelection &) )
94};
95
96#endif
97
98