QGIS API Documentation  2.15.0-Master (af20121)
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  QgsDebugMsg( QString( "Index count: %1" ).arg( selection.indexes().size() ) );
63 
64  Q_FOREACH ( const QModelIndex& index, selection.indexes() )
65  {
67 
68  ids << id;
69  }
70 
71  disconnect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
72 
73  if ( command.testFlag( QItemSelectionModel::ClearAndSelect ) )
74  {
75  if ( !mSyncEnabled )
76  {
77  mClearAndSelectBuffer = true;
78  Q_FOREACH ( QgsFeatureId id, ids )
79  {
80  if ( !mDeselectedBuffer.remove( id ) )
81  {
82  mSelectedBuffer.insert( id );
83  }
84  }
85  }
86  else
87  {
88  mFeatureSelectionManager->setSelectedFeatures( ids );
89  }
90  }
91  else if ( command.testFlag( QItemSelectionModel::Select ) )
92  {
93  if ( !mSyncEnabled )
94  {
95  Q_FOREACH ( QgsFeatureId id, ids )
96  {
97  if ( !mDeselectedBuffer.remove( id ) )
98  {
99  mSelectedBuffer.insert( id );
100  }
101  }
102  }
103  else
104  {
105  mFeatureSelectionManager->select( ids );
106  }
107  }
108  else if ( command.testFlag( QItemSelectionModel::Deselect ) )
109  {
110  if ( !mSyncEnabled )
111  {
112  Q_FOREACH ( QgsFeatureId id, ids )
113  {
114  if ( !mSelectedBuffer.remove( id ) )
115  {
116  mDeselectedBuffer.insert( id );
117  }
118  }
119  }
120  else
121  {
122  mFeatureSelectionManager->deselect( ids );
123  }
124  }
125 
126  connect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
127 
128  QModelIndexList updatedIndexes;
129  Q_FOREACH ( const QModelIndex& idx, selection.indexes() )
130  {
131  updatedIndexes.append( expandIndexToRow( idx ) );
132  }
133 
134  emit requestRepaint( updatedIndexes );
135 }
136 
138 {
139  mFeatureSelectionManager = featureSelectionManager;
140 
141  connect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
142 }
143 
144 void QgsFeatureSelectionModel::layerSelectionChanged( const QgsFeatureIds& selected, const QgsFeatureIds& deselected, bool clearAndSelect )
145 {
146  if ( clearAndSelect )
147  {
148  emit requestRepaint();
149  }
150  else
151  {
152  QModelIndexList updatedIndexes;
153  Q_FOREACH ( QgsFeatureId fid, selected )
154  {
155  updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
156  }
157 
158  Q_FOREACH ( QgsFeatureId fid, deselected )
159  {
160  updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
161  }
162 
163  emit requestRepaint( updatedIndexes );
164  }
165 }
166 
167 QModelIndexList QgsFeatureSelectionModel::expandIndexToRow( const QModelIndex& index ) const
168 {
169  QModelIndexList indexes;
170  const QAbstractItemModel* model = index.model();
171  int row = index.row();
172 
173  if ( !model )
174  return indexes;
175 
176  int columns = model->columnCount();
177  indexes.reserve( columns );
178  for ( int column = 0; column < columns; ++column )
179  {
180  indexes.append( model->index( row, column ) );
181  }
182 
183  return indexes;
184 }
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.