QGIS API Documentation  2.14.0-Essen
qgsfeatureselectionmodel.cpp
Go to the documentation of this file.
2 #include "qgsfeaturemodel.h"
5 #include "qgsvectorlayer.h"
6 #include <qdebug.h>
7 
9  : QItemSelectionModel( model, parent )
10  , mFeatureModel( featureModel )
11  , mSyncEnabled( true )
12  , mClearAndSelectBuffer( false )
13 {
14  setFeatureSelectionManager( featureSelectionManager );
15 }
16 
18 {
19  mSyncEnabled = enable;
20 
21  if ( mSyncEnabled )
22  {
23  if ( mClearAndSelectBuffer )
24  {
25  mFeatureSelectionManager->setSelectedFeatures( mSelectedBuffer );
26  }
27  else
28  {
29  mFeatureSelectionManager->select( mSelectedBuffer );
30  mFeatureSelectionManager->deselect( mDeselectedBuffer );
31  }
32 
33  mSelectedBuffer.clear();
34  mDeselectedBuffer.clear();
35  mClearAndSelectBuffer = false;
36  }
37 }
38 
40 {
41  if ( mSelectedBuffer.contains( fid ) )
42  return true;
43 
44  if ( mDeselectedBuffer.contains( fid ) )
45  return false;
46 
47  if ( !mClearAndSelectBuffer && mFeatureSelectionManager->selectedFeaturesIds().contains( fid ) )
48  return true;
49 
50  return false;
51 }
52 
54 {
56 }
57 
59 {
60  QgsFeatureIds ids;
61 
62  Q_FOREACH ( const QModelIndex& index, selection.indexes() )
63  {
65 
66  ids << id;
67  }
68 
69  disconnect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
70 
71  if ( command.testFlag( QItemSelectionModel::ClearAndSelect ) )
72  {
73  if ( !mSyncEnabled )
74  {
75  mClearAndSelectBuffer = true;
76  Q_FOREACH ( QgsFeatureId id, ids )
77  {
78  if ( !mDeselectedBuffer.remove( id ) )
79  {
80  mSelectedBuffer.insert( id );
81  }
82  }
83  }
84  else
85  {
86  mFeatureSelectionManager->setSelectedFeatures( ids );
87  }
88  }
89  else if ( command.testFlag( QItemSelectionModel::Select ) )
90  {
91  if ( !mSyncEnabled )
92  {
93  Q_FOREACH ( QgsFeatureId id, ids )
94  {
95  if ( !mDeselectedBuffer.remove( id ) )
96  {
97  mSelectedBuffer.insert( id );
98  }
99  }
100  }
101  else
102  {
103  mFeatureSelectionManager->select( ids );
104  }
105  }
106  else if ( command.testFlag( QItemSelectionModel::Deselect ) )
107  {
108  if ( !mSyncEnabled )
109  {
110  Q_FOREACH ( QgsFeatureId id, ids )
111  {
112  if ( !mSelectedBuffer.remove( id ) )
113  {
114  mDeselectedBuffer.insert( id );
115  }
116  }
117  }
118  else
119  {
120  mFeatureSelectionManager->deselect( ids );
121  }
122  }
123 
124  connect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
125 
126  QModelIndexList updatedIndexes;
127  Q_FOREACH ( const QModelIndex& idx, selection.indexes() )
128  {
129  updatedIndexes.append( expandIndexToRow( idx ) );
130  }
131 
132  emit requestRepaint( updatedIndexes );
133 }
134 
136 {
137  mFeatureSelectionManager = featureSelectionManager;
138 
139  connect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
140 }
141 
142 void QgsFeatureSelectionModel::layerSelectionChanged( const QgsFeatureIds& selected, const QgsFeatureIds& deselected, bool clearAndSelect )
143 {
144  if ( clearAndSelect )
145  {
146  emit requestRepaint();
147  }
148  else
149  {
150  QModelIndexList updatedIndexes;
151  Q_FOREACH ( QgsFeatureId fid, selected )
152  {
153  updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
154  }
155 
156  Q_FOREACH ( QgsFeatureId fid, deselected )
157  {
158  updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
159  }
160 
161  emit requestRepaint( updatedIndexes );
162  }
163 }
164 
165 QModelIndexList QgsFeatureSelectionModel::expandIndexToRow( const QModelIndex& index ) const
166 {
167  QModelIndexList indexes;
168  const QAbstractItemModel* model = index.model();
169  int row = index.row();
170 
171  if ( !model )
172  return indexes;
173 
174  int columns = model->columnCount();
175  indexes.reserve( columns );
176  for ( int column = 0; column < columns; ++column )
177  {
178  indexes.append( model->index( row, column ) );
179  }
180 
181  return indexes;
182 }
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
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
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.