QGIS API Documentation  2.99.0-Master (716ff6c)
qgscomposerscalebar.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerscalebar.h
3  -------------------
4  begin : March 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : [email protected]
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 #ifndef QGSCOMPOSERSCALEBAR_H
17 #define QGSCOMPOSERSCALEBAR_H
18 
19 #include "qgis_core.h"
20 #include "qgscomposeritem.h"
21 #include <QFont>
22 #include <QPen>
23 #include <QColor>
24 
25 class QgsComposerMap;
26 class QgsScaleBarStyle;
27 
32 class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
33 {
34  Q_OBJECT
35 
36  public:
37 
38  enum Alignment
39  {
40  Left = 0,
42  Right
43  };
44 
46  {
47  MapUnits = 0,
50  NauticalMiles
51  };
52 
56  {
57  SegmentSizeFixed = 0,
58  SegmentSizeFitWidth = 1
59  };
60 
61  QgsComposerScaleBar( QgsComposition *composition );
63 
65  virtual int type() const override { return ComposerScaleBar; }
66 
68  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
69 
70  //getters and setters
71  int numSegments() const {return mNumSegments;}
72  void setNumSegments( int nSegments );
73 
74  int numSegmentsLeft() const {return mNumSegmentsLeft;}
75  void setNumSegmentsLeft( int nSegmentsLeft );
76 
77  double numUnitsPerSegment() const {return mNumUnitsPerSegment;}
78  void setNumUnitsPerSegment( double units );
79 
86  SegmentSizeMode segmentSizeMode() const { return mSegmentSizeMode; }
87 
95  void setSegmentSizeMode( SegmentSizeMode mode );
96 
105  double minBarWidth() const { return mMinBarWidth; }
106 
116  void setMinBarWidth( double minWidth );
117 
126  double maxBarWidth() const { return mMaxBarWidth; }
127 
137  void setMaxBarWidth( double maxWidth );
138 
139  double numMapUnitsPerScaleBarUnit() const {return mNumMapUnitsPerScaleBarUnit;}
140  void setNumMapUnitsPerScaleBarUnit( double d ) {mNumMapUnitsPerScaleBarUnit = d;}
141 
142  QString unitLabeling() const {return mUnitLabeling;}
143  void setUnitLabeling( const QString &label ) {mUnitLabeling = label;}
144 
145  QFont font() const;
146  void setFont( const QFont &font );
147 
153  QColor fontColor() const {return mFontColor;}
154 
160  void setFontColor( const QColor &c ) {mFontColor = c;}
161 
167  QColor fillColor() const {return mFillColor;}
168 
174  void setFillColor( const QColor &color ) {mFillColor = color; mBrush.setColor( color ); }
175 
181  QColor fillColor2() const {return mFillColor2;}
182 
188  void setFillColor2( const QColor &color ) {mFillColor2 = color; mBrush2.setColor( color ); }
189 
194  QColor lineColor() const {return mLineColor;}
195 
200  void setLineColor( const QColor &color ) { mLineColor = color; mPen.setColor( mLineColor ); }
201 
206  double lineWidth() const {return mLineWidth;}
207 
212  void setLineWidth( double width ) { mLineWidth = width; mPen.setWidthF( width ); }
213 
219  QPen pen() const {return mPen;}
220 
227  QBrush brush() const {return mBrush;}
228 
235  QBrush brush2() const {return mBrush2;}
236 
237  double height() const {return mHeight;}
238  void setHeight( double h ) {mHeight = h;}
239 
240  void setComposerMap( const QgsComposerMap *map );
241  const QgsComposerMap *composerMap() const {return mComposerMap;}
242 
243  double labelBarSpace() const {return mLabelBarSpace;}
244  void setLabelBarSpace( double space ) {mLabelBarSpace = space;}
245 
246  double boxContentSpace() const {return mBoxContentSpace;}
247  void setBoxContentSpace( double space );
248 
249  double segmentMillimeters() const {return mSegmentMillimeters;}
250 
252  Alignment alignment() const { return mAlignment; }
253 
254  void setAlignment( Alignment a );
255 
256  ScaleBarUnits units() const { return mUnits; }
257 
258  void setUnits( ScaleBarUnits u );
259 
265  Qt::PenJoinStyle lineJoinStyle() const { return mLineJoinStyle; }
266 
273  void setLineJoinStyle( Qt::PenJoinStyle style );
274 
280  Qt::PenCapStyle lineCapStyle() const { return mLineCapStyle; }
281 
288  void setLineCapStyle( Qt::PenCapStyle style );
289 
291  void applyDefaultSettings();
293  void applyDefaultSize( ScaleBarUnits u = Meters );
294 
297  void setStyle( const QString &styleName );
298 
300  QString style() const;
301 
306  void segmentPositions( QList<QPair<double, double> > &posWidthList ) const;
307 
309  void adjustBoxSize();
310 
312  void update();
313 
315  QString firstLabelString() const;
316 
321  bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;
322 
327  bool readXml( const QDomElement &itemElem, const QDomDocument &doc ) override;
328 
330  void correctXPositionAlignment( double width, double widthAfter );
331 
332  //overridden to apply minimum size
333  void setSceneRect( const QRectF &rectangle ) override;
334 
335  public slots:
336  void updateSegmentSize();
338  void invalidateCurrentMap();
339  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
340 
341  protected:
342 
344  const QgsComposerMap *mComposerMap = nullptr;
356  double mMinBarWidth;
358  double mMaxBarWidth;
359 
361  QString mUnitLabeling;
363  QFont mFont;
364  QColor mFontColor;
366  QColor mFillColor = QColor( 0, 0, 0 );
368  QColor mFillColor2 = QColor( 255, 255, 255 );
370  QColor mLineColor = QColor( 0, 0, 0 );
372  double mLineWidth = 0.3;
374  QPen mPen;
376  QBrush mBrush;
378  QBrush mBrush2;
380  double mHeight;
382  QgsScaleBarStyle *mStyle = nullptr;
383 
386 
389 
392 
394 
396 
397  Qt::PenJoinStyle mLineJoinStyle;
398  Qt::PenCapStyle mLineCapStyle;
399 
401  void refreshSegmentMillimeters();
402 
404  double mapWidth() const;
405 };
406 
407 #endif //QGSCOMPOSERSCALEBAR_H
408 
409 
ScaleBarUnits units() const
A scale bar item that can be added to a map composition.
double numUnitsPerSegment() const
double mLabelBarSpace
Space between bar and Text labels.
double mHeight
Height of bars/lines.
double numMapUnitsPerScaleBarUnit() const
double mNumUnitsPerSegment
Size of a segment (in map units)
QPen pen() const
Returns the pen used for drawing the scalebar.
double mNumMapUnitsPerScaleBarUnit
Number of map units per scale bar units (e.g. 1000 to have km for a map with m units) ...
SegmentSizeMode segmentSizeMode() const
Returns the size mode for scale bar segments.
QColor fontColor() const
Returns the color used for drawing text in the scalebar.
Qt::PenJoinStyle mLineJoinStyle
void setUnitLabeling(const QString &label)
QBrush brush2() const
Returns the secondary brush for the scalebar.
A item that forms part of a map composition.
double mMinBarWidth
Minimum allowed bar width, when mSegmentSizeMode is FitWidth.
QBrush mBrush2
Secondary fill.
QColor fillColor2() const
Returns the secondary color used for fills in the scalebar.
SegmentSizeMode
Modes for setting size for scale bar segments.
DataDefinedProperty
Data defined properties for different item types.
void setNumMapUnitsPerScaleBarUnit(double d)
double mSegmentMillimeters
Width of a segment (in mm)
QBrush brush() const
Returns the primary brush for the scalebar.
Qt::PenCapStyle mLineCapStyle
virtual bool readXml(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Qt::PenCapStyle lineCapStyle() const
Returns the cap style used for drawing lines in the scalebar.
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties, const QgsExpressionContext *context=nullptr) override
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
double labelBarSpace() const
int mNumSegmentsLeft
Number of segments on left side.
Abstraction of composer scale bar style.
void setLabelBarSpace(double space)
Graphics scene for map printing.
Object representing map window.
Qt::PenJoinStyle lineJoinStyle() const
Returns the join style used for drawing lines in the scalebar.
QColor fillColor() const
Returns the color used for fills in the scalebar.
void setLineWidth(double width)
Sets the line width in millimeters for lines in the scalebar.
const QgsComposerMap * composerMap() const
QColor lineColor() const
Returns the color used for lines in the scalebar.
int mNumSegments
Number of segments on right side.
double segmentMillimeters() const
double mMaxBarWidth
Maximum allowed bar width, when mSegmentSizeMode is FitWidth.
void setFontColor(const QColor &c)
Sets the color used for drawing text in the scalebar.
SegmentSizeMode mSegmentSizeMode
Either fixed (i.e. mNumUnitsPerSegment) or try to best fit scale bar width (mMinBarWidth, mMaxBarWidth)
QString unitLabeling() const
double boxContentSpace() const
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
Alignment alignment() const
Left / Middle/ Right.
double mBoxContentSpace
Space between content and item box.
QString mUnitLabeling
Labeling of map units.
double lineWidth() const
Returns the line width in millimeters for lines in the scalebar.
virtual bool writeXml(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
void setLineColor(const QColor &color)
Sets the color used for lines in the scalebar.
void setFillColor2(const QColor &color)
Sets the secondary color used for fills in the scalebar.
void setFillColor(const QColor &color)
Sets the color used for fills in the scalebar.
All properties for item.
double minBarWidth() const
Returns the minimum size (in millimeters) for scale bar segments.
double maxBarWidth() const
Returns the maximum size (in millimeters) for scale bar segments.
virtual int type() const override
Return correct graphics item type.