QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgslayoutitemscalebar.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemscalebar.cpp
3  -------------------------
4  begin : November 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgslayoutitemscalebar.h"
18 #include "qgslayoutitemregistry.h"
20 #include "qgslayoutitemmap.h"
21 #include "qgslayout.h"
22 #include "qgslayoututils.h"
23 #include "qgsdistancearea.h"
25 #include "qgsscalebarrenderer.h"
26 #include "qgsmapsettings.h"
27 #include "qgsrectangle.h"
28 #include "qgsproject.h"
29 #include "qgssymbollayerutils.h"
30 #include "qgsfontutils.h"
31 #include "qgsunittypes.h"
32 #include "qgssettings.h"
33 #include "qgsstyleentityvisitor.h"
34 #include "qgsnumericformat.h"
36 #include "qgslinesymbollayer.h"
37 #include "qgsfillsymbollayer.h"
38 
39 #include <QDomDocument>
40 #include <QDomElement>
41 #include <QFontMetricsF>
42 #include <QPainter>
43 
44 #include <cmath>
45 
47  : QgsLayoutItem( layout )
48 {
51 }
52 
54 {
56 }
57 
59 {
60  return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemScaleBar.svg" ) );
61 }
62 
64 {
65  return new QgsLayoutItemScaleBar( layout );
66 }
67 
69 {
71  return QgsLayoutSize( mStyle->calculateBoxSize( context, mSettings, createScaleContext() ), QgsUnitTypes::LayoutMillimeters );
72 }
73 
75 {
76  if ( !mStyle )
77  return;
78 
80  {
81  // compatibility code - ScalebarLineColor and ScalebarLineWidth are deprecated
83  std::unique_ptr< QgsLineSymbol > sym( mSettings.lineSymbol()->clone() );
86  sym->setWidth( mDataDefinedProperties.valueAsDouble( QgsLayoutObject::ScalebarLineWidth, expContext, mSettings.lineWidth() ) );
88  sym->setColor( mDataDefinedProperties.valueAsColor( QgsLayoutObject::ScalebarLineColor, expContext, mSettings.lineColor() ) );
90  mSettings.setLineSymbol( sym.release() );
91  }
93  {
94  // compatibility code - ScalebarLineColor and ScalebarLineWidth are deprecated
96  std::unique_ptr< QgsFillSymbol > sym( mSettings.fillSymbol()->clone() );
98  sym->setColor( mDataDefinedProperties.valueAsColor( QgsLayoutObject::ScalebarFillColor, expContext, mSettings.fillColor() ) );
100  mSettings.setFillSymbol( sym.release() );
101  }
103  {
104  // compatibility code - ScalebarLineColor and ScalebarLineWidth are deprecated
106  std::unique_ptr< QgsFillSymbol > sym( mSettings.alternateFillSymbol()->clone() );
108  sym->setColor( mDataDefinedProperties.valueAsColor( QgsLayoutObject::ScalebarFillColor2, expContext, mSettings.fillColor2() ) );
110  mSettings.setAlternateFillSymbol( sym.release() );
111  }
112 
113  mStyle->draw( context.renderContext(), mSettings, createScaleContext() );
114 }
115 
117 {
118  if ( !mStyle )
119  {
120  mSettings.setNumberOfSegments( nSegments );
121  return;
122  }
123  mSettings.setNumberOfSegments( nSegments );
125 }
126 
128 {
129  if ( !mStyle )
130  {
131  mSettings.setUnitsPerSegment( units );
132  return;
133  }
134  mSettings.setUnitsPerSegment( units );
135  refreshSegmentMillimeters();
137 }
138 
140 {
141  if ( !mStyle )
142  {
143  mSettings.setSegmentSizeMode( mode );
144  return;
145  }
146  mSettings.setSegmentSizeMode( mode );
147  refreshSegmentMillimeters();
149 }
150 
152 {
153  if ( !mStyle )
154  {
155  mSettings.setMinimumBarWidth( minWidth );
156  return;
157  }
158  mSettings.setMinimumBarWidth( minWidth );
159  refreshSegmentMillimeters();
161 }
162 
164 {
165  if ( !mStyle )
166  {
167  mSettings.setMaximumBarWidth( maxWidth );
168  return;
169  }
170  mSettings.setMaximumBarWidth( maxWidth );
171  refreshSegmentMillimeters();
173 }
174 
176 {
177  return mSettings.textFormat();
178 }
179 
181 {
182  mSettings.setTextFormat( format );
183  refreshItemSize();
184  emit changed();
185 }
186 
188 {
189  return mSettings.lineSymbol();
190 }
191 
193 {
194  mSettings.setLineSymbol( symbol );
195 }
196 
198 {
199  return mSettings.divisionLineSymbol();
200 }
201 
203 {
204  mSettings.setDivisionLineSymbol( symbol );
205 }
206 
208 {
209  return mSettings.subdivisionLineSymbol();
210 }
211 
213 {
214  mSettings.setSubdivisionLineSymbol( symbol );
215 }
216 
218 {
219  return mSettings.fillSymbol();
220 }
221 
223 {
224  mSettings.setFillSymbol( symbol );
225 }
226 
228 {
229  return mSettings.alternateFillSymbol();
230 }
231 
233 {
234  mSettings.setAlternateFillSymbol( symbol );
235 }
236 
238 {
239  if ( !mStyle )
240  {
241  mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
242  return;
243  }
244  mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
246 }
247 
249 {
250  if ( !mStyle )
251  {
252  mSettings.setBoxContentSpace( space );
253  return;
254  }
255  mSettings.setBoxContentSpace( space );
256  refreshItemSize();
257 }
258 
260 {
261  disconnectCurrentMap();
262 
263  mMap = map;
264 
265  if ( !map )
266  {
267  return;
268  }
269 
270  connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
271  connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
272 
273  refreshSegmentMillimeters();
274  emit changed();
275 }
276 
277 void QgsLayoutItemScaleBar::disconnectCurrentMap()
278 {
279  if ( !mMap )
280  {
281  return;
282  }
283 
284  disconnect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
285  disconnect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
286  mMap = nullptr;
287 }
288 
290 {
292 
293  bool forceUpdate = false;
294  //updates data defined properties and redraws item to match
296  {
297  forceUpdate = true;
298  }
300  {
301  forceUpdate = true;
302  }
304  {
305  forceUpdate = true;
306  }
308  {
309  forceUpdate = true;
310  }
311  if ( forceUpdate )
312  {
313  refreshItemSize();
314  update();
315  }
316 
318 }
319 
320 void QgsLayoutItemScaleBar::refreshSegmentMillimeters()
321 {
322  if ( mMap )
323  {
324  //get mm dimension of composer map
325  QRectF composerItemRect = mMap->rect();
326 
327  switch ( mSettings.segmentSizeMode() )
328  {
330  {
331  //calculate size depending on mNumUnitsPerSegment
332  mSegmentMillimeters = composerItemRect.width() / mapWidth() * mSettings.unitsPerSegment();
333  break;
334  }
335 
337  {
338  if ( mSettings.maximumBarWidth() < mSettings.minimumBarWidth() )
339  {
340  mSegmentMillimeters = 0;
341  }
342  else
343  {
344  double nSegments = ( mSettings.numberOfSegmentsLeft() != 0 ) + mSettings.numberOfSegments();
345  // unitsPerSegments which fit minBarWidth resp. maxBarWidth
346  double minUnitsPerSeg = ( mSettings.minimumBarWidth() * mapWidth() ) / ( nSegments * composerItemRect.width() );
347  double maxUnitsPerSeg = ( mSettings.maximumBarWidth() * mapWidth() ) / ( nSegments * composerItemRect.width() );
348  mSettings.setUnitsPerSegment( QgsLayoutUtils::calculatePrettySize( minUnitsPerSeg, maxUnitsPerSeg ) );
349  mSegmentMillimeters = composerItemRect.width() / mapWidth() * mSettings.unitsPerSegment();
350  }
351  break;
352  }
353  }
354  }
355 }
356 
357 double QgsLayoutItemScaleBar::mapWidth() const
358 {
359  if ( !mMap )
360  {
361  return 0.0;
362  }
363 
364  QgsRectangle mapExtent = mMap->extent();
365  if ( mSettings.units() == QgsUnitTypes::DistanceUnknownUnit )
366  {
367  return mapExtent.width();
368  }
369  else
370  {
371  QgsDistanceArea da;
372  da.setSourceCrs( mMap->crs(), mLayout->project()->transformContext() );
373  da.setEllipsoid( mLayout->project()->ellipsoid() );
374 
376  double measure = da.measureLine( QgsPointXY( mapExtent.xMinimum(), mapExtent.yMinimum() ),
377  QgsPointXY( mapExtent.xMaximum(), mapExtent.yMinimum() ) );
378  measure /= QgsUnitTypes::fromUnitToUnitFactor( mSettings.units(), units );
379  return measure;
380  }
381 }
382 
383 QgsScaleBarRenderer::ScaleBarContext QgsLayoutItemScaleBar::createScaleContext() const
384 {
386  scaleContext.size = rect().size();
387  scaleContext.segmentWidth = mSegmentMillimeters;
388  scaleContext.scale = mMap ? mMap->scale() : 1.0;
389  scaleContext.flags = mStyle->flags();
390  return scaleContext;
391 }
392 
394 {
395  mSettings.setLabelVerticalPlacement( placement );
396  refreshItemSize();
397  emit changed();
398 }
399 
401 {
402  mSettings.setLabelHorizontalPlacement( placement );
403  refreshItemSize();
404  emit changed();
405 }
406 
408 {
409  mSettings.setAlignment( a );
410  refreshItemSize();
411  emit changed();
412 }
413 
415 {
416  mSettings.setUnits( u );
417  refreshSegmentMillimeters();
418  refreshItemSize();
419  emit changed();
420 }
421 
422 Qt::PenJoinStyle QgsLayoutItemScaleBar::lineJoinStyle() const
423 {
425  return mSettings.lineJoinStyle();
427 }
428 
429 void QgsLayoutItemScaleBar::setLineJoinStyle( Qt::PenJoinStyle style )
430 {
432  if ( mSettings.lineJoinStyle() == style )
433  {
434  //no change
435  return;
436  }
437  mSettings.setLineJoinStyle( style );
439  update();
440  emit changed();
441 }
442 
443 Qt::PenCapStyle QgsLayoutItemScaleBar::lineCapStyle() const
444 {
446  return mSettings.lineCapStyle();
448 }
449 
450 void QgsLayoutItemScaleBar::setLineCapStyle( Qt::PenCapStyle style )
451 {
453  if ( mSettings.lineCapStyle() == style )
454  {
455  //no change
456  return;
457  }
458  mSettings.setLineCapStyle( style );
460  update();
461  emit changed();
462 }
463 
465 {
466  //style
467  mStyle = qgis::make_unique< QgsSingleBoxScaleBarRenderer >();
468 
469  //default to no background
470  setBackgroundEnabled( false );
471 
472  //get default composer font from settings
473  QgsSettings settings;
474  QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
475  QgsTextFormat format;
476  QFont f;
477  if ( !defaultFontString.isEmpty() )
478  {
479  f.setFamily( defaultFontString );
480  }
481  format.setFont( f );
482  format.setSize( 12.0 );
484 
485  mSettings.setTextFormat( format );
486 
488  refreshItemSize();
489 
490  emit changed();
491 }
492 
494 {
495  return renderer->applyDefaultSettings( mSettings );
496 }
497 
499 {
500  if ( !mMap )
502 
504  // start with crs units
507  {
508  // geographic CRS, use metric units
510  }
511 
512  // try to pick reasonable choice between metric / imperial units
513  double widthInSelectedUnits = mapWidth();
514  double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
515  switch ( unit )
516  {
518  {
519  if ( initialUnitsPerSegment > 1000.0 )
520  {
522  }
523  break;
524  }
526  {
527  if ( initialUnitsPerSegment > 5419.95 )
528  {
530  }
531  break;
532  }
533  default:
534  break;
535  }
536 
537  return unit;
538 }
539 
541 {
542  mSettings.setUnits( units );
543  if ( mMap )
544  {
545  double upperMagnitudeMultiplier = 1.0;
546  double widthInSelectedUnits = mapWidth();
547  double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
548  mSettings.setUnitsPerSegment( initialUnitsPerSegment );
549 
551  upperMagnitudeMultiplier = 1;
552 
553  double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
554  int segmentMagnitude = std::floor( std::log10( segmentWidth ) );
555  double unitsPerSegment = upperMagnitudeMultiplier * ( std::pow( 10.0, segmentMagnitude ) );
556  double multiplier = std::floor( ( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
557 
558  if ( multiplier > 0 )
559  {
560  unitsPerSegment = unitsPerSegment * multiplier;
561  }
562  mSettings.setUnitsPerSegment( unitsPerSegment );
563  mSettings.setMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );
564 
565  mSettings.setNumberOfSegments( 2 );
566  mSettings.setNumberOfSegmentsLeft( 0 );
567  }
568 
569  refreshSegmentMillimeters();
571  emit changed();
572 }
573 
575 {
576  if ( !mStyle )
577  return;
578 
580  double widthMM = mStyle->calculateBoxSize( context, mSettings, createScaleContext() ).width();
581  QgsLayoutSize currentSize = sizeWithUnits();
582  currentSize.setWidth( mLayout->renderContext().measurementConverter().convert( QgsLayoutMeasurement( widthMM, QgsUnitTypes::LayoutMillimeters ), currentSize.units() ).length() );
583  attemptResize( currentSize );
584  update();
585  emit changed();
586 }
587 
589 {
590  //Don't adjust box size for numeric scale bars:
591  if ( mStyle && mStyle->id() != QLatin1String( "Numeric" ) )
592  {
593  refreshItemSize();
594  }
595  QgsLayoutItem::update();
596 }
597 
598 void QgsLayoutItemScaleBar::updateScale()
599 {
600  refreshSegmentMillimeters();
601  //Don't adjust box size for numeric scale bars:
602  if ( mStyle && mStyle->id() != QLatin1String( "Numeric" ) )
603  {
605  }
606  update();
607 }
608 
609 void QgsLayoutItemScaleBar::setStyle( const QString &styleName )
610 {
611  //switch depending on style name
612  std::unique_ptr< QgsScaleBarRenderer> renderer( QgsApplication::scaleBarRendererRegistry()->renderer( styleName ) );
613  if ( renderer )
614  {
615  mStyle = std::move( renderer );
616  }
617  refreshItemSize();
618  emit changed();
619 }
620 
622 {
623  if ( mStyle )
624  {
625  return mStyle->id();
626  }
627  else
628  {
629  return QString();
630  }
631 }
632 
634 {
635  return mSettings.numericFormat();
636 }
637 
639 {
640  mSettings.setNumericFormat( format );
641 }
642 
644 {
645  return mSettings.textFormat().font();
646 }
647 
648 void QgsLayoutItemScaleBar::setFont( const QFont &font )
649 {
651  mSettings.setFont( font );
653  refreshItemSize();
654  emit changed();
655 }
656 
658 {
659  QColor color = mSettings.textFormat().color();
660  color.setAlphaF( mSettings.textFormat().opacity() );
661  return color;
662 }
663 
664 void QgsLayoutItemScaleBar::setFontColor( const QColor &color )
665 {
666  mSettings.textFormat().setColor( color );
667  mSettings.textFormat().setOpacity( color.alphaF() );
668 }
669 
671 {
673  return mSettings.fillColor();
675 }
676 
677 void QgsLayoutItemScaleBar::setFillColor( const QColor &color )
678 {
680  mSettings.setFillColor( color );
682 }
683 
685 {
687  return mSettings.fillColor2();
689 }
690 
691 void QgsLayoutItemScaleBar::setFillColor2( const QColor &color )
692 {
694  mSettings.setFillColor2( color );
696 }
697 
699 {
701  return mSettings.lineColor();
703 }
704 
705 void QgsLayoutItemScaleBar::setLineColor( const QColor &color )
706 {
708  mSettings.setLineColor( color );
710 }
711 
713 {
715  return mSettings.lineWidth();
717 }
718 
720 {
722  mSettings.setLineWidth( width );
724 }
725 
727 {
729  return mSettings.pen();
731 }
732 
734 {
736  return mSettings.brush();
738 }
739 
741 {
743  return mSettings.brush2();
745 }
746 
747 bool QgsLayoutItemScaleBar::writePropertiesToElement( QDomElement &composerScaleBarElem, QDomDocument &doc, const QgsReadWriteContext &rwContext ) const
748 {
749  composerScaleBarElem.setAttribute( QStringLiteral( "height" ), QString::number( mSettings.height() ) );
750  composerScaleBarElem.setAttribute( QStringLiteral( "labelBarSpace" ), QString::number( mSettings.labelBarSpace() ) );
751  composerScaleBarElem.setAttribute( QStringLiteral( "boxContentSpace" ), QString::number( mSettings.boxContentSpace() ) );
752  composerScaleBarElem.setAttribute( QStringLiteral( "numSegments" ), mSettings.numberOfSegments() );
753  composerScaleBarElem.setAttribute( QStringLiteral( "numSegmentsLeft" ), mSettings.numberOfSegmentsLeft() );
754  composerScaleBarElem.setAttribute( QStringLiteral( "numSubdivisions" ), mSettings.numberOfSubdivisions() );
755  composerScaleBarElem.setAttribute( QStringLiteral( "subdivisionsHeight" ), mSettings.subdivisionsHeight() );
756  composerScaleBarElem.setAttribute( QStringLiteral( "numUnitsPerSegment" ), QString::number( mSettings.unitsPerSegment() ) );
757  composerScaleBarElem.setAttribute( QStringLiteral( "segmentSizeMode" ), mSettings.segmentSizeMode() );
758  composerScaleBarElem.setAttribute( QStringLiteral( "minBarWidth" ), mSettings.minimumBarWidth() );
759  composerScaleBarElem.setAttribute( QStringLiteral( "maxBarWidth" ), mSettings.maximumBarWidth() );
760  composerScaleBarElem.setAttribute( QStringLiteral( "segmentMillimeters" ), QString::number( mSegmentMillimeters ) );
761  composerScaleBarElem.setAttribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QString::number( mSettings.mapUnitsPerScaleBarUnit() ) );
762 
763  QDomElement textElem = mSettings.textFormat().writeXml( doc, rwContext );
764  composerScaleBarElem.appendChild( textElem );
765 
767  // kept just for allowing projects to open in QGIS < 3.14, remove for 4.0
768  composerScaleBarElem.setAttribute( QStringLiteral( "outlineWidth" ), QString::number( mSettings.lineWidth() ) );
769  composerScaleBarElem.setAttribute( QStringLiteral( "lineJoinStyle" ), QgsSymbolLayerUtils::encodePenJoinStyle( mSettings.lineJoinStyle() ) );
770  composerScaleBarElem.setAttribute( QStringLiteral( "lineCapStyle" ), QgsSymbolLayerUtils::encodePenCapStyle( mSettings.lineCapStyle() ) );
771  //pen color
772  QDomElement strokeColorElem = doc.createElement( QStringLiteral( "strokeColor" ) );
773  strokeColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.lineColor().red() ) );
774  strokeColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.lineColor().green() ) );
775  strokeColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.lineColor().blue() ) );
776  strokeColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.lineColor().alpha() ) );
777  composerScaleBarElem.appendChild( strokeColorElem );
779 
780  composerScaleBarElem.setAttribute( QStringLiteral( "unitLabel" ), mSettings.unitLabel() );
781  composerScaleBarElem.setAttribute( QStringLiteral( "unitType" ), QgsUnitTypes::encodeUnit( mSettings.units() ) );
782 
783  QDomElement numericFormatElem = doc.createElement( QStringLiteral( "numericFormat" ) );
784  mSettings.numericFormat()->writeXml( numericFormatElem, doc, rwContext );
785  composerScaleBarElem.appendChild( numericFormatElem );
786 
787  //style
788  if ( mStyle )
789  {
790  composerScaleBarElem.setAttribute( QStringLiteral( "style" ), mStyle->id() );
791  }
792 
793  //map id
794  if ( mMap )
795  {
796  composerScaleBarElem.setAttribute( QStringLiteral( "mapUuid" ), mMap->uuid() );
797  }
798 
799  //colors
800 
802  // kept just for allowing projects to open in QGIS < 3.14, remove for 4.0
803 
804  //fill color
805  QDomElement fillColorElem = doc.createElement( QStringLiteral( "fillColor" ) );
806  fillColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor().red() ) );
807  fillColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor().green() ) );
808  fillColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor().blue() ) );
809  fillColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor().alpha() ) );
810  composerScaleBarElem.appendChild( fillColorElem );
811 
812  //fill color 2
813  QDomElement fillColor2Elem = doc.createElement( QStringLiteral( "fillColor2" ) );
814  fillColor2Elem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor2().red() ) );
815  fillColor2Elem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor2().green() ) );
816  fillColor2Elem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor2().blue() ) );
817  fillColor2Elem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor2().alpha() ) );
818  composerScaleBarElem.appendChild( fillColor2Elem );
819 
821 
822  //label vertical/horizontal placement
823  composerScaleBarElem.setAttribute( QStringLiteral( "labelVerticalPlacement" ), QString::number( static_cast< int >( mSettings.labelVerticalPlacement() ) ) );
824  composerScaleBarElem.setAttribute( QStringLiteral( "labelHorizontalPlacement" ), QString::number( static_cast< int >( mSettings.labelHorizontalPlacement() ) ) );
825 
826  //alignment
827  composerScaleBarElem.setAttribute( QStringLiteral( "alignment" ), QString::number( static_cast< int >( mSettings.alignment() ) ) );
828 
829  QDomElement lineSymbol = doc.createElement( QStringLiteral( "lineSymbol" ) );
830  const QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol( QString(),
831  mSettings.lineSymbol(),
832  doc,
833  rwContext );
834  lineSymbol.appendChild( symbolElem );
835  composerScaleBarElem.appendChild( lineSymbol );
836 
837  QDomElement divisionSymbol = doc.createElement( QStringLiteral( "divisionLineSymbol" ) );
838  const QDomElement divisionSymbolElem = QgsSymbolLayerUtils::saveSymbol( QString(),
839  mSettings.divisionLineSymbol(),
840  doc,
841  rwContext );
842  divisionSymbol.appendChild( divisionSymbolElem );
843  composerScaleBarElem.appendChild( divisionSymbol );
844 
845  QDomElement subdivisionSymbol = doc.createElement( QStringLiteral( "subdivisionLineSymbol" ) );
846  const QDomElement subdivisionSymbolElem = QgsSymbolLayerUtils::saveSymbol( QString(),
847  mSettings.subdivisionLineSymbol(),
848  doc,
849  rwContext );
850  subdivisionSymbol.appendChild( subdivisionSymbolElem );
851  composerScaleBarElem.appendChild( subdivisionSymbol );
852 
853  QDomElement fillSymbol1Elem = doc.createElement( QStringLiteral( "fillSymbol1" ) );
854  const QDomElement symbol1Elem = QgsSymbolLayerUtils::saveSymbol( QString(),
855  mSettings.fillSymbol(),
856  doc,
857  rwContext );
858  fillSymbol1Elem.appendChild( symbol1Elem );
859  composerScaleBarElem.appendChild( fillSymbol1Elem );
860 
861  QDomElement fillSymbol2Elem = doc.createElement( QStringLiteral( "fillSymbol2" ) );
862  const QDomElement symbol2Elem = QgsSymbolLayerUtils::saveSymbol( QString(),
863  mSettings.alternateFillSymbol(),
864  doc,
865  rwContext );
866  fillSymbol2Elem.appendChild( symbol2Elem );
867  composerScaleBarElem.appendChild( fillSymbol2Elem );
868 
869  return true;
870 }
871 
872 bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext &context )
873 {
874  mSettings.setHeight( itemElem.attribute( QStringLiteral( "height" ), QStringLiteral( "5.0" ) ).toDouble() );
875  mSettings.setLabelBarSpace( itemElem.attribute( QStringLiteral( "labelBarSpace" ), QStringLiteral( "3.0" ) ).toDouble() );
876  mSettings.setBoxContentSpace( itemElem.attribute( QStringLiteral( "boxContentSpace" ), QStringLiteral( "1.0" ) ).toDouble() );
877  mSettings.setNumberOfSegments( itemElem.attribute( QStringLiteral( "numSegments" ), QStringLiteral( "2" ) ).toInt() );
878  mSettings.setNumberOfSegmentsLeft( itemElem.attribute( QStringLiteral( "numSegmentsLeft" ), QStringLiteral( "0" ) ).toInt() );
879  mSettings.setNumberOfSubdivisions( itemElem.attribute( QStringLiteral( "numSubdivisions" ), QStringLiteral( "1" ) ).toInt() );
880  mSettings.setSubdivisionsHeight( itemElem.attribute( QStringLiteral( "subdivisionsHeight" ), QStringLiteral( "1.5" ) ).toDouble() );
881  mSettings.setUnitsPerSegment( itemElem.attribute( QStringLiteral( "numUnitsPerSegment" ), QStringLiteral( "1.0" ) ).toDouble() );
882  mSettings.setSegmentSizeMode( static_cast<QgsScaleBarSettings::SegmentSizeMode>( itemElem.attribute( QStringLiteral( "segmentSizeMode" ), QStringLiteral( "0" ) ).toInt() ) );
883  mSettings.setMinimumBarWidth( itemElem.attribute( QStringLiteral( "minBarWidth" ), QStringLiteral( "50" ) ).toDouble() );
884  mSettings.setMaximumBarWidth( itemElem.attribute( QStringLiteral( "maxBarWidth" ), QStringLiteral( "150" ) ).toDouble() );
885  mSegmentMillimeters = itemElem.attribute( QStringLiteral( "segmentMillimeters" ), QStringLiteral( "0.0" ) ).toDouble();
886  mSettings.setMapUnitsPerScaleBarUnit( itemElem.attribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QStringLiteral( "1.0" ) ).toDouble() );
887 
888  QDomElement lineSymbolElem = itemElem.firstChildElement( QStringLiteral( "lineSymbol" ) );
889  bool foundLineSymbol = false;
890  if ( !lineSymbolElem.isNull() )
891  {
892  QDomElement symbolElem = lineSymbolElem.firstChildElement( QStringLiteral( "symbol" ) );
893  std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
894  if ( lineSymbol )
895  {
896  mSettings.setLineSymbol( lineSymbol.release() );
897  foundLineSymbol = true;
898  }
899  }
900  QDomElement divisionSymbolElem = itemElem.firstChildElement( QStringLiteral( "divisionLineSymbol" ) );
901  if ( !divisionSymbolElem.isNull() )
902  {
903  QDomElement symbolElem = divisionSymbolElem.firstChildElement( QStringLiteral( "symbol" ) );
904  std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
905  if ( lineSymbol )
906  {
907  mSettings.setDivisionLineSymbol( lineSymbol.release() );
908  }
909  }
910  else if ( foundLineSymbol )
911  {
912  mSettings.setDivisionLineSymbol( mSettings.lineSymbol()->clone() );
913  }
914  QDomElement subdivisionSymbolElem = itemElem.firstChildElement( QStringLiteral( "subdivisionLineSymbol" ) );
915  if ( !subdivisionSymbolElem.isNull() )
916  {
917  QDomElement symbolElem = subdivisionSymbolElem.firstChildElement( QStringLiteral( "symbol" ) );
918  std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
919  if ( lineSymbol )
920  {
921  mSettings.setSubdivisionLineSymbol( lineSymbol.release() );
922  }
923  }
924  else if ( foundLineSymbol )
925  {
926  mSettings.setSubdivisionLineSymbol( mSettings.lineSymbol()->clone() );
927  }
928 
929  if ( !foundLineSymbol )
930  {
931  // old project compatibility
932  std::unique_ptr< QgsLineSymbol > lineSymbol = qgis::make_unique< QgsLineSymbol >();
933  std::unique_ptr< QgsSimpleLineSymbolLayer > lineSymbolLayer = qgis::make_unique< QgsSimpleLineSymbolLayer >();
934  lineSymbolLayer->setWidth( itemElem.attribute( QStringLiteral( "outlineWidth" ), QStringLiteral( "0.3" ) ).toDouble() );
935  lineSymbolLayer->setWidthUnit( QgsUnitTypes::RenderMillimeters );
936  lineSymbolLayer->setPenJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( itemElem.attribute( QStringLiteral( "lineJoinStyle" ), QStringLiteral( "miter" ) ) ) );
937  lineSymbolLayer->setPenCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( itemElem.attribute( QStringLiteral( "lineCapStyle" ), QStringLiteral( "square" ) ) ) );
938 
939  //stroke color
940  QDomNodeList strokeColorList = itemElem.elementsByTagName( QStringLiteral( "strokeColor" ) );
941  if ( !strokeColorList.isEmpty() )
942  {
943  QDomElement strokeColorElem = strokeColorList.at( 0 ).toElement();
944  bool redOk, greenOk, blueOk, alphaOk;
945  int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
946 
947  strokeRed = strokeColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
948  strokeGreen = strokeColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
949  strokeBlue = strokeColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
950  strokeAlpha = strokeColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
951 
952  if ( redOk && greenOk && blueOk && alphaOk )
953  {
954  lineSymbolLayer->setColor( QColor( strokeRed, strokeGreen, strokeBlue, strokeAlpha ) );
955  }
956  }
957  else
958  {
959  lineSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral( "penColor" ), QStringLiteral( "#000000" ) ) ) );
960  }
961 
962  // need to translate the deprecated ScalebarLineWidth and ScalebarLineColor properties to symbol properties,
963  // and then remove them from the scalebar so they don't interfere and apply to other compatibility workarounds
964  lineSymbolLayer->setDataDefinedProperty( QgsSymbolLayer::PropertyStrokeWidth, dataDefinedProperties().property( QgsLayoutObject::ScalebarLineWidth ) );
966  lineSymbolLayer->setDataDefinedProperty( QgsSymbolLayer::PropertyStrokeColor, dataDefinedProperties().property( QgsLayoutObject::ScalebarLineColor ) );
968 
969  lineSymbol->changeSymbolLayer( 0, lineSymbolLayer.release() );
970  mSettings.setLineSymbol( lineSymbol->clone() );
971  mSettings.setDivisionLineSymbol( lineSymbol->clone() );
972  mSettings.setSubdivisionLineSymbol( lineSymbol.release() );
973  }
974 
975  mSettings.setUnitLabel( itemElem.attribute( QStringLiteral( "unitLabel" ) ) );
976 
977  QDomNodeList textFormatNodeList = itemElem.elementsByTagName( QStringLiteral( "text-style" ) );
978  if ( !textFormatNodeList.isEmpty() )
979  {
980  QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
981  mSettings.textFormat().readXml( textFormatElem, context );
982  }
983  else
984  {
985  QFont f;
986  if ( !QgsFontUtils::setFromXmlChildNode( f, itemElem, QStringLiteral( "scaleBarFont" ) ) )
987  {
988  f.fromString( itemElem.attribute( QStringLiteral( "font" ), QString() ) );
989  }
990  mSettings.textFormat().setFont( f );
991  if ( f.pointSizeF() > 0 )
992  {
993  mSettings.textFormat().setSize( f.pointSizeF() );
995  }
996  else if ( f.pixelSize() > 0 )
997  {
998  mSettings.textFormat().setSize( f.pixelSize() );
1000  }
1001  }
1002 
1003  QDomNodeList numericFormatNodeList = itemElem.elementsByTagName( QStringLiteral( "numericFormat" ) );
1004  if ( !numericFormatNodeList.isEmpty() )
1005  {
1006  QDomElement numericFormatElem = numericFormatNodeList.at( 0 ).toElement();
1007  mSettings.setNumericFormat( QgsApplication::numericFormatRegistry()->createFromXml( numericFormatElem, context ) );
1008  }
1009 
1010  QDomElement fillSymbol1Elem = itemElem.firstChildElement( QStringLiteral( "fillSymbol1" ) );
1011  bool foundFillSymbol1 = false;
1012  if ( !fillSymbol1Elem.isNull() )
1013  {
1014  QDomElement symbolElem = fillSymbol1Elem.firstChildElement( QStringLiteral( "symbol" ) );
1015  std::unique_ptr< QgsFillSymbol > fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
1016  if ( fillSymbol )
1017  {
1018  mSettings.setFillSymbol( fillSymbol.release() );
1019  foundFillSymbol1 = true;
1020  }
1021  }
1022  if ( !foundFillSymbol1 )
1023  {
1024  // old project compatibility
1025  std::unique_ptr< QgsFillSymbol > fillSymbol = qgis::make_unique< QgsFillSymbol >();
1026  std::unique_ptr< QgsSimpleFillSymbolLayer > fillSymbolLayer = qgis::make_unique< QgsSimpleFillSymbolLayer >();
1027  fillSymbolLayer->setStrokeStyle( Qt::NoPen );
1028 
1029  //fill color
1030  QDomNodeList fillColorList = itemElem.elementsByTagName( QStringLiteral( "fillColor" ) );
1031  if ( !fillColorList.isEmpty() )
1032  {
1033  QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
1034  bool redOk, greenOk, blueOk, alphaOk;
1035  int fillRed, fillGreen, fillBlue, fillAlpha;
1036 
1037  fillRed = fillColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1038  fillGreen = fillColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1039  fillBlue = fillColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1040  fillAlpha = fillColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1041 
1042  if ( redOk && greenOk && blueOk && alphaOk )
1043  {
1044  fillSymbolLayer->setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
1045  }
1046  }
1047  else
1048  {
1049  fillSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral( "brushColor" ), QStringLiteral( "#000000" ) ) ) );
1050  }
1051 
1052  // need to translate the deprecated ScalebarFillColor property to symbol properties,
1053  // and then remove them from the scalebar so they don't interfere and apply to other compatibility workarounds
1054  fillSymbolLayer->setDataDefinedProperty( QgsSymbolLayer::PropertyFillColor, dataDefinedProperties().property( QgsLayoutObject::ScalebarFillColor ) );
1056 
1057  fillSymbol->changeSymbolLayer( 0, fillSymbolLayer.release() );
1058  mSettings.setFillSymbol( fillSymbol.release() );
1059  }
1060 
1061  QDomElement fillSymbol2Elem = itemElem.firstChildElement( QStringLiteral( "fillSymbol2" ) );
1062  bool foundFillSymbol2 = false;
1063  if ( !fillSymbol2Elem.isNull() )
1064  {
1065  QDomElement symbolElem = fillSymbol2Elem.firstChildElement( QStringLiteral( "symbol" ) );
1066  std::unique_ptr< QgsFillSymbol > fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
1067  if ( fillSymbol )
1068  {
1069  mSettings.setAlternateFillSymbol( fillSymbol.release() );
1070  foundFillSymbol2 = true;
1071  }
1072  }
1073  if ( !foundFillSymbol2 )
1074  {
1075  // old project compatibility
1076  std::unique_ptr< QgsFillSymbol > fillSymbol = qgis::make_unique< QgsFillSymbol >();
1077  std::unique_ptr< QgsSimpleFillSymbolLayer > fillSymbolLayer = qgis::make_unique< QgsSimpleFillSymbolLayer >();
1078  fillSymbolLayer->setStrokeStyle( Qt::NoPen );
1079 
1080  //fill color 2
1081 
1082  QDomNodeList fillColor2List = itemElem.elementsByTagName( QStringLiteral( "fillColor2" ) );
1083  if ( !fillColor2List.isEmpty() )
1084  {
1085  QDomElement fillColor2Elem = fillColor2List.at( 0 ).toElement();
1086  bool redOk, greenOk, blueOk, alphaOk;
1087  int fillRed, fillGreen, fillBlue, fillAlpha;
1088 
1089  fillRed = fillColor2Elem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1090  fillGreen = fillColor2Elem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1091  fillBlue = fillColor2Elem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1092  fillAlpha = fillColor2Elem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1093 
1094  if ( redOk && greenOk && blueOk && alphaOk )
1095  {
1096  fillSymbolLayer->setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
1097  }
1098  }
1099  else
1100  {
1101  fillSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral( "brush2Color" ), QStringLiteral( "#ffffff" ) ) ) );
1102  }
1103 
1104  // need to translate the deprecated ScalebarFillColor2 property to symbol properties,
1105  // and then remove them from the scalebar so they don't interfere and apply to other compatibility workarounds
1106  fillSymbolLayer->setDataDefinedProperty( QgsSymbolLayer::PropertyFillColor, dataDefinedProperties().property( QgsLayoutObject::ScalebarFillColor2 ) );
1108 
1109  fillSymbol->changeSymbolLayer( 0, fillSymbolLayer.release() );
1110  mSettings.setAlternateFillSymbol( fillSymbol.release() );
1111 
1112  }
1113 
1114  //font color
1115  QDomNodeList textColorList = itemElem.elementsByTagName( QStringLiteral( "textColor" ) );
1116  if ( !textColorList.isEmpty() )
1117  {
1118  QDomElement textColorElem = textColorList.at( 0 ).toElement();
1119  bool redOk, greenOk, blueOk, alphaOk;
1120  int textRed, textGreen, textBlue, textAlpha;
1121 
1122  textRed = textColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1123  textGreen = textColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1124  textBlue = textColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1125  textAlpha = textColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1126 
1127  if ( redOk && greenOk && blueOk && alphaOk )
1128  {
1129  mSettings.textFormat().setColor( QColor( textRed, textGreen, textBlue, textAlpha ) );
1130  }
1131  }
1132  else if ( itemElem.hasAttribute( QStringLiteral( "fontColor" ) ) )
1133  {
1134  QColor c;
1135  c.setNamedColor( itemElem.attribute( QStringLiteral( "fontColor" ), QStringLiteral( "#000000" ) ) );
1136  mSettings.textFormat().setColor( c );
1137  }
1138 
1139  //style
1140  setStyle( itemElem.attribute( QStringLiteral( "style" ), QString() ) );
1141 
1142  //call attemptResize after setStyle to ensure the appropriate size limitations are applied
1143  attemptResize( QgsLayoutSize::decodeSize( itemElem.attribute( QStringLiteral( "size" ) ) ) );
1144 
1145  if ( itemElem.attribute( QStringLiteral( "unitType" ) ).isEmpty() )
1146  {
1148  switch ( itemElem.attribute( QStringLiteral( "units" ) ).toInt() )
1149  {
1150  case 0:
1152  break;
1153  case 1:
1155  break;
1156  case 2:
1158  break;
1159  case 3:
1161  break;
1162  }
1163  mSettings.setUnits( u );
1164  }
1165  else
1166  {
1167  mSettings.setUnits( QgsUnitTypes::decodeDistanceUnit( itemElem.attribute( QStringLiteral( "unitType" ) ) ) );
1168  }
1169 
1170  mSettings.setLabelVerticalPlacement( static_cast< QgsScaleBarSettings::LabelVerticalPlacement >( itemElem.attribute( QStringLiteral( "labelVerticalPlacement" ), QStringLiteral( "0" ) ).toInt() ) );
1171  mSettings.setLabelHorizontalPlacement( static_cast< QgsScaleBarSettings::LabelHorizontalPlacement >( itemElem.attribute( QStringLiteral( "labelHorizontalPlacement" ), QStringLiteral( "0" ) ).toInt() ) );
1172 
1173  mSettings.setAlignment( static_cast< QgsScaleBarSettings::Alignment >( itemElem.attribute( QStringLiteral( "alignment" ), QStringLiteral( "0" ) ).toInt() ) );
1174 
1175  //map
1176  disconnectCurrentMap();
1177  mMap = nullptr;
1178  mMapUuid = itemElem.attribute( QStringLiteral( "mapUuid" ) );
1179  return true;
1180 }
1181 
1182 
1184 {
1185  if ( mLayout && !mMapUuid.isEmpty() )
1186  {
1187  disconnectCurrentMap();
1188  mMap = qobject_cast< QgsLayoutItemMap * >( mLayout->itemByUuid( mMapUuid, true ) );
1189  if ( mMap )
1190  {
1191  connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
1192  connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
1193  }
1194  }
1195 
1196  updateScale();
1197 }
1198 
1200 {
1201  QgsStyleTextFormatEntity entity( mSettings.textFormat() );
1202  if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
1203  return false;
1204 
1205  return true;
1206 }
1207 
1209 {
1211 }
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
double valueAsDouble(int key, const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a double.
static QgsScaleBarRendererRegistry * scaleBarRendererRegistry()
Gets the registry of available scalebar renderers.
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
This class represents a coordinate reference system (CRS).
Q_GADGET QgsUnitTypes::DistanceUnit mapUnits
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
QgsUnitTypes::DistanceUnit lengthUnits() const
Returns the units of distance for length calculations made by this object.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgssymbol.h:1307
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
Definition: qgssymbol.cpp:2422
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.
Layout graphical items for displaying a map.
void extentChanged()
Emitted when the map's extent changes.
double scale() const
Returns the map scale.
QgsRectangle extent() const
Returns the current map extent.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
@ LayoutScaleBar
Scale bar item.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:45
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
Definition: qgslayoutitem.h:72
A layout item subclass for scale bars.
void setNumberOfSegments(int segments)
Sets the number of segments included in the scalebar.
Q_DECL_DEPRECATED QBrush brush() const
Returns the primary brush for the scalebar.
Q_DECL_DEPRECATED QColor fillColor2() const
Returns the secondary color used for fills in the scalebar.
Q_DECL_DEPRECATED double lineWidth() const
Returns the line width in millimeters for lines in the scalebar.
QIcon icon() const override
Returns the item's icon.
Q_DECL_DEPRECATED void setFillColor(const QColor &color)
Sets the color used for fills in the scalebar.
void setLabelHorizontalPlacement(QgsScaleBarSettings::LabelHorizontalPlacement placement)
Sets the horizontal placement of text labels.
QgsLineSymbol * divisionLineSymbol() const
Returns the line symbol used to render the scalebar divisions (only used for some scalebar types).
QgsFillSymbol * alternateFillSymbol() const
Returns the secondary fill symbol used to render the scalebar (only used for some scalebar types).
Q_DECL_DEPRECATED QBrush brush2() const
Returns the secondary brush for the scalebar.
void setMinimumBarWidth(double minWidth)
Sets the minimum width (in millimeters) for scale bar segments.
QgsFillSymbol * fillSymbol() const
Returns the primary fill symbol used to render the scalebar (only used for some scalebar types).
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
Q_DECL_DEPRECATED QColor fontColor() const
Returns the color used for drawing text in the scalebar.
void setMaximumBarWidth(double maxWidth)
Sets the maximum width (in millimeters) for scale bar segments.
Q_DECL_DEPRECATED QColor fillColor() const
Returns the color used for fills in the scalebar.
void setSegmentSizeMode(QgsScaleBarSettings::SegmentSizeMode mode)
Sets the size mode for scale bar segments.
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the scalebar.
Q_DECL_DEPRECATED void setLineColor(const QColor &color)
Sets the color used for lines in the scalebar.
void setDivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar divisions (only used for some scalebar types).
Q_DECL_DEPRECATED QColor lineColor() const
Returns the color used for lines in the scalebar.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
void update()
Adjusts the scale bar box size and updates the item.
void setFillSymbol(QgsFillSymbol *symbol)
Sets the primary fill symbol used to render the scalebar (only used for some scalebar types).
void setLabelVerticalPlacement(QgsScaleBarSettings::LabelVerticalPlacement placement)
Sets the vertical placement of text labels.
double unitsPerSegment() const
Returns the number of scalebar units per segment.
void finalizeRestoreFromXml() override
Called after all pending items have been restored from XML.
bool applyDefaultRendererSettings(QgsScaleBarRenderer *renderer)
Applies any default settings relating to the specified renderer to the item.
void setUnits(QgsUnitTypes::DistanceUnit units)
Sets the distance units used by the scalebar.
QgsLineSymbol * subdivisionLineSymbol() const
Returns the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
QgsUnitTypes::DistanceUnit guessUnits() const
Attempts to guess the most reasonable unit choice for the scalebar, given the current linked map's sc...
Q_DECL_DEPRECATED QFont font() const
Returns the font used for drawing text in the scalebar.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
void setAlignment(QgsScaleBarSettings::Alignment alignment)
Sets the scalebar alignment.
void setAlternateFillSymbol(QgsFillSymbol *symbol)
Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types).
void applyDefaultSize(QgsUnitTypes::DistanceUnit units=QgsUnitTypes::DistanceMeters)
Applies the default size to the scale bar (scale bar 1/5 of map item width)
int type() const override
void setLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar (only used for some scalebar types).
void setStyle(const QString &name)
Sets the scale bar style by name.
Q_DECL_DEPRECATED void setFillColor2(const QColor &color)
Sets the secondary color used for fills in the scalebar.
void applyDefaultSettings()
Applies the default scalebar settings to the scale bar.
Q_DECL_DEPRECATED Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
void setNumberOfSegmentsLeft(int segments)
Sets the number of segments included in the left part of the scalebar.
Q_DECL_DEPRECATED Qt::PenJoinStyle lineJoinStyle() const
Returns the join style used for drawing lines in the scalebar.
void setTextFormat(const QgsTextFormat &format)
Sets the text format used for drawing text in the scalebar.
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map item linked to the scalebar.
QgsLayoutSize minimumSize() const override
Returns the minimum allowed size of the item, if applicable, or an empty size if item can be freely r...
Q_DECL_DEPRECATED void setLineJoinStyle(Qt::PenJoinStyle style)
Sets the join style used when drawing the lines in the scalebar.
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties) override
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
Q_DECL_DEPRECATED void setFontColor(const QColor &color)
Sets the color used for drawing text in the scalebar.
Q_DECL_DEPRECATED void setLineWidth(double width)
Sets the line width in millimeters for lines in the scalebar.
Q_DECL_DEPRECATED QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
void setBoxContentSpace(double space)
Sets the space (margin) between the scalebar box and content in millimeters.
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
void setUnitLabel(const QString &label)
Sets the label for units.
QString style() const
Returns the scale bar style name.
ExportLayerBehavior exportLayerBehavior() const override
Returns the behavior of this item during exporting to layered exports (e.g.
QgsTextFormat textFormat() const
Returns the text format used for drawing text in the scalebar.
Q_DECL_DEPRECATED void setLineCapStyle(Qt::PenCapStyle style)
Sets the cap style used when drawing the lines in the scalebar.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
void resizeToMinimumWidth()
Resizes the scale bar to its minimum width, without changing the height.
void setUnitsPerSegment(double units)
Sets the number of scalebar units per segment.
static QgsLayoutItemScaleBar * create(QgsLayout *layout)
Returns a new scale bar item for the specified layout.
Q_DECL_DEPRECATED void setFont(const QFont &font)
Sets the font used for drawing text in the scalebar.
void setSubdivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
QgsUnitTypes::DistanceUnit units() const
Returns the distance units used by the scalebar.
QgsLayoutItemScaleBar(QgsLayout *layout)
Constructor for QgsLayoutItemScaleBar, with the specified parent layout.
Base class for graphical items within a QgsLayout.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
void refreshItemSize()
Refreshes an item's size by rechecking it against any possible item fixed or minimum sizes.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual QString displayName() const
Gets item display name.
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
virtual QString uuid() const
Returns the item identification string.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
@ CanGroupWithItemsOfSameType
Item can only be placed on layers with other items of the same type, but multiple items of this type ...
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
QgsPropertyCollection mDataDefinedProperties
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
QPointer< QgsLayout > mLayout
DataDefinedProperty
Data defined properties for different item types.
@ ScalebarFillColor2
Scalebar secondary fill color (deprecated, use data defined properties on scalebar fill symbol 2 inst...
@ ScalebarLineWidth
Scalebar line width (deprecated, use data defined properties on scalebar line symbol instead)
@ AllProperties
All properties for item.
@ ScalebarFillColor
Scalebar fill color (deprecated, use data defined properties on scalebar fill symbol 1 instead)
@ ScalebarLineColor
Scalebar line color (deprecated, use data defined properties on scalebar line symbol instead)
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the object's property collection, used for data defined overrides.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:41
static QgsLayoutSize decodeSize(const QString &string)
Decodes a size from a string.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
void setWidth(const double width)
Sets the width for the size.
Definition: qgslayoutsize.h:83
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
static double calculatePrettySize(double minimumSize, double maximumSize)
Calculates a "pretty" size which falls between the range [minimumSize, maximumSize].
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
A line symbol type, for rendering LineString and MultiLineString geometries.
Definition: qgssymbol.h:1204
QgsLineSymbol * clone() const override
Returns a deep copy of this symbol.
Definition: qgssymbol.cpp:2295
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
void writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes the format to an XML element.
A class to represent a 2D point.
Definition: qgspointxy.h:44
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
A store for object properties.
Definition: qgsproperty.h:232
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
Definition: qgsrectangle.h:202
Contains information about the context of a rendering operation.
Abstract base class for scale bar renderers.
virtual bool applyDefaultSettings(QgsScaleBarSettings &settings) const
Applies any default settings relating to the scalebar to the passed settings object.
double subdivisionsHeight() const
Returns the scalebar subdivisions height (in millimeters) for segments included in the right part of ...
void setSubdivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
Q_DECL_DEPRECATED QColor fillColor() const
Returns the color used for fills in the scalebar.
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
void setAlignment(Alignment alignment)
Sets the scalebar alignment.
QgsLineSymbol * subdivisionLineSymbol() const
Returns the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
Q_DECL_DEPRECATED QColor fillColor2() const
Returns the secondary color used for fills in the scalebar.
void setAlternateFillSymbol(QgsFillSymbol *symbol)
Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types).
SegmentSizeMode segmentSizeMode() const
Returns the size mode for the scale bar segments.
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
Q_DECL_DEPRECATED void setFillColor(const QColor &color)
Sets the color used for fills in the scalebar.
int numberOfSegments() const
Returns the number of segments included in the scalebar.
Q_DECL_DEPRECATED void setFillColor2(const QColor &color)
Sets the secondary color used for fills in the scalebar.
double maximumBarWidth() const
Returns the maximum width (in millimeters) for scale bar segments.
Q_DECL_DEPRECATED void setLineCapStyle(Qt::PenCapStyle style)
Sets the cap style used when drawing the lines in the scalebar.
QgsUnitTypes::DistanceUnit units() const
Returns the distance units used by the scalebar.
void setFillSymbol(QgsFillSymbol *symbol)
Sets the primary fill symbol used to render the scalebar (only used for some scalebar types).
double unitsPerSegment() const
Returns the number of scalebar units per segment.
Q_DECL_DEPRECATED void setLineJoinStyle(Qt::PenJoinStyle style)
Sets the join style used when drawing the lines in the scalebar.
Q_DECL_DEPRECATED QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
void setLabelVerticalPlacement(LabelVerticalPlacement placement)
Sets the vertical placement of text labels.
Q_DECL_DEPRECATED void setLineColor(const QColor &color)
Sets the color used for lines in the scalebar.
void setUnitLabel(const QString &label)
Sets the label for units.
LabelHorizontalPlacement labelHorizontalPlacement() const
Returns the horizontal placement of text labels.
void setLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar (only used for some scalebar types).
void setDivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar divisions (only used for some scalebar types).
void setBoxContentSpace(double space)
Sets the space (margin) between the scalebar box and content in millimeters.
Q_DECL_DEPRECATED void setLineWidth(double width)
Sets the line width in millimeters for lines in the scalebar.
Alignment alignment() const
Returns the scalebar alignment.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters.
void setHeight(double height)
Sets the scalebar height (in millimeters).
QgsFillSymbol * alternateFillSymbol() const
Returns the secondary fill symbol used to render the scalebar (only used for some scalebar types).
void setUnits(QgsUnitTypes::DistanceUnit units)
Sets the distance units used by the scalebar.
QgsFillSymbol * fillSymbol() const
Returns the primary fill symbol used to render the scalebar (only used for some scalebar types).
Q_DECL_DEPRECATED double lineWidth() const
Returns the line width in millimeters for lines in the scalebar.
Q_DECL_DEPRECATED QBrush brush2() const
Returns the secondary brush for the scalebar.
Alignment
Scalebar alignment.
void setNumberOfSubdivisions(int subdivisions)
Sets the number of subdivisions for segments included in the right part of the scalebar (only used fo...
LabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the scalebar.
void setTextFormat(const QgsTextFormat &format)
Sets the text format used for drawing text in the scalebar.
double minimumBarWidth() const
Returns the minimum width (in millimeters) for scale bar segments.
QString unitLabel() const
Returns the label for units.
int numberOfSubdivisions() const
Returns the number of subdivisions for segments included in the right part of the scalebar (only used...
Q_DECL_DEPRECATED QColor lineColor() const
Returns the color used for lines in the scalebar.
LabelHorizontalPlacement
Label horizontal placement.
void setLabelBarSpace(double space)
Sets the spacing (in millimeters) between labels and the scalebar.
void setNumberOfSegments(int segments)
Sets the number of segments included in the scalebar.
void setUnitsPerSegment(double units)
Sets the number of scalebar units per segment.
LabelVerticalPlacement
Label vertical placement.
Q_DECL_DEPRECATED Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
void setLabelHorizontalPlacement(LabelHorizontalPlacement placement)
Sets the horizontal placement of text labels.
void setSubdivisionsHeight(double height)
Sets the scalebar subdivisions height (in millimeters) for segments included in the right part of the...
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
double height() const
Returns the scalebar height (in millimeters).
int numberOfSegmentsLeft() const
Returns the number of segments included in the left part of the scalebar.
Q_DECL_DEPRECATED void setFont(const QFont &font)
Sets the font used for drawing text in the scalebar.
SegmentSizeMode
Modes for setting size for scale bar segments.
@ SegmentSizeFitWidth
Scale bar segment size is calculated to fit a size range.
@ SegmentSizeFixed
Scale bar segment size is fixed to a map unit.
Q_DECL_DEPRECATED Qt::PenJoinStyle lineJoinStyle() const
Returns the join style used for drawing lines in the scalebar.
void setNumberOfSegmentsLeft(int segments)
Sets the number of segments included in the left part of the scalebar.
Q_DECL_DEPRECATED QBrush brush() const
Returns the primary brush used for filling the scalebar.
void setSegmentSizeMode(SegmentSizeMode mode)
Sets the size mode for scale bar segments.
QgsLineSymbol * divisionLineSymbol() const
Returns the line symbol used to render the scalebar divisions (only used for some scalebar types).
void setMapUnitsPerScaleBarUnit(double units)
Sets the number of map units per scale bar unit used by the scalebar.
double mapUnitsPerScaleBarUnit() const
Returns the number of map units per scale bar unit used by the scalebar.
void setMinimumBarWidth(double width)
Sets the minimum width (in millimeters) for scale bar segments.
void setMaximumBarWidth(double width)
Sets the maximum width (in millimeters) for scale bar segments.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A text format entity for QgsStyle databases.
Definition: qgsstyle.h:1264
static Qt::PenJoinStyle decodePenJoinStyle(const QString &str)
static Qt::PenCapStyle decodePenCapStyle(const QString &str)
static QString encodePenCapStyle(Qt::PenCapStyle style)
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
static QString encodePenJoinStyle(Qt::PenJoinStyle style)
@ PropertyStrokeWidth
Stroke width.
@ PropertyFillColor
Fill color.
@ PropertyStrokeColor
Stroke color.
bool changeSymbolLayer(int index, QgsSymbolLayer *layer)
Deletes the current layer at the specified index and replaces it with layer.
Definition: qgssymbol.cpp:464
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
void setFont(const QFont &font)
Sets the font used for rendering text.
void setOpacity(double opacity)
Sets the text's opacity.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the size of rendered text.
double opacity() const
Returns the text's opacity.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
QColor color() const
Returns the color that text will be rendered in.
QFont font() const
Returns the font used for rendering text.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:68
@ DistanceMeters
Meters.
Definition: qgsunittypes.h:69
@ DistanceDegrees
Degrees, for planar geographic CRS distance measurements.
Definition: qgsunittypes.h:75
@ DistanceKilometers
Kilometers.
Definition: qgsunittypes.h:70
@ DistanceMiles
Terrestrial miles.
Definition: qgsunittypes.h:74
@ DistanceUnknownUnit
Unknown distance unit.
Definition: qgsunittypes.h:78
@ DistanceFeet
Imperial feet.
Definition: qgsunittypes.h:71
@ DistanceNauticalMiles
Nautical miles.
Definition: qgsunittypes.h:72
@ LayoutMillimeters
Millimeters.
Definition: qgsunittypes.h:182
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE QgsUnitTypes::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=nullptr)
Decodes a distance unit from a string.
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:172
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:170
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:798
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:797
const QgsCoordinateReferenceSystem & crs
Contains parameters regarding scalebar calculations.
Flags flags
Scalebar renderer flags.
QSizeF size
Destination size for scalebar.
double segmentWidth
The width, in millimeters, of each individual segment drawn.
Contains information relating to the style entity currently being visited.