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 "hollow" scalebar style
This matches the "hollow" scalebar style from ArcMap, and is a style used in South African mapping standards Fixes #20390 Sponsored by SLYR
- Loading branch information
1 parent
e7cc830
commit 1887846
Showing
16 changed files
with
461 additions
and
3 deletions.
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
python/core/auto_generated/scalebar/qgshollowscalebarrenderer.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,53 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/scalebar/qgshollowscalebarrenderer.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
class QgsHollowScaleBarRenderer: QgsScaleBarRenderer | ||
{ | ||
%Docstring | ||
Scalebar style that draws a single box with alternating color for the segments, with horizontal lines through | ||
alternating segments. AKA "South African" style. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgshollowscalebarrenderer.h" | ||
%End | ||
public: | ||
|
||
QgsHollowScaleBarRenderer(); | ||
%Docstring | ||
Constructor for QgsHollowScaleBarRenderer. | ||
%End | ||
|
||
virtual QString id() const; | ||
|
||
virtual QString visibleName() const; | ||
|
||
virtual int sortKey() const; | ||
|
||
virtual QgsHollowScaleBarRenderer *clone() const /Factory/; | ||
|
||
|
||
virtual void draw( QgsRenderContext &context, | ||
const QgsScaleBarSettings &settings, | ||
const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const; | ||
virtual bool applyDefaultSettings( QgsScaleBarSettings &settings ) const; | ||
|
||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/scalebar/qgshollowscalebarrenderer.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,183 @@ | ||
/*************************************************************************** | ||
qgshollowscalebarrenderer.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 "qgshollowscalebarrenderer.h" | ||
#include "qgsscalebarsettings.h" | ||
#include "qgslayoututils.h" | ||
#include "qgssymbol.h" | ||
#include "qgsfillsymbollayer.h" | ||
#include <QList> | ||
#include <QPainter> | ||
|
||
QString QgsHollowScaleBarRenderer::id() const | ||
{ | ||
return QStringLiteral( "hollow" ); | ||
} | ||
|
||
QString QgsHollowScaleBarRenderer::visibleName() const | ||
{ | ||
return QObject::tr( "Hollow" ); | ||
} | ||
|
||
int QgsHollowScaleBarRenderer::sortKey() const | ||
{ | ||
return 8; | ||
} | ||
|
||
QgsHollowScaleBarRenderer *QgsHollowScaleBarRenderer::clone() const | ||
{ | ||
return new QgsHollowScaleBarRenderer( *this ); | ||
} | ||
|
||
void QgsHollowScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const | ||
{ | ||
if ( !context.painter() ) | ||
{ | ||
return; | ||
} | ||
QPainter *painter = context.painter(); | ||
|
||
const double scaledLabelBarSpace = context.convertToPainterUnits( settings.labelBarSpace(), QgsUnitTypes::RenderMillimeters ); | ||
const double scaledBoxContentSpace = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters ); | ||
const QFontMetricsF fontMetrics = QgsTextRenderer::fontMetrics( context, settings.textFormat() ); | ||
const double barTopPosition = scaledBoxContentSpace + ( settings.labelVerticalPlacement() == QgsScaleBarSettings::LabelAboveSegment ? fontMetrics.ascent() + scaledLabelBarSpace : 0 ); | ||
const double barHeight = context.convertToPainterUnits( settings.height(), QgsUnitTypes::RenderMillimeters ); | ||
|
||
painter->save(); | ||
if ( context.flags() & QgsRenderContext::Antialiasing ) | ||
painter->setRenderHint( QPainter::Antialiasing, true ); | ||
|
||
std::unique_ptr< QgsLineSymbol > lineSymbol( settings.lineSymbol()->clone() ); | ||
lineSymbol->startRender( context ); | ||
|
||
std::unique_ptr< QgsFillSymbol > fillSymbol1( settings.fillSymbol1()->clone() ); | ||
fillSymbol1->startRender( context ); | ||
|
||
std::unique_ptr< QgsFillSymbol > fillSymbol2( settings.fillSymbol2()->clone() ); | ||
fillSymbol2->startRender( context ); | ||
|
||
painter->setPen( Qt::NoPen ); | ||
painter->setBrush( Qt::NoBrush ); | ||
|
||
bool useColor = true; //alternate brush color/white | ||
const double xOffset = firstLabelXOffset( settings, context, scaleContext ); | ||
|
||
const QList<double> positions = segmentPositions( context, scaleContext, settings ); | ||
const QList<double> widths = segmentWidths( scaleContext, settings ); | ||
|
||
// draw the fill | ||
double minX = 0; | ||
double maxX = 0; | ||
QgsFillSymbol *currentSymbol = nullptr; | ||
for ( int i = 0; i < positions.size(); ++i ) | ||
{ | ||
if ( useColor ) //alternating colors | ||
{ | ||
currentSymbol = fillSymbol1.get(); | ||
} | ||
else //secondary fill | ||
{ | ||
currentSymbol = fillSymbol2.get(); | ||
} | ||
|
||
const double thisX = context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset; | ||
const double thisWidth = context.convertToPainterUnits( widths.at( i ), QgsUnitTypes::RenderMillimeters ); | ||
|
||
if ( i == 0 ) | ||
minX = thisX; | ||
if ( i == positions.size() - 1 ) | ||
maxX = thisX + thisWidth; | ||
|
||
QRectF segmentRect( thisX, barTopPosition, thisWidth, barHeight ); | ||
currentSymbol->renderPolygon( QPolygonF() | ||
<< segmentRect.topLeft() | ||
<< segmentRect.topRight() | ||
<< segmentRect.bottomRight() | ||
<< segmentRect.bottomLeft() | ||
<< segmentRect.topLeft(), nullptr, nullptr, context ); | ||
useColor = !useColor; | ||
} | ||
|
||
// and then the lines | ||
// note that we do this layer-by-layer, to avoid ugliness where the lines touch the outer rect | ||
for ( int layer = 0; layer < lineSymbol->symbolLayerCount(); ++layer ) | ||
{ | ||
// horizontal lines | ||
bool drawLine = false; | ||
for ( int i = 0; i < positions.size(); ++i ) | ||
{ | ||
drawLine = !drawLine; | ||
if ( !drawLine ) | ||
continue; | ||
|
||
const double lineX = context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset; | ||
const double lineLength = context.convertToPainterUnits( widths.at( i ), QgsUnitTypes::RenderMillimeters ); | ||
lineSymbol->renderPolyline( QPolygonF() | ||
<< QPointF( lineX, barTopPosition + barHeight / 2.0 ) | ||
<< QPointF( lineX + lineLength, barTopPosition + barHeight / 2.0 ), | ||
nullptr, context, layer ); | ||
} | ||
|
||
// vertical lines | ||
for ( int i = 1; i < positions.size(); ++i ) | ||
{ | ||
const double lineX = context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset; | ||
lineSymbol->renderPolyline( QPolygonF() | ||
<< QPointF( lineX, barTopPosition ) | ||
<< QPointF( lineX, barTopPosition + barHeight ), | ||
nullptr, context, layer ); | ||
} | ||
|
||
// outside line | ||
lineSymbol->renderPolyline( QPolygonF() | ||
<< QPointF( minX, barTopPosition ) | ||
<< QPointF( maxX, barTopPosition ) | ||
<< QPointF( maxX, barTopPosition + barHeight ) | ||
<< QPointF( minX, barTopPosition + barHeight ) | ||
<< QPointF( minX, barTopPosition ), | ||
nullptr, context, layer ); | ||
} | ||
|
||
lineSymbol->stopRender( context ); | ||
fillSymbol1->stopRender( context ); | ||
fillSymbol2->stopRender( context ); | ||
painter->restore(); | ||
|
||
//draw labels using the default method | ||
drawDefaultLabels( context, settings, scaleContext ); | ||
} | ||
|
||
bool QgsHollowScaleBarRenderer::applyDefaultSettings( QgsScaleBarSettings &settings ) const | ||
{ | ||
// null the fill symbols by default | ||
std::unique_ptr< QgsFillSymbol > fillSymbol = qgis::make_unique< QgsFillSymbol >(); | ||
std::unique_ptr< QgsSimpleFillSymbolLayer > fillSymbolLayer = qgis::make_unique< QgsSimpleFillSymbolLayer >(); | ||
fillSymbolLayer->setColor( QColor( 0, 0, 0 ) ); | ||
fillSymbolLayer->setBrushStyle( Qt::NoBrush ); | ||
fillSymbolLayer->setStrokeStyle( Qt::NoPen ); | ||
fillSymbol->changeSymbolLayer( 0, fillSymbolLayer->clone() ); | ||
settings.setFillSymbol1( fillSymbol.release() ); | ||
|
||
fillSymbol = qgis::make_unique< QgsFillSymbol >(); | ||
fillSymbolLayer->setColor( QColor( 255, 255, 255 ) ); | ||
fillSymbol->changeSymbolLayer( 0, fillSymbolLayer.release() ); | ||
settings.setFillSymbol2( fillSymbol.release() ); | ||
|
||
return true; | ||
} | ||
|
||
|
||
|
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,52 @@ | ||
/*************************************************************************** | ||
qgshollowscalebarrenderer.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 QGSHOLLOWSCALEBARRENDERER_H | ||
#define QGSHOLLOWSCALEBARRENDERER_H | ||
|
||
#include "qgis_core.h" | ||
#include "qgsscalebarrenderer.h" | ||
#include <QString> | ||
|
||
/** | ||
* \class QgsHollowScaleBarRenderer | ||
* \ingroup core | ||
* Scalebar style that draws a single box with alternating color for the segments, with horizontal lines through | ||
* alternating segments. AKA "South African" style. | ||
* \since QGIS 3.14 | ||
*/ | ||
class CORE_EXPORT QgsHollowScaleBarRenderer: public QgsScaleBarRenderer | ||
{ | ||
public: | ||
|
||
/** | ||
* Constructor for QgsHollowScaleBarRenderer. | ||
*/ | ||
QgsHollowScaleBarRenderer() = default; | ||
|
||
QString id() const override; | ||
QString visibleName() const override; | ||
int sortKey() const override; | ||
QgsHollowScaleBarRenderer *clone() const override SIP_FACTORY; | ||
|
||
void draw( QgsRenderContext &context, | ||
const QgsScaleBarSettings &settings, | ||
const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const override; | ||
bool applyDefaultSettings( QgsScaleBarSettings &settings ) const override; | ||
|
||
}; | ||
|
||
#endif // QGSHOLLOWSCALEBARRENDERER_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
Oops, something went wrong.