Skip to content

Commit

Permalink
Expose antialiasing option in image export dialog
Browse files Browse the repository at this point in the history
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 17, 2017
1 parent 0f9aaf4 commit 2f0969e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 8 deletions.
10 changes: 10 additions & 0 deletions python/core/layout/qgslayoutexporter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down
7 changes: 7 additions & 0 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 );
Expand All @@ -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 ) )
{
Expand Down
10 changes: 10 additions & 0 deletions src/app/layout/qgslayoutimageexportoptionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions src/app/layout/qgslayoutimageexportoptionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 13 additions & 7 deletions src/core/layout/qgslayoutexporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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() )
Expand Down
11 changes: 11 additions & 0 deletions src/core/layout/qgslayoutexporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "qgis_core.h"
#include "qgsmargins.h"
#include "qgslayoutcontext.h"
#include <QPointer>
#include <QSize>
#include <QRectF>
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitemmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
10 changes: 10 additions & 0 deletions src/ui/layout/qgslayoutimageexportoptions.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit 2f0969e

Please sign in to comment.