QGIS API Documentation  2.99.0-Master (314842d)
qgscomposermapgrid.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposermapgrid.h
3  --------------------
4  begin : December 2013
5  copyright : (C) 2013 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSCOMPOSERMAPGRID_H
19 #define QGSCOMPOSERMAPGRID_H
20 
21 #include "qgis_core.h"
22 #include "qgscomposermapitem.h"
24 #include "qgsrectangle.h"
25 #include <QString>
26 #include <QPainter>
27 
29 class QgsLineSymbol;
30 class QgsMarkerSymbol;
31 class QgsComposerMapGrid;
32 class QgsComposerMap;
33 class QDomDocument;
34 class QDomElement;
35 class QPainter;
36 class QgsRenderContext;
37 
47 {
48  public:
49 
54 
63  void addGrid( QgsComposerMapGrid *grid );
64 
71  void removeGrid( const QString &gridId );
72 
79  void moveGridUp( const QString &gridId );
80 
87  void moveGridDown( const QString &gridId );
88 
94  const QgsComposerMapGrid *constGrid( const QString &gridId ) const;
95 
101  QgsComposerMapGrid *grid( const QString &gridId ) const;
102 
108  QgsComposerMapGrid *grid( const int index ) const;
109 
116  QgsComposerMapGrid &operator[]( int idx );
117 
121  QList< QgsComposerMapGrid * > asList() const;
122 
129  bool readXml( const QDomElement &elem, const QDomDocument &doc ) override;
130 
136  double maxGridExtension() const;
137 
148  void calculateMaxGridExtension( double &top, double &right, double &bottom, double &left ) const;
149 };
150 
151 //
152 // QgsComposerMapGrid
153 //
154 
162 class CORE_EXPORT QgsComposerMapGrid : public QgsComposerMapItem
163 {
164 
165  Q_OBJECT
166 
167  public:
168 
171  enum GridUnit
172  {
174  MM,
175  CM
176  };
177 
181  {
182  Solid = 0,
185  FrameAnnotationsOnly
186  };
187 
191  {
192  ShowAll = 0,
195  HideAll
196  };
197 
201  {
202  InsideMapFrame = 0,
204  };
205 
209  {
210  Horizontal = 0,
213  BoundaryDirection
214  };
215 
219  {
220  Decimal = 0,
228  CustomFormat
229  };
230 
234  {
238  Top
239  };
240 
244  {
245  NoFrame = 0,
250  LineBorder
251  };
252 
256  {
257  FrameLeft = 0x01,
258  FrameRight = 0x02,
259  FrameTop = 0x04,
260  FrameBottom = 0x08
261  };
262  Q_DECLARE_FLAGS( FrameSideFlags, FrameSideFlag )
263 
264 
267  {
268  Longitude = 0,
269  Latitude
270  };
271 
276  QgsComposerMapGrid( const QString &name, QgsComposerMap *map );
277 
278  virtual ~QgsComposerMapGrid();
279 
283  void draw( QPainter *painter ) override;
284 
290  bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;
291 
297  bool readXml( const QDomElement &itemElem, const QDomDocument &doc ) override;
298 
303  void setCrs( const QgsCoordinateReferenceSystem &crs );
304 
309  QgsCoordinateReferenceSystem crs() const { return mCRS; }
310 
315  void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
316 
321  QPainter::CompositionMode blendMode() const { return mBlendMode; }
322 
323  bool usesAdvancedEffects() const override;
324 
329  double maxExtension();
330 
341  void calculateMaxExtension( double &top, double &right, double &bottom, double &left );
342 
343  //
344  // GRID UNITS
345  //
346 
352  void setUnits( const GridUnit unit );
353 
359  GridUnit units() const { return mGridUnit; }
360 
367  void setIntervalX( const double interval );
368 
375  double intervalX() const { return mGridIntervalX; }
376 
383  void setIntervalY( const double interval );
384 
391  double intervalY() const { return mGridIntervalY; }
392 
399  void setOffsetX( const double offset );
400 
407  double offsetX() const { return mGridOffsetX; }
408 
415  void setOffsetY( const double offset );
416 
423  double offsetY() const { return mGridOffsetY; }
424 
425  //
426  // GRID APPEARANCE
427  //
428 
434  void setStyle( const GridStyle style );
435 
441  GridStyle style() const { return mGridStyle; }
442 
448  void setCrossLength( const double length ) { mCrossLength = length; }
449 
455  double crossLength() const { return mCrossLength; }
456 
464  void setGridLineWidth( const double width );
465 
473  void setGridLineColor( const QColor &color );
474 
482  void setLineSymbol( QgsLineSymbol *symbol );
483 
492  const QgsLineSymbol *lineSymbol() const { return mGridLineSymbol; }
493 
501  QgsLineSymbol *lineSymbol() { return mGridLineSymbol; }
502 
510  void setMarkerSymbol( QgsMarkerSymbol *symbol );
511 
520  const QgsMarkerSymbol *markerSymbol() const { return mGridMarkerSymbol; }
521 
529  QgsMarkerSymbol *markerSymbol() { return mGridMarkerSymbol; }
530 
531  //
532  // ANNOTATIONS
533  //
534 
539  void setAnnotationEnabled( const bool enabled ) { mShowGridAnnotation = enabled; }
540 
545  bool annotationEnabled() const { return mShowGridAnnotation; }
546 
551  void setAnnotationFont( const QFont &font ) { mGridAnnotationFont = font; }
552 
557  QFont annotationFont() const { return mGridAnnotationFont; }
558 
563  void setAnnotationFontColor( const QColor &color ) { mGridAnnotationFontColor = color; }
564 
569  QColor annotationFontColor() const { return mGridAnnotationFontColor; }
570 
575  void setAnnotationPrecision( const int precision ) { mGridAnnotationPrecision = precision; }
576 
581  int annotationPrecision() const { return mGridAnnotationPrecision; }
582 
590  void setAnnotationDisplay( const DisplayMode display, const BorderSide border );
591 
600  DisplayMode annotationDisplay( const BorderSide border ) const;
601 
608  void setAnnotationPosition( const AnnotationPosition position, const BorderSide border );
609 
616  AnnotationPosition annotationPosition( const BorderSide border ) const;
617 
622  void setAnnotationFrameDistance( const double distance ) { mAnnotationFrameDistance = distance; }
623 
628  double annotationFrameDistance() const { return mAnnotationFrameDistance; }
629 
635  void setAnnotationDirection( const AnnotationDirection direction, const BorderSide border );
636 
641  void setAnnotationDirection( const AnnotationDirection direction );
642 
648  AnnotationDirection annotationDirection( const BorderSide border ) const;
649 
654  void setAnnotationFormat( const AnnotationFormat format ) { mGridAnnotationFormat = format; }
655 
660  AnnotationFormat annotationFormat() const { return mGridAnnotationFormat; }
661 
668  void setAnnotationExpression( const QString &expression ) { mGridAnnotationExpressionString = expression; mGridAnnotationExpression.reset(); }
669 
676  QString annotationExpression() const { return mGridAnnotationExpressionString; }
677 
678  //
679  // GRID FRAME
680  //
681 
686  void setFrameStyle( const FrameStyle style ) { mGridFrameStyle = style; }
687 
692  FrameStyle frameStyle() const { return mGridFrameStyle; }
693 
700  void setFrameDivisions( const DisplayMode divisions, const BorderSide border );
701 
708  DisplayMode frameDivisions( const BorderSide border ) const;
709 
717  void setFrameSideFlags( QgsComposerMapGrid::FrameSideFlags flags );
718 
726  void setFrameSideFlag( const FrameSideFlag flag, bool on = true );
727 
735  FrameSideFlags frameSideFlags() const;
736 
745  bool testFrameSideFlag( const FrameSideFlag flag ) const;
746 
753  void setFrameWidth( const double width ) { mGridFrameWidth = width; }
754 
761  double frameWidth() const { return mGridFrameWidth; }
762 
768  void setFramePenSize( const double width ) { mGridFramePenThickness = width; }
769 
775  double framePenSize() const { return mGridFramePenThickness; }
776 
784  void setFramePenColor( const QColor &color ) { mGridFramePenColor = color; }
785 
793  QColor framePenColor() const {return mGridFramePenColor;}
794 
801  void setFrameFillColor1( const QColor &color ) { mGridFrameFillColor1 = color; }
802 
809  QColor frameFillColor1() const { return mGridFrameFillColor1; }
810 
817  void setFrameFillColor2( const QColor &color ) { mGridFrameFillColor2 = color; }
818 
825  QColor frameFillColor2() const { return mGridFrameFillColor2; }
826 
827  virtual QgsExpressionContext createExpressionContext() const override;
828 
829  private:
830 
831  QgsComposerMapGrid(); //forbidden
832 
833  struct GridExtension
834  {
835  GridExtension()
836  : top( 0.0 )
837  , right( 0.0 )
838  , bottom( 0.0 )
839  , left( 0.0 )
840  {}
841  double top;
842  double right;
843  double bottom;
844  double left;
845  };
846 
847  /*True if a re-transformation of grid lines is required*/
848  bool mTransformDirty;
849 
851  GridStyle mGridStyle;
853  double mGridIntervalX;
855  double mGridIntervalY;
857  double mGridOffsetX;
859  double mGridOffsetY;
861  QFont mGridAnnotationFont;
863  QColor mGridAnnotationFontColor;
865  int mGridAnnotationPrecision;
867  bool mShowGridAnnotation;
868 
870  DisplayMode mLeftGridAnnotationDisplay;
872  DisplayMode mRightGridAnnotationDisplay;
874  DisplayMode mTopGridAnnotationDisplay;
876  DisplayMode mBottomGridAnnotationDisplay;
877 
879  AnnotationPosition mLeftGridAnnotationPosition;
881  AnnotationPosition mRightGridAnnotationPosition;
883  AnnotationPosition mTopGridAnnotationPosition;
885  AnnotationPosition mBottomGridAnnotationPosition;
886 
888  double mAnnotationFrameDistance;
889 
891  AnnotationDirection mLeftGridAnnotationDirection;
893  AnnotationDirection mRightGridAnnotationDirection;
895  AnnotationDirection mTopGridAnnotationDirection;
897  AnnotationDirection mBottomGridAnnotationDirection;
898  AnnotationFormat mGridAnnotationFormat;
899 
900  QString mGridAnnotationExpressionString;
901  mutable std::unique_ptr< QgsExpression > mGridAnnotationExpression;
902 
903  FrameStyle mGridFrameStyle;
904  FrameSideFlags mGridFrameSides;
905  double mGridFrameWidth;
906  double mGridFramePenThickness;
907  QColor mGridFramePenColor;
908  QColor mGridFrameFillColor1;
909  QColor mGridFrameFillColor2;
910  double mCrossLength;
911 
913  DisplayMode mLeftFrameDivisions;
915  DisplayMode mRightFrameDivisions;
917  DisplayMode mTopFrameDivisions;
919  DisplayMode mBottomFrameDivisions;
920 
921  QgsLineSymbol *mGridLineSymbol = nullptr;
922  QgsMarkerSymbol *mGridMarkerSymbol = nullptr;
923 
925 
926  GridUnit mGridUnit;
927 
928  QPainter::CompositionMode mBlendMode;
929 
930  QList< QPair< double, QPolygonF > > mTransformedXLines;
931  QList< QPair< double, QPolygonF > > mTransformedYLines;
932  QList< QgsPoint > mTransformedIntersections;
933  QRectF mPrevPaintRect;
934  QPolygonF mPrevMapPolygon;
935 
936  class QgsMapAnnotation
937  {
938  public:
939  double coordinate;
940  QPointF itemPosition;
942  };
943 
944  void init();
945 
949  void drawGridFrame( QPainter *p, const QList< QPair< double, QLineF > > &hLines, const QList< QPair< double, QLineF > > &vLines, GridExtension *extension = nullptr ) const;
950 
959  void drawCoordinateAnnotations( QPainter *p, const QList< QPair< double, QLineF > > &hLines, const QList< QPair< double, QLineF > > &vLines, QgsExpressionContext &expressionContext, GridExtension *extension = nullptr ) const;
960 
964  void drawCoordinateAnnotation( QPainter *p, QPointF pos, const QString &annotationString, const AnnotationCoordinate coordinateType, GridExtension *extension = nullptr ) const;
965 
972  void drawAnnotation( QPainter *p, QPointF pos, int rotation, const QString &annotationText ) const;
973 
974  QString gridAnnotationString( double value, AnnotationCoordinate coord, QgsExpressionContext &expressionContext ) const;
975 
978  int xGridLines( QList< QPair< double, QLineF > > &lines ) const;
979 
982  int yGridLines( QList< QPair< double, QLineF > > &lines ) const;
983 
984  int xGridLinesCrsTransform( const QgsRectangle &bbox, const QgsCoordinateTransform &t, QList< QPair< double, QPolygonF > > &lines ) const;
985 
986  int yGridLinesCrsTransform( const QgsRectangle &bbox, const QgsCoordinateTransform &t, QList< QPair< double, QPolygonF > > &lines ) const;
987 
988  void drawGridLine( const QLineF &line, QgsRenderContext &context ) const;
989 
990  void drawGridLine( const QPolygonF &line, QgsRenderContext &context ) const;
991 
992  void sortGridLinesOnBorders( const QList< QPair< double, QLineF > > &hLines, const QList< QPair< double, QLineF > > &vLines, QMap< double, double > &leftFrameEntries,
993  QMap< double, double > &rightFrameEntries, QMap< double, double > &topFrameEntries, QMap< double, double > &bottomFrameEntries ) const;
994 
998  void drawGridFrameBorder( QPainter *p, const QMap< double, double > &borderPos, BorderSide border, double *extension = nullptr ) const;
999 
1004  BorderSide borderForLineCoord( QPointF p, const AnnotationCoordinate coordinateType ) const;
1005 
1007  int crsGridParams( QgsRectangle &crsRect, QgsCoordinateTransform &inverseTransform ) const;
1008 
1009  static QList<QPolygonF> trimLinesToMap( const QPolygonF &line, const QgsRectangle &rect );
1010 
1011  QPolygonF scalePolygon( const QPolygonF &polygon, const double scale ) const;
1012 
1014  void drawGridCrsTransform( QgsRenderContext &context, double dotsPerMM, QList< QPair< double, QLineF > > &horizontalLines,
1015  QList< QPair< double, QLineF > > &verticalLines, bool calculateLinesOnly = false );
1016 
1017  void drawGridNoTransform( QgsRenderContext &context, double dotsPerMM, QList<QPair<double, QLineF> > &horizontalLines, QList<QPair<double, QLineF> > &verticalLines, bool calculateLinesOnly = false ) const;
1018 
1019  void createDefaultGridLineSymbol();
1020 
1021  void createDefaultGridMarkerSymbol();
1022 
1023  void drawGridMarker( QPointF point, QgsRenderContext &context ) const;
1024 
1025  void drawGridFrameZebraBorder( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double *extension = nullptr ) const;
1026 
1027  void drawGridFrameTicks( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double *extension = nullptr ) const;
1028 
1029  void drawGridFrameLineBorder( QPainter *p, BorderSide border, double *extension = nullptr ) const;
1030 
1031  void calculateCrsTransformLines();
1032 
1033  bool shouldShowDivisionForSide( AnnotationCoordinate coordinate, BorderSide side ) const;
1034  bool shouldShowDivisionForDisplayMode( AnnotationCoordinate coordinate, DisplayMode mode ) const;
1035 
1036  friend class TestQgsComposerMapGrid;
1037 };
1038 
1039 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsComposerMapGrid::FrameSideFlags )
1040 
1041 #endif // QGSCOMPOSERMAPGRID_H
static unsigned index
QColor annotationFontColor() const
Gets the font color used for drawing grid annotations.
A rectangle specified with double values.
Definition: qgsrectangle.h:36
QFont annotationFont() const
Gets the font used for drawing grid annotations.
QColor frameFillColor2() const
Retrieves the second fill color for the grid frame.
void setBlendMode(const QPainter::CompositionMode mode)
Sets the blending mode used for drawing the grid.
virtual bool readXml(const QDomElement &itemElem, const QDomDocument &doc) override
Sets map item state from a DOM document.
virtual QgsExpressionContext createExpressionContext() const
Creates an expression context relating to the objects&#39; current state.
double intervalY() const
Gets the interval between grid lines in the y-direction.
GridStyle
Grid drawing style.
QPainter::CompositionMode blendMode() const
Retrieves the blending mode used for drawing the grid.
virtual void draw(QPainter *painter)=0
Draws the item on to a painter.
Draw line crosses at intersections of grid lines.
void setFramePenSize(const double width)
Sets the width of the stroke drawn in the grid frame.
Degree/minutes, with minutes using leading zeros where required.
QString annotationExpression() const
Returns the expression used for drawing grid annotations.
A collection of grids which is drawn above the map content in a QgsComposerMap.
FrameStyle
Style for grid frame.
virtual bool writeXml(QDomElement &elem, QDomDocument &doc) const override
Stores map item state in DOM element.
Draw annotations outside the map frame.
QColor frameFillColor1() const
Retrieves the first fill color for the grid frame.
virtual bool readXml(const QDomElement &elem, const QDomDocument &doc)=0
Sets the item stack&#39;s state from a DOM document.
void setAnnotationExpression(const QString &expression)
Sets the expression used for drawing grid annotations.
double intervalX() const
Gets the interval between grid lines in the x-direction.
Tick markers drawn outside map frame.
void setAnnotationFont(const QFont &font)
Sets the font used for drawing grid annotations.
Degree/minutes, use NSEW suffix.
const QgsLineSymbol * lineSymbol() const
Gets the line symbol used for drawing grid lines.
AnnotationDirection
Direction of grid annotations.
QColor framePenColor() const
Retrieves the color of the stroke drawn in the grid frame.
double crossLength() const
Retrieves the length of the cross segments drawn for the grid.
AnnotationFormat
Format for displaying grid annotations.
An item which is drawn inside a QgsComposerMap, e.g., a grid or map overview.
void setCrossLength(const double length)
Sets the length of the cross segments drawn for the grid.
Show latitude/y annotations/divisions only.
virtual bool enabled() const
Returns whether the item will be drawn.
virtual bool usesAdvancedEffects() const
Returns true if the item is drawn using advanced effects, such as blend modes.
Black/white pattern.
QgsMarkerSymbol * markerSymbol()
Gets the marker symbol used for drawing grid points.
double offsetY() const
Gets the offset for grid lines in the y-direction.
FrameStyle frameStyle() const
Gets the grid frame style.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
DisplayMode
Display settings for grid annotations and frames.
void setAnnotationFormat(const AnnotationFormat format)
Sets the format for drawing grid annotations.
AnnotationPosition
Position for grid annotations.
double frameWidth() const
Gets the grid frame width.
Grid units in millimeters.
void setAnnotationEnabled(const bool enabled)
Sets whether annotations should be shown for the grid.
GridStyle style() const
Gets the grid&#39;s style, which controls how the grid is drawn over the map&#39;s contents.
QgsComposerMapItem & operator[](int idx)
Returns a reference to an item within the stack.
FrameSideFlag
Flags for controlling which side of the map a frame is drawn on.
Decimal degrees, use NSEW suffix.
An individual grid which is drawn above the map content in a QgsComposerMap.
void setFrameWidth(const double width)
Sets the grid frame width.
bool annotationEnabled() const
Gets whether annotations are shown for the grid.
void setAnnotationFontColor(const QColor &color)
Sets the font color used for drawing grid annotations.
int annotationPrecision() const
Returns the coordinate precision for grid annotations.
Object representing map window.
QList< QgsComposerMapItem *> asList() const
Returns a list of QgsComposerMapItems contained by the stack.
BorderSide
Border sides for annotations.
void setFrameFillColor1(const QColor &color)
Sets the first fill color used for the grid frame.
void setFramePenColor(const QColor &color)
Sets the color of the stroke drawn in the grid frame.
Degree/minutes/seconds, use NSEW suffix.
double annotationFrameDistance() const
Gets the distance between the map frame and annotations.
double offsetX() const
Gets the offset for grid lines in the x-direction.
Degree/minutes/seconds, with minutes using leading zeros where required.
Contains information about the context of a rendering operation.
AnnotationFormat annotationFormat() const
Gets the format for drawing grid annotations.
QgsLineSymbol * lineSymbol()
Gets the line symbol used for drawing grid lines.
A collection of map items which are drawn above the map content in a QgsComposerMap.
const QgsMarkerSymbol * markerSymbol() const
Gets the marker symbol used for drawing grid points.
Draw annotations vertically, descending.
This class represents a coordinate reference system (CRS).
void setAnnotationPrecision(const int precision)
Sets the coordinate precision for grid annotations.
Class for doing transforms between two map coordinate systems.
Tick markers drawn both inside and outside the map frame.
Degree/minutes, use - for S/W coordinates.
GridUnit units() const
Gets the units used for grid measurements such as the interval and offset for grid lines...
Draw markers at intersections of grid lines.
void setFrameFillColor2(const QColor &color)
Sets the second fill color used for the grid frame.
void setFrameStyle(const FrameStyle style)
Sets the grid frame style.
Draw annotations vertically, ascending.
Degree/minutes/seconds, use - for S/W coordinates.
GridUnit
Unit for grid values.
QgsCoordinateReferenceSystem crs() const
Retrieves the CRS for the grid.
Show longitude/x annotations/divisions only.
Grid units follow map units.
AnnotationCoordinate
Annotation coordinate type.
double framePenSize() const
Retrieves the width of the stroke drawn in the grid frame.
void setAnnotationFrameDistance(const double distance)
Sets the distance between the map frame and annotations.
Tick markers drawn inside map frame.