QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsapplication.h"
19 #include "qgsnumericformat.h"
20 #include "qgsbasicnumericformat.h"
21 #include "qgslinesymbollayer.h"
22 #include "qgssymbol.h"
23 #include "qgsfillsymbollayer.h"
24 #include "qgslinesymbol.h"
25 #include "qgsfillsymbol.h"
26 
28 {
29  mTextFormat.setSize( 12.0 );
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( QgsUnitTypes::RenderMillimeters );
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  mNumSegments = other.mNumSegments;
93  mNumSegmentsLeft = other.mNumSegmentsLeft;
94  mNumSubdivisions = other.mNumSubdivisions;
95  mSubdivisionsHeight = other.mSubdivisionsHeight;
96  mNumUnitsPerSegment = other.mNumUnitsPerSegment;
97  mNumMapUnitsPerScaleBarUnit = other.mNumMapUnitsPerScaleBarUnit;
98  mSegmentSizeMode = other.mSegmentSizeMode;
99  mMinBarWidth = other.mMinBarWidth;
100  mMaxBarWidth = other.mMaxBarWidth;
101  mUnitLabeling = other.mUnitLabeling;
102  mTextFormat = other.mTextFormat;
103  mLineSymbol.reset( other.mLineSymbol->clone() );
104  mDivisionLineSymbol.reset( other.mDivisionLineSymbol->clone() );
105  mSubdivisionLineSymbol.reset( other.mSubdivisionLineSymbol->clone() );
106  mFillSymbol.reset( other.mFillSymbol->clone() );
107  mAlternateFillSymbol.reset( other.mAlternateFillSymbol->clone() );
108  mHeight = other.mHeight;
109  mLabelBarSpace = other.mLabelBarSpace;
110  mLabelVerticalPlacement = other.mLabelVerticalPlacement;
111  mLabelHorizontalPlacement = other.mLabelHorizontalPlacement;
112  mBoxContentSpace = other.mBoxContentSpace;
113  mAlignment = other.mAlignment;
114  mUnits = other.mUnits;
115  mNumericFormat.reset( other.mNumericFormat->clone() );
116  return *this;
117 }
118 
120 {
121  return mFillSymbol->color();
122 }
123 
124 void QgsScaleBarSettings::setFillColor( const QColor &color )
125 {
126  mFillSymbol->setColor( color );
127 }
128 
130 {
131  return mAlternateFillSymbol->color();
132 }
133 
134 void QgsScaleBarSettings::setFillColor2( const QColor &color )
135 {
136  mAlternateFillSymbol->setColor( color );
137 }
138 
140 {
141  return mLineSymbol->color();
142 }
143 
144 void QgsScaleBarSettings::setLineColor( const QColor &color )
145 {
146  for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
147  {
148  symbol->setColor( color );
149  }
150 }
151 
153 {
154  return mLineSymbol->width();
155 }
156 
158 {
159  for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
160  {
161  symbol->setWidth( width );
162  symbol->setOutputUnit( QgsUnitTypes::RenderMillimeters );
163  }
164 }
165 
167 {
168  QPen pen( mLineSymbol->color() );
169  if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
170  {
171  pen.setJoinStyle( line->penJoinStyle() );
172  pen.setCapStyle( line->penCapStyle() );
173  }
174  pen.setWidthF( mLineSymbol->width() );
175  return pen;
176 }
177 
178 void QgsScaleBarSettings::setPen( const QPen &pen )
179 {
180  for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
181  {
182  symbol->setColor( pen.color() );
183  symbol->setWidth( pen.widthF() );
184  symbol->setOutputUnit( QgsUnitTypes::RenderMillimeters );
185  if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
186  {
187  line->setPenJoinStyle( pen.joinStyle() );
188  line->setPenCapStyle( pen.capStyle() );
189  }
190  }
191 }
192 
194 {
195  return mLineSymbol.get();
196 }
197 
199 {
200  mLineSymbol.reset( symbol );
201 }
202 
204 {
205  return mDivisionLineSymbol.get();
206 }
207 
209 {
210  mDivisionLineSymbol.reset( symbol );
211 }
212 
214 {
215  return mSubdivisionLineSymbol.get();
216 }
217 
219 {
220  mSubdivisionLineSymbol.reset( symbol );
221 }
222 
224 {
225  return mFillSymbol.get();
226 }
227 
229 {
230  mFillSymbol.reset( symbol );
231 }
232 
234 {
235  return mAlternateFillSymbol.get();
236 }
237 
239 {
240  mAlternateFillSymbol.reset( symbol );
241 }
242 
244 {
245  QBrush b;
246  b.setColor( mFillSymbol->color() );
247  if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
248  {
249  b.setStyle( fill->brushStyle() );
250  }
251 
252  return b;
253 }
254 
255 void QgsScaleBarSettings::setBrush( const QBrush &brush )
256 {
257  mFillSymbol->setColor( brush.color() );
258  if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mFillSymbol->symbolLayer( 0 ) ) )
259  {
260  fill->setBrushStyle( brush.style() );
261  }
262 }
263 
265 {
266  QBrush b;
267  b.setColor( mAlternateFillSymbol->color() );
268  if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
269  {
270  b.setStyle( fill->brushStyle() );
271  }
272 
273  return b;
274 }
275 
276 void QgsScaleBarSettings::setBrush2( const QBrush &brush )
277 {
278  mAlternateFillSymbol->setColor( brush.color() );
279  if ( QgsSimpleFillSymbolLayer *fill = dynamic_cast< QgsSimpleFillSymbolLayer * >( mAlternateFillSymbol->symbolLayer( 0 ) ) )
280  {
281  fill->setBrushStyle( brush.style() );
282  }
283 }
284 
285 Qt::PenJoinStyle QgsScaleBarSettings::lineJoinStyle() const
286 {
287  if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
288  {
289  return line->penJoinStyle();
290  }
291  return Qt::MiterJoin;
292 }
293 
294 void QgsScaleBarSettings::setLineJoinStyle( Qt::PenJoinStyle style )
295 {
296  for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
297  {
298  if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
299  {
300  line->setPenJoinStyle( style );
301  }
302  }
303 }
304 
305 Qt::PenCapStyle QgsScaleBarSettings::lineCapStyle() const
306 {
307  if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( mLineSymbol->symbolLayer( 0 ) ) )
308  {
309  return line->penCapStyle();
310  }
311  return Qt::FlatCap;
312 }
313 
314 void QgsScaleBarSettings::setLineCapStyle( Qt::PenCapStyle style )
315 {
316  for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
317  {
318  if ( QgsSimpleLineSymbolLayer *line = dynamic_cast< QgsSimpleLineSymbolLayer * >( symbol->symbolLayer( 0 ) ) )
319  {
320  line->setPenCapStyle( style );
321  }
322  }
323 }
324 
326 {
327  return mNumericFormat.get();
328 }
329 
331 {
332  mNumericFormat.reset( format );
333 }
334 
336 
QgsScaleBarSettings::fillColor2
Q_DECL_DEPRECATED QColor fillColor2() const
Returns the secondary color used for fills in the scalebar.
Definition: qgsscalebarsettings.cpp:129
QgsScaleBarSettings::lineSymbol
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:193
QgsScaleBarSettings::setPen
Q_DECL_DEPRECATED void setPen(const QPen &pen)
Sets the pen used for drawing outlines in the scalebar.
Definition: qgsscalebarsettings.cpp:178
QgsUnitTypes::RenderPoints
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:173
QgsScaleBarSettings::lineWidth
Q_DECL_DEPRECATED double lineWidth() const
Returns the line width in millimeters for lines in the scalebar.
Definition: qgsscalebarsettings.cpp:152
QgsScaleBarSettings::setLineCapStyle
Q_DECL_DEPRECATED void setLineCapStyle(Qt::PenCapStyle style)
Sets the cap style used when drawing the lines in the scalebar.
Definition: qgsscalebarsettings.cpp:314
QgsScaleBarSettings::fillSymbol
QgsFillSymbol * fillSymbol() const
Returns the primary fill symbol used to render the scalebar (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:223
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
QgsNumericFormat
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
Definition: qgsnumericformat.h:259
qgsapplication.h
qgsscalebarsettings.h
QgsScaleBarSettings::~QgsScaleBarSettings
~QgsScaleBarSettings()
QgsTextFormat::setColor
void setColor(const QColor &color)
Sets the color that text will be rendered in.
Definition: qgstextformat.cpp:302
QgsScaleBarSettings::brush2
Q_DECL_DEPRECATED QBrush brush2() const
Returns the secondary brush for the scalebar.
Definition: qgsscalebarsettings.cpp:264
QgsScaleBarSettings::setFillColor2
Q_DECL_DEPRECATED void setFillColor2(const QColor &color)
Sets the secondary color used for fills in the scalebar.
Definition: qgsscalebarsettings.cpp:134
QgsScaleBarSettings::lineCapStyle
Q_DECL_DEPRECATED Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
Definition: qgsscalebarsettings.cpp:305
QgsScaleBarSettings::fillColor
Q_DECL_DEPRECATED QColor fillColor() const
Returns the color used for fills in the scalebar.
Definition: qgsscalebarsettings.cpp:119
QgsSimpleLineSymbolLayer
A simple line symbol layer, which renders lines using a line in a variety of styles (e....
Definition: qgslinesymbollayer.h:43
QgsScaleBarSettings::QgsScaleBarSettings
QgsScaleBarSettings()
Constructor for QgsScaleBarSettings.
Definition: qgsscalebarsettings.cpp:27
QgsScaleBarSettings
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Definition: qgsscalebarsettings.h:40
QgsScaleBarSettings::pen
Q_DECL_DEPRECATED QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
Definition: qgsscalebarsettings.cpp:166
QgsLineSymbol
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgslinesymbol.h:29
QgsScaleBarSettings::operator=
QgsScaleBarSettings & operator=(const QgsScaleBarSettings &other)
Definition: qgsscalebarsettings.cpp:90
QgsScaleBarSettings::setLineColor
Q_DECL_DEPRECATED void setLineColor(const QColor &color)
Sets the color used for lines in the scalebar.
Definition: qgsscalebarsettings.cpp:144
QgsScaleBarSettings::setLineWidth
Q_DECL_DEPRECATED void setLineWidth(double width)
Sets the line width in millimeters for lines in the scalebar.
Definition: qgsscalebarsettings.cpp:157
QgsScaleBarSettings::setBrush2
Q_DECL_DEPRECATED void setBrush2(const QBrush &brush)
Sets the secondary brush used for filling the scalebar.
Definition: qgsscalebarsettings.cpp:276
QgsScaleBarSettings::setLineSymbol
void setLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:198
qgsfillsymbollayer.h
QgsScaleBarSettings::setFillColor
Q_DECL_DEPRECATED void setFillColor(const QColor &color)
Sets the color used for fills in the scalebar.
Definition: qgsscalebarsettings.cpp:124
QgsScaleBarSettings::setSubdivisionLineSymbol
void setSubdivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:218
QgsScaleBarSettings::lineColor
Q_DECL_DEPRECATED QColor lineColor() const
Returns the color used for lines in the scalebar.
Definition: qgsscalebarsettings.cpp:139
QgsScaleBarSettings::brush
Q_DECL_DEPRECATED QBrush brush() const
Returns the primary brush used for filling the scalebar.
Definition: qgsscalebarsettings.cpp:243
qgsnumericformat.h
qgsbasicnumericformat.h
QgsScaleBarSettings::lineJoinStyle
Q_DECL_DEPRECATED Qt::PenJoinStyle lineJoinStyle() const
Returns the join style used for drawing lines in the scalebar.
Definition: qgsscalebarsettings.cpp:285
QgsScaleBarSettings::divisionLineSymbol
QgsLineSymbol * divisionLineSymbol() const
Returns the line symbol used to render the scalebar divisions (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:203
qgslinesymbollayer.h
QgsScaleBarSettings::subdivisionLineSymbol
QgsLineSymbol * subdivisionLineSymbol() const
Returns the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:213
QgsScaleBarSettings::setFillSymbol
void setFillSymbol(QgsFillSymbol *symbol)
Sets the primary fill symbol used to render the scalebar (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:228
QgsSimpleFillSymbolLayer
Definition: qgsfillsymbollayer.h:43
QgsFillSymbol
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgsfillsymbol.h:29
QgsScaleBarSettings::setLineJoinStyle
Q_DECL_DEPRECATED void setLineJoinStyle(Qt::PenJoinStyle style)
Sets the join style used when drawing the lines in the scalebar.
Definition: qgsscalebarsettings.cpp:294
QgsTextFormat::setSizeUnit
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the size of rendered text.
Definition: qgstextformat.cpp:269
QgsScaleBarSettings::setDivisionLineSymbol
void setDivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar divisions (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:208
QgsScaleBarSettings::setBrush
Q_DECL_DEPRECATED void setBrush(const QBrush &brush)
Sets the primary brush used for filling the scalebar.
Definition: qgsscalebarsettings.cpp:255
QgsTextFormat::setSize
void setSize(double size)
Sets the size for rendered text.
Definition: qgstextformat.cpp:291
QgsScaleBarSettings::setAlternateFillSymbol
void setAlternateFillSymbol(QgsFillSymbol *symbol)
Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:238
QgsScaleBarSettings::numericFormat
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
Definition: qgsscalebarsettings.cpp:325
QgsScaleBarSettings::alternateFillSymbol
QgsFillSymbol * alternateFillSymbol() const
Returns the secondary fill symbol used to render the scalebar (only used for some scalebar types).
Definition: qgsscalebarsettings.cpp:233
qgsfillsymbol.h
QgsScaleBarSettings::setNumericFormat
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the scalebar.
Definition: qgsscalebarsettings.cpp:330
qgssymbol.h
qgslinesymbol.h