Skip to content

Commit

Permalink
Port predefined scale handling to layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 7c086be commit 3ffdda3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 14 deletions.
15 changes: 15 additions & 0 deletions python/core/layout/qgslayoutcontext.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion python/core/layout/qgslayoutitemmap.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
42 changes: 30 additions & 12 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1919,9 +1919,7 @@ void QgsLayoutDesignerDialog::atlasPreviewTriggered( bool checked )

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

if ( checked )
Expand Down Expand Up @@ -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();
Expand All @@ -2003,9 +1999,7 @@ void QgsLayoutDesignerDialog::atlasNext()

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

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

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

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

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

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

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

#if 0 //TODO
loadAtlasPredefinedScalesFromProject();
#endif
if ( printAtlas->last() )
{
#if 0 //TODO
Expand All @@ -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
}

Expand Down Expand Up @@ -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 );
Expand Down
3 changes: 2 additions & 1 deletion src/app/layout/qgslayoutdesignerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayoutcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
16 changes: 16 additions & 0 deletions src/core/layout/qgslayoutcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

/**
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions tests/src/core/testqgslayoutcontext.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

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

private:
QString mReport;
Expand Down Expand Up @@ -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.