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() );
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
)
56 emit
currentChanged( proxyModel
->mapFromSource( current
), proxyModel
->mapFromSource( previous
) );
60 void ProxySelectionModel::changeCurrentColumn( const QModelIndex
& current
, const QModelIndex
& previous
)
65 emit
currentColumnChanged( proxyModel
->mapFromSource( current
), proxyModel
->mapFromSource( previous
) );
69 void ProxySelectionModel::changeCurrentRow( const QModelIndex
& current
, const QModelIndex
& previous
)
74 emit
currentRowChanged( proxyModel
->mapFromSource( current
), proxyModel
->mapFromSource( previous
) );
78 void ProxySelectionModel::changeSelection( const QItemSelection
& selected
, const QItemSelection
& deselected
)
83 emit
selectionChanged( proxyModel
->mapSelectionFromSource( selected
), proxyModel
->mapSelectionFromSource( deselected
) );
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
;
101 f
= QItemSelectionModel::NoUpdate
;
105 void ProxySelectionModel::forwardChangeCurrent( const QModelIndex
& current
, const QModelIndex
& previous
)
110 selectionModel
->setCurrentIndex( proxyModel
->mapToSource( current
), selectionFlags() );
114 void ProxySelectionModel::forwardChangeCurrentColumn( const QModelIndex
& current
, const QModelIndex
& previous
)
119 selectionModel
->setCurrentIndex( proxyModel
->mapToSource( current
), selectionFlags() );
123 void ProxySelectionModel::forwardChangeCurrentRow( const QModelIndex
& current
, const QModelIndex
& previous
)
128 selectionModel
->setCurrentIndex( proxyModel
->mapToSource( current
), selectionFlags() );
132 void ProxySelectionModel::forwardChangeSelection( const QItemSelection
& selected
, const QItemSelection
& deselected
)
137 selectionModel
->select( proxyModel
->mapSelectionToSource( selected
), QItemSelectionModel::Select
);
138 selectionModel
->select( proxyModel
->mapSelectionToSource( deselected
), QItemSelectionModel::Deselect
);
143 #include "proxyselectionmodel.moc"