Skip to content
Permalink
Browse files

Expose antialiasing option in image export dialog

Allows for creating non-antialiased images from layouts. Note that
some layout item types do not correctly respect this setting, but
at least map items do and the API is in place for them to be
fixed later.

Fixes #9281
  • Loading branch information
nyalldawson committed Dec 11, 2017
1 parent 0f9aaf4 commit 2f0969e2bd5b5e74e6c18412d27f86a8f7c1072a
@@ -95,6 +95,11 @@ to render sections of pages rather than full pages.

struct ImageExportSettings
{
ImageExportSettings();
%Docstring
Constructor for ImageExportSettings
%End

double dpi;
%Docstring
Resolution to export layout at
@@ -141,6 +146,11 @@ Resolution to export layout at
exported images.
%End

QgsLayoutContext::Flags flags;
%Docstring
Layout context flags, which control how the export will be created.
%End

};

ExportResult exportToImage( const QString &filePath, const ImageExportSettings &settings );
@@ -1450,13 +1450,15 @@ void QgsLayoutDesignerDialog::exportToRaster()
int marginRight = mLayout->customProperty( QStringLiteral( "imageCropMarginRight" ), 0 ).toInt();
int marginBottom = mLayout->customProperty( QStringLiteral( "imageCropMarginBottom" ), 0 ).toInt();
int marginLeft = mLayout->customProperty( QStringLiteral( "imageCropMarginLeft" ), 0 ).toInt();
bool antialias = mLayout->customProperty( QStringLiteral( "imageAntialias" ), true ).toBool();

QgsLayoutImageExportOptionsDialog imageDlg( this );
imageDlg.setImageSize( maxPageSize );
imageDlg.setResolution( dpi );
imageDlg.setCropToContents( cropToContents );
imageDlg.setCropMargins( marginTop, marginRight, marginBottom, marginLeft );
imageDlg.setGenerateWorldFile( mLayout->customProperty( QStringLiteral( "exportWorldFile" ), false ).toBool() );
imageDlg.setAntialiasing( antialias );

#if 0 //TODO
QgsAtlasComposition *atlasMap = &mComposition->atlasComposition();
@@ -1494,6 +1496,8 @@ void QgsLayoutDesignerDialog::exportToRaster()
mLayout->setCustomProperty( QStringLiteral( "imageCropMarginBottom" ), marginBottom );
mLayout->setCustomProperty( QStringLiteral( "imageCropMarginLeft" ), marginLeft );

mLayout->setCustomProperty( QStringLiteral( "imageAntialias" ), imageDlg.antialiasing() );

mView->setPaintingEnabled( false );

QgsLayoutExporter exporter( mLayout );
@@ -1507,6 +1511,9 @@ void QgsLayoutDesignerDialog::exportToRaster()
settings.imageSize = QSize( imageDlg.imageWidth(), imageDlg.imageHeight() );
}
settings.generateWorldFile = imageDlg.generateWorldFile();
settings.flags = QgsLayoutContext::FlagUseAdvancedEffects;
if ( imageDlg.antialiasing() )
settings.flags |= QgsLayoutContext::FlagAntialiasing;

switch ( exporter.exportToImage( fileNExt.first, settings ) )
{
@@ -105,6 +105,16 @@ bool QgsLayoutImageExportOptionsDialog::generateWorldFile() const
return mGenerateWorldFile->isChecked();
}

void QgsLayoutImageExportOptionsDialog::setAntialiasing( bool antialias )
{
mAntialiasingCheckBox->setChecked( antialias );
}

bool QgsLayoutImageExportOptionsDialog::antialiasing() const
{
return mAntialiasingCheckBox->isChecked();
}

void QgsLayoutImageExportOptionsDialog::getCropMargins( int &topMargin, int &rightMargin, int &bottomMargin, int &leftMargin ) const
{
topMargin = mTopMarginSpinBox->value();
@@ -96,6 +96,18 @@ class QgsLayoutImageExportOptionsDialog: public QDialog, private Ui::QgsLayoutIm
*/
bool generateWorldFile() const;

/**
* Sets whether antialiasing should be used in the export.
* \see antialiasing()
*/
void setAntialiasing( bool antialias );

/**
* Returns whether antialiasing should be used in the export.
* \see setAntialiasing()
*/
bool antialiasing() const;

/**
* Fetches the current crop to contents margin values, in pixels.
* \param topMargin destination for top margin
@@ -85,6 +85,8 @@ void QgsLayoutExporter::renderRegion( QPainter *painter, const QRectF &region )
setSnapLinesVisible( false );
#endif

painter->setRenderHint( QPainter::Antialiasing, mLayout->context().flags() & QgsLayoutContext::FlagAntialiasing );

mLayout->render( painter, QRectF( 0, 0, paintDevice->width(), paintDevice->height() ), region );

#if 0 // TODO
@@ -132,24 +134,27 @@ QImage QgsLayoutExporter::renderRegionToImage( const QRectF &region, QSize image
}

///@cond PRIVATE
class LayoutDpiRestorer
class LayoutContextSettingsRestorer
{
public:

LayoutDpiRestorer( QgsLayout *layout )
LayoutContextSettingsRestorer( QgsLayout *layout )
: mLayout( layout )
, mPreviousSetting( layout->context().dpi() )
, mPreviousDpi( layout->context().dpi() )
, mPreviousFlags( layout->context().flags() )
{
}

~LayoutDpiRestorer()
~LayoutContextSettingsRestorer()
{
mLayout->context().setDpi( mPreviousSetting );
mLayout->context().setDpi( mPreviousDpi );
mLayout->context().setFlags( mPreviousFlags );
}

private:
QgsLayout *mLayout = nullptr;
double mPreviousSetting = 0;
double mPreviousDpi = 0;
QgsLayoutContext::Flags mPreviousFlags = 0;
};
///@endcond PRIVATE

@@ -168,9 +173,10 @@ QgsLayoutExporter::ExportResult QgsLayoutExporter::exportToImage( const QString
QString baseName = fi.baseName();
QString extension = fi.completeSuffix();

LayoutDpiRestorer dpiRestorer( mLayout );
LayoutContextSettingsRestorer dpiRestorer( mLayout );
( void )dpiRestorer;
mLayout->context().setDpi( settings.dpi );
mLayout->context().setFlags( settings.flags );

QList< int > pages;
if ( settings.pages.empty() )
@@ -18,6 +18,7 @@

#include "qgis_core.h"
#include "qgsmargins.h"
#include "qgslayoutcontext.h"
#include <QPointer>
#include <QSize>
#include <QRectF>
@@ -111,6 +112,11 @@ class CORE_EXPORT QgsLayoutExporter
//! Contains settings relating to exporting layouts to raster images
struct ImageExportSettings
{
//! Constructor for ImageExportSettings
ImageExportSettings()
: flags( QgsLayoutContext::FlagAntialiasing | QgsLayoutContext::FlagUseAdvancedEffects )
{}

//! Resolution to export layout at
double dpi;

@@ -155,6 +161,11 @@ class CORE_EXPORT QgsLayoutExporter
*/
bool generateWorldFile = false;

/**
* Layout context flags, which control how the export will be created.
*/
QgsLayoutContext::Flags flags = 0;

};

/**
@@ -990,7 +990,7 @@ QgsMapSettings QgsLayoutItemMap::mapSettings( const QgsRectangle &extent, QSizeF

// layout-specific overrides of flags
jobMapSettings.setFlag( QgsMapSettings::ForceVectorOutput, true ); // force vector output (no caching of marker images etc.)
jobMapSettings.setFlag( QgsMapSettings::Antialiasing, true );
jobMapSettings.setFlag( QgsMapSettings::Antialiasing, mLayout->context().flags() & QgsLayoutContext::FlagAntialiasing );
jobMapSettings.setFlag( QgsMapSettings::DrawEditingInfo, false );
jobMapSettings.setFlag( QgsMapSettings::DrawSelection, false );
jobMapSettings.setFlag( QgsMapSettings::UseAdvancedEffects, mLayout->context().flags() & QgsLayoutContext::FlagUseAdvancedEffects );
@@ -244,6 +244,16 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="mAntialiasingCheckBox">
<property name="toolTip">
<string>If unchecked, the generated images will not be antialiased</string>
</property>
<property name="text">
<string>Enable antialiasing</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">

0 comments on commit 2f0969e

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