1/*
2 Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3 a KDAB Group company, info@kdab.net,
4 author Stephen Kelly <stephen@kdab.com>
5
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
10
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20*/
21
22#ifndef KLINKITEMSELECTIONMODEL_H
23#define KLINKITEMSELECTIONMODEL_H
24
25#include <QtGui/QItemSelectionModel>
26#include <QtGui/QAbstractProxyModel>
27
28#include "kdeui_export.h"
29
30class KLinkItemSelectionModelPrivate;
31
32/**
33 @brief Makes it possible to share a selection in multiple views which do not have the same source model
34
35 Although <a href="http://doc.trolltech.com/4.6/model-view-view.html#sharing-selections-between-views">multiple views can share the same QItemSelectionModel</a>, the views then need to have the same source model.
36
37 If there is a proxy model between the model and one of the views, or different proxy models in each, this class makes
38 it possible to share the selection between the views.
39
40 @image html kproxyitemselectionmodel-simple.png "Sharing a QItemSelectionModel between views on the same model is trivial"
41 @image html kproxyitemselectionmodel-error.png "If a proxy model is used, it is no longer possible to share the QItemSelectionModel directly"
42 @image html kproxyitemselectionmodel-solution.png "A KLinkItemSelectionModel can be used to map the selection through the proxy model"
43
44 @code
45 QAbstractItemModel *model = getModel();
46
47 QSortFilterProxyModel *proxy = new QSortFilterProxyModel();
48 proxy->setSourceModel(model);
49
50 QTreeView *view1 = new QTreeView(splitter);
51 view1->setModel(model);
52
53 KLinkItemSelectionModel *view2SelectionModel = new KLinkItemSelectionModel( proxy, view1->selectionModel());
54
55 QTreeView *view2 = new QTreeView(splitter);
56 // Note that the QAbstractItemModel passed to KLinkItemSelectionModel must be the same as what is used in the view
57 view2->setModel(proxy);
58 view2->setSelectionModel( view2SelectionModel );
59 @endcode
60
61 @image html kproxyitemselectionmodel-complex.png "Arbitrarily complex proxy configurations on the same root model can be used"
62
63 @code
64 QAbstractItemModel *model = getModel();
65
66 QSortFilterProxyModel *proxy1 = new QSortFilterProxyModel();
67 proxy1->setSourceModel(model);
68 QSortFilterProxyModel *proxy2 = new QSortFilterProxyModel();
69 proxy2->setSourceModel(proxy1);
70 QSortFilterProxyModel *proxy3 = new QSortFilterProxyModel();
71 proxy3->setSourceModel(proxy2);
72
73 QTreeView *view1 = new QTreeView(splitter);
74 view1->setModel(proxy3);
75
76 QSortFilterProxyModel *proxy4 = new QSortFilterProxyModel();
77 proxy4->setSourceModel(model);
78 QSortFilterProxyModel *proxy5 = new QSortFilterProxyModel();
79 proxy5->setSourceModel(proxy4);
80
81 KLinkItemSelectionModel *view2SelectionModel = new KLinkItemSelectionModel( proxy5, view1->selectionModel());
82
83 QTreeView *view2 = new QTreeView(splitter);
84 // Note that the QAbstractItemModel passed to KLinkItemSelectionModel must be the same as what is used in the view
85 view2->setModel(proxy5);
86 view2->setSelectionModel( view2SelectionModel );
87 @endcode
88
89 See also <a href="http://websvn.kde.org/trunk/KDE/kdelibs/kdeui/tests/proxymodeltestapp/proxyitemselectionwidget.cpp?view=markup">kdelibs/kdeui/tests/proxymodeltestapp/proxyitemselectionwidget.cpp</a>.
90
91 @since 4.5
92 @author Stephen Kelly <steveire@gmail.com>
93
94*/
95class KDEUI_EXPORT KLinkItemSelectionModel : public QItemSelectionModel
96{
97 Q_OBJECT
98public:
99 /**
100 Constructor.
101 */
102 KLinkItemSelectionModel(QAbstractItemModel *targetModel, QItemSelectionModel *linkedItemSelectionModel, QObject *parent = 0);
103 ~KLinkItemSelectionModel();
104 /* reimp */ void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
105 /* reimp */ void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
106
107protected:
108 KLinkItemSelectionModelPrivate * const d_ptr;
109
110private:
111 Q_DECLARE_PRIVATE(KLinkItemSelectionModel)
112 Q_PRIVATE_SLOT( d_func(), void sourceSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected))
113 Q_PRIVATE_SLOT( d_func(), void sourceCurrentChanged(const QModelIndex &current))
114 Q_PRIVATE_SLOT( d_func(), void slotCurrentChanged(const QModelIndex &current))
115};
116
117#endif
118