QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
QgsVectorSimplifyMethod
This class contains information how to simplify geometries fetched from a vector layer.
Definition: qgsvectorsimplifymethod.h:29
Qgis::TextRenderFormat
TextRenderFormat
Options for rendering text.
Definition: qgis.h:1331
QgsLayoutExporter::PdfExportSettings::exportThemes
QStringList exportThemes
Optional list of map themes to export as GeoPDF layer groups.
Definition: qgslayoutexporter.h:379
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsLayoutExporter::PrintError
@ PrintError
Could not start printing to destination device.
Definition: qgslayoutexporter.h:144
QgsLayoutExporter::ExportResult
ExportResult
Result codes for exporting layouts.
Definition: qgslayoutexporter.h:138
QgsLabelingResults
Class that stores computed placement from labeling engine.
Definition: qgslabelingresults.h:32
QgsLayoutExporter::PageExportDetails
Contains details of a page being exported by the class.
Definition: qgslayoutexporter.h:52
QgsLayoutExporter::Success
@ Success
Export was successful.
Definition: qgslayoutexporter.h:140
QgsLayoutRenderContext
Stores information relating to the current rendering settings for a layout.
Definition: qgslayoutrendercontext.h:36
QgsLayoutExporter::ImageExportSettings::pages
QList< int > pages
List of specific pages to export, or an empty list to export all pages.
Definition: qgslayoutexporter.h:193
QgsLayoutExporter::Canceled
@ Canceled
Export was canceled.
Definition: qgslayoutexporter.h:141
QgsLayoutExporter::ImageExportSettings::cropMargins
QgsMargins cropMargins
Crop to content margins, in pixels.
Definition: qgslayoutexporter.h:184
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
QgsLayoutExporter::errorFile
QString errorFile() const
Returns the file name corresponding to the last error encountered during an export.
Definition: qgslayoutexporter.h:595
QgsLayoutExporter::ImageExportSettings::predefinedMapScales
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Definition: qgslayoutexporter.h:220
QgsLayoutExporter::MemoryError
@ MemoryError
Unable to allocate memory required to export.
Definition: qgslayoutexporter.h:142
QgsLayoutExporter::SvgExportSettings::predefinedMapScales
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Definition: qgslayoutexporter.h:565
QgsAbstractLayoutIterator
An abstract base class for QgsLayout based classes which can be exported by QgsLayoutExporter.
Definition: qgsabstractlayoutiterator.h:30
QgsLayoutExporter::SvgLayerError
@ SvgLayerError
Could not create layered SVG file.
Definition: qgslayoutexporter.h:145
QgsLayoutItem::ExportLayerDetail
Contains details of a particular export layer relating to a layout item.
Definition: qgslayoutitem.h:492
QgsLayoutExporter::PageExportDetails::baseName
QString baseName
Base part of filename (i.e. file name without extension or '.')
Definition: qgslayoutexporter.h:58
QgsLayoutExporter::SvgExportSettings::cropMargins
QgsMargins cropMargins
Crop to content margins, in layout units.
Definition: qgslayoutexporter.h:512
qgslayoutitem.h
QgsLayoutExporter::SvgExportSettings::SvgExportSettings
SvgExportSettings()
Constructor for SvgExportSettings.
Definition: qgslayoutexporter.h:486
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsLayoutExporter::PdfExportSettings::predefinedMapScales
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Definition: qgslayoutexporter.h:386
QgsLayoutExporter::PageExportDetails::extension
QString extension
File suffix/extension (without the leading '.')
Definition: qgslayoutexporter.h:61
QgsLayoutExporter::ImageExportSettings::imageSize
QSize imageSize
Manual size in pixels for output image.
Definition: qgslayoutexporter.h:172
QgsLayoutExporter::PrintExportSettings::PrintExportSettings
PrintExportSettings()
Constructor for PrintExportSettings.
Definition: qgslayoutexporter.h:434
QgsMargins
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:37
QgsLayoutExporter
Handles rendering and exports of layouts to various formats.
Definition: qgslayoutexporter.h:46
QgsLayoutExporter::PageExportDetails::directory
QString directory
Target folder.
Definition: qgslayoutexporter.h:55
QgsLayoutExporter::PdfExportSettings::PdfExportSettings
PdfExportSettings()
Constructor for PdfExportSettings.
Definition: qgslayoutexporter.h:256
QgsLayoutExporter::PrintExportSettings
Contains settings relating to printing layouts.
Definition: qgslayoutexporter.h:431
QgsLayoutExporter::ImageExportSettings::ImageExportSettings
ImageExportSettings()
Constructor for ImageExportSettings.
Definition: qgslayoutexporter.h:153
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:317
QgsLayoutExporter::ImageExportSettings
Contains settings relating to exporting layouts to raster images.
Definition: qgslayoutexporter.h:150
qgslayoutrendercontext.h
qgslayoutreportcontext.h
c
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
Definition: porting_processing.dox:1
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutExporter::IteratorError
@ IteratorError
Error iterating over layout.
Definition: qgslayoutexporter.h:146
QgsLayoutExporter::SvgExportSettings
Contains settings relating to exporting layouts to SVG.
Definition: qgslayoutexporter.h:483
QgsLayoutExporter::PdfExportSettings
Contains settings relating to exporting layouts to PDF.
Definition: qgslayoutexporter.h:253
QgsLayoutExporter::PrintExportSettings::predefinedMapScales
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Definition: qgslayoutexporter.h:458
QgsLayoutExporter::FileError
@ FileError
Could not write to destination file, likely due to a lock held by another application.
Definition: qgslayoutexporter.h:143
qgsmargins.h