Skip to content

Commit 537a9fa

Browse files
author
mhugent
committed
Add the option to print maps as rasters
git-svn-id: http://svn.osgeo.org/qgis/trunk@10162 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 1f046b8 commit 537a9fa

File tree

6 files changed

+77
-5
lines changed

6 files changed

+77
-5
lines changed

src/app/composer/qgscomposer.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,6 @@ void QgsComposer::on_mActionPrint_activated( void )
460460
printer.setColorMode( QPrinter::Color );
461461

462462
QPrintDialog printDialog( &printer );
463-
464463
if ( printDialog.exec() == QDialog::Accepted )
465464
{
466465
//set user-defined resolution
@@ -475,6 +474,24 @@ void QgsComposer::on_mActionPrint_activated( void )
475474

476475
QApplication::setOverrideCursor( Qt::BusyCursor );
477476

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+
{
478495
#if QT_VERSION < 0x040400
479496
QRectF paperRect( 0, 0, mComposition->paperWidth(), mComposition->paperHeight() );
480497
QRect pageRect = printer.pageRect();
@@ -485,8 +502,8 @@ void QgsComposer::on_mActionPrint_activated( void )
485502
QRectF paperRectPixel = printer.pageRect( QPrinter::DevicePixel );
486503
mComposition->render( &p, paperRectPixel, paperRectMM );
487504
#endif
505+
}
488506
mComposition->setPlotStyle( savedPlotStyle );
489-
490507
QApplication::restoreOverrideCursor();
491508
}
492509
}

src/app/composer/qgscompositionwidget.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
4444
//read printout resolution from composition
4545
mResolutionLineEdit->setText( QString::number( mComposition->printResolution() ) );
4646

47+
//print as raster
48+
if(mComposition->printAsRaster())
49+
{
50+
mPrintAsRasterCheckBox->setCheckState(Qt::Checked);
51+
}
52+
else
53+
{
54+
mPrintAsRasterCheckBox->setCheckState(Qt::Unchecked);
55+
}
56+
4757
//snap grid
4858
if ( mComposition->snapToGridEnabled() )
4959
{
@@ -367,6 +377,23 @@ void QgsCompositionWidget::on_mResolutionLineEdit_textChanged( const QString& te
367377
}
368378
}
369379

380+
void QgsCompositionWidget::on_mPrintAsRasterCheckBox_stateChanged(int state)
381+
{
382+
if(!mComposition)
383+
{
384+
return;
385+
}
386+
387+
if(state == Qt::Checked)
388+
{
389+
mComposition->setPrintAsRaster(true);
390+
}
391+
else
392+
{
393+
mComposition->setPrintAsRaster(false);
394+
}
395+
}
396+
370397
void QgsCompositionWidget::on_mSnapToGridCheckBox_stateChanged( int state )
371398
{
372399
if ( mComposition )

src/app/composer/qgscompositionwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
4545
void on_mPaperWidthLineEdit_editingFinished();
4646
void on_mPaperHeightLineEdit_editingFinished();
4747
void on_mResolutionLineEdit_textChanged( const QString& text );
48+
void on_mPrintAsRasterCheckBox_stateChanged(int state);
4849

4950
void on_mSnapToGridCheckBox_stateChanged( int state );
5051
void on_mResolutionSpinBox_valueChanged( double d );

src/core/composer/qgscomposition.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ): QGraphicsScene( 0
3434
mPaperItem->setZValue( 0 );
3535
mPrintResolution = 300; //hardcoded default
3636
loadGridAppearanceSettings();
37+
38+
//mPrintAsRaster
39+
QSettings s;
40+
mPrintAsRaster = s.value("/qgis/composerPrintAsRaster", false).toBool();
3741
}
3842

39-
QgsComposition::QgsComposition(): QGraphicsScene( 0 ), mMapRenderer( 0 ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 )
43+
QgsComposition::QgsComposition(): QGraphicsScene( 0 ), mMapRenderer( 0 ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster(false), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 )
4044
{
41-
saveGridAppearanceSettings();
45+
loadGridAppearanceSettings();
46+
//mPrintAsRaster
47+
QSettings s;
48+
mPrintAsRaster = s.value("/qgis/composerPrintAsRaster", false).toBool();
4249
}
4350

4451
QgsComposition::~QgsComposition()
@@ -697,6 +704,13 @@ void QgsComposition::setGridStyle( GridStyle s )
697704
saveGridAppearanceSettings();
698705
}
699706

707+
void QgsComposition::setPrintAsRaster(bool enabled)
708+
{
709+
mPrintAsRaster = enabled;
710+
QSettings s;
711+
s.setValue("/qgis/composerPrintAsRaster", QVariant(mPrintAsRaster));
712+
}
713+
700714
void QgsComposition::loadGridAppearanceSettings()
701715
{
702716
//read grid style, grid color and pen width from settings

src/core/composer/qgscomposition.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
9999
int printResolution() const {return mPrintResolution;}
100100
void setPrintResolution( int dpi ) {mPrintResolution = dpi;}
101101

102+
bool printAsRaster() const {return mPrintAsRaster;}
103+
void setPrintAsRaster(bool enabled);
104+
102105
/**Returns pointer to map renderer of qgis map canvas*/
103106
QgsMapRenderer* mapRenderer() {return mMapRenderer;}
104107

@@ -162,6 +165,9 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
162165
/**Dpi for printout*/
163166
int mPrintResolution;
164167

168+
/**Flag if map should be printed as a raster (via QImage). False by default*/
169+
bool mPrintAsRaster;
170+
165171
/**Parameters for snap to grid function*/
166172
bool mSnapToGrid;
167173
double mSnapGridResolution;

src/ui/qgscompositionwidgetbase.ui

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<x>0</x>
77
<y>0</y>
88
<width>301</width>
9-
<height>735</height>
9+
<height>761</height>
1010
</rect>
1111
</property>
1212
<property name="sizePolicy" >
@@ -276,6 +276,13 @@
276276
<item row="2" column="1" >
277277
<widget class="QLineEdit" name="mResolutionLineEdit" />
278278
</item>
279+
<item row="3" column="0" >
280+
<widget class="QCheckBox" name="mPrintAsRasterCheckBox" >
281+
<property name="text" >
282+
<string>Print as raster</string>
283+
</property>
284+
</widget>
285+
</item>
279286
</layout>
280287
</widget>
281288
<layoutdefault spacing="6" margin="11" />

0 commit comments

Comments
 (0)