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