QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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 {
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#if defined( HAVE_QTPRINTER )
475 SIP_IF_FEATURE( HAVE_QTPRINTER )
476
477
483 ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
484
492 static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
494 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
495
496 SIP_END
497#endif
498
501 {
504 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
505 {}
506
508 double dpi = -1;
509
517 bool forceVectorOutput = false;
518
523 bool cropToContents = false;
524
530
536 bool exportAsLayers = false;
537
546 bool exportLabelsToSeparateLayers = true;
547
554 bool exportMetadata = true;
555
559 QgsLayoutRenderContext::Flags flags = QgsLayoutRenderContext::Flags();
560
568
575 bool simplifyGeometries = true;
576
582 QVector<qreal> predefinedMapScales;
583 };
584
591 ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
592
603 static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
605 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
606
607
612 QString errorFile() const { return mErrorFileName; }
613
621 QMap< QString, QgsLabelingResults * > labelingResults();
622
623#ifndef SIP_RUN
624
634 QMap< QString, QgsLabelingResults * > takeLabelingResults();
635#endif
636
652 bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
653 const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
654
661 void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
662
668 void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
669
678 static bool requiresRasterization( const QgsLayout *layout );
679
687 static bool containsAdvancedEffects( const QgsLayout *layout );
688
689 protected:
690
696 virtual QString generateFileName( const PageExportDetails &details ) const;
697
698 private:
699
700 QPointer< QgsLayout > mLayout;
701
702 void captureLabelingResults();
703 QMap< QString, QgsLabelingResults * > mLabelingResults;
704
705 mutable QString mErrorFileName;
706
707 QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
708
713 static int firstPageToBeExported( QgsLayout *layout );
714
718 static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata );
719
733 std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
734
736 void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
737
741 static void preparePrintAsPdf( QgsLayout *layout, QPagedPaintDevice *device, const QString &filePath );
742
743 static void preparePrint( QgsLayout *layout, QPagedPaintDevice *device, bool setFirstPageSize = false );
744
748 ExportResult print( QPagedPaintDevice *device );
749
758 ExportResult printPrivate( QPagedPaintDevice *device, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
759
760 static void updatePrinterPageSize( QgsLayout *layout, QPagedPaintDevice *device, int page );
761
762 ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
763 const QString &filename, unsigned int svgLayerId, const QString &layerName,
764 QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
765
766 void appendMetadataToSvg( QDomDocument &svg ) const;
767
768 bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
769 const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
770
771 ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items, const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc );
772
773 static QgsVectorSimplifyMethod createExportSimplifyMethod();
774 friend class TestQgsLayout;
775 friend class TestQgsLayoutExporter;
776
777};
778
779#endif //QGSLAYOUTEXPORTER_H
780
781
782
TextRenderFormat
Flags which control how map layer renderers behave.
Definition qgis.h:2287
@ 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.
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:49
The QgsMargins class defines the four margins of a rectangle.
Definition qgsmargins.h:37
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.
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.