Skip to content
Permalink
Browse files

add export to PDF to composer

git-svn-id: http://svn.osgeo.org/qgis/trunk@11165 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Jul 23, 2009
1 parent a4c7bb7 commit fe1e32436059fde44d21c882bf624e84021cc05f
Binary file not shown.
Binary file not shown.
@@ -239,6 +239,7 @@ void QgsComposer::setupTheme()
mActionSaveAsTemplate->setIcon( QgisApp::getThemeIcon( "/mActionFileSaveAs.png" ) );
mActionExportAsImage->setIcon( QgisApp::getThemeIcon( "/mActionExportMapServer.png" ) );
mActionExportAsSVG->setIcon( QgisApp::getThemeIcon( "/mActionSaveAsSVG.png" ) );
mActionExportAsPDF->setIcon( QgisApp::getThemeIcon( "/mActionSaveAsPDF.png" ) );
mActionPrint->setIcon( QgisApp::getThemeIcon( "/mActionFilePrint.png" ) );
mActionZoomAll->setIcon( QgisApp::getThemeIcon( "/mActionZoomFullExtent.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()
{
if ( !mComposition )
{
QPrinter printer;

QPrintDialog printDialog( &printer );
if ( printDialog.exec() != QDialog::Accepted )
return;

print( printer );
}

void QgsComposer::print( QPrinter &printer )
{
if( !mComposition )
return;
}

if ( containsWMSLayer() )
{
showWMSPrintingWarning();
}

QPrinter printer;

//try to set most of the print dialog settings based on composer properties
if ( mComposition->paperHeight() > mComposition->paperWidth() )
{
@@ -454,58 +490,50 @@ void QgsComposer::on_mActionPrint_triggered()
}

//set resolution based on composer setting


printer.setFullPage( true );
printer.setColorMode( QPrinter::Color );

QPrintDialog printDialog( &printer );
if ( printDialog.exec() == QDialog::Accepted )
{
//set user-defined resolution
if ( mComposition )
{
printer.setResolution( mComposition->printResolution() );
}
QPainter p( &printer );
//set user-defined resolution
printer.setResolution( mComposition->printResolution() );

QgsComposition::PlotStyle savedPlotStyle = mComposition->plotStyle();
mComposition->setPlotStyle( QgsComposition::Print );
QPainter p( &printer );

QApplication::setOverrideCursor( Qt::BusyCursor );
QgsComposition::PlotStyle savedPlotStyle = mComposition->plotStyle();
mComposition->setPlotStyle( QgsComposition::Print );

if ( mComposition->printAsRaster() )
{
//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
{
QApplication::setOverrideCursor( Qt::BusyCursor );

if ( mComposition->printAsRaster() )
{
//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 );
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 );
//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();
}
mComposition->setPlotStyle( savedPlotStyle );
QApplication::restoreOverrideCursor();
}

void QgsComposer::on_mActionExportAsImage_triggered()
@@ -119,6 +119,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Print as SVG
void on_mActionExportAsSVG_triggered();

//! Print as PDF
void on_mActionExportAsPDF_triggered();

//! Select item
void on_mActionSelectMoveItem_triggered();

@@ -243,6 +246,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Changes elements that are not suitable for this project
void cleanupAfterTemplateRead();

//! Print to a printer object
void print( QPrinter &printer );

//! Writes state under DOM element
void writeXML( QDomNode& parentNode, QDomDocument& doc );
//! Pointer to composer view
@@ -232,6 +232,7 @@
<addaction name="mActionLoadFromTemplate" />
<addaction name="mActionSaveAsTemplate" />
<addaction name="mActionExportAsImage" />
<addaction name="mActionExportAsPDF" />
<addaction name="mActionExportAsSVG" />
<addaction name="mActionPrint" />
<addaction name="separator" />
@@ -334,6 +335,14 @@
<string>Export as Image...</string>
</property>
</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" >
<property name="icon" >
<iconset>../themes/default/mActionSaveAsSVG.png</iconset>

0 comments on commit fe1e324

Please sign in to comment.
You can’t perform that action at this time.