QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 <QVector>
28 #include <functional>
29 
30 #ifndef QT_NO_PRINTER
31 #include <QPrinter>
32 
33 class QgsLayout;
34 class QPainter;
35 class QgsLayoutItemMap;
37 class QgsFeedback;
38 class QgsLabelingResults;
39 
46 class CORE_EXPORT QgsLayoutExporter
47 {
48 
49  public:
50 
53  {
55  QString directory;
56 
58  QString baseName;
59 
61  QString extension;
62 
64  int page = 0;
65  };
66 
70  QgsLayoutExporter( QgsLayout *layout );
71 
72  virtual ~QgsLayoutExporter();
73 
77  QgsLayout *layout() const;
78 
87  void renderPage( QPainter *painter, int page ) const;
88 
107  QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
108 
116  void renderRegion( QPainter *painter, const QRectF &region ) const;
117 
134  QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
135 
136 
139  {
147  };
148 
151  {
154  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
155  {}
156 
158  double dpi = -1;
159 
172  QSize imageSize;
173 
178  bool cropToContents = false;
179 
185 
193  QList< int > pages;
194 
199  bool generateWorldFile = false;
200 
207  bool exportMetadata = true;
208 
209 
213  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
214 
220  QVector<qreal> predefinedMapScales;
221 
222  };
223 
234  ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
235 
236 
247  static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
248  const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
249  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
250 
251 
254  {
257  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
258  {}
259 
261  double dpi = -1;
262 
268  bool rasterizeWholeImage = false;
269 
277  bool forceVectorOutput = false;
278 
284  bool appendGeoreference = true;
285 
292  bool exportMetadata = true;
293 
297  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
298 
305  Qgis::TextRenderFormat textRenderFormat = Qgis::TextRenderFormat::AlwaysOutlines;
306 
313  bool simplifyGeometries = true;
314 
326  bool writeGeoPdf = false;
327 
341  bool exportLayersAsSeperateFiles = false; // TODO QGIS 4 fix typo //#spellok
342 
351  bool useIso32000ExtensionFormatGeoreferencing = true;
352 
362  bool useOgcBestPracticeFormatGeoreferencing = false;
363 
369  bool includeGeoPdfFeatures = true;
370 
379  QStringList exportThemes;
380 
386  QVector<qreal> predefinedMapScales;
387 
388  };
389 
396  ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
397 
409  static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
410  const QgsLayoutExporter::PdfExportSettings &settings,
411  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
412 
425  static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
426  const QgsLayoutExporter::PdfExportSettings &settings,
427  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
428 
429 
432  {
435  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
436  {}
437 
439  double dpi = -1;
440 
446  bool rasterizeWholeImage = false;
447 
451  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
452 
458  QVector<qreal> predefinedMapScales;
459 
460  };
461 
468  ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
469 
477  static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
479  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
480 
481 
484  {
487  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
488  {}
489 
491  double dpi = -1;
492 
500  bool forceVectorOutput = false;
501 
506  bool cropToContents = false;
507 
513 
519  bool exportAsLayers = false;
520 
529  bool exportLabelsToSeparateLayers = true;
530 
537  bool exportMetadata = true;
538 
542  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
543 
550  Qgis::TextRenderFormat textRenderFormat = Qgis::TextRenderFormat::AlwaysOutlines;
551 
558  bool simplifyGeometries = true;
559 
565  QVector<qreal> predefinedMapScales;
566  };
567 
574  ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
575 
586  static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
587  const QgsLayoutExporter::SvgExportSettings &settings,
588  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
589 
590 
595  QString errorFile() const { return mErrorFileName; }
596 
604  QMap< QString, QgsLabelingResults * > labelingResults();
605 
606 #ifndef SIP_RUN
607 
617  QMap< QString, QgsLabelingResults * > takeLabelingResults();
618 #endif
619 
635  bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
636  const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
637 
644  void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
645 
651  void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
652 
661  static bool requiresRasterization( const QgsLayout *layout );
662 
670  static bool containsAdvancedEffects( const QgsLayout *layout );
671 
672  protected:
673 
679  virtual QString generateFileName( const PageExportDetails &details ) const;
680 
681  private:
682 
683  QPointer< QgsLayout > mLayout;
684 
685  void captureLabelingResults();
686  QMap< QString, QgsLabelingResults * > mLabelingResults;
687 
688  mutable QString mErrorFileName;
689 
690  QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
691 
696  static int firstPageToBeExported( QgsLayout *layout );
697 
701  static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
702 
716  std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
717 
719  void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
720 
724  static void preparePrintAsPdf( QgsLayout *layout, QPrinter &printer, const QString &filePath );
725 
726  static void preparePrint( QgsLayout *layout, QPrinter &printer, bool setFirstPageSize = false );
727 
731  ExportResult print( QPrinter &printer );
732 
741  ExportResult printPrivate( QPrinter &printer, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
742 
743  static void updatePrinterPageSize( QgsLayout *layout, QPrinter &printer, int page );
744 
745  ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
746  const QString &filename, unsigned int svgLayerId, const QString &layerName,
747  QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
748 
749  void appendMetadataToSvg( QDomDocument &svg ) const;
750 
751  bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
752  const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
753 
754  ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items, const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc );
755 
756  static QgsVectorSimplifyMethod createExportSimplifyMethod();
757  friend class TestQgsLayout;
758  friend class TestQgsLayoutExporter;
759 
760 };
761 
762 #endif // ! QT_NO_PRINTER
763 
764 #endif //QGSLAYOUTEXPORTER_H
765 
766 
767 
TextRenderFormat
Options for rendering text.
Definition: qgis.h:1003
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:101
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.