QGIS API Documentation  3.13.0-Master (b0e71a822e)
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 = nullptr;
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 = nullptr;
296 
304 
311  bool simplifyGeometries = true;
312 
324  bool writeGeoPdf = false;
325 
339  bool exportLayersAsSeperateFiles = false;
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 = nullptr;
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 = nullptr;
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 
Contains settings relating to printing layouts.
QgsMargins cropMargins
Crop to content margins, in layout units.
Unable to allocate memory required to export.
Could not write to destination file, likely due to a lock held by another application.
QString errorFile() const
Returns the file name corresponding to the last error encountered during an export.
QgsMargins cropMargins
Crop to content margins, in pixels.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
QList< int > pages
List of specific pages to export, or an empty list to export all pages.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:43
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
Contains details of a page being exported by the class.
QSize imageSize
Manual size in pixels for output image.
Layout graphical items for displaying a map.
An abstract base class for QgsLayout based classes which can be exported by QgsLayoutExporter.
Always render text using path objects (AKA outlines/curves).
QString extension
File suffix/extension (without the leading &#39;.&#39;)
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:92
QString baseName
Base part of filename (i.e. file name without extension or &#39;.&#39;)
Could not create layered SVG file.
ImageExportSettings()
Constructor for ImageExportSettings.
QStringList exportThemes
Optional list of map themes to export as GeoPDF layer groups.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
TextRenderFormat
Options for rendering text.
PrintExportSettings()
Constructor for PrintExportSettings.
PdfExportSettings()
Constructor for PdfExportSettings.
SvgExportSettings()
Constructor for SvgExportSettings.
Handles rendering and exports of layouts to various formats.
Contains settings relating to exporting layouts to PDF.
This class contains information how to simplify geometries fetched from a vector layer.
Contains settings relating to exporting layouts to raster images.
#define SIP_OUT
Definition: qgis_sip.h:58
Stores information relating to the current rendering settings for a layout.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Could not start printing to destination device.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Contains details of a particular export layer relating to a layout item.
Contains settings relating to exporting layouts to SVG.
Export was successful.
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:37
ExportResult
Result codes for exporting layouts.
Error iterating over layout.