QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgshollowscalebarrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshollowscalebarrenderer.cpp
3  --------------------------------
4  begin : March 2020
5  copyright : (C) 2020 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
18 #include "qgsscalebarsettings.h"
19 #include "qgslayoututils.h"
20 #include "qgssymbol.h"
21 #include "qgsfillsymbollayer.h"
22 #include "qgstextrenderer.h"
23 #include <QList>
24 #include <QPainter>
25 
27 {
28  return QStringLiteral( "hollow" );
29 }
30 
32 {
33  return QObject::tr( "Hollow" );
34 }
35 
36 QgsScaleBarRenderer::Flags QgsHollowScaleBarRenderer::flags() const
37 {
48 }
49 
51 {
52  return 8;
53 }
54 
56 {
57  return new QgsHollowScaleBarRenderer( *this );
58 }
59 
60 void QgsHollowScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
61 {
62  if ( !context.painter() )
63  {
64  return;
65  }
66  QPainter *painter = context.painter();
67 
68  const double scaledLabelBarSpace = context.convertToPainterUnits( settings.labelBarSpace(), QgsUnitTypes::RenderMillimeters );
69  const double scaledBoxContentSpace = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters );
70  const QFontMetricsF fontMetrics = QgsTextRenderer::fontMetrics( context, settings.textFormat() );
71  const double barTopPosition = scaledBoxContentSpace + ( settings.labelVerticalPlacement() == QgsScaleBarSettings::LabelAboveSegment ? fontMetrics.ascent() + scaledLabelBarSpace : 0 );
72  const double barHeight = context.convertToPainterUnits( settings.height(), QgsUnitTypes::RenderMillimeters );
73 
74  painter->save();
75  context.setPainterFlagsUsingContext( painter );
76 
77  std::unique_ptr< QgsLineSymbol > lineSymbol( settings.lineSymbol()->clone() );
78  lineSymbol->startRender( context );
79 
80  std::unique_ptr< QgsFillSymbol > fillSymbol1( settings.fillSymbol()->clone() );
81  fillSymbol1->startRender( context );
82 
83  std::unique_ptr< QgsFillSymbol > fillSymbol2( settings.alternateFillSymbol()->clone() );
84  fillSymbol2->startRender( context );
85 
86  painter->setPen( Qt::NoPen );
87  painter->setBrush( Qt::NoBrush );
88 
89  bool useColor = true; //alternate brush color/white
90  const double xOffset = firstLabelXOffset( settings, context, scaleContext );
91 
92  const QList<double> positions = segmentPositions( context, scaleContext, settings );
93  const QList<double> widths = segmentWidths( scaleContext, settings );
94 
95  // draw the fill
96  double minX = 0;
97  double maxX = 0;
98  QgsFillSymbol *currentSymbol = nullptr;
99  for ( int i = 0; i < positions.size(); ++i )
100  {
101  if ( useColor ) //alternating colors
102  {
103  currentSymbol = fillSymbol1.get();
104  }
105  else //secondary fill
106  {
107  currentSymbol = fillSymbol2.get();
108  }
109 
110  const double thisX = context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset;
111  const double thisWidth = context.convertToPainterUnits( widths.at( i ), QgsUnitTypes::RenderMillimeters );
112 
113  if ( i == 0 )
114  minX = thisX;
115  if ( i == positions.size() - 1 )
116  maxX = thisX + thisWidth;
117 
118  QRectF segmentRect( thisX, barTopPosition, thisWidth, barHeight );
119  currentSymbol->renderPolygon( QPolygonF()
120  << segmentRect.topLeft()
121  << segmentRect.topRight()
122  << segmentRect.bottomRight()
123  << segmentRect.bottomLeft()
124  << segmentRect.topLeft(), nullptr, nullptr, context );
125  useColor = !useColor;
126  }
127 
128  // and then the lines
129  // note that we do this layer-by-layer, to avoid ugliness where the lines touch the outer rect
130  for ( int layer = 0; layer < lineSymbol->symbolLayerCount(); ++layer )
131  {
132  // horizontal lines
133  bool drawLine = false;
134  for ( int i = 0; i < positions.size(); ++i )
135  {
136  drawLine = !drawLine;
137  if ( !drawLine )
138  continue;
139 
140  const double lineX = context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset;
141  const double lineLength = context.convertToPainterUnits( widths.at( i ), QgsUnitTypes::RenderMillimeters );
142  lineSymbol->renderPolyline( QPolygonF()
143  << QPointF( lineX, barTopPosition + barHeight / 2.0 )
144  << QPointF( lineX + lineLength, barTopPosition + barHeight / 2.0 ),
145  nullptr, context, layer );
146  }
147 
148  // vertical lines
149  for ( int i = 1; i < positions.size(); ++i )
150  {
151  const double lineX = context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset;
152  lineSymbol->renderPolyline( QPolygonF()
153  << QPointF( lineX, barTopPosition )
154  << QPointF( lineX, barTopPosition + barHeight ),
155  nullptr, context, layer );
156  }
157 
158  // outside line
159  lineSymbol->renderPolyline( QPolygonF()
160  << QPointF( minX, barTopPosition )
161  << QPointF( maxX, barTopPosition )
162  << QPointF( maxX, barTopPosition + barHeight )
163  << QPointF( minX, barTopPosition + barHeight )
164  << QPointF( minX, barTopPosition ),
165  nullptr, context, layer );
166  }
167 
168  lineSymbol->stopRender( context );
169  fillSymbol1->stopRender( context );
170  fillSymbol2->stopRender( context );
171  painter->restore();
172 
173  //draw labels using the default method
174  drawDefaultLabels( context, settings, scaleContext );
175 }
176 
178 {
179  // null the fill symbols by default
180  std::unique_ptr< QgsFillSymbol > fillSymbol = qgis::make_unique< QgsFillSymbol >();
181  std::unique_ptr< QgsSimpleFillSymbolLayer > fillSymbolLayer = qgis::make_unique< QgsSimpleFillSymbolLayer >();
182  fillSymbolLayer->setColor( QColor( 0, 0, 0 ) );
183  fillSymbolLayer->setBrushStyle( Qt::NoBrush );
184  fillSymbolLayer->setStrokeStyle( Qt::NoPen );
185  fillSymbol->changeSymbolLayer( 0, fillSymbolLayer->clone() );
186  settings.setFillSymbol( fillSymbol.release() );
187 
188  fillSymbol = qgis::make_unique< QgsFillSymbol >();
189  fillSymbolLayer->setColor( QColor( 255, 255, 255 ) );
190  fillSymbol->changeSymbolLayer( 0, fillSymbolLayer.release() );
191  settings.setAlternateFillSymbol( fillSymbol.release() );
192 
193  return true;
194 }
195 
196 
197 
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
Definition: qgssymbol.h:1307
void renderPolygon(const QPolygonF &points, const QVector< QPolygonF > *rings, const QgsFeature *f, QgsRenderContext &context, int layer=-1, bool selected=false)
Renders the symbol using the given render context.
Definition: qgssymbol.cpp:2318
QgsFillSymbol * clone() const override
Returns a deep copy of this symbol.
Definition: qgssymbol.cpp:2422
Scalebar style that draws a single box with alternating color for the segments, with horizontal lines...
int sortKey() const override
Returns a sorting key value, where renderers with a lower sort key will be shown earlier in lists.
bool applyDefaultSettings(QgsScaleBarSettings &settings) const override
Applies any default settings relating to the scalebar to the passed settings object.
QgsHollowScaleBarRenderer * clone() const override
Returns a clone of the renderer.
QString visibleName() const override
Returns the user friendly, translated name for the renderer.
QgsHollowScaleBarRenderer()=default
Constructor for QgsHollowScaleBarRenderer.
Flags flags() const override
Returns the scalebar rendering flags, which dictates the renderer's behavior.
void draw(QgsRenderContext &context, const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const override
Draws the scalebar using the specified settings and scaleContext to a destination render context.
QString id() const override
Returns the unique ID for this renderer.
QgsLineSymbol * clone() const override
Returns a deep copy of this symbol.
Definition: qgssymbol.cpp:2295
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
@ FlagUsesLabelVerticalPlacement
Renderer uses the QgsScaleBarSettings::labelVerticalPlacement() setting.
@ FlagUsesLabelHorizontalPlacement
Renderer uses the QgsScaleBarSettings::labelHorizontalPlacement() setting.
@ FlagUsesLineSymbol
Renderer utilizes the scalebar line symbol (see QgsScaleBarSettings::lineSymbol() )
@ FlagUsesUnitLabel
Renderer uses the QgsScaleBarSettings::unitLabel() setting.
@ FlagUsesSegments
Renderer uses the scalebar segments.
@ FlagUsesLabelBarSpace
Renderer uses the QgsScaleBarSettings::labelBarSpace() setting.
@ FlagUsesFillSymbol
Renderer utilizes the scalebar fill symbol (see QgsScaleBarSettings::fillSymbol() )
@ FlagRespectsMapUnitsPerScaleBarUnit
Renderer respects the QgsScaleBarSettings::mapUnitsPerScaleBarUnit() setting.
@ FlagRespectsUnits
Renderer respects the QgsScaleBarSettings::units() setting.
@ FlagUsesAlternateFillSymbol
Renderer utilizes the alternate scalebar fill symbol (see QgsScaleBarSettings::alternateFillSymbol() ...
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...
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.
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
void setAlternateFillSymbol(QgsFillSymbol *symbol)
Sets the secondary fill symbol used to render the scalebar (only used for some scalebar types).
void setFillSymbol(QgsFillSymbol *symbol)
Sets the primary fill symbol used to render the scalebar (only used for some scalebar types).
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters.
QgsFillSymbol * alternateFillSymbol() const
Returns the secondary fill symbol used to render the scalebar (only used for some scalebar types).
QgsFillSymbol * fillSymbol() const
Returns the primary fill symbol used to render the scalebar (only used for some scalebar types).
LabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
@ LabelAboveSegment
Labels are drawn above the scalebar.
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
double height() const
Returns the scalebar height (in millimeters).
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.
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
Contains parameters regarding scalebar calculations.