QGIS API Documentation  2.15.0-Master (13f053b)
qgsfeaturelistmodel.cpp
Go to the documentation of this file.
1 #include "qgsexception.h"
3 #include "qgsfeaturelistmodel.h"
7 
8 #include <QItemSelection>
9 #include <QSettings>
10 
12  : QAbstractProxyModel( parent )
13  , mInjectNull( false )
14 {
15  setSourceModel( sourceModel );
16  mExpression = new QgsExpression( "" );
17 }
18 
20 {
21  delete mExpression;
22 }
23 
25 {
27  mFilterModel = sourceModel;
28  if ( mFilterModel )
29  {
30  // rewire (filter-)change events in the source model so this proxy reflects the changes
31  connect( mFilterModel, SIGNAL( rowsAboutToBeRemoved( const QModelIndex&, int, int ) ), SLOT( onBeginRemoveRows( const QModelIndex&, int, int ) ) );
32  connect( mFilterModel, SIGNAL( rowsRemoved( const QModelIndex&, int, int ) ), SLOT( onEndRemoveRows( const QModelIndex&, int, int ) ) );
33  connect( mFilterModel, SIGNAL( rowsAboutToBeInserted( const QModelIndex&, int, int ) ), SLOT( onBeginInsertRows( const QModelIndex&, int, int ) ) );
34  connect( mFilterModel, SIGNAL( rowsInserted( const QModelIndex&, int, int ) ), SLOT( onEndInsertRows( const QModelIndex&, int, int ) ) );
35  // propagate sort order changes from source model to views connected to this model
36  connect( mFilterModel, SIGNAL( layoutAboutToBeChanged() ), this, SIGNAL( layoutAboutToBeChanged() ) );
37  connect( mFilterModel, SIGNAL( layoutChanged() ), this, SIGNAL( layoutChanged() ) );
38  }
39 }
40 
42 {
43  return mFilterModel->layerCache();
44 }
45 
47 {
48  return mFilterModel->masterModel()->rowToId( mapToMaster( index ).row() );
49 }
50 
52 {
53  return mFilterModel->mapFromMaster( mFilterModel->masterModel()->idToIndex( fid ) );
54 }
55 
57 {
58  if ( mInjectNull && index.row() == 0 )
59  {
60  if ( role == Qt::DisplayRole )
61  {
62  return QSettings().value( "qgis/nullValue", "NULL" ).toString();
63  }
64  else if ( role == QgsAttributeTableModel::FeatureIdRole )
65  {
66  return QVariant( QVariant::Int );
67  }
68  else
69  {
70  return QVariant( QVariant::Invalid );
71  }
72  }
73 
74  if ( role == Qt::DisplayRole || role == Qt::EditRole )
75  {
76  QgsFeature feat;
77 
78  mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
79 
80  QgsExpressionContext context;
83  << QgsExpressionContextUtils::layerScope( mFilterModel->layer() );
84  context.setFeature( feat );
85  return mExpression->evaluate( &context );
86  }
87 
88  if ( role == FeatureInfoRole )
89  {
90  FeatureInfo featInfo;
91 
92  QgsFeature feat;
93 
94  mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
95 
96  QgsVectorLayerEditBuffer* editBuffer = mFilterModel->layer()->editBuffer();
97 
98  if ( editBuffer )
99  {
100  const QList<QgsFeatureId> addedFeatures = editBuffer->addedFeatures().keys();
101  const QList<QgsFeatureId> changedFeatures = editBuffer->changedAttributeValues().keys();
102 
103  if ( addedFeatures.contains( feat.id() ) )
104  {
105  featInfo.isNew = true;
106  }
107  if ( changedFeatures.contains( feat.id() ) )
108  {
109  featInfo.isEdited = true;
110  }
111  }
112 
113  return QVariant::fromValue( featInfo );
114  }
115  else if ( role == FeatureRole )
116  {
117  QgsFeature feat;
118 
119  mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
120 
121  return QVariant::fromValue( feat );
122  }
123 
124  return sourceModel()->data( mapToSource( index ), role );
125 }
126 
128 {
129  if ( mInjectNull && index.row() == 0 )
130  {
131  return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
132  }
133  else
134  {
135  return sourceModel()->flags( mapToSource( index ) ) & ~Qt::ItemIsEditable;
136  }
137 }
138 
140 {
141  if ( mInjectNull != injectNull )
142  {
143  emit beginResetModel();
144  mInjectNull = injectNull;
145  emit endResetModel();
146  }
147 }
148 
150 {
151  return mInjectNull;
152 }
153 
155 {
156  return mFilterModel->masterModel();
157 }
158 
160 {
161  QgsExpression* exp = new QgsExpression( expression );
162 
163  QgsExpressionContext context;
166  << QgsExpressionContextUtils::layerScope( mFilterModel->layer() );
167 
168  exp->prepare( &context );
169 
170  if ( exp->hasParserError() )
171  {
172  mParserErrorString = exp->parserErrorString();
173  delete exp;
174  return false;
175  }
176 
177  delete mExpression;
178  mExpression = exp;
179 
180  emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
181  return true;
182 }
183 
185 {
186  return mParserErrorString;
187 }
188 
190 {
191  return mExpression->expression();
192 }
193 
195 {
196  return mFilterModel->layerCache()->featureAtId( idxToFid( index ), feat );
197 }
198 
199 void QgsFeatureListModel::onBeginRemoveRows( const QModelIndex& parent, int first, int last )
200 {
201  beginRemoveRows( parent, first, last );
202 }
203 
204 void QgsFeatureListModel::onEndRemoveRows( const QModelIndex& parent, int first, int last )
205 {
206  Q_UNUSED( parent )
207  Q_UNUSED( first )
208  Q_UNUSED( last )
209  endRemoveRows();
210 }
211 
212 void QgsFeatureListModel::onBeginInsertRows( const QModelIndex& parent, int first, int last )
213 {
214  beginInsertRows( parent, first, last );
215 }
216 
217 void QgsFeatureListModel::onEndInsertRows( const QModelIndex& parent, int first, int last )
218 {
219  Q_UNUSED( parent )
220  Q_UNUSED( first )
221  Q_UNUSED( last )
222  endInsertRows();
223 }
224 
226 {
227  if ( !proxyIndex.isValid() )
228  return QModelIndex();
229 
230  int offset = mInjectNull ? 1 : 0;
231 
232  return mFilterModel->mapToMaster( mFilterModel->index( proxyIndex.row() - offset, proxyIndex.column() ) );
233 }
234 
236 {
237  if ( !sourceIndex.isValid() )
238  return QModelIndex();
239 
240  int offset = mInjectNull ? 1 : 0;
241 
242  return createIndex( mFilterModel->mapFromMaster( sourceIndex ).row() + offset, 0 );
243 }
244 
246 {
247  return mapSelectionFromSource( mFilterModel->mapSelectionFromSource( selection ) );
248 }
249 
251 {
252  return mFilterModel->mapSelectionToSource( mapSelectionToSource( selection ) );
253 }
254 
255 // Override some methods from QAbstractProxyModel, not that interesting
256 
258 {
259  if ( !proxyIndex.isValid() )
260  return QModelIndex();
261 
262  int offset = mInjectNull ? 1 : 0;
263 
264  return sourceModel()->index( proxyIndex.row() - offset, proxyIndex.column() );
265 }
266 
268 {
269  if ( !sourceIndex.isValid() )
270  return QModelIndex();
271 
272  return createIndex( sourceIndex.row(), 0 );
273 }
274 
275 QModelIndex QgsFeatureListModel::index( int row, int column, const QModelIndex& parent ) const
276 {
277  Q_UNUSED( parent )
278 
279  return createIndex( row, column );
280 }
281 
283 {
284  Q_UNUSED( child )
285  return QModelIndex();
286 }
287 
289 {
290  Q_UNUSED( parent )
291  return 1;
292 }
293 
295 {
296  Q_UNUSED( parent )
297 
298  int offset = mInjectNull ? 1 : 0;
299 
300  return sourceModel()->rowCount() + offset;
301 }
302 
304 {
305  return mapFromMaster( masterModel()->idToIndex( fid ) );
306 }
307 
309 {
310  return QModelIndexList() << fidToIndex( fid );
311 }
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