Skip to content

Commit 2f0969e

Browse files
committedDec 17, 2017
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
1 parent 0f9aaf4 commit 2f0969e

File tree

8 files changed

+74
-8
lines changed

8 files changed

+74
-8
lines changed
 

‎python/core/layout/qgslayoutexporter.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ to render sections of pages rather than full pages.
9595

9696
struct ImageExportSettings
9797
{
98+
ImageExportSettings();
99+
%Docstring
100+
Constructor for ImageExportSettings
101+
%End
102+
98103
double dpi;
99104
%Docstring
100105
Resolution to export layout at
@@ -141,6 +146,11 @@ Resolution to export layout at
141146
exported images.
142147
%End
143148

149+
QgsLayoutContext::Flags flags;
150+
%Docstring
151+
Layout context flags, which control how the export will be created.
152+
%End
153+
144154
};
145155

146156
ExportResult exportToImage( const QString &filePath, const ImageExportSettings &settings );

‎src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,13 +1450,15 @@ void QgsLayoutDesignerDialog::exportToRaster()
14501450
int marginRight = mLayout->customProperty( QStringLiteral( "imageCropMarginRight" ), 0 ).toInt();
14511451
int marginBottom = mLayout->customProperty( QStringLiteral( "imageCropMarginBottom" ), 0 ).toInt();
14521452
int marginLeft = mLayout->customProperty( QStringLiteral( "imageCropMarginLeft" ), 0 ).toInt();
1453+
bool antialias = mLayout->customProperty( QStringLiteral( "imageAntialias" ), true ).toBool();
14531454

14541455
QgsLayoutImageExportOptionsDialog imageDlg( this );
14551456
imageDlg.setImageSize( maxPageSize );
14561457
imageDlg.setResolution( dpi );
14571458
imageDlg.setCropToContents( cropToContents );
14581459
imageDlg.setCropMargins( marginTop, marginRight, marginBottom, marginLeft );
14591460
imageDlg.setGenerateWorldFile( mLayout->customProperty( QStringLiteral( "exportWorldFile" ), false ).toBool() );
1461+
imageDlg.setAntialiasing( antialias );
14601462

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

1499+
mLayout->setCustomProperty( QStringLiteral( "imageAntialias" ), imageDlg.antialiasing() );
1500+
14971501
mView->setPaintingEnabled( false );
14981502

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

15111518
switch ( exporter.exportToImage( fileNExt.first, settings ) )
15121519
{

‎src/app/layout/qgslayoutimageexportoptionsdialog.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ bool QgsLayoutImageExportOptionsDialog::generateWorldFile() const
105105
return mGenerateWorldFile->isChecked();
106106
}
107107

108+
void QgsLayoutImageExportOptionsDialog::setAntialiasing( bool antialias )
109+
{
110+
mAntialiasingCheckBox->setChecked( antialias );
111+
}
112+
113+
bool QgsLayoutImageExportOptionsDialog::antialiasing() const
114+
{
115+
return mAntialiasingCheckBox->isChecked();
116+
}
117+
108118
void QgsLayoutImageExportOptionsDialog::getCropMargins( int &topMargin, int &rightMargin, int &bottomMargin, int &leftMargin ) const
109119
{
110120
topMargin = mTopMarginSpinBox->value();

‎src/app/layout/qgslayoutimageexportoptionsdialog.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ class QgsLayoutImageExportOptionsDialog: public QDialog, private Ui::QgsLayoutIm
9696
*/
9797
bool generateWorldFile() const;
9898

99+
/**
100+
* Sets whether antialiasing should be used in the export.
101+
* \see antialiasing()
102+
*/
103+
void setAntialiasing( bool antialias );
104+
105+
/**
106+
* Returns whether antialiasing should be used in the export.
107+
* \see setAntialiasing()
108+
*/
109+
bool antialiasing() const;
110+
99111
/**
100112
* Fetches the current crop to contents margin values, in pixels.
101113
* \param topMargin destination for top margin

‎src/core/layout/qgslayoutexporter.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ void QgsLayoutExporter::renderRegion( QPainter *painter, const QRectF &region )
8585
setSnapLinesVisible( false );
8686
#endif
8787

88+
painter->setRenderHint( QPainter::Antialiasing, mLayout->context().flags() & QgsLayoutContext::FlagAntialiasing );
89+
8890
mLayout->render( painter, QRectF( 0, 0, paintDevice->width(), paintDevice->height() ), region );
8991

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

134136
///@cond PRIVATE
135-
class LayoutDpiRestorer
137+
class LayoutContextSettingsRestorer
136138
{
137139
public:
138140

139-
LayoutDpiRestorer( QgsLayout *layout )
141+
LayoutContextSettingsRestorer( QgsLayout *layout )
140142
: mLayout( layout )
141-
, mPreviousSetting( layout->context().dpi() )
143+
, mPreviousDpi( layout->context().dpi() )
144+
, mPreviousFlags( layout->context().flags() )
142145
{
143146
}
144147

145-
~LayoutDpiRestorer()
148+
~LayoutContextSettingsRestorer()
146149
{
147-
mLayout->context().setDpi( mPreviousSetting );
150+
mLayout->context().setDpi( mPreviousDpi );
151+
mLayout->context().setFlags( mPreviousFlags );
148152
}
149153

150154
private:
151155
QgsLayout *mLayout = nullptr;
152-
double mPreviousSetting = 0;
156+
double mPreviousDpi = 0;
157+
QgsLayoutContext::Flags mPreviousFlags = 0;
153158
};
154159
///@endcond PRIVATE
155160

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

171-
LayoutDpiRestorer dpiRestorer( mLayout );
176+
LayoutContextSettingsRestorer dpiRestorer( mLayout );
172177
( void )dpiRestorer;
173178
mLayout->context().setDpi( settings.dpi );
179+
mLayout->context().setFlags( settings.flags );
174180

175181
QList< int > pages;
176182
if ( settings.pages.empty() )

‎src/core/layout/qgslayoutexporter.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "qgis_core.h"
2020
#include "qgsmargins.h"
21+
#include "qgslayoutcontext.h"
2122
#include <QPointer>
2223
#include <QSize>
2324
#include <QRectF>
@@ -111,6 +112,11 @@ class CORE_EXPORT QgsLayoutExporter
111112
//! Contains settings relating to exporting layouts to raster images
112113
struct ImageExportSettings
113114
{
115+
//! Constructor for ImageExportSettings
116+
ImageExportSettings()
117+
: flags( QgsLayoutContext::FlagAntialiasing | QgsLayoutContext::FlagUseAdvancedEffects )
118+
{}
119+
114120
//! Resolution to export layout at
115121
double dpi;
116122

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

164+
/**
165+
* Layout context flags, which control how the export will be created.
166+
*/
167+
QgsLayoutContext::Flags flags = 0;
168+
158169
};
159170

160171
/**

‎src/core/layout/qgslayoutitemmap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ QgsMapSettings QgsLayoutItemMap::mapSettings( const QgsRectangle &extent, QSizeF
990990

991991
// layout-specific overrides of flags
992992
jobMapSettings.setFlag( QgsMapSettings::ForceVectorOutput, true ); // force vector output (no caching of marker images etc.)
993-
jobMapSettings.setFlag( QgsMapSettings::Antialiasing, true );
993+
jobMapSettings.setFlag( QgsMapSettings::Antialiasing, mLayout->context().flags() & QgsLayoutContext::FlagAntialiasing );
994994
jobMapSettings.setFlag( QgsMapSettings::DrawEditingInfo, false );
995995
jobMapSettings.setFlag( QgsMapSettings::DrawSelection, false );
996996
jobMapSettings.setFlag( QgsMapSettings::UseAdvancedEffects, mLayout->context().flags() & QgsLayoutContext::FlagUseAdvancedEffects );

‎src/ui/layout/qgslayoutimageexportoptions.ui

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@
244244
</property>
245245
</widget>
246246
</item>
247+
<item>
248+
<widget class="QCheckBox" name="mAntialiasingCheckBox">
249+
<property name="toolTip">
250+
<string>If unchecked, the generated images will not be antialiased</string>
251+
</property>
252+
<property name="text">
253+
<string>Enable antialiasing</string>
254+
</property>
255+
</widget>
256+
</item>
247257
<item>
248258
<spacer name="verticalSpacer">
249259
<property name="orientation">

0 commit comments

Comments
 (0)
Please sign in to comment.