mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-09 00:08:52 -04:00
add export to PDF to composer
git-svn-id: http://svn.osgeo.org/qgis/trunk@11165 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
parent
a4c7bb7e3b
commit
fe1e324360
BIN
images/themes/classic/mActionSaveAsPDF.png
Normal file
BIN
images/themes/classic/mActionSaveAsPDF.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
BIN
images/themes/gis/mActionSaveAsPDF.png
Normal file
BIN
images/themes/gis/mActionSaveAsPDF.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 968 B |
@ -239,6 +239,7 @@ void QgsComposer::setupTheme()
|
|||||||
mActionSaveAsTemplate->setIcon( QgisApp::getThemeIcon( "/mActionFileSaveAs.png" ) );
|
mActionSaveAsTemplate->setIcon( QgisApp::getThemeIcon( "/mActionFileSaveAs.png" ) );
|
||||||
mActionExportAsImage->setIcon( QgisApp::getThemeIcon( "/mActionExportMapServer.png" ) );
|
mActionExportAsImage->setIcon( QgisApp::getThemeIcon( "/mActionExportMapServer.png" ) );
|
||||||
mActionExportAsSVG->setIcon( QgisApp::getThemeIcon( "/mActionSaveAsSVG.png" ) );
|
mActionExportAsSVG->setIcon( QgisApp::getThemeIcon( "/mActionSaveAsSVG.png" ) );
|
||||||
|
mActionExportAsPDF->setIcon( QgisApp::getThemeIcon( "/mActionSaveAsPDF.png" ) );
|
||||||
mActionPrint->setIcon( QgisApp::getThemeIcon( "/mActionFilePrint.png" ) );
|
mActionPrint->setIcon( QgisApp::getThemeIcon( "/mActionFilePrint.png" ) );
|
||||||
mActionZoomAll->setIcon( QgisApp::getThemeIcon( "/mActionZoomFullExtent.png" ) );
|
mActionZoomAll->setIcon( QgisApp::getThemeIcon( "/mActionZoomFullExtent.png" ) );
|
||||||
mActionZoomIn->setIcon( QgisApp::getThemeIcon( "/mActionZoomIn.png" ) );
|
mActionZoomIn->setIcon( QgisApp::getThemeIcon( "/mActionZoomIn.png" ) );
|
||||||
@ -429,20 +430,55 @@ void QgsComposer::on_mActionRefreshView_triggered()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsComposer::on_mActionExportAsPDF_triggered()
|
||||||
|
{
|
||||||
|
QSettings myQSettings; // where we keep last used filter in persistant state
|
||||||
|
QString myLastUsedFile = myQSettings.value( "/UI/lastSaveAsPdfFile", "qgis.pdf" ).toString();
|
||||||
|
QFileInfo file( myLastUsedFile );
|
||||||
|
QFileDialog *myQFileDialog = new QFileDialog( this, tr( "Choose a file name to save the map as" ),
|
||||||
|
file.path(), tr( "PDF Format" ) + " (*.pdf *PDF)" );
|
||||||
|
myQFileDialog->selectFile( file.fileName() );
|
||||||
|
myQFileDialog->setFileMode( QFileDialog::AnyFile );
|
||||||
|
myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );
|
||||||
|
|
||||||
|
int result = myQFileDialog->exec();
|
||||||
|
raise();
|
||||||
|
if ( result != QDialog::Accepted ) return;
|
||||||
|
|
||||||
|
QString myOutputFileNameQString = myQFileDialog->selectedFiles().first();
|
||||||
|
if ( myOutputFileNameQString == "" ) return;
|
||||||
|
|
||||||
|
myQSettings.setValue( "/UI/lastSaveAsPdfFile", myOutputFileNameQString );
|
||||||
|
|
||||||
|
QPrinter printer;
|
||||||
|
|
||||||
|
printer.setOutputFormat( QPrinter::PdfFormat );
|
||||||
|
printer.setOutputFileName( myOutputFileNameQString );
|
||||||
|
|
||||||
|
print( printer );
|
||||||
|
}
|
||||||
|
|
||||||
void QgsComposer::on_mActionPrint_triggered()
|
void QgsComposer::on_mActionPrint_triggered()
|
||||||
{
|
{
|
||||||
if ( !mComposition )
|
QPrinter printer;
|
||||||
{
|
|
||||||
|
QPrintDialog printDialog( &printer );
|
||||||
|
if ( printDialog.exec() != QDialog::Accepted )
|
||||||
|
return;
|
||||||
|
|
||||||
|
print( printer );
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsComposer::print( QPrinter &printer )
|
||||||
|
{
|
||||||
|
if( !mComposition )
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if ( containsWMSLayer() )
|
if ( containsWMSLayer() )
|
||||||
{
|
{
|
||||||
showWMSPrintingWarning();
|
showWMSPrintingWarning();
|
||||||
}
|
}
|
||||||
|
|
||||||
QPrinter printer;
|
|
||||||
|
|
||||||
//try to set most of the print dialog settings based on composer properties
|
//try to set most of the print dialog settings based on composer properties
|
||||||
if ( mComposition->paperHeight() > mComposition->paperWidth() )
|
if ( mComposition->paperHeight() > mComposition->paperWidth() )
|
||||||
{
|
{
|
||||||
@ -454,58 +490,50 @@ void QgsComposer::on_mActionPrint_triggered()
|
|||||||
}
|
}
|
||||||
|
|
||||||
//set resolution based on composer setting
|
//set resolution based on composer setting
|
||||||
|
|
||||||
|
|
||||||
printer.setFullPage( true );
|
printer.setFullPage( true );
|
||||||
printer.setColorMode( QPrinter::Color );
|
printer.setColorMode( QPrinter::Color );
|
||||||
|
|
||||||
QPrintDialog printDialog( &printer );
|
//set user-defined resolution
|
||||||
if ( printDialog.exec() == QDialog::Accepted )
|
printer.setResolution( mComposition->printResolution() );
|
||||||
|
|
||||||
|
QPainter p( &printer );
|
||||||
|
|
||||||
|
QgsComposition::PlotStyle savedPlotStyle = mComposition->plotStyle();
|
||||||
|
mComposition->setPlotStyle( QgsComposition::Print );
|
||||||
|
|
||||||
|
QApplication::setOverrideCursor( Qt::BusyCursor );
|
||||||
|
|
||||||
|
if ( mComposition->printAsRaster() )
|
||||||
{
|
{
|
||||||
//set user-defined resolution
|
//print out via QImage, code copied from on_mActionExportAsImage_activated
|
||||||
if ( mComposition )
|
int width = ( int )( mComposition->printResolution() * mComposition->paperWidth() / 25.4 );
|
||||||
{
|
int height = ( int )( mComposition-> printResolution() * mComposition->paperHeight() / 25.4 );
|
||||||
printer.setResolution( mComposition->printResolution() );
|
QImage image( QSize( width, height ), QImage::Format_ARGB32 );
|
||||||
}
|
image.setDotsPerMeterX( mComposition->printResolution() / 25.4 * 1000 );
|
||||||
QPainter p( &printer );
|
image.setDotsPerMeterY( mComposition->printResolution() / 25.4 * 1000 );
|
||||||
|
image.fill( 0 );
|
||||||
QgsComposition::PlotStyle savedPlotStyle = mComposition->plotStyle();
|
QPainter imagePainter( &image );
|
||||||
mComposition->setPlotStyle( QgsComposition::Print );
|
QRectF sourceArea( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
|
||||||
|
QRectF targetArea( 0, 0, width, height );
|
||||||
QApplication::setOverrideCursor( Qt::BusyCursor );
|
mComposition->render( &imagePainter, targetArea, sourceArea );
|
||||||
|
imagePainter.end();
|
||||||
if ( mComposition->printAsRaster() )
|
p.drawImage( targetArea, image, targetArea );
|
||||||
{
|
|
||||||
//print out via QImage, code copied from on_mActionExportAsImage_activated
|
|
||||||
int width = ( int )( mComposition->printResolution() * mComposition->paperWidth() / 25.4 );
|
|
||||||
int height = ( int )( mComposition-> printResolution() * mComposition->paperHeight() / 25.4 );
|
|
||||||
QImage image( QSize( width, height ), QImage::Format_ARGB32 );
|
|
||||||
image.setDotsPerMeterX( mComposition->printResolution() / 25.4 * 1000 );
|
|
||||||
image.setDotsPerMeterY( mComposition->printResolution() / 25.4 * 1000 );
|
|
||||||
image.fill( 0 );
|
|
||||||
QPainter imagePainter( &image );
|
|
||||||
QRectF sourceArea( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
|
|
||||||
QRectF targetArea( 0, 0, width, height );
|
|
||||||
mComposition->render( &imagePainter, targetArea, sourceArea );
|
|
||||||
imagePainter.end();
|
|
||||||
p.drawImage( targetArea, image, targetArea );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
#if QT_VERSION < 0x040400
|
|
||||||
QRectF paperRect( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
|
|
||||||
QRect pageRect = printer.pageRect();
|
|
||||||
mComposition->render( &p, pageRect, paperRect );
|
|
||||||
#else
|
|
||||||
//better in case of custom page size, but only possible with Qt>=4.4.0
|
|
||||||
QRectF paperRectMM = printer.pageRect( QPrinter::Millimeter );
|
|
||||||
QRectF paperRectPixel = printer.pageRect( QPrinter::DevicePixel );
|
|
||||||
mComposition->render( &p, paperRectPixel, paperRectMM );
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
mComposition->setPlotStyle( savedPlotStyle );
|
|
||||||
QApplication::restoreOverrideCursor();
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#if QT_VERSION < 0x040400
|
||||||
|
QRectF paperRect( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
|
||||||
|
QRect pageRect = printer.pageRect();
|
||||||
|
mComposition->render( &p, pageRect, paperRect );
|
||||||
|
#else
|
||||||
|
//better in case of custom page size, but only possible with Qt>=4.4.0
|
||||||
|
QRectF paperRectMM = printer.pageRect( QPrinter::Millimeter );
|
||||||
|
QRectF paperRectPixel = printer.pageRect( QPrinter::DevicePixel );
|
||||||
|
mComposition->render( &p, paperRectPixel, paperRectMM );
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
mComposition->setPlotStyle( savedPlotStyle );
|
||||||
|
QApplication::restoreOverrideCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QgsComposer::on_mActionExportAsImage_triggered()
|
void QgsComposer::on_mActionExportAsImage_triggered()
|
||||||
|
@ -119,6 +119,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
|
|||||||
//! Print as SVG
|
//! Print as SVG
|
||||||
void on_mActionExportAsSVG_triggered();
|
void on_mActionExportAsSVG_triggered();
|
||||||
|
|
||||||
|
//! Print as PDF
|
||||||
|
void on_mActionExportAsPDF_triggered();
|
||||||
|
|
||||||
//! Select item
|
//! Select item
|
||||||
void on_mActionSelectMoveItem_triggered();
|
void on_mActionSelectMoveItem_triggered();
|
||||||
|
|
||||||
@ -243,6 +246,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
|
|||||||
//! Changes elements that are not suitable for this project
|
//! Changes elements that are not suitable for this project
|
||||||
void cleanupAfterTemplateRead();
|
void cleanupAfterTemplateRead();
|
||||||
|
|
||||||
|
//! Print to a printer object
|
||||||
|
void print( QPrinter &printer );
|
||||||
|
|
||||||
//! Writes state under DOM element
|
//! Writes state under DOM element
|
||||||
void writeXML( QDomNode& parentNode, QDomDocument& doc );
|
void writeXML( QDomNode& parentNode, QDomDocument& doc );
|
||||||
//! Pointer to composer view
|
//! Pointer to composer view
|
||||||
|
@ -232,6 +232,7 @@
|
|||||||
<addaction name="mActionLoadFromTemplate" />
|
<addaction name="mActionLoadFromTemplate" />
|
||||||
<addaction name="mActionSaveAsTemplate" />
|
<addaction name="mActionSaveAsTemplate" />
|
||||||
<addaction name="mActionExportAsImage" />
|
<addaction name="mActionExportAsImage" />
|
||||||
|
<addaction name="mActionExportAsPDF" />
|
||||||
<addaction name="mActionExportAsSVG" />
|
<addaction name="mActionExportAsSVG" />
|
||||||
<addaction name="mActionPrint" />
|
<addaction name="mActionPrint" />
|
||||||
<addaction name="separator" />
|
<addaction name="separator" />
|
||||||
@ -334,6 +335,14 @@
|
|||||||
<string>Export as Image...</string>
|
<string>Export as Image...</string>
|
||||||
</property>
|
</property>
|
||||||
</action>
|
</action>
|
||||||
|
<action name="mActionExportAsPDF" >
|
||||||
|
<property name="icon" >
|
||||||
|
<iconset>../themes/default/mActionSaveAsPDF.png</iconset>
|
||||||
|
</property>
|
||||||
|
<property name="text" >
|
||||||
|
<string>Export as PDF...</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
<action name="mActionExportAsSVG" >
|
<action name="mActionExportAsSVG" >
|
||||||
<property name="icon" >
|
<property name="icon" >
|
||||||
<iconset>../themes/default/mActionSaveAsSVG.png</iconset>
|
<iconset>../themes/default/mActionSaveAsSVG.png</iconset>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user