Skip to content

Commit

Permalink
Add the option to print maps as rasters
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@10162 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Feb 13, 2009
1 parent 1f046b8 commit 537a9fa
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -460,7 +460,6 @@ void QgsComposer::on_mActionPrint_activated( void )
printer.setColorMode( QPrinter::Color );

QPrintDialog printDialog( &printer );

if ( printDialog.exec() == QDialog::Accepted )
{
//set user-defined resolution
Expand All @@ -475,6 +474,24 @@ void QgsComposer::on_mActionPrint_activated( void )

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();
Expand All @@ -485,8 +502,8 @@ void QgsComposer::on_mActionPrint_activated( void )
QRectF paperRectPixel = printer.pageRect( QPrinter::DevicePixel );
mComposition->render( &p, paperRectPixel, paperRectMM );
#endif
}
mComposition->setPlotStyle( savedPlotStyle );

QApplication::restoreOverrideCursor();
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/app/composer/qgscompositionwidget.cpp
Expand Up @@ -44,6 +44,16 @@ QgsCompositionWidget::QgsCompositionWidget( QWidget* parent, QgsComposition* c )
//read printout resolution from composition
mResolutionLineEdit->setText( QString::number( mComposition->printResolution() ) );

//print as raster
if(mComposition->printAsRaster())
{
mPrintAsRasterCheckBox->setCheckState(Qt::Checked);
}
else
{
mPrintAsRasterCheckBox->setCheckState(Qt::Unchecked);
}

//snap grid
if ( mComposition->snapToGridEnabled() )
{
Expand Down Expand Up @@ -367,6 +377,23 @@ void QgsCompositionWidget::on_mResolutionLineEdit_textChanged( const QString& te
}
}

void QgsCompositionWidget::on_mPrintAsRasterCheckBox_stateChanged(int state)
{
if(!mComposition)
{
return;
}

if(state == Qt::Checked)
{
mComposition->setPrintAsRaster(true);
}
else
{
mComposition->setPrintAsRaster(false);
}
}

void QgsCompositionWidget::on_mSnapToGridCheckBox_stateChanged( int state )
{
if ( mComposition )
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscompositionwidget.h
Expand Up @@ -45,6 +45,7 @@ class QgsCompositionWidget: public QWidget, private Ui::QgsCompositionWidgetBase
void on_mPaperWidthLineEdit_editingFinished();
void on_mPaperHeightLineEdit_editingFinished();
void on_mResolutionLineEdit_textChanged( const QString& text );
void on_mPrintAsRasterCheckBox_stateChanged(int state);

void on_mSnapToGridCheckBox_stateChanged( int state );
void on_mResolutionSpinBox_valueChanged( double d );
Expand Down
18 changes: 16 additions & 2 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -34,11 +34,18 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ): QGraphicsScene( 0
mPaperItem->setZValue( 0 );
mPrintResolution = 300; //hardcoded default
loadGridAppearanceSettings();

//mPrintAsRaster
QSettings s;
mPrintAsRaster = s.value("/qgis/composerPrintAsRaster", false).toBool();
}

QgsComposition::QgsComposition(): QGraphicsScene( 0 ), mMapRenderer( 0 ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 )
QgsComposition::QgsComposition(): QGraphicsScene( 0 ), mMapRenderer( 0 ), mPlotStyle( QgsComposition::Preview ), mPaperItem( 0 ), mPrintAsRaster(false), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 )
{
saveGridAppearanceSettings();
loadGridAppearanceSettings();
//mPrintAsRaster
QSettings s;
mPrintAsRaster = s.value("/qgis/composerPrintAsRaster", false).toBool();
}

QgsComposition::~QgsComposition()
Expand Down Expand Up @@ -697,6 +704,13 @@ void QgsComposition::setGridStyle( GridStyle s )
saveGridAppearanceSettings();
}

void QgsComposition::setPrintAsRaster(bool enabled)
{
mPrintAsRaster = enabled;
QSettings s;
s.setValue("/qgis/composerPrintAsRaster", QVariant(mPrintAsRaster));
}

void QgsComposition::loadGridAppearanceSettings()
{
//read grid style, grid color and pen width from settings
Expand Down
6 changes: 6 additions & 0 deletions src/core/composer/qgscomposition.h
Expand Up @@ -99,6 +99,9 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
int printResolution() const {return mPrintResolution;}
void setPrintResolution( int dpi ) {mPrintResolution = dpi;}

bool printAsRaster() const {return mPrintAsRaster;}
void setPrintAsRaster(bool enabled);

/**Returns pointer to map renderer of qgis map canvas*/
QgsMapRenderer* mapRenderer() {return mMapRenderer;}

Expand Down Expand Up @@ -162,6 +165,9 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
/**Dpi for printout*/
int mPrintResolution;

/**Flag if map should be printed as a raster (via QImage). False by default*/
bool mPrintAsRaster;

/**Parameters for snap to grid function*/
bool mSnapToGrid;
double mSnapGridResolution;
Expand Down
9 changes: 8 additions & 1 deletion src/ui/qgscompositionwidgetbase.ui
Expand Up @@ -6,7 +6,7 @@
<x>0</x>
<y>0</y>
<width>301</width>
<height>735</height>
<height>761</height>
</rect>
</property>
<property name="sizePolicy" >
Expand Down Expand Up @@ -276,6 +276,13 @@
<item row="2" column="1" >
<widget class="QLineEdit" name="mResolutionLineEdit" />
</item>
<item row="3" column="0" >
<widget class="QCheckBox" name="mPrintAsRasterCheckBox" >
<property name="text" >
<string>Print as raster</string>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11" />
Expand Down

0 comments on commit 537a9fa

Please sign in to comment.