QGIS API Documentation  2.99.0-Master (19b062c)
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  // scale painter from mm to dots
73  painter->scale( 1.0 / c.scaleFactor(), 1.0 / c.scaleFactor() );
74  mStyle->draw( c, mSettings, createScaleContext() );
75  painter->scale( c.scaleFactor(), c.scaleFactor() );
76 
77  //draw frame and selection boxes if necessary
78  drawFrame( painter );
79  if ( isSelected() )
80  {
81  drawSelectionBoxes( painter );
82  }
83 }
84 
86 {
87  if ( !mStyle )
88  {
89  mSettings.setNumberOfSegments( nSegments );
90  return;
91  }
92  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
93  mSettings.setNumberOfSegments( nSegments );
94  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
95  correctXPositionAlignment( width, widthAfter );
96  emit itemChanged();
97 }
98 
100 {
101  if ( !mStyle )
102  {
103  mSettings.setUnitsPerSegment( units );
104  return;
105  }
106  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
107  mSettings.setUnitsPerSegment( units );
108  refreshSegmentMillimeters();
109  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
110  correctXPositionAlignment( width, widthAfter );
111  emit itemChanged();
112 }
113 
115 {
116  if ( !mStyle )
117  {
118  mSettings.setSegmentSizeMode( mode );
119  return;
120  }
121  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
122  mSettings.setSegmentSizeMode( mode );
123  refreshSegmentMillimeters();
124  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
125  correctXPositionAlignment( width, widthAfter );
126  emit itemChanged();
127 }
128 
129 void QgsComposerScaleBar::setMinBarWidth( double minWidth )
130 {
131  if ( !mStyle )
132  {
133  mSettings.setMinimumBarWidth( minWidth );
134  return;
135  }
136  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
137  mSettings.setMinimumBarWidth( minWidth );
138  refreshSegmentMillimeters();
139  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
140  correctXPositionAlignment( width, widthAfter );
141  emit itemChanged();
142 }
143 
144 void QgsComposerScaleBar::setMaxBarWidth( double maxWidth )
145 {
146  if ( !mStyle )
147  {
148  mSettings.setMaximumBarWidth( maxWidth );
149  return;
150  }
151  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
152  mSettings.setMaximumBarWidth( maxWidth );
153  refreshSegmentMillimeters();
154  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
155  correctXPositionAlignment( width, widthAfter );
156  emit itemChanged();
157 }
158 
160 {
161  if ( !mStyle )
162  {
163  mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
164  return;
165  }
166  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
167  mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
168  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
169  correctXPositionAlignment( width, widthAfter );
170  emit itemChanged();
171 }
172 
174 {
175  if ( !mStyle )
176  {
177  mSettings.setBoxContentSpace( space );
178  return;
179  }
180  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
181  mSettings.setBoxContentSpace( space );
182  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
183  correctXPositionAlignment( width, widthAfter );
184  emit itemChanged();
185 }
186 
188 {
189  if ( mComposerMap )
190  {
191  disconnect( mComposerMap, &QgsComposerMap::extentChanged, this, &QgsComposerScaleBar::updateSegmentSize );
192  disconnect( mComposerMap, &QObject::destroyed, this, &QgsComposerScaleBar::invalidateCurrentMap );
193  }
194  mComposerMap = map;
195 
196  if ( !map )
197  {
198  return;
199  }
200 
202  connect( mComposerMap, &QObject::destroyed, this, &QgsComposerScaleBar::invalidateCurrentMap );
203 
204  refreshSegmentMillimeters();
205  emit itemChanged();
206 }
207 
209 {
210  if ( !mComposerMap )
211  {
212  return;
213  }
214 
215  disconnect( mComposerMap, &QgsComposerMap::extentChanged, this, &QgsComposerScaleBar::updateSegmentSize );
216  disconnect( mComposerMap, &QObject::destroyed, this, &QgsComposerScaleBar::invalidateCurrentMap );
217  mComposerMap = nullptr;
218 }
219 
221 {
223  const QgsExpressionContext *evalContext = context ? context : &scopedContext;
224 
225  bool forceUpdate = false;
226  //updates data defined properties and redraws item to match
228  {
229  QBrush b = mSettings.brush();
230  b.setColor( mDataDefinedProperties.valueAsColor( QgsComposerObject::ScalebarFillColor, *evalContext, mSettings.fillColor() ) );
231  mSettings.setBrush( b );
232  forceUpdate = true;
233  }
235  {
236  QBrush b = mSettings.brush2();
237  b.setColor( mDataDefinedProperties.valueAsColor( QgsComposerObject::ScalebarFillColor2, *evalContext, mSettings.fillColor2() ) );
238  mSettings.setBrush2( b );
239  forceUpdate = true;
240  }
242  {
243  QPen p = mSettings.pen();
244  p.setColor( mDataDefinedProperties.valueAsColor( QgsComposerObject::ScalebarLineColor, *evalContext, mSettings.lineColor() ) );
245  mSettings.setPen( p );
246  forceUpdate = true;
247  }
249  {
250  QPen p = mSettings.pen();
251  p.setWidthF( mDataDefinedProperties.valueAsDouble( QgsComposerObject::ScalebarLineWidth, *evalContext, mSettings.lineWidth() ) );
252  mSettings.setPen( p );
253  forceUpdate = true;
254  }
255  if ( forceUpdate )
256  {
257  update();
258  }
259 
261 }
262 
263 // nextNiceNumber(4573.23, d) = 5000 (d=1) -> 4600 (d=10) -> 4580 (d=100) -> 4574 (d=1000) -> etc
264 inline double nextNiceNumber( double a, double d = 1 )
265 {
266  double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
267  return std::ceil( a / s ) * s;
268 }
269 
270 // prevNiceNumber(4573.23, d) = 4000 (d=1) -> 4500 (d=10) -> 4570 (d=100) -> 4573 (d=1000) -> etc
271 inline double prevNiceNumber( double a, double d = 1 )
272 {
273  double s = std::pow( 10.0, std::floor( std::log10( a ) ) ) / d;
274  return std::floor( a / s ) * s;
275 }
276 
277 void QgsComposerScaleBar::refreshSegmentMillimeters()
278 {
279  if ( mComposerMap )
280  {
281  //get mm dimension of composer map
282  QRectF composerItemRect = mComposerMap->rect();
283 
285  {
286  //calculate size depending on mNumUnitsPerSegment
287  mSegmentMillimeters = composerItemRect.width() / mapWidth() * mSettings.unitsPerSegment();
288  }
289  else /*if(mSegmentSizeMode == SegmentSizeFitWidth)*/
290  {
291  if ( mSettings.maximumBarWidth() < mSettings.minimumBarWidth() )
292  {
293  mSegmentMillimeters = 0;
294  }
295  else
296  {
297  double nSegments = ( mSettings.numberOfSegmentsLeft() != 0 ) + mSettings.numberOfSegments();
298  // unitsPerSegments which fit minBarWidth resp. maxBarWidth
299  double minUnitsPerSeg = ( mSettings.minimumBarWidth() * mapWidth() ) / ( nSegments * composerItemRect.width() );
300  double maxUnitsPerSeg = ( mSettings.maximumBarWidth() * mapWidth() ) / ( nSegments * composerItemRect.width() );
301 
302  // Start with coarsest "nice" number closest to minUnitsPerSeg resp
303  // maxUnitsPerSeg, then proceed to finer numbers as long as neither
304  // lowerNiceUnitsPerSeg nor upperNiceUnitsPerSeg are are in
305  // [minUnitsPerSeg, maxUnitsPerSeg]
306  double lowerNiceUnitsPerSeg = nextNiceNumber( minUnitsPerSeg );
307  double upperNiceUnitsPerSeg = prevNiceNumber( maxUnitsPerSeg );
308 
309  double d = 1;
310  while ( lowerNiceUnitsPerSeg > maxUnitsPerSeg && upperNiceUnitsPerSeg < minUnitsPerSeg )
311  {
312  d *= 10;
313  lowerNiceUnitsPerSeg = nextNiceNumber( minUnitsPerSeg, d );
314  upperNiceUnitsPerSeg = prevNiceNumber( maxUnitsPerSeg, d );
315  }
316 
317  // Pick mNumUnitsPerSegment from {lowerNiceUnitsPerSeg, upperNiceUnitsPerSeg}, use the larger if possible
318  mSettings.setUnitsPerSegment( upperNiceUnitsPerSeg < minUnitsPerSeg ? lowerNiceUnitsPerSeg : upperNiceUnitsPerSeg );
319  mSegmentMillimeters = composerItemRect.width() / mapWidth() * mSettings.unitsPerSegment();
320  }
321  }
322  }
323 }
324 
325 double QgsComposerScaleBar::mapWidth() const
326 {
327  if ( !mComposerMap )
328  {
329  return 0.0;
330  }
331 
332  QgsRectangle composerMapRect = *( mComposerMap->currentMapExtent() );
333  if ( mSettings.units() == QgsUnitTypes::DistanceUnknownUnit )
334  {
335  return composerMapRect.width();
336  }
337  else
338  {
339  QgsDistanceArea da;
340  da.setSourceCrs( mComposerMap->crs() );
342 
344  double measure = da.measureLine( QgsPointXY( composerMapRect.xMinimum(), composerMapRect.yMinimum() ),
345  QgsPointXY( composerMapRect.xMaximum(), composerMapRect.yMinimum() ) );
346  measure /= QgsUnitTypes::fromUnitToUnitFactor( mSettings.units(), units );
347  return measure;
348  }
349 }
350 
351 QgsScaleBarRenderer::ScaleBarContext QgsComposerScaleBar::createScaleContext() const
352 {
354  scaleContext.size = rect().size();
355  scaleContext.segmentWidth = mSegmentMillimeters;
356  scaleContext.scale = mComposerMap ? mComposerMap->scale() : 1.0;
357  return scaleContext;
358 }
359 
361 {
362  mSettings.setAlignment( a );
363  update();
364  emit itemChanged();
365 }
366 
368 {
369  mSettings.setUnits( u );
370  refreshSegmentMillimeters();
371  emit itemChanged();
372 }
373 
375 {
376  if ( mSettings.lineJoinStyle() == style )
377  {
378  //no change
379  return;
380  }
381  mSettings.setLineJoinStyle( style );
382  update();
383  emit itemChanged();
384 }
385 
387 {
388  if ( mSettings.lineCapStyle() == style )
389  {
390  //no change
391  return;
392  }
393  mSettings.setLineCapStyle( style );
394  update();
395  emit itemChanged();
396 }
397 
399 {
400  //style
401  delete mStyle;
402  mStyle = new QgsSingleBoxScaleBarRenderer();
403 
404  //default to no background
405  setBackgroundEnabled( false );
406 
407  //get default composer font from settings
408  QgsSettings settings;
409  QString defaultFontString = settings.value( QStringLiteral( "Composer/defaultFont" ) ).toString();
410  QFont f;
411  if ( !defaultFontString.isEmpty() )
412  {
413  f.setFamily( defaultFontString );
414  }
415  f.setPointSizeF( 12.0 );
416  mSettings.setFont( f );
417 
419 
420  emit itemChanged();
421 }
422 
424 {
425  if ( mComposerMap )
426  {
427  setUnits( u );
428  double upperMagnitudeMultiplier = 1.0;
429  double widthInSelectedUnits = mapWidth();
430  double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
431  setNumUnitsPerSegment( initialUnitsPerSegment );
432 
433  switch ( u )
434  {
436  {
437  upperMagnitudeMultiplier = 1.0;
438  setUnitLabeling( tr( "units" ) );
439  break;
440  }
442  {
443  if ( initialUnitsPerSegment > 1000.0 )
444  {
445  upperMagnitudeMultiplier = 1000.0;
446  setUnitLabeling( tr( "km" ) );
447  }
448  else
449  {
450  upperMagnitudeMultiplier = 1.0;
451  setUnitLabeling( tr( "m" ) );
452  }
453  break;
454  }
456  {
457  if ( initialUnitsPerSegment > 5419.95 )
458  {
459  upperMagnitudeMultiplier = 5419.95;
460  setUnitLabeling( tr( "miles" ) );
461  }
462  else
463  {
464  upperMagnitudeMultiplier = 1.0;
465  setUnitLabeling( tr( "ft" ) );
466  }
467  break;
468  }
469 
470  default:
472  upperMagnitudeMultiplier = 1;
473  break;
474  }
475 
476  double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
477  int segmentMagnitude = std::floor( std::log10( segmentWidth ) );
478  double unitsPerSegment = upperMagnitudeMultiplier * ( std::pow( 10.0, segmentMagnitude ) );
479  double multiplier = std::floor( ( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
480 
481  if ( multiplier > 0 )
482  {
483  unitsPerSegment = unitsPerSegment * multiplier;
484  }
485  setNumUnitsPerSegment( unitsPerSegment );
486  setNumMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );
487 
488  setNumSegments( 4 );
489  setNumSegmentsLeft( 2 );
490  }
491 
492  refreshSegmentMillimeters();
493  adjustBoxSize();
494  emit itemChanged();
495 }
496 
498 {
499  if ( !mStyle )
500  {
501  return;
502  }
503 
504  QRectF box = QRectF( pos(), mStyle->calculateBoxSize( mSettings, createScaleContext() ) );
505  if ( rect().height() > box.height() )
506  {
507  //keep user specified item height if higher than minimum scale bar height
508  box.setHeight( rect().height() );
509  }
510 
511  //update rect for data defined size and position
512  QRectF newRect = evalItemRect( box, true );
513 
514  //scale bars have a minimum size, respect that regardless of data defined settings
515  if ( newRect.width() < box.width() )
516  {
517  newRect.setWidth( box.width() );
518  }
519  if ( newRect.height() < box.height() )
520  {
521  newRect.setHeight( box.height() );
522  }
523 
525 }
526 
527 void QgsComposerScaleBar::setSceneRect( const QRectF &rectangle )
528 {
529  QRectF box = QRectF( pos(), mStyle->calculateBoxSize( mSettings, createScaleContext() ) );
530  if ( rectangle.height() > box.height() )
531  {
532  //keep user specified item height if higher than minimum scale bar height
533  box.setHeight( rectangle.height() );
534  }
535  box.moveTopLeft( rectangle.topLeft() );
536 
537  //update rect for data defined size and position
538  QRectF newRect = evalItemRect( rectangle );
539 
540  //scale bars have a minimum size, respect that regardless of data defined settings
541  if ( newRect.width() < box.width() )
542  {
543  newRect.setWidth( box.width() );
544  }
545  if ( newRect.height() < box.height() )
546  {
547  newRect.setHeight( box.height() );
548  }
549 
551 }
552 
554 {
555  //Don't adjust box size for numeric scale bars:
556  if ( mStyle && mStyle->name() != QLatin1String( "Numeric" ) )
557  {
558  adjustBoxSize();
559  }
560  QgsComposerItem::update();
561 }
562 
564 {
565  if ( !mStyle )
566  {
567  return;
568  }
569  double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
570  refreshSegmentMillimeters();
571  double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
572  correctXPositionAlignment( width, widthAfter );
573  update();
574  emit itemChanged();
575 }
576 
577 void QgsComposerScaleBar::setStyle( const QString &styleName )
578 {
579  delete mStyle;
580  mStyle = nullptr;
581 
582  //switch depending on style name
583  if ( styleName == QLatin1String( "Single Box" ) )
584  {
585  mStyle = new QgsSingleBoxScaleBarRenderer();
586  }
587  else if ( styleName == QLatin1String( "Double Box" ) )
588  {
589  mStyle = new QgsDoubleBoxScaleBarRenderer();
590  }
591  else if ( styleName == QLatin1String( "Line Ticks Middle" ) || styleName == QLatin1String( "Line Ticks Down" ) || styleName == QLatin1String( "Line Ticks Up" ) )
592  {
594  if ( styleName == QLatin1String( "Line Ticks Middle" ) )
595  {
597  }
598  else if ( styleName == QLatin1String( "Line Ticks Down" ) )
599  {
601  }
602  else if ( styleName == QLatin1String( "Line Ticks Up" ) )
603  {
605  }
606  mStyle = tickStyle;
607  }
608  else if ( styleName == QLatin1String( "Numeric" ) )
609  {
610  mStyle = new QgsNumericScaleBarRenderer();
611  }
612  emit itemChanged();
613 }
614 
616 {
617  if ( mStyle )
618  {
619  return mStyle->name();
620  }
621  else
622  {
623  return QLatin1String( "" );
624  }
625 }
626 
628 {
629  return mSettings.font();
630 }
631 
632 void QgsComposerScaleBar::setFont( const QFont &font )
633 {
634  mSettings.setFont( font );
635  update();
636  emit itemChanged();
637 }
638 
639 bool QgsComposerScaleBar::writeXml( QDomElement &elem, QDomDocument &doc ) const
640 {
641  if ( elem.isNull() )
642  {
643  return false;
644  }
645 
646  QDomElement composerScaleBarElem = doc.createElement( QStringLiteral( "ComposerScaleBar" ) );
647  composerScaleBarElem.setAttribute( QStringLiteral( "height" ), QString::number( mSettings.height() ) );
648  composerScaleBarElem.setAttribute( QStringLiteral( "labelBarSpace" ), QString::number( mSettings.labelBarSpace() ) );
649  composerScaleBarElem.setAttribute( QStringLiteral( "boxContentSpace" ), QString::number( mSettings.boxContentSpace() ) );
650  composerScaleBarElem.setAttribute( QStringLiteral( "numSegments" ), mSettings.numberOfSegments() );
651  composerScaleBarElem.setAttribute( QStringLiteral( "numSegmentsLeft" ), mSettings.numberOfSegmentsLeft() );
652  composerScaleBarElem.setAttribute( QStringLiteral( "numUnitsPerSegment" ), QString::number( mSettings.unitsPerSegment() ) );
653  composerScaleBarElem.setAttribute( QStringLiteral( "segmentSizeMode" ), mSettings.segmentSizeMode() );
654  composerScaleBarElem.setAttribute( QStringLiteral( "minBarWidth" ), mSettings.minimumBarWidth() );
655  composerScaleBarElem.setAttribute( QStringLiteral( "maxBarWidth" ), mSettings.maximumBarWidth() );
656  composerScaleBarElem.setAttribute( QStringLiteral( "segmentMillimeters" ), QString::number( mSegmentMillimeters ) );
657  composerScaleBarElem.setAttribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QString::number( mSettings.mapUnitsPerScaleBarUnit() ) );
658  composerScaleBarElem.appendChild( QgsFontUtils::toXmlElement( mSettings.font(), doc, QStringLiteral( "scaleBarFont" ) ) );
659  composerScaleBarElem.setAttribute( QStringLiteral( "outlineWidth" ), QString::number( mSettings.lineWidth() ) );
660  composerScaleBarElem.setAttribute( QStringLiteral( "unitLabel" ), mSettings.unitLabel() );
661  composerScaleBarElem.setAttribute( QStringLiteral( "unitType" ), QgsUnitTypes::encodeUnit( mSettings.units() ) );
662  composerScaleBarElem.setAttribute( QStringLiteral( "lineJoinStyle" ), QgsSymbolLayerUtils::encodePenJoinStyle( mSettings.lineJoinStyle() ) );
663  composerScaleBarElem.setAttribute( QStringLiteral( "lineCapStyle" ), QgsSymbolLayerUtils::encodePenCapStyle( mSettings.lineCapStyle() ) );
664 
665  //style
666  if ( mStyle )
667  {
668  composerScaleBarElem.setAttribute( QStringLiteral( "style" ), mStyle->name() );
669  }
670 
671  //map id
672  if ( mComposerMap )
673  {
674  composerScaleBarElem.setAttribute( QStringLiteral( "mapId" ), mComposerMap->id() );
675  }
676 
677  //colors
678 
679  //fill color
680  QDomElement fillColorElem = doc.createElement( QStringLiteral( "fillColor" ) );
681  fillColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor().red() ) );
682  fillColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor().green() ) );
683  fillColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor().blue() ) );
684  fillColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor().alpha() ) );
685  composerScaleBarElem.appendChild( fillColorElem );
686 
687  //fill color 2
688  QDomElement fillColor2Elem = doc.createElement( QStringLiteral( "fillColor2" ) );
689  fillColor2Elem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor2().red() ) );
690  fillColor2Elem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor2().green() ) );
691  fillColor2Elem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor2().blue() ) );
692  fillColor2Elem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor2().alpha() ) );
693  composerScaleBarElem.appendChild( fillColor2Elem );
694 
695  //pen color
696  QDomElement strokeColorElem = doc.createElement( QStringLiteral( "strokeColor" ) );
697  strokeColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.lineColor().red() ) );
698  strokeColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.lineColor().green() ) );
699  strokeColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.lineColor().blue() ) );
700  strokeColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.lineColor().alpha() ) );
701  composerScaleBarElem.appendChild( strokeColorElem );
702 
703  //font color
704  QDomElement fontColorElem = doc.createElement( QStringLiteral( "textColor" ) );
705  fontColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fontColor().red() ) );
706  fontColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fontColor().green() ) );
707  fontColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fontColor().blue() ) );
708  fontColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fontColor().alpha() ) );
709  composerScaleBarElem.appendChild( fontColorElem );
710 
711  //alignment
712  composerScaleBarElem.setAttribute( QStringLiteral( "alignment" ), QString::number( static_cast< int >( mSettings.alignment() ) ) );
713 
714  elem.appendChild( composerScaleBarElem );
715  return _writeXml( composerScaleBarElem, doc );
716 }
717 
718 bool QgsComposerScaleBar::readXml( const QDomElement &itemElem, const QDomDocument &doc )
719 {
720  if ( itemElem.isNull() )
721  {
722  return false;
723  }
724 
725  mSettings.setHeight( itemElem.attribute( QStringLiteral( "height" ), QStringLiteral( "5.0" ) ).toDouble() );
726  mSettings.setLabelBarSpace( itemElem.attribute( QStringLiteral( "labelBarSpace" ), QStringLiteral( "3.0" ) ).toDouble() );
727  mSettings.setBoxContentSpace( itemElem.attribute( QStringLiteral( "boxContentSpace" ), QStringLiteral( "1.0" ) ).toDouble() );
728  mSettings.setNumberOfSegments( itemElem.attribute( QStringLiteral( "numSegments" ), QStringLiteral( "2" ) ).toInt() );
729  mSettings.setNumberOfSegmentsLeft( itemElem.attribute( QStringLiteral( "numSegmentsLeft" ), QStringLiteral( "0" ) ).toInt() );
730  mSettings.setUnitsPerSegment( itemElem.attribute( QStringLiteral( "numUnitsPerSegment" ), QStringLiteral( "1.0" ) ).toDouble() );
731  mSettings.setSegmentSizeMode( static_cast<QgsScaleBarSettings::SegmentSizeMode>( itemElem.attribute( QStringLiteral( "segmentSizeMode" ), QStringLiteral( "0" ) ).toInt() ) );
732  mSettings.setMinimumBarWidth( itemElem.attribute( QStringLiteral( "minBarWidth" ), QStringLiteral( "50" ) ).toDouble() );
733  mSettings.setMaximumBarWidth( itemElem.attribute( QStringLiteral( "maxBarWidth" ), QStringLiteral( "150" ) ).toDouble() );
734  mSegmentMillimeters = itemElem.attribute( QStringLiteral( "segmentMillimeters" ), QStringLiteral( "0.0" ) ).toDouble();
735  mSettings.setMapUnitsPerScaleBarUnit( itemElem.attribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QStringLiteral( "1.0" ) ).toDouble() );
736  mSettings.setLineWidth( itemElem.attribute( QStringLiteral( "outlineWidth" ), QStringLiteral( "0.3" ) ).toDouble() );
737  mSettings.setUnitLabel( itemElem.attribute( QStringLiteral( "unitLabel" ) ) );
738  mSettings.setLineJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( itemElem.attribute( QStringLiteral( "lineJoinStyle" ), QStringLiteral( "miter" ) ) ) );
739  mSettings.setLineCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( itemElem.attribute( QStringLiteral( "lineCapStyle" ), QStringLiteral( "square" ) ) ) );
740  QFont f;
741  if ( !QgsFontUtils::setFromXmlChildNode( f, itemElem, QStringLiteral( "scaleBarFont" ) ) )
742  {
743  f.fromString( itemElem.attribute( QStringLiteral( "font" ), QLatin1String( "" ) ) );
744  }
745  mSettings.setFont( f );
746 
747  //colors
748  //fill color
749  QDomNodeList fillColorList = itemElem.elementsByTagName( QStringLiteral( "fillColor" ) );
750  if ( !fillColorList.isEmpty() )
751  {
752  QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
753  bool redOk, greenOk, blueOk, alphaOk;
754  int fillRed, fillGreen, fillBlue, fillAlpha;
755 
756  fillRed = fillColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
757  fillGreen = fillColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
758  fillBlue = fillColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
759  fillAlpha = fillColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
760 
761  if ( redOk && greenOk && blueOk && alphaOk )
762  {
763  mSettings.setFillColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
764  }
765  }
766  else
767  {
768  mSettings.setFillColor( QColor( itemElem.attribute( QStringLiteral( "brushColor" ), QStringLiteral( "#000000" ) ) ) );
769  }
770 
771  //fill color 2
772  QDomNodeList fillColor2List = itemElem.elementsByTagName( QStringLiteral( "fillColor2" ) );
773  if ( !fillColor2List.isEmpty() )
774  {
775  QDomElement fillColor2Elem = fillColor2List.at( 0 ).toElement();
776  bool redOk, greenOk, blueOk, alphaOk;
777  int fillRed, fillGreen, fillBlue, fillAlpha;
778 
779  fillRed = fillColor2Elem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
780  fillGreen = fillColor2Elem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
781  fillBlue = fillColor2Elem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
782  fillAlpha = fillColor2Elem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
783 
784  if ( redOk && greenOk && blueOk && alphaOk )
785  {
786  mSettings.setFillColor2( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
787  }
788  }
789  else
790  {
791  mSettings.setFillColor2( QColor( itemElem.attribute( QStringLiteral( "brush2Color" ), QStringLiteral( "#ffffff" ) ) ) );
792  }
793 
794  //stroke color
795  QDomNodeList strokeColorList = itemElem.elementsByTagName( QStringLiteral( "strokeColor" ) );
796  if ( !strokeColorList.isEmpty() )
797  {
798  QDomElement strokeColorElem = strokeColorList.at( 0 ).toElement();
799  bool redOk, greenOk, blueOk, alphaOk;
800  int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
801 
802  strokeRed = strokeColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
803  strokeGreen = strokeColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
804  strokeBlue = strokeColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
805  strokeAlpha = strokeColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
806 
807  if ( redOk && greenOk && blueOk && alphaOk )
808  {
809  mSettings.setLineColor( QColor( strokeRed, strokeGreen, strokeBlue, strokeAlpha ) );
810  QPen p = mSettings.pen();
811  p.setColor( mSettings.lineColor() );
812  mSettings.setPen( p );
813  }
814  }
815  else
816  {
817  mSettings.setLineColor( QColor( itemElem.attribute( QStringLiteral( "penColor" ), QStringLiteral( "#000000" ) ) ) );
818  QPen p = mSettings.pen();
819  p.setColor( mSettings.lineColor() );
820  mSettings.setPen( p );
821  }
822 
823  //font color
824  QDomNodeList textColorList = itemElem.elementsByTagName( QStringLiteral( "textColor" ) );
825  if ( !textColorList.isEmpty() )
826  {
827  QDomElement textColorElem = textColorList.at( 0 ).toElement();
828  bool redOk, greenOk, blueOk, alphaOk;
829  int textRed, textGreen, textBlue, textAlpha;
830 
831  textRed = textColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
832  textGreen = textColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
833  textBlue = textColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
834  textAlpha = textColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
835 
836  if ( redOk && greenOk && blueOk && alphaOk )
837  {
838  mSettings.setFontColor( QColor( textRed, textGreen, textBlue, textAlpha ) );
839  }
840  }
841  else
842  {
843  QColor c;
844  c.setNamedColor( itemElem.attribute( QStringLiteral( "fontColor" ), QStringLiteral( "#000000" ) ) );
845  mSettings.setFontColor( c );
846  }
847 
848  //style
849  delete mStyle;
850  mStyle = nullptr;
851  QString styleString = itemElem.attribute( QStringLiteral( "style" ), QLatin1String( "" ) );
852  setStyle( tr( styleString.toLocal8Bit().data() ) );
853 
854  if ( itemElem.attribute( QStringLiteral( "unitType" ) ).isEmpty() )
855  {
857  switch ( itemElem.attribute( QStringLiteral( "units" ) ).toInt() )
858  {
859  case 0:
861  break;
862  case 1:
864  break;
865  case 2:
867  break;
868  case 3:
870  break;
871  }
872  mSettings.setUnits( u );
873  }
874  else
875  {
876  mSettings.setUnits( QgsUnitTypes::decodeDistanceUnit( itemElem.attribute( QStringLiteral( "unitType" ) ) ) );
877  }
878  mSettings.setAlignment( static_cast< QgsScaleBarSettings::Alignment >( itemElem.attribute( QStringLiteral( "alignment" ), QStringLiteral( "0" ) ).toInt() ) );
879 
880  //map
881  int mapId = itemElem.attribute( QStringLiteral( "mapId" ), QStringLiteral( "-1" ) ).toInt();
882  if ( mapId >= 0 )
883  {
885  mComposerMap = const_cast< QgsComposerMap *>( composerMap );
886  if ( mComposerMap )
887  {
889  connect( mComposerMap, &QObject::destroyed, this, &QgsComposerScaleBar::invalidateCurrentMap );
890  }
891  }
892 
894 
895  //restore general composer item properties
896  QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
897  if ( !composerItemList.isEmpty() )
898  {
899  QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
900  _readXml( composerItemElem, doc );
901  }
902 
903  return true;
904 }
905 
906 void QgsComposerScaleBar::correctXPositionAlignment( double width, double widthAfter )
907 {
908  //Don't adjust position for numeric scale bars:
909  if ( mStyle->name() == QLatin1String( "Numeric" ) )
910  {
911  return;
912  }
913 
914  if ( mSettings.alignment() == QgsScaleBarSettings::AlignMiddle )
915  {
916  move( -( widthAfter - width ) / 2.0, 0 );
917  }
918  else if ( mSettings.alignment() == QgsScaleBarSettings::AlignRight )
919  {
920  move( -( widthAfter - width ), 0 );
921  }
922 }
923 
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:138
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:126
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:111
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:116
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.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
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...
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.
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
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.