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;
77 std::unique_ptr< QPrinter > printer;
78 #endif // ! QT_NO_PRINTER 80 if ( mFileFormat == QStringLiteral(
"PDF" ) )
83 printer.reset(
new QPrinter() );
84 printer->setOutputFileName( mFileName );
85 printer->setOutputFormat( QPrinter::PdfFormat );
86 printer->setOrientation( QPrinter::Portrait );
88 printer->setPaperSize( mMapSettings.
outputSize() * 25.4 / mMapSettings.
outputDpi(), QPrinter::Millimeter );
89 printer->setPageMargins( 0, 0, 0, 0, QPrinter::Millimeter );
90 printer->setResolution( mMapSettings.
outputDpi() );
94 tempPainter.reset(
new QPainter( printer.get() ) );
95 destPainter = tempPainter.get();
100 #endif // ! QT_NO_PRINTER 106 img = QImage( mMapSettings.
outputSize(), QImage::Format_ARGB32 );
113 img.setDotsPerMeterX( 1000 * mMapSettings.
outputDpi() / 25.4 );
114 img.setDotsPerMeterY( 1000 * mMapSettings.
outputDpi() / 25.4 );
116 tempPainter.reset(
new QPainter( &img ) );
117 destPainter = tempPainter.get();
126 mJob->renderSynchronously();
129 mJob.reset(
nullptr );
140 decoration->
render( mMapSettings, context );
148 if ( !annotation || !annotation->
isVisible() )
172 context.
painter()->translate( itemX, itemY );
174 annotation->
render( context );
178 if ( !mFileName.isEmpty() )
182 if ( mForceRaster && mFileFormat == QStringLiteral(
"PDF" ) )
184 #ifndef QT_NO_PRINTER 186 pp.begin( printer.get() );
187 QRectF rect( 0, 0, img.width(), img.height() );
188 pp.drawImage( rect, img, rect );
193 #endif // !QT_NO_PRINTER 195 else if ( mFileFormat != QStringLiteral(
"PDF" ) )
197 bool success = img.save( mFileName, mFileFormat.toLocal8Bit().data() );
204 if ( mSaveWorldFile )
206 QFileInfo info = QFileInfo( mFileName );
209 QString outputSuffix = info.suffix();
210 QString worldFileName = info.absolutePath() +
'/' + info.baseName() +
'.' 211 + outputSuffix.at( 0 ) + outputSuffix.at( info.suffix().size() - 1 ) +
'w';
212 QFile worldFile( worldFileName );
214 if ( worldFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
216 QTextStream stream( &worldFile );
228 qDeleteAll( mAnnotations );
229 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.
Format is unsupported on the platform.
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...
Image allocation failure.
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.