22 #include <QFontMetricsF> 32 QPainter *painter = context.
painter();
43 double xOffset = fontMetrics.width( firstLabel ) / 2.0;
49 double currentLabelNumber = 0.0;
52 int segmentCounter = 0;
54 QString currentNumericLabel;
68 for (
int i = 0; i < positions.size(); ++i )
70 if ( segmentCounter == 0 && nSegmentsLeft > 0 )
73 currentNumericLabel = firstLabel;
75 else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft )
77 currentLabelNumber = 0.0;
80 if ( segmentCounter >= nSegmentsLeft )
86 if ( ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) && ( currentNumericLabel != QStringLiteral(
"0" ) || drawZero ) )
92 if ( segmentCounter == 0 )
110 if ( segmentCounter >= nSegmentsLeft )
118 if ( !positions.isEmpty() )
135 - fontMetrics.width( currentNumericLabel ) / 2.0 );
154 firstLabelWidth = ( firstLabelWidth - scaleContext.
segmentWidth ) / 2;
158 firstLabelWidth = 0.0;
163 firstLabelWidth = firstLabelWidth / 2;
168 QString largestNumberLabel = QString::number( largestLabelNumber );
169 QString largestLabel = largestNumberLabel +
' ' + settings.
unitLabel();
170 double largestLabelWidth;
176 largestLabelWidth = ( largestLabelWidth - scaleContext.
segmentWidth ) / 2;
180 largestLabelWidth = 0.0;
190 double width = firstLabelWidth + totalBarLength + 2 * settings.
pen().widthF() + largestLabelWidth + 2 * settings.
boxContentSpace();
193 return QSizeF( width, height );
204 return QStringLiteral(
"0" );
224 firstLabelWidth = ( firstLabelWidth - scaleContext.
segmentWidth ) / 2;
228 firstLabelWidth = 0.0;
233 firstLabelWidth = firstLabelWidth / 2;
235 return firstLabelWidth;
240 QList<double> positions;
249 positions << currentXCoord;
250 currentXCoord += leftSegmentSize;
256 positions << currentXCoord;
264 QList<double> widths;
273 widths << leftSegmentSize;
Single variable definition for use within a QgsExpressionContextScope.
double mapUnitsPerScaleBarUnit() const
Returns the number of map units per scale bar unit used by the scalebar.
QString unitLabel() const
Returns the label for units.
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
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 ...
QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
double segmentWidth
The width, in millimeters, of each individual segment drawn.
#define Q_NOWARN_DEPRECATED_PUSH
Q_DECL_DEPRECATED QFont font() const
Returns the font used for drawing text in the scalebar.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
Q_DECL_DEPRECATED double firstLabelXOffset(const QgsScaleBarSettings &settings) const
Returns the x-offset (in millimeters) used for the first label in the scalebar.
double height() const
Returns the scalebar height (in millimeters).
LabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
int numberOfSegments() const
Returns the number of segments included in the scalebar.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters. ...
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
QList< double > segmentWidths(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of widths of each segment of the scalebar.
Single scope for storing variables and functions for use within a QgsExpressionContext.
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format)
Returns the font metrics for the given text format, when rendered in the specified render context...
LabelHorizontalPlacement labelHorizontalPlacement() const
Returns the horizontal placement of text labels.
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...
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true)
Draws text within a rectangle using the specified settings.
Labels are drawn centered relative to segment's edge.
QgsExpressionContext & expressionContext()
Gets the expression context.
QString firstLabelString(const QgsScaleBarSettings &settings) const
Returns the text used for the first label in the scalebar.
#define Q_NOWARN_DEPRECATED_POP
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QList< double > segmentPositions(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of positions for each segment within the scalebar.
QPainter * painter()
Returns the destination QPainter for the render operation.
static double fontAscentMM(const QFont &font)
Calculates a font ascent in millimeters, including workarounds for QT font rendering issues...
Labels are drawn centered relative to segment.
double unitsPerSegment() const
Returns the number of scalebar units per segment.
virtual 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...
Container for all settings relating to text rendering.
int numberOfSegmentsLeft() const
Returns the number of segments included in the left part of the scalebar.
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.
Labels are drawn below the scalebar.
RAII class to pop scope from an expression context on destruction.
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Contains parameters regarding scalebar calculations.