Skip to content

Commit

Permalink
[processing] Fix atlas export relying on predefined scale for atlas-c…
Browse files Browse the repository at this point in the history
…ontrolled map items (#42081)

* [processing] Fix atlas export relying on predefined scale for atlas-controlled map items

* De-duplicate code, add a QgsLayoutUtils::predefinedScale function
  • Loading branch information
nirvn committed Mar 8, 2021
1 parent c91043a commit 22d0826
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 34 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/layout/qgslayoututils.sip.in
Expand Up @@ -251,6 +251,13 @@ the largest "pretty" number possible.
Returns ``True`` if an ``item`` is a clipping item for another layout item.

.. versionadded:: 3.16
%End

static QVector< double > predefinedScales( const QgsLayout *layout );
%Docstring
Returns a list of predefined scales associated with a ``layout``.

.. versionadded:: 3.20
%End

};
Expand Down
3 changes: 3 additions & 0 deletions src/analysis/processing/qgsalgorithmlayoutatlastoimage.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgslayout.h"
#include "qgslayoutatlas.h"
#include "qgslayoutitemmap.h"
#include "qgslayoututils.h"
#include "qgsprintlayout.h"
#include "qgsprocessingoutputs.h"
#include "qgslayoutexporter.h"
Expand Down Expand Up @@ -201,6 +202,8 @@ QVariantMap QgsLayoutAtlasToImageAlgorithm::processAlgorithm( const QVariantMap
else
settings.flags = settings.flags & ~QgsLayoutRenderContext::FlagAntialiasing;

settings.predefinedMapScales = QgsLayoutUtils::predefinedScales( layout.get() );

const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
if ( layers.size() > 0 )
{
Expand Down
3 changes: 3 additions & 0 deletions src/analysis/processing/qgsalgorithmlayoutatlastopdf.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgslayout.h"
#include "qgslayoutatlas.h"
#include "qgslayoutitemmap.h"
#include "qgslayoututils.h"
#include "qgsprintlayout.h"
#include "qgsprocessingoutputs.h"
#include "qgslayoutexporter.h"
Expand Down Expand Up @@ -184,6 +185,8 @@ QVariantMap QgsLayoutAtlasToPdfAlgorithm::processAlgorithm( const QVariantMap &p
else
settings.flags = settings.flags & ~QgsLayoutRenderContext::FlagDisableTiledRasterLayerRenders;

settings.predefinedMapScales = QgsLayoutUtils::predefinedScales( layout.get() );

const QList< QgsMapLayer * > layers = parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context );
if ( layers.size() > 0 )
{
Expand Down
40 changes: 8 additions & 32 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -38,6 +38,7 @@
#include "qgslayoutimageexportoptionsdialog.h"
#include "qgslayoutpdfexportoptionsdialog.h"
#include "qgslayoutitemmap.h"
#include "qgslayoututils.h"
#include "qgsprintlayout.h"
#include "qgsmapcanvas.h"
#include "qgsrendercontext.h"
Expand Down Expand Up @@ -2078,7 +2079,7 @@ void QgsLayoutDesignerDialog::print()

QgsLayoutExporter::PrintExportSettings printSettings;
printSettings.rasterizeWholeImage = mLayout->customProperty( QStringLiteral( "rasterize" ), false ).toBool();
printSettings.predefinedMapScales = predefinedScales();
printSettings.predefinedMapScales = QgsLayoutUtils::predefinedScales( mLayout );

QgsProxyProgressTask *proxyTask = new QgsProxyProgressTask( tr( "Printing “%1”" ).arg( mMasterLayout->name() ) );
QgsApplication::taskManager()->addTask( proxyTask );
Expand Down Expand Up @@ -2665,7 +2666,7 @@ void QgsLayoutDesignerDialog::printAtlas()

QgsLayoutExporter::PrintExportSettings printSettings;
printSettings.rasterizeWholeImage = mLayout->customProperty( QStringLiteral( "rasterize" ), false ).toBool();
printSettings.predefinedMapScales = predefinedScales();
printSettings.predefinedMapScales = QgsLayoutUtils::predefinedScales( mLayout );

QString error;
std::unique_ptr< QgsFeedback > feedback = std::make_unique< QgsFeedback >();
Expand Down Expand Up @@ -3762,7 +3763,7 @@ void QgsLayoutDesignerDialog::printReport()
QgsLayoutExporter::PrintExportSettings printSettings;
if ( mLayout )
printSettings.rasterizeWholeImage = mLayout->customProperty( QStringLiteral( "rasterize" ), false ).toBool();
printSettings.predefinedMapScales = predefinedScales();
printSettings.predefinedMapScales = QgsLayoutUtils::predefinedScales( mLayout );

QString error;
std::unique_ptr< QgsFeedback > feedback = std::make_unique< QgsFeedback >();
Expand Down Expand Up @@ -4285,7 +4286,7 @@ bool QgsLayoutDesignerDialog::getRasterExportSettings( QgsLayoutExporter::ImageE
settings.imageSize = imageSize;
}
settings.generateWorldFile = imageDlg.generateWorldFile();
settings.predefinedMapScales = predefinedScales();
settings.predefinedMapScales = QgsLayoutUtils::predefinedScales( mLayout );
settings.flags |= QgsLayoutRenderContext::FlagUseAdvancedEffects;
if ( imageDlg.antialiasing() )
settings.flags |= QgsLayoutRenderContext::FlagAntialiasing;
Expand Down Expand Up @@ -4400,7 +4401,7 @@ bool QgsLayoutDesignerDialog::getSvgExportSettings( QgsLayoutExporter::SvgExport
settings.exportMetadata = includeMetadata;
settings.textRenderFormat = textRenderFormat;
settings.simplifyGeometries = simplify;
settings.predefinedMapScales = predefinedScales();
settings.predefinedMapScales = QgsLayoutUtils::predefinedScales( mLayout );

if ( disableRasterTiles )
settings.flags = settings.flags | QgsLayoutRenderContext::FlagDisableTiledRasterLayerRenders;
Expand Down Expand Up @@ -4526,7 +4527,7 @@ bool QgsLayoutDesignerDialog::getPdfExportSettings( QgsLayoutExporter::PdfExport
settings.useOgcBestPracticeFormatGeoreferencing = useOgcBestPracticeFormat;
settings.useIso32000ExtensionFormatGeoreferencing = !useOgcBestPracticeFormat;
settings.exportThemes = exportThemes;
settings.predefinedMapScales = predefinedScales();
settings.predefinedMapScales = QgsLayoutUtils::predefinedScales( mLayout );

if ( disableRasterTiles )
settings.flags = settings.flags | QgsLayoutRenderContext::FlagDisableTiledRasterLayerRenders;
Expand Down Expand Up @@ -4638,32 +4639,7 @@ void QgsLayoutDesignerDialog::atlasFeatureChanged( const QgsFeature &feature )
void QgsLayoutDesignerDialog::loadPredefinedScalesFromProject()
{
if ( mLayout )
mLayout->renderContext().setPredefinedScales( predefinedScales() );
}

QVector<double> QgsLayoutDesignerDialog::predefinedScales() const
{
QgsProject *project = mMasterLayout->layoutProject();
// first look at project's scales
QVector< double > projectMapScales = project->viewSettings()->mapScales();
bool hasProjectScales( project->viewSettings()->useProjectScales() );
if ( !hasProjectScales || projectMapScales.isEmpty() )
{
// default to global map tool scales
QgsSettings settings;
QString scalesStr( settings.value( QStringLiteral( "Map/scales" ), Qgis::defaultProjectScales() ).toString() );
const QStringList scales = scalesStr.split( ',' );

for ( const QString &scale : scales )
{
QStringList parts( scale.split( ':' ) );
if ( parts.size() == 2 )
{
projectMapScales.push_back( parts[1].toDouble() );
}
}
}
return projectMapScales;
mLayout->renderContext().setPredefinedScales( QgsLayoutUtils::predefinedScales( mLayout ) );
}

QgsLayoutAtlas *QgsLayoutDesignerDialog::atlas()
Expand Down
1 change: 0 additions & 1 deletion src/app/layout/qgslayoutdesignerdialog.h
Expand Up @@ -546,7 +546,6 @@ class QgsLayoutDesignerDialog: public QMainWindow, public Ui::QgsLayoutDesignerB

//! Load predefined scales from the project's properties
void loadPredefinedScalesFromProject();
QVector<double> predefinedScales() const;

QgsLayoutAtlas *atlas();

Expand Down
31 changes: 30 additions & 1 deletion src/core/layout/qgslayoututils.cpp
Expand Up @@ -17,8 +17,11 @@

#include "qgslayoututils.h"
#include "qgslayout.h"
#include "qgsrendercontext.h"
#include "qgslayoutitemmap.h"
#include "qgsprojectviewsettings.h"
#include "qgsrendercontext.h"
#include "qgssettings.h"

#include <QStyleOptionGraphicsItem>
#include <QPainter>
#include <cmath>
Expand Down Expand Up @@ -496,3 +499,29 @@ double QgsLayoutUtils::mmToPoints( const double mmSize )
//conversion to points based on 1 point = 1/72 inch
return ( mmSize / 0.3527 );
}

QVector< double > QgsLayoutUtils::predefinedScales( const QgsLayout *layout )
{
QVector< double > mapScales;
if ( layout->project() )
mapScales = layout->project()->viewSettings()->mapScales();

bool hasProjectScales( layout->project()->viewSettings()->useProjectScales() );
if ( !hasProjectScales || mapScales.isEmpty() )
{
// default to global map tool scales
QgsSettings settings;
QString scalesStr( settings.value( QStringLiteral( "Map/scales" ), Qgis::defaultProjectScales() ).toString() );
const QStringList scales = scalesStr.split( ',' );
for ( const QString &scale : scales )
{
QStringList parts( scale.split( ':' ) );
if ( parts.size() == 2 )
{
mapScales.push_back( parts[1].toDouble() );
}
}
}

return mapScales;
}
7 changes: 7 additions & 0 deletions src/core/layout/qgslayoututils.h
Expand Up @@ -239,6 +239,13 @@ class CORE_EXPORT QgsLayoutUtils
*/
static bool itemIsAClippingSource( const QgsLayoutItem *item );

/**
* Returns a list of predefined scales associated with a \a layout.
*
* \since QGIS 3.20
*/
static QVector< double > predefinedScales( const QgsLayout *layout );

private:

//! Scale factor for upscaling fontsize and downscaling painter
Expand Down

0 comments on commit 22d0826

Please sign in to comment.