QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
20#include "qgsfillsymbol.h"
21#include "qgsfillsymbollayer.h"
22#include "qgslinesymbol.h"
23#include "qgslinesymbollayer.h"
24#include "qgsnumericformat.h"
25#include "qgssymbol.h"
26
28{
29 mTextFormat.setSize( 12.0 );
30 mTextFormat.setSizeUnit( Qgis::RenderUnit::Points );
31 mTextFormat.setColor( QColor( 0, 0, 0 ) );
32
33 mNumericFormat = std::make_unique< QgsBasicNumericFormat >();
34
35 mLineSymbol = std::make_unique< QgsLineSymbol >();
36 mLineSymbol->setColor( QColor( 0, 0, 0 ) );
37 mLineSymbol->setWidth( 0.3 );
38 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
39 {
40 line->setPenJoinStyle( Qt::MiterJoin );
41 line->setPenCapStyle( Qt::SquareCap );
42 }
43 mLineSymbol->setOutputUnit( Qgis::RenderUnit::Millimeters );
44 mDivisionLineSymbol.reset( mLineSymbol->clone() );
45 mSubdivisionLineSymbol.reset( mLineSymbol->clone() );
46
47 mFillSymbol = std::make_unique< QgsFillSymbol >();
48 mFillSymbol->setColor( QColor( 0, 0, 0 ) );
49 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
50 {
51 fill->setStrokeStyle( Qt::NoPen );
52 }
53 mAlternateFillSymbol = std::make_unique< QgsFillSymbol >();
54 mAlternateFillSymbol->setColor( QColor( 255, 255, 255 ) );
55 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
56 {
57 fill->setStrokeStyle( Qt::NoPen );
58 }
59}
60
62 : mNumSegments( other.mNumSegments )
63 , mNumSegmentsLeft( other.mNumSegmentsLeft )
64 , mNumSubdivisions( other.mNumSubdivisions )
65 , mSubdivisionsHeight( other.mSubdivisionsHeight )
66 , mNumUnitsPerSegment( other.mNumUnitsPerSegment )
67 , mNumMapUnitsPerScaleBarUnit( other.mNumMapUnitsPerScaleBarUnit )
68 , mSegmentSizeMode( other.mSegmentSizeMode )
69 , mMinBarWidth( other.mMinBarWidth )
70 , mMaxBarWidth( other.mMaxBarWidth )
71 , mUnitLabeling( other.mUnitLabeling )
72 , mTextFormat( other.mTextFormat )
73 , mHeight( other.mHeight )
74 , mLineSymbol( other.mLineSymbol->clone() )
75 , mDivisionLineSymbol( other.mDivisionLineSymbol->clone() )
76 , mSubdivisionLineSymbol( other.mSubdivisionLineSymbol->clone() )
77 , mFillSymbol( other.mFillSymbol->clone() )
78 , mAlternateFillSymbol( other.mAlternateFillSymbol->clone() )
79 , mLabelBarSpace( other.mLabelBarSpace )
80 , mLabelVerticalPlacement( other.mLabelVerticalPlacement )
81 , mLabelHorizontalPlacement( other.mLabelHorizontalPlacement )
82 , mBoxContentSpace( other.mBoxContentSpace )
83 , mAlignment( other.mAlignment )
84 , mUnits( other.mUnits )
85 , mNumericFormat( other.mNumericFormat->clone() )
86{
87
88}
89
91{
92 if ( &other == this )
93 return *this;
94
95 mNumSegments = other.mNumSegments;
96 mNumSegmentsLeft = other.mNumSegmentsLeft;
97 mNumSubdivisions = other.mNumSubdivisions;
98 mSubdivisionsHeight = other.mSubdivisionsHeight;
99 mNumUnitsPerSegment = other.mNumUnitsPerSegment;
100 mNumMapUnitsPerScaleBarUnit = other.mNumMapUnitsPerScaleBarUnit;
101 mSegmentSizeMode = other.mSegmentSizeMode;
102 mMinBarWidth = other.mMinBarWidth;
103 mMaxBarWidth = other.mMaxBarWidth;
104 mUnitLabeling = other.mUnitLabeling;
105 mTextFormat = other.mTextFormat;
106 mLineSymbol.reset( other.mLineSymbol->clone() );
107 mDivisionLineSymbol.reset( other.mDivisionLineSymbol->clone() );
108 mSubdivisionLineSymbol.reset( other.mSubdivisionLineSymbol->clone() );
109 mFillSymbol.reset( other.mFillSymbol->clone() );
110 mAlternateFillSymbol.reset( other.mAlternateFillSymbol->clone() );
111 mHeight = other.mHeight;
112 mLabelBarSpace = other.mLabelBarSpace;
113 mLabelVerticalPlacement = other.mLabelVerticalPlacement;
114 mLabelHorizontalPlacement = other.mLabelHorizontalPlacement;
115 mBoxContentSpace = other.mBoxContentSpace;
116 mAlignment = other.mAlignment;
117 mUnits = other.mUnits;
118 mNumericFormat.reset( other.mNumericFormat->clone() );
119 return *this;
120}
121
123{
124 return mFillSymbol->color();
125}
126
127void QgsScaleBarSettings::setFillColor( const QColor &color )
128{
129 mFillSymbol->setColor( color );
130}
131
133{
134 return mAlternateFillSymbol->color();
135}
136
137void QgsScaleBarSettings::setFillColor2( const QColor &color )
138{
139 mAlternateFillSymbol->setColor( color );
140}
141
143{
144 return mLineSymbol->color();
145}
146
147void QgsScaleBarSettings::setLineColor( const QColor &color )
148{
149 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
150 {
151 symbol->setColor( color );
152 }
153}
154
156{
157 return mLineSymbol->width();
158}
159
161{
162 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
163 {
164 symbol->setWidth( width );
165 symbol->setOutputUnit( Qgis::RenderUnit::Millimeters );
166 }
167}
168
170{
171 QPen pen( mLineSymbol->color() );
172 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
173 {
174 pen.setJoinStyle( line->penJoinStyle() );
175 pen.setCapStyle( line->penCapStyle() );
176 }
177 pen.setWidthF( mLineSymbol->width() );
178 return pen;
179}
180
182{
183 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
184 {
185 symbol->setColor( pen.color() );
186 symbol->setWidth( pen.widthF() );
187 symbol->setOutputUnit( Qgis::RenderUnit::Millimeters );
188 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
189 {
190 line->setPenJoinStyle( pen.joinStyle() );
191 line->setPenCapStyle( pen.capStyle() );
192 }
193 }
194}
195
197{
198 return mLineSymbol.get();
199}
200
202{
203 mLineSymbol.reset( symbol );
204}
205
207{
208 return mDivisionLineSymbol.get();
209}
210
212{
213 mDivisionLineSymbol.reset( symbol );
214}
215
217{
218 return mSubdivisionLineSymbol.get();
219}
220
222{
223 mSubdivisionLineSymbol.reset( symbol );
224}
225
227{
228 return mFillSymbol.get();
229}
230
232{
233 mFillSymbol.reset( symbol );
234}
235
237{
238 return mAlternateFillSymbol.get();
239}
240
242{
243 mAlternateFillSymbol.reset( symbol );
244}
245
247{
248 QBrush b;
249 b.setColor( mFillSymbol->color() );
250 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
251 {
252 b.setStyle( fill->brushStyle() );
253 }
254
255 return b;
256}
257
259{
260 mFillSymbol->setColor( brush.color() );
261 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
262 {
263 fill->setBrushStyle( brush.style() );
264 }
265}
266
268{
269 QBrush b;
270 b.setColor( mAlternateFillSymbol->color() );
271 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
272 {
273 b.setStyle( fill->brushStyle() );
274 }
275
276 return b;
277}
278
280{
281 mAlternateFillSymbol->setColor( brush.color() );
282 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
283 {
284 fill->setBrushStyle( brush.style() );
285 }
286}
287
289{
290 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
291 {
292 return line->penJoinStyle();
293 }
294 return Qt::MiterJoin;
295}
296
297void QgsScaleBarSettings::setLineJoinStyle( Qt::PenJoinStyle style )
298{
299 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
300 {
301 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
302 {
303 line->setPenJoinStyle( style );
304 }
305 }
306}
307
309{
310 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
311 {
312 return line->penCapStyle();
313 }
314 return Qt::FlatCap;
315}
316
317void QgsScaleBarSettings::setLineCapStyle( Qt::PenCapStyle style )
318{
319 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
320 {
321 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
322 {
323 line->setPenCapStyle( style );
324 }
325 }
326}
327
329{
330 return mNumericFormat.get();
331}
332
334{
335 mNumericFormat.reset( format );
336}
337
339
@ Millimeters
Millimeters.
Definition qgis.h:5184
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5188
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
A line symbol type, for rendering LineString and MultiLineString geometries.
Abstract base class for numeric formatters, which allow for formatting a numeric value for display.
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) const
Sets the color for the symbol.