QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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  {
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 
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  {
86  mFeatureSelectionManager->setSelectedFeatures( ids );
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  {
103  mFeatureSelectionManager->select( ids );
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  {
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  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 }