26#include <QImageWriter>
29using namespace Qt::StringLiterals;
33QString QgsLayoutToImageAlgorithm::name()
const
35 return u
"printlayouttoimage"_s;
38QString QgsLayoutToImageAlgorithm::displayName()
const
40 return QObject::tr(
"Export print layout as image" );
43QStringList QgsLayoutToImageAlgorithm::tags()
const
45 return QObject::tr(
"layout,composer,composition,save,png,jpeg,jpg" ).split(
',' );
48QString QgsLayoutToImageAlgorithm::group()
const
50 return QObject::tr(
"Cartography" );
53QString QgsLayoutToImageAlgorithm::groupId()
const
55 return u
"cartography"_s;
58QString QgsLayoutToImageAlgorithm::shortDescription()
const
60 return QObject::tr(
"Exports a print layout as an image." );
63QString QgsLayoutToImageAlgorithm::shortHelpString()
const
65 return QObject::tr(
"This algorithm outputs a print layout as an image file (e.g. PNG or JPEG images)." );
68void QgsLayoutToImageAlgorithm::initAlgorithm(
const QVariantMap & )
73 = std::make_unique<QgsProcessingParameterMultipleLayers>( u
"LAYERS"_s, QObject::tr(
"Map layers to assign to unlocked map item(s)" ),
Qgis::ProcessingSourceType::MapLayer, QVariant(),
true );
75 addParameter( layersParam.release() );
79 addParameter( dpiParam.release() );
81 auto appendGeorefParam = std::make_unique<QgsProcessingParameterBoolean>( u
"GEOREFERENCE"_s, QObject::tr(
"Generate world file" ),
true );
83 addParameter( appendGeorefParam.release() );
85 auto exportRDFParam = std::make_unique<QgsProcessingParameterBoolean>( u
"INCLUDE_METADATA"_s, QObject::tr(
"Export RDF metadata (title, author, etc.)" ),
true );
87 addParameter( exportRDFParam.release() );
89 auto antialias = std::make_unique<QgsProcessingParameterBoolean>( u
"ANTIALIAS"_s, QObject::tr(
"Enable antialiasing" ),
true );
91 addParameter( antialias.release() );
101QgsLayoutToImageAlgorithm *QgsLayoutToImageAlgorithm::createInstance()
const
103 return new QgsLayoutToImageAlgorithm();
109 QgsPrintLayout *l = parameterAsLayout( parameters, u
"LAYOUT"_s, context );
111 throw QgsProcessingException( QObject::tr(
"Cannot find layout with name \"%1\"" ).arg( parameters.value( u
"LAYOUT"_s ).toString() ) );
112 std::unique_ptr<QgsPrintLayout> layout( l->
clone() );
114 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"LAYERS"_s, context );
115 if ( layers.size() > 0 )
117 const QList<QGraphicsItem *> items = layout->items();
118 for ( QGraphicsItem *graphicsItem : items )
130 const QString dest = parameterAsFileOutput( parameters, u
"OUTPUT"_s, context );
135 if ( parameters.value( u
"DPI"_s ).isValid() )
137 settings.
dpi = parameterAsDouble( parameters, u
"DPI"_s, context );
140 settings.
exportMetadata = parameterAsBool( parameters, u
"INCLUDE_METADATA"_s, context );
141 settings.
generateWorldFile = parameterAsBool( parameters, u
"GEOREFERENCE"_s, context );
143 if ( parameterAsBool( parameters, u
"ANTIALIAS"_s, context ) )
148 switch ( exporter.exportToImage( dest, settings ) )
152 feedback->
pushInfo( QObject::tr(
"Successfully exported layout to %1" ).arg( QDir::toNativeSeparators( dest ) ) );
158 !exporter.errorMessage().isEmpty() ? exporter.errorMessage() : QObject::tr(
"Cannot write to %1.\n\nThis file may be open in another application." ).arg( QDir::toNativeSeparators( dest ) )
163 !exporter.errorMessage().isEmpty() ? exporter.errorMessage()
165 "Trying to create the image "
166 "resulted in a memory overflow.\n\n"
167 "Please try a lower resolution or a smaller paper size."
182 outputs.insert( u
"OUTPUT"_s, dest );
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer).
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
@ RequiresProject
The algorithm requires that a valid QgsProject is available from the processing context in order to e...
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
@ Antialiasing
Use antialiasing when drawing items.
void setProgress(double progress)
Sets the current progress for the feedback object.
Handles rendering and exports of layouts to various formats.
@ 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.
Layout graphical items for displaying a map.
bool keepLayerSet() const
Returns whether a stored layer set should be used or the current layer set from the project associate...
void setKeepLayerSet(bool enabled)
Sets whether the stored layer set should be used or the current layer set of the associated project.
bool followVisibilityPreset() const
Returns whether the map should follow a map theme.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the stored layers set.
Base class for graphical items within a QgsLayout.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
QgsPrintLayout * clone() const override
Creates a clone of the layout.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A print layout parameter, allowing users to select a print layout.
static QString supportedImageFileFilters()
Returns a file filter string of all supported image formats, suitable for use in file picker dialogs.
Contains settings relating to exporting layouts to raster images.
bool generateWorldFile
Set to true to generate an external world file alongside exported images.
bool exportMetadata
Indicates whether image export should include metadata generated from the layout's project's metadata...
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.