@@ -5740,173 +5740,34 @@ void QgisApp::updateFilterLegend()
57405740
57415741void QgisApp::saveMapAsImage ()
57425742{
5743- QList< QgsMapDecoration * > decorations;
5744- QString activeDecorations;
5743+ QList< QgsDecorationItem * > decorations;
57455744 Q_FOREACH ( QgsDecorationItem *decoration, mDecorationItems )
57465745 {
57475746 if ( decoration->enabled () )
57485747 {
57495748 decorations << decoration;
5750- if ( activeDecorations.isEmpty () )
5751- activeDecorations = decoration->name ().toLower ();
5752- else
5753- activeDecorations += QString ( " , %1" ).arg ( decoration->name ().toLower () );
57545749 }
57555750 }
57565751
5757- QgsMapSaveDialog dlg ( this , mMapCanvas , activeDecorations );
5758- if ( !dlg.exec () )
5759- return ;
5760-
5761- QPair< QString, QString> fileNameAndFilter = QgsGuiUtils::getSaveAsImageName ( this , tr ( " Choose a file name to save the map image as" ) );
5762- if ( fileNameAndFilter.first != QLatin1String ( " " ) )
5763- {
5764- QgsMapSettings ms = QgsMapSettings ();
5765- dlg.applyMapSettings ( ms );
5766-
5767- QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask ( ms, fileNameAndFilter.first , fileNameAndFilter.second );
5768-
5769- if ( dlg.drawAnnotations () )
5770- {
5771- mapRendererTask->addAnnotations ( QgsProject::instance ()->annotationManager ()->annotations () );
5772- }
5773-
5774- if ( dlg.drawDecorations () )
5775- {
5776- mapRendererTask->addDecorations ( decorations );
5777- }
5778-
5779- mapRendererTask->setSaveWorldFile ( dlg.saveWorldFile () );
5780-
5781- connect ( mapRendererTask, &QgsMapRendererTask::renderingComplete, this , [ = ]
5782- {
5783- messageBar ()->pushSuccess ( tr ( " Save as image" ), tr ( " Successfully saved map to image" ) );
5784- } );
5785- connect ( mapRendererTask, &QgsMapRendererTask::errorOccurred, this , [ = ]( int error )
5786- {
5787- switch ( error )
5788- {
5789- case QgsMapRendererTask::ImageAllocationFail:
5790- {
5791- messageBar ()->pushWarning ( tr ( " Save as image" ), tr ( " Could not allocate required memory for image" ) );
5792- break ;
5793- }
5794- case QgsMapRendererTask::ImageSaveFail:
5795- {
5796- messageBar ()->pushWarning ( tr ( " Save as image" ), tr ( " Could not save the image to file" ) );
5797- break ;
5798- }
5799- }
5800- } );
5801-
5802- QgsApplication::taskManager ()->addTask ( mapRendererTask );
5803- }
5804-
5752+ QgsMapSaveDialog *dlg = new QgsMapSaveDialog ( this , mMapCanvas , decorations, QgsProject::instance ()->annotationManager ()->annotations () );
5753+ dlg->setAttribute ( Qt::WA_DeleteOnClose );
5754+ dlg->show ();
58055755} // saveMapAsImage
58065756
58075757void QgisApp::saveMapAsPdf ()
58085758{
5809- QList< QgsMapDecoration * > decorations;
5810- QString activeDecorations;
5759+ QList< QgsDecorationItem * > decorations;
58115760 Q_FOREACH ( QgsDecorationItem *decoration, mDecorationItems )
58125761 {
58135762 if ( decoration->enabled () )
58145763 {
58155764 decorations << decoration;
5816- if ( activeDecorations.isEmpty () )
5817- activeDecorations = decoration->name ().toLower ();
5818- else
5819- activeDecorations += QString ( " , %1" ).arg ( decoration->name ().toLower () );
5820- }
5821- }
5822-
5823- QgsMapSaveDialog dlg ( this , mMapCanvas , activeDecorations, QgsMapSaveDialog::Pdf );
5824- if ( !dlg.exec () )
5825- return ;
5826-
5827- QgsSettings settings;
5828- QString lastUsedDir = settings.value ( QStringLiteral ( " UI/lastSaveAsImageDir" ), QDir::homePath () ).toString ();
5829- QString fileName = QFileDialog::getSaveFileName ( this , tr ( " Save map as" ), lastUsedDir, tr ( " PDF Format" ) + " (*.pdf *.PDF)" );
5830- if ( !fileName.isEmpty () )
5831- {
5832- QgsMapSettings ms = QgsMapSettings ();
5833- dlg.applyMapSettings ( ms );
5834-
5835- QPrinter *printer = new QPrinter ();
5836- printer->setOutputFileName ( fileName );
5837- printer->setOutputFormat ( QPrinter::PdfFormat );
5838- printer->setOrientation ( QPrinter::Portrait );
5839- // paper size needs to be given in millimeters in order to be able to set a resolution to pass onto the map renderer
5840- printer->setPaperSize ( dlg.size () * 25.4 / dlg.dpi (), QPrinter::Millimeter );
5841- printer->setPageMargins ( 0 , 0 , 0 , 0 , QPrinter::Millimeter );
5842- printer->setResolution ( dlg.dpi () );
5843-
5844- QPainter *p = new QPainter ();
5845- QImage *image = nullptr ;
5846- if ( dlg.saveAsRaster () )
5847- {
5848- image = new QImage ( dlg.size (), QImage::Format_ARGB32 );
5849- if ( image->isNull () )
5850- {
5851- messageBar ()->pushWarning ( tr ( " Save as PDF" ), tr ( " Could not allocate required memory for image" ) );
5852- delete p;
5853- delete image;
5854- delete printer;
5855-
5856- return ;
5857- }
5858-
5859- image->setDotsPerMeterX ( 1000 * dlg.dpi () / 25.4 );
5860- image->setDotsPerMeterY ( 1000 * dlg.dpi () / 25.4 );
5861- p->begin ( image );
58625765 }
5863- else
5864- {
5865- p->begin ( printer );
5866- }
5867-
5868- QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask ( ms, p );
5869-
5870- if ( dlg.drawAnnotations () )
5871- {
5872- mapRendererTask->addAnnotations ( QgsProject::instance ()->annotationManager ()->annotations () );
5873- }
5874-
5875- if ( dlg.drawDecorations () )
5876- {
5877- mapRendererTask->addDecorations ( decorations );
5878- }
5879-
5880- mapRendererTask->setSaveWorldFile ( dlg.saveWorldFile () );
5881-
5882- connect ( mapRendererTask, &QgsMapRendererTask::renderingComplete, this , [ this , p, image, printer ]
5883- {
5884- p->end ();
5885-
5886- if ( image )
5887- {
5888- QPainter pp;
5889- pp.begin ( printer );
5890- QRectF rect ( 0 , 0 , image->width (), image->height () );
5891- pp.drawImage ( rect, *image, rect );
5892- pp.end ();
5893- }
5894-
5895- messageBar ()->pushSuccess ( tr ( " Save as PDF" ), tr ( " Successfully saved map to PDF" ) );
5896- delete p;
5897- delete image;
5898- delete printer;
5899- } );
5900- connect ( mapRendererTask, &QgsMapRendererTask::errorOccurred, this , [ this , p, image, printer ]( int )
5901- {
5902- delete p;
5903- delete image;
5904- delete printer;
5905- } );
5906-
5907- QgsApplication::taskManager ()->addTask ( mapRendererTask );
59085766 }
59095767
5768+ QgsMapSaveDialog *dlg = new QgsMapSaveDialog ( this , mMapCanvas , decorations, QgsProject::instance ()->annotationManager ()->annotations (), QgsMapSaveDialog::Pdf );
5769+ dlg->setAttribute ( Qt::WA_DeleteOnClose );
5770+ dlg->show ();
59105771} // saveMapAsPdf
59115772
59125773// overloaded version of the above function
0 commit comments