@@ -239,6 +239,7 @@ void QgsComposer::setupTheme()
239
239
mActionSaveAsTemplate ->setIcon ( QgisApp::getThemeIcon ( " /mActionFileSaveAs.png" ) );
240
240
mActionExportAsImage ->setIcon ( QgisApp::getThemeIcon ( " /mActionExportMapServer.png" ) );
241
241
mActionExportAsSVG ->setIcon ( QgisApp::getThemeIcon ( " /mActionSaveAsSVG.png" ) );
242
+ mActionExportAsPDF ->setIcon ( QgisApp::getThemeIcon ( " /mActionSaveAsPDF.png" ) );
242
243
mActionPrint ->setIcon ( QgisApp::getThemeIcon ( " /mActionFilePrint.png" ) );
243
244
mActionZoomAll ->setIcon ( QgisApp::getThemeIcon ( " /mActionZoomFullExtent.png" ) );
244
245
mActionZoomIn ->setIcon ( QgisApp::getThemeIcon ( " /mActionZoomIn.png" ) );
@@ -429,20 +430,55 @@ void QgsComposer::on_mActionRefreshView_triggered()
429
430
}
430
431
}
431
432
433
+ void QgsComposer::on_mActionExportAsPDF_triggered ()
434
+ {
435
+ QSettings myQSettings; // where we keep last used filter in persistant state
436
+ QString myLastUsedFile = myQSettings.value ( " /UI/lastSaveAsPdfFile" , " qgis.pdf" ).toString ();
437
+ QFileInfo file ( myLastUsedFile );
438
+ QFileDialog *myQFileDialog = new QFileDialog ( this , tr ( " Choose a file name to save the map as" ),
439
+ file.path (), tr ( " PDF Format" ) + " (*.pdf *PDF)" );
440
+ myQFileDialog->selectFile ( file.fileName () );
441
+ myQFileDialog->setFileMode ( QFileDialog::AnyFile );
442
+ myQFileDialog->setAcceptMode ( QFileDialog::AcceptSave );
443
+
444
+ int result = myQFileDialog->exec ();
445
+ raise ();
446
+ if ( result != QDialog::Accepted ) return ;
447
+
448
+ QString myOutputFileNameQString = myQFileDialog->selectedFiles ().first ();
449
+ if ( myOutputFileNameQString == " " ) return ;
450
+
451
+ myQSettings.setValue ( " /UI/lastSaveAsPdfFile" , myOutputFileNameQString );
452
+
453
+ QPrinter printer;
454
+
455
+ printer.setOutputFormat ( QPrinter::PdfFormat );
456
+ printer.setOutputFileName ( myOutputFileNameQString );
457
+
458
+ print ( printer );
459
+ }
460
+
432
461
void QgsComposer::on_mActionPrint_triggered ()
433
462
{
434
- if ( !mComposition )
435
- {
463
+ QPrinter printer;
464
+
465
+ QPrintDialog printDialog ( &printer );
466
+ if ( printDialog.exec () != QDialog::Accepted )
467
+ return ;
468
+
469
+ print ( printer );
470
+ }
471
+
472
+ void QgsComposer::print ( QPrinter &printer )
473
+ {
474
+ if ( !mComposition )
436
475
return ;
437
- }
438
476
439
477
if ( containsWMSLayer () )
440
478
{
441
479
showWMSPrintingWarning ();
442
480
}
443
481
444
- QPrinter printer;
445
-
446
482
// try to set most of the print dialog settings based on composer properties
447
483
if ( mComposition ->paperHeight () > mComposition ->paperWidth () )
448
484
{
@@ -454,58 +490,50 @@ void QgsComposer::on_mActionPrint_triggered()
454
490
}
455
491
456
492
// set resolution based on composer setting
457
-
458
-
459
493
printer.setFullPage ( true );
460
494
printer.setColorMode ( QPrinter::Color );
461
495
462
- QPrintDialog printDialog ( &printer );
463
- if ( printDialog.exec () == QDialog::Accepted )
464
- {
465
- // set user-defined resolution
466
- if ( mComposition )
467
- {
468
- printer.setResolution ( mComposition ->printResolution () );
469
- }
470
- QPainter p ( &printer );
496
+ // set user-defined resolution
497
+ printer.setResolution ( mComposition ->printResolution () );
471
498
472
- QgsComposition::PlotStyle savedPlotStyle = mComposition ->plotStyle ();
473
- mComposition ->setPlotStyle ( QgsComposition::Print );
499
+ QPainter p ( &printer );
474
500
475
- QApplication::setOverrideCursor ( Qt::BusyCursor );
501
+ QgsComposition::PlotStyle savedPlotStyle = mComposition ->plotStyle ();
502
+ mComposition ->setPlotStyle ( QgsComposition::Print );
476
503
477
- if ( mComposition ->printAsRaster () )
478
- {
479
- // print out via QImage, code copied from on_mActionExportAsImage_activated
480
- int width = ( int )( mComposition ->printResolution () * mComposition ->paperWidth () / 25.4 );
481
- int height = ( int )( mComposition -> printResolution () * mComposition ->paperHeight () / 25.4 );
482
- QImage image ( QSize ( width, height ), QImage::Format_ARGB32 );
483
- image.setDotsPerMeterX ( mComposition ->printResolution () / 25.4 * 1000 );
484
- image.setDotsPerMeterY ( mComposition ->printResolution () / 25.4 * 1000 );
485
- image.fill ( 0 );
486
- QPainter imagePainter ( &image );
487
- QRectF sourceArea ( 0 , 0 , mComposition ->paperWidth (), mComposition ->paperHeight () );
488
- QRectF targetArea ( 0 , 0 , width, height );
489
- mComposition ->render ( &imagePainter, targetArea, sourceArea );
490
- imagePainter.end ();
491
- p.drawImage ( targetArea, image, targetArea );
492
- }
493
- else
494
- {
504
+ QApplication::setOverrideCursor ( Qt::BusyCursor );
505
+
506
+ if ( mComposition ->printAsRaster () )
507
+ {
508
+ // print out via QImage, code copied from on_mActionExportAsImage_activated
509
+ int width = ( int )( mComposition ->printResolution () * mComposition ->paperWidth () / 25.4 );
510
+ int height = ( int )( mComposition -> printResolution () * mComposition ->paperHeight () / 25.4 );
511
+ QImage image ( QSize ( width, height ), QImage::Format_ARGB32 );
512
+ image.setDotsPerMeterX ( mComposition ->printResolution () / 25.4 * 1000 );
513
+ image.setDotsPerMeterY ( mComposition ->printResolution () / 25.4 * 1000 );
514
+ image.fill ( 0 );
515
+ QPainter imagePainter ( &image );
516
+ QRectF sourceArea ( 0 , 0 , mComposition ->paperWidth (), mComposition ->paperHeight () );
517
+ QRectF targetArea ( 0 , 0 , width, height );
518
+ mComposition ->render ( &imagePainter, targetArea, sourceArea );
519
+ imagePainter.end ();
520
+ p.drawImage ( targetArea, image, targetArea );
521
+ }
522
+ else
523
+ {
495
524
#if QT_VERSION < 0x040400
496
- QRectF paperRect ( 0 , 0 , mComposition ->paperWidth (), mComposition ->paperHeight () );
497
- QRect pageRect = printer.pageRect ();
498
- mComposition ->render ( &p, pageRect, paperRect );
525
+ QRectF paperRect ( 0 , 0 , mComposition ->paperWidth (), mComposition ->paperHeight () );
526
+ QRect pageRect = printer.pageRect ();
527
+ mComposition ->render ( &p, pageRect, paperRect );
499
528
#else
500
- // better in case of custom page size, but only possible with Qt>=4.4.0
501
- QRectF paperRectMM = printer.pageRect ( QPrinter::Millimeter );
502
- QRectF paperRectPixel = printer.pageRect ( QPrinter::DevicePixel );
503
- mComposition ->render ( &p, paperRectPixel, paperRectMM );
529
+ // better in case of custom page size, but only possible with Qt>=4.4.0
530
+ QRectF paperRectMM = printer.pageRect ( QPrinter::Millimeter );
531
+ QRectF paperRectPixel = printer.pageRect ( QPrinter::DevicePixel );
532
+ mComposition ->render ( &p, paperRectPixel, paperRectMM );
504
533
#endif
505
- }
506
- mComposition ->setPlotStyle ( savedPlotStyle );
507
- QApplication::restoreOverrideCursor ();
508
534
}
535
+ mComposition ->setPlotStyle ( savedPlotStyle );
536
+ QApplication::restoreOverrideCursor ();
509
537
}
510
538
511
539
void QgsComposer::on_mActionExportAsImage_triggered ()
0 commit comments