QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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;
43
50class CORE_EXPORT QgsLayoutExporter
51{
52
53 public:
54
56 static const QgsSettingsEntryBool *settingOpenAfterExportingImage SIP_SKIP;
57
59 static const QgsSettingsEntryBool *settingOpenAfterExportingPdf SIP_SKIP;
60
62 static const QgsSettingsEntryBool *settingOpenAfterExportingSvg SIP_SKIP;
63
66 {
68 QString directory;
69
71 QString baseName;
72
74 QString extension;
75
77 int page = 0;
78 };
79
84
85 virtual ~QgsLayoutExporter();
86
90 QgsLayout *layout() const;
91
100 void renderPage( QPainter *painter, int page ) const;
101
120 QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
121
129 void renderRegion( QPainter *painter, const QRectF &region ) const;
130
147 QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
148
149
161
164 {
167 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
168 {}
169
171 double dpi = -1;
172
186
191 bool cropToContents = false;
192
198
206 QList< int > pages;
207
212 bool generateWorldFile = false;
213
220 bool exportMetadata = true;
221
222
226 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
227
233 QVector<qreal> predefinedMapScales;
234
235 };
236
247 ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
248
249
260 static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
261 const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
262 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
263
264
267 {
270 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
271 {}
272
274 double dpi = -1;
275
281 bool rasterizeWholeImage = false;
282
290 bool forceVectorOutput = false;
291
297 bool appendGeoreference = true;
298
305 bool exportMetadata = true;
306
310 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
311
319
326 bool simplifyGeometries = true;
327
339 bool writeGeoPdf = false;
340
354 bool exportLayersAsSeperateFiles = false; // TODO QGIS 4 fix typo //#spellok
355
364 bool useIso32000ExtensionFormatGeoreferencing = true;
365
375 bool useOgcBestPracticeFormatGeoreferencing = false;
376
382 bool includeGeoPdfFeatures = true;
383
392 QStringList exportThemes;
393
399 QVector<qreal> predefinedMapScales;
400 };
401
408 ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
409
421 static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
423 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
424
437 static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
439 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
440
441
444 {
447 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
448 {}
449
451 double dpi = -1;
452
458 bool rasterizeWholeImage = false;
459
463 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
464
470 QVector<qreal> predefinedMapScales;
471
472 };
473
474#ifndef QT_NO_PRINTER
475
482 ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
483
491 static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
493 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
494#endif
495
498 {
501 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
502 {}
503
505 double dpi = -1;
506
514 bool forceVectorOutput = false;
515
520 bool cropToContents = false;
521
527
533 bool exportAsLayers = false;
534
543 bool exportLabelsToSeparateLayers = true;
544
551 bool exportMetadata = true;
552
556 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
557
565
572 bool simplifyGeometries = true;
573
579 QVector<qreal> predefinedMapScales;
580 };
581
588 ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
589
600 static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
602 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
603
604
609 QString errorFile() const { return mErrorFileName; }
610
618 QMap< QString, QgsLabelingResults * > labelingResults();
619
620#ifndef SIP_RUN
621
631 QMap< QString, QgsLabelingResults * > takeLabelingResults();
632#endif
633
649 bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
650 const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
651
658 void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
659
665 void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
666
675 static bool requiresRasterization( const QgsLayout *layout );
676
684 static bool containsAdvancedEffects( const QgsLayout *layout );
685
686 protected:
687
693 virtual QString generateFileName( const PageExportDetails &details ) const;
694
695 private:
696
697 QPointer< QgsLayout > mLayout;
698
699 void captureLabelingResults();
700 QMap< QString, QgsLabelingResults * > mLabelingResults;
701
702 mutable QString mErrorFileName;
703
704 QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
705
710 static int firstPageToBeExported( QgsLayout *layout );
711
715 static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
716
730 std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
731
733 void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
734
738 static void preparePrintAsPdf( QgsLayout *layout, QPagedPaintDevice *device, const QString &filePath );
739
740 static void preparePrint( QgsLayout *layout, QPagedPaintDevice *device, bool setFirstPageSize = false );
741
745 ExportResult print( QPagedPaintDevice *device );
746
755 ExportResult printPrivate( QPagedPaintDevice *device, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
756
757 static void updatePrinterPageSize( QgsLayout *layout, QPagedPaintDevice *device, int page );
758
759 ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
760 const QString &filename, unsigned int svgLayerId, const QString &layerName,
761 QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
762
763 void appendMetadataToSvg( QDomDocument &svg ) const;
764
765 bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
766 const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
767
768 ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items, const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc );
769
770 static QgsVectorSimplifyMethod createExportSimplifyMethod();
771 friend class TestQgsLayout;
772 friend class TestQgsLayoutExporter;
773
774};
775
776#endif //QGSLAYOUTEXPORTER_H
777
778
779
TextRenderFormat
Flags which control how map layer renderers behave.
Definition qgis.h:2055
@ AlwaysOutlines
Always render text using path objects (AKA outlines/curves). This setting guarantees the best quality...
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
A boolean settings entry.
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_SKIP
Definition qgis_sip.h:126
#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.