25#include "moc_qgsfeatureselectionmodel.cpp"
27using namespace Qt::StringLiterals;
30 : QItemSelectionModel( model, parent )
31 , mFeatureModel( featureModel )
38 mSyncEnabled = enable;
42 if ( mClearAndSelectBuffer )
44 mFeatureSelectionManager->setSelectedFeatures( mSelectedBuffer );
48 mFeatureSelectionManager->select( mSelectedBuffer );
49 mFeatureSelectionManager->deselect( mDeselectedBuffer );
52 mSelectedBuffer.clear();
53 mDeselectedBuffer.clear();
54 mClearAndSelectBuffer =
false;
60 if ( mSelectedBuffer.contains( fid ) )
63 if ( mDeselectedBuffer.contains( fid ) )
66 if ( !mClearAndSelectBuffer && mFeatureSelectionManager->selectedFeatureIds().contains( fid ) )
81 QgsDebugMsgLevel( u
"Index count: %1"_s.arg( selection.indexes().size() ), 2 );
83 const auto constIndexes = selection.indexes();
84 for (
const QModelIndex &index : constIndexes )
93 if ( command.testFlag( QItemSelectionModel::ClearAndSelect ) )
97 mClearAndSelectBuffer =
true;
98 const auto constIds = ids;
101 if ( !mDeselectedBuffer.remove(
id ) )
103 mSelectedBuffer.insert(
id );
109 mFeatureSelectionManager->setSelectedFeatures( ids );
112 else if ( command.testFlag( QItemSelectionModel::Select ) )
116 const auto constIds = ids;
119 if ( !mDeselectedBuffer.remove(
id ) )
121 mSelectedBuffer.insert(
id );
127 mFeatureSelectionManager->select( ids );
130 else if ( command.testFlag( QItemSelectionModel::Deselect ) )
134 const auto constIds = ids;
137 if ( !mSelectedBuffer.remove(
id ) )
139 mDeselectedBuffer.insert(
id );
145 mFeatureSelectionManager->deselect( ids );
151 QModelIndexList updatedIndexes;
152 const auto indexes = selection.indexes();
153 for (
const QModelIndex &idx : indexes )
155 updatedIndexes.append( expandIndexToRow( idx ) );
163 if ( mFeatureSelectionManager )
166 mFeatureSelectionManager = featureSelectionManager;
171void QgsFeatureSelectionModel::layerSelectionChanged(
const QgsFeatureIds &selected,
const QgsFeatureIds &deselected,
bool clearAndSelect )
173 if ( clearAndSelect )
179 QModelIndexList updatedIndexes;
180 const auto constSelected = selected;
183 updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
186 const auto constDeselected = deselected;
189 updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
196QModelIndexList QgsFeatureSelectionModel::expandIndexToRow(
const QModelIndex &index )
const
198 QModelIndexList indexes;
199 const QAbstractItemModel *model = index.model();
200 const int row = index.row();
205 const int columns = model->columnCount();
206 indexes.reserve( columns );
207 for (
int column = 0; column < columns; ++column )
209 indexes.append( model->index( row, column ) );
@ 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)