Skip to content
Permalink
Browse files

Make a proper registry for scalebar renderers

  • Loading branch information
nyalldawson committed Mar 19, 2020
1 parent 58e6a2c commit b45203a6c4d6654a84539028f80ef44ea891ae3b
Showing with 607 additions and 85 deletions.
  1. +7 −0 python/core/auto_generated/qgsapplication.sip.in
  2. +8 −1 python/core/auto_generated/scalebar/qgsdoubleboxscalebarrenderer.sip.in
  3. +8 −1 python/core/auto_generated/scalebar/qgsnumericscalebarrenderer.sip.in
  4. +30 −1 python/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in
  5. +84 −0 python/core/auto_generated/scalebar/qgsscalebarrendererregistry.sip.in
  6. +8 −1 python/core/auto_generated/scalebar/qgssingleboxscalebarrenderer.sip.in
  7. +8 −2 python/core/auto_generated/scalebar/qgsticksscalebarrenderer.sip.in
  8. +1 −0 python/core/core_auto.sip
  9. +2 −0 src/core/CMakeLists.txt
  10. +8 −32 src/core/layout/qgslayoutitemscalebar.cpp
  11. +8 −0 src/core/qgsapplication.cpp
  12. +9 −0 src/core/qgsapplication.h
  13. +20 −0 src/core/scalebar/qgsdoubleboxscalebarrenderer.cpp
  14. +4 −1 src/core/scalebar/qgsdoubleboxscalebarrenderer.h
  15. +20 −0 src/core/scalebar/qgsnumericscalebarrenderer.cpp
  16. +4 −1 src/core/scalebar/qgsnumericscalebarrenderer.h
  17. +5 −0 src/core/scalebar/qgsscalebarrenderer.cpp
  18. +26 −1 src/core/scalebar/qgsscalebarrenderer.h
  19. +105 −0 src/core/scalebar/qgsscalebarrendererregistry.cpp
  20. +90 −0 src/core/scalebar/qgsscalebarrendererregistry.h
  21. +20 −0 src/core/scalebar/qgssingleboxscalebarrenderer.cpp
  22. +4 −1 src/core/scalebar/qgssingleboxscalebarrenderer.h
  23. +41 −1 src/core/scalebar/qgsticksscalebarrenderer.cpp
  24. +5 −2 src/core/scalebar/qgsticksscalebarrenderer.h
  25. +12 −40 src/gui/layout/qgslayoutscalebarwidget.cpp
  26. +1 −0 tests/src/python/CMakeLists.txt
  27. +69 −0 tests/src/python/test_qgsscalebarrendererregistry.py
@@ -843,6 +843,13 @@ Gets the registry of available field formatters.
Returns registry of available 3D renderers.

.. versionadded:: 3.0
%End

static QgsScaleBarRendererRegistry *scaleBarRendererRegistry() /KeepReference/;
%Docstring
Gets the registry of available scalebar renderers.

.. versionadded:: 3.14
%End

static QgsProjectStorageRegistry *projectStorageRegistry() /KeepReference/;
@@ -26,7 +26,14 @@ Double box with alternating colors.
Constructor for QgsDoubleBoxScaleBarRenderer.
%End

virtual QString name() const;
virtual QString id() const;

virtual QString visibleName() const;

virtual int sortKey() const;

virtual QgsDoubleBoxScaleBarRenderer *clone() const /Factory/;


virtual void draw( QgsRenderContext &context,
const QgsScaleBarSettings &settings,
@@ -26,7 +26,14 @@ A scale bar style that draws text in the form of '1:XXXXX'.
Constructor for QgsNumericScaleBarRenderer.
%End

virtual QString name() const;
virtual QString id() const;

virtual QString visibleName() const;

virtual int sortKey() const;

virtual QgsNumericScaleBarRenderer *clone() const /Factory/;


virtual void draw( QgsRenderContext &context,
const QgsScaleBarSettings &settings,
@@ -42,9 +42,38 @@ Constructor for QgsScaleBarRenderer.
%End
virtual ~QgsScaleBarRenderer();

virtual QString name() const = 0;
QString name() const /Deprecated/;
%Docstring
Returns the unique name for this style.

.. deprecated::
use id() instead
%End

virtual QString id() const = 0;
%Docstring
Returns the unique ID for this renderer.

.. versionadded:: 3.14
%End

virtual QString visibleName() const = 0;
%Docstring
Returns the user friendly, translated name for the renderer.

.. versionadded:: 3.14
%End

virtual int sortKey() const;
%Docstring
Returns a sorting key value, where renderers with a lower sort key will be shown earlier in lists.

Generally, subclasses should return QgsScaleBarRenderer.sortKey() as their sorting key.
%End

virtual QgsScaleBarRenderer *clone() const = 0 /Factory/;
%Docstring
Returns a clone of the renderer. The caller takes ownership of the returned value.
%End

virtual void draw( QgsRenderContext &context,
@@ -0,0 +1,84 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/scalebar/qgsscalebarrendererregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsScaleBarRendererRegistry
{
%Docstring
The QgsScaleBarRendererRegistry manages registered scalebar renderers.

A reference to the QgsScaleBarRendererRegistry can be obtained from
:py:func:`QgsApplication.scalebarRendererRegistry()`

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgsscalebarrendererregistry.h"
%End
public:

explicit QgsScaleBarRendererRegistry();
%Docstring
You should not normally need to create your own scalebar renderer registry.

Use the one provided by `QgsApplication.scalebarRendererRegistry()` instead.
%End
~QgsScaleBarRendererRegistry();

QStringList renderers() const;
%Docstring
Returns a list of the renderer ids currently contained in the registry.
%End

QStringList sortedRendererList() const;
%Docstring
Returns a list of the renderer ids currently contained in the registry,
sorted in an order respecting the renderer's sort keys and display strings.
%End

void addRenderer( QgsScaleBarRenderer *renderer /Transfer/ );
%Docstring
Adds a new ``renderer`` to the registry.

Ownership is transferred to the registry.
%End

void removeRenderer( const QString &id );
%Docstring
Removes the renderer with matching ``id`` from the registry.
%End

QgsScaleBarRenderer *renderer( const QString &id ) const /TransferBack/;
%Docstring
Creates a new scalebar renderer by ``id``. If there is no such ``id`` registered,
``None`` will be returned instead.

The caller takes ownership of the returned object.
%End

QString visibleName( const QString &id ) const;
%Docstring
Returns the translated, user-visible name for the renderer with matching ``id``.
%End

int sortKey( const QString &id ) const;
%Docstring
Returns the sorting key for the renderer with matching ``id``.
%End

};
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/scalebar/qgsscalebarrendererregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -27,7 +27,14 @@ color for the segments.
Constructor for QgsSingleBoxScaleBarRenderer.
%End

virtual QString name() const;
virtual QString id() const;

virtual QString visibleName() const;

virtual int sortKey() const;

virtual QgsSingleBoxScaleBarRenderer *clone() const /Factory/;


virtual void draw( QgsRenderContext &context,
const QgsScaleBarSettings &settings,
@@ -28,12 +28,18 @@ A scale bar that draws segments using short ticks.
TicksMiddle,
};

QgsTicksScaleBarRenderer();
QgsTicksScaleBarRenderer( TickPosition position = TicksMiddle );
%Docstring
Constructor for QgsTicksScaleBarRenderer.
%End

virtual QString name() const;
virtual QString id() const;

virtual QString visibleName() const;

virtual int sortKey() const;

virtual QgsTicksScaleBarRenderer *clone() const /Factory/;


virtual void draw( QgsRenderContext &context,
@@ -488,6 +488,7 @@
%Include auto_generated/scalebar/qgsdoubleboxscalebarrenderer.sip
%Include auto_generated/scalebar/qgsnumericscalebarrenderer.sip
%Include auto_generated/scalebar/qgsscalebarrenderer.sip
%Include auto_generated/scalebar/qgsscalebarrendererregistry.sip
%Include auto_generated/scalebar/qgsscalebarsettings.sip
%Include auto_generated/scalebar/qgssingleboxscalebarrenderer.sip
%Include auto_generated/scalebar/qgsticksscalebarrenderer.sip
@@ -183,6 +183,7 @@ SET(QGIS_CORE_SRCS
scalebar/qgsdoubleboxscalebarrenderer.cpp
scalebar/qgsnumericscalebarrenderer.cpp
scalebar/qgsscalebarrenderer.cpp
scalebar/qgsscalebarrendererregistry.cpp
scalebar/qgsscalebarsettings.cpp
scalebar/qgssingleboxscalebarrenderer.cpp
scalebar/qgsticksscalebarrenderer.cpp
@@ -1272,6 +1273,7 @@ SET(QGIS_CORE_HDRS
scalebar/qgsdoubleboxscalebarrenderer.h
scalebar/qgsnumericscalebarrenderer.h
scalebar/qgsscalebarrenderer.h
scalebar/qgsscalebarrendererregistry.h
scalebar/qgsscalebarsettings.h
scalebar/qgssingleboxscalebarrenderer.h
scalebar/qgsticksscalebarrenderer.h
@@ -16,6 +16,7 @@

#include "qgslayoutitemscalebar.h"
#include "qgslayoutitemregistry.h"
#include "qgsscalebarrendererregistry.h"
#include "qgslayoutitemmap.h"
#include "qgslayout.h"
#include "qgslayoututils.h"
@@ -490,7 +491,7 @@ void QgsLayoutItemScaleBar::resizeToMinimumWidth()
void QgsLayoutItemScaleBar::update()
{
//Don't adjust box size for numeric scale bars:
if ( mStyle && mStyle->name() != QLatin1String( "Numeric" ) )
if ( mStyle && mStyle->id() != QLatin1String( "Numeric" ) )
{
refreshItemSize();
}
@@ -507,34 +508,10 @@ void QgsLayoutItemScaleBar::updateScale()
void QgsLayoutItemScaleBar::setStyle( const QString &styleName )
{
//switch depending on style name
if ( styleName == QLatin1String( "Single Box" ) )
std::unique_ptr< QgsScaleBarRenderer> renderer( QgsApplication::scaleBarRendererRegistry()->renderer( styleName ) );
if ( renderer )
{
mStyle = qgis::make_unique< QgsSingleBoxScaleBarRenderer >();
}
else if ( styleName == QLatin1String( "Double Box" ) )
{
mStyle = qgis::make_unique< QgsDoubleBoxScaleBarRenderer >();
}
else if ( styleName == QLatin1String( "Line Ticks Middle" ) || styleName == QLatin1String( "Line Ticks Down" ) || styleName == QLatin1String( "Line Ticks Up" ) )
{
std::unique_ptr< QgsTicksScaleBarRenderer > tickStyle = qgis::make_unique< QgsTicksScaleBarRenderer >();
if ( styleName == QLatin1String( "Line Ticks Middle" ) )
{
tickStyle->setTickPosition( QgsTicksScaleBarRenderer::TicksMiddle );
}
else if ( styleName == QLatin1String( "Line Ticks Down" ) )
{
tickStyle->setTickPosition( QgsTicksScaleBarRenderer::TicksDown );
}
else if ( styleName == QLatin1String( "Line Ticks Up" ) )
{
tickStyle->setTickPosition( QgsTicksScaleBarRenderer::TicksUp );
}
mStyle = std::move( tickStyle );
}
else if ( styleName == QLatin1String( "Numeric" ) )
{
mStyle = qgis::make_unique< QgsNumericScaleBarRenderer >();
mStyle = std::move( renderer );
}
refreshItemSize();
emit changed();
@@ -544,7 +521,7 @@ QString QgsLayoutItemScaleBar::style() const
{
if ( mStyle )
{
return mStyle->name();
return mStyle->id();
}
else
{
@@ -619,7 +596,7 @@ bool QgsLayoutItemScaleBar::writePropertiesToElement( QDomElement &composerScale
//style
if ( mStyle )
{
composerScaleBarElem.setAttribute( QStringLiteral( "style" ), mStyle->name() );
composerScaleBarElem.setAttribute( QStringLiteral( "style" ), mStyle->id() );
}

//map id
@@ -817,8 +794,7 @@ bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemEl
}

//style
QString styleString = itemElem.attribute( QStringLiteral( "style" ), QString() );
setStyle( styleString.toLocal8Bit().data() );
setStyle( itemElem.attribute( QStringLiteral( "style" ), QString() ) );

if ( itemElem.attribute( QStringLiteral( "unitType" ) ).isEmpty() )
{
@@ -31,6 +31,7 @@
#include "qgstaskmanager.h"
#include "qgsnumericformatregistry.h"
#include "qgsfieldformatterregistry.h"
#include "qgsscalebarrendererregistry.h"
#include "qgssvgcache.h"
#include "qgsimagecache.h"
#include "qgscolorschemeregistry.h"
@@ -2209,6 +2210,11 @@ Qgs3DRendererRegistry *QgsApplication::renderer3DRegistry()
return members()->m3DRendererRegistry;
}

QgsScaleBarRendererRegistry *QgsApplication::scaleBarRendererRegistry()
{
return members()->mScaleBarRendererRegistry;
}

QgsProjectStorageRegistry *QgsApplication::projectStorageRegistry()
{
return members()->mProjectStorageRegistry;
@@ -2246,11 +2252,13 @@ QgsApplication::ApplicationMembers::ApplicationMembers()
mValidityCheckRegistry = new QgsValidityCheckRegistry();
mClassificationMethodRegistry = new QgsClassificationMethodRegistry();
mBookmarkManager = new QgsBookmarkManager( nullptr );
mScaleBarRendererRegistry = new QgsScaleBarRendererRegistry();
}

QgsApplication::ApplicationMembers::~ApplicationMembers()
{
delete mStyleModel;
delete mScaleBarRendererRegistry;
delete mValidityCheckRegistry;
delete mActionScopeRegistry;
delete m3DRendererRegistry;
@@ -57,6 +57,7 @@ class QgsBookmarkManager;
class QgsStyleModel;
class QgsNumericFormatRegistry;
class QgsConnectionRegistry;
class QgsScaleBarRendererRegistry;

/**
* \ingroup core
@@ -775,6 +776,13 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
static Qgs3DRendererRegistry *renderer3DRegistry() SIP_KEEPREFERENCE;

/**
* Gets the registry of available scalebar renderers.
*
* \since QGIS 3.14
*/
static QgsScaleBarRendererRegistry *scaleBarRendererRegistry() SIP_KEEPREFERENCE;

/**
* Returns registry of available project storage implementations.
* \since QGIS 3.2
@@ -909,6 +917,7 @@ class CORE_EXPORT QgsApplication : public QApplication
QgsFieldFormatterRegistry *mFieldFormatterRegistry = nullptr;
QgsGpsConnectionRegistry *mGpsConnectionRegistry = nullptr;
QgsNetworkContentFetcherRegistry *mNetworkContentFetcherRegistry = nullptr;
QgsScaleBarRendererRegistry *mScaleBarRendererRegistry = nullptr;
QgsValidityCheckRegistry *mValidityCheckRegistry = nullptr;
QgsMessageLog *mMessageLog = nullptr;
QgsPaintEffectRegistry *mPaintEffectRegistry = nullptr;

0 comments on commit b45203a

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