21 #include <QFontMetricsF> 31 QPainter *painter = context.
painter();
39 double xOffset = fontMetrics.width( firstLabel ) / 2.0;
43 double currentLabelNumber = 0.0;
46 int segmentCounter = 0;
47 QString currentNumericLabel;
51 for (
int i = 0; i < positions.size(); ++i )
53 if ( segmentCounter == 0 && nSegmentsLeft > 0 )
56 currentNumericLabel = firstLabel;
58 else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft )
60 currentLabelNumber = 0;
63 if ( segmentCounter >= nSegmentsLeft )
68 if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft )
72 QStringList() << currentNumericLabel, context, format );
75 if ( segmentCounter >= nSegmentsLeft )
83 if ( !positions.isEmpty() )
90 - fontMetrics.width( currentNumericLabel ) / 2.0,
92 QStringList() << ( currentNumericLabel +
' ' + settings.
unitLabel() ), context, format );
109 QString largestNumberLabel = QString::number( largestLabelNumber );
110 QString largestLabel = QString::number( largestLabelNumber ) +
' ' + settings.
unitLabel();
115 double width = firstLabelLeft + totalBarLength + 2 * settings.
pen().widthF() + largestLabelWidth + 2 * settings.
boxContentSpace();
118 return QSizeF( width, height );
129 return QStringLiteral(
"0" );
149 QList<double> positions;
158 positions << currentXCoord;
159 currentXCoord += leftSegmentSize;
165 positions << currentXCoord;
173 QList<double> widths;
182 widths << leftSegmentSize;
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
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 ...
double segmentWidth
The width, in millimeters, of each individual segment drawn.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
#define Q_NOWARN_DEPRECATED_PUSH
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.
QList< double > segmentPositions(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of positions for each segment within the scalebar.
int numberOfSegments() const
Returns the number of segments included in the scalebar.
double unitsPerSegment() const
Returns the number of scalebar units per segment.
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format)
Returns the font metrics for the given text format, when rendered in the specified render context...
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
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...
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.
#define Q_NOWARN_DEPRECATED_POP
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters. ...
Contains information about the context of a rendering operation.
double height() const
Returns the scalebar height (in millimeters).
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...
QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
QString firstLabelString(const QgsScaleBarSettings &settings) const
Returns the text used for the first label in the scalebar.
Q_DECL_DEPRECATED QFont font() const
Returns the font used for drawing text in the scalebar.
Container for all settings relating to text rendering.
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.
Q_DECL_DEPRECATED double firstLabelXOffset(const QgsScaleBarSettings &settings) const
Returns the x-offset (in millimeters) used for the first label in the scalebar.
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...
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
QString unitLabel() const
Returns the label for units.
QList< double > segmentWidths(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of widths of each segment of the scalebar.
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Contains parameters regarding scalebar calculations.