QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
24
25#include "moc_qgsfeatureselectionmodel.cpp"
26
27using namespace Qt::StringLiterals;
28
29QgsFeatureSelectionModel::QgsFeatureSelectionModel( QAbstractItemModel *model, QgsFeatureModel *featureModel, QgsIFeatureSelectionManager *featureSelectionManager, QObject *parent )
30 : QItemSelectionModel( model, parent )
31 , mFeatureModel( featureModel )
32{
33 setFeatureSelectionManager( featureSelectionManager );
34}
35
37{
38 mSyncEnabled = enable;
39
40 if ( mSyncEnabled )
41 {
42 if ( mClearAndSelectBuffer )
43 {
44 mFeatureSelectionManager->setSelectedFeatures( mSelectedBuffer );
45 }
46 else
47 {
48 mFeatureSelectionManager->select( mSelectedBuffer );
49 mFeatureSelectionManager->deselect( mDeselectedBuffer );
50 }
51
52 mSelectedBuffer.clear();
53 mDeselectedBuffer.clear();
54 mClearAndSelectBuffer = false;
55 }
56}
57
59{
60 if ( mSelectedBuffer.contains( fid ) )
61 return true;
62
63 if ( mDeselectedBuffer.contains( fid ) )
64 return false;
65
66 if ( !mClearAndSelectBuffer && mFeatureSelectionManager->selectedFeatureIds().contains( fid ) )
67 return true;
68
69 return false;
70}
71
72bool QgsFeatureSelectionModel::isSelected( const QModelIndex &index )
73{
74 return isSelected( index.model()->data( index, static_cast<int>( QgsAttributeTableModel::CustomRole::FeatureId ) ).toLongLong() );
75}
76
77void QgsFeatureSelectionModel::selectFeatures( const QItemSelection &selection, QItemSelectionModel::SelectionFlags command )
78{
79 QgsFeatureIds ids;
80
81 QgsDebugMsgLevel( u"Index count: %1"_s.arg( selection.indexes().size() ), 2 );
82
83 const auto constIndexes = selection.indexes();
84 for ( const QModelIndex &index : constIndexes )
85 {
86 const QgsFeatureId id = index.model()->data( index, static_cast<int>( QgsAttributeTableModel::CustomRole::FeatureId ) ).toLongLong();
87
88 ids << id;
89 }
90
91 disconnect( mFeatureSelectionManager, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsFeatureSelectionModel::layerSelectionChanged );
92
93 if ( command.testFlag( QItemSelectionModel::ClearAndSelect ) )
94 {
95 if ( !mSyncEnabled )
96 {
97 mClearAndSelectBuffer = true;
98 const auto constIds = ids;
99 for ( const QgsFeatureId id : constIds )
100 {
101 if ( !mDeselectedBuffer.remove( id ) )
102 {
103 mSelectedBuffer.insert( id );
104 }
105 }
106 }
107 else
108 {
109 mFeatureSelectionManager->setSelectedFeatures( ids );
110 }
111 }
112 else if ( command.testFlag( QItemSelectionModel::Select ) )
113 {
114 if ( !mSyncEnabled )
115 {
116 const auto constIds = ids;
117 for ( const QgsFeatureId id : constIds )
118 {
119 if ( !mDeselectedBuffer.remove( id ) )
120 {
121 mSelectedBuffer.insert( id );
122 }
123 }
124 }
125 else
126 {
127 mFeatureSelectionManager->select( ids );
128 }
129 }
130 else if ( command.testFlag( QItemSelectionModel::Deselect ) )
131 {
132 if ( !mSyncEnabled )
133 {
134 const auto constIds = ids;
135 for ( const QgsFeatureId id : constIds )
136 {
137 if ( !mSelectedBuffer.remove( id ) )
138 {
139 mDeselectedBuffer.insert( id );
140 }
141 }
142 }
143 else
144 {
145 mFeatureSelectionManager->deselect( ids );
146 }
147 }
148
149 connect( mFeatureSelectionManager, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsFeatureSelectionModel::layerSelectionChanged );
150
151 QModelIndexList updatedIndexes;
152 const auto indexes = selection.indexes();
153 for ( const QModelIndex &idx : indexes )
154 {
155 updatedIndexes.append( expandIndexToRow( idx ) );
156 }
157
158 emit requestRepaint( updatedIndexes );
159}
160
162{
163 if ( mFeatureSelectionManager )
164 disconnect( mFeatureSelectionManager, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsFeatureSelectionModel::layerSelectionChanged );
165
166 mFeatureSelectionManager = featureSelectionManager;
167
168 connect( mFeatureSelectionManager, &QgsIFeatureSelectionManager::selectionChanged, this, &QgsFeatureSelectionModel::layerSelectionChanged );
169}
170
171void QgsFeatureSelectionModel::layerSelectionChanged( const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect )
172{
173 if ( clearAndSelect )
174 {
175 emit requestRepaint();
176 }
177 else
178 {
179 QModelIndexList updatedIndexes;
180 const auto constSelected = selected;
181 for ( const QgsFeatureId fid : constSelected )
182 {
183 updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
184 }
185
186 const auto constDeselected = deselected;
187 for ( const QgsFeatureId fid : constDeselected )
188 {
189 updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
190 }
191
192 emit requestRepaint( updatedIndexes );
193 }
194}
195
196QModelIndexList QgsFeatureSelectionModel::expandIndexToRow( const QModelIndex &index ) const
197{
198 QModelIndexList indexes;
199 const QAbstractItemModel *model = index.model();
200 const int row = index.row();
201
202 if ( !model )
203 return indexes;
204
205 const int columns = model->columnCount();
206 indexes.reserve( columns );
207 for ( int column = 0; column < columns; ++column )
208 {
209 indexes.append( model->index( row, column ) );
210 }
211
212 return indexes;
213}
@ 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:63