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 
QgsScaleBarSettings()25 QgsScaleBarSettings::QgsScaleBarSettings()
26 {
27   mTextFormat.setSize( 12.0 );
28   mTextFormat.setSizeUnit( QgsUnitTypes::RenderPoints );
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 
QgsScaleBarSettings(const QgsScaleBarSettings & other)59 QgsScaleBarSettings::QgsScaleBarSettings( const QgsScaleBarSettings &other )
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 
operator =(const QgsScaleBarSettings & other)88 QgsScaleBarSettings &QgsScaleBarSettings::operator=( const QgsScaleBarSettings &other )
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 
fillColor() const117 QColor QgsScaleBarSettings::fillColor() const
118 {
119   return mFillSymbol->color();
120 }
121 
setFillColor(const QColor & color)122 void QgsScaleBarSettings::setFillColor( const QColor &color )
123 {
124   mFillSymbol->setColor( color );
125 }
126 
fillColor2() const127 QColor QgsScaleBarSettings::fillColor2() const
128 {
129   return mAlternateFillSymbol->color();
130 }
131 
setFillColor2(const QColor & color)132 void QgsScaleBarSettings::setFillColor2( const QColor &color )
133 {
134   mAlternateFillSymbol->setColor( color );
135 }
136 
lineColor() const137 QColor QgsScaleBarSettings::lineColor() const
138 {
139   return mLineSymbol->color();
140 }
141 
setLineColor(const QColor & color)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 
lineWidth() const150 double QgsScaleBarSettings::lineWidth() const
151 {
152   return mLineSymbol->width();
153 }
154 
setLineWidth(double width)155 void QgsScaleBarSettings::setLineWidth( double width )
156 {
157   for ( QgsLineSymbol *symbol : { mLineSymbol.get(), mDivisionLineSymbol.get(), mSubdivisionLineSymbol.get() } )
158   {
159     symbol->setWidth( width );
160     symbol->setOutputUnit( QgsUnitTypes::RenderMillimeters );
161   }
162 }
163 
pen() const164 QPen QgsScaleBarSettings::pen() const
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 
setPen(const QPen & pen)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 
lineSymbol() const191 QgsLineSymbol *QgsScaleBarSettings::lineSymbol() const
192 {
193   return mLineSymbol.get();
194 }
195 
setLineSymbol(QgsLineSymbol * symbol)196 void QgsScaleBarSettings::setLineSymbol( QgsLineSymbol *symbol )
197 {
198   mLineSymbol.reset( symbol );
199 }
200 
divisionLineSymbol() const201 QgsLineSymbol *QgsScaleBarSettings::divisionLineSymbol() const
202 {
203   return mDivisionLineSymbol.get();
204 }
205 
setDivisionLineSymbol(QgsLineSymbol * symbol)206 void QgsScaleBarSettings::setDivisionLineSymbol( QgsLineSymbol *symbol )
207 {
208   mDivisionLineSymbol.reset( symbol );
209 }
210 
subdivisionLineSymbol() const211 QgsLineSymbol *QgsScaleBarSettings::subdivisionLineSymbol() const
212 {
213   return mSubdivisionLineSymbol.get();
214 }
215 
setSubdivisionLineSymbol(QgsLineSymbol * symbol)216 void QgsScaleBarSettings::setSubdivisionLineSymbol( QgsLineSymbol *symbol )
217 {
218   mSubdivisionLineSymbol.reset( symbol );
219 }
220 
fillSymbol() const221 QgsFillSymbol *QgsScaleBarSettings::fillSymbol() const
222 {
223   return mFillSymbol.get();
224 }
225 
setFillSymbol(QgsFillSymbol * symbol)226 void QgsScaleBarSettings::setFillSymbol( QgsFillSymbol *symbol )
227 {
228   mFillSymbol.reset( symbol );
229 }
230 
alternateFillSymbol() const231 QgsFillSymbol *QgsScaleBarSettings::alternateFillSymbol() const
232 {
233   return mAlternateFillSymbol.get();
234 }
235 
setAlternateFillSymbol(QgsFillSymbol * symbol)236 void QgsScaleBarSettings::setAlternateFillSymbol( QgsFillSymbol *symbol )
237 {
238   mAlternateFillSymbol.reset( symbol );
239 }
240 
brush() const241 QBrush QgsScaleBarSettings::brush() const
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 
setBrush(const QBrush & brush)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 
brush2() const262 QBrush QgsScaleBarSettings::brush2() const
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 
setBrush2(const QBrush & brush)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 
lineJoinStyle() const283 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 
setLineJoinStyle(Qt::PenJoinStyle style)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 
lineCapStyle() const303 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 
setLineCapStyle(Qt::PenCapStyle style)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 
numericFormat() const323 const QgsNumericFormat *QgsScaleBarSettings::numericFormat() const
324 {
325   return mNumericFormat.get();
326 }
327 
setNumericFormat(QgsNumericFormat * format)328 void QgsScaleBarSettings::setNumericFormat( QgsNumericFormat *format )
329 {
330   mNumericFormat.reset( format );
331 }
332 
333 QgsScaleBarSettings::~QgsScaleBarSettings() = default;
334 
335