Skip to content
Permalink
Browse files

[FEATURE][layouts] Allow scalebar line style to be set using

standard QGIS line symbols

Instead of the previous settings of scalebar line color/width/cap styles,
we now expose the option as a full line symbol for more advanced styling

Also some code cleanups

Sponsored by SLYR
  • Loading branch information
nyalldawson committed Mar 20, 2020
1 parent f3e2ef1 commit 9378dcd73f125b738d5c2a66a3f11b6c213f77c9
Showing with 1,367 additions and 488 deletions.
  1. +57 −9 python/core/auto_generated/layout/qgslayoutitemscalebar.sip.in
  2. +24 −2 python/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in
  3. +61 −10 python/core/auto_generated/scalebar/qgsscalebarsettings.sip.in
  4. +12 −5 src/app/decorations/qgsdecorationscalebar.cpp
  5. +12 −11 src/core/layout/qgscompositionconverter.cpp
  6. +161 −50 src/core/layout/qgslayoutitemscalebar.cpp
  7. +37 −9 src/core/layout/qgslayoutitemscalebar.h
  8. +2 −2 src/core/layout/qgslayoutobject.h
  9. +59 −17 src/core/scalebar/qgsdoubleboxscalebarrenderer.cpp
  10. +1 −1 src/core/scalebar/qgsnumericscalebarrenderer.cpp
  11. +94 −1 src/core/scalebar/qgsscalebarrenderer.cpp
  12. +19 −2 src/core/scalebar/qgsscalebarrenderer.h
  13. +103 −15 src/core/scalebar/qgsscalebarsettings.cpp
  14. +42 −18 src/core/scalebar/qgsscalebarsettings.h
  15. +49 −13 src/core/scalebar/qgssingleboxscalebarrenderer.cpp
  16. +46 −36 src/core/scalebar/qgsticksscalebarrenderer.cpp
  17. +25 −100 src/gui/layout/qgslayoutscalebarwidget.cpp
  18. +1 −5 src/gui/layout/qgslayoutscalebarwidget.h
  19. +67 −170 src/ui/layout/qgslayoutscalebarwidgetbase.ui
  20. +207 −12 tests/src/core/testqgslayoutscalebar.cpp
  21. BIN ...rter/expected_importComposerTemplateScaleBar_0/expected_importComposerTemplateScaleBar_0_mask.png
  22. BIN ...compositionconverter/expected_importComposerTemplate_0/expected_importComposerTemplate_0_mask.png
  23. BIN ...mages/layout_scalebar/expected_layoutscalebar_datadefined/expected_layoutscalebar_datadefined.png
  24. BIN ...ages/layout_scalebar/expected_layoutscalebar_doublebox/expected_layoutscalebar_doublebox_mask.png
  25. BIN ...ar_doublebox_labelcenteredsegment/expected_layoutscalebar_doublebox_labelcenteredsegment_mask.png
  26. BIN ...bar/expected_layoutscalebar_doublebox_linesymbol/expected_layoutscalebar_doublebox_linesymbol.png
  27. BIN ...out_scalebar/expected_layoutscalebar_numericformat/expected_layoutscalebar_numericformat_mask.png
  28. BIN ...ages/layout_scalebar/expected_layoutscalebar_singlebox/expected_layoutscalebar_singlebox_mask.png
  29. BIN ...scalebar/expected_layoutscalebar_singlebox_alpha/expected_layoutscalebar_singlebox_alpha_mask.png
  30. BIN ...bar/expected_layoutscalebar_singlebox_linesymbol/expected_layoutscalebar_singlebox_linesymbol.png
  31. BIN ...es/layout_scalebar/expected_layoutscalebar_textformat/expected_layoutscalebar_textformat_mask.png
  32. BIN ...yout_scalebar/expected_layoutscalebar_tick_linesymbol/expected_layoutscalebar_tick_linesymbol.png
  33. +288 −0 tests/testdata/layouts/scalebar_old_datadefined.qgs
@@ -208,6 +208,27 @@ Sets the text ``format`` used for drawing text in the scalebar.
.. seealso:: :py:func:`textFormat`

.. versionadded:: 3.2
%End

QgsLineSymbol *lineSymbol() const;
%Docstring
Returns the line symbol used to render the scalebar (only used for some scalebar types).

Ownership is not transferred.

.. seealso:: :py:func:`setLineSymbol`

.. versionadded:: 3.14
%End

void setLineSymbol( QgsLineSymbol *symbol /Transfer/ );
%Docstring
Sets the line ``symbol`` used to render the scalebar (only used for some scalebar types). Ownership of ``symbol`` is
transferred to the scalebar.

.. seealso:: :py:func:`lineSymbol`

.. versionadded:: 3.14
%End

QFont font() const /Deprecated/;
@@ -290,39 +311,54 @@ Sets the secondary ``color`` used for fills in the scalebar.
.. seealso:: :py:func:`setFillColor2`
%End

QColor lineColor() const;
QColor lineColor() const /Deprecated/;
%Docstring
Returns the color used for lines in the scalebar.

.. seealso:: :py:func:`setLineColor`

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

void setLineColor( const QColor &color );
void setLineColor( const QColor &color ) /Deprecated/;
%Docstring
Sets the ``color`` used for lines in the scalebar.

.. seealso:: :py:func:`lineColor`

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

double lineWidth() const;
double lineWidth() const /Deprecated/;
%Docstring
Returns the line width in millimeters for lines in the scalebar.

.. seealso:: :py:func:`setLineWidth`

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

void setLineWidth( double width );
void setLineWidth( double width ) /Deprecated/;
%Docstring
Sets the line ``width`` in millimeters for lines in the scalebar.

.. seealso:: :py:func:`lineWidth`

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

QPen pen() const;
QPen pen() const /Deprecated/;
%Docstring
Returns the pen used for drawing outlines in the scalebar.

.. seealso:: :py:func:`brush`

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

QBrush brush() const;
@@ -466,32 +502,44 @@ Sets the distance ``units`` used by the scalebar.
.. seealso:: :py:func:`units`
%End

Qt::PenJoinStyle lineJoinStyle() const;
Qt::PenJoinStyle lineJoinStyle() const /Deprecated/;
%Docstring
Returns the join style used for drawing lines in the scalebar.

.. seealso:: :py:func:`setLineJoinStyle`

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

void setLineJoinStyle( Qt::PenJoinStyle style );
void setLineJoinStyle( Qt::PenJoinStyle style ) /Deprecated/;
%Docstring
Sets the join ``style`` used when drawing the lines in the scalebar

.. seealso:: :py:func:`lineJoinStyle`

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

Qt::PenCapStyle lineCapStyle() const;
Qt::PenCapStyle lineCapStyle() const /Deprecated/;
%Docstring
Returns the cap style used for drawing lines in the scalebar.

.. seealso:: :py:func:`setLineCapStyle`

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

void setLineCapStyle( Qt::PenCapStyle style );
void setLineCapStyle( Qt::PenCapStyle style ) /Deprecated/;
%Docstring
Sets the cap ``style`` used when drawing the lines in the scalebar.

.. seealso:: :py:func:`lineCapStyle`

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

void applyDefaultSettings();
@@ -83,10 +83,22 @@ Returns a clone of the renderer. The caller takes ownership of the returned valu
Draws the scalebar using the specified ``settings`` and ``scaleContext`` to a destination render ``context``.
%End

virtual QSizeF calculateBoxSize( const QgsScaleBarSettings &settings,
virtual QSizeF calculateBoxSize( const QgsScaleBarSettings &settings,
const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const /Deprecated/;
%Docstring
Calculates the required box size (in millimeters) for a scalebar using the specified ``settings`` and ``scaleContext``.

.. deprecated::
Use the version with a QgsRenderContext instead.
%End

virtual QSizeF calculateBoxSize( QgsRenderContext &context,
const QgsScaleBarSettings &settings,
const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const;
%Docstring
Calculates the required box size (in millimeters) for a scalebar using the specified ``settings`` and ``scaleContext``.

.. versionadded:: 3.14
%End

protected:
@@ -118,9 +130,19 @@ Returns the x-offset (in render context painter units) used for the first label
.. versionadded:: 3.2
%End

QList<double> segmentPositions( const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const;
QList<double> segmentPositions( const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const /Deprecated/;
%Docstring
Returns a list of positions for each segment within the scalebar.

.. deprecated::
use the version with a QgsRenderContext instead
%End

QList<double> segmentPositions( QgsRenderContext &context, const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const;
%Docstring
Returns a list of positions for each segment within the scalebar.

.. versionadded:: 3.14
%End

QList<double> segmentWidths( const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const;
@@ -327,48 +327,87 @@ Sets the secondary ``color`` used for fills in the scalebar.
.. seealso:: :py:func:`setFillColor2`
%End

QColor lineColor() const;
QColor lineColor() const /Deprecated/;
%Docstring
Returns the color used for lines in the scalebar.

.. seealso:: :py:func:`setLineColor`

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

void setLineColor( const QColor &color );
void setLineColor( const QColor &color ) /Deprecated/;
%Docstring
Sets the ``color`` used for lines in the scalebar.

.. seealso:: :py:func:`lineColor`

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

double lineWidth() const;
double lineWidth() const /Deprecated/;
%Docstring
Returns the line width in millimeters for lines in the scalebar.

.. seealso:: :py:func:`setLineWidth`

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

void setLineWidth( double width );
void setLineWidth( double width ) /Deprecated/;
%Docstring
Sets the line ``width`` in millimeters for lines in the scalebar.

.. seealso:: :py:func:`lineWidth`

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

QPen pen() const;
QPen pen() const /Deprecated/;
%Docstring
Returns the pen used for drawing outlines in the scalebar.

.. seealso:: :py:func:`setPen`

.. seealso:: :py:func:`brush`

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

void setPen( const QPen &pen );
void setPen( const QPen &pen ) /Deprecated/;
%Docstring
Sets the pen used for drawing outlines in the scalebar.

.. seealso:: :py:func:`pen`

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

QgsLineSymbol *lineSymbol() const;
%Docstring
Returns the line symbol used to render the scalebar (only used for some scalebar types).

Ownership is not transferred.

.. seealso:: :py:func:`setLineSymbol`

.. versionadded:: 3.14
%End

void setLineSymbol( QgsLineSymbol *symbol /Transfer/ );
%Docstring
Sets the line ``symbol`` used to render the scalebar (only used for some scalebar types). Ownership of ``symbol`` is
transferred to the scalebar.

.. seealso:: :py:func:`lineSymbol`

.. versionadded:: 3.14
%End

QBrush brush() const;
@@ -498,32 +537,44 @@ Sets the scalebar ``alignment``.
.. seealso:: :py:func:`alignment`
%End

Qt::PenJoinStyle lineJoinStyle() const;
Qt::PenJoinStyle lineJoinStyle() const /Deprecated/;
%Docstring
Returns the join style used for drawing lines in the scalebar.

.. seealso:: :py:func:`setLineJoinStyle`

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

void setLineJoinStyle( Qt::PenJoinStyle style );
void setLineJoinStyle( Qt::PenJoinStyle style ) /Deprecated/;
%Docstring
Sets the join ``style`` used when drawing the lines in the scalebar

.. seealso:: :py:func:`lineJoinStyle`

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

Qt::PenCapStyle lineCapStyle() const;
Qt::PenCapStyle lineCapStyle() const /Deprecated/;
%Docstring
Returns the cap style used for drawing lines in the scalebar.

.. seealso:: :py:func:`setLineCapStyle`

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

void setLineCapStyle( Qt::PenCapStyle style );
void setLineCapStyle( Qt::PenCapStyle style ) /Deprecated/;
%Docstring
Sets the cap ``style`` used when drawing the lines in the scalebar.

.. seealso:: :py:func:`lineCapStyle`

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

const QgsNumericFormat *numericFormat() const;
@@ -154,19 +154,26 @@ void QgsDecorationScaleBar::setupScaleBar()
tickStyle->setTickPosition( mStyleIndex == 0 ? QgsTicksScaleBarRenderer::TicksDown : QgsTicksScaleBarRenderer::TicksUp );
mStyle = std::move( tickStyle );
mSettings.setFillColor( mColor );
mSettings.setLineColor( mColor ); // Compatibility with pre 3.2 configuration
std::unique_ptr< QgsLineSymbol > lineSymbol = qgis::make_unique< QgsLineSymbol >();
lineSymbol->setColor( mColor ); // Compatibility with pre 3.2 configuration
lineSymbol->setWidth( 0.3 );
lineSymbol->setOutputUnit( QgsUnitTypes::RenderMillimeters );
mSettings.setLineSymbol( lineSymbol.release() );
mSettings.setHeight( 2.2 );
mSettings.setLineWidth( 0.3 );
break;
}
case 2:
case 3:
mStyle = qgis::make_unique< QgsSingleBoxScaleBarRenderer >();
mSettings.setFillColor( mColor );
mSettings.setFillColor2( QColor( "transparent" ) );
mSettings.setLineColor( mOutlineColor );
mSettings.setHeight( mStyleIndex == 2 ? 1 : 3 );
mSettings.setLineWidth( mStyleIndex == 2 ? 0.2 : 0.3 );
std::unique_ptr< QgsLineSymbol > lineSymbol = qgis::make_unique< QgsLineSymbol >();
lineSymbol->setColor( mOutlineColor ); // Compatibility with pre 3.2 configuration
lineSymbol->setWidth( mStyleIndex == 2 ? 0.2 : 0.3 );
lineSymbol->setOutputUnit( QgsUnitTypes::RenderMillimeters );
mSettings.setLineSymbol( lineSymbol.release() );

break;
}
mSettings.setLabelBarSpace( 1.8 );
@@ -317,7 +324,7 @@ void QgsDecorationScaleBar::render( const QgsMapSettings &mapSettings, QgsRender
scaleContext.scale = mapSettings.scale();

//Calculate total width of scale bar and label
QSizeF size = mStyle->calculateBoxSize( mSettings, scaleContext );
QSizeF size = mStyle->calculateBoxSize( context, mSettings, scaleContext );
size.setWidth( context.convertToPainterUnits( size.width(), QgsUnitTypes::RenderMillimeters ) );
size.setHeight( context.convertToPainterUnits( size.height(), QgsUnitTypes::RenderMillimeters ) );

0 comments on commit 9378dcd

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