QGIS API Documentation  3.6.0-Noosa (5873452)
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 <QPrinter>
20 #include "qgis_core.h"
21 #include "qgsmargins.h"
22 #include "qgslayoutrendercontext.h"
23 #include "qgslayoutreportcontext.h"
24 #include <QPointer>
25 #include <QSize>
26 #include <QRectF>
27 
28 #ifndef QT_NO_PRINTER
29 
30 class QgsLayout;
31 class QPainter;
32 class QgsLayoutItemMap;
34 class QgsFeedback;
35 
42 class CORE_EXPORT QgsLayoutExporter
43 {
44 
45  public:
46 
49  {
51  QString directory;
52 
54  QString baseName;
55 
57  QString extension;
58 
60  int page = 0;
61  };
62 
66  QgsLayoutExporter( QgsLayout *layout );
67 
68  virtual ~QgsLayoutExporter() = default;
69 
73  QgsLayout *layout() const;
74 
83  void renderPage( QPainter *painter, int page ) const;
84 
103  QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
104 
112  void renderRegion( QPainter *painter, const QRectF &region ) const;
113 
130  QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
131 
132 
135  {
143  };
144 
147  {
150  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
151  {}
152 
154  double dpi = -1;
155 
168  QSize imageSize;
169 
174  bool cropToContents = false;
175 
181 
189  QList< int > pages;
190 
195  bool generateWorldFile = false;
196 
203  bool exportMetadata = true;
204 
205 
209  QgsLayoutRenderContext::Flags flags = nullptr;
210 
211  };
212 
223  ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
224 
225 
236  static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
237  const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
238  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
239 
240 
243  {
246  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
247  {}
248 
250  double dpi = -1;
251 
257  bool rasterizeWholeImage = false;
258 
266  bool forceVectorOutput = false;
267 
274  bool exportMetadata = true;
275 
279  QgsLayoutRenderContext::Flags flags = nullptr;
280 
288 
289  };
290 
297  ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
298 
310  static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
311  const QgsLayoutExporter::PdfExportSettings &settings,
312  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
313 
326  static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
327  const QgsLayoutExporter::PdfExportSettings &settings,
328  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
329 
330 
333  {
336  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
337  {}
338 
340  double dpi = -1;
341 
347  bool rasterizeWholeImage = false;
348 
352  QgsLayoutRenderContext::Flags flags = nullptr;
353 
354  };
355 
362  ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
363 
371  static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
373  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
374 
375 
378  {
381  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
382  {}
383 
385  double dpi = -1;
386 
394  bool forceVectorOutput = false;
395 
400  bool cropToContents = false;
401 
407 
413  bool exportAsLayers = false;
414 
421  bool exportMetadata = true;
422 
426  QgsLayoutRenderContext::Flags flags = nullptr;
427 
435 
436  };
437 
444  ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
445 
456  static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
457  const QgsLayoutExporter::SvgExportSettings &settings,
458  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
459 
460 
465  QString errorFile() const { return mErrorFileName; }
466 
482  bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
483  const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
484 
491  void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
492 
498  void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
499 
500  protected:
501 
507  virtual QString generateFileName( const PageExportDetails &details ) const;
508 
509  private:
510 
511  QPointer< QgsLayout > mLayout;
512 
513  mutable QString mErrorFileName;
514 
515  QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
516 
521  static int firstPageToBeExported( QgsLayout *layout );
522 
526  static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
527 
541  std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
542 
544  void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
545 
549  static void preparePrintAsPdf( QgsLayout *layout, QPrinter &printer, const QString &filePath );
550 
551  static void preparePrint( QgsLayout *layout, QPrinter &printer, bool setFirstPageSize = false );
552 
556  ExportResult print( QPrinter &printer );
557 
566  ExportResult printPrivate( QPrinter &printer, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
567 
568  static void updatePrinterPageSize( QgsLayout *layout, QPrinter &printer, int page );
569 
570  ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
571  const QString &filename, int svgLayerId, const QString &layerName,
572  QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
573 
574  void appendMetadataToSvg( QDomDocument &svg ) const;
575 
576  bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
577  const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
578 
579  friend class TestQgsLayout;
580 
581 };
582 
583 #endif // ! QT_NO_PRINTER
584 
585 #endif //QGSLAYOUTEXPORTER_H
586 
587 
588 
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.
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.