QGIS API Documentation  2.99.0-Master (9fdd060)
qgscomposerscalebar.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerscalebar.cpp
3  -------------------
4  begin : March 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : [email protected]
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 "qgscomposerscalebar.h"
18 #include "qgscomposermap.h"
19 #include "qgscomposition.h"
20 #include "qgscomposerutils.h"
21 #include "qgsdistancearea.h"
22 #include "qgsscalebarrenderer.h"
24 #include "qgsmapsettings.h"
28 #include "qgsrectangle.h"
29 #include "qgsproject.h"
30 #include "qgssymbollayerutils.h"
31 #include "qgsfontutils.h"
32 #include "qgsunittypes.h"
33 #include "qgssettings.h"
34 
35 #include <QDomDocument>
36 #include <QDomElement>
37 #include <QFontMetricsF>
38 #include <QPainter>
39 
40 #include <cmath>
41 
43  : QgsComposerItem( composition )
44  , mSegmentMillimeters( 0.0 )
45 {
48 }
49 
51 {
52  delete mStyle;
53 }
54 
55 void QgsComposerScaleBar::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
56 {
57  Q_UNUSED( itemStyle );
58  Q_UNUSED( pWidget );
59  if ( !mStyle || !painter )
60  {
61  return;
62  }
63  if ( !shouldDrawItem() )
64  {
65  return;
66  }
67 
68  drawBackground( painter );
69 
71 
72  mStyle->draw( c, mSettings, createScaleContext() );
73 
74  //draw frame and selection boxes if necessary
75  drawFrame( painter );
76  if ( isSelected() )
77  {
78  drawSelectionBoxes( painter );
79  }
80 }
81 
83 {
84  if ( !mStyle )
85  {
86  mSettings.setNumberOfSegments( nSegments );
87  return;
88  }
89  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
90  mSettings.setNumberOfSegments( nSegments );
91  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
92  correctXPositionAlignment( width, widthAfter );
93  emit itemChanged();
94 }
95 
97 {
98  if ( !mStyle )
99  {
100  mSettings.setUnitsPerSegment( units );
101  return;
102  }
103  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
104  mSettings.setUnitsPerSegment( units );
105  refreshSegmentMillimeters();
106  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
107  correctXPositionAlignment( width, widthAfter );
108  emit itemChanged();
109 }
110 
112 {
113  if ( !mStyle )
114  {
115  mSettings.setSegmentSizeMode( mode );
116  return;
117  }
118  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
119  mSettings.setSegmentSizeMode( mode );
120  refreshSegmentMillimeters();
121  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
122  correctXPositionAlignment( width, widthAfter );
123  emit itemChanged();
124 }
125 
126 void QgsComposerScaleBar::setMinBarWidth( double minWidth )
127 {
128  if ( !mStyle )
129  {
130  mSettings.setMinimumBarWidth( minWidth );
131  return;
132  }
133  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
134  mSettings.setMinimumBarWidth( minWidth );
135  refreshSegmentMillimeters();
136  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
137  correctXPositionAlignment( width, widthAfter );
138  emit itemChanged();
139 }
140 
141 void QgsComposerScaleBar::setMaxBarWidth( double maxWidth )
142 {
143  if ( !mStyle )
144  {
145  mSettings.setMaximumBarWidth( maxWidth );
146  return;
147  }
148  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
149  mSettings.setMaximumBarWidth( maxWidth );
150  refreshSegmentMillimeters();
151  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
152  correctXPositionAlignment( width, widthAfter );
153  emit itemChanged();
154 }
155 
157 {
158  if ( !mStyle )
159  {
160  mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
161  return;
162  }
163  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
164  mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
165  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
166  correctXPositionAlignment( width, widthAfter );
167  emit itemChanged();
168 }
169 
171 {
172  if ( !mStyle )
173  {
174  mSettings.setBoxContentSpace( space );
175  return;
176  }
177  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
178  mSettings.setBoxContentSpace( space );
179  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
180  correctXPositionAlignment( width, widthAfter );
181  emit itemChanged();
182 }
183 
185 {
186  if ( mComposerMap )
187  {
188  disconnect( mComposerMap, &QgsComposerMap::extentChanged, this, &QgsComposerScaleBar::updateSegmentSize );
189  disconnect( mComposerMap, &QObject::destroyed, this, &QgsComposerScaleBar::invalidateCurrentMap );
190  }
191  mComposerMap = map;
192 
193  if ( !map )
194  {
195  return;
196  }
197 
199  connect( mComposerMap, &QObject::destroyed, this, &QgsComposerScaleBar::invalidateCurrentMap );
200 
201  refreshSegmentMillimeters();
202  emit itemChanged();
203 }
204 
206 {
207  if ( !mComposerMap )
208  {
209  return;
210  }
211 
212  disconnect( mComposerMap, &QgsComposerMap::extentChanged, this, &QgsComposerScaleBar::updateSegmentSize );
213  disconnect( mComposerMap, &QObject::destroyed, this, &QgsComposerScaleBar::invalidateCurrentMap );
214  mComposerMap = nullptr;
215 }
216 
218 {
220  const QgsExpressionContext *evalContext = context ? context : &scopedContext;
221 
222  bool forceUpdate = false;
223  //updates data defined properties and redraws item to match
225  {
226  QBrush b = mSettings.brush();
227  b.setColor( mDataDefinedProperties.valueAsColor( QgsComposerObject::ScalebarFillColor, *evalContext, mSettings.fillColor() ) );
228  mSettings.setBrush( b );
229  forceUpdate = true;
230  }
232  {
233  QBrush b = mSettings.brush2();
234  b.setColor( mDataDefinedProperties.valueAsColor( QgsComposerObject::ScalebarFillColor2, *evalContext, mSettings.fillColor2() ) );
235  mSettings.setBrush2( b );
236  forceUpdate = true;
237  }
239  {
240  QPen p = mSettings.pen();
241  p.setColor( mDataDefinedProperties.valueAsColor( QgsComposerObject::ScalebarLineColor, *evalContext, mSettings.lineColor() ) );
242  mSettings.setPen( p );
243  forceUpdate = true;
244  }
246  {
247  QPen p = mSettings.pen();
248  p.setWidthF( mDataDefinedProperties.valueAsDouble( QgsComposerObject::ScalebarLineWidth, *evalContext, mSettings.lineWidth() ) );
249  mSettings.setPen( p );
250  forceUpdate = true;
251  }
252  if ( forceUpdate )
253  {
254  update();
255  }
256 
258 }
259 
260 // nextNiceNumber(4573.23, d) = 5000 (d=1) -> 4600 (d=10) -> 4580 (d=100) -> 4574 (d=1000) -> etc
261 inline double nextNiceNumber( double a, double d = 1 )
262 {
263  double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
264  return std::ceil( a / s ) * s;
265 }
266 
267 // prevNiceNumber(4573.23, d) = 4000 (d=1) -> 4500 (d=10) -> 4570 (d=100) -> 4573 (d=1000) -> etc
268 inline double prevNiceNumber( double a, double d = 1 )
269 {
270  double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
271  return std::floor( a / s ) * s;
272 }
273 
274 void QgsComposerScaleBar::refreshSegmentMillimeters()
275 {
276  if ( mComposerMap )
277  {
278  //get mm dimension of composer map
279  QRectF composerItemRect = mComposerMap->rect();
280 
282  {
283  //calculate size depending on mNumUnitsPerSegment
284  mSegmentMillimeters = composerItemRect.width() / mapWidth() * mSettings.unitsPerSegment();
285  }
286  else /*if(mSegmentSizeMode == SegmentSizeFitWidth)*/
287  {
288  if ( mSettings.maximumBarWidth() < mSettings.minimumBarWidth() )
289  {
290  mSegmentMillimeters = 0;
291  }
292  else
293  {
294  double nSegments = ( mSettings.numberOfSegmentsLeft() != 0 ) + mSettings.numberOfSegments();
295  // unitsPerSegments which fit minBarWidth resp. maxBarWidth
296  double minUnitsPerSeg = ( mSettings.minimumBarWidth() * mapWidth() ) / ( nSegments * composerItemRect.width() );
297  double maxUnitsPerSeg = ( mSettings.maximumBarWidth() * mapWidth() ) / ( nSegments * composerItemRect.width() );
298 
299  // Start with coarsest "nice" number closest to minUnitsPerSeg resp
300  // maxUnitsPerSeg, then proceed to finer numbers as long as neither
301  // lowerNiceUnitsPerSeg nor upperNiceUnitsPerSeg are are in
302  // [minUnitsPerSeg, maxUnitsPerSeg]
303  double lowerNiceUnitsPerSeg = nextNiceNumber( minUnitsPerSeg );
304  double upperNiceUnitsPerSeg = prevNiceNumber( maxUnitsPerSeg );
305 
306  double d = 1;
307  while ( lowerNiceUnitsPerSeg > maxUnitsPerSeg && upperNiceUnitsPerSeg < minUnitsPerSeg )
308  {
309  d *= 10;
310  lowerNiceUnitsPerSeg = nextNiceNumber( minUnitsPerSeg, d );
311  upperNiceUnitsPerSeg = prevNiceNumber( maxUnitsPerSeg, d );
312  }
313 
314  // Pick mNumUnitsPerSegment from {lowerNiceUnitsPerSeg, upperNiceUnitsPerSeg}, use the larger if possible
315  mSettings.setUnitsPerSegment( upperNiceUnitsPerSeg < minUnitsPerSeg ? lowerNiceUnitsPerSeg : upperNiceUnitsPerSeg );
316  mSegmentMillimeters = composerItemRect.width() / mapWidth() * mSettings.unitsPerSegment();
317  }
318  }
319  }
320 }
321 
322 double QgsComposerScaleBar::mapWidth() const
323 {
324  if ( !mComposerMap )
325  {
326  return 0.0;
327  }
328 
329  QgsRectangle composerMapRect = *( mComposerMap->currentMapExtent() );
330  if ( mSettings.units() == QgsUnitTypes::DistanceUnknownUnit )
331  {
332  return composerMapRect.width();
333  }
334  else
335  {
336  QgsDistanceArea da;
337  da.setSourceCrs( mComposerMap->crs() );
339 
341  double measure = da.measureLine( QgsPointXY( composerMapRect.xMinimum(), composerMapRect.yMinimum() ),
342  QgsPointXY( composerMapRect.xMaximum(), composerMapRect.yMinimum() ) );
343  measure /= QgsUnitTypes::fromUnitToUnitFactor( mSettings.units(), units );
344  return measure;
345  }
346 }
347 
348 QgsScaleBarRenderer::ScaleBarContext QgsComposerScaleBar::createScaleContext() const
349 {
351  scaleContext.size = rect().size();
352  scaleContext.segmentWidth = mSegmentMillimeters;
353  scaleContext.scale = mComposerMap ? mComposerMap->scale() : 1.0;
354  return scaleContext;
355 }
356 
358 {
359  mSettings.setAlignment( a );
360  update();
361  emit itemChanged();
362 }
363 
365 {
366  mSettings.setUnits( u );
367  refreshSegmentMillimeters();
368  emit itemChanged();
369 }
370 
372 {
373  if ( mSettings.lineJoinStyle() == style )
374  {
375  //no change
376  return;
377  }
378  mSettings.setLineJoinStyle( style );
379  update();
380  emit itemChanged();
381 }
382 
384 {
385  if ( mSettings.lineCapStyle() == style )
386  {
387  //no change
388  return;
389  }
390  mSettings.setLineCapStyle( style );
391  update();
392  emit itemChanged();
393 }
394 
396 {
397  //style
398  delete mStyle;
399  mStyle = new QgsSingleBoxScaleBarRenderer();
400 
401  //default to no background
402  setBackgroundEnabled( false );
403 
404  //get default composer font from settings
405  QgsSettings settings;
406  QString defaultFontString = settings.value( QStringLiteral( "Composer/defaultFont" ) ).toString();
407  QFont f;
408  if ( !defaultFontString.isEmpty() )
409  {
410  f.setFamily( defaultFontString );
411  }
412  f.setPointSizeF( 12.0 );
413  mSettings.setFont( f );
414 
416 
417  emit itemChanged();
418 }
419 
421 {
422  if ( mComposerMap )
423  {
424  setUnits( u );
425  double upperMagnitudeMultiplier = 1.0;
426  double widthInSelectedUnits = mapWidth();
427  double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
428  setNumUnitsPerSegment( initialUnitsPerSegment );
429 
430  switch ( u )
431  {
433  {
434  upperMagnitudeMultiplier = 1.0;
435  setUnitLabeling( tr( "units" ) );
436  break;
437  }
439  {
440  if ( initialUnitsPerSegment > 1000.0 )
441  {
442  upperMagnitudeMultiplier = 1000.0;
443  setUnitLabeling( tr( "km" ) );
444  }
445  else
446  {
447  upperMagnitudeMultiplier = 1.0;
448  setUnitLabeling( tr( "m" ) );
449  }
450  break;
451  }
453  {
454  if ( initialUnitsPerSegment > 5419.95 )
455  {
456  upperMagnitudeMultiplier = 5419.95;
457  setUnitLabeling( tr( "miles" ) );
458  }
459  else
460  {
461  upperMagnitudeMultiplier = 1.0;
462  setUnitLabeling( tr( "ft" ) );
463  }
464  break;
465  }
466 
467  default:
469  upperMagnitudeMultiplier = 1;
470  break;
471  }
472 
473  double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
474  int segmentMagnitude = std::floor( std::log10( segmentWidth ) );
475  double unitsPerSegment = upperMagnitudeMultiplier * ( std::pow( 10.0, segmentMagnitude ) );
476  double multiplier = std::floor( ( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
477 
478  if ( multiplier > 0 )
479  {
480  unitsPerSegment = unitsPerSegment * multiplier;
481  }
482  setNumUnitsPerSegment( unitsPerSegment );
483  setNumMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );
484 
485  setNumSegments( 4 );
486  setNumSegmentsLeft( 2 );
487  }
488 
489  refreshSegmentMillimeters();
490  adjustBoxSize();
491  emit itemChanged();
492 }
493 
495 {
496  if ( !mStyle )
497  {
498  return;
499  }
500 
501  QRectF box = QRectF( pos(), mStyle->calculateBoxSize( mSettings, createScaleContext() ) );
502  if ( rect().height() > box.height() )
503  {
504  //keep user specified item height if higher than minimum scale bar height
505  box.setHeight( rect().height() );
506  }
507 
508  //update rect for data defined size and position
509  QRectF newRect = evalItemRect( box, true );
510 
511  //scale bars have a minimum size, respect that regardless of data defined settings
512  if ( newRect.width() < box.width() )
513  {
514  newRect.setWidth( box.width() );
515  }
516  if ( newRect.height() < box.height() )
517  {
518  newRect.setHeight( box.height() );
519  }
520 
522 }
523 
524 void QgsComposerScaleBar::setSceneRect( const QRectF &rectangle )
525 {
526  QRectF box = QRectF( pos(), mStyle->calculateBoxSize( mSettings, createScaleContext() ) );
527  if ( rectangle.height() > box.height() )
528  {
529  //keep user specified item height if higher than minimum scale bar height
530  box.setHeight( rectangle.height() );
531  }
532  box.moveTopLeft( rectangle.topLeft() );
533 
534  //update rect for data defined size and position
535  QRectF newRect = evalItemRect( rectangle );
536 
537  //scale bars have a minimum size, respect that regardless of data defined settings
538  if ( newRect.width() < box.width() )
539  {
540  newRect.setWidth( box.width() );
541  }
542  if ( newRect.height() < box.height() )
543  {
544  newRect.setHeight( box.height() );
545  }
546 
548 }
549 
551 {
552  //Don't adjust box size for numeric scale bars:
553  if ( mStyle && mStyle->name() != QLatin1String( "Numeric" ) )
554  {
555  adjustBoxSize();
556  }
557  QgsComposerItem::update();
558 }
559 
561 {
562  if ( !mStyle )
563  {
564  return;
565  }
566  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
567  refreshSegmentMillimeters();
568  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
569  correctXPositionAlignment( width, widthAfter );
570  update();
571  emit itemChanged();
572 }
573 
574 void QgsComposerScaleBar::setStyle( const QString &styleName )
575 {
576  delete mStyle;
577  mStyle = nullptr;
578 
579  //switch depending on style name
580  if ( styleName == QLatin1String( "Single Box" ) )
581  {
582  mStyle = new QgsSingleBoxScaleBarRenderer();
583  }
584  else if ( styleName == QLatin1String( "Double Box" ) )
585  {
586  mStyle = new QgsDoubleBoxScaleBarRenderer();
587  }
588  else if ( styleName == QLatin1String( "Line Ticks Middle" ) || styleName == QLatin1String( "Line Ticks Down" ) || styleName == QLatin1String( "Line Ticks Up" ) )
589  {
591  if ( styleName == QLatin1String( "Line Ticks Middle" ) )
592  {
594  }
595  else if ( styleName == QLatin1String( "Line Ticks Down" ) )
596  {
598  }
599  else if ( styleName == QLatin1String( "Line Ticks Up" ) )
600  {
602  }
603  mStyle = tickStyle;
604  }
605  else if ( styleName == QLatin1String( "Numeric" ) )
606  {
607  mStyle = new QgsNumericScaleBarRenderer();
608  }
609  emit itemChanged();
610 }
611 
613 {
614  if ( mStyle )
615  {
616  return mStyle->name();
617  }
618  else
619  {
620  return QLatin1String( "" );
621  }
622 }
623 
625 {
626  return mSettings.font();
627 }
628 
629 void QgsComposerScaleBar::setFont( const QFont &font )
630 {
631  mSettings.setFont( font );
632  update();
633  emit itemChanged();
634 }
635 
636 bool QgsComposerScaleBar::writeXml( QDomElement &elem, QDomDocument &doc ) const
637 {
638  if ( elem.isNull() )
639  {
640  return false;
641  }
642 
643  QDomElement composerScaleBarElem = doc.createElement( QStringLiteral( "ComposerScaleBar" ) );
644  composerScaleBarElem.setAttribute( QStringLiteral( "height" ), QString::number( mSettings.height() ) );
645  composerScaleBarElem.setAttribute( QStringLiteral( "labelBarSpace" ), QString::number( mSettings.labelBarSpace() ) );
646  composerScaleBarElem.setAttribute( QStringLiteral( "boxContentSpace" ), QString::number( mSettings.boxContentSpace() ) );
647  composerScaleBarElem.setAttribute( QStringLiteral( "numSegments" ), mSettings.numberOfSegments() );
648  composerScaleBarElem.setAttribute( QStringLiteral( "numSegmentsLeft" ), mSettings.numberOfSegmentsLeft() );
649  composerScaleBarElem.setAttribute( QStringLiteral( "numUnitsPerSegment" ), QString::number( mSettings.unitsPerSegment() ) );
650  composerScaleBarElem.setAttribute( QStringLiteral( "segmentSizeMode" ), mSettings.segmentSizeMode() );
651  composerScaleBarElem.setAttribute( QStringLiteral( "minBarWidth" ), mSettings.minimumBarWidth() );
652  composerScaleBarElem.setAttribute( QStringLiteral( "maxBarWidth" ), mSettings.maximumBarWidth() );
653  composerScaleBarElem.setAttribute( QStringLiteral( "segmentMillimeters" ), QString::number( mSegmentMillimeters ) );
654  composerScaleBarElem.setAttribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QString::number( mSettings.mapUnitsPerScaleBarUnit() ) );
655  composerScaleBarElem.appendChild( QgsFontUtils::toXmlElement( mSettings.font(), doc, QStringLiteral( "scaleBarFont" ) ) );
656  composerScaleBarElem.setAttribute( QStringLiteral( "outlineWidth" ), QString::number( mSettings.lineWidth() ) );
657  composerScaleBarElem.setAttribute( QStringLiteral( "unitLabel" ), mSettings.unitLabel() );
658  composerScaleBarElem.setAttribute( QStringLiteral( "unitType" ), QgsUnitTypes::encodeUnit( mSettings.units() ) );
659  composerScaleBarElem.setAttribute( QStringLiteral( "lineJoinStyle" ), QgsSymbolLayerUtils::encodePenJoinStyle( mSettings.lineJoinStyle() ) );
660  composerScaleBarElem.setAttribute( QStringLiteral( "lineCapStyle" ), QgsSymbolLayerUtils::encodePenCapStyle( mSettings.lineCapStyle() ) );
661 
662  //style
663  if ( mStyle )
664  {
665  composerScaleBarElem.setAttribute( QStringLiteral( "style" ), mStyle->name() );
666  }
667 
668  //map id
669  if ( mComposerMap )
670  {
671  composerScaleBarElem.setAttribute( QStringLiteral( "mapId" ), mComposerMap->id() );
672  }
673 
674  //colors
675 
676  //fill color
677  QDomElement fillColorElem = doc.createElement( QStringLiteral( "fillColor" ) );
678  fillColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor().red() ) );
679  fillColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor().green() ) );
680  fillColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor().blue() ) );
681  fillColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor().alpha() ) );
682  composerScaleBarElem.appendChild( fillColorElem );
683 
684  //fill color 2
685  QDomElement fillColor2Elem = doc.createElement( QStringLiteral( "fillColor2" ) );
686  fillColor2Elem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor2().red() ) );
687  fillColor2Elem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor2().green() ) );
688  fillColor2Elem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor2().blue() ) );
689  fillColor2Elem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor2().alpha() ) );
690  composerScaleBarElem.appendChild( fillColor2Elem );
691 
692  //pen color
693  QDomElement strokeColorElem = doc.createElement( QStringLiteral( "strokeColor" ) );
694  strokeColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.lineColor().red() ) );
695  strokeColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.lineColor().green() ) );
696  strokeColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.lineColor().blue() ) );
697  strokeColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.lineColor().alpha() ) );
698  composerScaleBarElem.appendChild( strokeColorElem );
699 
700  //font color
701  QDomElement fontColorElem = doc.createElement( QStringLiteral( "textColor" ) );
702  fontColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fontColor().red() ) );
703  fontColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fontColor().green() ) );
704  fontColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fontColor().blue() ) );
705  fontColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fontColor().alpha() ) );
706  composerScaleBarElem.appendChild( fontColorElem );
707 
708  //alignment
709  composerScaleBarElem.setAttribute( QStringLiteral( "alignment" ), QString::number( static_cast< int >( mSettings.alignment() ) ) );
710 
711  elem.appendChild( composerScaleBarElem );
712  return _writeXml( composerScaleBarElem, doc );
713 }
714 
715 bool QgsComposerScaleBar::readXml( const QDomElement &itemElem, const QDomDocument &doc )
716 {
717  if ( itemElem.isNull() )
718  {
719  return false;
720  }
721 
722  mSettings.setHeight( itemElem.attribute( QStringLiteral( "height" ), QStringLiteral( "5.0" ) ).toDouble() );
723  mSettings.setLabelBarSpace( itemElem.attribute( QStringLiteral( "labelBarSpace" ), QStringLiteral( "3.0" ) ).toDouble() );
724  mSettings.setBoxContentSpace( itemElem.attribute( QStringLiteral( "boxContentSpace" ), QStringLiteral( "1.0" ) ).toDouble() );
725  mSettings.setNumberOfSegments( itemElem.attribute( QStringLiteral( "numSegments" ), QStringLiteral( "2" ) ).toInt() );
726  mSettings.setNumberOfSegmentsLeft( itemElem.attribute( QStringLiteral( "numSegmentsLeft" ), QStringLiteral( "0" ) ).toInt() );
727  mSettings.setUnitsPerSegment( itemElem.attribute( QStringLiteral( "numUnitsPerSegment" ), QStringLiteral( "1.0" ) ).toDouble() );
728  mSettings.setSegmentSizeMode( static_cast<QgsScaleBarSettings::SegmentSizeMode>( itemElem.attribute( QStringLiteral( "segmentSizeMode" ), QStringLiteral( "0" ) ).toInt() ) );
729  mSettings.setMinimumBarWidth( itemElem.attribute( QStringLiteral( "minBarWidth" ), QStringLiteral( "50" ) ).toDouble() );
730  mSettings.setMaximumBarWidth( itemElem.attribute( QStringLiteral( "maxBarWidth" ), QStringLiteral( "150" ) ).toDouble() );
731  mSegmentMillimeters = itemElem.attribute( QStringLiteral( "segmentMillimeters" ), QStringLiteral( "0.0" ) ).toDouble();
732  mSettings.setMapUnitsPerScaleBarUnit( itemElem.attribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QStringLiteral( "1.0" ) ).toDouble() );
733  mSettings.setLineWidth( itemElem.attribute( QStringLiteral( "outlineWidth" ), QStringLiteral( "0.3" ) ).toDouble() );
734  mSettings.setUnitLabel( itemElem.attribute( QStringLiteral( "unitLabel" ) ) );
735  mSettings.setLineJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( itemElem.attribute( QStringLiteral( "lineJoinStyle" ), QStringLiteral( "miter" ) ) ) );
736  mSettings.setLineCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( itemElem.attribute( QStringLiteral( "lineCapStyle" ), QStringLiteral( "square" ) ) ) );
737  QFont f;
738  if ( !QgsFontUtils::setFromXmlChildNode( f, itemElem, QStringLiteral( "scaleBarFont" ) ) )
739  {
740  f.fromString( itemElem.attribute( QStringLiteral( "font" ), QLatin1String( "" ) ) );
741  }
742  mSettings.setFont( f );
743 
744  //colors
745  //fill color
746  QDomNodeList fillColorList = itemElem.elementsByTagName( QStringLiteral( "fillColor" ) );
747  if ( !fillColorList.isEmpty() )
748  {
749  QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
750  bool redOk, greenOk, blueOk, alphaOk;
751  int fillRed, fillGreen, fillBlue, fillAlpha;
752 
753  fillRed = fillColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
754  fillGreen = fillColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
755  fillBlue = fillColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
756  fillAlpha = fillColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
757 
758  if ( redOk && greenOk && blueOk && alphaOk )
759  {
760  mSettings.setFillColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
761  }
762  }
763  else
764  {
765  mSettings.setFillColor( QColor( itemElem.attribute( QStringLiteral( "brushColor" ), QStringLiteral( "#000000" ) ) ) );
766  }
767 
768  //fill color 2
769  QDomNodeList fillColor2List = itemElem.elementsByTagName( QStringLiteral( "fillColor2" ) );
770  if ( !fillColor2List.isEmpty() )
771  {
772  QDomElement fillColor2Elem = fillColor2List.at( 0 ).toElement();
773  bool redOk, greenOk, blueOk, alphaOk;
774  int fillRed, fillGreen, fillBlue, fillAlpha;
775 
776  fillRed = fillColor2Elem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
777  fillGreen = fillColor2Elem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
778  fillBlue = fillColor2Elem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
779  fillAlpha = fillColor2Elem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
780 
781  if ( redOk && greenOk && blueOk && alphaOk )
782  {
783  mSettings.setFillColor2( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
784  }
785  }
786  else
787  {
788  mSettings.setFillColor2( QColor( itemElem.attribute( QStringLiteral( "brush2Color" ), QStringLiteral( "#ffffff" ) ) ) );
789  }
790 
791  //stroke color
792  QDomNodeList strokeColorList = itemElem.elementsByTagName( QStringLiteral( "strokeColor" ) );
793  if ( !strokeColorList.isEmpty() )
794  {
795  QDomElement strokeColorElem = strokeColorList.at( 0 ).toElement();
796  bool redOk, greenOk, blueOk, alphaOk;
797  int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
798 
799  strokeRed = strokeColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
800  strokeGreen = strokeColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
801  strokeBlue = strokeColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
802  strokeAlpha = strokeColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
803 
804  if ( redOk && greenOk && blueOk && alphaOk )
805  {
806  mSettings.setLineColor( QColor( strokeRed, strokeGreen, strokeBlue, strokeAlpha ) );
807  QPen p = mSettings.pen();
808  p.setColor( mSettings.lineColor() );
809  mSettings.setPen( p );
810  }
811  }
812  else
813  {
814  mSettings.setLineColor( QColor( itemElem.attribute( QStringLiteral( "penColor" ), QStringLiteral( "#000000" ) ) ) );
815  QPen p = mSettings.pen();
816  p.setColor( mSettings.lineColor() );
817  mSettings.setPen( p );
818  }
819 
820  //font color
821  QDomNodeList textColorList = itemElem.elementsByTagName( QStringLiteral( "textColor" ) );
822  if ( !textColorList.isEmpty() )
823  {
824  QDomElement textColorElem = textColorList.at( 0 ).toElement();
825  bool redOk, greenOk, blueOk, alphaOk;
826  int textRed, textGreen, textBlue, textAlpha;
827 
828  textRed = textColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
829  textGreen = textColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
830  textBlue = textColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
831  textAlpha = textColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
832 
833  if ( redOk && greenOk && blueOk && alphaOk )
834  {
835  mSettings.setFontColor( QColor( textRed, textGreen, textBlue, textAlpha ) );
836  }
837  }
838  else
839  {
840  QColor c;
841  c.setNamedColor( itemElem.attribute( QStringLiteral( "fontColor" ), QStringLiteral( "#000000" ) ) );
842  mSettings.setFontColor( c );
843  }
844 
845  //style
846  delete mStyle;
847  mStyle = nullptr;
848  QString styleString = itemElem.attribute( QStringLiteral( "style" ), QLatin1String( "" ) );
849  setStyle( tr( styleString.toLocal8Bit().data() ) );
850 
851  if ( itemElem.attribute( QStringLiteral( "unitType" ) ).isEmpty() )
852  {
854  switch ( itemElem.attribute( QStringLiteral( "units" ) ).toInt() )
855  {
856  case 0:
858  break;
859  case 1:
861  break;
862  case 2:
864  break;
865  case 3:
867  break;
868  }
869  mSettings.setUnits( u );
870  }
871  else
872  {
873  mSettings.setUnits( QgsUnitTypes::decodeDistanceUnit( itemElem.attribute( QStringLiteral( "unitType" ) ) ) );
874  }
875  mSettings.setAlignment( static_cast< QgsScaleBarSettings::Alignment >( itemElem.attribute( QStringLiteral( "alignment" ), QStringLiteral( "0" ) ).toInt() ) );
876 
877  //map
878  int mapId = itemElem.attribute( QStringLiteral( "mapId" ), QStringLiteral( "-1" ) ).toInt();
879  if ( mapId >= 0 )
880  {
882  mComposerMap = const_cast< QgsComposerMap *>( composerMap );
883  if ( mComposerMap )
884  {
886  connect( mComposerMap, &QObject::destroyed, this, &QgsComposerScaleBar::invalidateCurrentMap );
887  }
888  }
889 
891 
892  //restore general composer item properties
893  QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
894  if ( !composerItemList.isEmpty() )
895  {
896  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
897  _readXml( composerItemElem, doc );
898  }
899 
900  return true;
901 }
902 
903 void QgsComposerScaleBar::correctXPositionAlignment( double width, double widthAfter )
904 {
905  //Don't adjust position for numeric scale bars:
906  if ( mStyle->name() == QLatin1String( "Numeric" ) )
907  {
908  return;
909  }
910 
911  if ( mSettings.alignment() == QgsScaleBarSettings::AlignMiddle )
912  {
913  move( -( widthAfter - width ) / 2.0, 0 );
914  }
915  else if ( mSettings.alignment() == QgsScaleBarSettings::AlignRight )
916  {
917  move( -( widthAfter - width ), 0 );
918  }
919 }
920 
double scale() const
Returns the map scale.
void setFontColor(const QColor &color)
Sets the color used for drawing text in the scalebar.
QgsUnitTypes::DistanceUnit lengthUnits() const
Returns the units of distance for length calculations made by this object.
void setMinimumBarWidth(double width)
Sets the minimum width (in millimeters) for scale bar segments.
bool writeXml(QDomElement &elem, QDomDocument &doc) const override
Stores state in Dom element.
A rectangle specified with double values.
Definition: qgsrectangle.h:39
double mapUnitsPerScaleBarUnit() const
Returns the number of map units per scale bar unit used by the scalebar.
QBrush brush() const
Returns the primary brush used for filling the scalebar.
const QgsComposerMap * getComposerMapById(const int id) const
Returns the composer map with specified id.
A scale bar that draws segments using short ticks.
void setLineColor(const QColor &color)
Sets the color used for lines in the scalebar.
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
virtual void draw(QgsRenderContext &context, const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const =0
Draws the scalebar using the specified settings and scaleContext to a destination render context...
QString unitLabel() const
Returns the label for units.
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties, const QgsExpressionContext *context=nullptr) override
void setLineCapStyle(Qt::PenCapStyle style)
Sets cap style used when drawing the lines in the scalebar.
void setFont(const QFont &font)
This class is a composition of two QSettings instances:
Definition: qgssettings.h:55
QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
double segmentWidth
Width of each individual segment (in millimeters)
QColor fontColor() const
Returns the color used for drawing text in the scalebar.
Alignment alignment() const
Returns the scalebar alignment.
void setPen(const QPen &pen)
Sets the pen used for drawing outlines in the scalebar.
QColor fillColor2() const
Returns the secondary color used for fills in the scalebar.
void itemChanged()
Emitted when the item changes.
A class to represent a 2D point.
Definition: qgspointxy.h:43
void setComposerMap(QgsComposerMap *map)
Sets the map item linked to the scalebar.
void setTickPosition(TickPosition position)
Sets the position for tick marks in the scalebar.
void setUnitLabeling(const QString &label)
SegmentSizeMode segmentSizeMode() const
Returns the size mode for the scale bar segments.
void applyDefaultSettings()
Apply default settings.
A item that forms part of a map composition.
double lineWidth() const
Returns the line width in millimeters for lines in the scalebar.
void setNumSegments(int nSegments)
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint.
virtual void refreshDataDefinedProperty(const DataDefinedProperty property=AllProperties, const QgsExpressionContext *context=nullptr)
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false, const QgsExpressionContext *context=nullptr)
Evaluates an item&#39;s bounding rect to consider data defined position and size of item and reference po...
void setNumSegmentsLeft(int nSegmentsLeft)
Alignment
Scalebar alignment.
int id() const
Get identification number.
void applyDefaultSize(QgsUnitTypes::DistanceUnit u=QgsUnitTypes::DistanceMeters)
Apply default size (scale bar 1/5 of map item width)
virtual void drawFrame(QPainter *p)
Draw black frame around item.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
QColor lineColor() const
Returns the color used for lines in the scalebar.
Scalebar secondary fill color.
void update()
Adjusts box size and calls QgsComposerItem::update()
QString ellipsoid
Definition: qgsproject.h:88
Scalebar style that draws a single box with alternating color for the segments.
QFont font() const
Returns the font used for drawing text in the scalebar.
void setUnits(QgsUnitTypes::DistanceUnit u)
Sets the scalebar distance units.
void setMinBarWidth(double minWidth)
Sets the minimum size (in millimeters) for scale bar segments.
static Qt::PenJoinStyle decodePenJoinStyle(const QString &str)
DataDefinedProperty
Data defined properties for different item types.
void setNumMapUnitsPerScaleBarUnit(double d)
QgsUnitTypes::DistanceUnit units() const
Returns the scalebar distance units.
QColor fillColor() const
Returns the color used for fills in the scalebar.
void adjustBoxSize()
Sets box size suitable to content.
void setMaxBarWidth(double maxWidth)
Sets the maximum size (in millimeters) for scale bar segments.
static QgsRenderContext createRenderContextForMap(QgsComposerMap *map, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified composer map and painter destination.
double height() const
Returns the scalebar height (in millimeters).
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=0) const
Calculates the current value of the property with the specified key and interprets it as a color...
void setFillColor(const QColor &color)
Sets the color used for fills in the scalebar.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
bool _writeXml(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document. Usually called from writeXml methods of ...
int numberOfSegments() const
Returns the number of segments included in the scalebar.
double minimumBarWidth() const
Returns the minimum width (in millimeters) for scale bar segments.
void setLineCapStyle(Qt::PenCapStyle style)
Sets the cap style used when drawing the lines in the scalebar.
void setLineJoinStyle(Qt::PenJoinStyle style)
Sets join style used when drawing the lines in the scalebar.
void setMapUnitsPerScaleBarUnit(double units)
Sets the number of map units per scale bar unit used by the scalebar.
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
QSizeF size
Destination size for scalebar.
void setStyle(const QString &styleName)
Sets style by name.
void setAlignment(Alignment alignment)
Sets the scalebar alignment.
QgsUnitTypes::DistanceUnit units() const
Returns the distance units used by the scalebar.
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:131
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters. ...
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
void setMaximumBarWidth(double width)
Sets the maximum width (in millimeters) for scale bar segments.
double prevNiceNumber(double a, double d=1)
void setBoxContentSpace(double space)
Sets the space (margin) between the scalebar box and content in millimeters.
QgsPropertyCollection mDataDefinedProperties
void setUnitsPerSegment(double units)
Sets the number of scalebar units per segment.
QgsComposerScaleBar(QgsComposition *composition)
static bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
void setFont(const QFont &font)
Sets the font used for drawing text in the scalebar.
void setSegmentSizeMode(SegmentSizeMode mode)
Sets the size mode for scale bar segments.
static Qt::PenCapStyle decodePenCapStyle(const QString &str)
void setFillColor2(const QColor &color)
Sets the secondary color used for fills in the scalebar.
void setAlignment(QgsScaleBarSettings::Alignment alignment)
Sets the alignment of the scalebar.
bool _readXml(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document. Usually called from readXml methods of su...
void setBackgroundEnabled(const bool drawBackground)
Set whether this item has a Background drawn around it or not.
SegmentSizeMode
Modes for setting size for scale bar segments.
Scale bar segment size is fixed to a map unit.
QgsComposerMap * composerMap() const
Returns the map item linked to the scalebar.
Graphics scene for map printing.
Object representing map window.
void setSegmentSizeMode(QgsScaleBarSettings::SegmentSizeMode mode)
Sets the size mode for scale bar segments.
double maximumBarWidth() const
Returns the maximum width (in millimeters) for scale bar segments.
bool readXml(const QDomElement &itemElem, const QDomDocument &doc) override
Sets state from Dom document.
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:119
void invalidateCurrentMap()
Sets mCompositionMap to 0 if the map is deleted.
Double box with alternating colors.
void setBoxContentSpace(double space)
virtual QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:104
virtual QString name() const =0
Returns the unique name for this style.
void setHeight(double height)
Sets the scalebar height (in millimeters).
Unknown distance unit.
Definition: qgsunittypes.h:54
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
QgsComposition * mComposition
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
Contains information about the context of a rendering operation.
void setLineJoinStyle(Qt::PenJoinStyle style)
Sets the join style used when drawing the lines in the scalebar.
QgsProject * project() const
The project associated with the composition.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
virtual void drawBackground(QPainter *p)
Draw background.
QString style() const
Returns style name.
void setLineWidth(double width)
Sets the line width in millimeters for lines in the scalebar.
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
void setLabelBarSpace(double space)
Sets the spacing (in millimeters) between labels and the scalebar.
A scale bar style that draws text in the form of &#39;1:XXXXX&#39;.
void setBrush2(const QBrush &brush)
Sets the secondary brush used for filling the scalebar.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:109
double unitsPerSegment() const
Returns the number of scalebar units per segment.
virtual QSizeF calculateBoxSize(const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const
Calculates the required box size (in millimeters) for a scalebar using the specified settings and sca...
Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
void setSceneRect(const QRectF &rectangle) override
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
double nextNiceNumber(double a, double d=1)
double valueAsDouble(int key, const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=0) const
Calculates the current value of the property with the specified key and interprets it as a double...
void setNumberOfSegmentsLeft(int segments)
Sets the number of segments included in the left part of the scalebar.
void setSourceCrs(const QgsCoordinateReferenceSystem &srcCRS)
Sets source spatial reference system.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
QBrush brush2() const
Returns the secondary brush for the scalebar.
void extentChanged()
void setNumUnitsPerSegment(double units)
static QString encodePenCapStyle(Qt::PenCapStyle style)
Qt::PenJoinStyle lineJoinStyle() const
Returns the join style used for drawing lines in the scalebar.
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
int numberOfSegmentsLeft() const
Returns the number of segments included in the left part of the scalebar.
void setUnits(QgsUnitTypes::DistanceUnit units)
Sets the distance units used by the scalebar.
void move(double dx, double dy)
Moves item in canvas coordinates.
void correctXPositionAlignment(double width, double widthAfter)
Moves scalebar position to the left / right depending on alignment and change in item width...
double measureLine(const QList< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
void setBrush(const QBrush &brush)
Sets the primary brush used for filling the scalebar.
const QgsRectangle * currentMapExtent() const
Returns a pointer to the current map extent, which is either the original user specified extent or th...
void setUnitLabel(const QString &label)
Sets the label for units.
static QString encodePenJoinStyle(Qt::PenJoinStyle style)
static Q_INVOKABLE QgsUnitTypes::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=0)
Decodes a distance unit from a string.
void setNumberOfSegments(int segments)
Sets the number of segments included in the scalebar.
All properties for item.
Contains parameters regarding scalebar calculations.