QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
44 class CORE_EXPORT QgsLayoutExporter
45 {
46 
47  public:
48 
51  {
53  QString directory;
54 
56  QString baseName;
57 
59  QString extension;
60 
62  int page = 0;
63  };
64 
68  QgsLayoutExporter( QgsLayout *layout );
69 
70  virtual ~QgsLayoutExporter() = default;
71 
75  QgsLayout *layout() const;
76 
85  void renderPage( QPainter *painter, int page ) const;
86 
105  QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
106 
114  void renderRegion( QPainter *painter, const QRectF &region ) const;
115 
132  QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
133 
134 
137  {
145  };
146 
149  {
152  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
153  {}
154 
156  double dpi = -1;
157 
170  QSize imageSize;
171 
176  bool cropToContents = false;
177 
183 
191  QList< int > pages;
192 
197  bool generateWorldFile = false;
198 
205  bool exportMetadata = true;
206 
207 
211  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
212 
218  QVector<qreal> predefinedMapScales;
219 
220  };
221 
232  ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
233 
234 
245  static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
246  const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
247  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
248 
249 
252  {
255  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
256  {}
257 
259  double dpi = -1;
260 
266  bool rasterizeWholeImage = false;
267 
275  bool forceVectorOutput = false;
276 
282  bool appendGeoreference = true;
283 
290  bool exportMetadata = true;
291 
295  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
296 
304 
311  bool simplifyGeometries = true;
312 
324  bool writeGeoPdf = false;
325 
339  bool exportLayersAsSeperateFiles = false; // TODO QGIS 4 fix typo //#spellok
340 
349  bool useIso32000ExtensionFormatGeoreferencing = true;
350 
360  bool useOgcBestPracticeFormatGeoreferencing = false;
361 
367  bool includeGeoPdfFeatures = true;
368 
377  QStringList exportThemes;
378 
384  QVector<qreal> predefinedMapScales;
385 
386  };
387 
394  ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
395 
407  static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
408  const QgsLayoutExporter::PdfExportSettings &settings,
409  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
410 
423  static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
424  const QgsLayoutExporter::PdfExportSettings &settings,
425  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
426 
427 
430  {
433  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
434  {}
435 
437  double dpi = -1;
438 
444  bool rasterizeWholeImage = false;
445 
449  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
450 
456  QVector<qreal> predefinedMapScales;
457 
458  };
459 
466  ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
467 
475  static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
477  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
478 
479 
482  {
485  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
486  {}
487 
489  double dpi = -1;
490 
498  bool forceVectorOutput = false;
499 
504  bool cropToContents = false;
505 
511 
517  bool exportAsLayers = false;
518 
527  bool exportLabelsToSeparateLayers = true;
528 
535  bool exportMetadata = true;
536 
540  QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
541 
549 
556  bool simplifyGeometries = true;
557 
563  QVector<qreal> predefinedMapScales;
564  };
565 
572  ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
573 
584  static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
585  const QgsLayoutExporter::SvgExportSettings &settings,
586  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
587 
588 
593  QString errorFile() const { return mErrorFileName; }
594 
610  bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
611  const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
612 
619  void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
620 
626  void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
627 
628  protected:
629 
635  virtual QString generateFileName( const PageExportDetails &details ) const;
636 
637  private:
638 
639  QPointer< QgsLayout > mLayout;
640 
641  mutable QString mErrorFileName;
642 
643  QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
644 
649  static int firstPageToBeExported( QgsLayout *layout );
650 
654  static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
655 
669  std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
670 
672  void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
673 
677  static void preparePrintAsPdf( QgsLayout *layout, QPrinter &printer, const QString &filePath );
678 
679  static void preparePrint( QgsLayout *layout, QPrinter &printer, bool setFirstPageSize = false );
680 
684  ExportResult print( QPrinter &printer );
685 
694  ExportResult printPrivate( QPrinter &printer, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
695 
696  static void updatePrinterPageSize( QgsLayout *layout, QPrinter &printer, int page );
697 
698  ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
699  const QString &filename, unsigned int svgLayerId, const QString &layerName,
700  QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
701 
702  void appendMetadataToSvg( QDomDocument &svg ) const;
703 
704  bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
705  const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
706 
707  ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items, const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc );
708 
709  static QgsVectorSimplifyMethod createExportSimplifyMethod();
710  friend class TestQgsLayout;
711  friend class TestQgsLayoutExporter;
712 
713 };
714 
715 #endif // ! QT_NO_PRINTER
716 
717 #endif //QGSLAYOUTEXPORTER_H
718 
719 
720 
QgsVectorSimplifyMethod
This class contains information how to simplify geometries fetched from a vector layer.
Definition: qgsvectorsimplifymethod.h:30
QgsLayoutExporter::PdfExportSettings::exportThemes
QStringList exportThemes
Optional list of map themes to export as GeoPDF layer groups.
Definition: qgslayoutexporter.h:377
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsLayoutExporter::PrintError
@ PrintError
Could not start printing to destination device.
Definition: qgslayoutexporter.h:142
QgsLayoutExporter::ExportResult
ExportResult
Result codes for exporting layouts.
Definition: qgslayoutexporter.h:137
QgsLayoutExporter::PageExportDetails
Contains details of a page being exported by the class.
Definition: qgslayoutexporter.h:51
QgsRenderContext::TextRenderFormat
TextRenderFormat
Options for rendering text.
Definition: qgsrendercontext.h:96
QgsLayoutExporter::Success
@ Success
Export was successful.
Definition: qgslayoutexporter.h:138
QgsLayoutRenderContext
Stores information relating to the current rendering settings for a layout.
Definition: qgslayoutrendercontext.h:33
QgsLayoutExporter::ImageExportSettings::pages
QList< int > pages
List of specific pages to export, or an empty list to export all pages.
Definition: qgslayoutexporter.h:191
QgsLayoutExporter::Canceled
@ Canceled
Export was canceled.
Definition: qgslayoutexporter.h:139
QgsLayoutExporter::~QgsLayoutExporter
virtual ~QgsLayoutExporter()=default
QgsLayoutExporter::ImageExportSettings::cropMargins
QgsMargins cropMargins
Crop to content margins, in pixels.
Definition: qgslayoutexporter.h:182
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
QgsLayoutExporter::errorFile
QString errorFile() const
Returns the file name corresponding to the last error encountered during an export.
Definition: qgslayoutexporter.h:593
QgsLayoutExporter::ImageExportSettings::predefinedMapScales
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Definition: qgslayoutexporter.h:218
QgsLayoutExporter::MemoryError
@ MemoryError
Unable to allocate memory required to export.
Definition: qgslayoutexporter.h:140
QgsLayoutExporter::SvgExportSettings::predefinedMapScales
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Definition: qgslayoutexporter.h:563
QgsAbstractLayoutIterator
An abstract base class for QgsLayout based classes which can be exported by QgsLayoutExporter.
Definition: qgsabstractlayoutiterator.h:31
QgsLayoutExporter::SvgLayerError
@ SvgLayerError
Could not create layered SVG file.
Definition: qgslayoutexporter.h:143
QgsLayoutItem::ExportLayerDetail
Contains details of a particular export layer relating to a layout item.
Definition: qgslayoutitem.h:491
QgsLayoutExporter::PageExportDetails::baseName
QString baseName
Base part of filename (i.e. file name without extension or '.')
Definition: qgslayoutexporter.h:56
QgsLayoutExporter::SvgExportSettings::cropMargins
QgsMargins cropMargins
Crop to content margins, in layout units.
Definition: qgslayoutexporter.h:510
qgslayoutitem.h
QgsLayoutExporter::SvgExportSettings::SvgExportSettings
SvgExportSettings()
Constructor for SvgExportSettings.
Definition: qgslayoutexporter.h:484
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:384
QgsLayoutExporter::PageExportDetails::extension
QString extension
File suffix/extension (without the leading '.')
Definition: qgslayoutexporter.h:59
QgsLayoutExporter::ImageExportSettings::imageSize
QSize imageSize
Manual size in pixels for output image.
Definition: qgslayoutexporter.h:170
QgsLayoutExporter::PrintExportSettings::PrintExportSettings
PrintExportSettings()
Constructor for PrintExportSettings.
Definition: qgslayoutexporter.h:432
QgsMargins
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:38
QgsLayoutExporter
Handles rendering and exports of layouts to various formats.
Definition: qgslayoutexporter.h:45
QgsLayoutExporter::PageExportDetails::directory
QString directory
Target folder.
Definition: qgslayoutexporter.h:53
QgsLayoutExporter::PdfExportSettings::PdfExportSettings
PdfExportSettings()
Constructor for PdfExportSettings.
Definition: qgslayoutexporter.h:254
QgsLayoutExporter::PrintExportSettings
Contains settings relating to printing layouts.
Definition: qgslayoutexporter.h:430
QgsLayoutExporter::ImageExportSettings::ImageExportSettings
ImageExportSettings()
Constructor for ImageExportSettings.
Definition: qgslayoutexporter.h:151
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:318
QgsLayoutExporter::ImageExportSettings
Contains settings relating to exporting layouts to raster images.
Definition: qgslayoutexporter.h:149
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:144
QgsLayoutExporter::SvgExportSettings
Contains settings relating to exporting layouts to SVG.
Definition: qgslayoutexporter.h:482
QgsLayoutExporter::PdfExportSettings
Contains settings relating to exporting layouts to PDF.
Definition: qgslayoutexporter.h:252
QgsLayoutExporter::PrintExportSettings::predefinedMapScales
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Definition: qgslayoutexporter.h:456
QgsRenderContext::TextFormatAlwaysOutlines
@ TextFormatAlwaysOutlines
Always render text using path objects (AKA outlines/curves).
Definition: qgsrendercontext.h:116
QgsLayoutExporter::FileError
@ FileError
Could not write to destination file, likely due to a lock held by another application.
Definition: qgslayoutexporter.h:141
qgsmargins.h