27#include <QFontMetricsF>
37 QPainter *painter = context.
painter();
48 const double xOffset = fontMetrics.horizontalAdvance( firstLabel ) / 2.0;
62 double currentLabelNumber = 0.0;
65 int segmentCounter = 0;
67 QString currentNumericLabel;
68 const QList<double> positions =
segmentPositions( context, scaleContext, settings );
83 for (
int i = 0; i < positions.size(); ++i )
85 if ( segmentCounter == 0 && nSegmentsLeft > 0 )
88 currentNumericLabel = firstLabel;
90 else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft )
92 currentLabelNumber = 0.0;
95 if ( segmentCounter >= nSegmentsLeft )
101 if ( ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) && ( currentNumericLabel != QLatin1String(
"0" ) || drawZero ) )
107 if ( segmentCounter == 0 )
125 if ( segmentCounter >= nSegmentsLeft )
133 if ( !positions.isEmpty() )
150 - fontMetrics.horizontalAdvance( currentNumericLabel ) / 2.0 );
179 firstLabelWidth = ( firstLabelWidth - scaleContext.
segmentWidth ) / 2;
183 firstLabelWidth = 0.0;
188 firstLabelWidth = firstLabelWidth / 2;
194 const QString largestLabel = largestNumberLabel +
' ' + settings.
unitLabel();
195 double largestLabelWidth;
201 largestLabelWidth = ( largestLabelWidth - scaleContext.
segmentWidth ) / 2;
205 largestLabelWidth = 0.0;
217 const double width = firstLabelWidth + totalBarLength + 2 * settings.
pen().widthF() + largestLabelWidth + 2 * settings.
boxContentSpace();
222 return QSizeF( width, height );
235 firstLabelWidth = ( firstLabelWidth - scaleContext.
segmentWidth ) / 2;
239 firstLabelWidth = 0.0;
244 firstLabelWidth = firstLabelWidth / 2;
250 const QString largestLabel = largestNumberLabel +
' ' + settings.
unitLabel();
251 double largestLabelWidth;
258 largestLabelWidth = ( largestLabelWidth - scaleContext.
segmentWidth ) / 2;
262 largestLabelWidth = 0.0;
272 const double totalBarLength = std::isnan( scaleContext.
segmentWidth ) ? 0
279 const double width = firstLabelWidth + totalBarLength + 2 * lineWidth + largestLabelWidth + 2 * settings.
boxContentSpace();
287 height = settings.
height();
293 return QSizeF( width, height );
329 firstLabelWidth = ( firstLabelWidth - scaleContext.
segmentWidth ) / 2;
333 firstLabelWidth = 0.0;
338 firstLabelWidth = firstLabelWidth / 2;
340 return firstLabelWidth;
345 QList<double> positions;
357 positions << currentXCoord;
358 currentXCoord += leftSegmentSize;
364 positions << currentXCoord;
372 QList<double> positions;
385 positions << currentXCoord;
386 currentXCoord += leftSegmentSize;
392 positions << currentXCoord;
400 QList<double> widths;
409 widths << leftSegmentSize;
@ CenteredSegment
Labels are drawn centered relative to segment.
@ CenteredEdge
Labels are drawn centered relative to segment's edge.
@ BelowSegment
Labels are drawn below the scalebar.
@ Millimeters
Millimeters.
RAII class to pop scope from an expression context on destruction.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
static double fontAscentMM(const QFont &font)
Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.
static double textWidthMM(const QFont &font, const QString &text)
Calculate a font width in millimeters for a text string, including workarounds for QT font rendering ...
A context for numeric formats.
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.
QgsExpressionContext & expressionContext()
Gets the expression context.
virtual Flags flags() const
Returns the scalebar rendering flags, which dictates the renderer's behavior.
virtual bool applyDefaultSettings(QgsScaleBarSettings &settings) const
Applies any default settings relating to the scalebar to the passed settings object.
virtual int sortKey() const
Returns a sorting key value, where renderers with a lower sort key will be shown earlier in lists.
void drawDefaultLabels(QgsRenderContext &context, const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const
Draws default scalebar labels using the specified settings and scaleContext to a destination render c...
virtual Q_DECL_DEPRECATED QSizeF calculateBoxSize(const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const
Calculates the required box size (in millimeters) for a scalebar using the specified settings and sca...
QString firstLabelString(const QgsScaleBarSettings &settings) const
Returns the text used for the first label in the scalebar.
QList< double > segmentWidths(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of widths of each segment of the scalebar.
Q_DECL_DEPRECATED QList< double > segmentPositions(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of positions for each segment within the scalebar.
Q_DECL_DEPRECATED double firstLabelXOffset(const QgsScaleBarSettings &settings) const
Returns the x-offset (in millimeters) used for the first label in the scalebar.
@ FlagUsesSubdivisionsHeight
Renderer uses the scalebar subdivisions height (see QgsScaleBarSettings::subdivisionsHeight() )
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
double subdivisionsHeight() const
Returns the scalebar subdivisions height (in millimeters) for segments included in the right part of ...
QgsLineSymbol * lineSymbol() const
Returns the line 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.
int numberOfSegments() const
Returns the number of segments included in the scalebar.
Qgis::ScaleBarDistanceLabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
double unitsPerSegment() const
Returns the number of scalebar units per segment.
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.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters.
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 QFont font() const
Returns the font used for drawing text in the scalebar.
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.
double mapUnitsPerScaleBarUnit() const
Returns the number of map units per scale bar unit used by the scalebar.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
Container for all settings relating to text rendering.
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
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.
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
Single variable definition for use within a QgsExpressionContextScope.
Contains parameters regarding scalebar calculations.
Flags flags
Scalebar renderer flags.
bool isValid() const
Returns true if the context has valid settings.
double segmentWidth
The width, in millimeters, of each individual segment drawn.