QGIS API Documentation 3.39.0-Master (3aed037ce22)
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 "qgsconfig.h"
21#include "qgsmargins.h"
24#include "qgslayoutitem.h"
25
26#include <QPdfWriter>
27#include <QPointer>
28#include <QSize>
29#include <QRectF>
30#include <QVector>
31#include <functional>
32
33#if defined( HAVE_QTPRINTER )
34#include <QPrinter>
35#endif
36
37class QgsLayout;
38class QPainter;
41class QgsFeedback;
44
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 {
166 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
167 {}
168
170 double dpi = -1;
171
185
190 bool cropToContents = false;
191
197
205 QList< int > pages;
206
211 bool generateWorldFile = false;
212
219 bool exportMetadata = true;
220
221
226
232 QVector<qreal> predefinedMapScales;
233
234 };
235
246 ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
247
248
259 static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
260 const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
261 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
262
263
266 {
268 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
269 {}
270
272 double dpi = -1;
273
279 bool rasterizeWholeImage = false;
280
288 bool forceVectorOutput = false;
289
295 bool appendGeoreference = true;
296
303 bool exportMetadata = true;
304
309
317
324 bool simplifyGeometries = true;
325
337 bool writeGeoPdf = false;
338
352 bool exportLayersAsSeperateFiles = false; // TODO QGIS 4 fix typo //#spellok
353
362 bool useIso32000ExtensionFormatGeoreferencing = true;
363
373 bool useOgcBestPracticeFormatGeoreferencing = false;
374
380 bool includeGeoPdfFeatures = true;
381
390 QStringList exportThemes;
391
397 QVector<qreal> predefinedMapScales;
398 };
399
406 ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
407
419 static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
421 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
422
435 static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
437 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
438
439
442 {
444 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
445 {}
446
448 double dpi = -1;
449
455 bool rasterizeWholeImage = false;
456
461
467 QVector<qreal> predefinedMapScales;
468
469 };
470
471#if defined( HAVE_QTPRINTER )
472 SIP_IF_FEATURE( HAVE_QTPRINTER )
473
474
480 ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
481
489 static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
491 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
492
493 SIP_END
494#endif
495
498 {
500 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
501 {}
502
504 double dpi = -1;
505
513 bool forceVectorOutput = false;
514
519 bool cropToContents = false;
520
526
532 bool exportAsLayers = false;
533
542 bool exportLabelsToSeparateLayers = true;
543
550 bool exportMetadata = true;
551
556
564
571 bool simplifyGeometries = true;
572
578 QVector<qreal> predefinedMapScales;
579 };
580
587 ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
588
599 static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
601 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
602
603
608 QString errorFile() const { return mErrorFileName; }
609
615 QString errorMessage() const { return mErrorMessage; }
616
624 QMap< QString, QgsLabelingResults * > labelingResults();
625
626#ifndef SIP_RUN
627
637 QMap< QString, QgsLabelingResults * > takeLabelingResults();
638#endif
639
655 bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
656 const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
657
664 void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
665
671 void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
672
681 static bool requiresRasterization( const QgsLayout *layout );
682
690 static bool containsAdvancedEffects( const QgsLayout *layout );
691
692 protected:
693
699 virtual QString generateFileName( const PageExportDetails &details ) const;
700
701 private:
702
703 QPointer< QgsLayout > mLayout;
704
705 void captureLabelingResults();
706 QMap< QString, QgsLabelingResults * > mLabelingResults;
707
708 mutable QString mErrorFileName;
709 mutable QString mErrorMessage;
710
711 QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
712
717 static int firstPageToBeExported( QgsLayout *layout );
718
722 static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
723
737 std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
738
740 void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
741
745 static void preparePrintAsPdf( QgsLayout *layout, QPagedPaintDevice *device, const QString &filePath );
746
747 static void preparePrint( QgsLayout *layout, QPagedPaintDevice *device, bool setFirstPageSize = false );
748
752 ExportResult print( QPagedPaintDevice *device );
753
762 ExportResult printPrivate( QPagedPaintDevice *device, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
763
764 static void updatePrinterPageSize( QgsLayout *layout, QPagedPaintDevice *device, int page );
765
766 ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
767 const QString &filename, unsigned int svgLayerId, const QString &layerName,
768 QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
769
770 void appendMetadataToSvg( QDomDocument &svg ) const;
771
772 bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
773 const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
774
775 ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items,
776 const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc,
777 const std::function<QString( QgsLayoutItem *item )> &getItemExportGroupFunc
778 );
779
780 static QgsVectorSimplifyMethod createExportSimplifyMethod();
781 static QgsMaskRenderSettings createExportMaskSettings();
782 friend class TestQgsLayout;
783 friend class TestQgsLayoutExporter;
784
785};
786
787#endif //QGSLAYOUTEXPORTER_H
788
789
790
TextRenderFormat
Options for rendering text.
Definition qgis.h:2489
@ 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:44
Class that stores computed placement from labeling engine.
Handles rendering and exports of layouts to various formats.
QString errorMessage() const
Returns a string describing the last error encountered during an export.
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.
Base class for graphical items within a QgsLayout.
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:49
The QgsMargins class defines the four margins of a rectangle.
Definition qgsmargins.h:37
Contains settings regarding how masks are calculated and handled during a map render.
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_IF_FEATURE(feature)
Definition qgis_sip.h:181
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_END
Definition qgis_sip.h:208
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.
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.
Contains settings relating to printing layouts.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Contains settings relating to exporting layouts to SVG.
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.