]> SALOME platform Git repositories - modules/hexablock.git/blob - src/HEXABLOCKGUI/klinkitemselectionmodel.hxx
Salome HOME
Porting to Paraview 3.98.1
[modules/hexablock.git] / src / HEXABLOCKGUI / klinkitemselectionmodel.hxx
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 <QtCore/QObject>
26 #include <QtGui/QItemSelectionModel>
27 #include <QtGui/QAbstractProxyModel>
28
29 #include <iostream>
30
31 // #include "kdeui_export.h"
32 // #include "klinkitemselectionmodel_p.hxx"
33
34 #include "kmodelindexproxymapper.hxx"
35
36 class KLinkItemSelectionModelPrivate;
37
38 class KLinkItemSelectionModel : public QItemSelectionModel
39 {
40     Q_OBJECT
41 public:
42     KLinkItemSelectionModel(QAbstractItemModel *targetModel, QItemSelectionModel *linkedItemSelectionModel, QObject *parent = 0);
43     ~KLinkItemSelectionModel();
44     /* reimp */ void select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command);
45     /* reimp */ void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command);
46
47 protected:
48     KLinkItemSelectionModelPrivate * const d_ptr;
49
50 private:
51     Q_DECLARE_PRIVATE(KLinkItemSelectionModel)
52     Q_PRIVATE_SLOT( d_func(), void sourceSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected))
53
54 };
55
56
57 // QAbstractProxyModel::mapSelectionFromSource creates invalid ranges to we filter
58 // those out manually in a loop. Hopefully fixed in Qt 4.7.2, so we ifdef it out.
59 // http://qt.gitorious.org/qt/qt/merge_requests/2474
60 // http://qt.gitorious.org/qt/qt/merge_requests/831
61 #if QT_VERSION < 0x040702
62 #define RANGE_FIX_HACK
63 #endif
64
65 #ifdef RANGE_FIX_HACK
66 static QItemSelection klink_removeInvalidRanges(const QItemSelection &selection)
67 {
68 //   std::cout<< " klink_removeInvalidRanges " << std::endl;
69   QItemSelection result;
70   Q_FOREACH(const QItemSelectionRange &range, selection)
71   {
72
73 //    Q_FOREACH(const QModelIndex &i, range.indexes ())
74 //    {
75 ////       std::cout<< " =====> " << i.data().toString().toStdString() << std::endl;
76 //    }
77     if (!range.isValid())
78       continue;
79 //     std::cout<< " is VALID !!"<< std::endl;
80     result << range;
81   }
82   return result;
83 }
84 #endif
85
86
87 class KLinkItemSelectionModelPrivate
88 {
89 public:
90     KLinkItemSelectionModelPrivate(KLinkItemSelectionModel *proxySelectionModel, QAbstractItemModel *model,
91                                     QItemSelectionModel *linkedItemSelectionModel)
92       : q_ptr(proxySelectionModel),
93         m_model(model),
94         m_linkedItemSelectionModel(linkedItemSelectionModel),
95         m_ignoreCurrentChanged(false),
96         m_indexMapper(new KModelIndexProxyMapper(model, linkedItemSelectionModel->model(), proxySelectionModel))
97     {
98     }
99
100     Q_DECLARE_PUBLIC(KLinkItemSelectionModel)
101     KLinkItemSelectionModel * const q_ptr;
102
103
104     bool assertSelectionValid(const QItemSelection &selection) const {
105       foreach(const QItemSelectionRange &range, selection) {
106 //         if (!range.isValid()) {
107 //           kDebug() << selection;
108 //         }
109         
110         Q_ASSERT(range.isValid());
111       }
112       return true;
113     }
114
115 void sourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
116 {
117
118     
119     Q_Q(KLinkItemSelectionModel);
120 #ifdef RANGE_FIX_HACK
121 //     std::cout<<"XXXXXXXXXXXXX  sourceSelectionChanged!!!!!!!! RANGE_FIX_HACK"<<std::endl;
122     QItemSelection _selected = klink_removeInvalidRanges(selected);
123     QItemSelection _deselected = klink_removeInvalidRanges(deselected);
124 #else
125 //     std::cout<<"XXXXXXXXXXXXX  sourceSelectionChanged!!!!!!!! "<<std::endl;
126     QItemSelection _selected = selected;
127     QItemSelection _deselected = deselected;
128 #endif
129     Q_ASSERT(assertSelectionValid(_selected));
130     Q_ASSERT(assertSelectionValid(_deselected));
131
132 //     std::cout << "XXXXXXXXXXXXX  _selected.count() "   << _selected.count() << std::endl;
133 //     std::cout << "XXXXXXXXXXXXX  _deselected.count() " << _deselected.count() << std::endl;
134
135     const QItemSelection mappedDeselection = m_indexMapper->mapSelectionRightToLeft(_deselected);
136     const QItemSelection mappedSelection   = m_indexMapper->mapSelectionRightToLeft(_selected);
137
138 //     const QItemSelection mappedDeselection = _deselected;
139 //     const QItemSelection mappedSelection   = _selected;
140
141 //     std::cout << "XXXXXXXXXXXXX  mappedSelection.count() "   << mappedSelection.count() << std::endl;
142 //     std::cout << "XXXXXXXXXXXXX  mappedDeselection.count() " << mappedDeselection.count() << std::endl;
143
144     q->QItemSelectionModel::select(mappedDeselection, QItemSelectionModel::Deselect);
145     q->QItemSelectionModel::select(mappedSelection, QItemSelectionModel::Select);
146
147 //     q->select(mappedDeselection, QItemSelectionModel::Deselect);
148 //     q->select(mappedSelection, QItemSelectionModel::Select);
149
150
151 }
152
153     QAbstractItemModel * const m_model;
154     QItemSelectionModel * const m_linkedItemSelectionModel;
155     bool m_ignoreCurrentChanged;
156     KModelIndexProxyMapper * const m_indexMapper;
157 };
158
159
160
161 #endif