QGIS API Documentation  2.17.0-Master (973e4b0)
qgsfeaturelistmodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturelistmodel.cpp
3  ---------------------
4  begin : February 2013
5  copyright : (C) 2013 by Matthias Kuhn
6  email : matthias at opengis dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgsexception.h"
16 #include "qgsvectordataprovider.h"
17 #include "qgsfeaturelistmodel.h"
18 #include "qgsattributetablemodel.h"
21 
22 #include <QItemSelection>
23 #include <QSettings>
24 
26  : QAbstractProxyModel( parent )
27  , mInjectNull( false )
28 {
29  setSourceModel( sourceModel );
30  mExpression = new QgsExpression( "" );
31 }
32 
34 {
35  delete mExpression;
36 }
37 
39 {
41  mFilterModel = sourceModel;
42  if ( mFilterModel )
43  {
44  // rewire (filter-)change events in the source model so this proxy reflects the changes
45  connect( mFilterModel, SIGNAL( rowsAboutToBeRemoved( const QModelIndex&, int, int ) ), SLOT( onBeginRemoveRows( const QModelIndex&, int, int ) ) );
46  connect( mFilterModel, SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ), SLOT( onEndRemoveRows( const QModelIndex&, int, int ) ) );
47  connect( mFilterModel, SIGNAL( rowsAboutToBeInserted( const QModelIndex&, int, int ) ), SLOT( onBeginInsertRows( const QModelIndex&, int, int ) ) );
48  connect( mFilterModel, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ), SLOT( onEndInsertRows( const QModelIndex&, int, int ) ) );
49  // propagate sort order changes from source model to views connected to this model
50  connect( mFilterModel, SIGNAL( layoutAboutToBeChanged() ), this, SIGNAL( layoutAboutToBeChanged() ) );
51  connect( mFilterModel, SIGNAL( layoutChanged() ), this, SIGNAL( layoutChanged() ) );
52  }
53 }
54 
56 {
57  return mFilterModel->layerCache();
58 }
59 
61 {
62  return mFilterModel->masterModel()->rowToId( mapToMaster( index ).row() );
63 }
64 
66 {
67  return mFilterModel->mapFromMaster( mFilterModel->masterModel()->idToIndex( fid ) );
68 }
69 
71 {
72  if ( mInjectNull && index.row() == 0 )
73  {
74  if ( role == Qt::DisplayRole )
75  {
76  return QSettings().value( "qgis/nullValue", "NULL" ).toString();
77  }
78  else if ( role == QgsAttributeTableModel::FeatureIdRole )
79  {
80  return QVariant( QVariant::Int );
81  }
82  else
83  {
84  return QVariant( QVariant::Invalid );
85  }
86  }
87 
88  if ( role == Qt::DisplayRole || role == Qt::EditRole )
89  {
90  QgsFeature feat;
91 
92  mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
93 
94  QgsExpressionContext context;
97  << QgsExpressionContextUtils::layerScope( mFilterModel->layer() );
98  context.setFeature( feat );
99  return mExpression->evaluate( &context );
100  }
101 
102  if ( role == FeatureInfoRole )
103  {
104  FeatureInfo featInfo;
105 
106  QgsFeature feat;
107 
108  mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
109 
110  QgsVectorLayerEditBuffer* editBuffer = mFilterModel->layer()->editBuffer();
111 
112  if ( editBuffer )
113  {
114  const QList<QgsFeatureId> addedFeatures = editBuffer->addedFeatures().keys();
115  const QList<QgsFeatureId> changedFeatures = editBuffer->changedAttributeValues().keys();
116 
117  if ( addedFeatures.contains( feat.id() ) )
118  {
119  featInfo.isNew = true;
120  }
121  if ( changedFeatures.contains( feat.id() ) )
122  {
123  featInfo.isEdited = true;
124  }
125  }
126 
127  return QVariant::fromValue( featInfo );
128  }
129  else if ( role == FeatureRole )
130  {
131  QgsFeature feat;
132 
133  mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
134 
135  return QVariant::fromValue( feat );
136  }
137  else if ( role == Qt::TextAlignmentRole )
138  {
139  return Qt::AlignLeft;
140  }
141 
142  return sourceModel()->data( mapToSource( index ), role );
143 }
144 
146 {
147  if ( mInjectNull && index.row() == 0 )
148  {
149  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
150  }
151  else
152  {
153  return sourceModel()->flags( mapToSource( index ) ) & ~Qt::ItemIsEditable;
154  }
155 }
156 
158 {
159  if ( mInjectNull != injectNull )
160  {
161  emit beginResetModel();
162  mInjectNull = injectNull;
163  emit endResetModel();
164  }
165 }
166 
168 {
169  return mInjectNull;
170 }
171 
173 {
174  return mFilterModel->masterModel();
175 }
176 
178 {
179  QgsExpression* exp = new QgsExpression( expression );
180 
181  QgsExpressionContext context;
184  << QgsExpressionContextUtils::layerScope( mFilterModel->layer() );
185 
186  exp->prepare( &context );
187 
188  if ( exp->hasParserError() )
189  {
190  mParserErrorString = exp->parserErrorString();
191  delete exp;
192  return false;
193  }
194 
195  delete mExpression;
196  mExpression = exp;
197 
198  emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
199  return true;
200 }
201 
203 {
204  return mParserErrorString;
205 }
206 
208 {
209  return mExpression->expression();
210 }
211 
213 {
214  return mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
215 }
216 
217 void QgsFeatureListModel::onBeginRemoveRows( const QModelIndex& parent, int first, int last )
218 {
219  beginRemoveRows( parent, first, last );
220 }
221 
222 void QgsFeatureListModel::onEndRemoveRows( const QModelIndex& parent, int first, int last )
223 {
224  Q_UNUSED( parent )
225  Q_UNUSED( first )
226  Q_UNUSED( last )
227  endRemoveRows();
228 }
229 
230 void QgsFeatureListModel::onBeginInsertRows( const QModelIndex& parent, int first, int last )
231 {
232  beginInsertRows( parent, first, last );
233 }
234 
235 void QgsFeatureListModel::onEndInsertRows( const QModelIndex& parent, int first, int last )
236 {
237  Q_UNUSED( parent )
238  Q_UNUSED( first )
239  Q_UNUSED( last )
240  endInsertRows();
241 }
242 
244 {
245  if ( !proxyIndex.isValid() )
246  return QModelIndex();
247 
248  int offset = mInjectNull ? 1 : 0;
249 
250  return mFilterModel->mapToMaster( mFilterModel->index( proxyIndex.row() - offset, proxyIndex.column() ) );
251 }
252 
254 {
255  if ( !sourceIndex.isValid() )
256  return QModelIndex();
257 
258  int offset = mInjectNull ? 1 : 0;
259 
260  return createIndex( mFilterModel->mapFromMaster( sourceIndex ).row() + offset, 0 );
261 }
262 
264 {
265  return mapSelectionFromSource( mFilterModel->mapSelectionFromSource( selection ) );
266 }
267 
269 {
270  return mFilterModel->mapSelectionToSource( mapSelectionToSource( selection ) );
271 }
272 
273 // Override some methods from QAbstractProxyModel, not that interesting
274 
276 {
277  if ( !proxyIndex.isValid() )
278  return QModelIndex();
279 
280  int offset = mInjectNull ? 1 : 0;
281 
282  return sourceModel()->index( proxyIndex.row() - offset, proxyIndex.column() );
283 }
284 
286 {
287  if ( !sourceIndex.isValid() )
288  return QModelIndex();
289 
290  return createIndex( sourceIndex.row(), 0 );
291 }
292 
293 QModelIndex QgsFeatureListModel::index( int row, int column, const QModelIndex& parent ) const
294 {
295  Q_UNUSED( parent )
296 
297  return createIndex( row, column );
298 }
299 
301 {
302  Q_UNUSED( child )
303  return QModelIndex();
304 }
305 
307 {
308  Q_UNUSED( parent )
309  return 1;
310 }
311 
313 {
314  Q_UNUSED( parent )
315 
316  int offset = mInjectNull ? 1 : 0;
317 
318  return sourceModel()->rowCount() + offset;
319 }
320 
322 {
323  return mapFromMaster( masterModel()->idToIndex( fid ) );
324 }
325 
327 {
328  return QModelIndexList() << fidToIndex( fid );
329 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:65
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
bool injectNull()
Returns the current state of null value injection.
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
Class for parsing and evaluation of expressions (formerly called "search strings").
QModelIndex fidToIndex(QgsFeatureId fid) override
virtual int rowCount(const QModelIndex &parent) const =0
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
bool setDisplayExpression(const QString &expression)
Q_DECL_DEPRECATED bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const override
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const override
const QgsChangedAttributesMap & changedAttributeValues()
Changed attributes values which are not commited.
virtual QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection) const
void onEndInsertRows(const QModelIndex &parent, int first, int last)
QModelIndex idToIndex(QgsFeatureId id) const
bool featureByIndex(const QModelIndex &index, QgsFeature &feat)
virtual QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection) const
virtual QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
virtual QItemSelection mapSelectionToMaster(const QItemSelection &selection) const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
QString parserErrorString()
Returns a detailed message about errors while parsing a QgsExpression.
virtual void setSourceModel(QgsAttributeTableFilterModel *sourceModel)
void onBeginInsertRows(const QModelIndex &parent, int first, int last)
Get the feature id of the feature in this row.
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
QList< Key > keys() const
void layoutAboutToBeChanged()
bool isValid() const
QgsVectorLayerEditBuffer * editBuffer()
Buffer with uncommitted editing operations. Only valid after editing has been turned on...
void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context...
QgsVectorLayerCache * layerCache() const
Returns the layerCache this filter acts on.
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
virtual QVariant data(const QModelIndex &index, int role) const override
virtual QItemSelection mapSelectionToSource(const QItemSelection &proxySelection) const
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void beginRemoveRows(const QModelIndex &parent, int first, int last)
int row() const
QModelIndexList fidToIndexList(QgsFeatureId fid)
virtual void setSourceModel(QAbstractItemModel *sourceModel)
virtual QVariant data(const QModelIndex &index, int role) const =0
virtual Qt::ItemFlags flags(const QModelIndex &index) const override
void rowsRemoved(const QModelIndex &parent, int start, int end)
QModelIndex createIndex(int row, int column, void *ptr) const
QgsAttributeTableModel * masterModel()
virtual QItemSelection mapSelectionToSource(const QItemSelection &proxySelection) const
void onBeginRemoveRows(const QModelIndex &parent, int first, int last)
This class caches features of a given QgsVectorLayer.
bool contains(const T &value) const
QVariant fromValue(const T &value)
void beginInsertRows(const QModelIndex &parent, int first, int last)
const QgsFeatureMap & addedFeatures()
New features which are not commited.
QVariant value(const QString &key, const QVariant &defaultValue) const
QAbstractItemModel * sourceModel() const
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
QgsFeatureId idxToFid(const QModelIndex &index) const
int column() const
qint64 QgsFeatureId
Definition: qgsfeature.h:31
void onEndRemoveRows(const QModelIndex &parent, int first, int last)
QString expression() const
Return the original, unmodified expression string.
QgsVectorLayerCache * layerCache()
virtual Qt::ItemFlags flags(const QModelIndex &index) const
void setInjectNull(bool injectNull)
If true is specified, a NULL value will be injected.
QgsFeatureId rowToId(int row) const
Maps row to feature id.
static QgsExpressionContextScope * projectScope()
Creates a new scope which contains variables and functions relating to the current QGIS project...
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
void rowsInserted(const QModelIndex &parent, int start, int end)
QModelIndex fidToIdx(const QgsFeatureId fid) const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
QString parserErrorString() const
Returns parser error.
virtual QItemSelection mapSelectionFromMaster(const QItemSelection &selection) const
QString toString() const
QgsFeatureListModel(QgsAttributeTableFilterModel *sourceModel, QObject *parent=nullptr)
QString displayExpression() const
typedef ItemFlags