Even though the last alpha was .91 this one is going to be 82 as I'd like to have...
[kdevelopdvcssupport.git] / plugins / projectmanagerview / proxyselectionmodel.cpp
blob890c9e6bb853bc6a804aea981eb40fb396ea7135
1 /* This file is part of KDevelop
2 Copyright 2008 Andreas Pakulat <apaku@gmx.de>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "proxyselectionmodel.h"
21 #include <QAbstractProxyModel>
22 #include <QAbstractItemView>
24 ProxySelectionModel::ProxySelectionModel( QAbstractItemView* itemview, QItemSelectionModel* sourceSelectionModel, QObject* parent )
25 : QItemSelectionModel( itemview->model(), parent ), proxyModel(0), selectionModel( sourceSelectionModel ), view( itemview ), doingUpdate( false )
27 proxyModel = dynamic_cast<QAbstractProxyModel*>( view->model() );
28 if( proxyModel )
30 connect( selectionModel, SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
31 this, SLOT(changeCurrent( const QModelIndex&, const QModelIndex& ) ) );
32 connect( selectionModel, SIGNAL( currentColumnChanged( const QModelIndex&, const QModelIndex& ) ),
33 this, SLOT(changeCurrentColumn( const QModelIndex&, const QModelIndex& ) ) );
34 connect( selectionModel, SIGNAL( currentRowChanged( const QModelIndex&, const QModelIndex& ) ),
35 this, SLOT(changeCurrentRow( const QModelIndex&, const QModelIndex& ) ) );
36 connect( selectionModel, SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ),
37 this, SLOT(changeSelection( const QItemSelection&, const QItemSelection& ) ) );
39 connect( this, SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
40 this, SLOT(forwardChangeCurrent( const QModelIndex&, const QModelIndex& ) ) );
41 connect( this, SIGNAL( currentColumnChanged( const QModelIndex&, const QModelIndex& ) ),
42 this, SLOT(forwardChangeCurrentColumn( const QModelIndex&, const QModelIndex& ) ) );
43 connect( this, SIGNAL( currentRowChanged( const QModelIndex&, const QModelIndex& ) ),
44 this, SLOT(forwardChangeCurrentRow( const QModelIndex&, const QModelIndex& ) ) );
45 connect( this, SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ),
46 this, SLOT(forwardChangeSelection( const QItemSelection&, const QItemSelection& ) ) );
48 view->setSelectionModel(this);
51 void ProxySelectionModel::changeCurrent( const QModelIndex& current, const QModelIndex& previous )
53 if( doingUpdate )
54 return;
55 doingUpdate = true;
56 emit currentChanged( proxyModel->mapFromSource( current ), proxyModel->mapFromSource( previous ) );
57 doingUpdate = false;
60 void ProxySelectionModel::changeCurrentColumn( const QModelIndex& current, const QModelIndex& previous )
62 if( doingUpdate )
63 return;
64 doingUpdate = true;
65 emit currentColumnChanged( proxyModel->mapFromSource( current ), proxyModel->mapFromSource( previous ) );
66 doingUpdate = false;
69 void ProxySelectionModel::changeCurrentRow( const QModelIndex& current, const QModelIndex& previous )
71 if( doingUpdate )
72 return;
73 doingUpdate = true;
74 emit currentRowChanged( proxyModel->mapFromSource( current ), proxyModel->mapFromSource( previous ) );
75 doingUpdate = false;
78 void ProxySelectionModel::changeSelection( const QItemSelection& selected, const QItemSelection& deselected )
80 if( doingUpdate )
81 return;
82 doingUpdate = true;
83 emit selectionChanged( proxyModel->mapSelectionFromSource( selected ), proxyModel->mapSelectionFromSource( deselected ) );
84 doingUpdate = false;
87 QItemSelectionModel::SelectionFlags ProxySelectionModel::selectionFlags() const
89 QItemSelectionModel::SelectionFlags f;
90 if( view->selectionMode() == QAbstractItemView::SingleSelection )
92 f = QItemSelectionModel::ClearAndSelect;
93 if( view->selectionBehavior() == QAbstractItemView::SelectItems )
94 f |= QItemSelectionModel::NoUpdate;
95 if( view->selectionBehavior() == QAbstractItemView::SelectRows )
96 f |= QItemSelectionModel::Rows;
97 if( view->selectionBehavior() == QAbstractItemView::SelectColumns )
98 f |= QItemSelectionModel::Columns;
100 else
101 f = QItemSelectionModel::NoUpdate;
102 return f;
105 void ProxySelectionModel::forwardChangeCurrent( const QModelIndex& current, const QModelIndex& previous )
107 if( doingUpdate )
108 return;
109 doingUpdate = true;
110 selectionModel->setCurrentIndex( proxyModel->mapToSource( current ), selectionFlags() );
111 doingUpdate = false;
114 void ProxySelectionModel::forwardChangeCurrentColumn( const QModelIndex& current, const QModelIndex& previous )
116 if( doingUpdate )
117 return;
118 doingUpdate = true;
119 selectionModel->setCurrentIndex( proxyModel->mapToSource( current ), selectionFlags() );
120 doingUpdate = false;
123 void ProxySelectionModel::forwardChangeCurrentRow( const QModelIndex& current, const QModelIndex& previous )
125 if( doingUpdate )
126 return;
127 doingUpdate = true;
128 selectionModel->setCurrentIndex( proxyModel->mapToSource( current ), selectionFlags() );
129 doingUpdate = false;
132 void ProxySelectionModel::forwardChangeSelection( const QItemSelection& selected, const QItemSelection& deselected )
134 if( doingUpdate )
135 return;
136 doingUpdate = true;
137 selectionModel->select( proxyModel->mapSelectionToSource( selected ), QItemSelectionModel::Select );
138 selectionModel->select( proxyModel->mapSelectionToSource( deselected ), QItemSelectionModel::Deselect );
139 doingUpdate = false;
143 #include "proxyselectionmodel.moc"