QGIS API Documentation  2.99.0-Master (c4aa4d0)
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 
36 #define SIP_NO_FILE
37 
49 class CORE_EXPORT QgsAtlasComposition : public QObject
50 {
51  Q_OBJECT
52  public:
53  QgsAtlasComposition( QgsComposition *composition );
54 
60  bool enabled() const { return mEnabled; }
61 
67  void setEnabled( bool enabled );
68 
74  bool hideCoverage() const { return mHideCoverage; }
75 
81  void setHideCoverage( bool hide );
82 
91  QString filenamePattern() const { return mFilenamePattern; }
92 
102  bool setFilenamePattern( const QString &pattern );
103 
110  QString filenamePatternErrorString() const { return mFilenameParserError; }
111 
117  QgsVectorLayer *coverageLayer() const { return mCoverageLayer.get(); }
118 
124  void setCoverageLayer( QgsVectorLayer *layer );
125 
133  QString pageNameExpression() const { return mPageNameExpression; }
134 
141  void setPageNameExpression( const QString &pageNameExpression ) { mPageNameExpression = pageNameExpression; }
142 
150  QString nameForPage( int pageNumber ) const;
151 
159  bool singleFile() const { return mSingleFile; }
160 
168  void setSingleFile( bool single ) { mSingleFile = single; }
169 
177  QString fileFormat() const { return mFileFormat; }
178 
186  void setFileFormat( const QString &format ) { mFileFormat = format; }
187 
188  bool sortFeatures() const { return mSortFeatures; }
189  void setSortFeatures( bool doSort ) { mSortFeatures = doSort; }
190 
191  bool sortAscending() const { return mSortAscending; }
192  void setSortAscending( bool ascending ) { mSortAscending = ascending; }
193 
194  bool filterFeatures() const { return mFilterFeatures; }
195  void setFilterFeatures( bool doFilter ) { mFilterFeatures = doFilter; }
196 
197  QString featureFilter() const { return mFeatureFilter; }
198  void setFeatureFilter( const QString &expression ) { mFeatureFilter = expression; }
199 
206  QString featureFilterErrorString() const { return mFilterParserError; }
207 
208  QString sortKeyAttributeName() const { return mSortKeyAttributeName; }
209  void setSortKeyAttributeName( const QString &fieldName ) { mSortKeyAttributeName = fieldName; }
210 
218  QVector<qreal> predefinedScales() const { return mPredefinedScales; }
219 
227  void setPredefinedScales( const QVector<qreal> &scales );
228 
232  bool beginRender();
234  void endRender();
235 
237  int numFeatures() const;
238 
245  bool prepareForFeature( const int i, const bool updateMaps = true );
246 
251  bool prepareForFeature( const QgsFeature *feat );
252 
254  QString currentFilename() const;
255 
256  void writeXml( QDomElement &elem, QDomDocument &doc ) const;
257 
265  void readXml( const QDomElement &elem, const QDomDocument &doc );
266 
267  QgsComposition *composition() { return mComposition; }
268 
273  int updateFeatures();
274 
279  QgsFeature feature() const { return mCurrentFeature; }
280 
285  QString currentPageName() const;
286 
291  int currentFeatureNumber() const { return mCurrentFeatureNo; }
292 
294  void prepareMap( QgsComposerMap *map );
295 
297  QgsGeometry currentGeometry( const QgsCoordinateReferenceSystem &projectedTo = QgsCoordinateReferenceSystem() ) const;
298 
299  public slots:
300 
305  void refreshFeature();
306 
307  void nextFeature();
308  void prevFeature();
309  void lastFeature();
310  void firstFeature();
311 
312  signals:
314  void parameterChanged();
315 
317  void toggled( bool );
318 
320  void statusMsgChanged( const QString &message );
321 
323  void coverageLayerChanged( QgsVectorLayer *layer );
324 
326  void renderBegun();
327 
329  void renderEnded();
330 
332  void featureChanged( QgsFeature *feature );
333 
338  void numberFeaturesChanged( int numFeatures );
339 
340  private:
341 
346  bool updateFilenameExpression();
347 
352  bool evalFeatureFilename( const QgsExpressionContext &context );
353 
354  QgsComposition *mComposition = nullptr;
355 
356  bool mEnabled;
357  bool mHideCoverage;
358  QString mFilenamePattern;
359  QgsVectorLayerRef mCoverageLayer;
360  bool mSingleFile;
361  QString mFileFormat;
362 
363  QString mCurrentFilename;
364  // feature ordering
365  bool mSortFeatures;
366  // sort direction
367  bool mSortAscending;
368 
369  // current atlas feature number
370  int mCurrentFeatureNo;
371 
372  public:
373  typedef QMap< QgsFeatureId, QVariant > SorterKeys;
374 
375  private slots:
376  void removeLayers( const QStringList &layers );
377 
378  private:
379  // value of field that is used for ordering of features
380  SorterKeys mFeatureKeys;
381  // key (attribute index) used for ordering
382  QString mSortKeyAttributeName;
383 
384  QString mPageNameExpression;
385 
386  // feature filtering
387  bool mFilterFeatures;
388  // feature expression filter
389  QString mFeatureFilter;
390 
391  // id of each iterated feature (after filtering and sorting) paired with atlas page name
392  QVector< QPair<QgsFeatureId, QString> > mFeatureIds;
393 
394  QgsFeature mCurrentFeature;
395 
396  std::unique_ptr<QgsExpression> mFilenameExpr;
397 
398  // bounding box of the current feature transformed into map crs
399  QgsRectangle mTransformedFeatureBounds;
400 
401  QString mFilenameParserError;
402  QString mFilterParserError;
403 
404  //forces all atlas enabled maps to redraw
405  void updateAtlasMaps();
406 
407  //computes the extent of the current feature, in the crs of the specified map
408  void computeExtent( QgsComposerMap *map );
409 
410  QgsExpressionContext createExpressionContext();
411 
412  //list of predefined scales
413  QVector<qreal> mPredefinedScales;
414 
415  // projected geometry cache
416  mutable QMap<long, QgsGeometry> mGeometryCache;
417 };
418 
419 #endif
420 
421 
422 
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:111
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.