23#include "moc_qgsfeatureselectionmodel.cpp"
26 : QItemSelectionModel( model, parent )
27 , mFeatureModel( featureModel )
28 , mSyncEnabled( true )
29 , mClearAndSelectBuffer( false )
36 mSyncEnabled = enable;
40 if ( mClearAndSelectBuffer )
42 mFeatureSelectionManager->setSelectedFeatures( mSelectedBuffer );
46 mFeatureSelectionManager->select( mSelectedBuffer );
47 mFeatureSelectionManager->deselect( mDeselectedBuffer );
50 mSelectedBuffer.clear();
51 mDeselectedBuffer.clear();
52 mClearAndSelectBuffer =
false;
58 if ( mSelectedBuffer.contains( fid ) )
61 if ( mDeselectedBuffer.contains( fid ) )
64 if ( !mClearAndSelectBuffer && mFeatureSelectionManager->selectedFeatureIds().contains( fid ) )
79 QgsDebugMsgLevel( QStringLiteral(
"Index count: %1" ).arg( selection.indexes().size() ), 2 );
81 const auto constIndexes = selection.indexes();
82 for (
const QModelIndex &index : constIndexes )
91 if ( command.testFlag( QItemSelectionModel::ClearAndSelect ) )
95 mClearAndSelectBuffer =
true;
96 const auto constIds = ids;
99 if ( !mDeselectedBuffer.remove(
id ) )
101 mSelectedBuffer.insert(
id );
107 mFeatureSelectionManager->setSelectedFeatures( ids );
110 else if ( command.testFlag( QItemSelectionModel::Select ) )
114 const auto constIds = ids;
117 if ( !mDeselectedBuffer.remove(
id ) )
119 mSelectedBuffer.insert(
id );
125 mFeatureSelectionManager->select( ids );
128 else if ( command.testFlag( QItemSelectionModel::Deselect ) )
132 const auto constIds = ids;
135 if ( !mSelectedBuffer.remove(
id ) )
137 mDeselectedBuffer.insert(
id );
143 mFeatureSelectionManager->deselect( ids );
149 QModelIndexList updatedIndexes;
150 const auto indexes = selection.indexes();
151 for (
const QModelIndex &idx : indexes )
153 updatedIndexes.append( expandIndexToRow( idx ) );
161 if ( mFeatureSelectionManager )
164 mFeatureSelectionManager = featureSelectionManager;
169void QgsFeatureSelectionModel::layerSelectionChanged(
const QgsFeatureIds &selected,
const QgsFeatureIds &deselected,
bool clearAndSelect )
171 if ( clearAndSelect )
177 QModelIndexList updatedIndexes;
178 const auto constSelected = selected;
181 updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
184 const auto constDeselected = deselected;
187 updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
194QModelIndexList QgsFeatureSelectionModel::expandIndexToRow(
const QModelIndex &index )
const
196 QModelIndexList indexes;
197 const QAbstractItemModel *model = index.model();
198 const int row = index.row();
203 const int columns = model->columnCount();
204 indexes.reserve( columns );
205 for (
int column = 0; column < columns; ++column )
207 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)