QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
42 #include <QDomDocument>
43 #include <QDomElement>
44 #include <QFontMetricsF>
86 std::unique_ptr< QgsLineSymbol > sym( mSettings.
lineSymbol()->
clone() );
99 std::unique_ptr< QgsFillSymbol > sym( mSettings.
fillSymbol()->
clone() );
116 mStyle->draw( context.
renderContext(), mSettings, createScaleContext() );
138 refreshSegmentMillimeters();
151 refreshSegmentMillimeters();
163 refreshSegmentMillimeters();
175 refreshSegmentMillimeters();
265 disconnectCurrentMap();
275 connect( mMap, &QObject::destroyed,
this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
277 refreshSegmentMillimeters();
281 void QgsLayoutItemScaleBar::disconnectCurrentMap()
289 disconnect( mMap, &QObject::destroyed,
this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
396 bool forceUpdate =
false;
440 refreshNumberOfSegmentsLeft( &context );
446 refreshNumberOfSegmentsRight( &context );
473 refreshUnitsPerSegment( &context );
479 refreshMinimumBarWidth( &context );
485 refreshMaximumBarWidth( &context );
517 void QgsLayoutItemScaleBar::refreshSegmentMillimeters()
522 const QRectF composerItemRect = mMap->rect();
529 mSegmentMillimeters = composerItemRect.width() / mapWidth() * mSettings.
unitsPerSegment();
537 mSegmentMillimeters = 0;
543 const double minUnitsPerSeg = ( mSettings.
minimumBarWidth() * mapWidth() ) / ( nSegments * composerItemRect.width() );
544 const double maxUnitsPerSeg = ( mSettings.
maximumBarWidth() * mapWidth() ) / ( nSegments * composerItemRect.width() );
546 mSegmentMillimeters = composerItemRect.width() / mapWidth() * mSettings.
unitsPerSegment();
554 double QgsLayoutItemScaleBar::mapWidth()
const
564 return mapExtent.
width();
583 scaleContext.
size = rect().size();
585 scaleContext.
scale = mMap ? mMap->
scale() : 1.0;
586 scaleContext.
flags = mStyle->flags();
614 refreshSegmentMillimeters();
664 mStyle = std::make_unique< QgsSingleBoxScaleBarRenderer >();
671 const QString defaultFontString = settings.
value( QStringLiteral(
"LayoutDesigner/defaultFont" ), QVariant(),
QgsSettings::Gui ).toString();
674 if ( !defaultFontString.isEmpty() )
676 f.setFamily( defaultFontString );
710 const double widthInSelectedUnits = mapWidth();
711 const double initialUnitsPerSegment = widthInSelectedUnits / 10.0;
716 if ( initialUnitsPerSegment > 1000.0 )
724 if ( initialUnitsPerSegment > 5419.95 )
742 double upperMagnitudeMultiplier = 1.0;
743 const double widthInSelectedUnits = mapWidth();
744 const double initialUnitsPerSegment = widthInSelectedUnits / 10.0;
748 upperMagnitudeMultiplier = 1;
750 const double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
751 const int segmentMagnitude = std::floor( std::log10( segmentWidth ) );
752 double unitsPerSegment = upperMagnitudeMultiplier * ( std::pow( 10.0, segmentMagnitude ) );
753 const double multiplier = std::floor( ( widthInSelectedUnits / (
unitsPerSegment * 10.0 ) ) / 2.5 ) * 2.5;
755 if ( multiplier > 0 )
766 refreshSegmentMillimeters();
777 const double widthMM = mStyle->calculateBoxSize( context, mSettings, createScaleContext() ).width();
788 if ( mStyle && mStyle->id() != QLatin1String(
"Numeric" ) )
792 QgsLayoutItem::update();
795 void QgsLayoutItemScaleBar::updateScale()
797 refreshSegmentMillimeters();
799 if ( mStyle && mStyle->id() != QLatin1String(
"Numeric" ) )
812 mStyle = std::move( renderer );
926 return mSettings.
pen();
933 return mSettings.
brush();
940 return mSettings.
brush2();
946 composerScaleBarElem.setAttribute( QStringLiteral(
"height" ), QString::number( mSettings.
height() ) );
947 composerScaleBarElem.setAttribute( QStringLiteral(
"labelBarSpace" ), QString::number( mSettings.
labelBarSpace() ) );
948 composerScaleBarElem.setAttribute( QStringLiteral(
"boxContentSpace" ), QString::number( mSettings.
boxContentSpace() ) );
949 composerScaleBarElem.setAttribute( QStringLiteral(
"numSegments" ), mSettings.
numberOfSegments() );
950 composerScaleBarElem.setAttribute( QStringLiteral(
"numSegmentsLeft" ), mSettings.
numberOfSegmentsLeft() );
951 composerScaleBarElem.setAttribute( QStringLiteral(
"numSubdivisions" ), mSettings.
numberOfSubdivisions() );
952 composerScaleBarElem.setAttribute( QStringLiteral(
"subdivisionsHeight" ), mSettings.
subdivisionsHeight() );
953 composerScaleBarElem.setAttribute( QStringLiteral(
"numUnitsPerSegment" ), QString::number( mSettings.
unitsPerSegment() ) );
954 composerScaleBarElem.setAttribute( QStringLiteral(
"segmentSizeMode" ), mSettings.
segmentSizeMode() );
955 composerScaleBarElem.setAttribute( QStringLiteral(
"minBarWidth" ), mSettings.
minimumBarWidth() );
956 composerScaleBarElem.setAttribute( QStringLiteral(
"maxBarWidth" ), mSettings.
maximumBarWidth() );
957 composerScaleBarElem.setAttribute( QStringLiteral(
"segmentMillimeters" ), QString::number( mSegmentMillimeters ) );
958 composerScaleBarElem.setAttribute( QStringLiteral(
"numMapUnitsPerScaleBarUnit" ), QString::number( mSettings.
mapUnitsPerScaleBarUnit() ) );
961 composerScaleBarElem.appendChild( textElem );
965 composerScaleBarElem.setAttribute( QStringLiteral(
"outlineWidth" ), QString::number( mSettings.
lineWidth() ) );
969 QDomElement strokeColorElem = doc.createElement( QStringLiteral(
"strokeColor" ) );
970 strokeColorElem.setAttribute( QStringLiteral(
"red" ), QString::number( mSettings.
lineColor().red() ) );
971 strokeColorElem.setAttribute( QStringLiteral(
"green" ), QString::number( mSettings.
lineColor().green() ) );
972 strokeColorElem.setAttribute( QStringLiteral(
"blue" ), QString::number( mSettings.
lineColor().blue() ) );
973 strokeColorElem.setAttribute( QStringLiteral(
"alpha" ), QString::number( mSettings.
lineColor().alpha() ) );
974 composerScaleBarElem.appendChild( strokeColorElem );
977 composerScaleBarElem.setAttribute( QStringLiteral(
"unitLabel" ), mSettings.
unitLabel() );
980 QDomElement numericFormatElem = doc.createElement( QStringLiteral(
"numericFormat" ) );
982 composerScaleBarElem.appendChild( numericFormatElem );
987 composerScaleBarElem.setAttribute( QStringLiteral(
"style" ), mStyle->id() );
993 composerScaleBarElem.setAttribute( QStringLiteral(
"mapUuid" ), mMap->
uuid() );
1002 QDomElement fillColorElem = doc.createElement( QStringLiteral(
"fillColor" ) );
1003 fillColorElem.setAttribute( QStringLiteral(
"red" ), QString::number( mSettings.
fillColor().red() ) );
1004 fillColorElem.setAttribute( QStringLiteral(
"green" ), QString::number( mSettings.
fillColor().green() ) );
1005 fillColorElem.setAttribute( QStringLiteral(
"blue" ), QString::number( mSettings.
fillColor().blue() ) );
1006 fillColorElem.setAttribute( QStringLiteral(
"alpha" ), QString::number( mSettings.
fillColor().alpha() ) );
1007 composerScaleBarElem.appendChild( fillColorElem );
1010 QDomElement fillColor2Elem = doc.createElement( QStringLiteral(
"fillColor2" ) );
1011 fillColor2Elem.setAttribute( QStringLiteral(
"red" ), QString::number( mSettings.
fillColor2().red() ) );
1012 fillColor2Elem.setAttribute( QStringLiteral(
"green" ), QString::number( mSettings.
fillColor2().green() ) );
1013 fillColor2Elem.setAttribute( QStringLiteral(
"blue" ), QString::number( mSettings.
fillColor2().blue() ) );
1014 fillColor2Elem.setAttribute( QStringLiteral(
"alpha" ), QString::number( mSettings.
fillColor2().alpha() ) );
1015 composerScaleBarElem.appendChild( fillColor2Elem );
1020 composerScaleBarElem.setAttribute( QStringLiteral(
"labelVerticalPlacement" ), QString::number(
static_cast< int >( mSettings.
labelVerticalPlacement() ) ) );
1021 composerScaleBarElem.setAttribute( QStringLiteral(
"labelHorizontalPlacement" ), QString::number(
static_cast< int >( mSettings.
labelHorizontalPlacement() ) ) );
1024 composerScaleBarElem.setAttribute( QStringLiteral(
"alignment" ), QString::number(
static_cast< int >( mSettings.
alignment() ) ) );
1026 QDomElement
lineSymbol = doc.createElement( QStringLiteral(
"lineSymbol" ) );
1032 composerScaleBarElem.appendChild(
lineSymbol );
1034 QDomElement divisionSymbol = doc.createElement( QStringLiteral(
"divisionLineSymbol" ) );
1039 divisionSymbol.appendChild( divisionSymbolElem );
1040 composerScaleBarElem.appendChild( divisionSymbol );
1042 QDomElement subdivisionSymbol = doc.createElement( QStringLiteral(
"subdivisionLineSymbol" ) );
1047 subdivisionSymbol.appendChild( subdivisionSymbolElem );
1048 composerScaleBarElem.appendChild( subdivisionSymbol );
1050 QDomElement fillSymbol1Elem = doc.createElement( QStringLiteral(
"fillSymbol1" ) );
1055 fillSymbol1Elem.appendChild( symbol1Elem );
1056 composerScaleBarElem.appendChild( fillSymbol1Elem );
1058 QDomElement fillSymbol2Elem = doc.createElement( QStringLiteral(
"fillSymbol2" ) );
1063 fillSymbol2Elem.appendChild( symbol2Elem );
1064 composerScaleBarElem.appendChild( fillSymbol2Elem );
1072 mSettings.
setHeight( itemElem.attribute( QStringLiteral(
"height" ), QStringLiteral(
"5.0" ) ).toDouble() );
1073 mSettings.
setLabelBarSpace( itemElem.attribute( QStringLiteral(
"labelBarSpace" ), QStringLiteral(
"3.0" ) ).toDouble() );
1074 mSettings.
setBoxContentSpace( itemElem.attribute( QStringLiteral(
"boxContentSpace" ), QStringLiteral(
"1.0" ) ).toDouble() );
1075 mSettings.
setNumberOfSegments( itemElem.attribute( QStringLiteral(
"numSegments" ), QStringLiteral(
"2" ) ).toInt() );
1076 mSettings.
setNumberOfSegmentsLeft( itemElem.attribute( QStringLiteral(
"numSegmentsLeft" ), QStringLiteral(
"0" ) ).toInt() );
1077 mSettings.
setNumberOfSubdivisions( itemElem.attribute( QStringLiteral(
"numSubdivisions" ), QStringLiteral(
"1" ) ).toInt() );
1078 mSettings.
setSubdivisionsHeight( itemElem.attribute( QStringLiteral(
"subdivisionsHeight" ), QStringLiteral(
"1.5" ) ).toDouble() );
1079 mSettings.
setUnitsPerSegment( itemElem.attribute( QStringLiteral(
"numUnitsPerSegment" ), QStringLiteral(
"1.0" ) ).toDouble() );
1081 mSettings.
setMinimumBarWidth( itemElem.attribute( QStringLiteral(
"minBarWidth" ), QStringLiteral(
"50" ) ).toDouble() );
1082 mSettings.
setMaximumBarWidth( itemElem.attribute( QStringLiteral(
"maxBarWidth" ), QStringLiteral(
"150" ) ).toDouble() );
1083 mSegmentMillimeters = itemElem.attribute( QStringLiteral(
"segmentMillimeters" ), QStringLiteral(
"0.0" ) ).toDouble();
1084 mSettings.
setMapUnitsPerScaleBarUnit( itemElem.attribute( QStringLiteral(
"numMapUnitsPerScaleBarUnit" ), QStringLiteral(
"1.0" ) ).toDouble() );
1086 const QDomElement lineSymbolElem = itemElem.firstChildElement( QStringLiteral(
"lineSymbol" ) );
1087 bool foundLineSymbol =
false;
1088 if ( !lineSymbolElem.isNull() )
1090 const QDomElement symbolElem = lineSymbolElem.firstChildElement( QStringLiteral(
"symbol" ) );
1091 std::unique_ptr< QgsLineSymbol >
lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
1095 foundLineSymbol =
true;
1098 const QDomElement divisionSymbolElem = itemElem.firstChildElement( QStringLiteral(
"divisionLineSymbol" ) );
1099 if ( !divisionSymbolElem.isNull() )
1101 const QDomElement symbolElem = divisionSymbolElem.firstChildElement( QStringLiteral(
"symbol" ) );
1102 std::unique_ptr< QgsLineSymbol >
lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
1108 else if ( foundLineSymbol )
1112 const QDomElement subdivisionSymbolElem = itemElem.firstChildElement( QStringLiteral(
"subdivisionLineSymbol" ) );
1113 if ( !subdivisionSymbolElem.isNull() )
1115 const QDomElement symbolElem = subdivisionSymbolElem.firstChildElement( QStringLiteral(
"symbol" ) );
1116 std::unique_ptr< QgsLineSymbol >
lineSymbol( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context ) );
1122 else if ( foundLineSymbol )
1127 if ( !foundLineSymbol )
1130 std::unique_ptr< QgsLineSymbol >
lineSymbol = std::make_unique< QgsLineSymbol >();
1131 std::unique_ptr< QgsSimpleLineSymbolLayer > lineSymbolLayer = std::make_unique< QgsSimpleLineSymbolLayer >();
1132 lineSymbolLayer->setWidth( itemElem.attribute( QStringLiteral(
"outlineWidth" ), QStringLiteral(
"0.3" ) ).toDouble() );
1138 const QDomNodeList strokeColorList = itemElem.elementsByTagName( QStringLiteral(
"strokeColor" ) );
1139 if ( !strokeColorList.isEmpty() )
1141 const QDomElement strokeColorElem = strokeColorList.at( 0 ).toElement();
1142 bool redOk, greenOk, blueOk, alphaOk;
1143 int strokeRed, strokeGreen, strokeBlue, strokeAlpha;
1145 strokeRed = strokeColorElem.attribute( QStringLiteral(
"red" ) ).toDouble( &redOk );
1146 strokeGreen = strokeColorElem.attribute( QStringLiteral(
"green" ) ).toDouble( &greenOk );
1147 strokeBlue = strokeColorElem.attribute( QStringLiteral(
"blue" ) ).toDouble( &blueOk );
1148 strokeAlpha = strokeColorElem.attribute( QStringLiteral(
"alpha" ) ).toDouble( &alphaOk );
1150 if ( redOk && greenOk && blueOk && alphaOk )
1152 lineSymbolLayer->setColor( QColor( strokeRed, strokeGreen, strokeBlue, strokeAlpha ) );
1157 lineSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral(
"penColor" ), QStringLiteral(
"#000000" ) ) ) );
1173 mSettings.
setUnitLabel( itemElem.attribute( QStringLiteral(
"unitLabel" ) ) );
1175 const QDomNodeList textFormatNodeList = itemElem.elementsByTagName( QStringLiteral(
"text-style" ) );
1176 if ( !textFormatNodeList.isEmpty() )
1178 const QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
1186 f.fromString( itemElem.attribute( QStringLiteral(
"font" ), QString() ) );
1189 if ( f.pointSizeF() > 0 )
1194 else if ( f.pixelSize() > 0 )
1201 const QDomNodeList numericFormatNodeList = itemElem.elementsByTagName( QStringLiteral(
"numericFormat" ) );
1202 if ( !numericFormatNodeList.isEmpty() )
1204 const QDomElement numericFormatElem = numericFormatNodeList.at( 0 ).toElement();
1208 const QDomElement fillSymbol1Elem = itemElem.firstChildElement( QStringLiteral(
"fillSymbol1" ) );
1209 bool foundFillSymbol1 =
false;
1210 if ( !fillSymbol1Elem.isNull() )
1212 const QDomElement symbolElem = fillSymbol1Elem.firstChildElement( QStringLiteral(
"symbol" ) );
1213 std::unique_ptr< QgsFillSymbol >
fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
1217 foundFillSymbol1 =
true;
1220 if ( !foundFillSymbol1 )
1223 std::unique_ptr< QgsFillSymbol >
fillSymbol = std::make_unique< QgsFillSymbol >();
1224 std::unique_ptr< QgsSimpleFillSymbolLayer > fillSymbolLayer = std::make_unique< QgsSimpleFillSymbolLayer >();
1225 fillSymbolLayer->setStrokeStyle( Qt::NoPen );
1228 const QDomNodeList fillColorList = itemElem.elementsByTagName( QStringLiteral(
"fillColor" ) );
1229 if ( !fillColorList.isEmpty() )
1231 const QDomElement fillColorElem = fillColorList.at( 0 ).toElement();
1232 bool redOk, greenOk, blueOk, alphaOk;
1233 int fillRed, fillGreen, fillBlue, fillAlpha;
1235 fillRed = fillColorElem.attribute( QStringLiteral(
"red" ) ).toDouble( &redOk );
1236 fillGreen = fillColorElem.attribute( QStringLiteral(
"green" ) ).toDouble( &greenOk );
1237 fillBlue = fillColorElem.attribute( QStringLiteral(
"blue" ) ).toDouble( &blueOk );
1238 fillAlpha = fillColorElem.attribute( QStringLiteral(
"alpha" ) ).toDouble( &alphaOk );
1240 if ( redOk && greenOk && blueOk && alphaOk )
1242 fillSymbolLayer->setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
1247 fillSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral(
"brushColor" ), QStringLiteral(
"#000000" ) ) ) );
1259 const QDomElement fillSymbol2Elem = itemElem.firstChildElement( QStringLiteral(
"fillSymbol2" ) );
1260 bool foundFillSymbol2 =
false;
1261 if ( !fillSymbol2Elem.isNull() )
1263 const QDomElement symbolElem = fillSymbol2Elem.firstChildElement( QStringLiteral(
"symbol" ) );
1264 std::unique_ptr< QgsFillSymbol >
fillSymbol( QgsSymbolLayerUtils::loadSymbol<QgsFillSymbol>( symbolElem, context ) );
1268 foundFillSymbol2 =
true;
1271 if ( !foundFillSymbol2 )
1274 std::unique_ptr< QgsFillSymbol >
fillSymbol = std::make_unique< QgsFillSymbol >();
1275 std::unique_ptr< QgsSimpleFillSymbolLayer > fillSymbolLayer = std::make_unique< QgsSimpleFillSymbolLayer >();
1276 fillSymbolLayer->setStrokeStyle( Qt::NoPen );
1280 const QDomNodeList fillColor2List = itemElem.elementsByTagName( QStringLiteral(
"fillColor2" ) );
1281 if ( !fillColor2List.isEmpty() )
1283 const QDomElement fillColor2Elem = fillColor2List.at( 0 ).toElement();
1284 bool redOk, greenOk, blueOk, alphaOk;
1285 int fillRed, fillGreen, fillBlue, fillAlpha;
1287 fillRed = fillColor2Elem.attribute( QStringLiteral(
"red" ) ).toDouble( &redOk );
1288 fillGreen = fillColor2Elem.attribute( QStringLiteral(
"green" ) ).toDouble( &greenOk );
1289 fillBlue = fillColor2Elem.attribute( QStringLiteral(
"blue" ) ).toDouble( &blueOk );
1290 fillAlpha = fillColor2Elem.attribute( QStringLiteral(
"alpha" ) ).toDouble( &alphaOk );
1292 if ( redOk && greenOk && blueOk && alphaOk )
1294 fillSymbolLayer->setColor( QColor( fillRed, fillGreen, fillBlue, fillAlpha ) );
1299 fillSymbolLayer->setColor( QColor( itemElem.attribute( QStringLiteral(
"brush2Color" ), QStringLiteral(
"#ffffff" ) ) ) );
1313 const QDomNodeList textColorList = itemElem.elementsByTagName( QStringLiteral(
"textColor" ) );
1314 if ( !textColorList.isEmpty() )
1316 const QDomElement textColorElem = textColorList.at( 0 ).toElement();
1317 bool redOk, greenOk, blueOk, alphaOk;
1318 int textRed, textGreen, textBlue, textAlpha;
1320 textRed = textColorElem.attribute( QStringLiteral(
"red" ) ).toDouble( &redOk );
1321 textGreen = textColorElem.attribute( QStringLiteral(
"green" ) ).toDouble( &greenOk );
1322 textBlue = textColorElem.attribute( QStringLiteral(
"blue" ) ).toDouble( &blueOk );
1323 textAlpha = textColorElem.attribute( QStringLiteral(
"alpha" ) ).toDouble( &alphaOk );
1325 if ( redOk && greenOk && blueOk && alphaOk )
1327 mSettings.
textFormat().
setColor( QColor( textRed, textGreen, textBlue, textAlpha ) );
1330 else if ( itemElem.hasAttribute( QStringLiteral(
"fontColor" ) ) )
1333 c.setNamedColor( itemElem.attribute( QStringLiteral(
"fontColor" ), QStringLiteral(
"#000000" ) ) );
1338 setStyle( itemElem.attribute( QStringLiteral(
"style" ), QString() ) );
1343 if ( itemElem.attribute( QStringLiteral(
"unitType" ) ).isEmpty() )
1346 switch ( itemElem.attribute( QStringLiteral(
"units" ) ).toInt() )
1374 disconnectCurrentMap();
1376 mMapUuid = itemElem.attribute( QStringLiteral(
"mapUuid" ) );
1383 if (
mLayout && !mMapUuid.isEmpty() )
1385 disconnectCurrentMap();
1386 mMap = qobject_cast< QgsLayoutItemMap * >(
mLayout->itemByUuid( mMapUuid,
true ) );
1390 connect( mMap, &QObject::destroyed,
this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
SegmentSizeMode
Modes for setting size for scale bar segments.
void setLabelBarSpace(double space)
Sets the spacing (in millimeters) between labels and the scalebar.
void setNumberOfSegments(int segments)
Sets the number of segments included in the scalebar.
Q_DECL_DEPRECATED QColor fillColor2() const
Returns the secondary color used for fills in the scalebar.
@ ScalebarFillColor2
Scalebar secondary fill color (deprecated, use data defined properties on scalebar fill symbol 2 inst...
Q_DECL_DEPRECATED QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
double height() const
Returns the scalebar height (in millimeters).
Q_DECL_DEPRECATED void setFontColor(const QColor &color)
Sets the color used for drawing text in the scalebar.
const QgsLayout * layout() const
Returns the layout the object is attached to.
Q_DECL_DEPRECATED void setFont(const QFont &font)
Sets the font used for drawing text in the scalebar.
double mapUnitsPerScaleBarUnit() const
Returns the number of map units per scale bar unit used by the scalebar.
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.
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
void setFont(const QFont &font)
Sets the font used for rendering text.
A store for object properties.
Contains parameters regarding scalebar calculations.
void refreshItemSize()
Refreshes an item's size by rechecking it against any possible item fixed or minimum sizes.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the size.
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.
void setAlignment(QgsScaleBarSettings::Alignment alignment)
Sets the scalebar alignment.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsLayoutItemScaleBar(QgsLayout *layout)
Constructor for QgsLayoutItemScaleBar, with the specified parent layout.
The class is used as a container of context for various read/write operations on other objects.
@ DistanceUnknownUnit
Unknown distance unit.
@ ScalebarRightSegments
Number of segments on the right of 0 (since QGIS 3.26)
void setSegmentSizeMode(QgsScaleBarSettings::SegmentSizeMode mode)
Sets the size mode for scale bar segments.
void setAlignment(Alignment alignment)
Sets the scalebar alignment.
@ ScalebarLineColor
Scalebar line color (deprecated, use data defined properties on scalebar line symbol instead)
void extentChanged()
Emitted when the map's extent changes.
const QgsCoordinateReferenceSystem & crs
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).
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
QgsLineSymbol * clone() const override
Returns a deep copy of this symbol.
@ PropertyFillColor
Fill color.
void setUnitsPerSegment(double units)
Sets the number of scalebar units per segment.
@ RenderPoints
Points (e.g., for font sizes)
Q_DECL_DEPRECATED void setLineJoinStyle(Qt::PenJoinStyle style)
Sets the join style used when drawing the lines in the scalebar.
void setBoxContentSpace(double space)
Sets the space (margin) between the scalebar box and content in millimeters.
void setLabelHorizontalPlacement(LabelHorizontalPlacement placement)
Sets the horizontal placement of text labels.
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
static QString encodePenCapStyle(Qt::PenCapStyle style)
Q_DECL_DEPRECATED double lineWidth() const
Returns the line width in millimeters for lines in the scalebar.
void setLinkedMap(QgsLayoutItemMap *map)
Sets the map item linked to the scalebar.
Contains settings and helpers relating to a render of a QgsLayoutItem.
static Q_INVOKABLE QString toAbbreviatedString(QgsUnitTypes::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
void finalizeRestoreFromXml() override
Called after all pending items have been restored from XML.
Q_DECL_DEPRECATED void setLineCapStyle(Qt::PenCapStyle style)
Sets the cap style used when drawing the lines in the scalebar.
QgsUnitTypes::DistanceUnit units() const
Returns the distance units used by the scalebar.
void setLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar (only used for some scalebar types).
QgsPropertyCollection mDataDefinedProperties
Q_DECL_DEPRECATED void setLineWidth(double width)
Sets the line width in millimeters for lines in the scalebar.
Contains information about the context of a rendering operation.
QgsFillSymbol * fillSymbol() const
Returns the primary fill symbol used to render the scalebar (only used for some scalebar types).
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
QgsLineSymbol * divisionLineSymbol() const
Returns the line symbol used to render the scalebar divisions (only used for some scalebar types).
Q_DECL_DEPRECATED void setLineColor(const QColor &color)
Sets the color used for lines in the scalebar.
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
@ RenderMillimeters
Millimeters.
This class is a composition of two QSettings instances:
LabelHorizontalPlacement
Label horizontal placement.
void setMaximumBarWidth(double maxWidth)
Sets the maximum width (in millimeters) for scale bar segments.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
static QgsLayoutSize decodeSize(const QString &string)
Decodes a size from a string.
void setLabelHorizontalPlacement(QgsScaleBarSettings::LabelHorizontalPlacement placement)
Sets the horizontal placement of text labels.
DistanceUnit
Units of distance.
void setMaximumBarWidth(double width)
Sets the maximum width (in millimeters) for scale bar segments.
@ PropertyStrokeColor
Stroke color.
@ SegmentSizeFitWidth
Scale bar segment size is calculated to fit a size range.
bool applyDefaultRendererSettings(QgsScaleBarRenderer *renderer)
Applies any default settings relating to the specified renderer to the item.
void changed()
Emitted when the object's properties change.
void setHeight(double height)
Sets the scalebar height (in millimeters).
@ DistanceKilometers
Kilometers.
QgsTextFormat textFormat() const
Returns the text format used for drawing text in the scalebar.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
A rectangle specified with double values.
Contains information relating to the style entity currently being visited.
void setNumberOfSubdivisions(int subdivisions)
Sets the number of subdivisions for segments included in the right part of the scalebar (only used fo...
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
Q_DECL_DEPRECATED QFont font() const
Returns the font used for drawing text in the scalebar.
bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
SegmentSizeMode segmentSizeMode() const
Returns the size mode for the scale bar segments.
QColor color() const
Returns the color that text will be rendered in.
void setUnits(QgsUnitTypes::DistanceUnit units)
Sets the distance units used by the scalebar.
double subdivisionsHeight() const
Returns the scalebar subdivisions height (in millimeters) for segments included in the right part of ...
Q_DECL_DEPRECATED void setFont(const QFont &font)
Sets the font used for drawing text in the scalebar.
QSizeF size
Destination size for scalebar.
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
static Q_INVOKABLE QgsUnitTypes::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=nullptr)
Decodes a distance unit from a string.
void setNumberOfSegments(int segments)
Sets the number of segments included in the scalebar.
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
#define Q_NOWARN_DEPRECATED_POP
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
static Q_INVOKABLE double fromUnitToUnitFactor(QgsUnitTypes::DistanceUnit fromUnit, QgsUnitTypes::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
Container for all settings relating to text rendering.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
Q_DECL_DEPRECATED QColor fillColor() const
Returns the color used for fills in the scalebar.
virtual bool applyDefaultSettings(QgsScaleBarSettings &settings) const
Applies any default settings relating to the scalebar to the passed settings object.
@ AllProperties
All properties for item.
Q_DECL_DEPRECATED QColor lineColor() const
Returns the color used for lines in the scalebar.
Alignment alignment() const
Returns the scalebar alignment.
double unitsPerSegment() const
Returns the number of scalebar units per segment.
void setLabelVerticalPlacement(QgsScaleBarSettings::LabelVerticalPlacement placement)
Sets the vertical placement of text labels.
bool changeSymbolLayer(int index, QgsSymbolLayer *layer)
Deletes the current layer at the specified index and replaces it with layer.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters.
Q_DECL_DEPRECATED QBrush brush2() const
Returns the secondary brush for the scalebar.
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
static double calculatePrettySize(double minimumSize, double maximumSize)
Calculates a "pretty" size which falls between the range [minimumSize, maximumSize].
void setHeight(double height)
Sets the scalebar height (in millimeters).
Q_DECL_DEPRECATED void setFillColor2(const QColor &color)
Sets the secondary color used for fills in the scalebar.
Q_DECL_DEPRECATED Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
@ SegmentSizeFixed
Scale bar segment size is fixed to a map unit.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
@ DistanceDegrees
Degrees, for planar geographic CRS distance measurements.
Alignment
Scalebar alignment.
int numberOfSegments() const
Returns the number of segments included in the scalebar.
void setUnitLabel(const QString &label)
Sets the label for units.
void setTextFormat(const QgsTextFormat &format)
Sets the text format used for drawing text in the scalebar.
void setSubdivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
LabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
void setUnits(QgsUnitTypes::DistanceUnit units)
Sets the distance units used by the scalebar.
@ ScalebarRightSegmentSubdivisions
Number of subdivisions per segment on right of 0 (since QGIS 3.26)
QgsFillSymbol * fillSymbol() const
Returns the primary fill symbol used to render the scalebar (only used for some scalebar types).
Q_DECL_DEPRECATED QColor fillColor() const
Returns the color used for fills in the scalebar.
@ ScalebarMinimumWidth
Scalebar segment minimum width (since QGIS 3.26)
void setSubdivisionsHeight(double height)
Sets the scalebar subdivisions height (in millimeters) for segments included in the right part of the...
double height() const
Returns the scalebar height (in millimeters).
@ ScalebarSegmentWidth
Scalebar width in map units of a single segment (since QGIS 3.26)
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
Q_DECL_DEPRECATED void setFillColor(const QColor &color)
Sets the color used for fills in the scalebar.
@ ScalebarLeftSegments
Number of segments on the left of 0 (since QGIS 3.26)
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
QgsUnitTypes::DistanceUnit lengthUnits() const
Returns the units of distance for length calculations made by this object.
double scale
Scale denominator.
Q_DECL_DEPRECATED Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
Q_DECL_DEPRECATED QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
A line symbol type, for rendering LineString and MultiLineString geometries.
QgsRectangle extent() const
Returns the current map extent.
@ DistanceFeet
Imperial feet.
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).
Abstract base class for scale bar renderers.
A text format entity for QgsStyle databases.
Q_DECL_DEPRECATED void setLineColor(const QColor &color)
Sets the color used for lines in the scalebar.
bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
double opacity() const
Returns the text's opacity.
QgsUnitTypes::DistanceUnit units() const
Returns the distance units used by the scalebar.
void setMinimumBarWidth(double minWidth)
Sets the minimum width (in millimeters) for scale bar segments.
Q_DECL_DEPRECATED void setLineWidth(double width)
Sets the line width in millimeters for lines in the scalebar.
int numberOfSegmentsLeft() const
Returns the number of segments included in the left part of the scalebar.
QgsLineSymbol * subdivisionLineSymbol() const
Returns the line symbol used to render the scalebar subdivisions (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 readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
QIcon icon() const override
Returns the item's icon.
double maximumBarWidth() const
Returns the maximum width (in millimeters) for scale bar segments.
Base class for graphical items within a QgsLayout.
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
void setLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar (only used for some scalebar types).
@ PropertyStrokeWidth
Stroke width.
Q_DECL_DEPRECATED void setFillColor(const QColor &color)
Sets the color used for fills in the scalebar.
void applyDefaultSize(QgsUnitTypes::DistanceUnit units=QgsUnitTypes::DistanceMeters)
Applies the default size to the scale bar (scale bar 1/5 of map item width)
void setSubdivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
@ ScalebarHeight
Scalebar height (since QGIS 3.26)
Q_DECL_DEPRECATED QColor lineColor() const
Returns the color used for lines in the scalebar.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
void setStyle(const QString &name)
Sets the scale bar style by name.
This class represents a coordinate reference system (CRS).
@ ScalebarLineWidth
Scalebar line width (deprecated, use data defined properties on scalebar line symbol instead)
Q_DECL_DEPRECATED QBrush brush() const
Returns the primary brush used for filling the scalebar.
@ LayoutScaleBar
Scale bar item.
void setLabelVerticalPlacement(LabelVerticalPlacement placement)
Sets the vertical placement of text labels.
Q_DECL_DEPRECATED void setLineCapStyle(Qt::PenCapStyle style)
Sets the cap style used when drawing the 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 segmentWidth
The width, in millimeters, of each individual segment drawn.
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the scalebar.
A class to represent a 2D point.
static Qt::PenCapStyle decodePenCapStyle(const QString &str)
Layout graphical items for displaying a map.
Q_DECL_DEPRECATED void setFillColor2(const QColor &color)
Sets the secondary color used for fills in the scalebar.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the object's property collection, used for data defined overrides.
Q_DECL_DEPRECATED Qt::PenJoinStyle lineJoinStyle() const
Returns the join style used for drawing lines in the scalebar.
void resizeToMinimumWidth()
Resizes the scale bar to its minimum width, without changing the height.
void setWidth(const double width)
Sets the width for the size.
Q_DECL_DEPRECATED double lineWidth() const
Returns the line width in millimeters for lines in the scalebar.
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.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
virtual QString uuid() const
Returns the item identification string.
void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties) override
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
Q_DECL_DEPRECATED Qt::PenJoinStyle lineJoinStyle() const
Returns the join style used for drawing lines in the scalebar.
QgsUnitTypes::DistanceUnit mapUnits
QgsLineSymbol * divisionLineSymbol() const
Returns the line symbol used to render the scalebar divisions (only used for some scalebar types).
QgsUnitTypes::DistanceUnit guessUnits() const
Attempts to guess the most reasonable unit choice for the scalebar, given the current linked map's sc...
void setMinimumBarWidth(double width)
Sets the minimum width (in millimeters) for scale bar segments.
QPointer< QgsLayout > mLayout
QgsLineSymbol * subdivisionLineSymbol() const
Returns the line symbol used to render the scalebar subdivisions (only used for some scalebar types).
int numberOfSubdivisions() const
Returns the number of subdivisions for segments included in the right part of the scalebar (only used...
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
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
void setBoxContentSpace(double space)
Sets the space (margin) between the scalebar box and content in millimeters.
virtual QString displayName() const
Gets item display name.
Q_DECL_DEPRECATED QColor fillColor2() const
Returns the secondary color used for fills in the scalebar.
double unitsPerSegment() const
Returns the number of scalebar units per segment.
void setNumberOfSubdivisions(int subdivisions)
Sets the number of subdivisions for segments included in the right part of the scalebar (only used fo...
void setOpacity(double opacity)
Sets the text's opacity.
Q_DECL_DEPRECATED QBrush brush() const
Returns the primary brush for the scalebar.
void setFillSymbol(QgsFillSymbol *symbol)
Sets the primary fill symbol used to render the scalebar (only used for some scalebar types).
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
double scale() const
Returns the map scale.
Q_DECL_DEPRECATED void setLineJoinStyle(Qt::PenJoinStyle style)
Sets the join style used when drawing the lines in the scalebar.
void setUnitsPerSegment(double units)
Sets the number of scalebar units per segment.
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
double minimumBarWidth() const
Returns the minimum width (in millimeters) for scale bar segments.
static Qt::PenJoinStyle decodePenJoinStyle(const QString &str)
QFont font() const
Returns the font used for rendering text.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
QgsLayoutSize minimumSize() const override
Returns the minimum allowed size of the item, if applicable, or an empty size if item can be freely r...
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the size of rendered text.
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
@ DistanceMiles
Terrestrial miles.
double maximumBarWidth() const
Returns the maximum width (in millimeters) for scale bar segments.
QgsFillSymbol * alternateFillSymbol() const
Returns the secondary fill symbol used to render the scalebar (only used for some scalebar types).
void setDivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar divisions (only used for some scalebar types).
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
static QString encodePenJoinStyle(Qt::PenJoinStyle style)
void setDivisionLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used to render the scalebar divisions (only used for some scalebar types).
@ DistanceNauticalMiles
Nautical miles.
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
@ LayoutMillimeters
Millimeters.
QString style() const
Returns the scale bar style name.
A layout item subclass for scale bars.
LabelHorizontalPlacement labelHorizontalPlacement() const
Returns the horizontal placement of text labels.
int type() const override
void setSize(double size)
Sets the size for rendered text.
void setAlternateFillSymbol(QgsFillSymbol *symbol)
Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types).
QString unitLabel() const
Returns the label for units.
void setNumberOfSegmentsLeft(int segments)
Sets the number of segments included in the left part of the scalebar.
Q_DECL_DEPRECATED QColor fontColor() const
Returns the color used for drawing text in the scalebar.
void applyDefaultSettings()
Applies the default scalebar settings to the scale bar.
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
#define Q_NOWARN_DEPRECATED_PUSH
Q_DECL_DEPRECATED QBrush brush2() const
Returns the secondary brush for the scalebar.
@ ScalebarFillColor
Scalebar fill color (deprecated, use data defined properties on scalebar fill symbol 1 instead)
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
ExportLayerBehavior exportLayerBehavior() const override
Returns the behavior of this item during exporting to layered exports (e.g.
QgsFillSymbol * alternateFillSymbol() const
Returns the secondary fill symbol used to render the scalebar (only used for some scalebar types).
double minimumBarWidth() const
Returns the minimum width (in millimeters) for scale bar segments.
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the scalebar.
@ CanGroupWithItemsOfSameType
Item can only be placed on layers with other items of the same type, but multiple items of this type ...
void setSegmentSizeMode(SegmentSizeMode mode)
Sets the size mode for scale bar segments.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
void setTextFormat(const QgsTextFormat &format)
Sets the text format used for drawing text in the scalebar.
@ ScalebarSubdivisionHeight
Scalebar subdivision height (since QGIS 3.26)
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
void setNumberOfSegmentsLeft(int segments)
Sets the number of segments included in the left part of the scalebar.
Flags flags
Scalebar renderer flags.
@ ScalebarMaximumWidth
Scalebar segment maximum width (since QGIS 3.26)
DataDefinedProperty
Data defined properties for different item types.
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.
void setAlternateFillSymbol(QgsFillSymbol *symbol)
Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types).
void setUnitLabel(const QString &label)
Sets the label for units.
static QgsScaleBarRendererRegistry * scaleBarRendererRegistry()
Gets the registry of available scalebar renderers.
static QgsLayoutItemScaleBar * create(QgsLayout *layout)
Returns a new scale bar item for the specified layout.
LabelVerticalPlacement
Label vertical placement.