QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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  ***************************************************************************/
15 #include "qgsattributetablemodel.h"
16 #include "qgsfeaturemodel.h"
19 #include "qgsvectorlayer.h"
20 #include <qdebug.h>
21 
23  : QItemSelectionModel( model, parent )
24  , mFeatureModel( featureModel )
25  , mSyncEnabled( true )
26  , mClearAndSelectBuffer( false )
27 {
28  setFeatureSelectionManager( featureSelectionManager );
29 }
30 
32 {
33  mSyncEnabled = enable;
34 
35  if ( mSyncEnabled )
36  {
37  if ( mClearAndSelectBuffer )
38  {
39  mFeatureSelectionManager->setSelectedFeatures( mSelectedBuffer );
40  }
41  else
42  {
43  mFeatureSelectionManager->select( mSelectedBuffer );
44  mFeatureSelectionManager->deselect( mDeselectedBuffer );
45  }
46 
47  mSelectedBuffer.clear();
48  mDeselectedBuffer.clear();
49  mClearAndSelectBuffer = false;
50  }
51 }
52 
54 {
55  if ( mSelectedBuffer.contains( fid ) )
56  return true;
57 
58  if ( mDeselectedBuffer.contains( fid ) )
59  return false;
60 
61  if ( !mClearAndSelectBuffer && mFeatureSelectionManager->selectedFeaturesIds().contains( fid ) )
62  return true;
63 
64  return false;
65 }
66 
68 {
70 }
71 
73 {
74  QgsFeatureIds ids;
75 
76  QgsDebugMsg( QString( "Index count: %1" ).arg( selection.indexes().size() ) );
77 
78  Q_FOREACH ( const QModelIndex& index, selection.indexes() )
79  {
81 
82  ids << id;
83  }
84 
85  disconnect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
86 
87  if ( command.testFlag( QItemSelectionModel::ClearAndSelect ) )
88  {
89  if ( !mSyncEnabled )
90  {
91  mClearAndSelectBuffer = true;
92  Q_FOREACH ( QgsFeatureId id, ids )
93  {
94  if ( !mDeselectedBuffer.remove( id ) )
95  {
96  mSelectedBuffer.insert( id );
97  }
98  }
99  }
100  else
101  {
102  mFeatureSelectionManager->setSelectedFeatures( ids );
103  }
104  }
105  else if ( command.testFlag( QItemSelectionModel::Select ) )
106  {
107  if ( !mSyncEnabled )
108  {
109  Q_FOREACH ( QgsFeatureId id, ids )
110  {
111  if ( !mDeselectedBuffer.remove( id ) )
112  {
113  mSelectedBuffer.insert( id );
114  }
115  }
116  }
117  else
118  {
119  mFeatureSelectionManager->select( ids );
120  }
121  }
122  else if ( command.testFlag( QItemSelectionModel::Deselect ) )
123  {
124  if ( !mSyncEnabled )
125  {
126  Q_FOREACH ( QgsFeatureId id, ids )
127  {
128  if ( !mSelectedBuffer.remove( id ) )
129  {
130  mDeselectedBuffer.insert( id );
131  }
132  }
133  }
134  else
135  {
136  mFeatureSelectionManager->deselect( ids );
137  }
138  }
139 
140  connect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
141 
142  QModelIndexList updatedIndexes;
143  Q_FOREACH ( const QModelIndex& idx, selection.indexes() )
144  {
145  updatedIndexes.append( expandIndexToRow( idx ) );
146  }
147 
148  emit requestRepaint( updatedIndexes );
149 }
150 
152 {
153  mFeatureSelectionManager = featureSelectionManager;
154 
155  connect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
156 }
157 
158 void QgsFeatureSelectionModel::layerSelectionChanged( const QgsFeatureIds& selected, const QgsFeatureIds& deselected, bool clearAndSelect )
159 {
160  if ( clearAndSelect )
161  {
162  emit requestRepaint();
163  }
164  else
165  {
166  QModelIndexList updatedIndexes;
167  Q_FOREACH ( QgsFeatureId fid, selected )
168  {
169  updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
170  }
171 
172  Q_FOREACH ( QgsFeatureId fid, deselected )
173  {
174  updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
175  }
176 
177  emit requestRepaint( updatedIndexes );
178  }
179 }
180 
181 QModelIndexList QgsFeatureSelectionModel::expandIndexToRow( const QModelIndex& index ) const
182 {
183  QModelIndexList indexes;
184  const QAbstractItemModel* model = index.model();
185  int row = index.row();
186 
187  if ( !model )
188  return indexes;
189 
190  int columns = model->columnCount();
191  indexes.reserve( columns );
192  for ( int column = 0; column < columns; ++column )
193  {
194  indexes.append( model->index( row, column ) );
195  }
196 
197  return indexes;
198 }
QModelIndexList indexes() const
qlonglong toLongLong(bool *ok) const
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
static unsigned index
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
virtual const QgsFeatureIds & selectedFeaturesIds() const =0
Return reference to identifiers of selected features.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const=0
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
virtual void setSelectedFeatures(const QgsFeatureIds &ids)=0
Change selection to the new set of features.
const_iterator insert(const T &value)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void enableSync(bool enable)
Enables or disables synchronisation to the QgsVectorLayer When synchronisation is disabled...
virtual QModelIndex fidToIndex(QgsFeatureId fid)=0
Get the feature id of the feature in this row.
virtual void deselect(const QgsFeatureIds &ids)=0
Deselect features.
void requestRepaint()
Request a repaint of the visible items of connected views.
const QItemSelection selection() const
int row() const
virtual void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
virtual QVariant data(const QModelIndex &index, int role) const=0
bool contains(const T &value) const
virtual void selectFeatures(const QItemSelection &selection, const SelectionFlags &command)
Select features on this table.
const QAbstractItemModel * model() const
bool remove(const T &value)
virtual int columnCount(const QModelIndex &parent) const=0
qint64 QgsFeatureId
Definition: qgsfeature.h:31
void clear()
QgsFeatureSelectionModel(QAbstractItemModel *model, QgsFeatureModel *featureModel, QgsIFeatureSelectionManager *featureSelectionHandler, QObject *parent)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Is an interface class to abstract feature selection handling.
const QAbstractItemModel * model() const
virtual void select(const QgsFeatureIds &ids)=0
Select features.