41 QPainter *painter = context.
painter();
67 const QString unitLabel = settings.
unitLabel();
68 const bool hasUnitLabel = !unitLabel.isEmpty();
70 const bool hasUnitLabelAbove
77 double barTopPosition = scaledBoxContentSpace;
78 if ( hasUnitLabelAbove )
80 barTopPosition += fontMetrics.ascent() + scaledLabelBarSpace;
82 if ( hasDistanceLabelsAbove )
84 barTopPosition += fontMetrics.ascent() + scaledLabelBarSpace;
87 const double barBottomPosition = barTopPosition + scaledHeight;
88 const double barMiddleY = barTopPosition + scaledHeight / 2.0;
90 double distanceLabelY = 0.0;
91 if ( hasDistanceLabelsAbove )
93 distanceLabelY = barTopPosition - scaledLabelBarSpace;
95 else if ( hasDistanceLabelsBelow )
97 distanceLabelY = barBottomPosition + scaledLabelBarSpace + fontMetrics.ascent();
100 const QList<double> positions =
segmentPositions( context, scaleContext, settings );
108 for (
int i = 0; i < positions.size(); ++i )
110 const bool isFirstLabel = ( i == 0 );
111 const bool isLastLabel = ( i == positions.size() - 1 );
113 QString currentNumericLabel;
114 if ( i < nSegmentsLeft )
118 currentNumericLabel = firstLabel;
128 const double val = ( i - nSegmentsLeft + 1 ) * settings.
unitsPerSegment();
132 const double segmentWidthMM = ( i < nSegmentsLeft && nSegmentsLeft > 0 ) ? ( scaleContext.
segmentWidth / nSegmentsLeft ) : scaleContext.
segmentWidth;
136 QString labelText = currentNumericLabel;
142 labelText = unitLabel + labelText;
147 labelText = labelText + unitLabel;
159 double currentLabelNumber = 0.0;
160 int segmentCounter = 0;
161 QString currentNumericLabel;
163 for (
int i = 0; i < positions.size(); ++i )
165 if ( segmentCounter == 0 && nSegmentsLeft > 0 )
168 currentNumericLabel = firstLabel;
170 else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft )
173 currentLabelNumber = 0.0;
176 if ( segmentCounter >= nSegmentsLeft )
182 if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft )
184 const bool isFirstLabel = ( segmentCounter == 0 );
195 pos.setX( pos.x() + fontMetrics.horizontalAdvance( currentNumericLabel ) / 2.0 );
200 QString labelText = currentNumericLabel;
205 labelText = unitLabel + labelText;
209 labelText = labelText + unitLabel;
216 if ( segmentCounter >= nSegmentsLeft )
224 if ( !positions.isEmpty() )
227 QString lastLabelText = currentNumericLabel;
235 lastLabelText = unitLabel + lastLabelText;
239 lastLabelText = lastLabelText + unitLabel;
244 pos.setY( distanceLabelY );
254 - fontMetrics.horizontalAdvance( currentNumericLabel ) / 2.0
269 if ( hasUnitLabel && !positions.isEmpty() )
274 const double unitLabelYAbove = scaledBoxContentSpace + fontMetrics.ascent();
276 double unitLabelYBelow = barBottomPosition + scaledLabelBarSpace + fontMetrics.ascent();
277 if ( hasDistanceLabelsBelow )
279 unitLabelYBelow += fontMetrics.ascent() + scaledLabelBarSpace;
284 const QPointF pos( barLeftX - scaledLabelBarSpace, barMiddleY + ( fontMetrics.ascent() - fontMetrics.descent() ) / 2.0 );
289 const QPointF pos( barRightX + scaledLabelBarSpace, barMiddleY + ( fontMetrics.ascent() - fontMetrics.descent() ) / 2.0 );
294 const QPointF pos( barLeftX, unitLabelYAbove );
299 const QPointF pos( ( barLeftX + barRightX ) / 2.0, unitLabelYAbove );
304 const QPointF pos( barRightX, unitLabelYAbove );
309 const QPointF pos( barLeftX, unitLabelYBelow );
314 const QPointF pos( ( barLeftX + barRightX ) / 2.0, unitLabelYBelow );
319 const QPointF pos( barRightX, unitLabelYBelow );
325 const QPointF pos( firstDivCenterX, barMiddleY + ( fontMetrics.ascent() - fontMetrics.descent() ) / 2.0 );
403 const QString unitLabel = settings.
unitLabel();
404 const bool hasUnitLabel = !unitLabel.isEmpty();
412 double firstLabelWidth = 0.0;
415 const QString fullFirstLabel = unitLabel + firstNumLabel;
421 firstLabelWidth = ( fullWidth > firstSegmentWidth ) ? ( fullWidth - firstSegmentWidth ) / 2.0 : 0.0;
425 firstLabelWidth = fullWidth - numWidth / 2.0;
435 firstLabelWidth = ( fullWidth > firstSegmentWidth ) ? ( fullWidth - firstSegmentWidth ) / 2.0 : 0.0;
439 firstLabelWidth = fullWidth / 2.0;
446 QString largestLabel = largestNumberLabel;
451 largestLabel = unitLabel + largestLabel;
455 largestLabel = largestLabel + unitLabel;
459 double largestLabelWidth;
466 largestLabelWidth = ( largestLabelWidth - scaleContext.
segmentWidth ) / 2.0;
470 largestLabelWidth = 0.0;
496 double leftExtension = firstLabelWidth;
497 double rightExtension = largestLabelWidth;
506 leftExtension = std::max( leftExtension, unitLabelWidth + labelBarSpaceMM );
510 rightExtension = std::max( rightExtension, unitLabelWidth + labelBarSpaceMM );
514 leftExtension = std::max( leftExtension, unitLabelWidth / 2.0 );
518 rightExtension = std::max( rightExtension, unitLabelWidth / 2.0 );
522 const double centerOverhang = ( unitLabelWidth - totalBarLength ) / 2.0;
523 if ( centerOverhang > 0 )
525 leftExtension = std::max( leftExtension, centerOverhang );
526 rightExtension = std::max( rightExtension, centerOverhang );
531 const double width = leftExtension + totalBarLength + 2.0 * lineWidth + rightExtension + 2.0 * settings.
boxContentSpace();
539 barHeight = settings.
height();
550 bool hasUnitLabelAbove =
false;
551 bool hasUnitLabelBelow =
false;
562 double topSpace = 0.0;
563 if ( hasDistanceLabelsAbove )
567 if ( hasUnitLabelAbove )
573 double bottomSpace = 0.0;
574 if ( hasDistanceLabelsBelow )
578 if ( hasUnitLabelBelow )
584 height += topSpace + bottomSpace;
586 return QSizeF( width, height );
633 const QString unitLabel = settings.
unitLabel();
634 const bool hasUnitLabel = !unitLabel.isEmpty();
643 double firstLabelWidth = 0.0;
647 const QString fullFirstLabel = unitLabel + firstNumLabel;
653 if ( fullWidth > firstSegmentWidthPx )
655 firstLabelWidth = ( fullWidth - firstSegmentWidthPx ) / 2.0;
659 firstLabelWidth = 0.0;
664 firstLabelWidth = fullWidth - numWidth / 2.0;
674 if ( fullWidth > firstSegmentWidthPx )
676 firstLabelWidth = ( fullWidth - firstSegmentWidthPx ) / 2.0;
680 firstLabelWidth = 0.0;
685 firstLabelWidth = fullWidth / 2.0;
689 double xOffset = firstLabelWidth;
698 xOffset = std::max( xOffset, unitLabelWidth + scaledLabelBarSpace );
702 xOffset = std::max( xOffset, unitLabelWidth / 2.0 );
708 const double centerOverhang = ( unitLabelWidth - totalBarLengthPx ) / 2.0;
709 if ( centerOverhang > 0 )
711 xOffset = std::max( xOffset, centerOverhang );
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).
static double textWidth(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, QFontMetricsF *fontMetrics=nullptr)
Returns the width of a text based on a given format.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor=1.0)
Returns the font metrics for the given text format, when rendered in the specified render context.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Point, QFontMetricsF *fontMetrics=nullptr, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), double maxLineWidth=0)
Returns the height of a text based on a given format.