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