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