24 #include <QImageWriter>
28 QString QgsLayoutToImageAlgorithm::name()
const
30 return QStringLiteral(
"printlayouttoimage" );
33 QString QgsLayoutToImageAlgorithm::displayName()
const
35 return QObject::tr(
"Export print layout as image" );
38 QStringList QgsLayoutToImageAlgorithm::tags()
const
40 return QObject::tr(
"layout,composer,composition,save,png,jpeg,jpg" ).split(
',' );
43 QString QgsLayoutToImageAlgorithm::group()
const
45 return QObject::tr(
"Cartography" );
48 QString QgsLayoutToImageAlgorithm::groupId()
const
50 return QStringLiteral(
"cartography" );
53 QString QgsLayoutToImageAlgorithm::shortDescription()
const
55 return QObject::tr(
"Exports a print layout as an image." );
58 QString QgsLayoutToImageAlgorithm::shortHelpString()
const
60 return QObject::tr(
"This algorithm outputs a print layout as an image file (e.g. PNG or JPEG images)." );
63 void QgsLayoutToImageAlgorithm::initAlgorithm(
const QVariantMap & )
67 std::unique_ptr< QgsProcessingParameterMultipleLayers > layersParam = std::make_unique< QgsProcessingParameterMultipleLayers>( QStringLiteral(
"LAYERS" ), QObject::tr(
"Map layers to assign to unlocked map item(s)" ),
QgsProcessing::TypeMapLayer, QVariant(),
true );
69 addParameter( layersParam.release() );
71 std::unique_ptr< QgsProcessingParameterNumber > dpiParam = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral(
"DPI" ), QObject::tr(
"DPI (leave blank for default layout DPI)" ),
QgsProcessingParameterNumber::Double, QVariant(),
true, 0 );
73 addParameter( dpiParam.release() );
75 std::unique_ptr< QgsProcessingParameterBoolean > appendGeorefParam = std::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"GEOREFERENCE" ), QObject::tr(
"Generate world file" ),
true );
77 addParameter( appendGeorefParam.release() );
79 std::unique_ptr< QgsProcessingParameterBoolean > exportRDFParam = std::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"INCLUDE_METADATA" ), QObject::tr(
"Export RDF metadata (title, author, etc.)" ),
true );
81 addParameter( exportRDFParam.release() );
83 std::unique_ptr< QgsProcessingParameterBoolean > antialias = std::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"ANTIALIAS" ), QObject::tr(
"Enable antialiasing" ),
true );
85 addParameter( antialias.release() );
87 QStringList imageFilters;
88 const auto supportedImageFormats { QImageWriter::supportedImageFormats() };
89 for (
const QByteArray &format : supportedImageFormats )
91 if ( format ==
"svg" )
94 QString longName = format.toUpper() + QObject::tr(
" format" );
95 QString glob = QStringLiteral(
"*." ) + format;
97 if ( format ==
"png" && !imageFilters.empty() )
98 imageFilters.insert( 0, QStringLiteral(
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() ) );
100 imageFilters.append( QStringLiteral(
"%1 (%2 %3)" ).arg( longName, glob.toLower(), glob.toUpper() ) );
106 QgsProcessingAlgorithm::Flags QgsLayoutToImageAlgorithm::flags()
const
111 QgsLayoutToImageAlgorithm *QgsLayoutToImageAlgorithm::createInstance()
const
113 return new QgsLayoutToImageAlgorithm();
119 QgsPrintLayout *l = parameterAsLayout( parameters, QStringLiteral(
"LAYOUT" ), context );
121 throw QgsProcessingException( QObject::tr(
"Cannot find layout with name \"%1\"" ).arg( parameters.value( QStringLiteral(
"LAYOUT" ) ).toString() ) );
122 std::unique_ptr< QgsPrintLayout > layout( l->
clone() );
124 const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral(
"LAYERS" ), context );
125 if ( layers.size() > 0 )
127 const QList<QGraphicsItem *> items = layout->items();
128 for ( QGraphicsItem *graphicsItem : items )
140 const QString dest = parameterAsFileOutput( parameters, QStringLiteral(
"OUTPUT" ), context );
145 if ( parameters.value( QStringLiteral(
"DPI" ) ).isValid() )
147 settings.
dpi = parameterAsDouble( parameters, QStringLiteral(
"DPI" ), context );
150 settings.
exportMetadata = parameterAsBool( parameters, QStringLiteral(
"INCLUDE_METADATA" ), context );
151 settings.
generateWorldFile = parameterAsBool( parameters, QStringLiteral(
"GEOREFERENCE" ), context );
153 if ( parameterAsBool( parameters, QStringLiteral(
"ANTIALIAS" ), context ) )
158 switch ( exporter.exportToImage( dest, settings ) )
162 feedback->
pushInfo( QObject::tr(
"Successfully exported layout to %1" ).arg( QDir::toNativeSeparators( dest ) ) );
167 throw QgsProcessingException( QObject::tr(
"Cannot write to %1.\n\nThis file may be open in another application." ).arg( QDir::toNativeSeparators( dest ) ) );
171 "resulted in a memory overflow.\n\n"
172 "Please try a lower resolution or a smaller paper size." ) );
185 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
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.
Stores information relating to the current rendering settings for a layout.
@ FlagAntialiasing
Use antialiasing when drawing items.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
QgsPrintLayout * clone() const override
Creates a clone of the layout.
virtual Flags 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.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
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.
@ Double
Double/float values.
@ TypeMapLayer
Any map layer type (raster or vector or mesh)
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...
QgsLayoutRenderContext::Flags 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.