@@ -1706,6 +1706,7 @@ void QgisApp::createActions()
1706
1706
connect ( mActionSaveProject , &QAction::triggered, this , &QgisApp::fileSave );
1707
1707
connect ( mActionSaveProjectAs , &QAction::triggered, this , &QgisApp::fileSaveAs );
1708
1708
connect ( mActionSaveMapAsImage , &QAction::triggered, this , [ = ] { saveMapAsImage (); } );
1709
+ connect ( mActionSaveMapAsPdf , &QAction::triggered, this , [ = ] { saveMapAsPdf (); } );
1709
1710
connect ( mActionNewMapCanvas , &QAction::triggered, this , &QgisApp::newMapCanvas );
1710
1711
connect ( mActionNewPrintComposer , &QAction::triggered, this , &QgisApp::newPrintComposer );
1711
1712
connect ( mActionShowComposerManager , &QAction::triggered, this , &QgisApp::showComposerManager );
@@ -2703,6 +2704,7 @@ void QgisApp::setTheme( const QString &themeName )
2703
2704
mActionNewPrintComposer ->setIcon ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mActionNewComposer.svg" ) ) );
2704
2705
mActionShowComposerManager ->setIcon ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mActionComposerManager.svg" ) ) );
2705
2706
mActionSaveMapAsImage ->setIcon ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mActionSaveMapAsImage.svg" ) ) );
2707
+ mActionSaveMapAsPdf ->setIcon ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mActionSaveAsPDF.svg" ) ) );
2706
2708
mActionExit ->setIcon ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mActionFileExit.png" ) ) );
2707
2709
mActionAddOgrLayer ->setIcon ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mActionAddOgrLayer.svg" ) ) );
2708
2710
mActionAddRasterLayer ->setIcon ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mActionAddRasterLayer.svg" ) ) );
@@ -5801,8 +5803,8 @@ void QgisApp::saveMapAsImage()
5801
5803
if ( !dlg.exec () )
5802
5804
return ;
5803
5805
5804
- QPair< QString, QString> myFileNameAndFilter = QgisGui::getSaveAsImageName ( this , tr ( " Choose a file name to save the map image as" ) );
5805
- if ( myFileNameAndFilter .first != QLatin1String ( " " ) )
5806
+ QPair< QString, QString> fileNameAndFilter = QgisGui::getSaveAsImageName ( this , tr ( " Choose a file name to save the map image as" ) );
5807
+ if ( fileNameAndFilter .first != QLatin1String ( " " ) )
5806
5808
{
5807
5809
QgsMapSettings ms = QgsMapSettings ();
5808
5810
ms.setDestinationCrs ( QgsProject::instance ()->crs () );
@@ -5813,7 +5815,7 @@ void QgisApp::saveMapAsImage()
5813
5815
ms.setRotation ( mMapCanvas ->rotation () );
5814
5816
ms.setLayers ( mMapCanvas ->layers () );
5815
5817
5816
- QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask ( ms, myFileNameAndFilter .first , myFileNameAndFilter .second );
5818
+ QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask ( ms, fileNameAndFilter .first , fileNameAndFilter .second );
5817
5819
5818
5820
if ( dlg.drawAnnotations () )
5819
5821
{
@@ -5853,6 +5855,117 @@ void QgisApp::saveMapAsImage()
5853
5855
5854
5856
} // saveMapAsImage
5855
5857
5858
+ void QgisApp::saveMapAsPdf ()
5859
+ {
5860
+ QList< QgsMapDecoration * > decorations;
5861
+ QString activeDecorations;
5862
+ Q_FOREACH ( QgsDecorationItem *decoration, mDecorationItems )
5863
+ {
5864
+ if ( decoration->enabled () )
5865
+ {
5866
+ decorations << decoration;
5867
+ if ( activeDecorations.isEmpty () )
5868
+ activeDecorations = decoration->name ().toLower ();
5869
+ else
5870
+ activeDecorations += QString ( " , %1" ).arg ( decoration->name ().toLower () );
5871
+ }
5872
+ }
5873
+
5874
+ QgsMapSaveDialog dlg ( this , mMapCanvas , activeDecorations, QgsMapSaveDialog::Pdf );
5875
+ if ( !dlg.exec () )
5876
+ return ;
5877
+
5878
+ QgsSettings settings;
5879
+ QString lastUsedDir = settings.value ( QStringLiteral ( " UI/lastSaveAsImageDir" ), QDir::homePath () ).toString ();
5880
+ QString fileName = QFileDialog::getSaveFileName ( this , tr ( " Save map as" ), lastUsedDir, tr ( " PDF Format" ) + " (*.pdf *.PDF)" );
5881
+ if ( !fileName.isEmpty () )
5882
+ {
5883
+ QgsMapSettings ms = QgsMapSettings ();
5884
+ ms.setDestinationCrs ( QgsProject::instance ()->crs () );
5885
+ ms.setExtent ( dlg.extent () );
5886
+ ms.setOutputSize ( dlg.size () );
5887
+ ms.setOutputDpi ( dlg.dpi () );
5888
+ ms.setBackgroundColor ( mMapCanvas ->canvasColor () );
5889
+ ms.setRotation ( mMapCanvas ->rotation () );
5890
+ ms.setLayers ( mMapCanvas ->layers () );
5891
+
5892
+ QPrinter *printer = new QPrinter ();
5893
+ printer->setOutputFileName ( fileName );
5894
+ printer->setOutputFormat ( QPrinter::PdfFormat );
5895
+ printer->setOrientation ( QPrinter::Portrait );
5896
+ printer->setPaperSize ( dlg.size (), QPrinter::DevicePixel );
5897
+ printer->setPageMargins ( 0 , 0 , 0 , 0 , QPrinter::DevicePixel );
5898
+
5899
+ QPainter *p = new QPainter ();
5900
+ QImage *image = nullptr ;
5901
+ if ( dlg.saveAsRaster () )
5902
+ {
5903
+ image = new QImage ( dlg.size (), QImage::Format_ARGB32 );
5904
+ p->begin ( image );
5905
+ }
5906
+ else
5907
+ {
5908
+ p->begin ( printer );
5909
+ }
5910
+
5911
+ QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask ( ms, p );
5912
+
5913
+ if ( dlg.drawAnnotations () )
5914
+ {
5915
+ mapRendererTask->addAnnotations ( QgsProject::instance ()->annotationManager ()->annotations () );
5916
+ }
5917
+
5918
+ if ( dlg.drawDecorations () )
5919
+ {
5920
+ mapRendererTask->addDecorations ( decorations );
5921
+ }
5922
+
5923
+ mapRendererTask->setSaveWorldFile ( dlg.saveWorldFile () );
5924
+
5925
+ connect ( mapRendererTask, &QgsMapRendererTask::renderingComplete, this , [ this , p, image, printer ]
5926
+ {
5927
+ messageBar ()->pushSuccess ( tr ( " Save as image" ), tr ( " Successfully saved map to image" ) );
5928
+ p->end ();
5929
+
5930
+ if ( image )
5931
+ {
5932
+ QPainter pp;
5933
+ pp.begin ( printer );
5934
+ QRectF rect ( 0 , 0 , image->width (), image->height () );
5935
+ pp.drawImage ( rect, *image, rect );
5936
+ pp.end ();
5937
+ }
5938
+
5939
+ delete p;
5940
+ delete image;
5941
+ delete printer;
5942
+ } );
5943
+ connect ( mapRendererTask, &QgsMapRendererTask::errorOccurred, this , [ this , p, image, printer ]( int error )
5944
+ {
5945
+ switch ( error )
5946
+ {
5947
+ case QgsMapRendererTask::ImageAllocationFail:
5948
+ {
5949
+ messageBar ()->pushWarning ( tr ( " Save as image" ), tr ( " Could not allocate required memory for image" ) );
5950
+ break ;
5951
+ }
5952
+ case QgsMapRendererTask::ImageSaveFail:
5953
+ {
5954
+ messageBar ()->pushWarning ( tr ( " Save as image" ), tr ( " Could not save the image to file" ) );
5955
+ break ;
5956
+ }
5957
+ }
5958
+
5959
+ delete p;
5960
+ delete image;
5961
+ delete printer;
5962
+ } );
5963
+
5964
+ QgsApplication::taskManager ()->addTask ( mapRendererTask );
5965
+ }
5966
+
5967
+ } // saveMapAsPdf
5968
+
5856
5969
// overloaded version of the above function
5857
5970
void QgisApp::saveMapAsImage ( const QString &imageFileNameQString, QPixmap *theQPixmap )
5858
5971
{
0 commit comments