Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void QgisApp::saveMapAsImage()
- {
- QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
- if ( myFileNameAndFilter.first != QLatin1String( "" ) )
- {
- QgsMapSettings ms = QgsMapSettings();
- int dpi = 300;
- QSize size = mMapCanvas->size() * (dpi / 90);
- ms.setDestinationCrs( QgsProject::instance()->crs() );
- ms.setExtent( mMapCanvas->extent() );
- ms.setOutputSize( size );
- ms.setOutputDpi( dpi );
- ms.setBackgroundColor( Qt::white );
- ms.setRotation( mMapCanvas->rotation() );
- ms.setLayers( mMapCanvas->layers() );
- QImage img = QImage( size, QImage::Format_ARGB32 );
- QPainter p( &img );
- QgsMapRendererCustomPainterJob r( ms, &p );
- r.renderSynchronously();
- QgsRenderContext context = QgsRenderContext::fromMapSettings( ms );
- context.setPainter( &p );
- QList< QgsAnnotation * > annotations = QgsProject::instance()->annotationManager()->annotations();
- Q_FOREACH ( QgsAnnotation *annotation, annotations )
- {
- if ( !annotation || !annotation->isVisible() )
- {
- continue;
- }
- if ( annotation->mapLayer() && !mMapCanvas->layers().contains( annotation->mapLayer() ) )
- {
- continue;
- }
- context.painter()->save();
- context.painter()->setRenderHint( QPainter::Antialiasing, context.flags() & QgsRenderContext::Antialiasing );
- double itemX, itemY;
- if ( annotation->hasFixedMapPosition() )
- {
- itemX = annotation->mapPosition().x();
- itemY = annotation->mapPosition().y();
- }
- else
- {
- itemX = annotation->relativePosition().x() * size.width();
- itemY = annotation->relativePosition().y() * size.height();
- }
- //TODO: deal with CRS transform?
- context.painter()->translate( itemX, itemY );
- annotation->render( context );
- context.painter()->restore();
- }
- p.end();
- img.save( myFileNameAndFilter.first, myFileNameAndFilter.second.toLocal8Bit().data() );
- statusBar()->showMessage( tr( "Saved map image to %1" ).arg( myFileNameAndFilter.first ) );
- }
- } // saveMapAsImage
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement