QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsticksscalebarrenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsticksscalebarrenderer.cpp
3 ----------------------------
4 begin : June 2008
5 copyright : (C) 2008 by Marco Hugentobler
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include "qgslinesymbol.h"
20#include "qgsscalebarsettings.h"
21#include "qgssymbol.h"
22#include "qgstextrenderer.h"
23
24#include <QPainter>
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
34
36{
37 switch ( mTickPosition )
38 {
39 case TicksUp:
40 return u"Line Ticks Up"_s;
41 case TicksDown:
42 return u"Line Ticks Down"_s;
43 case TicksMiddle:
44 return u"Line Ticks Middle"_s;
45 }
46 return QString(); // to make gcc happy
47}
48
50{
51 switch ( mTickPosition )
52 {
53 case TicksUp:
54 return QObject::tr( "Line Ticks Up" );
55 case TicksDown:
56 return QObject::tr( "Line Ticks Down" );
57 case TicksMiddle:
58 return QObject::tr( "Line Ticks Middle" );
59 }
60 return QString(); // to make gcc happy
61
62}
63
65{
66 switch ( mTickPosition )
67 {
68 case TicksUp:
69 return 5;
70 case TicksDown:
71 return 4;
72 case TicksMiddle:
73 return 3;
74 }
75 return 6;
76}
77
93
98
99void QgsTicksScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
100{
101 if ( !context.painter() )
102 return;
103
104 QPainter *painter = context.painter();
105
106 const double scaledLabelBarSpace = context.convertToPainterUnits( settings.labelBarSpace(), Qgis::RenderUnit::Millimeters );
107 const double scaledBoxContentSpace = context.convertToPainterUnits( settings.boxContentSpace(), Qgis::RenderUnit::Millimeters );
108 const QFontMetricsF fontMetrics = QgsTextRenderer::fontMetrics( context, settings.textFormat() );
109 const double barTopPosition = scaledBoxContentSpace + ( settings.labelVerticalPlacement() == Qgis::ScaleBarDistanceLabelVerticalPlacement::AboveSegment ? fontMetrics.ascent() + scaledLabelBarSpace : 0 );
110 const double scaledHeight = context.convertToPainterUnits( settings.height(), Qgis::RenderUnit::Millimeters );
111 const double scaledSubdivisionsHeight = context.convertToPainterUnits( settings.subdivisionsHeight(), Qgis::RenderUnit::Millimeters );
112 const double scaledMaxHeight = ( ( settings.numberOfSubdivisions() > 1 ) && ( scaledSubdivisionsHeight > scaledHeight ) ) ? scaledSubdivisionsHeight : scaledHeight;
113 const double middlePosition = barTopPosition + scaledMaxHeight / 2.0;
114 const double bottomPosition = barTopPosition + scaledMaxHeight;
115
116 const double xOffset = firstLabelXOffset( settings, context, scaleContext );
117
118 painter->save();
119 context.setPainterFlagsUsingContext( painter );
120
121 std::unique_ptr< QgsLineSymbol > symbol( settings.lineSymbol()->clone() );
122 symbol->startRender( context );
123
124 std::unique_ptr< QgsLineSymbol > divisionSymbol( settings.divisionLineSymbol()->clone() );
125 divisionSymbol->startRender( context );
126
127 std::unique_ptr< QgsLineSymbol > subdivisionSymbol( settings.subdivisionLineSymbol()->clone() );
128 subdivisionSymbol->startRender( context );
129
130 const QList<double> positions = segmentPositions( context, scaleContext, settings );
131
132 // vertical positions
133 double verticalPos = 0.0;
134 QList<double> subTickPositionsY;
135 QList<double> tickPositionsY;
136 switch ( mTickPosition )
137 {
138 case TicksDown:
139 verticalPos = barTopPosition;
140 subTickPositionsY << verticalPos;
141 subTickPositionsY << verticalPos + scaledSubdivisionsHeight;
142 tickPositionsY << verticalPos;
143 tickPositionsY << verticalPos + scaledHeight;
144 break;
145 case TicksMiddle:
146 verticalPos = middlePosition;
147 subTickPositionsY << verticalPos + scaledSubdivisionsHeight / 2.0;
148 subTickPositionsY << verticalPos - scaledSubdivisionsHeight / 2.0;
149 tickPositionsY << verticalPos + scaledHeight / 2.0;
150 tickPositionsY << verticalPos - scaledHeight / 2.0;
151 break;
152 case TicksUp:
153 verticalPos = bottomPosition;
154 subTickPositionsY << verticalPos;
155 subTickPositionsY << verticalPos - scaledSubdivisionsHeight;
156 tickPositionsY << verticalPos;
157 tickPositionsY << verticalPos - scaledHeight;
158 break;
159 }
160
161 int symbolLayerCount = symbol->symbolLayerCount();
162 symbolLayerCount = std::max( symbolLayerCount, divisionSymbol->symbolLayerCount() );
163 symbolLayerCount = std::max( symbolLayerCount, subdivisionSymbol->symbolLayerCount() );
164
165 // we render the bar symbol-layer-by-symbol-layer, to avoid ugliness where the lines overlap in multi-layer symbols
166 for ( int layer = 0; layer < symbolLayerCount; ++ layer )
167 {
168 const bool drawDivisionsForThisSymbolLayer = layer < divisionSymbol->symbolLayerCount();
169 const bool drawSubdivisionsForThisSymbolLayer = layer < subdivisionSymbol->symbolLayerCount();
170 const bool drawLineForThisSymbolLayer = layer < symbol->symbolLayerCount();
171
172 if ( drawDivisionsForThisSymbolLayer )
173 {
174 // first draw the vertical lines for segments
175 for ( int i = 0; i < positions.size(); ++i )
176 {
177 const double thisX = context.convertToPainterUnits( positions.at( i ), Qgis::RenderUnit::Millimeters ) + xOffset;
178 divisionSymbol->renderPolyline( QPolygonF() << QPointF( thisX, tickPositionsY.at( 0 ) )
179 << QPointF( thisX, tickPositionsY.at( 1 ) ), nullptr, context, layer );
180 }
181 }
182
183 // draw the vertical lines for right subdivisions
184 if ( drawSubdivisionsForThisSymbolLayer )
185 {
186 for ( int i = settings.numberOfSegmentsLeft(); i < positions.size(); ++i )
187 {
188 for ( int j = 1; j < settings.numberOfSubdivisions(); ++j )
189 {
190 const double thisSubX = context.convertToPainterUnits( positions.at( i ) + j * scaleContext.segmentWidth / settings.numberOfSubdivisions(), Qgis::RenderUnit::Millimeters ) + xOffset;
191 subdivisionSymbol->renderPolyline( QPolygonF() << QPointF( thisSubX, subTickPositionsY.at( 0 ) )
192 << QPointF( thisSubX, subTickPositionsY.at( 1 ) ), nullptr, context, layer );
193 }
194 }
195 }
196
197 //draw last tick and horizontal line
198 if ( !positions.isEmpty() )
199 {
200 const double lastTickPositionX = context.convertToPainterUnits( positions.at( positions.size() - 1 ) + scaleContext.segmentWidth, Qgis::RenderUnit::Millimeters ) + xOffset;
201
202 //last vertical line
203 if ( drawDivisionsForThisSymbolLayer )
204 {
205 divisionSymbol->renderPolyline( QPolygonF() << QPointF( lastTickPositionX, tickPositionsY.at( 0 ) )
206 << QPointF( lastTickPositionX, tickPositionsY.at( 1 ) ),
207 nullptr, context, layer );
208 }
209
210 //horizontal line
211 if ( drawLineForThisSymbolLayer )
212 {
213 symbol->renderPolyline( QPolygonF() << QPointF( xOffset + context.convertToPainterUnits( positions.at( 0 ), Qgis::RenderUnit::Millimeters ), verticalPos )
214 << QPointF( lastTickPositionX, verticalPos ), nullptr, context, layer );
215 }
216 }
217 }
218
219 symbol->stopRender( context );
220 divisionSymbol->stopRender( context );
221 subdivisionSymbol->stopRender( context );
222
223 painter->restore();
224
225 //draw labels using the default method
226 drawDefaultLabels( context, settings, scaleContext );
227}
228
229
@ AboveSegment
Labels are drawn above the scalebar.
Definition qgis.h:5428
@ Millimeters
Millimeters.
Definition qgis.h:5280
QgsLineSymbol * clone() const override
Returns a deep copy of this symbol.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
void drawDefaultLabels(QgsRenderContext &context, const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const
Draws default scalebar labels using the specified settings and scaleContext to a destination render c...
Q_DECL_DEPRECATED QList< double > segmentPositions(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of positions for each segment within the scalebar.
Q_DECL_DEPRECATED double firstLabelXOffset(const QgsScaleBarSettings &settings) const
Returns the x-offset (in millimeters) used for the first label in the scalebar.
@ FlagUsesLabelVerticalPlacement
Renderer uses the QgsScaleBarSettings::labelVerticalPlacement() setting.
@ FlagUsesLabelHorizontalPlacement
Renderer uses the QgsScaleBarSettings::labelHorizontalPlacement() setting.
@ FlagUsesLineSymbol
Renderer utilizes the scalebar line symbol (see QgsScaleBarSettings::lineSymbol() ).
@ FlagUsesUnitLabel
Renderer uses the QgsScaleBarSettings::unitLabel() setting.
@ FlagUsesSubdivisionsHeight
Renderer uses the scalebar subdivisions height (see QgsScaleBarSettings::subdivisionsHeight() ).
@ FlagUsesSubdivisionSymbol
Renderer utilizes the scalebar subdivision symbol (see QgsScaleBarSettings::subdivisionLineSymbol() )...
@ FlagUsesSegments
Renderer uses the scalebar segments.
@ FlagUsesLabelBarSpace
Renderer uses the QgsScaleBarSettings::labelBarSpace() setting.
@ FlagRespectsMapUnitsPerScaleBarUnit
Renderer respects the QgsScaleBarSettings::mapUnitsPerScaleBarUnit() setting.
@ FlagRespectsUnits
Renderer respects the QgsScaleBarSettings::units() setting.
@ FlagUsesSubdivisions
Renderer uses the scalebar subdivisions (see QgsScaleBarSettings::numberOfSubdivisions() ).
@ FlagUsesDivisionSymbol
Renderer utilizes the scalebar division symbol (see QgsScaleBarSettings::divisionLineSymbol() ).
Stores the appearance and layout settings for scalebar drawing with QgsScaleBarRenderer.
double subdivisionsHeight() const
Returns the scalebar subdivisions height (in millimeters) for segments included in the right part of ...
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
QgsLineSymbol * subdivisionLineSymbol() const
Returns the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
Qgis::ScaleBarDistanceLabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters.
int numberOfSubdivisions() const
Returns the number of subdivisions for segments included in the right part of the scalebar (only used...
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
double height() const
Returns the scalebar height (in millimeters).
int numberOfSegmentsLeft() const
Returns the number of segments included in the left part of the scalebar.
QgsLineSymbol * divisionLineSymbol() const
Returns the line symbol used to render the scalebar divisions (only used for some scalebar types).
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor=1.0)
Returns the font metrics for the given text format, when rendered in the specified render context.
@ TicksUp
Render ticks above line.
@ TicksMiddle
Render ticks crossing line.
@ TicksDown
Render ticks below line.
Flags flags() const override
Returns the scalebar rendering flags, which dictates the renderer's behavior.
QString visibleName() const override
Returns the user friendly, translated name for the renderer.
int sortKey() const override
Returns a sorting key value, where renderers with a lower sort key will be shown earlier in lists.
QgsTicksScaleBarRenderer(TickPosition position=TicksMiddle)
Constructor for QgsTicksScaleBarRenderer.
void draw(QgsRenderContext &context, const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const override
Draws the scalebar using the specified settings and scaleContext to a destination render context.
QString id() const override
Returns the unique ID for this renderer.
QgsTicksScaleBarRenderer * clone() const override
Returns a clone of the renderer.
Contains parameters regarding scalebar calculations.
double segmentWidth
The width, in millimeters, of each individual segment drawn.