QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 "qgsconfig.h"
20
21#include <functional>
22
23#include "qgis_core.h"
24#include "qgslayoutitem.h"
27#include "qgsmargins.h"
28
29#include <QPdfWriter>
30#include <QPointer>
31#include <QRectF>
32#include <QSize>
33#include <QVector>
34
35#if defined( HAVE_QTPRINTER )
36#include <QPrinter>
37#endif
38
39class QgsLayout;
40class QPainter;
43class QgsFeedback;
47
53class CORE_EXPORT QgsLayoutExporter
54{
55 public:
58
61
64
67
70 {
72 QString directory;
73
75 QString baseName;
76
78 QString extension;
79
81 int page = 0;
82 };
83
88
89 virtual ~QgsLayoutExporter();
90
94 QgsLayout *layout() const;
95
104 void renderPage( QPainter *painter, int page ) const;
105
124 QImage renderPageToImage( int page, QSize imageSize = QSize(), double dpi = -1 ) const;
125
133 void renderRegion( QPainter *painter, const QRectF &region ) const;
134
151 QImage renderRegionToImage( const QRectF &region, QSize imageSize = QSize(), double dpi = -1 ) const;
152
153
165
168 {
170 : flags( Qgis::LayoutRenderFlag::Antialiasing | Qgis::LayoutRenderFlag::UseAdvancedEffects )
171 {}
172
174 double dpi = -1;
175
189
194 bool cropToContents = false;
195
201
209 QList< int > pages;
210
215 bool generateWorldFile = false;
216
223 bool exportMetadata = true;
224
229
235 QVector<qreal> predefinedMapScales;
236
242 int quality = -1;
243 };
244
255 ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
256
257
269 QgsAbstractLayoutIterator *iterator, const QString &baseFilePath, const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr
270 );
271
272
275 {
277 : flags( Qgis::LayoutRenderFlag::Antialiasing | Qgis::LayoutRenderFlag::UseAdvancedEffects )
278 {}
279
281 double dpi = -1;
282
289
297 bool forceVectorOutput = false;
298
305
312 bool exportMetadata = true;
313
318
326
334
346 bool writeGeoPdf = false;
347
361 bool exportLayersAsSeperateFiles = false; // TODO QGIS 5 fix typo //#spellok
362
372
385
392
401 QStringList exportThemes;
402
408 QVector<qreal> predefinedMapScales;
409 };
410
417 ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
418
431 QgsAbstractLayoutIterator *iterator, const QString &fileName, const QgsLayoutExporter::PdfExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr
432 );
433
447 QgsAbstractLayoutIterator *iterator, const QString &baseFilePath, const QgsLayoutExporter::PdfExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr
448 );
449
450
453 {
455 : flags( Qgis::LayoutRenderFlag::Antialiasing | Qgis::LayoutRenderFlag::UseAdvancedEffects )
456 {}
457
459 double dpi = -1;
460
467
472
478 QVector<qreal> predefinedMapScales;
479 };
480
481#if defined( HAVE_QTPRINTER )
482 SIP_IF_FEATURE( HAVE_QTPRINTER )
483
484
490 ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
491
499 static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
500
501 SIP_END
502#endif
503
506 {
508 : flags( Qgis::LayoutRenderFlag::Antialiasing | Qgis::LayoutRenderFlag::UseAdvancedEffects )
509 {}
510
512 double dpi = -1;
513
521 bool forceVectorOutput = false;
522
527 bool cropToContents = false;
528
534
540 bool exportAsLayers = false;
541
551
558 bool exportMetadata = true;
559
564
572
580
586 QVector<qreal> predefinedMapScales;
587 };
588
595 ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
596
608 QgsAbstractLayoutIterator *iterator, const QString &baseFilePath, const QgsLayoutExporter::SvgExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr
609 );
610
611
616 QString errorFile() const { return mErrorFileName; }
617
623 QString errorMessage() const { return mErrorMessage; }
624
632 QMap< QString, QgsLabelingResults * > labelingResults();
633
634#ifndef SIP_RUN
635
645 QMap< QString, QgsLabelingResults * > takeLabelingResults();
646#endif
647
663 bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
664
671 void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
672
678 void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
679
688 static bool requiresRasterization( const QgsLayout *layout );
689
697 static bool containsAdvancedEffects( const QgsLayout *layout );
698
699 protected:
705 virtual QString generateFileName( const PageExportDetails &details ) const;
706
707 private:
708 QPointer< QgsLayout > mLayout;
709
710 void captureLabelingResults();
711 QMap< QString, QgsLabelingResults * > mLabelingResults;
712
713 mutable QString mErrorFileName;
714 mutable QString mErrorMessage;
715
716 QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
717
722 static int firstPageToBeExported( QgsLayout *layout );
723
727 static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata, int quality = -1 );
728
742 std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
743
745 void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
746
750 static void preparePrintAsPdf( QgsLayout *layout, QPdfWriter *device, const QString &filePath );
751
752 static void preparePrint( QgsLayout *layout, QPagedPaintDevice *device, bool setFirstPageSize = false );
753
757 ExportResult print( QPagedPaintDevice *device );
758
767 ExportResult printPrivate( QPagedPaintDevice *device, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
768
769 static void updatePrinterPageSize( QgsLayout *layout, QPagedPaintDevice *device, int page );
770
771 ExportResult renderToLayeredSvg(
772 const SvgExportSettings &settings,
773 double width,
774 double height,
775 int page,
776 const QRectF &bounds,
777 const QString &filename,
778 unsigned int svgLayerId,
779 const QString &layerName,
780 QDomDocument &svg,
781 QDomNode &svgDocRoot,
782 bool includeMetadata
783 ) const;
784
785 void appendMetadataToSvg( QDomDocument &svg ) const;
786
787 bool georeferenceOutputPrivate(
788 const QString &file, QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false
789 ) const;
790
791 ExportResult handleLayeredExport(
792 const QList<QGraphicsItem *> &items,
793 const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc,
794 const std::function<QString( QgsLayoutItem *item )> &getItemExportGroupFunc
795 );
796
797 // Returns PDF creator (used also as producer)
798 static QString getCreator();
799
800 // Set PDF XMP metadata on pdfWriter for given layout
801 static void setXmpMetadata( QPdfWriter *pdfWriter, QgsLayout *layout );
802
803 static QgsVectorSimplifyMethod createExportSimplifyMethod();
804 static QgsMaskRenderSettings createExportMaskSettings();
805 friend class TestQgsLayout;
807};
808
809#endif //QGSLAYOUTEXPORTER_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
QFlags< LayoutRenderFlag > LayoutRenderFlags
Flags for controlling how a layout is rendered.
Definition qgis.h:5423
TextRenderFormat
Options for rendering text.
Definition qgis.h:2923
@ AlwaysOutlines
Always render text using path objects (AKA outlines/curves). This setting guarantees the best quality...
Definition qgis.h:2924
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
Stores computed placement from labeling engine.
ExportResult exportToSvg(const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings)
Exports the layout as an SVG to the filePath, using the specified export settings.
ExportResult exportToImage(const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings)
Exports the layout to the filePath, using the specified export settings.
QString errorMessage() const
Returns a string describing the last error encountered during an export.
ExportResult exportToPdf(const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings)
Exports the layout as a PDF to the filePath, using the specified export settings.
QImage renderRegionToImage(const QRectF &region, QSize imageSize=QSize(), double dpi=-1) const
Renders a region of the layout to an image.
QgsLayout * layout() const
Returns the layout linked to this exporter.
static const QgsSettingsEntryBool * settingOpenAfterExportingPdf
Settings entry - Whether to automatically open pdfs after exporting them.
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.
static const QgsSettingsEntryInteger * settingImageQuality
Settings entry - Image quality for lossy formats.
QImage renderPageToImage(int page, QSize imageSize=QSize(), double dpi=-1) const
Renders a full page to an image.
QgsLayoutExporter(QgsLayout *layout)
Constructor for QgsLayoutExporter, for the specified layout.
friend class TestQgsLayoutExporter
static ExportResult exportToPdfs(QgsAbstractLayoutIterator *iterator, const QString &baseFilePath, const QgsLayoutExporter::PdfExportSettings &settings, QString &error, QgsFeedback *feedback=nullptr)
Exports a layout iterator to multiple PDF files, with the specified export settings.
QString errorFile() const
Returns the file name corresponding to the last error encountered during an export.
void renderPage(QPainter *painter, int page) const
Renders a full page to a destination painter.
static const QgsSettingsEntryBool * settingOpenAfterExportingImage
Settings entry - Whether to automatically open images after exporting them.
static const QgsSettingsEntryBool * settingOpenAfterExportingSvg
Settings entry - Whether to automatically open svgs after exporting them.
void renderRegion(QPainter *painter, const QRectF &region) const
Renders a region from the layout to a painter.
friend class TestQgsLayout
Layout graphical items for displaying a map.
Base class for graphical items within a QgsLayout.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:50
Defines the four margins of a rectangle.
Definition qgsmargins.h:40
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:113
A boolean settings entry.
An integer settings entry.
Contains settings for simplifying 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:133
#define SIP_IF_FEATURE(feature)
Definition qgis_sip.h:188
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_END
Definition qgis_sip.h:215
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.
bool generateWorldFile
Set to true to generate an external world file alongside exported images.
QSize imageSize
Manual size in pixels for output image.
bool exportMetadata
Indicates whether image export should include metadata generated from the layout's project's metadata...
int quality
Image quality, typically used for JPEG compression (whose quality ranges from 1 to 100) if quality is...
Qgis::LayoutRenderFlags flags
Layout context flags, which control how the export will be created.
bool cropToContents
Set to true if image should be cropped so only parts of the layout containing items are exported.
double dpi
Resolution to export layout at. If dpi <= 0 the default layout dpi will be used.
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 '.').
int page
Page number, where 0 = first page.
Contains settings relating to exporting layouts to PDF.
bool useIso32000ExtensionFormatGeoreferencing
true if ISO3200 extension format georeferencing should be used.
bool forceVectorOutput
Set to true to force vector object exports, even when the resultant appearance will differ from the l...
bool rasterizeWholeImage
Set to true to force whole layout to be rasterized while exporting.
QStringList exportThemes
Optional list of map themes to export as Geospatial PDF layer groups.
bool includeGeoPdfFeatures
true if feature vector information (such as attributes) should be exported during Geospatial PDF expo...
bool exportMetadata
Indicates whether PDF export should include metadata generated from the layout's project's metadata.
bool appendGeoreference
Indicates whether PDF export should append georeference data.
Qgis::LayoutRenderFlags flags
Layout context flags, which control how the export will be created.
bool writeGeoPdf
true if geospatial PDF files should be created, instead of normal PDF files.
double dpi
Resolution to export layout at. If dpi <= 0 the default layout dpi will be used.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
bool exportLayersAsSeperateFiles
true if individual layers from the layout should be rendered to separate PDF files.
bool simplifyGeometries
Indicates whether vector geometries should be simplified to avoid redundant extraneous detail,...
bool useOgcBestPracticeFormatGeoreferencing
true if OGC "best practice" format georeferencing should be used.
Qgis::TextRenderFormat textRenderFormat
Text rendering format, which controls how text should be rendered in the export (e....
Contains settings relating to printing layouts.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
Qgis::LayoutRenderFlags flags
Layout context flags, which control how the export will be created.
double dpi
Resolution to export layout at. If dpi <= 0 the default layout dpi will be used.
bool rasterizeWholeImage
Set to true to force whole layout to be rasterized while exporting.
Contains settings relating to exporting layouts to SVG.
bool forceVectorOutput
Set to true to force vector object exports, even when the resultant appearance will differ from the l...
Qgis::LayoutRenderFlags flags
Layout context flags, which control how the export will be created.
Qgis::TextRenderFormat textRenderFormat
Text rendering format, which controls how text should be rendered in the export (e....
bool exportAsLayers
Set to true to export as a layered SVG file.
bool simplifyGeometries
Indicates whether vector geometries should be simplified to avoid redundant extraneous detail,...
bool exportMetadata
Indicates whether SVG export should include RDF metadata generated from the layout's project's metada...
double dpi
Resolution to export layout at. If dpi <= 0 the default layout dpi will be used.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
bool exportLabelsToSeparateLayers
Set to true to export labels to separate layers (grouped by map layer) in layered SVG exports.
bool cropToContents
Set to true if image should be cropped so only parts of the layout containing items are exported.
QgsMargins cropMargins
Crop to content margins, in layout units.
Contains details of a particular export layer relating to a layout item.