QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 {
24  QAbstractProxyModel::setSourceModel( sourceModel );
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 }
QgsFeatureId id() const
Get the feature id for this feature.
Definition: qgsfeature.cpp:100
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:87
static unsigned index
QgsFeatureListModel(QgsAttributeTableFilterModel *sourceModel, QObject *parent=NULL)
virtual QVariant data(const QModelIndex &index, int role) const
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
Definition: qgsexpression.h:94
const QString expression() const
Alias for dump()
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
const QgsChangedAttributesMap & changedAttributeValues()
Changed attributes values which are not commited.
void onEndInsertRows(const QModelIndex &parent, int first, int last)
QModelIndex idToIndex(QgsFeatureId id) const
bool featureByIndex(const QModelIndex &index, QgsFeature &feat)
Container of fields for a vector layer.
Definition: qgsfield.h:172
virtual QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
virtual QItemSelection mapSelectionToMaster(const QItemSelection &selection) const
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
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)
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
QgsVectorLayerEditBuffer * editBuffer()
Buffer with uncommitted editing operations. Only valid after editing has been turned on...
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
QgsVectorLayerCache * layerCache() const
Returns the layerCache this filter acts on.
bool setDisplayExpression(const QString expression)
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
QModelIndexList fidToIndexList(QgsFeatureId fid)
virtual QModelIndex parent(const QModelIndex &child) const
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
QgsAttributeTableModel * masterModel()
void onBeginRemoveRows(const QModelIndex &parent, int first, int last)
This class caches features of a given QgsVectorLayer.
const QgsFeatureMap & addedFeatures()
New features which are not commited.
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
virtual const QgsFields & fields() const =0
Return a map of indexes with field names for this layer.
virtual QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
bool featureAtId(QgsFeatureId featureId, QgsFeature &feature, bool skipCache=false)
Gets the feature at the given feature id.
QModelIndex fidToIndex(QgsFeatureId fid)
QgsFeatureId idxToFid(const QModelIndex &index) const
qint64 QgsFeatureId
Definition: qgsfeature.h:30
void onEndRemoveRows(const QModelIndex &parent, int first, int last)
QgsVectorLayerCache * layerCache()
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const
QgsFeatureId rowToId(int row) const
Maps row to feature id.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
QgsVectorDataProvider * dataProvider()
Returns the data provider.
QModelIndex fidToIdx(const QgsFeatureId fid) const
QString parserErrorString() const
Returns parser error.
Definition: qgsexpression.h:96
virtual QItemSelection mapSelectionFromMaster(const QItemSelection &selection) const
QString displayExpression() const
virtual Qt::ItemFlags flags(const QModelIndex &index) const