Skip to content

Commit

Permalink
[FEATURE][layouts] Allow scalebar line style to be set using
Browse files Browse the repository at this point in the history
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 22, 2020
1 parent f3e2ef1 commit 9378dcd
Show file tree
Hide file tree
Showing 33 changed files with 1,367 additions and 488 deletions.
66 changes: 57 additions & 9 deletions python/core/auto_generated/layout/qgslayoutitemscalebar.sip.in
Expand Up @@ -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/;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
26 changes: 24 additions & 2 deletions python/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
71 changes: 61 additions & 10 deletions python/core/auto_generated/scalebar/qgsscalebarsettings.sip.in
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 12 additions & 5 deletions src/app/decorations/qgsdecorationscalebar.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 ) );

Expand Down

0 comments on commit 9378dcd

Please sign in to comment.