QGIS API Documentation  2.0.1-Dufour
 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"
4 #include "qgsvectorlayer.h"
5 #include <qdebug.h>
6 
7 QgsFeatureSelectionModel::QgsFeatureSelectionModel( QAbstractItemModel* model, QgsFeatureModel* featureModel, QgsVectorLayer* layer, QObject* parent )
8  : QItemSelectionModel( model, parent )
9  , mFeatureModel( featureModel )
10  , mLayer( layer )
11  , mSyncEnabled( true )
12  , mClearAndSelectBuffer( false )
13 {
14  connect( mLayer, SIGNAL( selectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ), this, SLOT( layerSelectionChanged( QgsFeatureIds, QgsFeatureIds, bool ) ) );
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 
47  if ( !mClearAndSelectBuffer && mLayer->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( mLayer, 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  {
103  mLayer->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  mLayer->deselect( ids );
121  }
122  }
123 
124  connect( mLayer, 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 
135 void QgsFeatureSelectionModel::layerSelectionChanged( QgsFeatureIds selected, QgsFeatureIds deselected, bool clearAndSelect )
136 {
137  if ( clearAndSelect )
138  {
139  emit requestRepaint();
140  }
141  else
142  {
143  QModelIndexList updatedIndexes;
144  foreach ( QgsFeatureId fid, selected )
145  {
146  updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
147  }
148 
149  foreach ( QgsFeatureId fid, deselected )
150  {
151  updatedIndexes.append( expandIndexToRow( mFeatureModel->fidToIndex( fid ) ) );
152  }
153 
154  emit requestRepaint( updatedIndexes );
155  }
156 }
157 
158 QModelIndexList QgsFeatureSelectionModel::expandIndexToRow( const QModelIndex& index ) const
159 {
160  QModelIndexList indexes;
161  const QAbstractItemModel* model = index.model();
162  int row = index.row();
163 
164  if ( !model )
165  return indexes;
166 
167  for ( int column = 0; column < model->columnCount(); ++column )
168  {
169  indexes.append( model->index( row, column ) );
170  }
171 
172  return indexes;
173 }