QGIS API Documentation  2.99.0-Master (d55fa22)
qgsatlascomposition.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsatlascomposermap.h
3  ---------------------
4  begin : October 2012
5  copyright : (C) 2005 by Hugo Mercier
6  email : hugo dot mercier at oslandia dot com
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSATLASCOMPOSITION_H
17 #define QGSATLASCOMPOSITION_H
18 
19 #include "qgis_core.h"
20 #include "qgsfeature.h"
21 #include "qgsgeometry.h"
22 #include "qgsrectangle.h"
23 #include "qgsexpression.h"
24 #include "qgsvectorlayerref.h"
25 #include <memory>
26 #include <QString>
27 #include <QDomElement>
28 #include <QDomDocument>
29 #include <QStringList>
30 
31 class QgsComposerMap;
32 class QgsComposition;
33 class QgsVectorLayer;
35 
44 class CORE_EXPORT QgsAtlasComposition : public QObject
45 {
46  Q_OBJECT
47  public:
48  QgsAtlasComposition( QgsComposition *composition );
49 
54  bool enabled() const { return mEnabled; }
55 
60  void setEnabled( bool enabled );
61 
66  bool hideCoverage() const { return mHideCoverage; }
67 
72  void setHideCoverage( bool hide );
73 
81  QString filenamePattern() const { return mFilenamePattern; }
82 
91  bool setFilenamePattern( const QString &pattern );
92 
98  QString filenamePatternErrorString() const { return mFilenameParserError; }
99 
104  QgsVectorLayer *coverageLayer() const { return mCoverageLayer.get(); }
105 
110  void setCoverageLayer( QgsVectorLayer *layer );
111 
118  QString pageNameExpression() const { return mPageNameExpression; }
119 
125  void setPageNameExpression( const QString &pageNameExpression ) { mPageNameExpression = pageNameExpression; }
126 
133  QString nameForPage( int pageNumber ) const;
134 
141  bool singleFile() const { return mSingleFile; }
142 
149  void setSingleFile( bool single ) { mSingleFile = single; }
150 
151  bool sortFeatures() const { return mSortFeatures; }
152  void setSortFeatures( bool doSort ) { mSortFeatures = doSort; }
153 
154  bool sortAscending() const { return mSortAscending; }
155  void setSortAscending( bool ascending ) { mSortAscending = ascending; }
156 
157  bool filterFeatures() const { return mFilterFeatures; }
158  void setFilterFeatures( bool doFilter ) { mFilterFeatures = doFilter; }
159 
160  QString featureFilter() const { return mFeatureFilter; }
161  void setFeatureFilter( const QString &expression ) { mFeatureFilter = expression; }
162 
168  QString featureFilterErrorString() const { return mFilterParserError; }
169 
170  QString sortKeyAttributeName() const { return mSortKeyAttributeName; }
171  void setSortKeyAttributeName( const QString &fieldName ) { mSortKeyAttributeName = fieldName; }
172 
179  QVector<qreal> predefinedScales() const { return mPredefinedScales; }
180 
187  void setPredefinedScales( const QVector<qreal> &scales );
188 
191  bool beginRender();
193  void endRender();
194 
196  int numFeatures() const;
197 
203  bool prepareForFeature( const int i, const bool updateMaps = true );
204 
208  bool prepareForFeature( const QgsFeature *feat );
209 
211  QString currentFilename() const;
212 
213  void writeXml( QDomElement &elem, QDomDocument &doc ) const;
214 
221  void readXml( const QDomElement &elem, const QDomDocument &doc );
222 
223  QgsComposition *composition() { return mComposition; }
224 
228  int updateFeatures();
229 
233  QgsFeature feature() const { return mCurrentFeature; }
234 
238  QString currentPageName() const;
239 
243  int currentFeatureNumber() const { return mCurrentFeatureNo; }
244 
246  void prepareMap( QgsComposerMap *map );
247 
249  QgsGeometry currentGeometry( const QgsCoordinateReferenceSystem &projectedTo = QgsCoordinateReferenceSystem() ) const;
250 
251  public slots:
252 
256  void refreshFeature();
257 
258  void nextFeature();
259  void prevFeature();
260  void lastFeature();
261  void firstFeature();
262 
263  signals:
265  void parameterChanged();
266 
268  void toggled( bool );
269 
271  void statusMsgChanged( const QString &message );
272 
274  void coverageLayerChanged( QgsVectorLayer *layer );
275 
277  void renderBegun();
278 
280  void renderEnded();
281 
283  void featureChanged( QgsFeature *feature );
284 
288  void numberFeaturesChanged( int numFeatures );
289 
290  private:
291 
295  bool updateFilenameExpression();
296 
300  bool evalFeatureFilename( const QgsExpressionContext &context );
301 
302  QgsComposition *mComposition = nullptr;
303 
304  bool mEnabled;
305  bool mHideCoverage;
306  QString mFilenamePattern;
307  QgsVectorLayerRef mCoverageLayer;
308  bool mSingleFile;
309 
310  QString mCurrentFilename;
311  // feature ordering
312  bool mSortFeatures;
313  // sort direction
314  bool mSortAscending;
315 
316  // current atlas feature number
317  int mCurrentFeatureNo;
318 
319  public:
320  typedef QMap< QgsFeatureId, QVariant > SorterKeys;
321 
322  private slots:
323  void removeLayers( const QStringList &layers );
324 
325  private:
326  // value of field that is used for ordering of features
327  SorterKeys mFeatureKeys;
328  // key (attribute index) used for ordering
329  QString mSortKeyAttributeName;
330 
331  QString mPageNameExpression;
332 
333  // feature filtering
334  bool mFilterFeatures;
335  // feature expression filter
336  QString mFeatureFilter;
337 
338  // id of each iterated feature (after filtering and sorting) paired with atlas page name
339  QVector< QPair<QgsFeatureId, QString> > mFeatureIds;
340 
341  QgsFeature mCurrentFeature;
342 
343  std::unique_ptr<QgsExpression> mFilenameExpr;
344 
345  // bounding box of the current feature transformed into map crs
346  QgsRectangle mTransformedFeatureBounds;
347 
348  QString mFilenameParserError;
349  QString mFilterParserError;
350 
351  //forces all atlas enabled maps to redraw
352  void updateAtlasMaps();
353 
354  //computes the extent of the current feature, in the crs of the specified map
355  void computeExtent( QgsComposerMap *map );
356 
357  QgsExpressionContext createExpressionContext();
358 
359  //list of predefined scales
360  QVector<qreal> mPredefinedScales;
361 
362  // projected geometry cache
363  mutable QMap<long, QgsGeometry> mGeometryCache;
364 };
365 
366 #endif
367 
368 
369 
QString sortKeyAttributeName() const
QMap< QgsFeatureId, QVariant > SorterKeys
A rectangle specified with double values.
Definition: qgsrectangle.h:38
void setSortFeatures(bool doSort)
QString filenamePatternErrorString() const
Returns an error string from parsing the filename expression.
QgsComposition * composition()
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:96
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
QVector< qreal > predefinedScales() const
Returns the current list of predefined scales for the atlas.
QString pageNameExpression() const
Returns the expression used for calculating the page name.
void setSortKeyAttributeName(const QString &fieldName)
void setSortAscending(bool ascending)
void setPageNameExpression(const QString &pageNameExpression)
Sets the expression used for calculating the page name.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool hideCoverage() const
Returns true if the atlas is set to hide the coverage layer.
QString featureFilter() const
Graphics scene for map printing.
Object representing map window.
int currentFeatureNumber() const
Returns the current feature number, where a value of 0 corresponds to the first feature.
void setFilterFeatures(bool doFilter)
void setFeatureFilter(const QString &expression)
void setSingleFile(bool single)
Sets whether the atlas should be exported to a single file.
This class represents a coordinate reference system (CRS).
QString filenamePattern() const
Returns the filename expression used for generating output filenames for each atlas page...
Class used to render an Atlas, iterating over geometry features.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
QgsFeature feature() const
Returns the current atlas feature.
QString featureFilterErrorString() const
Returns an error string from parsing the feature filter expression.
Represents a vector layer which manages a vector based data sets.
bool enabled() const
Returns whether the atlas generation is enabled.
bool singleFile() const
Returns whether the atlas will be exported to a single file.