QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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;
45
51class CORE_EXPORT QgsLayoutExporter
52{
53
54 public:
55
57 static const QgsSettingsEntryBool *settingOpenAfterExportingImage SIP_SKIP;
58
60 static const QgsSettingsEntryBool *settingOpenAfterExportingPdf SIP_SKIP;
61
63 static const QgsSettingsEntryBool *settingOpenAfterExportingSvg SIP_SKIP;
64
66 static const QgsSettingsEntryInteger *settingImageQuality SIP_SKIP;
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( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
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
225
230
236 QVector<qreal> predefinedMapScales;
237
238
244 int quality = -1;
245
246 };
247
258 ExportResult exportToImage( const QString &filePath, const QgsLayoutExporter::ImageExportSettings &settings );
259
260
271 static ExportResult exportToImage( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
272 const QString &extension, const QgsLayoutExporter::ImageExportSettings &settings,
273 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
274
275
278 {
280 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
281 {}
282
284 double dpi = -1;
285
291 bool rasterizeWholeImage = false;
292
300 bool forceVectorOutput = false;
301
307 bool appendGeoreference = true;
308
315 bool exportMetadata = true;
316
321
329
336 bool simplifyGeometries = true;
337
349 bool writeGeoPdf = false;
350
364 bool exportLayersAsSeperateFiles = false; // TODO QGIS 4 fix typo //#spellok
365
374 bool useIso32000ExtensionFormatGeoreferencing = true;
375
385 bool useOgcBestPracticeFormatGeoreferencing = false;
386
392 bool includeGeoPdfFeatures = true;
393
402 QStringList exportThemes;
403
409 QVector<qreal> predefinedMapScales;
410 };
411
418 ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
419
431 static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
433 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
434
447 static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
449 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
450
451
454 {
456 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
457 {}
458
460 double dpi = -1;
461
467 bool rasterizeWholeImage = false;
468
473
479 QVector<qreal> predefinedMapScales;
480
481 };
482
483#if defined( HAVE_QTPRINTER )
484 SIP_IF_FEATURE( HAVE_QTPRINTER )
485
486
492 ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
493
501 static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
503 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
504
505 SIP_END
506#endif
507
510 {
512 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
513 {}
514
516 double dpi = -1;
517
525 bool forceVectorOutput = false;
526
531 bool cropToContents = false;
532
538
544 bool exportAsLayers = false;
545
554 bool exportLabelsToSeparateLayers = true;
555
562 bool exportMetadata = true;
563
568
576
583 bool simplifyGeometries = true;
584
590 QVector<qreal> predefinedMapScales;
591 };
592
599 ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
600
611 static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
613 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
614
615
620 QString errorFile() const { return mErrorFileName; }
621
627 QString errorMessage() const { return mErrorMessage; }
628
636 QMap< QString, QgsLabelingResults * > labelingResults();
637
638#ifndef SIP_RUN
639
649 QMap< QString, QgsLabelingResults * > takeLabelingResults();
650#endif
651
667 bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
668 const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
669
676 void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
677
683 void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
684
693 static bool requiresRasterization( const QgsLayout *layout );
694
702 static bool containsAdvancedEffects( const QgsLayout *layout );
703
704 protected:
705
711 virtual QString generateFileName( const PageExportDetails &details ) const;
712
713 private:
714
715 QPointer< QgsLayout > mLayout;
716
717 void captureLabelingResults();
718 QMap< QString, QgsLabelingResults * > mLabelingResults;
719
720 mutable QString mErrorFileName;
721 mutable QString mErrorMessage;
722
723 QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
724
729 static int firstPageToBeExported( QgsLayout *layout );
730
734 static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata, int quality = -1 );
735
749 std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
750
752 void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
753
757 static void preparePrintAsPdf( QgsLayout *layout, QPdfWriter *device, const QString &filePath );
758
759 static void preparePrint( QgsLayout *layout, QPagedPaintDevice *device, bool setFirstPageSize = false );
760
764 ExportResult print( QPagedPaintDevice *device );
765
774 ExportResult printPrivate( QPagedPaintDevice *device, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
775
776 static void updatePrinterPageSize( QgsLayout *layout, QPagedPaintDevice *device, int page );
777
778 ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
779 const QString &filename, unsigned int svgLayerId, const QString &layerName,
780 QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
781
782 void appendMetadataToSvg( QDomDocument &svg ) const;
783
784 bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
785 const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
786
787 ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items,
788 const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc,
789 const std::function<QString( QgsLayoutItem *item )> &getItemExportGroupFunc
790 );
791
792 // Returns PDF creator (used also as producer)
793 static QString getCreator();
794
795 // Set PDF XMP metadata on pdfWriter for given layout
796 static void setXmpMetadata( QPdfWriter *pdfWriter, QgsLayout *layout );
797
798 static QgsVectorSimplifyMethod createExportSimplifyMethod();
799 static QgsMaskRenderSettings createExportMaskSettings();
800 friend class TestQgsLayout;
801 friend class TestQgsLayoutExporter;
802
803};
804
805#endif //QGSLAYOUTEXPORTER_H
806
807
808
TextRenderFormat
Options for rendering text.
Definition qgis.h:2624
@ 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.
An integer 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 Geospatial PDF 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.