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