QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsattributetablefiltermodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  QgsAttributeTableFilterModel.cpp
3  --------------------------------------
4  Date : Feb 2009
5  Copyright : (C) 2009 Vita Cizek
6  Email : weetya (at) gmail.com
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 
16 #include <QItemSelectionModel>
17 
18 #include "qgis.h"
20 #include "qgsattributetablemodel.h"
21 #include "qgsfeatureiterator.h"
22 #include "qgsvectorlayer.h"
24 #include "qgsfeature.h"
25 #include "qgsmapcanvas.h"
26 #include "qgslogger.h"
27 #include "qgsrenderer.h"
30 #include "qgsapplication.h"
31 #include "qgsvectorlayercache.h"
32 #include "qgsrendercontext.h"
33 #include "qgsmapcanvasutils.h"
34 
36 // Filter Model //
38 
40  : QSortFilterProxyModel( parent )
41  , mCanvas( canvas )
42 {
43  setSourceModel( sourceModel );
44  setDynamicSortFilter( true );
45  setSortRole( QgsAttributeTableModel::SortRole );
46  connect( layer(), &QgsVectorLayer::selectionChanged, this, &QgsAttributeTableFilterModel::selectionChanged );
47 
48  mReloadVisibleTimer.setSingleShot( true );
49  connect( &mReloadVisibleTimer, &QTimer::timeout, this, &QgsAttributeTableFilterModel::reloadVisible );
50  mFilterFeaturesTimer.setSingleShot( true );
51  connect( &mFilterFeaturesTimer, &QTimer::timeout, this, &QgsAttributeTableFilterModel::filterFeatures );
52 }
53 
54 bool QgsAttributeTableFilterModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
55 {
56  if ( mSelectedOnTop )
57  {
58  const bool leftSelected = layer()->selectedFeatureIds().contains( masterModel()->rowToId( left.row() ) );
59  const bool rightSelected = layer()->selectedFeatureIds().contains( masterModel()->rowToId( right.row() ) );
60 
61  if ( leftSelected && !rightSelected )
62  {
63  return sortOrder() == Qt::AscendingOrder;
64  }
65  else if ( rightSelected && !leftSelected )
66  {
67  return sortOrder() == Qt::DescendingOrder;
68  }
69  }
70 
71  if ( mTableModel->sortCacheExpression().isEmpty() )
72  {
73  //shortcut when no sort order set
74  return false;
75  }
76 
78  right.data( QgsAttributeTableModel::SortRole ) );
79 }
80 
81 void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )
82 {
83  if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
84  order = Qt::AscendingOrder;
85  if ( column < 0 || column >= mColumnMapping.size() )
86  {
87  sort( QString() );
88  }
89  else
90  {
91  const int myColumn = mColumnMapping.at( column );
92  masterModel()->prefetchColumnData( myColumn );
93  QSortFilterProxyModel::sort( myColumn, order );
94  }
95  emit sortColumnChanged( column, order );
96 }
97 
98 QVariant QgsAttributeTableFilterModel::data( const QModelIndex &index, int role ) const
99 {
100  if ( mapColumnToSource( index.column() ) == -1 ) // actions
101  {
102  if ( role == TypeRole )
103  return ColumnTypeActionButton;
104  else if ( role == QgsAttributeTableModel::FeatureIdRole )
105  {
106  const QModelIndex fieldIndex = QSortFilterProxyModel::mapToSource( QSortFilterProxyModel::index( index.row(), 0, index.parent() ) );
107  return sourceModel()->data( fieldIndex, QgsAttributeTableModel::FeatureIdRole );
108  }
109  }
110  else if ( role == TypeRole )
111  return ColumnTypeField;
112 
113  return QSortFilterProxyModel::data( index, role );
114 }
115 
116 QVariant QgsAttributeTableFilterModel::headerData( int section, Qt::Orientation orientation, int role ) const
117 {
118  if ( orientation == Qt::Horizontal )
119  {
120  if ( mColumnMapping.at( section ) == -1 && role == Qt::DisplayRole )
121  return tr( "Actions" );
122  else
123  return QSortFilterProxyModel::headerData( section, orientation, role );
124  }
125  else
126  {
127  if ( role == Qt::DisplayRole )
128  return section + 1;
129  else
130  {
131  const int sourceSection = mapToSource( index( section, ( !mColumnMapping.isEmpty() && mColumnMapping.at( 0 ) == -1 ) ? 1 : 0 ) ).row();
132  return sourceModel()->headerData( sourceSection, orientation, role );
133  }
134  }
135 }
136 
138 {
139  return mColumnMapping.indexOf( -1 );
140 }
141 
142 int QgsAttributeTableFilterModel::columnCount( const QModelIndex &parent ) const
143 {
144  Q_UNUSED( parent )
145  return mColumnMapping.count();
146 }
147 
149 {
150  const QgsAttributeTableConfig oldConfig = mConfig;
151  mConfig = config;
152  mConfig.update( layer()->fields() );
153 
154  if ( !force && mConfig.hasSameColumns( oldConfig ) )
155  {
156  return;
157  }
158 
159  QVector<int> newColumnMapping;
160  const auto constColumns = mConfig.columns();
161  for ( const QgsAttributeTableConfig::ColumnConfig &columnConfig : constColumns )
162  {
163  // Hidden? Forget about this column
164  if ( columnConfig.hidden )
165  continue;
166 
167  // The new value for the mapping (field index or -1 for action column)
168  const int newValue = ( columnConfig.type == QgsAttributeTableConfig::Action ) ? -1 : layer()->fields().lookupField( columnConfig.name );
169  newColumnMapping << newValue;
170  }
171 
172  if ( newColumnMapping != mColumnMapping )
173  {
174  bool requiresReset = false;
175  int firstRemovedColumn = -1;
176  int removedColumnCount = 0;
177 
178  // Check if there have a contiguous set of columns have been removed or if we require a full reset
179  for ( int i = 0; i < std::min( newColumnMapping.size(), mColumnMapping.size() - removedColumnCount ); ++i )
180  {
181  if ( newColumnMapping.at( i ) == mColumnMapping.at( i + removedColumnCount ) )
182  continue;
183 
184  if ( firstRemovedColumn == -1 )
185  {
186  firstRemovedColumn = i;
187 
188  while ( i < mColumnMapping.size() - removedColumnCount && mColumnMapping.at( i + removedColumnCount ) != newColumnMapping.at( i ) )
189  {
190  ++removedColumnCount;
191  }
192  }
193  else
194  {
195  requiresReset = true;
196  break;
197  }
198  }
199 
200  // No difference found so far
201  if ( firstRemovedColumn == -1 )
202  {
203  if ( newColumnMapping.size() > mColumnMapping.size() )
204  {
205  // More columns: appended to the end
206  beginInsertColumns( QModelIndex(), mColumnMapping.size(), newColumnMapping.size() - 1 );
207  mColumnMapping = newColumnMapping;
208  endInsertColumns();
209  }
210  else
211  {
212  // Less columns: removed from the end
213  beginRemoveColumns( QModelIndex(), newColumnMapping.size(), mColumnMapping.size() - 1 );
214  mColumnMapping = newColumnMapping;
215  endRemoveColumns();
216  }
217  }
218  else
219  {
220  if ( newColumnMapping.size() == mColumnMapping.size() - removedColumnCount )
221  {
222  //the amount of removed column in the model need to be equal removedColumnCount
223  beginRemoveColumns( QModelIndex(), firstRemovedColumn, firstRemovedColumn + removedColumnCount - 1 );
224  mColumnMapping = newColumnMapping;
225  endRemoveColumns();
226  }
227  else
228  {
229  requiresReset = true;
230  }
231  }
232 
233  if ( requiresReset )
234  {
235  beginResetModel();
236  mColumnMapping = newColumnMapping;
237  endResetModel();
238  }
239  }
240 
241  if ( !config.sortExpression().isEmpty() )
242  sort( config.sortExpression(), config.sortOrder() );
243 }
244 
246 {
247  mFilterExpression = expression;
248  mFilterExpressionContext = context;
249 }
250 
251 void QgsAttributeTableFilterModel::sort( const QString &expression, Qt::SortOrder order )
252 {
253  if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
254  order = Qt::AscendingOrder;
255 
256  QSortFilterProxyModel::sort( -1 );
257  masterModel()->prefetchSortData( expression );
258  QSortFilterProxyModel::sort( 0, order );
259 }
260 
262 {
263  return masterModel()->sortCacheExpression();
264 }
265 
267 {
268  if ( mSelectedOnTop != selectedOnTop )
269  {
270  mSelectedOnTop = selectedOnTop;
271  int column = sortColumn();
272  Qt::SortOrder order = sortOrder();
273 
274  // set default sort values if they are not correctly set
275  if ( column < 0 )
276  column = 0;
277 
278  if ( order != Qt::AscendingOrder && order != Qt::DescendingOrder )
279  order = Qt::AscendingOrder;
280 
281  sort( 0, Qt::AscendingOrder );
282  invalidate();
283  }
284 }
285 
287 {
288  mTableModel = sourceModel;
289 
290  for ( int i = 0; i < mTableModel->columnCount() - mTableModel->extraColumns(); ++i )
291  {
292  mColumnMapping.append( i );
293  }
294 
295  QSortFilterProxyModel::setSourceModel( sourceModel );
296 
297  // Disconnect any code to update columns in the parent, we handle this manually
298  disconnect( mTableModel, SIGNAL( columnsAboutToBeInserted( QModelIndex, int, int ) ), this, SLOT( _q_sourceColumnsAboutToBeInserted( QModelIndex, int, int ) ) );
299  disconnect( mTableModel, SIGNAL( columnsInserted( QModelIndex, int, int ) ), this, SLOT( _q_sourceColumnsInserted( QModelIndex, int, int ) ) );
300  disconnect( mTableModel, SIGNAL( columnsAboutToBeRemoved( QModelIndex, int, int ) ), this, SLOT( _q_sourceColumnsAboutToBeRemoved( QModelIndex, int, int ) ) );
301  disconnect( mTableModel, SIGNAL( columnsRemoved( QModelIndex, int, int ) ), this, SLOT( _q_sourceColumnsRemoved( QModelIndex, int, int ) ) );
302  // The following connections are needed in order to keep the filter model in sync, see: regression #15974
303  connect( mTableModel, &QAbstractItemModel::columnsAboutToBeInserted, this, &QgsAttributeTableFilterModel::onColumnsChanged );
304  connect( mTableModel, &QAbstractItemModel::columnsAboutToBeRemoved, this, &QgsAttributeTableFilterModel::onColumnsChanged );
305 
306 }
307 
309 {
310  return mSelectedOnTop;
311 }
312 
314 {
315  mFilteredFeatures = ids;
317  invalidateFilter();
318 }
319 
321 {
322  QgsFeatureIds ids;
323  ids.reserve( rowCount() );
324  for ( int i = 0; i < rowCount(); ++i )
325  {
326  const QModelIndex row = index( i, 0 );
327  ids << rowToId( row );
328  }
329  return ids;
330 }
331 
333 {
334  if ( filterMode != mFilterMode )
335  {
338  mFilterMode = filterMode;
339  invalidate();
340  }
341 }
342 
344 {
345  // cleanup existing connections
346  switch ( mFilterMode )
347  {
348  case ShowVisible:
349  disconnect( mCanvas, &QgsMapCanvas::extentsChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
350  disconnect( mCanvas, &QgsMapCanvas::temporalRangeChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
351  disconnect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
352  disconnect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
353  break;
354  case ShowAll:
355  case ShowEdited:
356  case ShowSelected:
357  break;
358  case ShowFilteredList:
359  disconnect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedFilterFeatures );
360  disconnect( layer(), &QgsVectorLayer::attributeValueChanged, this, &QgsAttributeTableFilterModel::onAttributeValueChanged );
361  disconnect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::onGeometryChanged );
362  break;
363  }
364 }
365 
367 {
368  // setup new connections
369  switch ( filterMode )
370  {
371  case ShowVisible:
372  connect( mCanvas, &QgsMapCanvas::extentsChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
373  connect( mCanvas, &QgsMapCanvas::temporalRangeChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
374  connect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
375  connect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::startTimedReloadVisible );
377  break;
378  case ShowAll:
379  case ShowEdited:
380  case ShowSelected:
381  break;
382  case ShowFilteredList:
383  connect( layer(), &QgsVectorLayer::featureAdded, this, &QgsAttributeTableFilterModel::startTimedFilterFeatures );
384  connect( layer(), &QgsVectorLayer::attributeValueChanged, this, &QgsAttributeTableFilterModel::onAttributeValueChanged );
385  connect( layer(), &QgsVectorLayer::geometryChanged, this, &QgsAttributeTableFilterModel::onGeometryChanged );
386  break;
387  }
388 }
389 
390 bool QgsAttributeTableFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
391 {
392  Q_UNUSED( sourceParent )
393  switch ( mFilterMode )
394  {
395  case ShowAll:
396  return true;
397 
398  case ShowFilteredList:
399  return mFilteredFeatures.contains( masterModel()->rowToId( sourceRow ) );
400 
401  case ShowSelected:
402  return layer()->selectedFeatureIds().contains( masterModel()->rowToId( sourceRow ) );
403 
404  case ShowVisible:
405  return mFilteredFeatures.contains( masterModel()->rowToId( sourceRow ) );
406 
407  case ShowEdited:
408  {
409  QgsVectorLayerEditBuffer *editBuffer = layer()->editBuffer();
410  if ( editBuffer )
411  {
412  const QgsFeatureId fid = masterModel()->rowToId( sourceRow );
413 
414  if ( editBuffer->isFeatureAdded( fid ) )
415  return true;
416 
417  if ( editBuffer->isFeatureAttributesChanged( fid ) )
418  return true;
419 
420  if ( editBuffer->isFeatureGeometryChanged( fid ) )
421  return true;
422 
423  return false;
424  }
425  return false;
426  }
427 
428  default:
429  Q_ASSERT( false ); // In debug mode complain
430  return true; // In release mode accept row
431  }
432  // returns are handled in their respective case statement above
433 }
434 
436 {
437  reloadVisible();
438 }
439 
440 void QgsAttributeTableFilterModel::reloadVisible()
441 {
443  invalidateFilter();
444  emit visibleReloaded();
445 }
446 
447 void QgsAttributeTableFilterModel::onAttributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
448 {
449  Q_UNUSED( fid );
450  Q_UNUSED( value );
451 
452  if ( mFilterExpression.referencedAttributeIndexes( layer()->fields() ).contains( idx ) )
453  {
454  startTimedFilterFeatures();
455  }
456 }
457 
458 void QgsAttributeTableFilterModel::onGeometryChanged()
459 {
460  if ( mFilterExpression.needsGeometry() )
461  {
462  startTimedFilterFeatures();
463  }
464 }
465 
466 void QgsAttributeTableFilterModel::startTimedReloadVisible()
467 {
468  mReloadVisibleTimer.start( 10 );
469 }
470 
471 void QgsAttributeTableFilterModel::startTimedFilterFeatures()
472 {
473  mFilterFeaturesTimer.start( 10 );
474 }
475 
477 {
478  if ( !mFilterExpression.isValid() )
479  return;
480 
482  QgsDistanceArea distanceArea;
483 
484  distanceArea.setSourceCrs( mTableModel->layer()->crs(), QgsProject::instance()->transformContext() );
485  distanceArea.setEllipsoid( QgsProject::instance()->ellipsoid() );
486 
487  const bool fetchGeom = mFilterExpression.needsGeometry();
488 
489  QApplication::setOverrideCursor( Qt::WaitCursor );
490 
491  mFilterExpression.setGeomCalculator( &distanceArea );
492  mFilterExpression.setDistanceUnits( QgsProject::instance()->distanceUnits() );
493  mFilterExpression.setAreaUnits( QgsProject::instance()->areaUnits() );
494  QgsFeatureRequest request( mTableModel->request() );
495  request.setSubsetOfAttributes( mFilterExpression.referencedColumns(), mTableModel->layer()->fields() );
496  if ( !fetchGeom )
497  {
499  }
500  else
501  {
502  // force geometry extraction if the filter requests it
503  request.setFlags( request.flags() & ~QgsFeatureRequest::NoGeometry );
504  }
505  QgsFeatureIterator featIt = mTableModel->layer()->getFeatures( request );
506 
507  QgsFeature f;
508 
509  // Record the first evaluation error
510  QString error;
511 
512  while ( featIt.nextFeature( f ) )
513  {
514  mFilterExpressionContext.setFeature( f );
515  if ( mFilterExpression.evaluate( &mFilterExpressionContext ).toInt() != 0 )
516  filteredFeatures << f.id();
517 
518  // check if there were errors during evaluating
519  if ( mFilterExpression.hasEvalError() && error.isEmpty() )
520  {
521  error = mFilterExpression.evalErrorString( );
522  }
523  }
524 
525  featIt.close();
526 
528 
529  QApplication::restoreOverrideCursor();
530 
531  emit featuresFiltered();
532 
533  if ( ! error.isEmpty() )
534  {
535  emit filterError( error );
536  }
537 
538 }
539 
540 
541 void QgsAttributeTableFilterModel::selectionChanged()
542 {
543  if ( ShowSelected == mFilterMode )
544  {
545  invalidateFilter();
546  }
547  else if ( mSelectedOnTop )
548  {
549  invalidate();
550  }
551 }
552 
553 void QgsAttributeTableFilterModel::onColumnsChanged()
554 {
555  setAttributeTableConfig( mConfig );
556 }
557 
558 int QgsAttributeTableFilterModel::mapColumnToSource( int column ) const
559 {
560  if ( mColumnMapping.isEmpty() )
561  return column;
562  if ( column < 0 || column >= mColumnMapping.size() )
563  return -1;
564  else
565  return mColumnMapping.at( column );
566 }
567 
568 int QgsAttributeTableFilterModel::mapColumnFromSource( int column ) const
569 {
570  if ( mColumnMapping.isEmpty() )
571  return column;
572  else
573  return mColumnMapping.indexOf( column );
574 }
575 
577 {
578  if ( !layer() )
579  return;
580 
581  bool filter = false;
582  const QgsRectangle rect = mCanvas->mapSettings().mapToLayerCoordinates( layer(), mCanvas->extent() );
583  QgsRenderContext renderContext;
585 
586  mFilteredFeatures.clear();
587  if ( !layer()->renderer() )
588  {
589  QgsDebugMsg( QStringLiteral( "Cannot get renderer" ) );
590  return;
591  }
592 
593  std::unique_ptr< QgsFeatureRenderer > renderer( layer()->renderer()->clone() );
594 
595  const QgsMapSettings &ms = mCanvas->mapSettings();
596  if ( !layer()->isInScaleRange( ms.scale() ) )
597  {
598  QgsDebugMsg( QStringLiteral( "Out of scale limits" ) );
599  }
600  else
601  {
602  if ( renderer->capabilities() & QgsFeatureRenderer::ScaleDependent )
603  {
604  // setup scale
605  // mapRenderer()->renderContext()->scale is not automatically updated when
606  // render extent changes (because it's scale is used to identify if changed
607  // since last render) -> use local context
608  renderContext.setExtent( ms.visibleExtent() );
609  renderContext.setMapToPixel( ms.mapToPixel() );
610  renderContext.setRendererScale( ms.scale() );
611  }
612 
613  filter = renderer->capabilities() & QgsFeatureRenderer::Filter;
614  }
615 
616  renderer->startRender( renderContext, layer()->fields() );
617 
618  QgsFeatureRequest r( masterModel()->request() );
620  {
621  r.setFilterRect( r.filterRect().intersect( rect ) );
622  }
623  else
624  {
625  r.setFilterRect( rect );
626  }
627 
628  const QString canvasFilter = QgsMapCanvasUtils::filterForLayer( mCanvas, layer() );
629  if ( canvasFilter == QLatin1String( "FALSE" ) )
630  return;
631  if ( !canvasFilter.isEmpty() )
632  r.setFilterExpression( canvasFilter );
633 
635 
636  QgsFeature f;
637 
638  while ( features.nextFeature( f ) )
639  {
640  renderContext.expressionContext().setFeature( f );
641  if ( !filter || renderer->willRenderFeature( f, renderContext ) )
642  {
643  mFilteredFeatures << f.id();
644  }
645 #if 0
646  if ( t.elapsed() > 5000 )
647  {
648  bool cancel = false;
649  emit progress( i, cancel );
650  if ( cancel )
651  break;
652 
653  t.restart();
654  }
655 #endif
656  }
657 
658  features.close();
659 
660  if ( renderer->capabilities() & QgsFeatureRenderer::ScaleDependent )
661  {
662  renderer->stopRender( renderContext );
663  }
664 }
665 
667 {
668  return masterModel()->rowToId( mapToSource( row ).row() );
669 }
670 
672 {
673  return mapFromMaster( masterModel()->idToIndex( fid ) );
674 }
675 
677 {
678  QModelIndexList indexes;
679  const auto constIdToIndexList = masterModel()->idToIndexList( fid );
680  for ( const QModelIndex &idx : constIdToIndexList )
681  {
682  indexes.append( mapFromMaster( idx ) );
683  }
684 
685  return indexes;
686 }
687 
688 QModelIndex QgsAttributeTableFilterModel::mapToSource( const QModelIndex &proxyIndex ) const
689 {
690  if ( !proxyIndex.isValid() )
691  return QModelIndex();
692 
693  int sourceColumn = mapColumnToSource( proxyIndex.column() );
694 
695  // For the action column there is no matching column in the source model, just return the first one
696  // so we are still able to query for the feature id, the feature...
697  if ( sourceColumn == -1 )
698  sourceColumn = 0;
699 
700  return QSortFilterProxyModel::mapToSource( index( proxyIndex.row(), sourceColumn, proxyIndex.parent() ) );
701 }
702 
703 QModelIndex QgsAttributeTableFilterModel::mapFromSource( const QModelIndex &sourceIndex ) const
704 {
705  const QModelIndex proxyIndex = QSortFilterProxyModel::mapFromSource( sourceIndex );
706 
707  if ( proxyIndex.column() < 0 )
708  return QModelIndex();
709 
710  int col = mapColumnFromSource( proxyIndex.column() );
711 
712  if ( col == -1 )
713  col = 0;
714 
715  return index( proxyIndex.row(), col, proxyIndex.parent() );
716 }
717 
718 Qt::ItemFlags QgsAttributeTableFilterModel::flags( const QModelIndex &index ) const
719 {
720  // Handle the action column flags here, the master model doesn't know it
721  if ( mapColumnToSource( index.column() ) == -1 )
722  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
723 
724  const QModelIndex source_index = mapToSource( index );
725  return masterModel()->flags( source_index );
726 }
QgsFeatureRequest::NoGeometry
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition: qgsfeaturerequest.h:115
QgsAttributeTableConfig::sortOrder
Qt::SortOrder sortOrder() const
Gets the sort order.
Definition: qgsattributetableconfig.cpp:244
QgsVectorLayer::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Definition: qgsvectorlayer.cpp:1052
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsMapLayer::crs
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
QgsVectorLayer::editBuffer
Q_INVOKABLE QgsVectorLayerEditBuffer * editBuffer()
Buffer with uncommitted editing operations. Only valid after editing has been turned on.
Definition: qgsvectorlayer.h:2014
QgsAttributeTableFilterModel::ShowAll
@ ShowAll
Show all features.
Definition: qgsattributetablefiltermodel.h:47
qgsexpressioncontextutils.h
QgsFeatureRenderer::Filter
@ Filter
Features may be filtered, i.e. some features may not be rendered (categorized, rule based ....
Definition: qgsrenderer.h:265
QgsVectorLayerEditBuffer::isFeatureAttributesChanged
bool isFeatureAttributesChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had an attribute changed but not committed.
Definition: qgsvectorlayereditbuffer.h:146
QgsExpressionContext::appendScopes
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
Definition: qgsexpressioncontext.cpp:499
QgsMapCanvas::extent
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
Definition: qgsmapcanvas.cpp:1331
QgsAttributeTableConfig::sortExpression
QString sortExpression() const
Gets the expression used for sorting.
Definition: qgsattributetableconfig.cpp:209
QgsMapSettings::mapToLayerCoordinates
QgsPointXY mapToLayerCoordinates(const QgsMapLayer *layer, QgsPointXY point) const
transform point coordinates from output CRS to layer's CRS
Definition: qgsmapsettings.cpp:633
QgsAttributeTableModel::idToIndexList
QModelIndexList idToIndexList(QgsFeatureId id) const
Definition: qgsattributetablemodel.cpp:572
QgsExpression::setDistanceUnits
void setDistanceUnits(QgsUnitTypes::DistanceUnit unit)
Sets the desired distance units for calculations involving geomCalculator(), e.g.,...
Definition: qgsexpression.cpp:419
qgsvectorlayercache.h
QgsAttributeTableFilterModel::sortExpression
QString sortExpression() const
The expression which is used to sort the attribute table.
Definition: qgsattributetablefiltermodel.cpp:261
QgsRenderContext::expressionContext
QgsExpressionContext & expressionContext()
Gets the expression context.
Definition: qgsrendercontext.h:625
QgsAttributeTableFilterModel::visibleReloaded
void visibleReloaded()
Emitted when the the visible features on extend are reloaded (the list is created)
QgsExpression::evalErrorString
QString evalErrorString() const
Returns evaluation error.
Definition: qgsexpression.cpp:383
qgsmapcanvas.h
QgsExpression::referencedColumns
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
Definition: qgsexpression.cpp:221
QgsAttributeTableFilterModel::connectFilterModeConnections
void connectFilterModeConnections(FilterMode filterMode)
Disconnect the connections set for the new filterMode.
Definition: qgsattributetablefiltermodel.cpp:366
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:437
qgsVariantLessThan
bool qgsVariantLessThan(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether the first is less than the second.
Definition: qgis.cpp:119
qgsmapcanvasutils.h
QgsAttributeTableFilterModel::setSourceModel
void setSourceModel(QgsAttributeTableModel *sourceModel)
Set the attribute table model that backs this model.
Definition: qgsattributetablefiltermodel.cpp:286
QgsAttributeTableFilterModel::ShowFilteredList
@ ShowFilteredList
Show only features whose ids are on the filter list. {.
Definition: qgsattributetablefiltermodel.h:50
QgsAttributeTableFilterModel::extentsChanged
Q_DECL_DEPRECATED void extentsChanged()
Is called upon every change of the visible extents on the map canvas.
Definition: qgsattributetablefiltermodel.cpp:435
QgsMapCanvas::temporalRangeChanged
void temporalRangeChanged()
Emitted when the map canvas temporal range changes.
qgsfeatureiterator.h
QgsExpression::referencedAttributeIndexes
QSet< int > referencedAttributeIndexes(const QgsFields &fields) const
Returns a list of field name indexes obtained from the provided fields.
Definition: qgsexpression.cpp:245
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsExpression::isValid
bool isValid() const
Checks if this expression is valid.
Definition: qgsexpression.cpp:201
QgsAttributeTableConfig::ColumnConfig
Defines the configuration of a column in the attribute table.
Definition: qgsattributetableconfig.h:52
qgsfeature.h
QgsAttributeTableFilterModel::sort
void sort(int column, Qt::SortOrder order=Qt::AscendingOrder) override
Sort by the given column using the given order.
Definition: qgsattributetablefiltermodel.cpp:81
QgsProject::transformContext
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:110
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsFeatureRequest::filterRect
QgsRectangle filterRect() const
Returns the rectangle from which features will be taken.
Definition: qgsfeaturerequest.cpp:117
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:480
QgsAttributeTableFilterModel::ShowEdited
@ ShowEdited
Show only features which have unsaved changes.
Definition: qgsattributetablefiltermodel.h:51
QgsExpressionContextUtils::globalProjectLayerScopes
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Definition: qgsexpressioncontextutils.cpp:377
QgsFeatureRequest::setSubsetOfAttributes
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Definition: qgsfeaturerequest.cpp:228
QgsVectorLayerCache::getFeatures
QgsFeatureIterator getFeatures(const QgsFeatureRequest &featureRequest=QgsFeatureRequest())
Query this VectorLayerCache for features.
Definition: qgsvectorlayercache.cpp:400
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsRectangle::intersect
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
Definition: qgsrectangle.h:333
QgsAttributeTableFilterModel::filteredFeatures
QgsFeatureIds filteredFeatures()
Gets a list of currently filtered feature ids.
Definition: qgsattributetablefiltermodel.cpp:320
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsAttributeTableFilterModel::columnCount
int columnCount(const QModelIndex &parent) const override
Definition: qgsattributetablefiltermodel.cpp:142
QgsFeatureRequest::flags
Flags flags() const
Returns the flags which affect how features are fetched.
Definition: qgsfeaturerequest.h:673
QgsAttributeTableFilterModel::ColumnTypeField
@ ColumnTypeField
This column shows a field.
Definition: qgsattributetablefiltermodel.h:60
QgsVectorLayerEditBuffer::isFeatureGeometryChanged
bool isFeatureGeometryChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had its geometry changed but not committed.
Definition: qgsvectorlayereditbuffer.h:179
QgsExpression::setAreaUnits
void setAreaUnits(QgsUnitTypes::AreaUnit unit)
Sets the desired areal units for calculations involving geomCalculator(), e.g., "$area".
Definition: qgsexpression.cpp:429
QgsFeatureRequest::setFilterExpression
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
Definition: qgsfeaturerequest.cpp:167
qgsapplication.h
QgsFeatureRequest::setFilterRect
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
Definition: qgsfeaturerequest.cpp:101
QgsExpression::setGeomCalculator
void setGeomCalculator(const QgsDistanceArea *calc)
Sets the geometry calculator used for distance and area calculations in expressions.
Definition: qgsexpression.cpp:318
QgsAttributeTableConfig
This is a container for configuration of the attribute table. The configuration is specific for one v...
Definition: qgsattributetableconfig.h:36
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsDistanceArea::setEllipsoid
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
Definition: qgsdistancearea.cpp:89
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
qgsvectorlayertemporalproperties.h
QgsExpression::hasEvalError
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
Definition: qgsexpression.cpp:378
QgsAttributeTableModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns item flags for the index.
Definition: qgsattributetablemodel.cpp:826
QgsAttributeTableFilterModel::featuresFiltered
void featuresFiltered()
Emitted when the filtering of the features has been done.
QgsAttributeTableConfig::Action
@ Action
This column represents an action widget.
Definition: qgsattributetableconfig.h:46
QgsVectorLayer::selectionChanged
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
Emitted when selection was changed.
QgsAttributeTableModel::rowToId
QgsFeatureId rowToId(int row) const
Maps row to feature id.
Definition: qgsattributetablemodel.cpp:587
QgsAttributeTableFilterModel::generateListOfVisibleFeatures
void generateListOfVisibleFeatures()
Updates the list of currently visible features on the map canvas.
Definition: qgsattributetablefiltermodel.cpp:576
QgsFeatureIterator::close
bool close()
Definition: qgsfeatureiterator.h:412
QgsAttributeTableFilterModel::sortColumnChanged
void sortColumnChanged(int column, Qt::SortOrder order)
Emitted whenever the sort column is changed.
QgsRenderContext::setExtent
void setExtent(const QgsRectangle &extent)
When rendering a map layer, calling this method sets the "clipping" extent for the layer (in the laye...
Definition: qgsrendercontext.h:426
QgsAttributeTableFilterModel::layer
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
Definition: qgsattributetablefiltermodel.h:160
QgsAttributeTableFilterModel::fidToIndexList
QModelIndexList fidToIndexList(QgsFeatureId fid)
Definition: qgsattributetablefiltermodel.cpp:676
QgsAttributeTableModel
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
Definition: qgsattributetablemodel.h:49
QgsVectorLayer::attributeValueChanged
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Emitted whenever an attribute value change is done in the edit buffer.
qgsattributetablefiltermodel.h
QgsAttributeTableConfig::columns
QVector< QgsAttributeTableConfig::ColumnConfig > columns() const
Gets the list with all columns and their configuration.
Definition: qgsattributetableconfig.cpp:20
QgsVectorLayer::selectedFeatureIds
const Q_INVOKABLE QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Definition: qgsvectorlayer.cpp:3621
QgsMapCanvas::extentsChanged
void extentsChanged()
Emitted when the extents of the map change.
QgsAttributeTableFilterModel::fidToIndex
QModelIndex fidToIndex(QgsFeatureId fid) override
Definition: qgsattributetablefiltermodel.cpp:671
qgsrendercontext.h
QgsAttributeTableFilterModel::masterModel
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
Definition: qgsattributetablefiltermodel.h:174
QgsRenderContext::setMapToPixel
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
Definition: qgsrendercontext.h:412
QgsAttributeTableModel::prefetchSortData
void prefetchSortData(const QString &expression, unsigned long cacheIndex=0)
Prefetches the entire data for an expression.
Definition: qgsattributetablemodel.cpp:956
QgsAttributeTableFilterModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: qgsattributetablefiltermodel.cpp:718
QgsAttributeTableFilterModel::setAttributeTableConfig
void setAttributeTableConfig(const QgsAttributeTableConfig &config, bool force=false)
Set the attribute table configuration to control which fields are shown, in which order they are show...
Definition: qgsattributetablefiltermodel.cpp:148
QgsAttributeTableFilterModel::setSelectedOnTop
void setSelectedOnTop(bool selectedOnTop)
Changes the sort order of the features.
Definition: qgsattributetablefiltermodel.cpp:266
QgsAttributeTableFilterModel::rowToId
QgsFeatureId rowToId(const QModelIndex &row)
Returns the feature id for a given model index.
Definition: qgsattributetablefiltermodel.cpp:666
QgsAttributeTableFilterModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: qgsattributetablefiltermodel.cpp:98
QgsMapSettings::scale
double scale() const
Returns the calculated map scale.
Definition: qgsmapsettings.cpp:458
qgsattributetablemodel.h
QgsAttributeTableModel::extraColumns
int extraColumns() const
Empty extra columns to announce from this model.
Definition: qgsattributetablemodel.cpp:102
QgsAttributeTableModel::layer
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
Definition: qgsattributetablemodel.h:168
qgsrenderer.h
QgsDistanceArea::setSourceCrs
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
Definition: qgsdistancearea.cpp:83
QgsAttributeTableFilterModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition: qgsattributetablefiltermodel.cpp:116
QgsAttributeTableModel::prefetchColumnData
void prefetchColumnData(int column)
Caches the entire data for one column.
Definition: qgsattributetablemodel.cpp:944
QgsAttributeTableFilterModel::TypeRole
@ TypeRole
The type of a given column.
Definition: qgsattributetablefiltermodel.h:72
QgsAttributeTableFilterModel::filterAcceptsRow
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
Returns true if the source row will be accepted.
Definition: qgsattributetablefiltermodel.cpp:390
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsAttributeTableFilterModel::selectedOnTop
bool selectedOnTop()
Returns if selected features are currently shown on top.
Definition: qgsattributetablefiltermodel.cpp:308
QgsFeatureRequest::spatialFilterType
Qgis::SpatialFilterType spatialFilterType() const
Returns the spatial filter type which is currently set on this request.
Definition: qgsfeaturerequest.h:369
QgsExpression::evaluate
QVariant evaluate()
Evaluate the feature and return the result.
Definition: qgsexpression.cpp:350
QgsAttributeTableFilterModel::filterError
void filterError(const QString &errorMessage)
Emitted when an error occurred while filtering features.
QgsAttributeTableModel::sortCacheExpression
QString sortCacheExpression(unsigned long cacheIndex=0) const
The expression which was used to fill the sorting cache at index cacheIndex.
Definition: qgsattributetablemodel.cpp:1027
QgsRenderContext::setRendererScale
void setRendererScale(double scale)
Sets the renderer map scale.
Definition: qgsrendercontext.h:487
qgsvectorlayer.h
QgsAttributeTableModel::FeatureIdRole
@ FeatureIdRole
Get the feature id of the feature in this row.
Definition: qgsattributetablemodel.h:56
QgsAttributeTableFilterModel::setFilterMode
void setFilterMode(FilterMode filterMode)
Set the filter mode the filter will use.
Definition: qgsattributetablefiltermodel.cpp:332
QgsAttributeTableFilterModel::disconnectFilterModeConnections
void disconnectFilterModeConnections()
Disconnect the connections set for the current filterMode.
Definition: qgsattributetablefiltermodel.cpp:343
QgsVectorLayerEditBuffer::isFeatureAdded
bool isFeatureAdded(QgsFeatureId id) const
Returns true if the specified feature ID has been added but not committed.
Definition: qgsvectorlayereditbuffer.h:132
QgsAttributeTableFilterModel::mapToSource
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
Definition: qgsattributetablefiltermodel.cpp:688
QgsAttributeTableFilterModel::mapFromMaster
QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
Definition: qgsattributetablefiltermodel.h:191
QgsAttributeTableFilterModel::ColumnTypeActionButton
@ ColumnTypeActionButton
This column shows action buttons.
Definition: qgsattributetablefiltermodel.h:61
QgsAttributeTableFilterModel::ShowSelected
@ ShowSelected
Show only selected features.
Definition: qgsattributetablefiltermodel.h:48
QgsAttributeTableFilterModel::mapFromSource
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
Definition: qgsattributetablefiltermodel.cpp:703
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsAttributeTableFilterModel::setFilterExpression
void setFilterExpression(const QgsExpression &expression, const QgsExpressionContext &context)
Set the expression and the context to be stored in case of the features need to be filtered again (li...
Definition: qgsattributetablefiltermodel.cpp:245
QgsVectorLayerEditBuffer
Definition: qgsvectorlayereditbuffer.h:37
QgsAttributeTableFilterModel::filterFeatures
void filterFeatures()
Updates the filtered features in the filter model.
Definition: qgsattributetablefiltermodel.cpp:476
QgsFeatureRenderer::ScaleDependent
@ ScaleDependent
Depends on scale if feature will be rendered (rule based )
Definition: qgsrenderer.h:266
QgsVectorLayer::geometryChanged
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Emitted whenever a geometry change is done in the edit buffer.
QgsAttributeTableFilterModel::lessThan
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override
Used by the sorting algorithm.
Definition: qgsattributetablefiltermodel.cpp:54
Qgis::SpatialFilterType::BoundingBox
@ BoundingBox
Filter using a bounding box.
QgsAttributeTableFilterModel::QgsAttributeTableFilterModel
QgsAttributeTableFilterModel(QgsMapCanvas *canvas, QgsAttributeTableModel *sourceModel, QObject *parent=nullptr)
Make sure, the master model is already loaded, so the selection will get synchronized.
Definition: qgsattributetablefiltermodel.cpp:39
QgsAttributeTableFilterModel::setFilteredFeatures
virtual void setFilteredFeatures(const QgsFeatureIds &ids)
Specify a list of features, which the filter will accept.
Definition: qgsattributetablefiltermodel.cpp:313
QgsAttributeTableFilterModel::actionColumnIndex
int actionColumnIndex() const
Gets the index of the first column that contains an action widget.
Definition: qgsattributetablefiltermodel.cpp:137
QgsExpression::needsGeometry
bool needsGeometry() const
Returns true if the expression uses feature geometry for some computation.
Definition: qgsexpression.cpp:270
QgsDistanceArea
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
Definition: qgsdistancearea.h:52
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsAttributeTableFilterModel::filterMode
FilterMode filterMode()
The current filterModel.
Definition: qgsattributetablefiltermodel.h:153
QgsAttributeTableModel::SortRole
@ SortRole
Role used for sorting start here.
Definition: qgsattributetablemodel.h:60
qgslogger.h
QgsAttributeTableModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Returns the number of columns.
Definition: qgsattributetablemodel.cpp:615
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:349
QgsAttributeTableModel::request
const QgsFeatureRequest & request() const
Gets the the feature request.
Definition: qgsattributetablemodel.cpp:1051
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
QgsMapSettings::visibleExtent
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
Definition: qgsmapsettings.cpp:411
QgsAttributeTableConfig::hasSameColumns
bool hasSameColumns(const QgsAttributeTableConfig &other) const
Compare this configuration's columns name, type, and order to other.
Definition: qgsattributetableconfig.cpp:299
QgsAttributeTableFilterModel::FilterMode
FilterMode
The filter mode defines how the rows should be filtered.
Definition: qgsattributetablefiltermodel.h:45
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsFeatureRequest::setFlags
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
Definition: qgsfeaturerequest.cpp:222
QgsMapSettings::mapToPixel
const QgsMapToPixel & mapToPixel() const
Definition: qgsmapsettings.h:527
qgsvectorlayereditbuffer.h
QgsAttributeTableModel::layerCache
QgsVectorLayerCache * layerCache() const
Returns the layer cache this model uses as backend.
Definition: qgsattributetablemodel.h:173
QgsMapCanvasUtils::filterForLayer
static QString filterForLayer(QgsMapCanvas *canvas, QgsVectorLayer *layer)
Constructs a filter to use for selecting features from the given layer, in order to apply filters whi...
Definition: qgsmapcanvasutils.cpp:79
QgsAttributeTableFilterModel::ShowVisible
@ ShowVisible
Show only visible features (depends on the map canvas)
Definition: qgsattributetablefiltermodel.h:49
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QgsExpressionContext::setFeature
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Definition: qgsexpressioncontext.cpp:525
QgsAttributeTableConfig::update
void update(const QgsFields &fields)
Update the configuration with the given fields.
Definition: qgsattributetableconfig.cpp:55
QgsVectorLayer::featureAdded
void featureAdded(QgsFeatureId fid)
Emitted when a new feature has been added to the layer.