QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 class QgsFeedback;
33 
40 class CORE_EXPORT QgsLayoutExporter
41 {
42 
43  public:
44 
47  {
49  QString directory;
50 
52  QString baseName;
53 
55  QString extension;
56 
58  int page = 0;
59  };
60 
64  QgsLayoutExporter( QgsLayout *layout );
65 
66  virtual ~QgsLayoutExporter() = default;
67 
71  QgsLayout *layout() const;
72 
81  void renderPage( QPainter *painter, int page ) const;
82 
101  QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
102 
110  void renderRegion( QPainter *painter, const QRectF &region ) const;
111 
128  QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
129 
130 
133  {
141  };
142 
145  {
148  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
149  {}
150 
152  double dpi = -1;
153 
166  QSize imageSize;
167 
172  bool cropToContents = false;
173 
179 
187  QList< int > pages;
188 
193  bool generateWorldFile = false;
194 
201  bool exportMetadata = true;
202 
203 
207  QgsLayoutRenderContext::Flags flags = nullptr;
208 
209  };
210 
221  ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
222 
223 
234  static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
235  const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
236  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
237 
238 
241  {
244  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
245  {}
246 
248  double dpi = -1;
249 
255  bool rasterizeWholeImage = false;
256 
264  bool forceVectorOutput = false;
265 
272  bool exportMetadata = true;
273 
277  QgsLayoutRenderContext::Flags flags = nullptr;
278 
286 
287  };
288 
295  ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
296 
308  static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
309  const QgsLayoutExporter::PdfExportSettings &settings,
310  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
311 
324  static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
325  const QgsLayoutExporter::PdfExportSettings &settings,
326  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
327 
328 
331  {
334  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
335  {}
336 
338  double dpi = -1;
339 
345  bool rasterizeWholeImage = false;
346 
350  QgsLayoutRenderContext::Flags flags = nullptr;
351 
352  };
353 
360  ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
361 
369  static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
371  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
372 
373 
376  {
379  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
380  {}
381 
383  double dpi = -1;
384 
392  bool forceVectorOutput = false;
393 
398  bool cropToContents = false;
399 
405 
411  bool exportAsLayers = false;
412 
419  bool exportMetadata = true;
420 
424  QgsLayoutRenderContext::Flags flags = nullptr;
425 
433 
434  };
435 
442  ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
443 
454  static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
455  const QgsLayoutExporter::SvgExportSettings &settings,
456  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
457 
458 
463  QString errorFile() const { return mErrorFileName; }
464 
480  bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
481  const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
482 
489  void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
490 
496  void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
497 
498  protected:
499 
505  virtual QString generateFileName( const PageExportDetails &details ) const;
506 
507  private:
508 
509  QPointer< QgsLayout > mLayout;
510 
511  mutable QString mErrorFileName;
512 
513  QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
514 
519  static int firstPageToBeExported( QgsLayout *layout );
520 
524  static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
525 
539  std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
540 
542  void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
543 
547  static void preparePrintAsPdf( QgsLayout *layout, QPrinter &printer, const QString &filePath );
548 
549  static void preparePrint( QgsLayout *layout, QPrinter &printer, bool setFirstPageSize = false );
550 
554  ExportResult print( QPrinter &printer );
555 
564  ExportResult printPrivate( QPrinter &printer, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
565 
566  static void updatePrinterPageSize( QgsLayout *layout, QPrinter &printer, int page );
567 
568  ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
569  const QString &filename, int svgLayerId, const QString &layerName,
570  QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
571 
572  void appendMetadataToSvg( QDomDocument &svg ) const;
573 
574  bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
575  const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
576 
577  friend class TestQgsLayout;
578 
579 };
580 
581 #endif //QGSLAYOUTEXPORTER_H
582 
583 
584 
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 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;)
Reads and writes project states.
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.
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.
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.