Skip to content
Permalink
Browse files

[processing] Fix atlas export relying on predefined scale for atlas-c…

…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

(cherry picked from commit 22d0826)
  • Loading branch information
nirvn authored and nyalldawson committed Mar 19, 2021
1 parent 163ee62 commit d38c70b9edd514b13dfb463b36a3f6bce1a0c0b3
@@ -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

};
@@ -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"
@@ -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 )
{
@@ -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"
@@ -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 )
{
@@ -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"
@@ -2030,7 +2031,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 );
@@ -2617,7 +2618,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 = qgis::make_unique< QgsFeedback >();
@@ -3714,7 +3715,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 = qgis::make_unique< QgsFeedback >();
@@ -4237,7 +4238,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;
@@ -4352,7 +4353,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;
@@ -4478,7 +4479,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;
@@ -4590,32 +4591,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()
@@ -544,7 +544,6 @@ class QgsLayoutDesignerDialog: public QMainWindow, public Ui::QgsLayoutDesignerB

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

QgsLayoutAtlas *atlas();

@@ -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>
@@ -500,3 +503,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;
}
@@ -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

0 comments on commit d38c70b

Please sign in to comment.