QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsfeaturelistmodel.cpp
Go to the documentation of this file.
1 #include "qgsexception.h"
3 #include "qgsfeaturelistmodel.h"
7 
8 #include <QItemSelection>
9 
11  : QAbstractProxyModel( parent )
12 {
13  setSourceModel( sourceModel );
14  mExpression = new QgsExpression( "" );
15 }
16 
18 {
19  delete mExpression;
20 }
21 
23 {
25  mFilterModel = sourceModel;
26  if ( mFilterModel )
27  {
28  // rewire (filter-)change events in the source model so this proxy reflects the changes
29  connect( mFilterModel, SIGNAL( rowsAboutToBeRemoved( const QModelIndex&, int, int ) ), SLOT( onBeginRemoveRows( const QModelIndex&, int, int ) ) );
30  connect( mFilterModel, SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ), SLOT( onEndRemoveRows( const QModelIndex&, int, int ) ) );
31  connect( mFilterModel, SIGNAL( rowsAboutToBeInserted( const QModelIndex&, int, int ) ), SLOT( onBeginInsertRows( const QModelIndex&, int, int ) ) );
32  connect( mFilterModel, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ), SLOT( onEndInsertRows( const QModelIndex&, int, int ) ) );
33  // propagate sort order changes from source model to views connected to this model
34  connect( mFilterModel, SIGNAL( layoutAboutToBeChanged() ), this, SIGNAL( layoutAboutToBeChanged() ) );
35  connect( mFilterModel, SIGNAL( layoutChanged() ), this, SIGNAL( layoutChanged() ) );
36  }
37 }
38 
40 {
41  return mFilterModel->layerCache();
42 }
43 
45 {
46  return mFilterModel->masterModel()->rowToId( mapToMaster( index ).row() );
47 }
48 
49 QModelIndex QgsFeatureListModel::fidToIdx( const QgsFeatureId fid ) const
50 {
51  return mFilterModel->mapFromMaster( mFilterModel->masterModel()->idToIndex( fid ) );
52 }
53 
54 QVariant QgsFeatureListModel::data( const QModelIndex &index, int role ) const
55 {
56  if ( role == Qt::DisplayRole || role == Qt::EditRole )
57  {
58  QgsFeature feat;
59 
60  mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
61 
62  const QgsFields fields = mFilterModel->layer()->pendingFields();
63 
64  return mExpression->evaluate( &feat, fields );
65  }
66 
67  if ( role == FeatureInfoRole )
68  {
69  FeatureInfo featInfo;
70 
71  QgsFeature feat;
72 
73  mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
74 
75  QgsVectorLayerEditBuffer* editBuffer = mFilterModel->layer()->editBuffer();
76 
77  if ( editBuffer )
78  {
79  const QList<QgsFeatureId> addedFeatures = editBuffer->addedFeatures().keys();
80  const QList<QgsFeatureId> changedFeatures = editBuffer->changedAttributeValues().keys();
81 
82  if ( addedFeatures.contains( feat.id() ) )
83  {
84  featInfo.isNew = true;
85  }
86  if ( changedFeatures.contains( feat.id() ) )
87  {
88  featInfo.isEdited = true;
89  }
90  }
91 
92  return QVariant::fromValue( featInfo );
93  }
94  else if ( role == FeatureRole )
95  {
96  QgsFeature feat;
97 
98  mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
99 
100  return QVariant::fromValue( feat );
101  }
102 
103  return sourceModel()->data( mapToSource( index ), role );
104 }
105 
106 Qt::ItemFlags QgsFeatureListModel::flags( const QModelIndex &index ) const
107 {
108  return sourceModel()->flags( mapToSource( index ) ) & ~Qt::ItemIsEditable;
109 }
110 
112 {
113  return mFilterModel->masterModel();
114 }
115 
116 bool QgsFeatureListModel::setDisplayExpression( const QString expression )
117 {
118  const QgsFields fields = mFilterModel->layer()->dataProvider()->fields();
119 
120  QgsExpression* exp = new QgsExpression( expression );
121 
122  exp->prepare( fields );
123 
124  if ( exp->hasParserError() )
125  {
126  mParserErrorString = exp->parserErrorString();
127  delete exp;
128  return false;
129  }
130 
131  delete mExpression;
132  mExpression = exp;
133 
134  emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
135  return true;
136 }
137 
139 {
140  return mParserErrorString;
141 }
142 
144 {
145  return mExpression->expression();
146 }
147 
148 bool QgsFeatureListModel::featureByIndex( const QModelIndex &index, QgsFeature &feat )
149 {
150  return mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
151 }
152 
153 void QgsFeatureListModel::onBeginRemoveRows( const QModelIndex& parent, int first, int last )
154 {
155  beginRemoveRows( parent, first, last );
156 }
157 
158 void QgsFeatureListModel::onEndRemoveRows( const QModelIndex& parent, int first, int last )
159 {
160  Q_UNUSED( parent )
161  Q_UNUSED( first )
162  Q_UNUSED( last )
163  endRemoveRows();
164 }
165 
166 void QgsFeatureListModel::onBeginInsertRows( const QModelIndex& parent, int first, int last )
167 {
168  beginInsertRows( parent, first, last );
169 }
170 
171 void QgsFeatureListModel::onEndInsertRows( const QModelIndex& parent, int first, int last )
172 {
173  Q_UNUSED( parent )
174  Q_UNUSED( first )
175  Q_UNUSED( last )
176  endInsertRows();
177 }
178 
179 QModelIndex QgsFeatureListModel::mapToMaster( const QModelIndex &proxyIndex ) const
180 {
181  if ( !proxyIndex.isValid() )
182  return QModelIndex();
183 
184  return mFilterModel->mapToMaster( mFilterModel->index( proxyIndex.row(), proxyIndex.column() ) );
185 }
186 
187 QModelIndex QgsFeatureListModel::mapFromMaster( const QModelIndex &sourceIndex ) const
188 {
189  if ( !sourceIndex.isValid() )
190  return QModelIndex();
191 
192  return createIndex( mFilterModel->mapFromMaster( sourceIndex ).row(), 0 );
193 }
194 
195 QItemSelection QgsFeatureListModel::mapSelectionFromMaster( const QItemSelection& selection ) const
196 {
197  return mapSelectionFromSource( mFilterModel->mapSelectionFromSource( selection ) );
198 }
199 
200 QItemSelection QgsFeatureListModel::mapSelectionToMaster( const QItemSelection& selection ) const
201 {
202  return mFilterModel->mapSelectionToSource( mapSelectionToSource( selection ) );
203 }
204 
205 // Override some methods from QAbstractProxyModel, not that interesting
206 
207 QModelIndex QgsFeatureListModel::mapToSource( const QModelIndex &proxyIndex ) const
208 {
209  if ( !proxyIndex.isValid() )
210  return QModelIndex();
211 
212  return sourceModel()->index( proxyIndex.row(), proxyIndex.column() );
213 }
214 
215 QModelIndex QgsFeatureListModel::mapFromSource( const QModelIndex &sourceIndex ) const
216 {
217  if ( !sourceIndex.isValid() )
218  return QModelIndex();
219 
220  return createIndex( sourceIndex.row(), 0 );
221 }
222 
223 QModelIndex QgsFeatureListModel::index( int row, int column, const QModelIndex& parent ) const
224 {
225  Q_UNUSED( parent )
226  return createIndex( row, column );
227 }
228 
229 QModelIndex QgsFeatureListModel::parent( const QModelIndex& child ) const
230 {
231  Q_UNUSED( child )
232  return QModelIndex();
233 }
234 
235 int QgsFeatureListModel::columnCount( const QModelIndex&parent ) const
236 {
237  Q_UNUSED( parent )
238  return 1;
239 }
240 
241 int QgsFeatureListModel::rowCount( const QModelIndex& parent ) const
242 {
243  Q_UNUSED( parent )
244  return sourceModel()->rowCount();
245 }
246 
248 {
249  return mapFromMaster( masterModel()->idToIndex( fid ) );
250 }
251 
253 {
254  return QModelIndexList() << fidToIndex( fid );
255 }