Skip to content
Permalink
Browse files

Port predefined scale handling to layouts

  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 7c086be commit 3ffdda3e304968cbd8e77e3921a99de8160e0e91
@@ -233,6 +233,21 @@ and customise their rendering based on the layer.
If ``layer`` is -1, all item layers should be rendered.

.. seealso:: :py:func:`setCurrentExportLayer()`
%End

void setPredefinedScales( const QVector<qreal> &scales );
%Docstring
Sets the list of predefined ``scales`` to use with the layout. This is used
for maps which are set to the predefined atlas scaling mode.

.. seealso:: :py:func:`predefinedScales()`
%End

QVector<qreal> predefinedScales() const;
%Docstring
Returns the current list of predefined scales for use with the layout.

.. seealso:: :py:func:`setPredefinedScales()`
%End

signals:
@@ -480,7 +480,6 @@ Return map settings that will be used for drawing of the map.
True if a draw is already in progress
%End


virtual QRectF boundingRect() const;


@@ -1919,9 +1919,7 @@ void QgsLayoutDesignerDialog::atlasPreviewTriggered( bool checked )

if ( checked )
{
#if 0 //TODO
loadAtlasPredefinedScalesFromProject();
#endif
}

if ( checked )
@@ -1985,9 +1983,7 @@ void QgsLayoutDesignerDialog::atlasPageComboEditingFinished()
else if ( page != atlas->currentFeatureNumber() + 1 )
{
QgisApp::instance()->mapCanvas()->stopRendering();
#if 0 //TODO
loadAtlasPredefinedScalesFromProject();
#endif
atlas->seekTo( page - 1 );
#if 0 //TODO
emit atlasPreviewFeatureChanged();
@@ -2003,9 +1999,7 @@ void QgsLayoutDesignerDialog::atlasNext()

QgisApp::instance()->mapCanvas()->stopRendering();

#if 0 //TODO
loadAtlasPredefinedScalesFromProject();
#endif
if ( printAtlas->next() )
{
#if 0 //TODO
@@ -2022,9 +2016,7 @@ void QgsLayoutDesignerDialog::atlasPrevious()

QgisApp::instance()->mapCanvas()->stopRendering();

#if 0 //TODO
loadAtlasPredefinedScalesFromProject();
#endif
if ( printAtlas->previous() )
{
#if 0 //TODO
@@ -2041,9 +2033,7 @@ void QgsLayoutDesignerDialog::atlasFirst()

QgisApp::instance()->mapCanvas()->stopRendering();

#if 0 //TODO
loadAtlasPredefinedScalesFromProject();
#endif
if ( printAtlas->first() )
{
#if 0 //TODO
@@ -2060,9 +2050,7 @@ void QgsLayoutDesignerDialog::atlasLast()

QgisApp::instance()->mapCanvas()->stopRendering();

#if 0 //TODO
loadAtlasPredefinedScalesFromProject();
#endif
if ( printAtlas->last() )
{
#if 0 //TODO
@@ -2073,21 +2061,26 @@ void QgsLayoutDesignerDialog::atlasLast()

void QgsLayoutDesignerDialog::printAtlas()
{
loadAtlasPredefinedScalesFromProject();
//TODO
}

void QgsLayoutDesignerDialog::exportAtlasToRaster()
{
loadAtlasPredefinedScalesFromProject();
//TODO

}

void QgsLayoutDesignerDialog::exportAtlasToSvg()
{
loadAtlasPredefinedScalesFromProject();
//TODO
}

void QgsLayoutDesignerDialog::exportAtlasToPdf()
{
loadAtlasPredefinedScalesFromProject();
//TODO
}

@@ -2435,6 +2428,31 @@ void QgsLayoutDesignerDialog::atlasFeatureChanged( const QgsFeature &feature )
mapCanvas->expressionContextScope().addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "atlas_geometry" ), QVariant::fromValue( feature.geometry() ), true ) );
}

void QgsLayoutDesignerDialog::loadAtlasPredefinedScalesFromProject()
{
QVector<qreal> projectScales;
// first look at project's scales
QStringList scales( mLayout->project()->readListEntry( QStringLiteral( "Scales" ), QStringLiteral( "/ScalesList" ) ) );
bool hasProjectScales( mLayout->project()->readBoolEntry( QStringLiteral( "Scales" ), QStringLiteral( "/useProjectScales" ) ) );
if ( !hasProjectScales || scales.isEmpty() )
{
// default to global map tool scales
QgsSettings settings;
QString scalesStr( settings.value( QStringLiteral( "Map/scales" ), PROJECT_SCALES ).toString() );
scales = scalesStr.split( ',' );
}

for ( auto scaleIt = scales.constBegin(); scaleIt != scales.constEnd(); ++scaleIt )
{
QStringList parts( scaleIt->split( ':' ) );
if ( parts.size() == 2 )
{
projectScales.push_back( parts[1].toDouble() );
}
}
mLayout->context().setPredefinedScales( projectScales );
}

QgsLayoutAtlas *QgsLayoutDesignerDialog::atlas()
{
QgsPrintLayout *layout = qobject_cast< QgsPrintLayout *>( mLayout );
@@ -423,7 +423,8 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner

void atlasFeatureChanged( const QgsFeature &feature );


//! Load predefined scales from the project's properties
void loadAtlasPredefinedScalesFromProject();

QgsLayoutAtlas *atlas();
};
@@ -160,3 +160,10 @@ void QgsLayoutContext::setPagesVisible( bool visible )
{
mPagesVisible = visible;
}

void QgsLayoutContext::setPredefinedScales( const QVector<qreal> &scales )
{
mPredefinedScales = scales;
// make sure the list is sorted
std::sort( mPredefinedScales.begin(), mPredefinedScales.end() );
}
@@ -233,6 +233,19 @@ class CORE_EXPORT QgsLayoutContext : public QObject
*/
int currentExportLayer() const { return mCurrentExportLayer; }

/**
* Sets the list of predefined \a scales to use with the layout. This is used
* for maps which are set to the predefined atlas scaling mode.
* \see predefinedScales()
*/
void setPredefinedScales( const QVector<qreal> &scales );

/**
* Returns the current list of predefined scales for use with the layout.
* \see setPredefinedScales()
*/
QVector<qreal> predefinedScales() const { return mPredefinedScales; }

signals:

/**
@@ -278,6 +291,9 @@ class CORE_EXPORT QgsLayoutContext : public QObject
// projected geometry cache
mutable QMap<long, QgsGeometry> mGeometryCache;

//list of predefined scales
QVector<qreal> mPredefinedScales;

friend class QgsLayoutExporter;
friend class TestQgsLayout;
friend class LayoutContextPreviewSettingRestorer;
@@ -1,3 +1,4 @@

/***************************************************************************
testqgslayoutcontext.cpp
------------------------
@@ -44,6 +45,7 @@ class TestQgsLayoutContext: public QObject
void boundingBoxes();
void exportLayer();
void geometry();
void scales();

private:
QString mReport;
@@ -242,5 +244,17 @@ void TestQgsLayoutContext::geometry()
delete layer;
}

void TestQgsLayoutContext::scales()
{
QVector< qreal > scales;
scales << 1 << 15 << 5 << 10;

QgsLayoutContext context( nullptr );
context.setPredefinedScales( scales );

// should be sorted
QCOMPARE( context.predefinedScales(), QVector< qreal >() << 1 << 5 << 10 << 15 );
}

QGSTEST_MAIN( TestQgsLayoutContext )
#include "testqgslayoutcontext.moc"

0 comments on commit 3ffdda3

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