QGIS API Documentation  3.0.2-Girona (307d082)
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 = 0 ) const;
101 
109  void renderRegion( QPainter *painter, const QRectF &region ) const;
110 
127  QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = 0 ) 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 
197  QgsLayoutRenderContext::Flags flags = 0;
198 
199  };
200 
211  ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
212 
213 
224  static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
225  const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
226  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
227 
228 
231  {
234  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
235  {}
236 
238  double dpi = -1;
239 
245  bool rasterizeWholeImage = false;
246 
254  bool forceVectorOutput = false;
255 
259  QgsLayoutRenderContext::Flags flags = 0;
260 
261  };
262 
269  ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
270 
282  static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
283  const QgsLayoutExporter::PdfExportSettings &settings,
284  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
285 
298  static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
299  const QgsLayoutExporter::PdfExportSettings &settings,
300  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
301 
302 
305  {
308  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
309  {}
310 
312  double dpi = -1;
313 
319  bool rasterizeWholeImage = false;
320 
324  QgsLayoutRenderContext::Flags flags = 0;
325 
326  };
327 
334  ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
335 
343  static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
345  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
346 
347 
350  {
353  : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
354  {}
355 
357  double dpi = -1;
358 
366  bool forceVectorOutput = false;
367 
372  bool cropToContents = false;
373 
379 
385  bool exportAsLayers = false;
386 
390  QgsLayoutRenderContext::Flags flags = 0;
391 
392  };
393 
400  ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
401 
412  static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
413  const QgsLayoutExporter::SvgExportSettings &settings,
414  QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
415 
416 
421  QString errorFile() const { return mErrorFileName; }
422 
438  bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
439  const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
440 
447  void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
448 
454  void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
455 
456  protected:
457 
463  virtual QString generateFileName( const PageExportDetails &details ) const;
464 
465  private:
466 
467  QPointer< QgsLayout > mLayout;
468 
469  mutable QString mErrorFileName;
470 
471  QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
472 
476  static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat );
477 
491  std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
492 
494  void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
495 
499  static void preparePrintAsPdf( QgsLayout *layout, QPrinter &printer, const QString &filePath );
500 
501  static void preparePrint( QgsLayout *layout, QPrinter &printer, bool setFirstPageSize = false );
502 
506  ExportResult print( QPrinter &printer );
507 
516  ExportResult printPrivate( QPrinter &printer, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
517 
518  static void updatePrinterPageSize( QgsLayout *layout, QPrinter &printer, int page );
519 
520  ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, QRectF bounds,
521  const QString &filename, int svgLayerId, const QString &layerName,
522  QDomDocument &svg, QDomNode &svgDocRoot ) const;
523 
524  friend class TestQgsLayout;
525 
526 };
527 
528 #endif //QGSLAYOUTEXPORTER_H
529 
530 
531 
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
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;)
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.