QGIS API Documentation  3.2.0-Bonn (bc43194)
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 <QPointer>
24 #include <QSize>
25 #include <QRectF>
26 #include <QPrinter>
27 
28 class QgsLayout;
29 class QPainter;
30 class QgsLayoutItemMap;
32 
39 class CORE_EXPORT QgsLayoutExporter
40 {
41 
42  public:
43 
46  {
48  QString directory;
49 
51  QString baseName;
52 
54  QString extension;
55 
57  int page = 0;
58  };
59 
63  QgsLayoutExporter( QgsLayout *layout );
64 
65  virtual ~QgsLayoutExporter() = default;
66 
70  QgsLayout *layout() const;
71 
80  void renderPage( QPainter *painter, int page ) const;
81 
100  QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
101 
109  void renderRegion( QPainter *painter, const QRectF &region ) const;
110 
127  QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
128 
129 
132  {
140  };
141 
144  {
147  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
148  {}
149 
151  double dpi = -1;
152 
165  QSize imageSize;
166 
171  bool cropToContents = false;
172 
178 
186  QList< int > pages;
187 
192  bool generateWorldFile = false;
193 
200  bool exportMetadata = true;
201 
202 
206  QgsLayoutRenderContext::Flags flags = nullptr;
207 
208  };
209 
220  ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
221 
222 
233  static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
234  const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
235  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
236 
237 
240  {
243  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
244  {}
245 
247  double dpi = -1;
248 
254  bool rasterizeWholeImage = false;
255 
263  bool forceVectorOutput = false;
264 
271  bool exportMetadata = true;
272 
276  QgsLayoutRenderContext::Flags flags = nullptr;
277 
278  };
279 
286  ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
287 
299  static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
300  const QgsLayoutExporter::PdfExportSettings &settings,
301  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
302 
315  static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
316  const QgsLayoutExporter::PdfExportSettings &settings,
317  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
318 
319 
322  {
325  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
326  {}
327 
329  double dpi = -1;
330 
336  bool rasterizeWholeImage = false;
337 
341  QgsLayoutRenderContext::Flags flags = nullptr;
342 
343  };
344 
351  ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
352 
360  static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
362  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
363 
364 
367  {
370  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
371  {}
372 
374  double dpi = -1;
375 
383  bool forceVectorOutput = false;
384 
389  bool cropToContents = false;
390 
396 
402  bool exportAsLayers = false;
403 
410  bool exportMetadata = true;
411 
415  QgsLayoutRenderContext::Flags flags = nullptr;
416 
417  };
418 
425  ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
426 
437  static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
438  const QgsLayoutExporter::SvgExportSettings &settings,
439  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
440 
441 
446  QString errorFile() const { return mErrorFileName; }
447 
463  bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
464  const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
465 
472  void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
473 
479  void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
480 
481  protected:
482 
488  virtual QString generateFileName( const PageExportDetails &details ) const;
489 
490  private:
491 
492  QPointer< QgsLayout > mLayout;
493 
494  mutable QString mErrorFileName;
495 
496  QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
497 
501  static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
502 
516  std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
517 
519  void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
520 
524  static void preparePrintAsPdf( QgsLayout *layout, QPrinter &printer, const QString &filePath );
525 
526  static void preparePrint( QgsLayout *layout, QPrinter &printer, bool setFirstPageSize = false );
527 
531  ExportResult print( QPrinter &printer );
532 
541  ExportResult printPrivate( QPrinter &printer, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
542 
543  static void updatePrinterPageSize( QgsLayout *layout, QPrinter &printer, int page );
544 
545  ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, QRectF bounds,
546  const QString &filename, int svgLayerId, const QString &layerName,
547  QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
548 
549  void appendMetadataToSvg( QDomDocument &svg ) const;
550 
551  bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
552  const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
553 
554  friend class TestQgsLayout;
555 
556 };
557 
558 #endif //QGSLAYOUTEXPORTER_H
559 
560 
561 
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.
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 cancelation 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.
QString extension
File suffix/extension (without the leading &#39;.&#39;)
Reads and writes project states.
Definition: qgsproject.h:85
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.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
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.
Contains settings relating to exporting layouts to raster images.
#define SIP_OUT
Definition: qgis_sip.h:51
Stores information relating to the current rendering settings for a layout.
Could not start printing to destination device.
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.