QGIS API Documentation 4.3.0-Master (0cfde48c85b)
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
6 email : marco.hugentobler@karto.baug.ethz.ch
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
32
34{
35 switch ( mTickPosition )
36 {
37 case TicksUp:
38 return u"Line Ticks Up"_s;
39 case TicksDown:
40 return u"Line Ticks Down"_s;
41 case TicksMiddle:
42 return u"Line Ticks Middle"_s;
43 }
44 return QString(); // to make gcc happy
45}
46
48{
49 switch ( mTickPosition )
50 {
51 case TicksUp:
52 return QObject::tr( "Line Ticks Up" );
53 case TicksDown:
54 return QObject::tr( "Line Ticks Down" );
55 case TicksMiddle:
56 return QObject::tr( "Line Ticks Middle" );
57 }
58 return QString(); // to make gcc happy
59}
60
62{
63 switch ( mTickPosition )
64 {
65 case TicksUp:
66 return 5;
67 case TicksDown:
68 return 4;
69 case TicksMiddle:
70 return 3;
71 }
72 return 6;
73}
74
90
95
96void QgsTicksScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
97{
98 if ( !context.painter() )
99 return;
100
101 QPainter *painter = context.painter();
102
103 const double barTopPosition = verticalOffset( context, settings );
104 const double scaledHeight = context.convertToPainterUnits( settings.height(), Qgis::RenderUnit::Millimeters );
105 const double scaledSubdivisionsHeight = context.convertToPainterUnits( settings.subdivisionsHeight(), Qgis::RenderUnit::Millimeters );
106 const double scaledMaxHeight = ( ( settings.numberOfSubdivisions() > 1 ) && ( scaledSubdivisionsHeight > scaledHeight ) ) ? scaledSubdivisionsHeight : scaledHeight;
107 const double middlePosition = barTopPosition + scaledMaxHeight / 2.0;
108 const double bottomPosition = barTopPosition + scaledMaxHeight;
109
110 const double xOffset = firstLabelXOffset( settings, context, scaleContext );
111
112 painter->save();
113 context.setPainterFlagsUsingContext( painter );
114
115 std::unique_ptr< QgsLineSymbol > symbol( settings.lineSymbol()->clone() );
116 symbol->startRender( context );
117
118 std::unique_ptr< QgsLineSymbol > divisionSymbol( settings.divisionLineSymbol()->clone() );
119 divisionSymbol->startRender( context );
120
121 std::unique_ptr< QgsLineSymbol > subdivisionSymbol( settings.subdivisionLineSymbol()->clone() );
122 subdivisionSymbol->startRender( context );
123
124 const QList<double> positions = segmentPositions( context, scaleContext, settings );
125
126 // vertical positions
127 double verticalPos = 0.0;
128 QList<double> subTickPositionsY;
129 QList<double> tickPositionsY;
130 switch ( mTickPosition )
131 {
132 case TicksDown:
133 verticalPos = barTopPosition;
134 subTickPositionsY << verticalPos;
135 subTickPositionsY << verticalPos + scaledSubdivisionsHeight;
136 tickPositionsY << verticalPos;
137 tickPositionsY << verticalPos + scaledHeight;
138 break;
139 case TicksMiddle:
140 verticalPos = middlePosition;
141 subTickPositionsY << verticalPos + scaledSubdivisionsHeight / 2.0;
142 subTickPositionsY << verticalPos - scaledSubdivisionsHeight / 2.0;
143 tickPositionsY << verticalPos + scaledHeight / 2.0;
144 tickPositionsY << verticalPos - scaledHeight / 2.0;
145 break;
146 case TicksUp:
147 verticalPos = bottomPosition;
148 subTickPositionsY << verticalPos;
149 subTickPositionsY << verticalPos - scaledSubdivisionsHeight;
150 tickPositionsY << verticalPos;
151 tickPositionsY << verticalPos - scaledHeight;
152 break;
153 }
154
155 int symbolLayerCount = symbol->symbolLayerCount();
156 symbolLayerCount = std::max( symbolLayerCount, divisionSymbol->symbolLayerCount() );
157 symbolLayerCount = std::max( symbolLayerCount, subdivisionSymbol->symbolLayerCount() );
158
159 // we render the bar symbol-layer-by-symbol-layer, to avoid ugliness where the lines overlap in multi-layer symbols
160 for ( int layer = 0; layer < symbolLayerCount; ++layer )
161 {
162 const bool drawDivisionsForThisSymbolLayer = layer < divisionSymbol->symbolLayerCount();
163 const bool drawSubdivisionsForThisSymbolLayer = layer < subdivisionSymbol->symbolLayerCount();
164 const bool drawLineForThisSymbolLayer = layer < symbol->symbolLayerCount();
165
166 if ( drawDivisionsForThisSymbolLayer )
167 {
168 // first draw the vertical lines for segments
169 for ( int i = 0; i < positions.size(); ++i )
170 {
171 const double thisX = context.convertToPainterUnits( positions.at( i ), Qgis::RenderUnit::Millimeters ) + xOffset;
172 divisionSymbol->renderPolyline( QPolygonF() << QPointF( thisX, tickPositionsY.at( 0 ) ) << QPointF( thisX, tickPositionsY.at( 1 ) ), nullptr, context, layer );
173 }
174 }
175
176 // draw the vertical lines for right subdivisions
177 if ( drawSubdivisionsForThisSymbolLayer )
178 {
179 for ( int i = settings.numberOfSegmentsLeft(); i < positions.size(); ++i )
180 {
181 for ( int j = 1; j < settings.numberOfSubdivisions(); ++j )
182 {
183 const double thisSubX = context.convertToPainterUnits( positions.at( i ) + j * scaleContext.segmentWidth / settings.numberOfSubdivisions(), Qgis::RenderUnit::Millimeters ) + xOffset;
184 subdivisionSymbol->renderPolyline( QPolygonF() << QPointF( thisSubX, subTickPositionsY.at( 0 ) ) << QPointF( thisSubX, subTickPositionsY.at( 1 ) ), nullptr, context, layer );
185 }
186 }
187 }
188
189 //draw last tick and horizontal line
190 if ( !positions.isEmpty() )
191 {
192 const double lastTickPositionX = context.convertToPainterUnits( positions.at( positions.size() - 1 ) + scaleContext.segmentWidth, Qgis::RenderUnit::Millimeters ) + xOffset;
193
194 //last vertical line
195 if ( drawDivisionsForThisSymbolLayer )
196 {
197 divisionSymbol->renderPolyline( QPolygonF() << QPointF( lastTickPositionX, tickPositionsY.at( 0 ) ) << QPointF( lastTickPositionX, tickPositionsY.at( 1 ) ), nullptr, context, layer );
198 }
199
200 //horizontal line
201 if ( drawLineForThisSymbolLayer )
202 {
203 symbol->renderPolyline( QPolygonF() << QPointF( xOffset + context.convertToPainterUnits( positions.at( 0 ), Qgis::RenderUnit::Millimeters ), verticalPos ) << QPointF( lastTickPositionX, verticalPos ), nullptr, context, layer );
204 }
205 }
206 }
207
208 symbol->stopRender( context );
209 divisionSymbol->stopRender( context );
210 subdivisionSymbol->stopRender( context );
211
212 painter->restore();
213
214 //draw labels using the default method
215 drawDefaultLabels( context, settings, scaleContext );
216}
@ Millimeters
Millimeters.
Definition qgis.h:5759
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 ...
double verticalOffset(QgsRenderContext &context, const QgsScaleBarSettings &settings) const
Calculates the vertical offset to the top of the bar, accounting for text placed above the bar.
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).
int numberOfSubdivisions() const
Returns the number of subdivisions for segments included in the right part of the scalebar (only used...
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).
@ 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.