QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
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
20#include "qgslayoutitemmap.h"
21#include "qgslayout.h"
22#include "qgslayoututils.h"
23#include "qgsdistancearea.h"
25#include "qgsscalebarrenderer.h"
26#include "qgsmessagelog.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"
34#include "qgsnumericformat.h"
36#include "qgslinesymbollayer.h"
37#include "qgsfillsymbollayer.h"
38#include "qgslinesymbol.h"
39#include "qgsfillsymbol.h"
41
42#include <QDomDocument>
43#include <QDomElement>
44#include <QFontMetricsF>
45#include <QPainter>
46#include <QTextDocument>
47#include <QAbstractTextDocumentLayout>
48
49#include <cmath>
50
57
62
64{
65 return QgsApplication::getThemeIcon( QStringLiteral( "/mLayoutItemScaleBar.svg" ) );
66}
67
72
74{
76
77 const QgsScaleBarRenderer::ScaleBarContext scaleContext = createScaleContext();
78 if ( !scaleContext.isValid() )
79 return QgsLayoutSize();
80
81 return QgsLayoutSize( mStyle->calculateBoxSize( context, mSettings, scaleContext ), Qgis::LayoutUnit::Millimeters );
82}
83
85{
86 if ( !mStyle )
87 return;
88
89 const QgsScaleBarRenderer::ScaleBarContext scaleContext = createScaleContext();
90 if ( !scaleContext.isValid() )
91 {
92 if ( mLayout->renderContext().isPreviewRender() )
93 {
94 // No initial render available - so draw some preview text alerting user
95 QPainter *painter = context.renderContext().painter();
96
97 const double scale = context.renderContext().convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
98 const QRectF thisPaintRect = QRectF( 0, 0, rect().width() * scale, rect().height() * scale );
99
100 painter->setBrush( QBrush( QColor( 255, 125, 125, 125 ) ) );
101 painter->setPen( Qt::NoPen );
102 painter->drawRect( thisPaintRect );
103 painter->setBrush( Qt::NoBrush );
104
105 painter->setPen( QColor( 200, 0, 0, 255 ) );
106 QTextDocument td;
107 td.setTextWidth( thisPaintRect.width() );
108 td.setHtml( QStringLiteral( "<span style=\"color: rgb(200,0,0);\"><b>%1</b><br>%2</span>" ).arg(
109 tr( "Invalid scale!" ),
110 tr( "The scale bar cannot be rendered due to invalid settings or an incompatible linked map extent." ) ) );
111 painter->setClipRect( thisPaintRect );
112 QAbstractTextDocumentLayout::PaintContext ctx;
113 td.documentLayout()->draw( painter, ctx );
114 }
115 return;
116 }
117
119 {
120 // compatibility code - ScalebarLineColor and ScalebarLineWidth are deprecated
122 std::unique_ptr< QgsLineSymbol > sym( mSettings.lineSymbol()->clone() );
129 mSettings.setLineSymbol( sym.release() );
130 }
132 {
133 // compatibility code - ScalebarLineColor and ScalebarLineWidth are deprecated
135 std::unique_ptr< QgsFillSymbol > sym( mSettings.fillSymbol()->clone() );
139 mSettings.setFillSymbol( sym.release() );
140 }
142 {
143 // compatibility code - ScalebarLineColor and ScalebarLineWidth are deprecated
145 std::unique_ptr< QgsFillSymbol > sym( mSettings.alternateFillSymbol()->clone() );
149 mSettings.setAlternateFillSymbol( sym.release() );
150 }
151
152 mStyle->draw( context.renderContext(), mSettings, scaleContext );
153}
154
156{
157 if ( !mStyle )
158 {
159 mSettings.setNumberOfSegments( nSegments );
160 return;
161 }
162 mSettings.setNumberOfSegments( nSegments );
164}
165
167{
168 if ( !mStyle )
169 {
170 mSettings.setUnitsPerSegment( units );
171 return;
172 }
173 mSettings.setUnitsPerSegment( units );
174 refreshSegmentMillimeters();
176}
177
178
180{
181 if ( !mStyle )
182 {
183 mSettings.setSegmentSizeMode( mode );
184 return;
185 }
186 mSettings.setSegmentSizeMode( mode );
187 refreshSegmentMillimeters();
189}
190
192{
193 if ( !mStyle )
194 {
195 mSettings.setMinimumBarWidth( minWidth );
196 return;
197 }
198 mSettings.setMinimumBarWidth( minWidth );
199 refreshSegmentMillimeters();
201}
202
204{
205 if ( !mStyle )
206 {
207 mSettings.setMaximumBarWidth( maxWidth );
208 return;
209 }
210 mSettings.setMaximumBarWidth( maxWidth );
211 refreshSegmentMillimeters();
213}
214
216{
217 return mSettings.textFormat();
218}
219
221{
222 mSettings.setTextFormat( format );
224 emit changed();
225}
226
228{
229 return mSettings.lineSymbol();
230}
231
233{
234 mSettings.setLineSymbol( symbol );
235}
236
241
246
251
256
258{
259 return mSettings.fillSymbol();
260}
261
263{
264 mSettings.setFillSymbol( symbol );
265}
266
271
276
278{
279 if ( !mStyle )
280 {
281 mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
282 return;
283 }
284 mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
286}
287
289{
290 if ( !mStyle )
291 {
292 mSettings.setBoxContentSpace( space );
293 return;
294 }
295 mSettings.setBoxContentSpace( space );
297}
298
300{
301 disconnectCurrentMap();
302
303 mMap = map;
304
305 if ( !map )
306 {
307 return;
308 }
309
310 connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
311 connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
312
313 refreshSegmentMillimeters();
314 emit changed();
315}
316
317void QgsLayoutItemScaleBar::disconnectCurrentMap()
318{
319 if ( !mMap )
320 {
321 return;
322 }
323
324 disconnect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
325 disconnect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
326 mMap = nullptr;
327}
328
333
335{
336 if ( mMethod == method )
337 return;
338
339 mMethod = method;
340 refreshSegmentMillimeters();
342}
343
344void QgsLayoutItemScaleBar::refreshUnitsPerSegment( const QgsExpressionContext *context )
345{
347 {
348 double unitsPerSegment = mSettings.unitsPerSegment();
349 bool ok = false;
351
352 if ( !ok )
353 {
354 QgsMessageLog::logMessage( tr( "Scalebar units per segment expression eval error" ) );
355 }
356 else
357 {
359 }
360 }
361}
362
363void QgsLayoutItemScaleBar::refreshMinimumBarWidth( const QgsExpressionContext *context )
364{
366 {
367 double minimumBarWidth = mSettings.minimumBarWidth();
368
369 bool ok = false;
371
372 if ( !ok )
373 {
374 QgsMessageLog::logMessage( tr( "Scalebar minimum segment width expression eval error" ) );
375 }
376 else
377 {
379 }
380 }
381}
382
383void QgsLayoutItemScaleBar::refreshMaximumBarWidth( const QgsExpressionContext *context )
384{
386 {
387 double maximumBarWidth = mSettings.maximumBarWidth();
388
389 bool ok = false;
391
392 if ( !ok )
393 {
394 QgsMessageLog::logMessage( tr( "Scalebar maximum segment width expression eval error" ) );
395 }
396 else
397 {
399 }
400 }
401}
402
403void QgsLayoutItemScaleBar::refreshNumberOfSegmentsLeft( const QgsExpressionContext *context )
404{
406 {
407 int leftSegments = mSettings.numberOfSegmentsLeft();
408
409 bool ok = false;
411
412 if ( !ok )
413 {
414 QgsMessageLog::logMessage( tr( "Scalebar left segment count expression eval error" ) );
415 }
416 else
417 {
418 setNumberOfSegmentsLeft( leftSegments );
419 }
420 }
421}
422
423void QgsLayoutItemScaleBar::refreshNumberOfSegmentsRight( const QgsExpressionContext *context )
424{
426 {
427 int rightSegments = mSettings.numberOfSegments();
428
429 bool ok = false;
431
432 if ( !ok )
433 {
434 QgsMessageLog::logMessage( tr( "Scalebar left segment count expression eval error" ) );
435 }
436 else
437 {
438 setNumberOfSegments( rightSegments );
439 }
440 }
441}
442
444{
446
447 bool forceUpdate = false;
448
451 {
452 double height = mSettings.height();
453
454 bool ok = false;
456
457 if ( !ok )
458 {
459 QgsMessageLog::logMessage( tr( "Scalebar height expression eval error" ) );
460 }
461 else
462 {
463 setHeight( height );
464 }
465
466 forceUpdate = true;
467 }
468
471 {
472 double height = mSettings.subdivisionsHeight();
473
474 bool ok = false;
476
477 if ( !ok )
478 {
479 QgsMessageLog::logMessage( tr( "Scalebar subdivision height expression eval error" ) );
480 }
481 else
482 {
484 }
485
486 forceUpdate = true;
487 }
488
490 {
491 refreshNumberOfSegmentsLeft( &context );
492 forceUpdate = true;
493 }
494
496 {
497 refreshNumberOfSegmentsRight( &context );
498 forceUpdate = true;
499 }
500
503 {
504 int segments = mSettings.numberOfSubdivisions();
505
506 bool ok = false;
508
509 if ( !ok )
510 {
511 QgsMessageLog::logMessage( tr( "Scalebar number of subdivisions expression eval error" ) );
512 }
513 else
514 {
515 setNumberOfSubdivisions( segments );
516 }
517
518 forceUpdate = true;
519 }
520
521
523 {
524 refreshUnitsPerSegment( &context );
525 forceUpdate = true;
526 }
527
529 {
530 refreshMinimumBarWidth( &context );
531 forceUpdate = true;
532 }
533
535 {
536 refreshMaximumBarWidth( &context );
537 forceUpdate = true;
538 }
539
540 // updates data defined properties and redraws item to match
541 // -- Deprecated --
543 {
544 forceUpdate = true;
545 }
547 {
548 forceUpdate = true;
549 }
551 {
552 forceUpdate = true;
553 }
555 {
556 forceUpdate = true;
557 }
558
559 if ( forceUpdate )
560 {
562 update();
563 }
564
566}
567
568void QgsLayoutItemScaleBar::refreshSegmentMillimeters()
569{
570 if ( mMap )
571 {
572 //get mm dimension of composer map
573 const QRectF composerItemRect = mMap->rect();
574
575 const double currentMapWidth = mapWidth();
576 if ( qgsDoubleNear( currentMapWidth, 0 ) || std::isnan( currentMapWidth ) )
577 {
578 mSegmentMillimeters = std::numeric_limits< double >::quiet_NaN();
579 return;
580 }
581
582 switch ( mSettings.segmentSizeMode() )
583 {
585 {
586 //calculate size depending on mNumUnitsPerSegment
587 mSegmentMillimeters = composerItemRect.width() / currentMapWidth * mSettings.unitsPerSegment();
588 break;
589 }
590
592 {
593 if ( mSettings.maximumBarWidth() < mSettings.minimumBarWidth() )
594 {
595 mSegmentMillimeters = 0;
596 }
597 else
598 {
599 const double nSegments = ( mSettings.numberOfSegmentsLeft() != 0 ) + mSettings.numberOfSegments();
600 // unitsPerSegments which fit minBarWidth resp. maxBarWidth
601 const double minUnitsPerSeg = ( mSettings.minimumBarWidth() * currentMapWidth ) / ( nSegments * composerItemRect.width() );
602 const double maxUnitsPerSeg = ( mSettings.maximumBarWidth() * currentMapWidth ) / ( nSegments * composerItemRect.width() );
603 mSettings.setUnitsPerSegment( QgsLayoutUtils::calculatePrettySize( minUnitsPerSeg, maxUnitsPerSeg ) );
604 mSegmentMillimeters = composerItemRect.width() / currentMapWidth * mSettings.unitsPerSegment();
605 }
606 break;
607 }
608 }
609 }
610}
611
612double QgsLayoutItemScaleBar::mapWidth() const
613{
614 if ( !mMap )
615 {
616 return 0.0;
617 }
618
619 const QgsRectangle mapExtent = mMap->extent();
620 if ( mSettings.units() == Qgis::DistanceUnit::Unknown )
621 {
622 return mapExtent.width();
623 }
624 else
625 {
627 da.setSourceCrs( mMap->crs(), mLayout->project()->transformContext() );
628 da.setEllipsoid( mLayout->project()->ellipsoid() );
629
631
632 QList< double > yValues;
633 switch ( mMethod )
634 {
636 yValues << mapExtent.yMaximum();
637 break;
638
640 yValues << 0.5 * ( mapExtent.yMaximum() + mapExtent.yMinimum() );
641 break;
642
643
645 yValues << mapExtent.yMinimum();
646 break;
647
649 yValues << mapExtent.yMaximum();
650 yValues << 0.5 * ( mapExtent.yMaximum() + mapExtent.yMinimum() );
651 yValues << mapExtent.yMinimum();
652 break;
653 }
654
655 double sumValidMeasures = 0;
656 int validMeasureCount = 0;
657
658 for ( const double y : std::as_const( yValues ) )
659 {
660 try
661 {
662 double measure = da.measureLine( QgsPointXY( mapExtent.xMinimum(), y ),
663 QgsPointXY( mapExtent.xMaximum(), y ) );
664 if ( std::isnan( measure ) )
665 {
666 // TODO report errors to user
667 QgsDebugError( QStringLiteral( "An error occurred while calculating length" ) );
668 continue;
669 }
670
671 measure /= QgsUnitTypes::fromUnitToUnitFactor( mSettings.units(), units );
672 sumValidMeasures += measure;
673 validMeasureCount++;
674 }
675 catch ( QgsCsException & )
676 {
677 // TODO report errors to user
678 QgsDebugError( QStringLiteral( "An error occurred while calculating length" ) );
679 continue;
680 }
681 }
682
683 if ( validMeasureCount == 0 )
684 return std::numeric_limits< double >::quiet_NaN();
685
686 return sumValidMeasures / validMeasureCount;
687 }
688}
689
690QgsScaleBarRenderer::ScaleBarContext QgsLayoutItemScaleBar::createScaleContext() const
691{
693 scaleContext.size = rect().size();
694 scaleContext.segmentWidth = mSegmentMillimeters;
695 scaleContext.scale = mMap ? mMap->scale() : 1.0;
696 scaleContext.flags = mStyle->flags();
697 return scaleContext;
698}
699
706
713
715{
716 mSettings.setAlignment( a );
718 emit changed();
719}
720
722{
723 mSettings.setUnits( u );
724 refreshSegmentMillimeters();
726 emit changed();
727}
728
730{
732 return mSettings.lineJoinStyle();
734}
735
736void QgsLayoutItemScaleBar::setLineJoinStyle( Qt::PenJoinStyle style )
737{
739 if ( mSettings.lineJoinStyle() == style )
740 {
741 //no change
742 return;
743 }
744 mSettings.setLineJoinStyle( style );
746 update();
747 emit changed();
748}
749
751{
753 return mSettings.lineCapStyle();
755}
756
757void QgsLayoutItemScaleBar::setLineCapStyle( Qt::PenCapStyle style )
758{
760 if ( mSettings.lineCapStyle() == style )
761 {
762 //no change
763 return;
764 }
765 mSettings.setLineCapStyle( style );
767 update();
768 emit changed();
769}
770
772{
773 //style
774 mStyle = std::make_unique< QgsSingleBoxScaleBarRenderer >();
775
776 //default to no background
777 setBackgroundEnabled( false );
778
779 //get default composer font from settings
780 const QgsSettings settings;
781 const QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
782 QgsTextFormat format;
783 QFont f;
784 if ( !defaultFontString.isEmpty() )
785 {
786 QgsFontUtils::setFontFamily( f, defaultFontString );
787 }
788 format.setFont( f );
789 format.setSize( 12.0 );
791
792 mSettings.setTextFormat( format );
793
796
797 emit changed();
798}
799
801{
802 return renderer->applyDefaultSettings( mSettings );
803}
804
806{
807 if ( !mMap )
809
810 const QgsCoordinateReferenceSystem crs = mMap->crs();
811 // start with crs units
814 {
815 // geographic CRS, use metric units
817 }
818
819 // try to pick reasonable choice between metric / imperial units
820 const double widthInSelectedUnits = mapWidth();
821 if ( std::isnan( widthInSelectedUnits ) )
822 return unit;
823
824 const double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
825 switch ( unit )
826 {
828 {
829 if ( initialUnitsPerSegment > 1000.0 )
830 {
832 }
833 break;
834 }
836 {
837 if ( initialUnitsPerSegment > 5419.95 )
838 {
840 }
841 break;
842 }
843 default:
844 break;
845 }
846
847 return unit;
848}
849
851{
852 mSettings.setUnits( units );
853 if ( mMap )
854 {
855 double upperMagnitudeMultiplier = 1.0;
856 const double widthInSelectedUnits = mapWidth();
857 if ( !std::isnan( widthInSelectedUnits ) )
858 {
859 const double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
860 mSettings.setUnitsPerSegment( initialUnitsPerSegment );
861
863 upperMagnitudeMultiplier = 1;
864
865 const double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
866 const int segmentMagnitude = std::floor( std::log10( segmentWidth ) );
867 double unitsPerSegment = upperMagnitudeMultiplier * ( std::pow( 10.0, segmentMagnitude ) );
868 const double multiplier = std::floor( ( widthInSelectedUnits / ( unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
869
870 if ( multiplier > 0 )
871 {
872 unitsPerSegment = unitsPerSegment * multiplier;
873 }
875 mSettings.setMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );
876
877 mSettings.setNumberOfSegments( 2 );
878 mSettings.setNumberOfSegmentsLeft( 0 );
879 }
880 }
881
882 refreshSegmentMillimeters();
884 emit changed();
885}
886
888{
889 if ( !mStyle )
890 return;
891
893
894 const QgsScaleBarRenderer::ScaleBarContext scaleContext = createScaleContext();
895 if ( !scaleContext.isValid() )
896 return;
897
898 const double widthMM = mStyle->calculateBoxSize( context, mSettings, scaleContext ).width();
899 QgsLayoutSize currentSize = sizeWithUnits();
900 currentSize.setWidth( mLayout->renderContext().measurementConverter().convert( QgsLayoutMeasurement( widthMM, Qgis::LayoutUnit::Millimeters ), currentSize.units() ).length() );
901 attemptResize( currentSize );
902 update();
903 emit changed();
904}
905
907{
908 //Don't adjust box size for numeric scale bars:
909 if ( mStyle && mStyle->id() != QLatin1String( "Numeric" ) )
910 {
912 }
913 QgsLayoutItem::update();
914}
915
916void QgsLayoutItemScaleBar::updateScale()
917{
918 refreshSegmentMillimeters();
919 //Don't adjust box size for numeric scale bars:
920 if ( mStyle && mStyle->id() != QLatin1String( "Numeric" ) )
921 {
923 }
924 update();
925}
926
927void QgsLayoutItemScaleBar::setStyle( const QString &styleName )
928{
929 //switch depending on style name
930 std::unique_ptr< QgsScaleBarRenderer> renderer( QgsApplication::scaleBarRendererRegistry()->renderer( styleName ) );
931 if ( renderer )
932 {
933 mStyle = std::move( renderer );
934 }
936 emit changed();
937}
938
940{
941 if ( mStyle )
942 {
943 return mStyle->id();
944 }
945 else
946 {
947 return QString();
948 }
949}
950
952{
953 return mSettings.numericFormat();
954}
955
957{
958 mSettings.setNumericFormat( format );
959}
960
962{
963 return mSettings.textFormat().font();
964}
965
966void QgsLayoutItemScaleBar::setFont( const QFont &font )
967{
969 mSettings.setFont( font );
972 emit changed();
973}
974
976{
977 QColor color = mSettings.textFormat().color();
978 color.setAlphaF( mSettings.textFormat().opacity() );
979 return color;
980}
981
982void QgsLayoutItemScaleBar::setFontColor( const QColor &color )
983{
984 mSettings.textFormat().setColor( color );
985 mSettings.textFormat().setOpacity( color.alphaF() );
986}
987
994
995void QgsLayoutItemScaleBar::setFillColor( const QColor &color )
996{
998 mSettings.setFillColor( color );
1000}
1001
1003{
1005 return mSettings.fillColor2();
1007}
1008
1009void QgsLayoutItemScaleBar::setFillColor2( const QColor &color )
1010{
1012 mSettings.setFillColor2( color );
1014}
1015
1017{
1019 return mSettings.lineColor();
1021}
1022
1023void QgsLayoutItemScaleBar::setLineColor( const QColor &color )
1024{
1026 mSettings.setLineColor( color );
1028}
1029
1031{
1033 return mSettings.lineWidth();
1035}
1036
1038{
1040 mSettings.setLineWidth( width );
1042}
1043
1045{
1047 return mSettings.pen();
1049}
1050
1052{
1054 return mSettings.brush();
1056}
1057
1059{
1061 return mSettings.brush2();
1063}
1064
1065bool QgsLayoutItemScaleBar::writePropertiesToElement( QDomElement &composerScaleBarElem, QDomDocument &doc, const QgsReadWriteContext &rwContext ) const
1066{
1067 composerScaleBarElem.setAttribute( QStringLiteral( "height" ), QString::number( mSettings.height() ) );
1068 composerScaleBarElem.setAttribute( QStringLiteral( "labelBarSpace" ), QString::number( mSettings.labelBarSpace() ) );
1069 composerScaleBarElem.setAttribute( QStringLiteral( "boxContentSpace" ), QString::number( mSettings.boxContentSpace() ) );
1070 composerScaleBarElem.setAttribute( QStringLiteral( "numSegments" ), mSettings.numberOfSegments() );
1071 composerScaleBarElem.setAttribute( QStringLiteral( "numSegmentsLeft" ), mSettings.numberOfSegmentsLeft() );
1072 composerScaleBarElem.setAttribute( QStringLiteral( "numSubdivisions" ), mSettings.numberOfSubdivisions() );
1073 composerScaleBarElem.setAttribute( QStringLiteral( "subdivisionsHeight" ), mSettings.subdivisionsHeight() );
1074 composerScaleBarElem.setAttribute( QStringLiteral( "numUnitsPerSegment" ), QString::number( mSettings.unitsPerSegment() ) );
1075 composerScaleBarElem.setAttribute( QStringLiteral( "segmentSizeMode" ), static_cast< int >( mSettings.segmentSizeMode() ) );
1076 composerScaleBarElem.setAttribute( QStringLiteral( "minBarWidth" ), mSettings.minimumBarWidth() );
1077 composerScaleBarElem.setAttribute( QStringLiteral( "maxBarWidth" ), mSettings.maximumBarWidth() );
1078 composerScaleBarElem.setAttribute( QStringLiteral( "segmentMillimeters" ), QString::number( mSegmentMillimeters ) );
1079 composerScaleBarElem.setAttribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QString::number( mSettings.mapUnitsPerScaleBarUnit() ) );
1080 composerScaleBarElem.setAttribute( QStringLiteral( "method" ), qgsEnumValueToKey( mMethod ) );
1081
1082 const QDomElement textElem = mSettings.textFormat().writeXml( doc, rwContext );
1083 composerScaleBarElem.appendChild( textElem );
1084
1086 // kept just for allowing projects to open in QGIS < 3.14, remove for 4.0
1087 composerScaleBarElem.setAttribute( QStringLiteral( "outlineWidth" ), QString::number( mSettings.lineWidth() ) );
1088 composerScaleBarElem.setAttribute( QStringLiteral( "lineJoinStyle" ), QgsSymbolLayerUtils::encodePenJoinStyle( mSettings.lineJoinStyle() ) );
1089 composerScaleBarElem.setAttribute( QStringLiteral( "lineCapStyle" ), QgsSymbolLayerUtils::encodePenCapStyle( mSettings.lineCapStyle() ) );
1090 //pen color
1091 QDomElement strokeColorElem = doc.createElement( QStringLiteral( "strokeColor" ) );
1092 strokeColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.lineColor().red() ) );
1093 strokeColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.lineColor().green() ) );
1094 strokeColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.lineColor().blue() ) );
1095 strokeColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.lineColor().alpha() ) );
1096 composerScaleBarElem.appendChild( strokeColorElem );
1098
1099 composerScaleBarElem.setAttribute( QStringLiteral( "unitLabel" ), mSettings.unitLabel() );
1100 composerScaleBarElem.setAttribute( QStringLiteral( "unitType" ), QgsUnitTypes::encodeUnit( mSettings.units() ) );
1101
1102 QDomElement numericFormatElem = doc.createElement( QStringLiteral( "numericFormat" ) );
1103 mSettings.numericFormat()->writeXml( numericFormatElem, doc, rwContext );
1104 composerScaleBarElem.appendChild( numericFormatElem );
1105
1106 //style
1107 if ( mStyle )
1108 {
1109 composerScaleBarElem.setAttribute( QStringLiteral( "style" ), mStyle->id() );
1110 }
1111
1112 //map id
1113 if ( mMap )
1114 {
1115 composerScaleBarElem.setAttribute( QStringLiteral( "mapUuid" ), mMap->uuid() );
1116 }
1117
1118 //colors
1119
1121 // kept just for allowing projects to open in QGIS < 3.14, remove for 4.0
1122
1123 //fill color
1124 QDomElement fillColorElem = doc.createElement( QStringLiteral( "fillColor" ) );
1125 fillColorElem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor().red() ) );
1126 fillColorElem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor().green() ) );
1127 fillColorElem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor().blue() ) );
1128 fillColorElem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor().alpha() ) );
1129 composerScaleBarElem.appendChild( fillColorElem );
1130
1131 //fill color 2
1132 QDomElement fillColor2Elem = doc.createElement( QStringLiteral( "fillColor2" ) );
1133 fillColor2Elem.setAttribute( QStringLiteral( "red" ), QString::number( mSettings.fillColor2().red() ) );
1134 fillColor2Elem.setAttribute( QStringLiteral( "green" ), QString::number( mSettings.fillColor2().green() ) );
1135 fillColor2Elem.setAttribute( QStringLiteral( "blue" ), QString::number( mSettings.fillColor2().blue() ) );
1136 fillColor2Elem.setAttribute( QStringLiteral( "alpha" ), QString::number( mSettings.fillColor2().alpha() ) );
1137 composerScaleBarElem.appendChild( fillColor2Elem );
1138
1140
1141 //label vertical/horizontal placement
1142 composerScaleBarElem.setAttribute( QStringLiteral( "labelVerticalPlacement" ), QString::number( static_cast< int >( mSettings.labelVerticalPlacement() ) ) );
1143 composerScaleBarElem.setAttribute( QStringLiteral( "labelHorizontalPlacement" ), QString::number( static_cast< int >( mSettings.labelHorizontalPlacement() ) ) );
1144
1145 //alignment
1146 composerScaleBarElem.setAttribute( QStringLiteral( "alignment" ), QString::number( static_cast< int >( mSettings.alignment() ) ) );
1147
1148 QDomElement lineSymbol = doc.createElement( QStringLiteral( "lineSymbol" ) );
1149 const QDomElement symbolElem = QgsSymbolLayerUtils::saveSymbol( QString(),
1150 mSettings.lineSymbol(),
1151 doc,
1152 rwContext );
1153 lineSymbol.appendChild( symbolElem );
1154 composerScaleBarElem.appendChild( lineSymbol );
1155
1156 QDomElement divisionSymbol = doc.createElement( QStringLiteral( "divisionLineSymbol" ) );
1157 const QDomElement divisionSymbolElem = QgsSymbolLayerUtils::saveSymbol( QString(),
1158 mSettings.divisionLineSymbol(),
1159 doc,
1160 rwContext );
1161 divisionSymbol.appendChild( divisionSymbolElem );
1162 composerScaleBarElem.appendChild( divisionSymbol );
1163
1164 QDomElement subdivisionSymbol = doc.createElement( QStringLiteral( "subdivisionLineSymbol" ) );
1165 const QDomElement subdivisionSymbolElem = QgsSymbolLayerUtils::saveSymbol( QString(),
1166 mSettings.subdivisionLineSymbol(),
1167 doc,
1168 rwContext );
1169 subdivisionSymbol.appendChild( subdivisionSymbolElem );
1170 composerScaleBarElem.appendChild( subdivisionSymbol );
1171
1172 QDomElement fillSymbol1Elem = doc.createElement( QStringLiteral( "fillSymbol1" ) );
1173 const QDomElement symbol1Elem = QgsSymbolLayerUtils::saveSymbol( QString(),
1174 mSettings.fillSymbol(),
1175 doc,
1176 rwContext );
1177 fillSymbol1Elem.appendChild( symbol1Elem );
1178 composerScaleBarElem.appendChild( fillSymbol1Elem );
1179
1180 QDomElement fillSymbol2Elem = doc.createElement( QStringLiteral( "fillSymbol2" ) );
1181 const QDomElement symbol2Elem = QgsSymbolLayerUtils::saveSymbol( QString(),
1182 mSettings.alternateFillSymbol(),
1183 doc,
1184 rwContext );
1185 fillSymbol2Elem.appendChild( symbol2Elem );
1186 composerScaleBarElem.appendChild( fillSymbol2Elem );
1187
1188 return true;
1189}
1190
1191
1192bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext &context )
1193{
1194 mSettings.setHeight( itemElem.attribute( QStringLiteral( "height" ), QStringLiteral( "5.0" ) ).toDouble() );
1195 mSettings.setLabelBarSpace( itemElem.attribute( QStringLiteral( "labelBarSpace" ), QStringLiteral( "3.0" ) ).toDouble() );
1196 mSettings.setBoxContentSpace( itemElem.attribute( QStringLiteral( "boxContentSpace" ), QStringLiteral( "1.0" ) ).toDouble() );
1197 mSettings.setNumberOfSegments( itemElem.attribute( QStringLiteral( "numSegments" ), QStringLiteral( "2" ) ).toInt() );
1198 mSettings.setNumberOfSegmentsLeft( itemElem.attribute( QStringLiteral( "numSegmentsLeft" ), QStringLiteral( "0" ) ).toInt() );
1199 mSettings.setNumberOfSubdivisions( itemElem.attribute( QStringLiteral( "numSubdivisions" ), QStringLiteral( "1" ) ).toInt() );
1200 mSettings.setSubdivisionsHeight( itemElem.attribute( QStringLiteral( "subdivisionsHeight" ), QStringLiteral( "1.5" ) ).toDouble() );
1201 mSettings.setUnitsPerSegment( itemElem.attribute( QStringLiteral( "numUnitsPerSegment" ), QStringLiteral( "1.0" ) ).toDouble() );
1202 mSettings.setSegmentSizeMode( static_cast<Qgis::ScaleBarSegmentSizeMode >( itemElem.attribute( QStringLiteral( "segmentSizeMode" ), QStringLiteral( "0" ) ).toInt() ) );
1203 mSettings.setMinimumBarWidth( itemElem.attribute( QStringLiteral( "minBarWidth" ), QStringLiteral( "50" ) ).toDouble() );
1204 mSettings.setMaximumBarWidth( itemElem.attribute( QStringLiteral( "maxBarWidth" ), QStringLiteral( "150" ) ).toDouble() );
1205 mSegmentMillimeters = itemElem.attribute( QStringLiteral( "segmentMillimeters" ), QStringLiteral( "0.0" ) ).toDouble();
1206 mSettings.setMapUnitsPerScaleBarUnit( itemElem.attribute( QStringLiteral( "numMapUnitsPerScaleBarUnit" ), QStringLiteral( "1.0" ) ).toDouble() );
1207
1208 // default to horizontal bottom to keep same behavior for older projects
1209 mMethod = qgsEnumKeyToValue( itemElem.attribute( QStringLiteral( "method" ) ), Qgis::ScaleCalculationMethod::HorizontalBottom );
1210
1211 const QDomElement lineSymbolElem = itemElem.firstChildElement( QStringLiteral( "lineSymbol" ) );
1212 bool foundLineSymbol = false;
1213 if ( !lineSymbolElem.isNull() )
1214 {
1215 const QDomElement symbolElem = lineSymbolElem.firstChildElement( QStringLiteral( "symbol" ) );
1216 std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
1217 if ( lineSymbol )
1218 {
1219 mSettings.setLineSymbol( lineSymbol.release() );
1220 foundLineSymbol = true;
1221 }
1222 }
1223 const QDomElement divisionSymbolElem = itemElem.firstChildElement( QStringLiteral( "divisionLineSymbol" ) );
1224 if ( !divisionSymbolElem.isNull() )
1225 {
1226 const QDomElement symbolElem = divisionSymbolElem.firstChildElement( QStringLiteral( "symbol" ) );
1227 std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
1228 if ( lineSymbol )
1229 {
1230 mSettings.setDivisionLineSymbol( lineSymbol.release() );
1231 }
1232 }
1233 else if ( foundLineSymbol )
1234 {
1235 mSettings.setDivisionLineSymbol( mSettings.lineSymbol()->clone() );
1236 }
1237 const QDomElement subdivisionSymbolElem = itemElem.firstChildElement( QStringLiteral( "subdivisionLineSymbol" ) );
1238 if ( !subdivisionSymbolElem.isNull() )
1239 {
1240 const QDomElement symbolElem = subdivisionSymbolElem.firstChildElement( QStringLiteral( "symbol" ) );
1241 std::unique_ptr< QgsLineSymbol > lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
1242 if ( lineSymbol )
1243 {
1244 mSettings.setSubdivisionLineSymbol( lineSymbol.release() );
1245 }
1246 }
1247 else if ( foundLineSymbol )
1248 {
1249 mSettings.setSubdivisionLineSymbol( mSettings.lineSymbol()->clone() );
1250 }
1251
1252 if ( !foundLineSymbol )
1253 {
1254 // old project compatibility
1255 std::unique_ptr< QgsLineSymbol > lineSymbol = std::make_unique< QgsLineSymbol >();
1256 std::unique_ptr< QgsSimpleLineSymbolLayer > lineSymbolLayer = std::make_unique< QgsSimpleLineSymbolLayer >();
1257 lineSymbolLayer->setWidth( itemElem.attribute( QStringLiteral( "outlineWidth" ), QStringLiteral( "0.3" ) ).toDouble() );
1258 lineSymbolLayer->setWidthUnit( Qgis::RenderUnit::Millimeters );
1259 lineSymbolLayer->setPenJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( itemElem.attribute( QStringLiteral( "lineJoinStyle" ), QStringLiteral( "miter" ) ) ) );
1260 lineSymbolLayer->setPenCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( itemElem.attribute( QStringLiteral( "lineCapStyle" ), QStringLiteral( "square" ) ) ) );
1261
1262 //stroke color
1263 const QDomNodeList strokeColorList = itemElem.elementsByTagName( QStringLiteral( "strokeColor" ) );
1264 if ( !strokeColorList.isEmpty() )
1265 {
1266 const QDomElement strokeColorElem = strokeColorList.at( 0 ).toElement();
1267 bool redOk, greenOk, blueOk, alphaOk;
1268 int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
1269
1270 strokeRed = strokeColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1271 strokeGreen = strokeColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1272 strokeBlue = strokeColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1273 strokeAlpha = strokeColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1274
1275 if ( redOk && greenOk && blueOk && alphaOk )
1276 {
1277 lineSymbolLayer->setColor( QColor( strokeRed, strokeGreen, strokeBlue, strokeAlpha ) );
1278 }
1279 }
1280 else
1281 {
1282 lineSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral( "penColor" ), QStringLiteral( "#000000" ) ) ) );
1283 }
1284
1285 // need to translate the deprecated ScalebarLineWidth and ScalebarLineColor properties to symbol properties,
1286 // and then remove them from the scalebar so they don't interfere and apply to other compatibility workarounds
1291
1292 lineSymbol->changeSymbolLayer( 0, lineSymbolLayer.release() );
1293 mSettings.setLineSymbol( lineSymbol->clone() );
1294 mSettings.setDivisionLineSymbol( lineSymbol->clone() );
1295 mSettings.setSubdivisionLineSymbol( lineSymbol.release() );
1296 }
1297
1298 mSettings.setUnitLabel( itemElem.attribute( QStringLiteral( "unitLabel" ) ) );
1299
1300 const QDomNodeList textFormatNodeList = itemElem.elementsByTagName( QStringLiteral( "text-style" ) );
1301 if ( !textFormatNodeList.isEmpty() )
1302 {
1303 const QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
1304 mSettings.textFormat().readXml( textFormatElem, context );
1305 }
1306 else
1307 {
1308 QFont f;
1309 if ( !QgsFontUtils::setFromXmlChildNode( f, itemElem, QStringLiteral( "scaleBarFont" ) ) )
1310 {
1311 f.fromString( itemElem.attribute( QStringLiteral( "font" ), QString() ) );
1312 }
1313 mSettings.textFormat().setFont( f );
1314 if ( f.pointSizeF() > 0 )
1315 {
1316 mSettings.textFormat().setSize( f.pointSizeF() );
1318 }
1319 else if ( f.pixelSize() > 0 )
1320 {
1321 mSettings.textFormat().setSize( f.pixelSize() );
1323 }
1324 }
1325
1326 const QDomNodeList numericFormatNodeList = itemElem.elementsByTagName( QStringLiteral( "numericFormat" ) );
1327 if ( !numericFormatNodeList.isEmpty() )
1328 {
1329 const QDomElement numericFormatElem = numericFormatNodeList.at( 0 ).toElement();
1330 mSettings.setNumericFormat( QgsApplication::numericFormatRegistry()->createFromXml( numericFormatElem, context ) );
1331 }
1332
1333 const QDomElement fillSymbol1Elem = itemElem.firstChildElement( QStringLiteral( "fillSymbol1" ) );
1334 bool foundFillSymbol1 = false;
1335 if ( !fillSymbol1Elem.isNull() )
1336 {
1337 const QDomElement symbolElem = fillSymbol1Elem.firstChildElement( QStringLiteral( "symbol" ) );
1338 std::unique_ptr< QgsFillSymbol > fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
1339 if ( fillSymbol )
1340 {
1341 mSettings.setFillSymbol( fillSymbol.release() );
1342 foundFillSymbol1 = true;
1343 }
1344 }
1345 if ( !foundFillSymbol1 )
1346 {
1347 // old project compatibility
1348 std::unique_ptr< QgsFillSymbol > fillSymbol = std::make_unique< QgsFillSymbol >();
1349 std::unique_ptr< QgsSimpleFillSymbolLayer > fillSymbolLayer = std::make_unique< QgsSimpleFillSymbolLayer >();
1350 fillSymbolLayer->setStrokeStyle( Qt::NoPen );
1351
1352 //fill color
1353 const QDomNodeList fillColorList = itemElem.elementsByTagName( QStringLiteral( "fillColor" ) );
1354 if ( !fillColorList.isEmpty() )
1355 {
1356 const QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
1357 bool redOk, greenOk, blueOk, alphaOk;
1358 int fillRed, fillGreen, fillBlue, fillAlpha;
1359
1360 fillRed = fillColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1361 fillGreen = fillColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1362 fillBlue = fillColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1363 fillAlpha = fillColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1364
1365 if ( redOk && greenOk && blueOk && alphaOk )
1366 {
1367 fillSymbolLayer->setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
1368 }
1369 }
1370 else
1371 {
1372 fillSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral( "brushColor" ), QStringLiteral( "#000000" ) ) ) );
1373 }
1374
1375 // need to translate the deprecated ScalebarFillColor property to symbol properties,
1376 // and then remove them from the scalebar so they don't interfere and apply to other compatibility workarounds
1379
1380 fillSymbol->changeSymbolLayer( 0, fillSymbolLayer.release() );
1381 mSettings.setFillSymbol( fillSymbol.release() );
1382 }
1383
1384 const QDomElement fillSymbol2Elem = itemElem.firstChildElement( QStringLiteral( "fillSymbol2" ) );
1385 bool foundFillSymbol2 = false;
1386 if ( !fillSymbol2Elem.isNull() )
1387 {
1388 const QDomElement symbolElem = fillSymbol2Elem.firstChildElement( QStringLiteral( "symbol" ) );
1389 std::unique_ptr< QgsFillSymbol > fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
1390 if ( fillSymbol )
1391 {
1392 mSettings.setAlternateFillSymbol( fillSymbol.release() );
1393 foundFillSymbol2 = true;
1394 }
1395 }
1396 if ( !foundFillSymbol2 )
1397 {
1398 // old project compatibility
1399 std::unique_ptr< QgsFillSymbol > fillSymbol = std::make_unique< QgsFillSymbol >();
1400 std::unique_ptr< QgsSimpleFillSymbolLayer > fillSymbolLayer = std::make_unique< QgsSimpleFillSymbolLayer >();
1401 fillSymbolLayer->setStrokeStyle( Qt::NoPen );
1402
1403 //fill color 2
1404
1405 const QDomNodeList fillColor2List = itemElem.elementsByTagName( QStringLiteral( "fillColor2" ) );
1406 if ( !fillColor2List.isEmpty() )
1407 {
1408 const QDomElement fillColor2Elem = fillColor2List.at( 0 ).toElement();
1409 bool redOk, greenOk, blueOk, alphaOk;
1410 int fillRed, fillGreen, fillBlue, fillAlpha;
1411
1412 fillRed = fillColor2Elem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1413 fillGreen = fillColor2Elem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1414 fillBlue = fillColor2Elem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1415 fillAlpha = fillColor2Elem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1416
1417 if ( redOk && greenOk && blueOk && alphaOk )
1418 {
1419 fillSymbolLayer->setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
1420 }
1421 }
1422 else
1423 {
1424 fillSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral( "brush2Color" ), QStringLiteral( "#ffffff" ) ) ) );
1425 }
1426
1427 // need to translate the deprecated ScalebarFillColor2 property to symbol properties,
1428 // and then remove them from the scalebar so they don't interfere and apply to other compatibility workarounds
1431
1432 fillSymbol->changeSymbolLayer( 0, fillSymbolLayer.release() );
1433 mSettings.setAlternateFillSymbol( fillSymbol.release() );
1434
1435 }
1436
1437 //font color
1438 const QDomNodeList textColorList = itemElem.elementsByTagName( QStringLiteral( "textColor" ) );
1439 if ( !textColorList.isEmpty() )
1440 {
1441 const QDomElement textColorElem = textColorList.at( 0 ).toElement();
1442 bool redOk, greenOk, blueOk, alphaOk;
1443 int textRed, textGreen, textBlue, textAlpha;
1444
1445 textRed = textColorElem.attribute( QStringLiteral( "red" ) ).toDouble( &redOk );
1446 textGreen = textColorElem.attribute( QStringLiteral( "green" ) ).toDouble( &greenOk );
1447 textBlue = textColorElem.attribute( QStringLiteral( "blue" ) ).toDouble( &blueOk );
1448 textAlpha = textColorElem.attribute( QStringLiteral( "alpha" ) ).toDouble( &alphaOk );
1449
1450 if ( redOk && greenOk && blueOk && alphaOk )
1451 {
1452 mSettings.textFormat().setColor( QColor( textRed, textGreen, textBlue, textAlpha ) );
1453 }
1454 }
1455 else if ( itemElem.hasAttribute( QStringLiteral( "fontColor" ) ) )
1456 {
1457 QColor c;
1458 c.setNamedColor( itemElem.attribute( QStringLiteral( "fontColor" ), QStringLiteral( "#000000" ) ) );
1459 mSettings.textFormat().setColor( c );
1460 }
1461
1462 //style
1463 setStyle( itemElem.attribute( QStringLiteral( "style" ), QString() ) );
1464
1465 //call attemptResize after setStyle to ensure the appropriate size limitations are applied
1466 attemptResize( QgsLayoutSize::decodeSize( itemElem.attribute( QStringLiteral( "size" ) ) ) );
1467
1468 if ( itemElem.attribute( QStringLiteral( "unitType" ) ).isEmpty() )
1469 {
1471 switch ( itemElem.attribute( QStringLiteral( "units" ) ).toInt() )
1472 {
1473 case 0:
1475 break;
1476 case 1:
1478 break;
1479 case 2:
1481 break;
1482 case 3:
1484 break;
1485 }
1486 mSettings.setUnits( u );
1487 }
1488 else
1489 {
1490 mSettings.setUnits( QgsUnitTypes::decodeDistanceUnit( itemElem.attribute( QStringLiteral( "unitType" ) ) ) );
1491 }
1492
1493 mSettings.setLabelVerticalPlacement( static_cast< Qgis::ScaleBarDistanceLabelVerticalPlacement >( itemElem.attribute( QStringLiteral( "labelVerticalPlacement" ), QStringLiteral( "0" ) ).toInt() ) );
1494 mSettings.setLabelHorizontalPlacement( static_cast< Qgis::ScaleBarDistanceLabelHorizontalPlacement >( itemElem.attribute( QStringLiteral( "labelHorizontalPlacement" ), QStringLiteral( "0" ) ).toInt() ) );
1495
1496 mSettings.setAlignment( static_cast< Qgis::ScaleBarAlignment >( itemElem.attribute( QStringLiteral( "alignment" ), QStringLiteral( "0" ) ).toInt() ) );
1497
1498 //map
1499 disconnectCurrentMap();
1500 mMap = nullptr;
1501 mMapUuid = itemElem.attribute( QStringLiteral( "mapUuid" ) );
1502 return true;
1503}
1504
1505
1507{
1508 if ( mLayout && !mMapUuid.isEmpty() )
1509 {
1510 disconnectCurrentMap();
1511 mMap = qobject_cast< QgsLayoutItemMap * >( mLayout->itemByUuid( mMapUuid, true ) );
1512 if ( mMap )
1513 {
1514 connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
1515 connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
1516 }
1517 }
1518
1519 updateScale();
1520}
1521
1523{
1524 QgsStyleTextFormatEntity entity( mSettings.textFormat() );
1525 if ( !visitor->visit( QgsStyleEntityVisitorInterface::StyleLeaf( &entity, uuid(), displayName() ) ) )
1526 return false;
1527
1528 return true;
1529}
1530
ScaleBarDistanceLabelHorizontalPlacement
Scale bar distance label horizontal placement.
Definition qgis.h:4965
ScaleBarDistanceLabelVerticalPlacement
Scale bar distance label vertical placement.
Definition qgis.h:4951
@ Millimeters
Millimeters.
ScaleBarAlignment
Scalebar alignment.
Definition qgis.h:4922
DistanceUnit
Units of distance.
Definition qgis.h:4669
@ 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:4906
@ HorizontalTop
Calculate horizontally, across top of map.
@ HorizontalMiddle
Calculate horizontally, across midle of map.
@ 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:4937
@ 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.
This class 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.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
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.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
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)
Adds a message to the log instance (and creates it if necessary).
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: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.
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double width() const
Returns the width of the rectangle.
double xMaximum() const
Returns the x maximum value (right side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
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.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
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:1459
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:6127
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6494
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6108
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6493
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:5917
#define QgsDebugError(str)
Definition qgslogger.h:38
const QgsCoordinateReferenceSystem & crs
Contains parameters regarding scalebar calculations.
Flags flags
Scalebar renderer flags.
QSizeF size
Destination size for scalebar.
bool isValid() const
Returns true if the context has valid settings.
double segmentWidth
The width, in millimeters, of each individual segment drawn.
Contains information relating to the style entity currently being visited.