Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE][layouts] Add "Stepped Line" scalebar style
Designed to match the appearance and behavior of the ArcMap equivalent, this was a scalebar format which was previously impossible to replicate in QGIS. Also fixes #26589 Sponsored by SLYR
- Loading branch information
1 parent
3747098
commit e7cc830
Showing
11 changed files
with
255 additions
and
7 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
python/core/auto_generated/scalebar/qgssteppedlinescalebarrenderer.sip.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/scalebar/qgssteppedlinescalebarrenderer.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
class QgsSteppedLineScaleBarRenderer: QgsScaleBarRenderer | ||
{ | ||
%Docstring | ||
Scalebar style that draws a stepped line representation of a scalebar. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgssteppedlinescalebarrenderer.h" | ||
%End | ||
public: | ||
|
||
QgsSteppedLineScaleBarRenderer(); | ||
%Docstring | ||
Constructor for QgsSteppedLineScaleBarRenderer. | ||
%End | ||
|
||
virtual QString id() const; | ||
|
||
virtual QString visibleName() const; | ||
|
||
virtual int sortKey() const; | ||
|
||
virtual QgsSteppedLineScaleBarRenderer* clone() const /Factory/; | ||
|
||
|
||
virtual void draw( QgsRenderContext &context, | ||
const QgsScaleBarSettings &settings, | ||
const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const; | ||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/scalebar/qgssteppedlinescalebarrenderer.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/*************************************************************************** | ||
qgssteppedlinescalebarrenderer.cpp | ||
-------------------------------- | ||
begin : March 2020 | ||
copyright : (C) 2020 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgssteppedlinescalebarrenderer.h" | ||
#include "qgsscalebarsettings.h" | ||
#include "qgslayoututils.h" | ||
#include "qgssymbol.h" | ||
#include <QList> | ||
#include <QPainter> | ||
|
||
QString QgsSteppedLineScaleBarRenderer::id() const | ||
{ | ||
return QStringLiteral( "stepped" ); | ||
} | ||
|
||
QString QgsSteppedLineScaleBarRenderer::visibleName() const | ||
{ | ||
return QObject::tr( "Stepped Line" ); | ||
} | ||
|
||
int QgsSteppedLineScaleBarRenderer::sortKey() const | ||
{ | ||
return 7; | ||
} | ||
|
||
QgsSteppedLineScaleBarRenderer *QgsSteppedLineScaleBarRenderer::clone() const | ||
{ | ||
return new QgsSteppedLineScaleBarRenderer( *this ); | ||
} | ||
|
||
void QgsSteppedLineScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const | ||
{ | ||
if ( !context.painter() ) | ||
{ | ||
return; | ||
} | ||
QPainter *painter = context.painter(); | ||
|
||
std::unique_ptr< QgsLineSymbol > sym( settings.lineSymbol()->clone() ); | ||
sym->startRender( context ) ; | ||
|
||
double scaledLabelBarSpace = context.convertToPainterUnits( settings.labelBarSpace(), QgsUnitTypes::RenderMillimeters ); | ||
double scaledBoxContentSpace = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters ); | ||
QFontMetricsF fontMetrics = QgsTextRenderer::fontMetrics( context, settings.textFormat() ); | ||
double barTopPosition = scaledBoxContentSpace + ( settings.labelVerticalPlacement() == QgsScaleBarSettings::LabelAboveSegment ? fontMetrics.ascent() + scaledLabelBarSpace : 0 ); | ||
const double barBottomPosition = barTopPosition + context.convertToPainterUnits( settings.height(), QgsUnitTypes::RenderMillimeters ); | ||
|
||
painter->save(); | ||
if ( context.flags() & QgsRenderContext::Antialiasing ) | ||
painter->setRenderHint( QPainter::Antialiasing, true ); | ||
|
||
painter->setPen( Qt::NoPen ); | ||
|
||
double xOffset = firstLabelXOffset( settings, context, scaleContext ); | ||
|
||
QList<double> positions = segmentPositions( context, scaleContext, settings ); | ||
QList<double> widths = segmentWidths( scaleContext, settings ); | ||
|
||
QPolygonF points; | ||
|
||
for ( int i = 0; i < positions.size() + 1; ++i ) | ||
{ | ||
// we render one extra place, corresponding to the final position + width (i.e. the "end" of the bar) | ||
double x = i < positions.size() ? context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset | ||
: context.convertToPainterUnits( positions.at( i - 1 ), QgsUnitTypes::RenderMillimeters ) + xOffset + context.convertToPainterUnits( widths.at( i - 1 ), QgsUnitTypes::RenderMillimeters ); | ||
if ( i % 2 == 0 ) | ||
{ | ||
points << QPointF( x, barBottomPosition ) << QPointF( x, barTopPosition ); | ||
} | ||
else | ||
{ | ||
points << QPointF( x, barTopPosition ) << QPointF( x, barBottomPosition ) ; | ||
} | ||
} | ||
|
||
sym->renderPolyline( points, nullptr, context ); | ||
|
||
painter->restore(); | ||
|
||
sym->stopRender( context ); | ||
|
||
//draw labels using the default method | ||
drawDefaultLabels( context, settings, scaleContext ); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*************************************************************************** | ||
qgssteppedlinescalebarrenderer.h | ||
-------------------------------- | ||
begin : March 2020 | ||
copyright : (C) 2020 by Nyall Dawson | ||
email : nyall dot dawson at gmail dot com | ||
***************************************************************************/ | ||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSSTEPPEDLINESCALEBARRENDERER_H | ||
#define QGSSTEPPEDLINESCALEBARRENDERER_H | ||
|
||
#include "qgis_core.h" | ||
#include "qgsscalebarrenderer.h" | ||
#include <QString> | ||
|
||
/** | ||
* \class QgsSteppedLineScaleBarRenderer | ||
* \ingroup core | ||
* Scalebar style that draws a stepped line representation of a scalebar. | ||
* \since QGIS 3.14 | ||
*/ | ||
class CORE_EXPORT QgsSteppedLineScaleBarRenderer: public QgsScaleBarRenderer | ||
{ | ||
public: | ||
|
||
/** | ||
* Constructor for QgsSteppedLineScaleBarRenderer. | ||
*/ | ||
QgsSteppedLineScaleBarRenderer() = default; | ||
|
||
QString id() const override; | ||
QString visibleName() const override; | ||
int sortKey() const override; | ||
QgsSteppedLineScaleBarRenderer* clone() const override SIP_FACTORY; | ||
|
||
void draw( QgsRenderContext &context, | ||
const QgsScaleBarSettings &settings, | ||
const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const override; | ||
|
||
}; | ||
|
||
#endif // QGSSTEPPEDLINESCALEBARRENDERER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+8.54 KB
...ut_scalebar/expected_layoutscalebar_stepped/expected_layoutscalebar_stepped.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.