QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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#include <QPointer>
25#include <QSize>
26#include <QRectF>
27#include <QVector>
28#include <functional>
29
30#ifndef QT_NO_PRINTER
31#include <QPrinter>
32#endif
33
34class QgsLayout;
35class QPainter;
38class QgsFeedback;
40
47class CORE_EXPORT QgsLayoutExporter
48{
49
50 public:
51
54 {
56 QString directory;
57
59 QString baseName;
60
62 QString extension;
63
65 int page = 0;
66 };
67
72
73 virtual ~QgsLayoutExporter();
74
78 QgsLayout *layout() const;
79
88 void renderPage( QPainter *painter, int page ) const;
89
108 QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
109
117 void renderRegion( QPainter *painter, const QRectF &region ) const;
118
135 QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
136
137
140 {
148 };
149
152 {
155 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
156 {}
157
159 double dpi = -1;
160
174
179 bool cropToContents = false;
180
186
194 QList< int > pages;
195
200 bool generateWorldFile = false;
201
208 bool exportMetadata = true;
209
210
214 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
215
221 QVector<qreal> predefinedMapScales;
222
223 };
224
235 ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
236
237
248 static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
249 const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
250 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
251
252
255 {
258 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
259 {}
260
262 double dpi = -1;
263
269 bool rasterizeWholeImage = false;
270
278 bool forceVectorOutput = false;
279
285 bool appendGeoreference = true;
286
293 bool exportMetadata = true;
294
298 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
299
306 Qgis::TextRenderFormat textRenderFormat = Qgis::TextRenderFormat::AlwaysOutlines;
307
314 bool simplifyGeometries = true;
315
327 bool writeGeoPdf = false;
328
342 bool exportLayersAsSeperateFiles = false; // TODO QGIS 4 fix typo //#spellok
343
352 bool useIso32000ExtensionFormatGeoreferencing = true;
353
363 bool useOgcBestPracticeFormatGeoreferencing = false;
364
370 bool includeGeoPdfFeatures = true;
371
380 QStringList exportThemes;
381
387 QVector<qreal> predefinedMapScales;
388
389 };
390
397 ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
398
410 static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
412 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
413
426 static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
428 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
429
430
433 {
436 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
437 {}
438
440 double dpi = -1;
441
447 bool rasterizeWholeImage = false;
448
452 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
453
459 QVector<qreal> predefinedMapScales;
460
461 };
462
463#ifndef QT_NO_PRINTER
464
471 ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
472
480 static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
482 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
483#endif
484
487 {
490 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
491 {}
492
494 double dpi = -1;
495
503 bool forceVectorOutput = false;
504
509 bool cropToContents = false;
510
516
522 bool exportAsLayers = false;
523
532 bool exportLabelsToSeparateLayers = true;
533
540 bool exportMetadata = true;
541
545 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
546
553 Qgis::TextRenderFormat textRenderFormat = Qgis::TextRenderFormat::AlwaysOutlines;
554
561 bool simplifyGeometries = true;
562
568 QVector<qreal> predefinedMapScales;
569 };
570
577 ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
578
589 static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
591 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
592
593
598 QString errorFile() const { return mErrorFileName; }
599
607 QMap< QString, QgsLabelingResults * > labelingResults();
608
609#ifndef SIP_RUN
610
620 QMap< QString, QgsLabelingResults * > takeLabelingResults();
621#endif
622
638 bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
639 const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
640
647 void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
648
654 void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
655
664 static bool requiresRasterization( const QgsLayout *layout );
665
673 static bool containsAdvancedEffects( const QgsLayout *layout );
674
675 protected:
676
682 virtual QString generateFileName( const PageExportDetails &details ) const;
683
684 private:
685
686 QPointer< QgsLayout > mLayout;
687
688 void captureLabelingResults();
689 QMap< QString, QgsLabelingResults * > mLabelingResults;
690
691 mutable QString mErrorFileName;
692
693 QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
694
699 static int firstPageToBeExported( QgsLayout *layout );
700
704 static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
705
719 std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
720
722 void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
723
724#ifndef QT_NO_PRINTER
725
729 static void preparePrintAsPdf( QgsLayout *layout, QPrinter &printer, const QString &filePath );
730
731 static void preparePrint( QgsLayout *layout, QPrinter &printer, bool setFirstPageSize = false );
732
736 ExportResult print( QPrinter &printer );
737
746 ExportResult printPrivate( QPrinter &printer, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
747
748 static void updatePrinterPageSize( QgsLayout *layout, QPrinter &printer, int page );
749#endif
750
751 ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
752 const QString &filename, unsigned int svgLayerId, const QString &layerName,
753 QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
754
755 void appendMetadataToSvg( QDomDocument &svg ) const;
756
757 bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
758 const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
759
760 ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items, const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc );
761
762 static QgsVectorSimplifyMethod createExportSimplifyMethod();
763 friend class TestQgsLayout;
764 friend class TestQgsLayoutExporter;
765
766};
767
768#endif //QGSLAYOUTEXPORTER_H
769
770
771
TextRenderFormat
Options for rendering text.
Definition: qgis.h:1727
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:105
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.