QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsfeatureselectionmodel.cpp
Go to the documentation of this file.
2 #include "qgsfeaturemodel.h"
5 #include "qgsvectorlayer.h"
6 #include <qdebug.h>
7 
8 QgsFeatureSelectionModel::QgsFeatureSelectionModel( QAbstractItemModel* model, QgsFeatureModel* featureModel, QgsIFeatureSelectionManager* featureSelectionManager, QObject* parent )
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  {
24  {
26  }
27  else
28  {
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 
48  return true;
49 
50  return false;
51 }
52 
53 bool QgsFeatureSelectionModel::isSelected( const QModelIndex &index )
54 {
55  return isSelected( index.model()->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong() );
56 }
57 
58 void QgsFeatureSelectionModel::selectFeatures( const QItemSelection &selection, QItemSelectionModel::SelectionFlags command )
59 {
60  QgsFeatureIds ids;
61 
62  foreach ( const QModelIndex index, selection.indexes() )
63  {
64  QgsFeatureId id = index.model()->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
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  foreach ( QgsFeatureId id, ids )
77  {
78  if ( !mDeselectedBuffer.remove( id ) )
79  {
80  mSelectedBuffer.insert( id );
81  }
82  }
83  }
84  else
85  {
87  }
88  }
89  else if ( command.testFlag( QItemSelectionModel::Select ) )
90  {
91  if ( !mSyncEnabled )
92  {
93  foreach ( QgsFeatureId id, ids )
94  {
95  if ( !mDeselectedBuffer.remove( id ) )
96  {
97  mSelectedBuffer.insert( id );
98  }
99  }
100  }
101  else
102  {
104  }
105  }
106  else if ( command.testFlag( QItemSelectionModel::Deselect ) )
107  {
108  if ( !mSyncEnabled )
109  {
110  foreach ( QgsFeatureId id, ids )
111  {
112  if ( !mSelectedBuffer.remove( id ) )
113  {
114  mDeselectedBuffer.insert( id );
115  }
116  }
117  }
118  else
119  {
121  }
122  }
123 
124  connect( mFeatureSelectionManager, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
125 
126  QModelIndexList updatedIndexes;
127  foreach ( 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( QgsFeatureIds selected, QgsFeatureIds deselected, bool clearAndSelect )
143 {
144  if ( clearAndSelect )
145  {
146  emit requestRepaint();
147  }
148  else
149  {
150  QModelIndexList updatedIndexes;
151  foreach ( QgsFeatureId fid, selected )
152  {
153  updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
154  }
155 
156  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  for ( int column = 0; column < model->columnCount(); ++column )
175  {
176  indexes.append( model->index( row, column ) );
177  }
178 
179  return indexes;
180 }
QgsFeatureIds mDeselectedBuffer
If sync is disabled Holds a list of newly deselected features which will be synced when re-enabled...
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.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:325
virtual void setSelectedFeatures(const QgsFeatureIds &ids)=0
Change selection to the new set of features.
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.
virtual void selectFeatures(const QItemSelection &selection, SelectionFlags command)
Select features on this table.
virtual void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
QgsIFeatureSelectionManager * mFeatureSelectionManager
virtual void layerSelectionChanged(QgsFeatureIds selected, QgsFeatureIds deselected, bool clearAndSelect)
qint64 QgsFeatureId
Definition: qgsfeature.h:30
QgsFeatureSelectionModel(QAbstractItemModel *model, QgsFeatureModel *featureModel, QgsIFeatureSelectionManager *featureSelectionHandler, QObject *parent)
Is an interface class to abstract feature selection handling.
QgsFeatureIds mSelectedBuffer
If sync is disabled Holds a list of newly selected features which will be synced when re-enabled...
bool mClearAndSelectBuffer
If sync is disabled Is set to true, if a clear and select operation should be performed before syncin...
virtual void select(const QgsFeatureIds &ids)=0
Select features.
QModelIndexList expandIndexToRow(const QModelIndex &index) const