QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
89{
90 if ( &other == this )
91 return *this;
92
93 mNumSegments = other.mNumSegments;
94 mNumSegmentsLeft = other.mNumSegmentsLeft;
95 mNumSubdivisions = other.mNumSubdivisions;
96 mSubdivisionsHeight = other.mSubdivisionsHeight;
97 mNumUnitsPerSegment = other.mNumUnitsPerSegment;
98 mNumMapUnitsPerScaleBarUnit = other.mNumMapUnitsPerScaleBarUnit;
99 mSegmentSizeMode = other.mSegmentSizeMode;
100 mMinBarWidth = other.mMinBarWidth;
101 mMaxBarWidth = other.mMaxBarWidth;
102 mUnitLabeling = other.mUnitLabeling;
103 mTextFormat = other.mTextFormat;
104 mLineSymbol.reset( other.mLineSymbol->clone() );
105 mDivisionLineSymbol.reset( other.mDivisionLineSymbol->clone() );
106 mSubdivisionLineSymbol.reset( other.mSubdivisionLineSymbol->clone() );
107 mFillSymbol.reset( other.mFillSymbol->clone() );
108 mAlternateFillSymbol.reset( other.mAlternateFillSymbol->clone() );
109 mHeight = other.mHeight;
110 mLabelBarSpace = other.mLabelBarSpace;
111 mLabelVerticalPlacement = other.mLabelVerticalPlacement;
112 mLabelHorizontalPlacement = other.mLabelHorizontalPlacement;
113 mBoxContentSpace = other.mBoxContentSpace;
114 mAlignment = other.mAlignment;
115 mUnits = other.mUnits;
116 mNumericFormat.reset( other.mNumericFormat->clone() );
117 return *this;
118}
119
121{
122 return mFillSymbol->color();
123}
124
125void QgsScaleBarSettings::setFillColor( const QColor &color )
126{
127 mFillSymbol->setColor( color );
128}
129
131{
132 return mAlternateFillSymbol->color();
133}
134
135void QgsScaleBarSettings::setFillColor2( const QColor &color )
136{
137 mAlternateFillSymbol->setColor( color );
138}
139
141{
142 return mLineSymbol->color();
143}
144
145void QgsScaleBarSettings::setLineColor( const QColor &color )
146{
147 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
148 {
149 symbol->setColor( color );
150 }
151}
152
154{
155 return mLineSymbol->width();
156}
157
159{
160 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
161 {
162 symbol->setWidth( width );
163 symbol->setOutputUnit( Qgis::RenderUnit::Millimeters );
164 }
165}
166
168{
169 QPen pen( mLineSymbol->color() );
170 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
171 {
172 pen.setJoinStyle( line->penJoinStyle() );
173 pen.setCapStyle( line->penCapStyle() );
174 }
175 pen.setWidthF( mLineSymbol->width() );
176 return pen;
177}
178
180{
181 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
182 {
183 symbol->setColor( pen.color() );
184 symbol->setWidth( pen.widthF() );
185 symbol->setOutputUnit( Qgis::RenderUnit::Millimeters );
186 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
187 {
188 line->setPenJoinStyle( pen.joinStyle() );
189 line->setPenCapStyle( pen.capStyle() );
190 }
191 }
192}
193
195{
196 return mLineSymbol.get();
197}
198
200{
201 mLineSymbol.reset( symbol );
202}
203
205{
206 return mDivisionLineSymbol.get();
207}
208
210{
211 mDivisionLineSymbol.reset( symbol );
212}
213
215{
216 return mSubdivisionLineSymbol.get();
217}
218
220{
221 mSubdivisionLineSymbol.reset( symbol );
222}
223
225{
226 return mFillSymbol.get();
227}
228
230{
231 mFillSymbol.reset( symbol );
232}
233
235{
236 return mAlternateFillSymbol.get();
237}
238
240{
241 mAlternateFillSymbol.reset( symbol );
242}
243
245{
246 QBrush b;
247 b.setColor( mFillSymbol->color() );
248 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
249 {
250 b.setStyle( fill->brushStyle() );
251 }
252
253 return b;
254}
255
257{
258 mFillSymbol->setColor( brush.color() );
259 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
260 {
261 fill->setBrushStyle( brush.style() );
262 }
263}
264
266{
267 QBrush b;
268 b.setColor( mAlternateFillSymbol->color() );
269 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
270 {
271 b.setStyle( fill->brushStyle() );
272 }
273
274 return b;
275}
276
278{
279 mAlternateFillSymbol->setColor( brush.color() );
280 if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
281 {
282 fill->setBrushStyle( brush.style() );
283 }
284}
285
287{
288 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
289 {
290 return line->penJoinStyle();
291 }
292 return Qt::MiterJoin;
293}
294
295void QgsScaleBarSettings::setLineJoinStyle( Qt::PenJoinStyle style )
296{
297 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
298 {
299 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
300 {
301 line->setPenJoinStyle( style );
302 }
303 }
304}
305
307{
308 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
309 {
310 return line->penCapStyle();
311 }
312 return Qt::FlatCap;
313}
314
315void QgsScaleBarSettings::setLineCapStyle( Qt::PenCapStyle style )
316{
317 for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
318 {
319 if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
320 {
321 line->setPenCapStyle( style );
322 }
323 }
324}
325
327{
328 return mNumericFormat.get();
329}
330
332{
333 mNumericFormat.reset( format );
334}
335
@ Millimeters
Millimeters.
Definition qgis.h:5341
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5345
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.