QGIS API Documentation 3.43.0-Master (b60ef06885e)
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
18#include "moc_qgslayoutitemscalebar.cpp"
21#include "qgslayoutitemmap.h"
22#include "qgslayout.h"
23#include "qgslayoututils.h"
24#include "qgsdistancearea.h"
26#include "qgsscalebarrenderer.h"
27#include "qgsmessagelog.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"
35#include "qgsnumericformat.h"
37#include "qgslinesymbollayer.h"
38#include "qgsfillsymbollayer.h"
39#include "qgslinesymbol.h"
40#include "qgsfillsymbol.h"
42
43#include <QDomDocument>
44#include <QDomElement>
45#include <QFontMetricsF>
46#include <QPainter>
47#include <QTextDocument>
48#include <QAbstractTextDocumentLayout>
49
50#include <cmath>
51
58
63
65{
66 return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemScaleBar.svg" ) );
67}
68
73
75{
77
78 const QgsScaleBarRenderer::ScaleBarContext scaleContext = createScaleContext();
79 if ( !scaleContext.isValid() )
80 return QgsLayoutSize();
81
82 return QgsLayoutSize( mStyle->calculateBoxSize( context, mSettings, scaleContext ), Qgis::LayoutUnit::Millimeters );
83}
84
86{
87 if ( !mStyle )
88 return;
89
90 const QgsScaleBarRenderer::ScaleBarContext scaleContext = createScaleContext();
91 if ( !scaleContext.isValid() )
92 {
93 if ( mLayout->renderContext().isPreviewRender() )
94 {
95 // No initial render available - so draw some preview text alerting user
96 QPainter *painter = context.renderContext().painter();
97
98 const double scale = context.renderContext().convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
99 const QRectF thisPaintRect = QRectF( 0, 0, rect().width() * scale, rect().height() * scale );
100
101 painter->setBrush( QBrush( QColor( 255, 125, 125, 125 ) ) );
102 painter->setPen( Qt::NoPen );
103 painter->drawRect( thisPaintRect );
104 painter->setBrush( Qt::NoBrush );
105
106 painter->setPen( QColor( 200, 0, 0, 255 ) );
107 QTextDocument td;
108 td.setTextWidth( thisPaintRect.width() );
109 td.setHtml( QStringLiteral( "<span style=\"color: rgb(200,0,0);\"><b>%1</b><br>%2</span>" ).arg(
110 tr( "Invalid scale!" ),
111 tr( "The scale bar cannot be rendered due to invalid settings or an incompatible linked map extent." ) ) );
112 painter->setClipRect( thisPaintRect );
113 QAbstractTextDocumentLayout::PaintContext ctx;
114 td.documentLayout()->draw( painter, ctx );
115 }
116 return;
117 }
118
120 {
121 // compatibility code - ScalebarLineColor and ScalebarLineWidth are deprecated
123 std::unique_ptr< QgsLineSymbol > sym( mSettings.lineSymbol()->clone() );
130 mSettings.setLineSymbol( sym.release() );
131 }
133 {
134 // compatibility code - ScalebarLineColor and ScalebarLineWidth are deprecated
136 std::unique_ptr< QgsFillSymbol > sym( mSettings.fillSymbol()->clone() );
140 mSettings.setFillSymbol( sym.release() );
141 }
143 {
144 // compatibility code - ScalebarLineColor and ScalebarLineWidth are deprecated
146 std::unique_ptr< QgsFillSymbol > sym( mSettings.alternateFillSymbol()->clone() );
150 mSettings.setAlternateFillSymbol( sym.release() );
151 }
152
153 mStyle->draw( context.renderContext(), mSettings, scaleContext );
154}
155
157{
158 if ( !mStyle )
159 {
160 mSettings.setNumberOfSegments( nSegments );
161 return;
162 }
163 mSettings.setNumberOfSegments( nSegments );
165}
166
168{
169 if ( !mStyle )
170 {
171 mSettings.setUnitsPerSegment( units );
172 return;
173 }
174 mSettings.setUnitsPerSegment( units );
175 refreshSegmentMillimeters();
177}
178
179
181{
182 if ( !mStyle )
183 {
184 mSettings.setSegmentSizeMode( mode );
185 return;
186 }
187 mSettings.setSegmentSizeMode( mode );
188 refreshSegmentMillimeters();
190}
191
193{
194 if ( !mStyle )
195 {
196 mSettings.setMinimumBarWidth( minWidth );
197 return;
198 }
199 mSettings.setMinimumBarWidth( minWidth );
200 refreshSegmentMillimeters();
202}
203
205{
206 if ( !mStyle )
207 {
208 mSettings.setMaximumBarWidth( maxWidth );
209 return;
210 }
211 mSettings.setMaximumBarWidth( maxWidth );
212 refreshSegmentMillimeters();
214}
215
217{
218 return mSettings.textFormat();
219}
220
222{
223 mSettings.setTextFormat( format );
225 emit changed();
226}
227
229{
230 return mSettings.lineSymbol();
231}
232
234{
235 mSettings.setLineSymbol( symbol );
236}
237
242
247
252
257
259{
260 return mSettings.fillSymbol();
261}
262
264{
265 mSettings.setFillSymbol( symbol );
266}
267
272
277
279{
280 if ( !mStyle )
281 {
282 mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
283 return;
284 }
285 mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
287}
288
290{
291 if ( !mStyle )
292 {
293 mSettings.setBoxContentSpace( space );
294 return;
295 }
296 mSettings.setBoxContentSpace( space );
298}
299
301{
302 disconnectCurrentMap();
303
304 mMap = map;
305
306 if ( !map )
307 {
308 return;
309 }
310
311 connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
312 connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
313
314 refreshSegmentMillimeters();
315 emit changed();
316}
317
318void QgsLayoutItemScaleBar::disconnectCurrentMap()
319{
320 if ( !mMap )
321 {
322 return;
323 }
324
325 disconnect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
326 disconnect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
327 mMap = nullptr;
328}
329
334
336{
337 if ( mMethod == method )
338 return;
339
340 mMethod = method;
341 refreshSegmentMillimeters();
343}
344
345void QgsLayoutItemScaleBar::refreshUnitsPerSegment( const QgsExpressionContext *context )
346{
348 {
349 double unitsPerSegment = mSettings.unitsPerSegment();
350 bool ok = false;
352
353 if ( !ok )
354 {
355 QgsMessageLog::logMessage( tr( "Scalebar units per segment expression eval error" ) );
356 }
357 else
358 {
360 }
361 }
362}
363
364void QgsLayoutItemScaleBar::refreshMinimumBarWidth( const QgsExpressionContext *context )
365{
367 {
368 double minimumBarWidth = mSettings.minimumBarWidth();
369
370 bool ok = false;
372
373 if ( !ok )
374 {
375 QgsMessageLog::logMessage( tr( "Scalebar minimum segment width expression eval error" ) );
376 }
377 else
378 {
380 }
381 }
382}
383
384void QgsLayoutItemScaleBar::refreshMaximumBarWidth( const QgsExpressionContext *context )
385{
387 {
388 double maximumBarWidth = mSettings.maximumBarWidth();
389
390 bool ok = false;
392
393 if ( !ok )
394 {
395 QgsMessageLog::logMessage( tr( "Scalebar maximum segment width expression eval error" ) );
396 }
397 else
398 {
400 }
401 }
402}
403
404void QgsLayoutItemScaleBar::refreshNumberOfSegmentsLeft( const QgsExpressionContext *context )
405{
407 {
408 int leftSegments = mSettings.numberOfSegmentsLeft();
409
410 bool ok = false;
412
413 if ( !ok )
414 {
415 QgsMessageLog::logMessage( tr( "Scalebar left segment count expression eval error" ) );
416 }
417 else
418 {
419 setNumberOfSegmentsLeft( leftSegments );
420 }
421 }
422}
423
424void QgsLayoutItemScaleBar::refreshNumberOfSegmentsRight( const QgsExpressionContext *context )
425{
427 {
428 int rightSegments = mSettings.numberOfSegments();
429
430 bool ok = false;
432
433 if ( !ok )
434 {
435 QgsMessageLog::logMessage( tr( "Scalebar left segment count expression eval error" ) );
436 }
437 else
438 {
439 setNumberOfSegments( rightSegments );
440 }
441 }
442}
443
445{
447
448 bool forceUpdate = false;
449
452 {
453 double height = mSettings.height();
454
455 bool ok = false;
457
458 if ( !ok )
459 {
460 QgsMessageLog::logMessage( tr( "Scalebar height expression eval error" ) );
461 }
462 else
463 {
464 setHeight( height );
465 }
466
467 forceUpdate = true;
468 }
469
472 {
473 double height = mSettings.subdivisionsHeight();
474
475 bool ok = false;
477
478 if ( !ok )
479 {
480 QgsMessageLog::logMessage( tr( "Scalebar subdivision height expression eval error" ) );
481 }
482 else
483 {
485 }
486
487 forceUpdate = true;
488 }
489
491 {
492 refreshNumberOfSegmentsLeft( &context );
493 forceUpdate = true;
494 }
495
497 {
498 refreshNumberOfSegmentsRight( &context );
499 forceUpdate = true;
500 }
501
504 {
505 int segments = mSettings.numberOfSubdivisions();
506
507 bool ok = false;
509
510 if ( !ok )
511 {
512 QgsMessageLog::logMessage( tr( "Scalebar number of subdivisions expression eval error" ) );
513 }
514 else
515 {
516 setNumberOfSubdivisions( segments );
517 }
518
519 forceUpdate = true;
520 }
521
522
524 {
525 refreshUnitsPerSegment( &context );
526 forceUpdate = true;
527 }
528
530 {
531 refreshMinimumBarWidth( &context );
532 forceUpdate = true;
533 }
534
536 {
537 refreshMaximumBarWidth( &context );
538 forceUpdate = true;
539 }
540
541 // updates data defined properties and redraws item to match
542 // -- Deprecated --
544 {
545 forceUpdate = true;
546 }
548 {
549 forceUpdate = true;
550 }
552 {
553 forceUpdate = true;
554 }
556 {
557 forceUpdate = true;
558 }
559
560 if ( forceUpdate )
561 {
563 update();
564 }
565
567}
568
569void QgsLayoutItemScaleBar::refreshSegmentMillimeters()
570{
571 if ( mMap )
572 {
573 //get mm dimension of composer map
574 const QRectF composerItemRect = mMap->rect();
575
576 const double currentMapWidth = mapWidth();
577 if ( qgsDoubleNear( currentMapWidth, 0 ) || std::isnan( currentMapWidth ) )
578 {
579 mSegmentMillimeters = std::numeric_limits< double >::quiet_NaN();
580 return;
581 }
582
583 switch ( mSettings.segmentSizeMode() )
584 {
586 {
587 //calculate size depending on mNumUnitsPerSegment
588 mSegmentMillimeters = composerItemRect.width() / currentMapWidth * mSettings.unitsPerSegment();
589 break;
590 }
591
593 {
594 if ( mSettings.maximumBarWidth() < mSettings.minimumBarWidth() )
595 {
596 mSegmentMillimeters = 0;
597 }
598 else
599 {
600 const double nSegments = ( mSettings.numberOfSegmentsLeft() != 0 ) + mSettings.numberOfSegments();
601 // unitsPerSegments which fit minBarWidth resp. maxBarWidth
602 const double minUnitsPerSeg = ( mSettings.minimumBarWidth() * currentMapWidth ) / ( nSegments * composerItemRect.width() );
603 const double maxUnitsPerSeg = ( mSettings.maximumBarWidth() * currentMapWidth ) / ( nSegments * composerItemRect.width() );
604 mSettings.setUnitsPerSegment( QgsLayoutUtils::calculatePrettySize( minUnitsPerSeg, maxUnitsPerSeg ) );
605 mSegmentMillimeters = composerItemRect.width() / currentMapWidth * mSettings.unitsPerSegment();
606 }
607 break;
608 }
609 }
610 }
611}
612
613double QgsLayoutItemScaleBar::mapWidth() const
614{
615 if ( !mMap )
616 {
617 return 0.0;
618 }
619
620 const QgsRectangle mapExtent = mMap->extent();
621 if ( mSettings.units() == Qgis::DistanceUnit::Unknown )
622 {
623 return mapExtent.width();
624 }
625 else
626 {
628 da.setSourceCrs( mMap->crs(), mLayout->project()->transformContext() );
629 da.setEllipsoid( mLayout->project()->ellipsoid() );
630
632
633 QList< double > yValues;
634 switch ( mMethod )
635 {
637 yValues << mapExtent.yMaximum();
638 break;
639
641 yValues << 0.5 * ( mapExtent.yMaximum() + mapExtent.yMinimum() );
642 break;
643
644
646 yValues << mapExtent.yMinimum();
647 break;
648
650 yValues << mapExtent.yMaximum();
651 yValues << 0.5 * ( mapExtent.yMaximum() + mapExtent.yMinimum() );
652 yValues << mapExtent.yMinimum();
653 break;
654
656 if ( mMap->crs().mapUnits() == Qgis::DistanceUnit::Degrees )
657 {
658 yValues << 0;
659 }
660 else
661 {
662 // this method is only for degree based systems, so just fallback to default if not degree based
663 yValues << 0.5 * ( mapExtent.yMaximum() + mapExtent.yMinimum() );
664 }
665 break;
666 }
667
668 double sumValidMeasures = 0;
669 int validMeasureCount = 0;
670
671 for ( const double y : std::as_const( yValues ) )
672 {
673 try
674 {
675 double measure = da.measureLine( QgsPointXY( mapExtent.xMinimum(), y ),
676 QgsPointXY( mapExtent.xMaximum(), y ) );
677 if ( std::isnan( measure ) )
678 {
679 // TODO report errors to user
680 QgsDebugError( QStringLiteral( "An error occurred while calculating length" ) );
681 continue;
682 }
683
684 measure /= QgsUnitTypes::fromUnitToUnitFactor( mSettings.units(), units );
685 sumValidMeasures += measure;
686 validMeasureCount++;
687 }
688 catch ( QgsCsException & )
689 {
690 // TODO report errors to user
691 QgsDebugError( QStringLiteral( "An error occurred while calculating length" ) );
692 continue;
693 }
694 }
695
696 if ( validMeasureCount == 0 )
697 return std::numeric_limits< double >::quiet_NaN();
698
699 return sumValidMeasures / validMeasureCount;
700 }
701}
702
703QgsScaleBarRenderer::ScaleBarContext QgsLayoutItemScaleBar::createScaleContext() const
704{
706 scaleContext.size = rect().size();
707 scaleContext.segmentWidth = mSegmentMillimeters;
708 scaleContext.scale = mMap ? mMap->scale() : 1.0;
709 scaleContext.flags = mStyle->flags();
710 return scaleContext;
711}
712
719
726
728{
729 mSettings.setAlignment( a );
731 emit changed();
732}
733
735{
736 mSettings.setUnits( u );
737 refreshSegmentMillimeters();
739 emit changed();
740}
741
743{
745 return mSettings.lineJoinStyle();
747}
748
749void QgsLayoutItemScaleBar::setLineJoinStyle( Qt::PenJoinStyle style )
750{
752 if ( mSettings.lineJoinStyle() == style )
753 {
754 //no change
755 return;
756 }
757 mSettings.setLineJoinStyle( style );
759 update();
760 emit changed();
761}
762
764{
766 return mSettings.lineCapStyle();
768}
769
770void QgsLayoutItemScaleBar::setLineCapStyle( Qt::PenCapStyle style )
771{
773 if ( mSettings.lineCapStyle() == style )
774 {
775 //no change
776 return;
777 }
778 mSettings.setLineCapStyle( style );
780 update();
781 emit changed();
782}
783
785{
786 //style
787 mStyle = std::make_unique< QgsSingleBoxScaleBarRenderer >();
788
789 //default to no background
790 setBackgroundEnabled( false );
791
792 //get default composer font from settings
793 const QgsSettings settings;
794 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
795 QgsTextFormat format;
796 QFont f;
797 if ( !defaultFontString.isEmpty() )
798 {
799 QgsFontUtils::setFontFamily( f, defaultFontString );
800 }
801 format.setFont( f );
802 format.setSize( 12.0 );
804
805 mSettings.setTextFormat( format );
806
809
810 emit changed();
811}
812
814{
815 return renderer->applyDefaultSettings( mSettings );
816}
817
819{
820 if ( !mMap )
822
823 const QgsCoordinateReferenceSystem crs = mMap->crs();
824 // start with crs units
827 {
828 // geographic CRS, use metric units
830 }
831
832 // try to pick reasonable choice between metric / imperial units
833 const double widthInSelectedUnits = mapWidth();
834 if ( std::isnan( widthInSelectedUnits ) )
835 return unit;
836
837 const double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
838 switch ( unit )
839 {
841 {
842 if ( initialUnitsPerSegment > 1000.0 )
843 {
845 }
846 break;
847 }
849 {
850 if ( initialUnitsPerSegment > 5419.95 )
851 {
853 }
854 break;
855 }
856 default:
857 break;
858 }
859
860 return unit;
861}
862
864{
865 mSettings.setUnits( units );
866 if ( mMap )
867 {
868 double upperMagnitudeMultiplier = 1.0;
869 const double widthInSelectedUnits = mapWidth();
870 if ( !std::isnan( widthInSelectedUnits ) )
871 {
872 const double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
873 mSettings.setUnitsPerSegment( initialUnitsPerSegment );
874
876 upperMagnitudeMultiplier = 1;
877
878 const double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
879 const int segmentMagnitude = std::floor( std::log10( segmentWidth ) );
880 double unitsPerSegment = upperMagnitudeMultiplier * ( std::pow( 10.0, segmentMagnitude ) );
881 const double multiplier = std::floor( ( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
882
883 if ( multiplier > 0 )
884 {
885 unitsPerSegment = unitsPerSegment * multiplier;
886 }
888 mSettings.setMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );
889
890 mSettings.setNumberOfSegments( 2 );
891 mSettings.setNumberOfSegmentsLeft( 0 );
892 }
893 }
894
895 refreshSegmentMillimeters();
897 emit changed();
898}
899
901{
902 if ( !mStyle )
903 return;
904
906
907 const QgsScaleBarRenderer::ScaleBarContext scaleContext = createScaleContext();
908 if ( !scaleContext.isValid() )
909 return;
910
911 const double widthMM = mStyle->calculateBoxSize( context, mSettings, scaleContext ).width();
912 QgsLayoutSize currentSize = sizeWithUnits();
913 currentSize.setWidth( mLayout->renderContext().measurementConverter().convert( QgsLayoutMeasurement( widthMM, Qgis::LayoutUnit::Millimeters ), currentSize.units() ).length() );
914 attemptResize( currentSize );
915 update();
916 emit changed();
917}
918
920{
921 //Don't adjust box size for numeric scale bars:
922 if ( mStyle && mStyle->id() != QLatin1String( "Numeric" ) )
923 {
925 }
926 QgsLayoutItem::update();
927}
928
929void QgsLayoutItemScaleBar::updateScale()
930{
931 refreshSegmentMillimeters();
932 //Don't adjust box size for numeric scale bars:
933 if ( mStyle && mStyle->id() != QLatin1String( "Numeric" ) )
934 {
936 }
937 update();
938}
939
940void QgsLayoutItemScaleBar::setStyle( const QString &styleName )
941{
942 //switch depending on style name
943 std::unique_ptr< QgsScaleBarRenderer> renderer( QgsApplication::scaleBarRendererRegistry()->renderer( styleName ) );
944 if ( renderer )
945 {
946 mStyle = std::move( renderer );
947 }
949 emit changed();
950}
951
953{
954 if ( mStyle )
955 {
956 return mStyle->id();
957 }
958 else
959 {
960 return QString();
961 }
962}
963
965{
966 return mSettings.numericFormat();
967}
968
970{
971 mSettings.setNumericFormat( format );
972}
973
975{
976 return mSettings.textFormat().font();
977}
978
979void QgsLayoutItemScaleBar::setFont( const QFont &font )
980{
982 mSettings.setFont( font );
985 emit changed();
986}
987
989{
990 QColor color = mSettings.textFormat().color();
991 color.setAlphaF( mSettings.textFormat().opacity() );
992 return color;
993}
994
995void QgsLayoutItemScaleBar::setFontColor( const QColor &color )
996{
997 mSettings.textFormat().setColor( color );
998 mSettings.textFormat().setOpacity( color.alphaF() );
999}
1000
1002{
1004 return mSettings.fillColor();
1006}
1007
1008void QgsLayoutItemScaleBar::setFillColor( const QColor &color )
1009{
1011 mSettings.setFillColor( color );
1013}
1014
1016{
1018 return mSettings.fillColor2();
1020}
1021
1022void QgsLayoutItemScaleBar::setFillColor2( const QColor &color )
1023{
1025 mSettings.setFillColor2( color );
1027}
1028
1030{
1032 return mSettings.lineColor();
1034}
1035
1036void QgsLayoutItemScaleBar::setLineColor( const QColor &color )
1037{
1039 mSettings.setLineColor( color );
1041}
1042
1044{
1046 return mSettings.lineWidth();
1048}
1049
1051{
1053 mSettings.setLineWidth( width );
1055}
1056
1058{
1060 return mSettings.pen();
1062}
1063
1065{
1067 return mSettings.brush();
1069}
1070
1072{
1074 return mSettings.brush2();
1076}
1077
1078bool QgsLayoutItemScaleBar::writePropertiesToElement( QDomElement &composerScaleBarElem, QDomDocument &doc, const QgsReadWriteContext &rwContext ) const
1079{
1080 composerScaleBarElem.setAttribute( QStringLiteral( "height" ), QString::number( mSettings.height() ) );
1081 composerScaleBarElem.setAttribute( QStringLiteral( "labelBarSpace" ), QString::number( mSettings.labelBarSpace() ) );
1082 composerScaleBarElem.setAttribute( QStringLiteral( "boxContentSpace" ), QString::number( mSettings.boxContentSpace() ) );
1083 composerScaleBarElem.setAttribute( QStringLiteral( "numSegments" ), mSettings.numberOfSegments() );
1084 composerScaleBarElem.setAttribute( QStringLiteral( "numSegmentsLeft" ), mSettings.numberOfSegmentsLeft() );
1085 composerScaleBarElem.setAttribute( QStringLiteral( "numSubdivisions" ), mSettings.numberOfSubdivisions() );
1086 composerScaleBarElem.setAttribute( QStringLiteral( "subdivisionsHeight" ), mSettings.subdivisionsHeight() );
1087 composerScaleBarElem.setAttribute( QStringLiteral( "numUnitsPerSegment" ), QString::number( mSettings.unitsPerSegment() ) );
1088 composerScaleBarElem.setAttribute( QStringLiteral( "segmentSizeMode" ), static_cast< int >( mSettings.segmentSizeMode() ) );
1089 composerScaleBarElem.setAttribute( QStringLiteral( "minBarWidth" ), mSettings.minimumBarWidth() );
1090 composerScaleBarElem.setAttribute( QStringLiteral( "maxBarWidth" ), mSettings.maximumBarWidth() );
1091 composerScaleBarElem.setAttribute( QStringLiteral( "segmentMillimeters" ), QString::number( mSegmentMillimeters ) );
1092 composerScaleBarElem.setAttribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QString::number( mSettings.mapUnitsPerScaleBarUnit() ) );
1093 composerScaleBarElem.setAttribute( QStringLiteral( "method" ), qgsEnumValueToKey( mMethod ) );
1094
1095 const QDomElement textElem = mSettings.textFormat().writeXml( doc, rwContext );
1096 composerScaleBarElem.appendChild( textElem );
1097
1099 // kept just for allowing projects to open in QGIS < 3.14, remove for 4.0
1100 composerScaleBarElem.setAttribute( QStringLiteral( "outlineWidth" ), QString::number( mSettings.lineWidth() ) );
1101 composerScaleBarElem.setAttribute( QStringLiteral( "lineJoinStyle" ), QgsSymbolLayerUtils::encodePenJoinStyle( mSettings.lineJoinStyle() ) );
1102 composerScaleBarElem.setAttribute( QStringLiteral( "lineCapStyle" ), QgsSymbolLayerUtils::encodePenCapStyle( mSettings.lineCapStyle() ) );
1103 //pen color
1104 QDomElement strokeColorElem = doc.createElement( QStringLiteral( "strokeColor" ) );
1105 strokeColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.lineColor().red() ) );
1106 strokeColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.lineColor().green() ) );
1107 strokeColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.lineColor().blue() ) );
1108 strokeColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.lineColor().alpha() ) );
1109 composerScaleBarElem.appendChild( strokeColorElem );
1111
1112 composerScaleBarElem.setAttribute( QStringLiteral( "unitLabel" ), mSettings.unitLabel() );
1113 composerScaleBarElem.setAttribute( QStringLiteral( "unitType" ), QgsUnitTypes::encodeUnit( mSettings.units() ) );
1114
1115 QDomElement numericFormatElem = doc.createElement( QStringLiteral( "numericFormat" ) );
1116 mSettings.numericFormat()->writeXml( numericFormatElem, doc, rwContext );
1117 composerScaleBarElem.appendChild( numericFormatElem );
1118
1119 //style
1120 if ( mStyle )
1121 {
1122 composerScaleBarElem.setAttribute( QStringLiteral( "style" ), mStyle->id() );
1123 }
1124
1125 //map id
1126 if ( mMap )
1127 {
1128 composerScaleBarElem.setAttribute( QStringLiteral( "mapUuid" ), mMap->uuid() );
1129 }
1130
1131 //colors
1132
1134 // kept just for allowing projects to open in QGIS < 3.14, remove for 4.0
1135
1136 //fill color
1137 QDomElement fillColorElem = doc.createElement( QStringLiteral( "fillColor" ) );
1138 fillColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor().red() ) );
1139 fillColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor().green() ) );
1140 fillColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor().blue() ) );
1141 fillColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor().alpha() ) );
1142 composerScaleBarElem.appendChild( fillColorElem );
1143
1144 //fill color 2
1145 QDomElement fillColor2Elem = doc.createElement( QStringLiteral( "fillColor2" ) );
1146 fillColor2Elem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor2().red() ) );
1147 fillColor2Elem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor2().green() ) );
1148 fillColor2Elem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor2().blue() ) );
1149 fillColor2Elem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor2().alpha() ) );
1150 composerScaleBarElem.appendChild( fillColor2Elem );
1151
1153
1154 //label vertical/horizontal placement
1155 composerScaleBarElem.setAttribute( QStringLiteral( "labelVerticalPlacement" ), QString::number( static_cast< int >( mSettings.labelVerticalPlacement() ) ) );
1156 composerScaleBarElem.setAttribute( QStringLiteral( "labelHorizontalPlacement" ), QString::number( static_cast< int >( mSettings.labelHorizontalPlacement() ) ) );
1157
1158 //alignment
1159 composerScaleBarElem.setAttribute( QStringLiteral( "alignment" ), QString::number( static_cast< int >( mSettings.alignment() ) ) );
1160
1161 QDomElement lineSymbol = doc.createElement( QStringLiteral( "lineSymbol" ) );
1162 const QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol( QString(),
1163 mSettings.lineSymbol(),
1164 doc,
1165 rwContext );
1166 lineSymbol.appendChild( symbolElem );
1167 composerScaleBarElem.appendChild( lineSymbol );
1168
1169 QDomElement divisionSymbol = doc.createElement( QStringLiteral( "divisionLineSymbol" ) );
1170 const QDomElement divisionSymbolElem = QgsSymbolLayerUtils::saveSymbol( QString(),
1171 mSettings.divisionLineSymbol(),
1172 doc,
1173 rwContext );
1174 divisionSymbol.appendChild( divisionSymbolElem );
1175 composerScaleBarElem.appendChild( divisionSymbol );
1176
1177 QDomElement subdivisionSymbol = doc.createElement( QStringLiteral( "subdivisionLineSymbol" ) );
1178 const QDomElement subdivisionSymbolElem = QgsSymbolLayerUtils::saveSymbol( QString(),
1179 mSettings.subdivisionLineSymbol(),
1180 doc,
1181 rwContext );
1182 subdivisionSymbol.appendChild( subdivisionSymbolElem );
1183 composerScaleBarElem.appendChild( subdivisionSymbol );
1184
1185 QDomElement fillSymbol1Elem = doc.createElement( QStringLiteral( "fillSymbol1" ) );
1186 const QDomElement symbol1Elem = QgsSymbolLayerUtils::saveSymbol( QString(),
1187 mSettings.fillSymbol(),
1188 doc,
1189 rwContext );
1190 fillSymbol1Elem.appendChild( symbol1Elem );
1191 composerScaleBarElem.appendChild( fillSymbol1Elem );
1192
1193 QDomElement fillSymbol2Elem = doc.createElement( QStringLiteral( "fillSymbol2" ) );
1194 const QDomElement symbol2Elem = QgsSymbolLayerUtils::saveSymbol( QString(),
1195 mSettings.alternateFillSymbol(),
1196 doc,
1197 rwContext );
1198 fillSymbol2Elem.appendChild( symbol2Elem );
1199 composerScaleBarElem.appendChild( fillSymbol2Elem );
1200
1201 return true;
1202}
1203
1204
1205bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext &context )
1206{
1207 mSettings.setHeight( itemElem.attribute( QStringLiteral( "height" ), QStringLiteral( "5.0" ) ).toDouble() );
1208 mSettings.setLabelBarSpace( itemElem.attribute( QStringLiteral( "labelBarSpace" ), QStringLiteral( "3.0" ) ).toDouble() );
1209 mSettings.setBoxContentSpace( itemElem.attribute( QStringLiteral( "boxContentSpace" ), QStringLiteral( "1.0" ) ).toDouble() );
1210 mSettings.setNumberOfSegments( itemElem.attribute( QStringLiteral( "numSegments" ), QStringLiteral( "2" ) ).toInt() );
1211 mSettings.setNumberOfSegmentsLeft( itemElem.attribute( QStringLiteral( "numSegmentsLeft" ), QStringLiteral( "0" ) ).toInt() );
1212 mSettings.setNumberOfSubdivisions( itemElem.attribute( QStringLiteral( "numSubdivisions" ), QStringLiteral( "1" ) ).toInt() );
1213 mSettings.setSubdivisionsHeight( itemElem.attribute( QStringLiteral( "subdivisionsHeight" ), QStringLiteral( "1.5" ) ).toDouble() );
1214 mSettings.setUnitsPerSegment( itemElem.attribute( QStringLiteral( "numUnitsPerSegment" ), QStringLiteral( "1.0" ) ).toDouble() );
1215 mSettings.setSegmentSizeMode( static_cast<Qgis::ScaleBarSegmentSizeMode >( itemElem.attribute( QStringLiteral( "segmentSizeMode" ), QStringLiteral( "0" ) ).toInt() ) );
1216 mSettings.setMinimumBarWidth( itemElem.attribute( QStringLiteral( "minBarWidth" ), QStringLiteral( "50" ) ).toDouble() );
1217 mSettings.setMaximumBarWidth( itemElem.attribute( QStringLiteral( "maxBarWidth" ), QStringLiteral( "150" ) ).toDouble() );
1218 mSegmentMillimeters = itemElem.attribute( QStringLiteral( "segmentMillimeters" ), QStringLiteral( "0.0" ) ).toDouble();
1219 mSettings.setMapUnitsPerScaleBarUnit( itemElem.attribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QStringLiteral( "1.0" ) ).toDouble() );
1220
1221 // default to horizontal bottom to keep same behavior for older projects
1222 mMethod = qgsEnumKeyToValue( itemElem.attribute( QStringLiteral( "method" ) ), Qgis::ScaleCalculationMethod::HorizontalBottom );
1223
1224 const QDomElement lineSymbolElem = itemElem.firstChildElement( QStringLiteral( "lineSymbol" ) );
1225 bool foundLineSymbol = false;
1226 if ( !lineSymbolElem.isNull() )
1227 {
1228 const QDomElement symbolElem = lineSymbolElem.firstChildElement( QStringLiteral( "symbol" ) );
1229 std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
1230 if ( lineSymbol )
1231 {
1232 mSettings.setLineSymbol( lineSymbol.release() );
1233 foundLineSymbol = true;
1234 }
1235 }
1236 const QDomElement divisionSymbolElem = itemElem.firstChildElement( QStringLiteral( "divisionLineSymbol" ) );
1237 if ( !divisionSymbolElem.isNull() )
1238 {
1239 const QDomElement symbolElem = divisionSymbolElem.firstChildElement( QStringLiteral( "symbol" ) );
1240 std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
1241 if ( lineSymbol )
1242 {
1243 mSettings.setDivisionLineSymbol( lineSymbol.release() );
1244 }
1245 }
1246 else if ( foundLineSymbol )
1247 {
1248 mSettings.setDivisionLineSymbol( mSettings.lineSymbol()->clone() );
1249 }
1250 const QDomElement subdivisionSymbolElem = itemElem.firstChildElement( QStringLiteral( "subdivisionLineSymbol" ) );
1251 if ( !subdivisionSymbolElem.isNull() )
1252 {
1253 const QDomElement symbolElem = subdivisionSymbolElem.firstChildElement( QStringLiteral( "symbol" ) );
1254 std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
1255 if ( lineSymbol )
1256 {
1257 mSettings.setSubdivisionLineSymbol( lineSymbol.release() );
1258 }
1259 }
1260 else if ( foundLineSymbol )
1261 {
1262 mSettings.setSubdivisionLineSymbol( mSettings.lineSymbol()->clone() );
1263 }
1264
1265 if ( !foundLineSymbol )
1266 {
1267 // old project compatibility
1268 auto lineSymbol = std::make_unique< QgsLineSymbol >();
1269 auto lineSymbolLayer = std::make_unique< QgsSimpleLineSymbolLayer >();
1270 lineSymbolLayer->setWidth( itemElem.attribute( QStringLiteral( "outlineWidth" ), QStringLiteral( "0.3" ) ).toDouble() );
1271 lineSymbolLayer->setWidthUnit( Qgis::RenderUnit::Millimeters );
1272 lineSymbolLayer->setPenJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( itemElem.attribute( QStringLiteral( "lineJoinStyle" ), QStringLiteral( "miter" ) ) ) );
1273 lineSymbolLayer->setPenCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( itemElem.attribute( QStringLiteral( "lineCapStyle" ), QStringLiteral( "square" ) ) ) );
1274
1275 //stroke color
1276 const QDomNodeList strokeColorList = itemElem.elementsByTagName( QStringLiteral( "strokeColor" ) );
1277 if ( !strokeColorList.isEmpty() )
1278 {
1279 const QDomElement strokeColorElem = strokeColorList.at( 0 ).toElement();
1280 bool redOk, greenOk, blueOk, alphaOk;
1281 int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
1282
1283 strokeRed = strokeColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1284 strokeGreen = strokeColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1285 strokeBlue = strokeColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1286 strokeAlpha = strokeColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1287
1288 if ( redOk && greenOk && blueOk && alphaOk )
1289 {
1290 lineSymbolLayer->setColor( QColor( strokeRed, strokeGreen, strokeBlue, strokeAlpha ) );
1291 }
1292 }
1293 else
1294 {
1295 lineSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral( "penColor" ), QStringLiteral( "#000000" ) ) ) );
1296 }
1297
1298 // need to translate the deprecated ScalebarLineWidth and ScalebarLineColor properties to symbol properties,
1299 // and then remove them from the scalebar so they don't interfere and apply to other compatibility workarounds
1304
1305 lineSymbol->changeSymbolLayer( 0, lineSymbolLayer.release() );
1306 mSettings.setLineSymbol( lineSymbol->clone() );
1307 mSettings.setDivisionLineSymbol( lineSymbol->clone() );
1308 mSettings.setSubdivisionLineSymbol( lineSymbol.release() );
1309 }
1310
1311 mSettings.setUnitLabel( itemElem.attribute( QStringLiteral( "unitLabel" ) ) );
1312
1313 const QDomNodeList textFormatNodeList = itemElem.elementsByTagName( QStringLiteral( "text-style" ) );
1314 if ( !textFormatNodeList.isEmpty() )
1315 {
1316 const QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
1317 mSettings.textFormat().readXml( textFormatElem, context );
1318 }
1319 else
1320 {
1321 QFont f;
1322 if ( !QgsFontUtils::setFromXmlChildNode( f, itemElem, QStringLiteral( "scaleBarFont" ) ) )
1323 {
1324 f.fromString( itemElem.attribute( QStringLiteral( "font" ), QString() ) );
1325 }
1326 mSettings.textFormat().setFont( f );
1327 if ( f.pointSizeF() > 0 )
1328 {
1329 mSettings.textFormat().setSize( f.pointSizeF() );
1331 }
1332 else if ( f.pixelSize() > 0 )
1333 {
1334 mSettings.textFormat().setSize( f.pixelSize() );
1336 }
1337 }
1338
1339 const QDomNodeList numericFormatNodeList = itemElem.elementsByTagName( QStringLiteral( "numericFormat" ) );
1340 if ( !numericFormatNodeList.isEmpty() )
1341 {
1342 const QDomElement numericFormatElem = numericFormatNodeList.at( 0 ).toElement();
1343 mSettings.setNumericFormat( QgsApplication::numericFormatRegistry()->createFromXml( numericFormatElem, context ) );
1344 }
1345
1346 const QDomElement fillSymbol1Elem = itemElem.firstChildElement( QStringLiteral( "fillSymbol1" ) );
1347 bool foundFillSymbol1 = false;
1348 if ( !fillSymbol1Elem.isNull() )
1349 {
1350 const QDomElement symbolElem = fillSymbol1Elem.firstChildElement( QStringLiteral( "symbol" ) );
1351 std::unique_ptr< QgsFillSymbol > fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
1352 if ( fillSymbol )
1353 {
1354 mSettings.setFillSymbol( fillSymbol.release() );
1355 foundFillSymbol1 = true;
1356 }
1357 }
1358 if ( !foundFillSymbol1 )
1359 {
1360 // old project compatibility
1361 auto fillSymbol = std::make_unique< QgsFillSymbol >();
1362 auto fillSymbolLayer = std::make_unique< QgsSimpleFillSymbolLayer >();
1363 fillSymbolLayer->setStrokeStyle( Qt::NoPen );
1364
1365 //fill color
1366 const QDomNodeList fillColorList = itemElem.elementsByTagName( QStringLiteral( "fillColor" ) );
1367 if ( !fillColorList.isEmpty() )
1368 {
1369 const QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
1370 bool redOk, greenOk, blueOk, alphaOk;
1371 int fillRed, fillGreen, fillBlue, fillAlpha;
1372
1373 fillRed = fillColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1374 fillGreen = fillColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1375 fillBlue = fillColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1376 fillAlpha = fillColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1377
1378 if ( redOk && greenOk && blueOk && alphaOk )
1379 {
1380 fillSymbolLayer->setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
1381 }
1382 }
1383 else
1384 {
1385 fillSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral( "brushColor" ), QStringLiteral( "#000000" ) ) ) );
1386 }
1387
1388 // need to translate the deprecated ScalebarFillColor property to symbol properties,
1389 // and then remove them from the scalebar so they don't interfere and apply to other compatibility workarounds
1392
1393 fillSymbol->changeSymbolLayer( 0, fillSymbolLayer.release() );
1394 mSettings.setFillSymbol( fillSymbol.release() );
1395 }
1396
1397 const QDomElement fillSymbol2Elem = itemElem.firstChildElement( QStringLiteral( "fillSymbol2" ) );
1398 bool foundFillSymbol2 = false;
1399 if ( !fillSymbol2Elem.isNull() )
1400 {
1401 const QDomElement symbolElem = fillSymbol2Elem.firstChildElement( QStringLiteral( "symbol" ) );
1402 std::unique_ptr< QgsFillSymbol > fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
1403 if ( fillSymbol )
1404 {
1405 mSettings.setAlternateFillSymbol( fillSymbol.release() );
1406 foundFillSymbol2 = true;
1407 }
1408 }
1409 if ( !foundFillSymbol2 )
1410 {
1411 // old project compatibility
1412 auto fillSymbol = std::make_unique< QgsFillSymbol >();
1413 auto fillSymbolLayer = std::make_unique< QgsSimpleFillSymbolLayer >();
1414 fillSymbolLayer->setStrokeStyle( Qt::NoPen );
1415
1416 //fill color 2
1417
1418 const QDomNodeList fillColor2List = itemElem.elementsByTagName( QStringLiteral( "fillColor2" ) );
1419 if ( !fillColor2List.isEmpty() )
1420 {
1421 const QDomElement fillColor2Elem = fillColor2List.at( 0 ).toElement();
1422 bool redOk, greenOk, blueOk, alphaOk;
1423 int fillRed, fillGreen, fillBlue, fillAlpha;
1424
1425 fillRed = fillColor2Elem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1426 fillGreen = fillColor2Elem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1427 fillBlue = fillColor2Elem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1428 fillAlpha = fillColor2Elem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1429
1430 if ( redOk && greenOk && blueOk && alphaOk )
1431 {
1432 fillSymbolLayer->setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
1433 }
1434 }
1435 else
1436 {
1437 fillSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral( "brush2Color" ), QStringLiteral( "#ffffff" ) ) ) );
1438 }
1439
1440 // need to translate the deprecated ScalebarFillColor2 property to symbol properties,
1441 // and then remove them from the scalebar so they don't interfere and apply to other compatibility workarounds
1444
1445 fillSymbol->changeSymbolLayer( 0, fillSymbolLayer.release() );
1446 mSettings.setAlternateFillSymbol( fillSymbol.release() );
1447
1448 }
1449
1450 //font color
1451 const QDomNodeList textColorList = itemElem.elementsByTagName( QStringLiteral( "textColor" ) );
1452 if ( !textColorList.isEmpty() )
1453 {
1454 const QDomElement textColorElem = textColorList.at( 0 ).toElement();
1455 bool redOk, greenOk, blueOk, alphaOk;
1456 int textRed, textGreen, textBlue, textAlpha;
1457
1458 textRed = textColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1459 textGreen = textColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1460 textBlue = textColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1461 textAlpha = textColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1462
1463 if ( redOk && greenOk && blueOk && alphaOk )
1464 {
1465 mSettings.textFormat().setColor( QColor( textRed, textGreen, textBlue, textAlpha ) );
1466 }
1467 }
1468 else if ( itemElem.hasAttribute( QStringLiteral( "fontColor" ) ) )
1469 {
1470 QColor c;
1471 c.setNamedColor( itemElem.attribute( QStringLiteral( "fontColor" ), QStringLiteral( "#000000" ) ) );
1472 mSettings.textFormat().setColor( c );
1473 }
1474
1475 //style
1476 setStyle( itemElem.attribute( QStringLiteral( "style" ), QString() ) );
1477
1478 //call attemptResize after setStyle to ensure the appropriate size limitations are applied
1479 attemptResize( QgsLayoutSize::decodeSize( itemElem.attribute( QStringLiteral( "size" ) ) ) );
1480
1481 if ( itemElem.attribute( QStringLiteral( "unitType" ) ).isEmpty() )
1482 {
1484 switch ( itemElem.attribute( QStringLiteral( "units" ) ).toInt() )
1485 {
1486 case 0:
1488 break;
1489 case 1:
1491 break;
1492 case 2:
1494 break;
1495 case 3:
1497 break;
1498 }
1499 mSettings.setUnits( u );
1500 }
1501 else
1502 {
1503 mSettings.setUnits( QgsUnitTypes::decodeDistanceUnit( itemElem.attribute( QStringLiteral( "unitType" ) ) ) );
1504 }
1505
1506 mSettings.setLabelVerticalPlacement( static_cast< Qgis::ScaleBarDistanceLabelVerticalPlacement >( itemElem.attribute( QStringLiteral( "labelVerticalPlacement" ), QStringLiteral( "0" ) ).toInt() ) );
1507 mSettings.setLabelHorizontalPlacement( static_cast< Qgis::ScaleBarDistanceLabelHorizontalPlacement >( itemElem.attribute( QStringLiteral( "labelHorizontalPlacement" ), QStringLiteral( "0" ) ).toInt() ) );
1508
1509 mSettings.setAlignment( static_cast< Qgis::ScaleBarAlignment >( itemElem.attribute( QStringLiteral( "alignment" ), QStringLiteral( "0" ) ).toInt() ) );
1510
1511 //map
1512 disconnectCurrentMap();
1513 mMap = nullptr;
1514 mMapUuid = itemElem.attribute( QStringLiteral( "mapUuid" ) );
1515 return true;
1516}
1517
1518
1520{
1521 if ( mLayout && !mMapUuid.isEmpty() )
1522 {
1523 disconnectCurrentMap();
1524 mMap = qobject_cast< QgsLayoutItemMap * >( mLayout->itemByUuid( mMapUuid, true ) );
1525 if ( mMap )
1526 {
1527 connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
1528 connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
1529 }
1530 }
1531
1532 updateScale();
1533}
1534
1536{
1537 QgsStyleTextFormatEntity entity( mSettings.textFormat() );
1538 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
1539 return false;
1540
1541 return true;
1542}
1543
ScaleBarDistanceLabelHorizontalPlacement
Scale bar distance label horizontal placement.
Definition qgis.h:5140
ScaleBarDistanceLabelVerticalPlacement
Scale bar distance label vertical placement.
Definition qgis.h:5126
@ Millimeters
Millimeters.
ScaleBarAlignment
Scalebar alignment.
Definition qgis.h:5097
DistanceUnit
Units of distance.
Definition qgis.h:4843
@ Feet
Imperial feet.
@ Miles
Terrestrial miles.
@ Unknown
Unknown distance unit.
@ Degrees
Degrees, for planar geographic CRS distance measurements.
@ NauticalMiles
Nautical miles.
@ Kilometers
Kilometers.
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
ScaleCalculationMethod
Scale calculation logic.
Definition qgis.h:5080
@ HorizontalTop
Calculate horizontally, across top of map.
@ HorizontalMiddle
Calculate horizontally, across midle of map.
@ AtEquator
Always calculate the scale at the equator, regardless of the actual visible map extent....
@ HorizontalAverage
Calculate horizontally, using the average of the top, middle and bottom scales.
@ HorizontalBottom
Calculate horizontally, across bottom of map.
ScaleBarSegmentSizeMode
Modes for setting size for scale bar segments.
Definition qgis.h:5112
@ FitWidth
Scale bar segment size is calculated to fit a size range.
@ Fixed
Scale bar segment size is fixed to a map unit.
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.
int valueAsInt(int key, const QgsExpressionContext &context, int defaultValue=0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as an integer.
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, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Represents a coordinate reference system (CRS).
Custom exception class for Coordinate Reference System related exceptions.
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.
Qgis::DistanceUnit lengthUnits() const
Returns the units of distance for length calculations made by this object.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
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.
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
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.
static void setFontFamily(QFont &font, const QString &family)
Sets the family for a font object.
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.
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
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.
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.
double height() const
Returns the scalebar height (in millimeters).
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 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).
double unitsPerSegment() const
Returns the number of scalebar units per segment.
void setAlignment(Qgis::ScaleBarAlignment alignment)
Sets the scalebar alignment.
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.
double minimumBarWidth() const
Returns the minimum width (in millimeters) for scale bar segments.
QgsLineSymbol * subdivisionLineSymbol() const
Returns the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
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 setAlternateFillSymbol(QgsFillSymbol *symbol)
Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types).
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.
void setLabelVerticalPlacement(Qgis::ScaleBarDistanceLabelVerticalPlacement placement)
Sets the vertical placement of text labels.
void setLabelHorizontalPlacement(Qgis::ScaleBarDistanceLabelHorizontalPlacement placement)
Sets the horizontal placement of text labels.
void setHeight(double height)
Sets the scalebar height (in millimeters).
Q_DECL_DEPRECATED Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
void setSubdivisionsHeight(double height)
Sets the scalebar subdivisions height (in millimeters) for segments included in the right part of the...
void setNumberOfSegmentsLeft(int segments)
Sets the number of segments included in the left part of the scalebar.
Qgis::DistanceUnit units() const
Returns the distance units used by the scalebar.
void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::DataDefinedProperty::AllProperties) override
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
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 setUnits(Qgis::DistanceUnit units)
Sets the distance units used by 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.
void setSegmentSizeMode(Qgis::ScaleBarSegmentSizeMode mode)
Sets the size mode for scale bar segments.
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
double maximumBarWidth() const
Returns the maximum width (in millimeters) for scale bar segments.
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.
void setMethod(Qgis::ScaleCalculationMethod method)
Sets the scale calculation method, which determines how the bar's scale will be calculated.
Q_DECL_DEPRECATED void setFont(const QFont &font)
Sets the font used for drawing text in the scalebar.
Qgis::DistanceUnit guessUnits() const
Attempts to guess the most reasonable unit choice for the scalebar, given the current linked map's sc...
void setSubdivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
void applyDefaultSize(Qgis::DistanceUnit units=Qgis::DistanceUnit::Meters)
Applies the default size to the scale bar (scale bar 1/5 of map item width)
Qgis::ScaleCalculationMethod method() const
Returns the scale calculation method, which determines how the bar's scale will be calculated.
void setNumberOfSubdivisions(int subdivisions)
Sets the number of subdivisions for segments included in the right part of the scalebar (only used fo...
QgsLayoutItemScaleBar(QgsLayout *layout)
Constructor for QgsLayoutItemScaleBar, with the specified parent layout.
Base class for graphical items within a QgsLayout.
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::DataDefinedProperty::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
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 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.
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
QgsPropertyCollection mDataDefinedProperties
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the object's property collection, used for data defined overrides.
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.
@ ScalebarMinimumWidth
Scalebar segment minimum width.
@ ScalebarRightSegments
Number of segments on the right of 0.
@ ScalebarLineColor
Scalebar line color (deprecated, use data defined properties on scalebar line symbol instead)
@ ScalebarRightSegmentSubdivisions
Number of subdivisions per segment on right of 0.
@ ScalebarMaximumWidth
Scalebar segment maximum width.
@ ScalebarFillColor2
Scalebar secondary fill color (deprecated, use data defined properties on scalebar fill symbol 2 inst...
@ ScalebarSubdivisionHeight
Scalebar subdivision height.
@ ScalebarFillColor
Scalebar fill color (deprecated, use data defined properties on scalebar fill symbol 1 instead)
@ ScalebarLineWidth
Scalebar line width (deprecated, use data defined properties on scalebar line symbol instead)
@ ScalebarLeftSegments
Number of segments on the left of 0.
@ ScalebarSegmentWidth
Scalebar width in map units of a single segment.
@ AllProperties
All properties for item.
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
static QgsLayoutSize decodeSize(const QString &string)
Decodes a size from a string.
void setWidth(const double width)
Sets the width for the size.
Qgis::LayoutUnit units() const
Returns the units for the size.
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:49
A line symbol type, for rendering LineString and MultiLineString geometries.
QgsLineSymbol * clone() const override
Returns a deep copy of this symbol.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Abstract base class for numeric formatters, which allow for formatting a numeric value for display.
void writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Writes the format to an XML element.
Represents a 2D point.
Definition qgspointxy.h:60
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
A store for object properties.
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render 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.
Qgis::ScaleBarAlignment alignment() const
Returns the scalebar alignment.
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).
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).
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.
void setLabelHorizontalPlacement(Qgis::ScaleBarDistanceLabelHorizontalPlacement placement)
Sets the horizontal placement of text labels.
Qgis::ScaleBarDistanceLabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
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.
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
Q_DECL_DEPRECATED QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
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.
void setLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar (only used for some scalebar types).
void setSegmentSizeMode(Qgis::ScaleBarSegmentSizeMode mode)
Sets the size mode for scale bar segments.
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.
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).
Qgis::ScaleBarSegmentSizeMode segmentSizeMode() const
Returns the size mode for the scale bar segments.
void setLabelVerticalPlacement(Qgis::ScaleBarDistanceLabelVerticalPlacement placement)
Sets the vertical placement of text labels.
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.
void setNumberOfSubdivisions(int subdivisions)
Sets the number of subdivisions for segments included in the right part of the scalebar (only used fo...
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...
Qgis::ScaleBarDistanceLabelHorizontalPlacement labelHorizontalPlacement() const
Returns the horizontal placement of text labels.
Q_DECL_DEPRECATED QColor lineColor() const
Returns the color used for lines in the scalebar.
Qgis::DistanceUnit units() const
Returns the distance units used by the scalebar.
void setLabelBarSpace(double space)
Sets the spacing (in millimeters) between labels and the scalebar.
void setAlignment(Qgis::ScaleBarAlignment alignment)
Sets the scalebar alignment.
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.
Q_DECL_DEPRECATED Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
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.
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.
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.
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.
void setUnits(Qgis::DistanceUnit units)
Sets the distance units 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.
Stores settings for use within QGIS.
Definition qgssettings.h:66
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:1460
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)
@ StrokeColor
Stroke color.
@ StrokeWidth
Stroke width.
bool changeSymbolLayer(int index, QgsSymbolLayer *layer)
Deletes the current layer at the specified index and replaces it with layer.
Container for all settings relating to text rendering.
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 setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
void setOpacity(double opacity)
Sets the text's opacity.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
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.
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE Qgis::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=nullptr)
Decodes a distance unit from a string.
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
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:6496
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6819
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6477
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6818
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6286
#define QgsDebugError(str)
Definition qgslogger.h:40
const QgsCoordinateReferenceSystem & crs
Contains parameters regarding scalebar calculations.
QSizeF size
Destination size for scalebar.
bool isValid() const
Returns true if the context has valid settings.
QgsScaleBarRenderer::Flags flags
Scalebar renderer flags.
double segmentWidth
The width, in millimeters, of each individual segment drawn.
Contains information relating to the style entity currently being visited.