QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsscalebarsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsscalebarsettings.cpp
3 -----------------------
4 begin : January 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
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
17#include "qgsscalebarsettings.h"
18#include "qgsnumericformat.h"
20#include "qgslinesymbollayer.h"
21#include "qgssymbol.h"
22#include "qgsfillsymbollayer.h"
23#include "qgslinesymbol.h"
24#include "qgsfillsymbol.h"
25
27{
28 mTextFormat.setSize( 12.0 );
30 mTextFormat.setColor( QColor( 0, 0, 0 ) );
31
32 mNumericFormat = std::make_unique< QgsBasicNumericFormat >();
33
34 mLineSymbol = std::make_unique< QgsLineSymbol >();
35 mLineSymbol->setColor( QColor( 0, 0, 0 ) );
36 mLineSymbol->setWidth( 0.3 );
37 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
38 {
39 line->setPenJoinStyle( Qt::MiterJoin );
40 line->setPenCapStyle( Qt::SquareCap );
41 }
42 mLineSymbol->setOutputUnit( Qgis::RenderUnit::Millimeters );
43 mDivisionLineSymbol.reset( mLineSymbol->clone() );
44 mSubdivisionLineSymbol.reset( mLineSymbol->clone() );
45
46 mFillSymbol = std::make_unique< QgsFillSymbol >();
47 mFillSymbol->setColor( QColor( 0, 0, 0 ) );
48 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
49 {
50 fill->setStrokeStyle( Qt::NoPen );
51 }
52 mAlternateFillSymbol = std::make_unique< QgsFillSymbol >();
53 mAlternateFillSymbol->setColor( QColor( 255, 255, 255 ) );
54 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
55 {
56 fill->setStrokeStyle( Qt::NoPen );
57 }
58}
59
61 : mNumSegments( other.mNumSegments )
62 , mNumSegmentsLeft( other.mNumSegmentsLeft )
63 , mNumSubdivisions( other.mNumSubdivisions )
64 , mSubdivisionsHeight( other.mSubdivisionsHeight )
65 , mNumUnitsPerSegment( other.mNumUnitsPerSegment )
66 , mNumMapUnitsPerScaleBarUnit( other.mNumMapUnitsPerScaleBarUnit )
67 , mSegmentSizeMode( other.mSegmentSizeMode )
68 , mMinBarWidth( other.mMinBarWidth )
69 , mMaxBarWidth( other.mMaxBarWidth )
70 , mUnitLabeling( other.mUnitLabeling )
71 , mTextFormat( other.mTextFormat )
72 , mHeight( other.mHeight )
73 , mLineSymbol( other.mLineSymbol->clone() )
74 , mDivisionLineSymbol( other.mDivisionLineSymbol->clone() )
75 , mSubdivisionLineSymbol( other.mSubdivisionLineSymbol->clone() )
76 , mFillSymbol( other.mFillSymbol->clone() )
77 , mAlternateFillSymbol( other.mAlternateFillSymbol->clone() )
78 , mLabelBarSpace( other.mLabelBarSpace )
79 , mLabelVerticalPlacement( other.mLabelVerticalPlacement )
80 , mLabelHorizontalPlacement( other.mLabelHorizontalPlacement )
81 , mBoxContentSpace( other.mBoxContentSpace )
82 , mAlignment( other.mAlignment )
83 , mUnits( other.mUnits )
84 , mNumericFormat( other.mNumericFormat->clone() )
85{
86
87}
88
90{
91 mNumSegments = other.mNumSegments;
92 mNumSegmentsLeft = other.mNumSegmentsLeft;
93 mNumSubdivisions = other.mNumSubdivisions;
94 mSubdivisionsHeight = other.mSubdivisionsHeight;
95 mNumUnitsPerSegment = other.mNumUnitsPerSegment;
96 mNumMapUnitsPerScaleBarUnit = other.mNumMapUnitsPerScaleBarUnit;
97 mSegmentSizeMode = other.mSegmentSizeMode;
98 mMinBarWidth = other.mMinBarWidth;
99 mMaxBarWidth = other.mMaxBarWidth;
100 mUnitLabeling = other.mUnitLabeling;
101 mTextFormat = other.mTextFormat;
102 mLineSymbol.reset( other.mLineSymbol->clone() );
103 mDivisionLineSymbol.reset( other.mDivisionLineSymbol->clone() );
104 mSubdivisionLineSymbol.reset( other.mSubdivisionLineSymbol->clone() );
105 mFillSymbol.reset( other.mFillSymbol->clone() );
106 mAlternateFillSymbol.reset( other.mAlternateFillSymbol->clone() );
107 mHeight = other.mHeight;
108 mLabelBarSpace = other.mLabelBarSpace;
109 mLabelVerticalPlacement = other.mLabelVerticalPlacement;
110 mLabelHorizontalPlacement = other.mLabelHorizontalPlacement;
111 mBoxContentSpace = other.mBoxContentSpace;
112 mAlignment = other.mAlignment;
113 mUnits = other.mUnits;
114 mNumericFormat.reset( other.mNumericFormat->clone() );
115 return *this;
116}
117
119{
120 return mFillSymbol->color();
121}
122
123void QgsScaleBarSettings::setFillColor( const QColor &color )
124{
125 mFillSymbol->setColor( color );
126}
127
129{
130 return mAlternateFillSymbol->color();
131}
132
133void QgsScaleBarSettings::setFillColor2( const QColor &color )
134{
135 mAlternateFillSymbol->setColor( color );
136}
137
139{
140 return mLineSymbol->color();
141}
142
143void QgsScaleBarSettings::setLineColor( const QColor &color )
144{
145 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
146 {
147 symbol->setColor( color );
148 }
149}
150
152{
153 return mLineSymbol->width();
154}
155
157{
158 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
159 {
160 symbol->setWidth( width );
161 symbol->setOutputUnit( Qgis::RenderUnit::Millimeters );
162 }
163}
164
166{
167 QPen pen( mLineSymbol->color() );
168 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
169 {
170 pen.setJoinStyle( line->penJoinStyle() );
171 pen.setCapStyle( line->penCapStyle() );
172 }
173 pen.setWidthF( mLineSymbol->width() );
174 return pen;
175}
176
177void QgsScaleBarSettings::setPen( const QPen &pen )
178{
179 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
180 {
181 symbol->setColor( pen.color() );
182 symbol->setWidth( pen.widthF() );
183 symbol->setOutputUnit( Qgis::RenderUnit::Millimeters );
184 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
185 {
186 line->setPenJoinStyle( pen.joinStyle() );
187 line->setPenCapStyle( pen.capStyle() );
188 }
189 }
190}
191
193{
194 return mLineSymbol.get();
195}
196
198{
199 mLineSymbol.reset( symbol );
200}
201
203{
204 return mDivisionLineSymbol.get();
205}
206
208{
209 mDivisionLineSymbol.reset( symbol );
210}
211
213{
214 return mSubdivisionLineSymbol.get();
215}
216
218{
219 mSubdivisionLineSymbol.reset( symbol );
220}
221
223{
224 return mFillSymbol.get();
225}
226
228{
229 mFillSymbol.reset( symbol );
230}
231
233{
234 return mAlternateFillSymbol.get();
235}
236
238{
239 mAlternateFillSymbol.reset( symbol );
240}
241
243{
244 QBrush b;
245 b.setColor( mFillSymbol->color() );
246 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
247 {
248 b.setStyle( fill->brushStyle() );
249 }
250
251 return b;
252}
253
254void QgsScaleBarSettings::setBrush( const QBrush &brush )
255{
256 mFillSymbol->setColor( brush.color() );
257 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
258 {
259 fill->setBrushStyle( brush.style() );
260 }
261}
262
264{
265 QBrush b;
266 b.setColor( mAlternateFillSymbol->color() );
267 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
268 {
269 b.setStyle( fill->brushStyle() );
270 }
271
272 return b;
273}
274
275void QgsScaleBarSettings::setBrush2( const QBrush &brush )
276{
277 mAlternateFillSymbol->setColor( brush.color() );
278 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
279 {
280 fill->setBrushStyle( brush.style() );
281 }
282}
283
285{
286 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
287 {
288 return line->penJoinStyle();
289 }
290 return Qt::MiterJoin;
291}
292
293void QgsScaleBarSettings::setLineJoinStyle( Qt::PenJoinStyle style )
294{
295 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
296 {
297 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
298 {
299 line->setPenJoinStyle( style );
300 }
301 }
302}
303
305{
306 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
307 {
308 return line->penCapStyle();
309 }
310 return Qt::FlatCap;
311}
312
313void QgsScaleBarSettings::setLineCapStyle( Qt::PenCapStyle style )
314{
315 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
316 {
317 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
318 {
319 line->setPenCapStyle( style );
320 }
321 }
322}
323
325{
326 return mNumericFormat.get();
327}
328
330{
331 mNumericFormat.reset( format );
332}
333
335
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A line symbol type, for rendering LineString and MultiLineString geometries.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
void setSubdivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
Q_DECL_DEPRECATED QColor fillColor() const
Returns the color used for fills in the scalebar.
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).
Q_DECL_DEPRECATED QColor fillColor2() const
Returns the secondary color used for fills in the scalebar.
void setAlternateFillSymbol(QgsFillSymbol *symbol)
Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types).
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
Q_DECL_DEPRECATED void setFillColor(const QColor &color)
Sets the color used for fills in the scalebar.
Q_DECL_DEPRECATED void setFillColor2(const QColor &color)
Sets the secondary color used for fills in the scalebar.
Q_DECL_DEPRECATED void setLineCapStyle(Qt::PenCapStyle style)
Sets the cap style used when drawing the lines in the scalebar.
void setFillSymbol(QgsFillSymbol *symbol)
Sets the primary fill symbol used to render the scalebar (only used for some scalebar types).
Q_DECL_DEPRECATED void setLineJoinStyle(Qt::PenJoinStyle style)
Sets the join style used when drawing the lines in the scalebar.
Q_DECL_DEPRECATED QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
Q_DECL_DEPRECATED void setPen(const QPen &pen)
Sets the pen used for drawing outlines in the scalebar.
Q_DECL_DEPRECATED void setBrush(const QBrush &brush)
Sets the primary brush used for filling the scalebar.
Q_DECL_DEPRECATED void setBrush2(const QBrush &brush)
Sets the secondary brush used for filling the scalebar.
Q_DECL_DEPRECATED void setLineColor(const QColor &color)
Sets the color used for lines in the scalebar.
void setLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar (only used for some scalebar types).
void setDivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar divisions (only used for some scalebar types).
Q_DECL_DEPRECATED void setLineWidth(double width)
Sets the line width in millimeters for lines in the scalebar.
QgsFillSymbol * alternateFillSymbol() const
Returns the secondary fill symbol used to render the scalebar (only used for some scalebar types).
QgsFillSymbol * fillSymbol() const
Returns the primary fill symbol used to render the scalebar (only used for some scalebar types).
Q_DECL_DEPRECATED double lineWidth() const
Returns the line width in millimeters for lines in the scalebar.
Q_DECL_DEPRECATED QBrush brush2() const
Returns the secondary brush for the scalebar.
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the scalebar.
Q_DECL_DEPRECATED QColor lineColor() const
Returns the color used for lines in the scalebar.
Q_DECL_DEPRECATED Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
QgsScaleBarSettings & operator=(const QgsScaleBarSettings &other)
Q_DECL_DEPRECATED Qt::PenJoinStyle lineJoinStyle() const
Returns the join style used for drawing lines in the scalebar.
Q_DECL_DEPRECATED QBrush brush() const
Returns the primary brush used for filling the scalebar.
QgsLineSymbol * divisionLineSymbol() const
Returns the line symbol used to render the scalebar divisions (only used for some scalebar types).
Renders polygons using a single fill and stroke color.
A simple line symbol layer, which renders lines using a line in a variety of styles (e....
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.