QGIS API Documentation 4.1.0-Master (01362494303)
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
402 QStringList exportThemes;
403
409 QVector<qreal> predefinedMapScales;
410
421 bool useLayerTreeConfig = false;
422 };
423
430 ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
431
444 QgsAbstractLayoutIterator *iterator, const QString &fileName, const QgsLayoutExporter::PdfExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr
445 );
446
460 QgsAbstractLayoutIterator *iterator, const QString &baseFilePath, const QgsLayoutExporter::PdfExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr
461 );
462
463
466 {
468 : flags( Qgis::LayoutRenderFlag::Antialiasing | Qgis::LayoutRenderFlag::UseAdvancedEffects )
469 {}
470
472 double dpi = -1;
473
480
485
491 QVector<qreal> predefinedMapScales;
492 };
493
494#if defined( HAVE_QTPRINTER )
495 SIP_IF_FEATURE( HAVE_QTPRINTER )
496
497
503 ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
504
512 static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
513
514 SIP_END
515#endif
516
519 {
521 : flags( Qgis::LayoutRenderFlag::Antialiasing | Qgis::LayoutRenderFlag::UseAdvancedEffects )
522 {}
523
525 double dpi = -1;
526
534 bool forceVectorOutput = false;
535
540 bool cropToContents = false;
541
547
553 bool exportAsLayers = false;
554
564
571 bool exportMetadata = true;
572
577
585
593
599 QVector<qreal> predefinedMapScales;
600 };
601
608 ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
609
621 QgsAbstractLayoutIterator *iterator, const QString &baseFilePath, const QgsLayoutExporter::SvgExportSettings &settings, QString &error SIP_OUT, QgsFeedback *feedback = nullptr
622 );
623
624
629 QString errorFile() const { return mErrorFileName; }
630
636 QString errorMessage() const { return mErrorMessage; }
637
645 QMap< QString, QgsLabelingResults * > labelingResults();
646
647#ifndef SIP_RUN
648
658 QMap< QString, QgsLabelingResults * > takeLabelingResults();
659#endif
660
676 bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
677
684 void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
685
691 void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
692
701 static bool requiresRasterization( const QgsLayout *layout );
702
710 static bool containsAdvancedEffects( const QgsLayout *layout );
711
712 protected:
718 virtual QString generateFileName( const PageExportDetails &details ) const;
719
720 private:
721 QPointer< QgsLayout > mLayout;
722
723 void captureLabelingResults();
724 QMap< QString, QgsLabelingResults * > mLabelingResults;
725
726 mutable QString mErrorFileName;
727 mutable QString mErrorMessage;
728
729 QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
730
735 static int firstPageToBeExported( QgsLayout *layout );
736
740 static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata, int quality = -1 );
741
755 std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
756
758 void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
759
763 static void preparePrintAsPdf( QgsLayout *layout, QPdfWriter *device, const QString &filePath );
764
765 static void preparePrint( QgsLayout *layout, QPagedPaintDevice *device, bool setFirstPageSize = false );
766
770 ExportResult print( QPagedPaintDevice *device );
771
780 ExportResult printPrivate( QPagedPaintDevice *device, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
781
782 static void updatePrinterPageSize( QgsLayout *layout, QPagedPaintDevice *device, int page );
783
784 ExportResult renderToLayeredSvg(
785 const SvgExportSettings &settings,
786 double width,
787 double height,
788 int page,
789 const QRectF &bounds,
790 const QString &filename,
791 unsigned int svgLayerId,
792 const QString &layerName,
793 QDomDocument &svg,
794 QDomNode &svgDocRoot,
795 bool includeMetadata
796 ) const;
797
798 void appendMetadataToSvg( QDomDocument &svg ) const;
799
800 bool georeferenceOutputPrivate(
801 const QString &file, QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false
802 ) const;
803
804 ExportResult handleLayeredExport(
805 const QList<QGraphicsItem *> &items,
806 const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc,
807 const std::function<QString( QgsLayoutItem *item )> &getItemExportGroupFunc
808 );
809
810 // Returns PDF creator (used also as producer)
811 static QString getCreator();
812
813 // Set PDF XMP metadata on pdfWriter for given layout
814 static void setXmpMetadata( QPdfWriter *pdfWriter, QgsLayout *layout );
815
816 static QgsVectorSimplifyMethod createExportSimplifyMethod();
817 static QgsMaskRenderSettings createExportMaskSettings();
818 friend class TestQgsLayout;
820};
821
822#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:5567
TextRenderFormat
Options for rendering text.
Definition qgis.h:2990
@ AlwaysOutlines
Always render text using path objects (AKA outlines/curves). This setting guarantees the best quality...
Definition qgis.h:2991
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:51
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:114
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 useLayerTreeConfig
If set to true, the layer tree from the QGIS project should be used when creating a Geospatial 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.