QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
334  bool useIso32000ExtensionFormatGeoreferencing = true;
335 
345  bool useOgcBestPracticeFormatGeoreferencing = false;
346 
352  bool includeGeoPdfFeatures = true;
353 
362  QStringList exportThemes;
363 
369  QVector<qreal> predefinedMapScales;
370 
371  };
372 
379  ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
380 
392  static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
393  const QgsLayoutExporter::PdfExportSettings &settings,
394  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
395 
408  static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
409  const QgsLayoutExporter::PdfExportSettings &settings,
410  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
411 
412 
415  {
418  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
419  {}
420 
422  double dpi = -1;
423 
429  bool rasterizeWholeImage = false;
430 
434  QgsLayoutRenderContext::Flags flags = nullptr;
435 
441  QVector<qreal> predefinedMapScales;
442 
443  };
444 
451  ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
452 
460  static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
462  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
463 
464 
467  {
470  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
471  {}
472 
474  double dpi = -1;
475 
483  bool forceVectorOutput = false;
484 
489  bool cropToContents = false;
490 
496 
502  bool exportAsLayers = false;
503 
512  bool exportLabelsToSeparateLayers = true;
513 
520  bool exportMetadata = true;
521 
525  QgsLayoutRenderContext::Flags flags = nullptr;
526 
534 
541  bool simplifyGeometries = true;
542 
548  QVector<qreal> predefinedMapScales;
549  };
550 
557  ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
558 
569  static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
570  const QgsLayoutExporter::SvgExportSettings &settings,
571  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
572 
573 
578  QString errorFile() const { return mErrorFileName; }
579 
595  bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
596  const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
597 
604  void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
605 
611  void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
612 
613  protected:
614 
620  virtual QString generateFileName( const PageExportDetails &details ) const;
621 
622  private:
623 
624  QPointer< QgsLayout > mLayout;
625 
626  mutable QString mErrorFileName;
627 
628  QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
629 
634  static int firstPageToBeExported( QgsLayout *layout );
635 
639  static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
640 
654  std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
655 
657  void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
658 
662  static void preparePrintAsPdf( QgsLayout *layout, QPrinter &printer, const QString &filePath );
663 
664  static void preparePrint( QgsLayout *layout, QPrinter &printer, bool setFirstPageSize = false );
665 
669  ExportResult print( QPrinter &printer );
670 
679  ExportResult printPrivate( QPrinter &printer, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
680 
681  static void updatePrinterPageSize( QgsLayout *layout, QPrinter &printer, int page );
682 
683  ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
684  const QString &filename, unsigned int svgLayerId, const QString &layerName,
685  QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
686 
687  void appendMetadataToSvg( QDomDocument &svg ) const;
688 
689  bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
690  const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
691 
692  ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items, const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc );
693 
694  static QgsVectorSimplifyMethod createExportSimplifyMethod();
695  friend class TestQgsLayout;
696  friend class TestQgsLayoutExporter;
697 
698 };
699 
700 #endif // ! QT_NO_PRINTER
701 
702 #endif //QGSLAYOUTEXPORTER_H
703 
704 
705 
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:44
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:89
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.