QGIS API Documentation  2.99.0-Master (25b0421)
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 
45 class CORE_EXPORT QgsAtlasComposition : public QObject
46 {
47  Q_OBJECT
48  public:
49  QgsAtlasComposition( QgsComposition *composition );
50 
56  bool enabled() const { return mEnabled; }
57 
63  void setEnabled( bool enabled );
64 
70  bool hideCoverage() const { return mHideCoverage; }
71 
77  void setHideCoverage( bool hide );
78 
87  QString filenamePattern() const { return mFilenamePattern; }
88 
98  bool setFilenamePattern( const QString &pattern );
99 
106  QString filenamePatternErrorString() const { return mFilenameParserError; }
107 
113  QgsVectorLayer *coverageLayer() const { return mCoverageLayer.get(); }
114 
120  void setCoverageLayer( QgsVectorLayer *layer );
121 
129  QString pageNameExpression() const { return mPageNameExpression; }
130 
137  void setPageNameExpression( const QString &pageNameExpression ) { mPageNameExpression = pageNameExpression; }
138 
146  QString nameForPage( int pageNumber ) const;
147 
155  bool singleFile() const { return mSingleFile; }
156 
164  void setSingleFile( bool single ) { mSingleFile = single; }
165 
173  QString fileFormat() const { return mFileFormat; }
174 
182  void setFileFormat( const QString &format ) { mFileFormat = format; }
183 
184  bool sortFeatures() const { return mSortFeatures; }
185  void setSortFeatures( bool doSort ) { mSortFeatures = doSort; }
186 
187  bool sortAscending() const { return mSortAscending; }
188  void setSortAscending( bool ascending ) { mSortAscending = ascending; }
189 
190  bool filterFeatures() const { return mFilterFeatures; }
191  void setFilterFeatures( bool doFilter ) { mFilterFeatures = doFilter; }
192 
193  QString featureFilter() const { return mFeatureFilter; }
194  void setFeatureFilter( const QString &expression ) { mFeatureFilter = expression; }
195 
202  QString featureFilterErrorString() const { return mFilterParserError; }
203 
204  QString sortKeyAttributeName() const { return mSortKeyAttributeName; }
205  void setSortKeyAttributeName( const QString &fieldName ) { mSortKeyAttributeName = fieldName; }
206 
214  QVector<qreal> predefinedScales() const { return mPredefinedScales; }
215 
223  void setPredefinedScales( const QVector<qreal> &scales );
224 
228  bool beginRender();
230  void endRender();
231 
233  int numFeatures() const;
234 
241  bool prepareForFeature( const int i, const bool updateMaps = true );
242 
247  bool prepareForFeature( const QgsFeature *feat );
248 
250  QString currentFilename() const;
251 
252  void writeXml( QDomElement &elem, QDomDocument &doc ) const;
253 
261  void readXml( const QDomElement &elem, const QDomDocument &doc );
262 
263  QgsComposition *composition() { return mComposition; }
264 
269  int updateFeatures();
270 
275  QgsFeature feature() const { return mCurrentFeature; }
276 
281  QString currentPageName() const;
282 
287  int currentFeatureNumber() const { return mCurrentFeatureNo; }
288 
290  void prepareMap( QgsComposerMap *map );
291 
293  QgsGeometry currentGeometry( const QgsCoordinateReferenceSystem &projectedTo = QgsCoordinateReferenceSystem() ) const;
294 
295  public slots:
296 
301  void refreshFeature();
302 
303  void nextFeature();
304  void prevFeature();
305  void lastFeature();
306  void firstFeature();
307 
308  signals:
310  void parameterChanged();
311 
313  void toggled( bool );
314 
316  void statusMsgChanged( const QString &message );
317 
319  void coverageLayerChanged( QgsVectorLayer *layer );
320 
322  void renderBegun();
323 
325  void renderEnded();
326 
328  void featureChanged( QgsFeature *feature );
329 
334  void numberFeaturesChanged( int numFeatures );
335 
336  private:
337 
342  bool updateFilenameExpression();
343 
348  bool evalFeatureFilename( const QgsExpressionContext &context );
349 
350  QgsComposition *mComposition = nullptr;
351 
352  bool mEnabled;
353  bool mHideCoverage;
354  QString mFilenamePattern;
355  QgsVectorLayerRef mCoverageLayer;
356  bool mSingleFile;
357  QString mFileFormat;
358 
359  QString mCurrentFilename;
360  // feature ordering
361  bool mSortFeatures;
362  // sort direction
363  bool mSortAscending;
364 
365  // current atlas feature number
366  int mCurrentFeatureNo;
367 
368  public:
369  typedef QMap< QgsFeatureId, QVariant > SorterKeys;
370 
371  private slots:
372  void removeLayers( const QStringList &layers );
373 
374  private:
375  // value of field that is used for ordering of features
376  SorterKeys mFeatureKeys;
377  // key (attribute index) used for ordering
378  QString mSortKeyAttributeName;
379 
380  QString mPageNameExpression;
381 
382  // feature filtering
383  bool mFilterFeatures;
384  // feature expression filter
385  QString mFeatureFilter;
386 
387  // id of each iterated feature (after filtering and sorting) paired with atlas page name
388  QVector< QPair<QgsFeatureId, QString> > mFeatureIds;
389 
390  QgsFeature mCurrentFeature;
391 
392  std::unique_ptr<QgsExpression> mFilenameExpr;
393 
394  // bounding box of the current feature transformed into map crs
395  QgsRectangle mTransformedFeatureBounds;
396 
397  QString mFilenameParserError;
398  QString mFilterParserError;
399 
400  //forces all atlas enabled maps to redraw
401  void updateAtlasMaps();
402 
403  //computes the extent of the current feature, in the crs of the specified map
404  void computeExtent( QgsComposerMap *map );
405 
406  QgsExpressionContext createExpressionContext();
407 
408  //list of predefined scales
409  QVector<qreal> mPredefinedScales;
410 
411  // projected geometry cache
412  mutable QMap<long, QgsGeometry> mGeometryCache;
413 };
414 
415 #endif
416 
417 
418 
QString sortKeyAttributeName() const
QMap< QgsFeatureId, QVariant > SorterKeys
A rectangle specified with double values.
Definition: qgsrectangle.h:39
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:94
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
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)
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.
void setFileFormat(const QString &format)
Sets the atlas file format used for image exports.
bool enabled() const
Returns whether the atlas generation is enabled.
bool singleFile() const
Returns whether the atlas will be exported to a single file.