QGIS API Documentation  2.99.0-Master (9f5e33a)
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 
157  QString fileFormat() const { return mFileFormat; }
158 
165  void setFileFormat( QString format ) { mFileFormat = format; }
166 
167  bool sortFeatures() const { return mSortFeatures; }
168  void setSortFeatures( bool doSort ) { mSortFeatures = doSort; }
169 
170  bool sortAscending() const { return mSortAscending; }
171  void setSortAscending( bool ascending ) { mSortAscending = ascending; }
172 
173  bool filterFeatures() const { return mFilterFeatures; }
174  void setFilterFeatures( bool doFilter ) { mFilterFeatures = doFilter; }
175 
176  QString featureFilter() const { return mFeatureFilter; }
177  void setFeatureFilter( const QString &expression ) { mFeatureFilter = expression; }
178 
184  QString featureFilterErrorString() const { return mFilterParserError; }
185 
186  QString sortKeyAttributeName() const { return mSortKeyAttributeName; }
187  void setSortKeyAttributeName( const QString &fieldName ) { mSortKeyAttributeName = fieldName; }
188 
195  QVector<qreal> predefinedScales() const { return mPredefinedScales; }
196 
203  void setPredefinedScales( const QVector<qreal> &scales );
204 
207  bool beginRender();
209  void endRender();
210 
212  int numFeatures() const;
213 
219  bool prepareForFeature( const int i, const bool updateMaps = true );
220 
224  bool prepareForFeature( const QgsFeature *feat );
225 
227  QString currentFilename() const;
228 
229  void writeXml( QDomElement &elem, QDomDocument &doc ) const;
230 
237  void readXml( const QDomElement &elem, const QDomDocument &doc );
238 
239  QgsComposition *composition() { return mComposition; }
240 
244  int updateFeatures();
245 
249  QgsFeature feature() const { return mCurrentFeature; }
250 
254  QString currentPageName() const;
255 
259  int currentFeatureNumber() const { return mCurrentFeatureNo; }
260 
262  void prepareMap( QgsComposerMap *map );
263 
265  QgsGeometry currentGeometry( const QgsCoordinateReferenceSystem &projectedTo = QgsCoordinateReferenceSystem() ) const;
266 
267  public slots:
268 
272  void refreshFeature();
273 
274  void nextFeature();
275  void prevFeature();
276  void lastFeature();
277  void firstFeature();
278 
279  signals:
281  void parameterChanged();
282 
284  void toggled( bool );
285 
287  void statusMsgChanged( const QString &message );
288 
290  void coverageLayerChanged( QgsVectorLayer *layer );
291 
293  void renderBegun();
294 
296  void renderEnded();
297 
299  void featureChanged( QgsFeature *feature );
300 
304  void numberFeaturesChanged( int numFeatures );
305 
306  private:
307 
311  bool updateFilenameExpression();
312 
316  bool evalFeatureFilename( const QgsExpressionContext &context );
317 
318  QgsComposition *mComposition = nullptr;
319 
320  bool mEnabled;
321  bool mHideCoverage;
322  QString mFilenamePattern;
323  QgsVectorLayerRef mCoverageLayer;
324  bool mSingleFile;
325  QString mFileFormat;
326 
327  QString mCurrentFilename;
328  // feature ordering
329  bool mSortFeatures;
330  // sort direction
331  bool mSortAscending;
332 
333  // current atlas feature number
334  int mCurrentFeatureNo;
335 
336  public:
337  typedef QMap< QgsFeatureId, QVariant > SorterKeys;
338 
339  private slots:
340  void removeLayers( const QStringList &layers );
341 
342  private:
343  // value of field that is used for ordering of features
344  SorterKeys mFeatureKeys;
345  // key (attribute index) used for ordering
346  QString mSortKeyAttributeName;
347 
348  QString mPageNameExpression;
349 
350  // feature filtering
351  bool mFilterFeatures;
352  // feature expression filter
353  QString mFeatureFilter;
354 
355  // id of each iterated feature (after filtering and sorting) paired with atlas page name
356  QVector< QPair<QgsFeatureId, QString> > mFeatureIds;
357 
358  QgsFeature mCurrentFeature;
359 
360  std::unique_ptr<QgsExpression> mFilenameExpr;
361 
362  // bounding box of the current feature transformed into map crs
363  QgsRectangle mTransformedFeatureBounds;
364 
365  QString mFilenameParserError;
366  QString mFilterParserError;
367 
368  //forces all atlas enabled maps to redraw
369  void updateAtlasMaps();
370 
371  //computes the extent of the current feature, in the crs of the specified map
372  void computeExtent( QgsComposerMap *map );
373 
374  QgsExpressionContext createExpressionContext();
375 
376  //list of predefined scales
377  QVector<qreal> mPredefinedScales;
378 
379  // projected geometry cache
380  mutable QMap<long, QgsGeometry> mGeometryCache;
381 };
382 
383 #endif
384 
385 
386 
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 setFileFormat(QString format)
Sets the atlas file format used for image exports.
QString fileFormat() const
Returns the atlas file format used for image exports.
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.