Skip to content
Permalink
Browse files
Deduplicate code in save as image / save as PDF
  • Loading branch information
nirvn committed May 13, 2017
1 parent e01b7ef commit 75e95024f43fac0bf13511d2dd1cb9137c4a8b89
Showing with 39 additions and 24 deletions.
  1. +2 −20 src/app/qgisapp.cpp
  2. +32 −4 src/app/qgsmapsavedialog.cpp
  3. +5 −0 src/app/qgsmapsavedialog.h
@@ -5807,13 +5807,7 @@ void QgisApp::saveMapAsImage()
if ( fileNameAndFilter.first != QLatin1String( "" ) )
{
QgsMapSettings ms = QgsMapSettings();
ms.setDestinationCrs( QgsProject::instance()->crs() );
ms.setExtent( dlg.extent() );
ms.setOutputSize( dlg.size() );
ms.setOutputDpi( dlg.dpi() );
ms.setBackgroundColor( mMapCanvas->canvasColor() );
ms.setRotation( mMapCanvas->rotation() );
ms.setLayers( mMapCanvas->layers() );
dlg.applyMapSettings( ms );

QgsMapRendererTask *mapRendererTask = new QgsMapRendererTask( ms, fileNameAndFilter.first, fileNameAndFilter.second );

@@ -5881,19 +5875,7 @@ void QgisApp::saveMapAsPdf()
if ( !fileName.isEmpty() )
{
QgsMapSettings ms = QgsMapSettings();

ms.setFlag( QgsMapSettings::ForceVectorOutput, true ); // force vector output (no caching of marker images etc.)
ms.setFlag( QgsMapSettings::Antialiasing, true );
ms.setFlag( QgsMapSettings::DrawEditingInfo, false );
ms.setFlag( QgsMapSettings::DrawSelection, false );

ms.setDestinationCrs( QgsProject::instance()->crs() );
ms.setExtent( dlg.extent() );
ms.setOutputSize( dlg.size() );
ms.setOutputDpi( dlg.dpi() );
ms.setBackgroundColor( mMapCanvas->canvasColor() );
ms.setRotation( mMapCanvas->rotation() );
ms.setLayers( mMapCanvas->layers() );
dlg.applyMapSettings( ms );

QPrinter *printer = new QPrinter();
printer->setOutputFileName( fileName );
@@ -23,6 +23,7 @@
#include "qgsextentgroupbox.h"
#include "qgsmapsettings.h"
#include "qgsmapsettingsutils.h"
#include "qgssettings.h"

#include <QCheckBox>
#include <QSpinBox>
@@ -32,11 +33,13 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();

QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, const QString &activeDecorations, DialogType type )
: QDialog( parent )
, mDialogType( type )
, mMapCanvas( mapCanvas )
{
setupUi( this );

// Use unrotated visible extent to insure output size and scale matches canvas
QgsMapSettings ms = mapCanvas->mapSettings();
QgsMapSettings ms = mMapCanvas->mapSettings();
ms.setRotation( 0 );
mExtent = ms.visibleExtent();
mDpi = ms.outputDpi();
@@ -49,7 +52,7 @@ QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, co
mExtentGroupBox->setOutputExtentFromCurrent();

mScaleWidget->setScale( 1 / ms.scale() );
mScaleWidget->setMapCanvas( mapCanvas );
mScaleWidget->setMapCanvas( mMapCanvas );
mScaleWidget->setShowCurrentScaleButton( true );

mDrawDecorations->setText( tr( "Draw active decorations: %1" ).arg( !activeDecorations.isEmpty() ? activeDecorations : tr( "none" ) ) );
@@ -62,11 +65,11 @@ QgsMapSaveDialog::QgsMapSaveDialog( QWidget *parent, QgsMapCanvas *mapCanvas, co

updateOutputSize();

if ( type == QgsMapSaveDialog::Pdf )
if ( mDialogType == QgsMapSaveDialog::Pdf )
{
mSaveWorldFile->setVisible( false );

QStringList layers = QgsMapSettingsUtils::containsAdvancedEffects( mapCanvas->mapSettings() );
QStringList layers = QgsMapSettingsUtils::containsAdvancedEffects( mMapCanvas->mapSettings() );
if ( !layers.isEmpty() )
{
// Limit number of items to avoid extreme dialog height
@@ -184,3 +187,28 @@ bool QgsMapSaveDialog::saveAsRaster() const
{
return mSaveAsRaster->isChecked();
}

void QgsMapSaveDialog::applyMapSettings( QgsMapSettings &mapSettings )
{
QgsSettings settings;

if ( mDialogType == QgsMapSaveDialog::Pdf )
{
mapSettings.setFlag( QgsMapSettings::Antialiasing, true ); // hardcode antialising when saving as PDF
}
else
{
mapSettings.setFlag( QgsMapSettings::Antialiasing, settings.value( QStringLiteral( "qgis/enable_anti_aliasing" ), true ).toBool() );
}
mapSettings.setFlag( QgsMapSettings::ForceVectorOutput, true ); // force vector output (no caching of marker images etc.)
mapSettings.setFlag( QgsMapSettings::DrawEditingInfo, false );
mapSettings.setFlag( QgsMapSettings::DrawSelection, true );

mapSettings.setDestinationCrs( mMapCanvas->mapSettings().destinationCrs() );
mapSettings.setExtent( extent() );
mapSettings.setOutputSize( size() );
mapSettings.setOutputDpi( dpi() );
mapSettings.setBackgroundColor( mMapCanvas->canvasColor() );
mapSettings.setRotation( mMapCanvas->rotation() );
mapSettings.setLayers( mMapCanvas->layers() );
}
@@ -68,6 +68,9 @@ class APP_EXPORT QgsMapSaveDialog: public QDialog, private Ui::QgsMapSaveDialog
//! returns whether the map will be rasterized
bool saveAsRaster() const;

//! configure a map settings object
void applyMapSettings( QgsMapSettings &mapSettings );

private:

void updateDpi( int dpi );
@@ -77,6 +80,8 @@ class APP_EXPORT QgsMapSaveDialog: public QDialog, private Ui::QgsMapSaveDialog
void updateScale( double scale );
void updateOutputSize();

DialogType mDialogType;
QgsMapCanvas *mMapCanvas;
QgsRectangle mExtent;
int mDpi;
QSize mSize;

0 comments on commit 75e9502

Please sign in to comment.