Salome HOME
8304b3d7f76a22f1cc55d9e9080ed7a69fd3ad71
[modules/hexablock.git] / src / HEXABLOCKGUI / klinkitemselectionmodel.cxx
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 #include <QtCore/QObject>
23 #include "klinkitemselectionmodel.hxx"
24 // #include "klinkitemselectionmodel_p.hxx"
25
26 #include "kmodelindexproxymapper.hxx"
27
28 // #include "kdebug.h"
29
30 #include <QItemSelection>
31
32 #include <iostream>
33
34
35 KLinkItemSelectionModel::KLinkItemSelectionModel(QAbstractItemModel *model, QItemSelectionModel *proxySelector, QObject *parent)
36         : QItemSelectionModel(model, parent),
37         d_ptr(new KLinkItemSelectionModelPrivate(this, model, proxySelector))
38 {
39     connect(proxySelector, SIGNAL(selectionChanged(QItemSelection, QItemSelection)), SLOT(sourceSelectionChanged(QItemSelection, QItemSelection)));
40 }
41
42 KLinkItemSelectionModel::~KLinkItemSelectionModel()
43 {
44     delete d_ptr;
45 }
46
47 void KLinkItemSelectionModel::select(const QModelIndex &index, QItemSelectionModel::SelectionFlags command)
48 {
49     //std::cout<<"KLinkItemSelectionModel::select(const QModelIndex &index"<<std::endl;
50     Q_D(KLinkItemSelectionModel);
51     // When an item is removed, the current index is set to the top index in the model.
52     // That causes a selectionChanged signal with a selection which we do not want.
53     if (d->m_ignoreCurrentChanged) {
54         return;
55     }
56     QItemSelectionModel::select(index, command);
57     if (index.isValid())
58         d->m_linkedItemSelectionModel->select(d->m_indexMapper->mapSelectionLeftToRight(QItemSelection(index, index)), command);
59     else {
60         d->m_linkedItemSelectionModel->clearSelection();
61     }
62 }
63
64 // // QAbstractProxyModel::mapSelectionFromSource creates invalid ranges to we filter
65 // // those out manually in a loop. Hopefully fixed in Qt 4.7.2, so we ifdef it out.
66 // // http://qt.gitorious.org/qt/qt/merge_requests/2474
67 // // http://qt.gitorious.org/qt/qt/merge_requests/831
68 // #if QT_VERSION < 0x040702
69 // #define RANGE_FIX_HACK
70 // #endif
71 // 
72 // #ifdef RANGE_FIX_HACK
73 // static QItemSelection klink_removeInvalidRanges(const QItemSelection &selection)
74 // {
75 //   QItemSelection result;
76 //   Q_FOREACH(const QItemSelectionRange &range, selection)
77 //   {
78 //     if (!range.isValid())
79 //       continue;
80 //     result << range;
81 //   }
82 //   return result;
83 // }
84 // #endif
85
86 void KLinkItemSelectionModel::select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
87 {
88 //  std::cout<<"KLinkItemSelectionModel::select(const QItemSelection &selection"<<std::endl;
89
90   Q_FOREACH(const QItemSelectionRange &range, selection){
91 //     if (range.isEmpty()){
92 //       std::cout<<"range    EMPTY"<<std::endl;
93 //     } else {
94 //       std::cout<<"range    NOT EMPTY"<<std::endl;
95 //     }
96 //    if (range.isValid()){
97 //      std::cout<<"range    VALID"<<std::endl;
98 //      std::cout<<"count           =>"<<range.indexes().count()<<std::endl;
99 //      Q_FOREACH(const QModelIndex &i, range.indexes())
100 //        std::cout<< " KLinkItemSelectionModel::select =====> " << i.data().toString().toStdString() << std::endl;
101 //    } else {
102 //      std::cout<<"range    NOT VALID"<<std::endl;
103 //    }
104   }
105
106
107     Q_D(KLinkItemSelectionModel);
108     d->m_ignoreCurrentChanged = true;
109 #ifdef RANGE_FIX_HACK
110     QItemSelection _selection = klink_removeInvalidRanges(selection);
111 #else
112     QItemSelection _selection = selection;
113 #endif
114     QItemSelectionModel::select(_selection, command);
115     Q_ASSERT(d->assertSelectionValid(_selection));
116     QItemSelection mappedSelection = d->m_indexMapper->mapSelectionLeftToRight(_selection);
117     Q_ASSERT(d->assertSelectionValid(mappedSelection));
118     d->m_linkedItemSelectionModel->select(mappedSelection, command);
119     d->m_ignoreCurrentChanged = false;
120 }
121
122
123
124 // #include "klinkitemselectionmodel_moc.cxx"
125 // #include "klinkitemselectionmodel.moc"
126