QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
25 #include <QFontMetricsF>
35 QPainter *painter = context.
painter();
46 double xOffset = fontMetrics.width( firstLabel ) / 2.0;
60 double currentLabelNumber = 0.0;
63 int segmentCounter = 0;
65 QString currentNumericLabel;
66 QList<double> positions =
segmentPositions( context, scaleContext, settings );
81 for (
int i = 0; i < positions.size(); ++i )
83 if ( segmentCounter == 0 && nSegmentsLeft > 0 )
86 currentNumericLabel = firstLabel;
88 else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft )
90 currentLabelNumber = 0.0;
93 if ( segmentCounter >= nSegmentsLeft )
99 if ( ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) && ( currentNumericLabel != QStringLiteral(
"0" ) || drawZero ) )
105 if ( segmentCounter == 0 )
123 if ( segmentCounter >= nSegmentsLeft )
131 if ( !positions.isEmpty() )
148 - fontMetrics.width( currentNumericLabel ) / 2.0 );
177 firstLabelWidth = ( firstLabelWidth - scaleContext.
segmentWidth ) / 2;
181 firstLabelWidth = 0.0;
186 firstLabelWidth = firstLabelWidth / 2;
192 QString largestLabel = largestNumberLabel +
' ' + settings.
unitLabel();
193 double largestLabelWidth;
199 largestLabelWidth = ( largestLabelWidth - scaleContext.
segmentWidth ) / 2;
203 largestLabelWidth = 0.0;
215 double width = firstLabelWidth + totalBarLength + 2 * settings.
pen().widthF() + largestLabelWidth + 2 * settings.
boxContentSpace();
220 return QSizeF( width, height );
233 firstLabelWidth = ( firstLabelWidth - scaleContext.
segmentWidth ) / 2;
237 firstLabelWidth = 0.0;
242 firstLabelWidth = firstLabelWidth / 2;
248 QString largestLabel = largestNumberLabel +
' ' + settings.
unitLabel();
249 double largestLabelWidth;
256 largestLabelWidth = ( largestLabelWidth - scaleContext.
segmentWidth ) / 2;
260 largestLabelWidth = 0.0;
275 double width = firstLabelWidth + totalBarLength + 2 * lineWidth + largestLabelWidth + 2 * settings.
boxContentSpace();
283 height = settings.
height();
289 return QSizeF( width, height );
325 firstLabelWidth = ( firstLabelWidth - scaleContext.
segmentWidth ) / 2;
329 firstLabelWidth = 0.0;
334 firstLabelWidth = firstLabelWidth / 2;
336 return firstLabelWidth;
341 QList<double> positions;
353 positions << currentXCoord;
354 currentXCoord += leftSegmentSize;
360 positions << currentXCoord;
368 QList<double> positions;
381 positions << currentXCoord;
382 currentXCoord += leftSegmentSize;
388 positions << currentXCoord;
396 QList<double> widths;
405 widths << leftSegmentSize;
double height() const
Returns the scalebar height (in millimeters).
double mapUnitsPerScaleBarUnit() const
Returns the number of map units per scale bar unit used by the scalebar.
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
Contains parameters regarding scalebar calculations.
QgsExpressionContext & expressionContext()
Gets the expression context.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
@ AlignCenter
Center align.
QList< double > segmentWidths(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of widths of each segment of the scalebar.
@ RenderMillimeters
Millimeters.
@ LabelBelowSegment
Labels are drawn below the scalebar.
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format)
Returns the font metrics for the given text format, when rendered in the specified render context.
@ LabelCenteredEdge
Labels are drawn centered relative to segment's edge.
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.
double subdivisionsHeight() const
Returns the scalebar subdivisions height (in millimeters) for segments included in the right part of ...
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.
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
#define Q_NOWARN_DEPRECATED_POP
virtual bool applyDefaultSettings(QgsScaleBarSettings &settings) const
Applies any default settings relating to the scalebar to the passed settings object.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters.
int numberOfSegments() const
Returns the number of segments included in the scalebar.
LabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
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.
int numberOfSegmentsLeft() const
Returns the number of segments included in the left part of the scalebar.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode=Point, QFontMetricsF *fontMetrics=nullptr)
Returns the height 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.
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
QString firstLabelString(const QgsScaleBarSettings &settings) const
Returns the text used for the first label in the scalebar.
Single scope for storing variables and functions for use within a QgsExpressionContext....
virtual int sortKey() const
Returns a sorting key value, where renderers with a lower sort key will be shown earlier in lists.
double segmentWidth
The width, in millimeters, of each individual segment drawn.
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...
virtual Flags flags() const
Returns the scalebar rendering flags, which dictates the renderer's behavior.
int numberOfSubdivisions() const
Returns the number of subdivisions for segments included in the right part of the scalebar (only used...
double unitsPerSegment() const
Returns the number of scalebar units per segment.
@ FlagUsesSubdivisionsHeight
Renderer uses the scalebar subdivisions height (see QgsScaleBarSettings::subdivisionsHeight() )
@ LabelCenteredSegment
Labels are drawn centered relative to segment.
static double fontAscentMM(const QFont &font)
Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.
Q_DECL_DEPRECATED QFont font() const
Returns the font used for drawing text in the scalebar.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
LabelHorizontalPlacement labelHorizontalPlacement() const
Returns the horizontal placement of text labels.
QPainter * painter()
Returns the destination QPainter for the render operation.
QString unitLabel() const
Returns the label for units.
Single variable definition for use within a QgsExpressionContextScope.
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
#define Q_NOWARN_DEPRECATED_PUSH
Flags flags
Scalebar renderer flags.
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 ...
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.