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 const QString longName = format.toUpper() + QObject::tr(
" format" );
95 const 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 );