QGIS API Documentation 3.41.0-Master (88383c3d16f)
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
387 bool useOgcBestPracticeFormatGeoreferencing = false;
388
394 bool includeGeoPdfFeatures = true;
395
404 QStringList exportThemes;
405
411 QVector<qreal> predefinedMapScales;
412 };
413
420 ExportResult exportToPdf( const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings );
421
433 static ExportResult exportToPdf( QgsAbstractLayoutIterator *iterator, const QString &fileName,
435 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
436
449 static ExportResult exportToPdfs( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
451 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
452
453
456 {
458 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
459 {}
460
462 double dpi = -1;
463
469 bool rasterizeWholeImage = false;
470
475
481 QVector<qreal> predefinedMapScales;
482
483 };
484
485#if defined( HAVE_QTPRINTER )
486 SIP_IF_FEATURE( HAVE_QTPRINTER )
487
488
494 ExportResult print( QPrinter &printer, const QgsLayoutExporter::PrintExportSettings &settings );
495
503 static ExportResult print( QgsAbstractLayoutIterator *iterator, QPrinter &printer,
505 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
506
507 SIP_END
508#endif
509
512 {
514 : flags( QgsLayoutRenderContext::FlagAntialiasing | QgsLayoutRenderContext::FlagUseAdvancedEffects )
515 {}
516
518 double dpi = -1;
519
527 bool forceVectorOutput = false;
528
533 bool cropToContents = false;
534
540
546 bool exportAsLayers = false;
547
556 bool exportLabelsToSeparateLayers = true;
557
564 bool exportMetadata = true;
565
570
578
585 bool simplifyGeometries = true;
586
592 QVector<qreal> predefinedMapScales;
593 };
594
601 ExportResult exportToSvg( const QString &filePath, const QgsLayoutExporter::SvgExportSettings &settings );
602
613 static ExportResult exportToSvg( QgsAbstractLayoutIterator *iterator, const QString &baseFilePath,
615 QString &error SIP_OUT, QgsFeedback *feedback = nullptr );
616
617
622 QString errorFile() const { return mErrorFileName; }
623
629 QString errorMessage() const { return mErrorMessage; }
630
638 QMap< QString, QgsLabelingResults * > labelingResults();
639
640#ifndef SIP_RUN
641
651 QMap< QString, QgsLabelingResults * > takeLabelingResults();
652#endif
653
669 bool georeferenceOutput( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
670 const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
671
678 void computeWorldFileParameters( double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
679
685 void computeWorldFileParameters( const QRectF &region, double &a, double &b, double &c, double &d, double &e, double &f, double dpi = -1 ) const;
686
695 static bool requiresRasterization( const QgsLayout *layout );
696
704 static bool containsAdvancedEffects( const QgsLayout *layout );
705
706 protected:
707
713 virtual QString generateFileName( const PageExportDetails &details ) const;
714
715 private:
716
717 QPointer< QgsLayout > mLayout;
718
719 void captureLabelingResults();
720 QMap< QString, QgsLabelingResults * > mLabelingResults;
721
722 mutable QString mErrorFileName;
723 mutable QString mErrorMessage;
724
725 QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;
726
731 static int firstPageToBeExported( QgsLayout *layout );
732
736 static bool saveImage( const QImage &image, const QString &imageFilename, const QString &imageFormat, QgsProject *projectForMetadata, int quality = -1 );
737
751 std::unique_ptr<double[]> computeGeoTransform( const QgsLayoutItemMap *referenceMap = nullptr, const QRectF &exportRegion = QRectF(), double dpi = -1 ) const;
752
754 void writeWorldFile( const QString &fileName, double a, double b, double c, double d, double e, double f ) const;
755
759 static void preparePrintAsPdf( QgsLayout *layout, QPdfWriter *device, const QString &filePath );
760
761 static void preparePrint( QgsLayout *layout, QPagedPaintDevice *device, bool setFirstPageSize = false );
762
766 ExportResult print( QPagedPaintDevice *device );
767
776 ExportResult printPrivate( QPagedPaintDevice *device, QPainter &painter, bool startNewPage = false, double dpi = -1, bool rasterize = false );
777
778 static void updatePrinterPageSize( QgsLayout *layout, QPagedPaintDevice *device, int page );
779
780 ExportResult renderToLayeredSvg( const SvgExportSettings &settings, double width, double height, int page, const QRectF &bounds,
781 const QString &filename, unsigned int svgLayerId, const QString &layerName,
782 QDomDocument &svg, QDomNode &svgDocRoot, bool includeMetadata ) const;
783
784 void appendMetadataToSvg( QDomDocument &svg ) const;
785
786 bool georeferenceOutputPrivate( const QString &file, QgsLayoutItemMap *referenceMap = nullptr,
787 const QRectF &exportRegion = QRectF(), double dpi = -1, bool includeGeoreference = true, bool includeMetadata = false ) const;
788
789 ExportResult handleLayeredExport( const QList<QGraphicsItem *> &items,
790 const std::function<QgsLayoutExporter::ExportResult( unsigned int layerId, const QgsLayoutItem::ExportLayerDetail &layerDetails )> &exportFunc,
791 const std::function<QString( QgsLayoutItem *item )> &getItemExportGroupFunc
792 );
793
794 // Returns PDF creator (used also as producer)
795 static QString getCreator();
796
797 // Set PDF XMP metadata on pdfWriter for given layout
798 static void setXmpMetadata( QPdfWriter *pdfWriter, QgsLayout *layout );
799
800 static QgsVectorSimplifyMethod createExportSimplifyMethod();
801 static QgsMaskRenderSettings createExportMaskSettings();
802 friend class TestQgsLayout;
803 friend class TestQgsLayoutExporter;
804
805};
806
807#endif //QGSLAYOUTEXPORTER_H
808
809
810
TextRenderFormat
Options for rendering text.
Definition qgis.h:2707
@ 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.