QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsfeatureselectionmodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfeatureselectionmodel.cpp
3 ---------------------
4 begin : April 2013
5 copyright : (C) 2013 by Matthias Kuhn
6 email : matthias at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
16
18#include "qgsfeaturemodel.h"
20#include "qgslogger.h"
21#include "qgsvectorlayer.h"
22
23#include "moc_qgsfeatureselectionmodel.cpp"
24
25QgsFeatureSelectionModel::QgsFeatureSelectionModel( QAbstractItemModel *model, QgsFeatureModel *featureModel, QgsIFeatureSelectionManager *featureSelectionManager, QObject *parent )
26 : QItemSelectionModel( model, parent )
27 , mFeatureModel( featureModel )
28 , mSyncEnabled( true )
29 , mClearAndSelectBuffer( false )
30{
31 setFeatureSelectionManager( featureSelectionManager );
32}
33
35{
36 mSyncEnabled = enable;
37
38 if ( mSyncEnabled )
39 {
40 if ( mClearAndSelectBuffer )
41 {
42 mFeatureSelectionManager->setSelectedFeatures( mSelectedBuffer );
43 }
44 else
45 {
46 mFeatureSelectionManager->select( mSelectedBuffer );
47 mFeatureSelectionManager->deselect( mDeselectedBuffer );
48 }
49
50 mSelectedBuffer.clear();
51 mDeselectedBuffer.clear();
52 mClearAndSelectBuffer = false;
53 }
54}
55
57{
58 if ( mSelectedBuffer.contains( fid ) )
59 return true;
60
61 if ( mDeselectedBuffer.contains( fid ) )
62 return false;
63
64 if ( !mClearAndSelectBuffer && mFeatureSelectionManager->selectedFeatureIds().contains( fid ) )
65 return true;
66
67 return false;
68}
69
70bool QgsFeatureSelectionModel::isSelected( const QModelIndex &index )
71{
72 return isSelected( index.model()->data( index, static_cast<int>( QgsAttributeTableModel::CustomRole::FeatureId ) ).toLongLong() );
73}
74
75void QgsFeatureSelectionModel::selectFeatures( const QItemSelection &selection, QItemSelectionModel::SelectionFlags command )
76{
77 QgsFeatureIds ids;
78
79 QgsDebugMsgLevel( QStringLiteral( "Index count: %1" ).arg( selection.indexes().size() ), 2 );
80
81 const auto constIndexes = selection.indexes();
82 for ( const QModelIndex &index : constIndexes )
83 {
84 const QgsFeatureId id = index.model()->data( index, static_cast<int>( QgsAttributeTableModel::CustomRole::FeatureId ) ).toLongLong();
85
86 ids << id;
87 }
88
89 disconnect( mFeatureSelectionManager, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsFeatureSelectionModel::layerSelectionChanged );
90
91 if ( command.testFlag( QItemSelectionModel::ClearAndSelect ) )
92 {
93 if ( !mSyncEnabled )
94 {
95 mClearAndSelectBuffer = true;
96 const auto constIds = ids;
97 for ( const QgsFeatureId id : constIds )
98 {
99 if ( !mDeselectedBuffer.remove( id ) )
100 {
101 mSelectedBuffer.insert( id );
102 }
103 }
104 }
105 else
106 {
107 mFeatureSelectionManager->setSelectedFeatures( ids );
108 }
109 }
110 else if ( command.testFlag( QItemSelectionModel::Select ) )
111 {
112 if ( !mSyncEnabled )
113 {
114 const auto constIds = ids;
115 for ( const QgsFeatureId id : constIds )
116 {
117 if ( !mDeselectedBuffer.remove( id ) )
118 {
119 mSelectedBuffer.insert( id );
120 }
121 }
122 }
123 else
124 {
125 mFeatureSelectionManager->select( ids );
126 }
127 }
128 else if ( command.testFlag( QItemSelectionModel::Deselect ) )
129 {
130 if ( !mSyncEnabled )
131 {
132 const auto constIds = ids;
133 for ( const QgsFeatureId id : constIds )
134 {
135 if ( !mSelectedBuffer.remove( id ) )
136 {
137 mDeselectedBuffer.insert( id );
138 }
139 }
140 }
141 else
142 {
143 mFeatureSelectionManager->deselect( ids );
144 }
145 }
146
147 connect( mFeatureSelectionManager, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsFeatureSelectionModel::layerSelectionChanged );
148
149 QModelIndexList updatedIndexes;
150 const auto indexes = selection.indexes();
151 for ( const QModelIndex &idx : indexes )
152 {
153 updatedIndexes.append( expandIndexToRow( idx ) );
154 }
155
156 emit requestRepaint( updatedIndexes );
157}
158
160{
161 if ( mFeatureSelectionManager )
162 disconnect( mFeatureSelectionManager, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsFeatureSelectionModel::layerSelectionChanged );
163
164 mFeatureSelectionManager = featureSelectionManager;
165
166 connect( mFeatureSelectionManager, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsFeatureSelectionModel::layerSelectionChanged );
167}
168
169void QgsFeatureSelectionModel::layerSelectionChanged( const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect )
170{
171 if ( clearAndSelect )
172 {
173 emit requestRepaint();
174 }
175 else
176 {
177 QModelIndexList updatedIndexes;
178 const auto constSelected = selected;
179 for ( const QgsFeatureId fid : constSelected )
180 {
181 updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
182 }
183
184 const auto constDeselected = deselected;
185 for ( const QgsFeatureId fid : constDeselected )
186 {
187 updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
188 }
189
190 emit requestRepaint( updatedIndexes );
191 }
192}
193
194QModelIndexList QgsFeatureSelectionModel::expandIndexToRow( const QModelIndex &index ) const
195{
196 QModelIndexList indexes;
197 const QAbstractItemModel *model = index.model();
198 const int row = index.row();
199
200 if ( !model )
201 return indexes;
202
203 const int columns = model->columnCount();
204 indexes.reserve( columns );
205 for ( int column = 0; column < columns; ++column )
206 {
207 indexes.append( model->index( row, column ) );
208 }
209
210 return indexes;
211}
@ FeatureId
Get the feature id of the feature in this row.
Base class for feature models.
void enableSync(bool enable)
Enables or disables synchronisation to the QgsVectorLayer When synchronisation is disabled,...
virtual void selectFeatures(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
Select features on this table.
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
QgsFeatureSelectionModel(QAbstractItemModel *model, QgsFeatureModel *featureModel, QgsIFeatureSelectionManager *featureSelectionHandler, QObject *parent)
virtual void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
void requestRepaint()
Request a repaint of the visible items of connected views.
Is an interface class to abstract feature selection handling.
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
Emitted when selection was changed.
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61