QGIS API Documentation  2.99.0-Master (37c43df)
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 "qgscomposermapitem.h"
23 #include "qgsrectangle.h"
24 #include <QString>
25 #include <QPainter>
26 
28 class QgsLineSymbol;
29 class QgsMarkerSymbol;
30 class QgsComposerMapGrid;
31 class QgsComposerMap;
32 class QDomDocument;
33 class QDomElement;
34 class QPainter;
35 class QgsRenderContext;
36 
46 {
47  public:
48 
53 
54  virtual ~QgsComposerMapGridStack();
55 
64  void addGrid( QgsComposerMapGrid* grid );
65 
72  void removeGrid( const QString& gridId );
73 
80  void moveGridUp( const QString& gridId );
81 
88  void moveGridDown( const QString& gridId );
89 
95  const QgsComposerMapGrid* constGrid( const QString& gridId ) const;
96 
102  QgsComposerMapGrid* grid( const QString& gridId ) const;
103 
109  QgsComposerMapGrid* grid( const int index ) const;
110 
117  QgsComposerMapGrid &operator[]( int idx );
118 
122  QList< QgsComposerMapGrid* > asList() const;
123 
130  bool readXml( const QDomElement& elem, const QDomDocument& doc ) override;
131 
137  double maxGridExtension() const;
138 
149  void calculateMaxGridExtension( double& top, double& right, double& bottom, double& left ) const;
150 };
151 
152 //
153 // QgsComposerMapGrid
154 //
155 
163 class CORE_EXPORT QgsComposerMapGrid : public QgsComposerMapItem
164 {
165 
166  Q_OBJECT
167 
168  public:
169 
172  enum GridUnit
173  {
175  MM,
176  CM
177  };
178 
182  {
183  Solid = 0,
186  FrameAnnotationsOnly
187  };
188 
192  {
193  ShowAll = 0,
196  HideAll
197  };
198 
202  {
203  InsideMapFrame = 0,
205  };
206 
210  {
211  Horizontal = 0,
214  BoundaryDirection
215  };
216 
220  {
221  Decimal = 0,
229  CustomFormat
230  };
231 
235  {
239  Top
240  };
241 
245  {
246  NoFrame = 0,
251  LineBorder
252  };
253 
257  {
258  FrameLeft = 0x01,
259  FrameRight = 0x02,
260  FrameTop = 0x04,
261  FrameBottom = 0x08
262  };
263  Q_DECLARE_FLAGS( FrameSideFlags, FrameSideFlag )
264 
265 
268  {
269  Longitude = 0,
270  Latitude
271  };
272 
277  QgsComposerMapGrid( const QString& name, QgsComposerMap* map );
278 
279  virtual ~QgsComposerMapGrid();
280 
284  void draw( QPainter* painter ) override;
285 
291  bool writeXml( QDomElement& elem, QDomDocument & doc ) const override;
292 
298  bool readXml( const QDomElement& itemElem, const QDomDocument& doc ) override;
299 
304  void setCrs( const QgsCoordinateReferenceSystem& crs );
305 
310  QgsCoordinateReferenceSystem crs() const { return mCRS; }
311 
316  void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
317 
322  QPainter::CompositionMode blendMode() const { return mBlendMode; }
323 
324  bool usesAdvancedEffects() const override;
325 
330  double maxExtension();
331 
342  void calculateMaxExtension( double& top, double& right, double& bottom, double& left );
343 
344  //
345  // GRID UNITS
346  //
347 
353  void setUnits( const GridUnit unit );
354 
360  GridUnit units() const { return mGridUnit; }
361 
368  void setIntervalX( const double interval );
369 
376  double intervalX() const { return mGridIntervalX; }
377 
384  void setIntervalY( const double interval );
385 
392  double intervalY() const { return mGridIntervalY; }
393 
400  void setOffsetX( const double offset );
401 
408  double offsetX() const { return mGridOffsetX; }
409 
416  void setOffsetY( const double offset );
417 
424  double offsetY() const { return mGridOffsetY; }
425 
426  //
427  // GRID APPEARANCE
428  //
429 
435  void setStyle( const GridStyle style );
436 
442  GridStyle style() const { return mGridStyle; }
443 
449  void setCrossLength( const double length ) { mCrossLength = length; }
450 
456  double crossLength() const { return mCrossLength; }
457 
465  void setGridLineWidth( const double width );
466 
474  void setGridLineColor( const QColor& color );
475 
483  void setLineSymbol( QgsLineSymbol* symbol );
484 
493  const QgsLineSymbol* lineSymbol() const { return mGridLineSymbol; }
494 
502  QgsLineSymbol* lineSymbol() { return mGridLineSymbol; }
503 
511  void setMarkerSymbol( QgsMarkerSymbol* symbol );
512 
521  const QgsMarkerSymbol* markerSymbol() const { return mGridMarkerSymbol; }
522 
530  QgsMarkerSymbol* markerSymbol() { return mGridMarkerSymbol; }
531 
532  //
533  // ANNOTATIONS
534  //
535 
540  void setAnnotationEnabled( const bool enabled ) { mShowGridAnnotation = enabled; }
541 
546  bool annotationEnabled() const { return mShowGridAnnotation; }
547 
552  void setAnnotationFont( const QFont& font ) { mGridAnnotationFont = font; }
553 
558  QFont annotationFont() const { return mGridAnnotationFont; }
559 
564  void setAnnotationFontColor( const QColor& color ) { mGridAnnotationFontColor = color; }
565 
570  QColor annotationFontColor() const { return mGridAnnotationFontColor; }
571 
576  void setAnnotationPrecision( const int precision ) { mGridAnnotationPrecision = precision; }
577 
582  int annotationPrecision() const { return mGridAnnotationPrecision; }
583 
591  void setAnnotationDisplay( const DisplayMode display, const BorderSide border );
592 
601  DisplayMode annotationDisplay( const BorderSide border ) const;
602 
609  void setAnnotationPosition( const AnnotationPosition position, const BorderSide border );
610 
617  AnnotationPosition annotationPosition( const BorderSide border ) const;
618 
623  void setAnnotationFrameDistance( const double distance ) { mAnnotationFrameDistance = distance; }
624 
629  double annotationFrameDistance() const { return mAnnotationFrameDistance; }
630 
636  void setAnnotationDirection( const AnnotationDirection direction, const BorderSide border );
637 
642  void setAnnotationDirection( const AnnotationDirection direction );
643 
649  AnnotationDirection annotationDirection( const BorderSide border ) const;
650 
655  void setAnnotationFormat( const AnnotationFormat format ) { mGridAnnotationFormat = format; }
656 
661  AnnotationFormat annotationFormat() const { return mGridAnnotationFormat; }
662 
669  void setAnnotationExpression( const QString& expression ) { mGridAnnotationExpressionString = expression; mGridAnnotationExpression.reset(); }
670 
677  QString annotationExpression() const { return mGridAnnotationExpressionString; }
678 
679  //
680  // GRID FRAME
681  //
682 
687  void setFrameStyle( const FrameStyle style ) { mGridFrameStyle = style; }
688 
693  FrameStyle frameStyle() const { return mGridFrameStyle; }
694 
701  void setFrameDivisions( const DisplayMode divisions, const BorderSide border );
702 
709  DisplayMode frameDivisions( const BorderSide border ) const;
710 
718  void setFrameSideFlags( QgsComposerMapGrid::FrameSideFlags flags );
719 
727  void setFrameSideFlag( const FrameSideFlag flag, bool on = true );
728 
736  FrameSideFlags frameSideFlags() const;
737 
746  bool testFrameSideFlag( const FrameSideFlag flag ) const;
747 
754  void setFrameWidth( const double width ) { mGridFrameWidth = width; }
755 
762  double frameWidth() const { return mGridFrameWidth; }
763 
769  void setFramePenSize( const double width ) { mGridFramePenThickness = width; }
770 
776  double framePenSize() const { return mGridFramePenThickness; }
777 
785  void setFramePenColor( const QColor& color ) { mGridFramePenColor = color; }
786 
794  QColor framePenColor() const {return mGridFramePenColor;}
795 
802  void setFrameFillColor1( const QColor& color ) { mGridFrameFillColor1 = color; }
803 
810  QColor frameFillColor1() const { return mGridFrameFillColor1; }
811 
818  void setFrameFillColor2( const QColor& color ) { mGridFrameFillColor2 = color; }
819 
826  QColor frameFillColor2() const { return mGridFrameFillColor2; }
827 
828  virtual QgsExpressionContext createExpressionContext() const override;
829 
830  private:
831 
832  QgsComposerMapGrid(); //forbidden
833 
834  struct GridExtension
835  {
836  GridExtension()
837  : top( 0.0 )
838  , right( 0.0 )
839  , bottom( 0.0 )
840  , left( 0.0 )
841  {}
842  double top;
843  double right;
844  double bottom;
845  double left;
846  };
847 
848  /*True if a re-transformation of grid lines is required*/
849  bool mTransformDirty;
850 
852  GridStyle mGridStyle;
854  double mGridIntervalX;
856  double mGridIntervalY;
858  double mGridOffsetX;
860  double mGridOffsetY;
862  QFont mGridAnnotationFont;
864  QColor mGridAnnotationFontColor;
866  int mGridAnnotationPrecision;
868  bool mShowGridAnnotation;
869 
871  DisplayMode mLeftGridAnnotationDisplay;
873  DisplayMode mRightGridAnnotationDisplay;
875  DisplayMode mTopGridAnnotationDisplay;
877  DisplayMode mBottomGridAnnotationDisplay;
878 
880  AnnotationPosition mLeftGridAnnotationPosition;
882  AnnotationPosition mRightGridAnnotationPosition;
884  AnnotationPosition mTopGridAnnotationPosition;
886  AnnotationPosition mBottomGridAnnotationPosition;
887 
889  double mAnnotationFrameDistance;
890 
892  AnnotationDirection mLeftGridAnnotationDirection;
894  AnnotationDirection mRightGridAnnotationDirection;
896  AnnotationDirection mTopGridAnnotationDirection;
898  AnnotationDirection mBottomGridAnnotationDirection;
899  AnnotationFormat mGridAnnotationFormat;
900 
901  QString mGridAnnotationExpressionString;
902  mutable QScopedPointer< QgsExpression > mGridAnnotationExpression;
903 
904  FrameStyle mGridFrameStyle;
905  FrameSideFlags mGridFrameSides;
906  double mGridFrameWidth;
907  double mGridFramePenThickness;
908  QColor mGridFramePenColor;
909  QColor mGridFrameFillColor1;
910  QColor mGridFrameFillColor2;
911  double mCrossLength;
912 
914  DisplayMode mLeftFrameDivisions;
916  DisplayMode mRightFrameDivisions;
918  DisplayMode mTopFrameDivisions;
920  DisplayMode mBottomFrameDivisions;
921 
922  QgsLineSymbol* mGridLineSymbol;
923  QgsMarkerSymbol* mGridMarkerSymbol;
924 
926 
927  GridUnit mGridUnit;
928 
929  QPainter::CompositionMode mBlendMode;
930 
931  QList< QPair< double, QPolygonF > > mTransformedXLines;
932  QList< QPair< double, QPolygonF > > mTransformedYLines;
933  QList< QgsPoint > mTransformedIntersections;
934  QRectF mPrevPaintRect;
935  QPolygonF mPrevMapPolygon;
936 
937  class QgsMapAnnotation
938  {
939  public:
940  double coordinate;
941  QPointF itemPosition;
943  };
944 
945  void init();
946 
950  void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, GridExtension* extension = nullptr ) const;
951 
960  void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QgsExpressionContext& expressionContext, GridExtension* extension = nullptr ) const;
961 
965  void drawCoordinateAnnotation( QPainter* p, QPointF pos, const QString &annotationString, const AnnotationCoordinate coordinateType, GridExtension* extension = nullptr ) const;
966 
973  void drawAnnotation( QPainter* p, QPointF pos, int rotation, const QString& annotationText ) const;
974 
975  QString gridAnnotationString( double value, AnnotationCoordinate coord, QgsExpressionContext& expressionContext ) const;
976 
979  int xGridLines( QList< QPair< double, QLineF > >& lines ) const;
980 
983  int yGridLines( QList< QPair< double, QLineF > >& lines ) const;
984 
985  int xGridLinesCrsTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
986 
987  int yGridLinesCrsTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
988 
989  void drawGridLine( const QLineF& line, QgsRenderContext &context ) const;
990 
991  void drawGridLine( const QPolygonF& line, QgsRenderContext &context ) const;
992 
993  void sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
994  QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const;
995 
999  void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, BorderSide border, double* extension = nullptr ) const;
1000 
1005  BorderSide borderForLineCoord( QPointF p, const AnnotationCoordinate coordinateType ) const;
1006 
1008  int crsGridParams( QgsRectangle& crsRect, QgsCoordinateTransform& inverseTransform ) const;
1009 
1010  static QList<QPolygonF> trimLinesToMap( const QPolygonF &line, const QgsRectangle &rect );
1011 
1012  QPolygonF scalePolygon( const QPolygonF &polygon, const double scale ) const;
1013 
1015  void drawGridCrsTransform( QgsRenderContext &context, double dotsPerMM, QList< QPair< double, QLineF > > &horizontalLines,
1016  QList< QPair< double, QLineF > > &verticalLines , bool calculateLinesOnly = false );
1017 
1018  void drawGridNoTransform( QgsRenderContext &context, double dotsPerMM, QList<QPair<double, QLineF> > &horizontalLines, QList<QPair<double, QLineF> > &verticalLines, bool calculateLinesOnly = false ) const;
1019 
1020  void createDefaultGridLineSymbol();
1021 
1022  void createDefaultGridMarkerSymbol();
1023 
1024  void drawGridMarker( QPointF point, QgsRenderContext &context ) const;
1025 
1026  void drawGridFrameZebraBorder( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double* extension = nullptr ) const;
1027 
1028  void drawGridFrameTicks( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double* extension = nullptr ) const;
1029 
1030  void drawGridFrameLineBorder( QPainter *p, BorderSide border, double* extension = nullptr ) const;
1031 
1032  void calculateCrsTransformLines();
1033 
1034  bool shouldShowDivisionForSide( AnnotationCoordinate coordinate, BorderSide side ) const;
1035  bool shouldShowDivisionForDisplayMode( AnnotationCoordinate coordinate, DisplayMode mode ) const;
1036 
1037  friend class TestQgsComposerMapGrid;
1038 };
1039 
1040 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsComposerMapGrid::FrameSideFlags )
1041 
1042 #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:35
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 outline 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 outline 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, eg 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 outline 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 outline 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.