QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgslayoutexporter.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutexporter.h
3  -------------------
4  begin : October 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail 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 QGSLAYOUTEXPORTER_H
17 #define QGSLAYOUTEXPORTER_H
18 
19 #include "qgis_core.h"
20 #include "qgsmargins.h"
21 #include "qgslayoutrendercontext.h"
22 #include "qgslayoutreportcontext.h"
23 #include "qgslayoutitem.h"
24 #include <QPointer>
25 #include <QSize>
26 #include <QRectF>
27 #include <functional>
28 
29 #ifndef QT_NO_PRINTER
30 #include <QPrinter>
31 
32 class QgsLayout;
33 class QPainter;
34 class QgsLayoutItemMap;
36 class QgsFeedback;
37 class QgsLabelingResults;
38 
45 class CORE_EXPORT QgsLayoutExporter
46 {
47 
48  public:
49 
52  {
54  QString directory;
55 
57  QString baseName;
58 
60  QString extension;
61 
63  int page = 0;
64  };
65 
69  QgsLayoutExporter( QgsLayout *layout );
70 
71  virtual ~QgsLayoutExporter();
72 
76  QgsLayout *layout() const;
77 
86  void renderPage( QPainter *painter, int page ) const;
87 
106  QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
107 
115  void renderRegion( QPainter *painter, const QRectF &region ) const;
116 
133  QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
134 
135 
138  {
146  };
147 
150  {
153  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
154  {}
155 
157  double dpi = -1;
158 
171  QSize imageSize;
172 
177  bool cropToContents = false;
178 
184 
192  QList< int > pages;
193 
198  bool generateWorldFile = false;
199 
206  bool exportMetadata = true;
207 
208 
212  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
213 
219  QVector<qreal> predefinedMapScales;
220 
221  };
222 
233  ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
234 
235 
246  static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
247  const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
248  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
249 
250 
253  {
256  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
257  {}
258 
260  double dpi = -1;
261 
267  bool rasterizeWholeImage = false;
268 
276  bool forceVectorOutput = false;
277 
283  bool appendGeoreference = true;
284 
291  bool exportMetadata = true;
292 
296  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
297 
305 
312  bool simplifyGeometries = true;
313 
325  bool writeGeoPdf = false;
326 
340  bool exportLayersAsSeperateFiles = false; // TODO QGIS 4 fix typo //#spellok
341 
350  bool useIso32000ExtensionFormatGeoreferencing = true;
351 
361  bool useOgcBestPracticeFormatGeoreferencing = false;
362 
368  bool includeGeoPdfFeatures = true;
369 
378  QStringList exportThemes;
379 
385  QVector<qreal> predefinedMapScales;
386 
387  };
388 
395  ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
396 
408  static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
409  const QgsLayoutExporter::PdfExportSettings &settings,
410  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
411 
424  static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
425  const QgsLayoutExporter::PdfExportSettings &settings,
426  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
427 
428 
431  {
434  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
435  {}
436 
438  double dpi = -1;
439 
445  bool rasterizeWholeImage = false;
446 
450  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
451 
457  QVector<qreal> predefinedMapScales;
458 
459  };
460 
467  ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
468 
476  static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
478  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
479 
480 
483  {
486  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
487  {}
488 
490  double dpi = -1;
491 
499  bool forceVectorOutput = false;
500 
505  bool cropToContents = false;
506 
512 
518  bool exportAsLayers = false;
519 
528  bool exportLabelsToSeparateLayers = true;
529 
536  bool exportMetadata = true;
537 
541  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
542 
550 
557  bool simplifyGeometries = true;
558 
564  QVector<qreal> predefinedMapScales;
565  };
566 
573  ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
574 
585  static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
586  const QgsLayoutExporter::SvgExportSettings &settings,
587  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
588 
589 
594  QString errorFile() const { return mErrorFileName; }
595 
603  QMap< QString, QgsLabelingResults * > labelingResults();
604 
605 #ifndef SIP_RUN
606 
616  QMap< QString, QgsLabelingResults * > takeLabelingResults();
617 #endif
618 
634  bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
635  const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
636 
643  void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
644 
650  void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
651 
660  static bool requiresRasterization( const QgsLayout *layout );
661 
669  static bool containsAdvancedEffects( const QgsLayout *layout );
670 
671  protected:
672 
678  virtual QString generateFileName( const PageExportDetails &details ) const;
679 
680  private:
681 
682  QPointer< QgsLayout > mLayout;
683 
684  void captureLabelingResults();
685  QMap< QString, QgsLabelingResults * > mLabelingResults;
686 
687  mutable QString mErrorFileName;
688 
689  QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
690 
695  static int firstPageToBeExported( QgsLayout *layout );
696 
700  static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
701 
715  std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
716 
718  void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
719 
723  static void preparePrintAsPdf( QgsLayout *layout, QPrinter &printer, const QString &filePath );
724 
725  static void preparePrint( QgsLayout *layout, QPrinter &printer, bool setFirstPageSize = false );
726 
730  ExportResult print( QPrinter &printer );
731 
740  ExportResult printPrivate( QPrinter &printer, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
741 
742  static void updatePrinterPageSize( QgsLayout *layout, QPrinter &printer, int page );
743 
744  ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
745  const QString &filename, unsigned int svgLayerId, const QString &layerName,
746  QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
747 
748  void appendMetadataToSvg( QDomDocument &svg ) const;
749 
750  bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
751  const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
752 
753  ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items, const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc );
754 
755  static QgsVectorSimplifyMethod createExportSimplifyMethod();
756  friend class TestQgsLayout;
757  friend class TestQgsLayoutExporter;
758 
759 };
760 
761 #endif // ! QT_NO_PRINTER
762 
763 #endif //QGSLAYOUTEXPORTER_H
764 
765 
766 
An abstract base class for QgsLayout based classes which can be exported by QgsLayoutExporter.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Class that stores computed placement from labeling engine.
Handles rendering and exports of layouts to various formats.
ExportResult
Result codes for exporting layouts.
@ Canceled
Export was canceled.
@ MemoryError
Unable to allocate memory required to export.
@ PrintError
Could not start printing to destination device.
@ IteratorError
Error iterating over layout.
@ FileError
Could not write to destination file, likely due to a lock held by another application.
@ Success
Export was successful.
@ SvgLayerError
Could not create layered SVG file.
QString errorFile() const
Returns the file name corresponding to the last error encountered during an export.
Layout graphical items for displaying a map.
Stores information relating to the current rendering settings for a layout.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:38
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:99
TextRenderFormat
Options for rendering text.
@ TextFormatAlwaysOutlines
Always render text using path objects (AKA outlines/curves).
This class contains information how to simplify geometries fetched from a vector layer.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define SIP_OUT
Definition: qgis_sip.h:58
Contains settings relating to exporting layouts to raster images.
QgsMargins cropMargins
Crop to content margins, in pixels.
QList< int > pages
List of specific pages to export, or an empty list to export all pages.
QSize imageSize
Manual size in pixels for output image.
ImageExportSettings()
Constructor for ImageExportSettings.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Contains details of a page being exported by the class.
QString baseName
Base part of filename (i.e. file name without extension or '.')
QString extension
File suffix/extension (without the leading '.')
Contains settings relating to exporting layouts to PDF.
QStringList exportThemes
Optional list of map themes to export as GeoPDF layer groups.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
PdfExportSettings()
Constructor for PdfExportSettings.
Contains settings relating to printing layouts.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
PrintExportSettings()
Constructor for PrintExportSettings.
Contains settings relating to exporting layouts to SVG.
SvgExportSettings()
Constructor for SvgExportSettings.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
QgsMargins cropMargins
Crop to content margins, in layout units.
Contains details of a particular export layer relating to a layout item.