24 #include <QTextStream> 28 :
QgsTask( tr(
"Saving as image" ) )
30 , mFileName( fileName )
31 , mFileFormat( fileFormat )
32 , mForceRaster( forceRaster )
37 :
QgsTask( tr(
"Rendering to painter" ) )
45 qDeleteAll( mAnnotations );
50 mAnnotations << a->
clone();
56 mDecorations = decorations;
64 mJob->cancelWithoutBlocking();
73 std::unique_ptr< QPainter > tempPainter;
74 QPainter *destPainter = mPainter;
75 std::unique_ptr< QPrinter > printer;
77 if ( mFileFormat == QStringLiteral(
"PDF" ) )
79 printer.reset(
new QPrinter() );
80 printer->setOutputFileName( mFileName );
81 printer->setOutputFormat( QPrinter::PdfFormat );
82 printer->setOrientation( QPrinter::Portrait );
84 printer->setPaperSize( mMapSettings.
outputSize() * 25.4 / mMapSettings.
outputDpi(), QPrinter::Millimeter );
85 printer->setPageMargins( 0, 0, 0, 0, QPrinter::Millimeter );
86 printer->setResolution( mMapSettings.
outputDpi() );
90 tempPainter.reset(
new QPainter( printer.get() ) );
91 destPainter = tempPainter.get();
98 img = QImage( mMapSettings.
outputSize(), QImage::Format_ARGB32 );
105 img.setDotsPerMeterX( 1000 * mMapSettings.
outputDpi() / 25.4 );
106 img.setDotsPerMeterY( 1000 * mMapSettings.
outputDpi() / 25.4 );
108 tempPainter.reset(
new QPainter( &img ) );
109 destPainter = tempPainter.get();
118 mJob->renderSynchronously();
121 mJob.reset(
nullptr );
132 decoration->
render( mMapSettings, context );
140 if ( !annotation || !annotation->
isVisible() )
164 context.
painter()->translate( itemX, itemY );
166 annotation->
render( context );
170 if ( !mFileName.isEmpty() )
174 if ( mForceRaster && mFileFormat == QStringLiteral(
"PDF" ) )
177 pp.begin( printer.get() );
178 QRectF rect( 0, 0, img.width(), img.height() );
179 pp.drawImage( rect, img, rect );
182 else if ( mFileFormat != QStringLiteral(
"PDF" ) )
184 bool success = img.save( mFileName, mFileFormat.toLocal8Bit().data() );
191 if ( mSaveWorldFile )
193 QFileInfo info = QFileInfo( mFileName );
196 QString outputSuffix = info.suffix();
197 QString worldFileName = info.absolutePath() +
'/' + info.baseName() +
'.' 198 + outputSuffix.at( 0 ) + outputSuffix.at( info.suffix().size() - 1 ) +
'w';
199 QFile worldFile( worldFileName );
201 if ( worldFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
203 QTextStream stream( &worldFile );
215 qDeleteAll( mAnnotations );
216 mAnnotations.clear();
Job implementation that renders everything sequentially using a custom painter.
bool run() override
Performs the task's operation.
bool isVisible() const
Returns true if the annotation is visible and should be rendered.
Use antialiasing while drawing.
Interface for map decorations.
Flags flags() const
Returns combination of flags used for rendering.
QList< QgsMapLayer * > layers() const
Gets list of layers for map rendering The layers are stored in the reverse order of how they are rend...
bool isCanceled() const
Will return true if task should terminate ASAP.
void addDecorations(const QList< QgsMapDecoration *> &decorations)
Adds decorations to be rendered on the map.
QPointF relativePosition() const
Returns the relative position of the annotation, if it is not attached to a fixed map position...
static QString worldFileContent(const QgsMapSettings &mapSettings)
Creates the content of a world file.
QgsMapRendererTask(const QgsMapSettings &ms, const QString &fileName, const QString &fileFormat=QString("PNG"), bool forceRaster=false)
Constructor for QgsMapRendererTask to render a map to an image file.
Abstract base class for annotation items which are drawn over a map.
The QgsMapSettings class contains configuration for rendering of the map.
void addAnnotations(QList< QgsAnnotation * > annotations)
Adds annotations to be rendered on the map.
QgsRectangle extent() const
Returns geographical coordinates of the rectangle that should be rendered.
double width() const
Returns the width of the rectangle.
void render(QgsRenderContext &context) const
Renders the annotation to a target render context.
Abstract base class for long running background tasks.
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
QgsMapLayer * mapLayer() const
Returns the map layer associated with the annotation.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
virtual void cancel()
Notifies the task that it should terminate.
double outputDpi() const
Returns DPI used for conversion between real world units (e.g.
void finished(bool result) override
If the task is managed by a QgsTaskManager, this will be called after the task has finished (whether ...
Contains information about the context of a rendering operation.
void errorOccurred(int error)
Emitted when map rendering failed.
QPainter * painter()
Returns the destination QPainter for the render operation.
virtual void render(const QgsMapSettings &mapSettings, QgsRenderContext &context)=0
Renders a map decoration.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
void cancel() override
Notifies the task that it should terminate.
virtual QgsAnnotation * clone() const =0
Clones the annotation, returning a new copy of the annotation reflecting the annotation's current sta...
double xMinimum() const
Returns the x minimum value (left side of rectangle).
QSize outputSize() const
Returns the size of the resulting map image.
void renderingComplete()
Emitted when the map rendering is successfully completed.
double height() const
Returns the height of the rectangle.