QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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"
23#include "qgslayoutitem.h"
24
25#include <QPdfWriter>
26#include <QPointer>
27#include <QSize>
28#include <QRectF>
29#include <QVector>
30#include <functional>
31
32#ifndef QT_NO_PRINTER
33#include <QPrinter>
34#endif
35
36class QgsLayout;
37class QPainter;
40class QgsFeedback;
42
49class CORE_EXPORT QgsLayoutExporter
50{
51
52 public:
53
56 {
58 QString directory;
59
61 QString baseName;
62
64 QString extension;
65
67 int page = 0;
68 };
69
74
75 virtual ~QgsLayoutExporter();
76
80 QgsLayout *layout() const;
81
90 void renderPage( QPainter *painter, int page ) const;
91
110 QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
111
119 void renderRegion( QPainter *painter, const QRectF &region ) const;
120
137 QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
138
139
142 {
150 };
151
154 {
157 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
158 {}
159
161 double dpi = -1;
162
176
181 bool cropToContents = false;
182
188
196 QList< int > pages;
197
202 bool generateWorldFile = false;
203
210 bool exportMetadata = true;
211
212
216 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
217
223 QVector<qreal> predefinedMapScales;
224
225 };
226
237 ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
238
239
250 static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
251 const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
252 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
253
254
257 {
260 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
261 {}
262
264 double dpi = -1;
265
271 bool rasterizeWholeImage = false;
272
280 bool forceVectorOutput = false;
281
287 bool appendGeoreference = true;
288
295 bool exportMetadata = true;
296
300 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
301
308 Qgis::TextRenderFormat textRenderFormat = Qgis::TextRenderFormat::AlwaysOutlines;
309
316 bool simplifyGeometries = true;
317
329 bool writeGeoPdf = false;
330
344 bool exportLayersAsSeperateFiles = false; // TODO QGIS 4 fix typo //#spellok
345
354 bool useIso32000ExtensionFormatGeoreferencing = true;
355
365 bool useOgcBestPracticeFormatGeoreferencing = false;
366
372 bool includeGeoPdfFeatures = true;
373
382 QStringList exportThemes;
383
389 QVector<qreal> predefinedMapScales;
390
391 };
392
399 ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
400
412 static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
414 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
415
428 static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
430 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
431
432
435 {
438 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
439 {}
440
442 double dpi = -1;
443
449 bool rasterizeWholeImage = false;
450
454 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
455
461 QVector<qreal> predefinedMapScales;
462
463 };
464
465#ifndef QT_NO_PRINTER
466
473 ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
474
482 static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
484 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
485#endif
486
489 {
492 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
493 {}
494
496 double dpi = -1;
497
505 bool forceVectorOutput = false;
506
511 bool cropToContents = false;
512
518
524 bool exportAsLayers = false;
525
534 bool exportLabelsToSeparateLayers = true;
535
542 bool exportMetadata = true;
543
547 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
548
555 Qgis::TextRenderFormat textRenderFormat = Qgis::TextRenderFormat::AlwaysOutlines;
556
563 bool simplifyGeometries = true;
564
570 QVector<qreal> predefinedMapScales;
571 };
572
579 ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
580
591 static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
593 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
594
595
600 QString errorFile() const { return mErrorFileName; }
601
609 QMap< QString, QgsLabelingResults * > labelingResults();
610
611#ifndef SIP_RUN
612
622 QMap< QString, QgsLabelingResults * > takeLabelingResults();
623#endif
624
640 bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
641 const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
642
649 void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
650
656 void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
657
666 static bool requiresRasterization( const QgsLayout *layout );
667
675 static bool containsAdvancedEffects( const QgsLayout *layout );
676
677 protected:
678
684 virtual QString generateFileName( const PageExportDetails &details ) const;
685
686 private:
687
688 QPointer< QgsLayout > mLayout;
689
690 void captureLabelingResults();
691 QMap< QString, QgsLabelingResults * > mLabelingResults;
692
693 mutable QString mErrorFileName;
694
695 QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
696
701 static int firstPageToBeExported( QgsLayout *layout );
702
706 static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
707
721 std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
722
724 void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
725
729 static void preparePrintAsPdf( QgsLayout *layout, QPagedPaintDevice *device, const QString &filePath );
730
731 static void preparePrint( QgsLayout *layout, QPagedPaintDevice *device, bool setFirstPageSize = false );
732
736 ExportResult print( QPagedPaintDevice *device );
737
746 ExportResult printPrivate( QPagedPaintDevice *device, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
747
748 static void updatePrinterPageSize( QgsLayout *layout, QPagedPaintDevice *device, int page );
749
750 ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
751 const QString &filename, unsigned int svgLayerId, const QString &layerName,
752 QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
753
754 void appendMetadataToSvg( QDomDocument &svg ) const;
755
756 bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
757 const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
758
759 ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items, const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc );
760
761 static QgsVectorSimplifyMethod createExportSimplifyMethod();
762 friend class TestQgsLayout;
763 friend class TestQgsLayoutExporter;
764
765};
766
767#endif //QGSLAYOUTEXPORTER_H
768
769
770
TextRenderFormat
Options for rendering text.
Definition: qgis.h:1885
An abstract base class for QgsLayout based classes which can be exported by QgsLayoutExporter.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
Class that stores computed placement from labeling engine.
Handles rendering and exports of layouts to various formats.
ExportResult
Result codes for exporting layouts.
@ Canceled
Export was canceled.
@ MemoryError
Unable to allocate memory required to export.
@ PrintError
Could not start printing to destination device.
@ IteratorError
Error iterating over layout.
@ FileError
Could not write to destination file, likely due to a lock held by another application.
@ Success
Export was successful.
@ SvgLayerError
Could not create layered SVG file.
QString errorFile() const
Returns the file name corresponding to the last error encountered during an export.
Layout graphical items for displaying a map.
Stores information relating to the current rendering settings for a layout.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:38
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:107
This class contains information how to simplify geometries fetched from a vector layer.
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
#define SIP_OUT
Definition: qgis_sip.h:58
Contains settings relating to exporting layouts to raster images.
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.
QSize imageSize
Manual size in pixels for output image.
ImageExportSettings()
Constructor for ImageExportSettings.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Contains details of a page being exported by the class.
QString baseName
Base part of filename (i.e. file name without extension or '.')
QString extension
File suffix/extension (without the leading '.')
Contains settings relating to exporting layouts to PDF.
QStringList exportThemes
Optional list of map themes to export as GeoPDF layer groups.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
PdfExportSettings()
Constructor for PdfExportSettings.
Contains settings relating to printing layouts.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
PrintExportSettings()
Constructor for PrintExportSettings.
Contains settings relating to exporting layouts to SVG.
SvgExportSettings()
Constructor for SvgExportSettings.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
QgsMargins cropMargins
Crop to content margins, in layout units.
Contains details of a particular export layer relating to a layout item.