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 );
48 const auto constAnnotations = annotations;
51 mAnnotations << a->clone();
57 mDecorations = decorations;
65 mJob->cancelWithoutBlocking();
74 std::unique_ptr< QPainter > tempPainter;
75 QPainter *destPainter = mPainter;
78 std::unique_ptr< QPrinter > printer;
79 #endif // ! QT_NO_PRINTER 81 if ( mFileFormat == QStringLiteral(
"PDF" ) )
84 printer.reset(
new QPrinter() );
85 printer->setOutputFileName( mFileName );
86 printer->setOutputFormat( QPrinter::PdfFormat );
87 printer->setOrientation( QPrinter::Portrait );
89 printer->setPaperSize( mMapSettings.
outputSize() * 25.4 / mMapSettings.
outputDpi(), QPrinter::Millimeter );
90 printer->setPageMargins( 0, 0, 0, 0, QPrinter::Millimeter );
91 printer->setResolution( mMapSettings.
outputDpi() );
95 tempPainter.reset(
new QPainter( printer.get() ) );
96 destPainter = tempPainter.get();
101 #endif // ! QT_NO_PRINTER 107 img = QImage( mMapSettings.
outputSize(), QImage::Format_ARGB32 );
114 img.setDotsPerMeterX( 1000 * mMapSettings.
outputDpi() / 25.4 );
115 img.setDotsPerMeterY( 1000 * mMapSettings.
outputDpi() / 25.4 );
117 tempPainter.reset(
new QPainter( &img ) );
118 destPainter = tempPainter.get();
127 mJob->renderSynchronously();
130 mJob.reset(
nullptr );
139 const auto constMDecorations = mDecorations;
142 decoration->render( mMapSettings, context );
145 const auto constMAnnotations = mAnnotations;
151 if ( !annotation || !annotation->isVisible() )
155 if ( annotation->mapLayer() && !mMapSettings.
layers().contains( annotation->mapLayer() ) )
164 if ( annotation->hasFixedMapPosition() )
171 itemX = annotation->relativePosition().x() * mMapSettings.
outputSize().width();
172 itemY = annotation->relativePosition().y() * mMapSettings.
outputSize().height();
175 context.
painter()->translate( itemX, itemY );
177 annotation->render( context );
181 if ( !mFileName.isEmpty() )
185 if ( mForceRaster && mFileFormat == QStringLiteral(
"PDF" ) )
187 #ifndef QT_NO_PRINTER 189 pp.begin( printer.get() );
190 QRectF rect( 0, 0, img.width(), img.height() );
191 pp.drawImage( rect, img, rect );
196 #endif // !QT_NO_PRINTER 198 else if ( mFileFormat != QStringLiteral(
"PDF" ) )
200 bool success = img.save( mFileName, mFileFormat.toLocal8Bit().data() );
207 if ( mSaveWorldFile )
209 QFileInfo info = QFileInfo( mFileName );
212 QString outputSuffix = info.suffix();
213 QString worldFileName = info.absolutePath() +
'/' + info.baseName() +
'.' 214 + outputSuffix.at( 0 ) + outputSuffix.at( info.suffix().size() - 1 ) +
'w';
215 QFile worldFile( worldFileName );
217 if ( worldFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
219 QTextStream stream( &worldFile );
231 qDeleteAll( mAnnotations );
232 mAnnotations.clear();
Job implementation that renders everything sequentially using a custom painter.
bool run() override
Performs the task's operation.
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.
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.
Abstract base class for long running background tasks.
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
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.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
void cancel() override
Notifies the task that it should terminate.
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.