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