QGIS API Documentation  2.15.0-Master (5f66276)
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 "qgsrendercontext.h"
25 #include <QString>
26 #include <QPainter>
27 
29 class QgsLineSymbolV2;
30 class QgsMarkerSymbolV2;
31 class QgsComposerMapGrid;
32 class QgsComposerMap;
33 class QDomDocument;
34 class QDomElement;
35 class QPainter;
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 
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  Disabled
207  };
208 
212  {
213  Horizontal = 0,
216  BoundaryDirection
217  };
218 
222  {
223  Decimal = 0,
231  CustomFormat
232  };
233 
237  {
241  Top
242  };
243 
247  {
248  NoFrame = 0,
253  LineBorder
254  };
255 
259  {
260  FrameLeft = 0x01,
261  FrameRight = 0x02,
262  FrameTop = 0x04,
263  FrameBottom = 0x08
264  };
265  Q_DECLARE_FLAGS( FrameSideFlags, FrameSideFlag )
266 
267 
270  {
271  Longitude = 0,
272  Latitude
273  };
274 
279  QgsComposerMapGrid( const QString& name, QgsComposerMap* map );
280 
281  virtual ~QgsComposerMapGrid();
282 
286  void draw( QPainter* painter ) override;
287 
293  bool writeXML( QDomElement& elem, QDomDocument & doc ) const override;
294 
300  bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) override;
301 
306  void setCrs( const QgsCoordinateReferenceSystem& crs );
307 
312  QgsCoordinateReferenceSystem crs() const { return mCRS; }
313 
318  void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
319 
324  QPainter::CompositionMode blendMode() const { return mBlendMode; }
325 
326  bool usesAdvancedEffects() const override;
327 
332  double maxExtension();
333 
344  void calculateMaxExtension( double& top, double& right, double& bottom, double& left );
345 
346  //
347  // GRID UNITS
348  //
349 
355  void setUnits( const GridUnit unit );
356 
362  GridUnit units() const { return mGridUnit; }
363 
370  void setIntervalX( const double interval );
371 
378  double intervalX() const { return mGridIntervalX; }
379 
386  void setIntervalY( const double interval );
387 
394  double intervalY() const { return mGridIntervalY; }
395 
402  void setOffsetX( const double offset );
403 
410  double offsetX() const { return mGridOffsetX; }
411 
418  void setOffsetY( const double offset );
419 
426  double offsetY() const { return mGridOffsetY; }
427 
428  //
429  // GRID APPEARANCE
430  //
431 
437  void setStyle( const GridStyle style );
438 
444  GridStyle style() const { return mGridStyle; }
445 
451  void setCrossLength( const double length ) { mCrossLength = length; }
452 
458  double crossLength() const { return mCrossLength; }
459 
467  void setGridLineWidth( const double width );
468 
476  void setGridLineColor( const QColor& color );
477 
485  void setLineSymbol( QgsLineSymbolV2* symbol );
486 
495  const QgsLineSymbolV2* lineSymbol() const { return mGridLineSymbol; }
496 
504  QgsLineSymbolV2* lineSymbol() { return mGridLineSymbol; }
505 
513  void setMarkerSymbol( QgsMarkerSymbolV2* symbol );
514 
523  const QgsMarkerSymbolV2* markerSymbol() const { return mGridMarkerSymbol; }
524 
532  QgsMarkerSymbolV2* markerSymbol() { return mGridMarkerSymbol; }
533 
534  //
535  // ANNOTATIONS
536  //
537 
542  void setAnnotationEnabled( const bool enabled ) { mShowGridAnnotation = enabled; }
543 
548  bool annotationEnabled() const { return mShowGridAnnotation; }
549 
554  void setAnnotationFont( const QFont& font ) { mGridAnnotationFont = font; }
555 
560  QFont annotationFont() const { return mGridAnnotationFont; }
561 
566  void setAnnotationFontColor( const QColor& color ) { mGridAnnotationFontColor = color; }
567 
572  QColor annotationFontColor() const { return mGridAnnotationFontColor; }
573 
578  void setAnnotationPrecision( const int precision ) { mGridAnnotationPrecision = precision; }
579 
584  int annotationPrecision() const { return mGridAnnotationPrecision; }
585 
593  void setAnnotationDisplay( const DisplayMode display, const BorderSide border );
594 
603  DisplayMode annotationDisplay( const BorderSide border ) const;
604 
611  void setAnnotationPosition( const AnnotationPosition position, const BorderSide border );
612 
619  AnnotationPosition annotationPosition( const BorderSide border ) const;
620 
625  void setAnnotationFrameDistance( const double distance ) { mAnnotationFrameDistance = distance; }
626 
631  double annotationFrameDistance() const { return mAnnotationFrameDistance; }
632 
638  void setAnnotationDirection( const AnnotationDirection direction, const BorderSide border );
639 
644  void setAnnotationDirection( const AnnotationDirection direction );
645 
651  AnnotationDirection annotationDirection( const BorderSide border ) const;
652 
657  void setAnnotationFormat( const AnnotationFormat format ) { mGridAnnotationFormat = format; }
658 
663  AnnotationFormat annotationFormat() const { return mGridAnnotationFormat; }
664 
671  void setAnnotationExpression( const QString& expression ) { mGridAnnotationExpressionString = expression; mGridAnnotationExpression.reset(); }
672 
679  QString annotationExpression() const { return mGridAnnotationExpressionString; }
680 
681  //
682  // GRID FRAME
683  //
684 
689  void setFrameStyle( const FrameStyle style ) { mGridFrameStyle = style; }
690 
695  FrameStyle frameStyle() const { return mGridFrameStyle; }
696 
703  void setFrameDivisions( const DisplayMode divisions, const BorderSide border );
704 
711  DisplayMode frameDivisions( const BorderSide border ) const;
712 
720  void setFrameSideFlags( const FrameSideFlags& flags );
721 
729  void setFrameSideFlag( const FrameSideFlag flag, bool on = true );
730 
738  FrameSideFlags frameSideFlags() const;
739 
748  bool testFrameSideFlag( const FrameSideFlag flag ) const;
749 
756  void setFrameWidth( const double width ) { mGridFrameWidth = width; }
757 
764  double frameWidth() const { return mGridFrameWidth; }
765 
771  void setFramePenSize( const double width ) { mGridFramePenThickness = width; }
772 
778  double framePenSize() const { return mGridFramePenThickness; }
779 
787  void setFramePenColor( const QColor& color ) { mGridFramePenColor = color; }
788 
796  QColor framePenColor() const {return mGridFramePenColor;}
797 
804  void setFrameFillColor1( const QColor& color ) { mGridFrameFillColor1 = color; }
805 
812  QColor frameFillColor1() const { return mGridFrameFillColor1; }
813 
820  void setFrameFillColor2( const QColor& color ) { mGridFrameFillColor2 = color; }
821 
828  QColor frameFillColor2() const { return mGridFrameFillColor2; }
829 
830  virtual QgsExpressionContext* createExpressionContext() const override;
831 
832  private:
833 
834  QgsComposerMapGrid(); //forbidden
835 
836  struct GridExtension
837  {
838  GridExtension()
839  : top( 0.0 )
840  , right( 0.0 )
841  , bottom( 0.0 )
842  , left( 0.0 )
843  {}
844  double top;
845  double right;
846  double bottom;
847  double left;
848  };
849 
850  /*True if a re-transformation of grid lines is required*/
851  bool mTransformDirty;
852 
854  GridStyle mGridStyle;
856  double mGridIntervalX;
858  double mGridIntervalY;
860  double mGridOffsetX;
862  double mGridOffsetY;
864  QFont mGridAnnotationFont;
866  QColor mGridAnnotationFontColor;
868  int mGridAnnotationPrecision;
870  bool mShowGridAnnotation;
871 
873  DisplayMode mLeftGridAnnotationDisplay;
875  DisplayMode mRightGridAnnotationDisplay;
877  DisplayMode mTopGridAnnotationDisplay;
879  DisplayMode mBottomGridAnnotationDisplay;
880 
882  AnnotationPosition mLeftGridAnnotationPosition;
884  AnnotationPosition mRightGridAnnotationPosition;
886  AnnotationPosition mTopGridAnnotationPosition;
888  AnnotationPosition mBottomGridAnnotationPosition;
889 
891  double mAnnotationFrameDistance;
892 
894  AnnotationDirection mLeftGridAnnotationDirection;
896  AnnotationDirection mRightGridAnnotationDirection;
898  AnnotationDirection mTopGridAnnotationDirection;
900  AnnotationDirection mBottomGridAnnotationDirection;
901  AnnotationFormat mGridAnnotationFormat;
902 
903  QString mGridAnnotationExpressionString;
904  mutable QScopedPointer< QgsExpression > mGridAnnotationExpression;
905 
906  FrameStyle mGridFrameStyle;
907  FrameSideFlags mGridFrameSides;
908  double mGridFrameWidth;
909  double mGridFramePenThickness;
910  QColor mGridFramePenColor;
911  QColor mGridFrameFillColor1;
912  QColor mGridFrameFillColor2;
913  double mCrossLength;
914 
916  DisplayMode mLeftFrameDivisions;
918  DisplayMode mRightFrameDivisions;
920  DisplayMode mTopFrameDivisions;
922  DisplayMode mBottomFrameDivisions;
923 
924  QgsLineSymbolV2* mGridLineSymbol;
925  QgsMarkerSymbolV2* mGridMarkerSymbol;
926 
928 
929  GridUnit mGridUnit;
930 
931  QPainter::CompositionMode mBlendMode;
932 
933  QList< QPair< double, QPolygonF > > mTransformedXLines;
934  QList< QPair< double, QPolygonF > > mTransformedYLines;
935  QList< QgsPoint > mTransformedIntersections;
936  QRectF mPrevPaintRect;
937  QPolygonF mPrevMapPolygon;
938 
939  class QgsMapAnnotation
940  {
941  public:
942  double coordinate;
943  QPointF itemPosition;
945  };
946 
947  void init();
948 
952  void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, GridExtension* extension = nullptr ) const;
953 
962  void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QgsExpressionContext& expressionContext, GridExtension* extension = nullptr ) const;
963 
967  void drawCoordinateAnnotation( QPainter* p, QPointF pos, QString annotationString, const AnnotationCoordinate coordinateType, GridExtension* extension = nullptr ) const;
968 
975  void drawAnnotation( QPainter* p, QPointF pos, int rotation, const QString& annotationText ) const;
976 
977  QString gridAnnotationString( double value, AnnotationCoordinate coord, QgsExpressionContext& expressionContext ) const;
978 
981  int xGridLines( QList< QPair< double, QLineF > >& lines ) const;
982 
985  int yGridLines( QList< QPair< double, QLineF > >& lines ) const;
986 
987  int xGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
988 
989  int yGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
990 
991  void drawGridLine( const QLineF& line, QgsRenderContext &context ) const;
992 
993  void drawGridLine( const QPolygonF& line, QgsRenderContext &context ) const;
994 
995  void sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
996  QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const;
997 
1001  void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, BorderSide border, double* extension = nullptr ) const;
1002 
1007  BorderSide borderForLineCoord( QPointF p, const AnnotationCoordinate coordinateType ) const;
1008 
1010  int crsGridParams( QgsRectangle& crsRect, QgsCoordinateTransform& inverseTransform ) const;
1011 
1012  static QList<QPolygonF> trimLinesToMap( const QPolygonF &line, const QgsRectangle &rect );
1013 
1014  QPolygonF scalePolygon( const QPolygonF &polygon, const double scale ) const;
1015 
1017  void drawGridCRSTransform( QgsRenderContext &context, double dotsPerMM, QList< QPair< double, QLineF > > &horizontalLines,
1018  QList< QPair< double, QLineF > > &verticalLines , bool calculateLinesOnly = false );
1019 
1020  void drawGridNoTransform( QgsRenderContext &context, double dotsPerMM, QList<QPair<double, QLineF> > &horizontalLines, QList<QPair<double, QLineF> > &verticalLines, bool calculateLinesOnly = false ) const;
1021 
1022  void createDefaultGridLineSymbol();
1023 
1024  void createDefaultGridMarkerSymbol();
1025 
1026  void drawGridMarker( QPointF point, QgsRenderContext &context ) const;
1027 
1028  void drawGridFrameZebraBorder( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double* extension = nullptr ) const;
1029 
1030  void drawGridFrameTicks( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double* extension = nullptr ) const;
1031 
1032  void drawGridFrameLineBorder( QPainter *p, BorderSide border, double* extension = nullptr ) const;
1033 
1034  void calculateCRSTransformLines();
1035 
1036  bool shouldShowDivisionForSide( AnnotationCoordinate coordinate, BorderSide side ) const;
1037  bool shouldShowDivisionForDisplayMode( AnnotationCoordinate coordinate, DisplayMode mode ) const;
1038 
1039  friend class TestQgsComposerMapGrid;
1040 };
1041 
1042 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsComposerMapGrid::FrameSideFlags )
1043 
1044 #endif // QGSCOMPOSERMAPGRID_H
QColor frameFillColor1() const
Retrieves the first fill color for the grid frame.
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
const QgsMarkerSymbolV2 * markerSymbol() const
Gets the marker symbol used for drawing grid points.
double intervalX() const
Gets the interval between grid lines in the x-direction.
void setBlendMode(const QPainter::CompositionMode mode)
Sets the blending mode used for drawing the grid.
GridStyle
Grid drawing style.
virtual void draw(QPainter *painter)=0
Draws the item on to a painter.
const QgsLineSymbolV2 * lineSymbol() const
Gets the line symbol used for drawing grid lines.
void setFramePenSize(const double width)
Sets the width of the outline drawn in the grid frame.
A collection of grids which is drawn above the map content in a QgsComposerMap.
FrameStyle
Style for grid frame.
double annotationFrameDistance() const
Gets the distance between the map frame and annotations.
QColor frameFillColor2() const
Retrieves the second fill color for the grid frame.
void setAnnotationExpression(const QString &expression)
Sets the expression used for drawing grid annotations.
FrameStyle frameStyle() const
Gets the grid frame style.
void setAnnotationFont(const QFont &font)
Sets the font used for drawing grid annotations.
QColor framePenColor() const
Retrieves the color of the outline drawn in the grid frame.
AnnotationDirection
Direction of grid annotations.
QgsMarkerSymbolV2 * markerSymbol()
Gets the marker symbol used for drawing grid points.
AnnotationFormat annotationFormat() const
Gets the format for drawing grid annotations.
AnnotationFormat
Format for displaying grid annotations.
An item which is drawn inside a QgsComposerMap, eg a grid or map overview.
double intervalY() const
Gets the interval between grid lines in the y-direction.
void setCrossLength(const double length)
Sets the length of the cross segments drawn for the grid.
QFont annotationFont() const
Gets the font used for drawing grid annotations.
virtual QgsExpressionContext * createExpressionContext() const
Creates an expression context relating to the objects&#39; current state.
QList< QgsComposerMapItem * > asList() const
Returns a list of QgsComposerMapItems contained by the stack.
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 offsetX() const
Gets the offset for grid lines in the x-direction.
void setAnnotationEnabled(const bool enabled)
Sets whether annotations should be shown for the grid.
QgsComposerMapItem & operator[](int idx)
Returns a reference to an item within the stack.
GridStyle style() const
Gets the grid&#39;s style, which controls how the grid is drawn over the map&#39;s contents.
FrameSideFlag
Flags for controlling which side of the map a frame is drawn on.
An individual grid which is drawn above the map content in a QgsComposerMap.
QgsLineSymbolV2 * lineSymbol()
Gets the line symbol used for drawing grid lines.
double offsetY() const
Gets the offset for grid lines in the y-direction.
double framePenSize() const
Retrieves the width of the outline drawn in the grid frame.
GridUnit units() const
Gets the units used for grid measurements such as the interval and offset for grid lines...
void setFrameWidth(const double width)
Sets the grid frame width.
void setAnnotationFontColor(const QColor &color)
Sets the font color used for drawing grid annotations.
Object representing map window.
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.
int annotationPrecision() const
Returns the coordinate precision for grid annotations.
QPainter::CompositionMode blendMode() const
Retrieves the blending mode used for drawing the grid.
QString annotationExpression() const
Returns the expression used for drawing grid annotations.
virtual bool enabled() const
Returns whether the item will be drawn.
QColor annotationFontColor() const
Gets the font color used for drawing grid annotations.
Contains information about the context of a rendering operation.
A collection of map items which are drawn above the map content in a QgsComposerMap.
Class for storing 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.
bool annotationEnabled() const
Gets whether annotations are shown for the grid.
double crossLength() const
Retrieves the length of the cross segments drawn for the grid.
virtual bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
Sets map item state from a DOM document.
virtual bool usesAdvancedEffects() const
Returns true if the item is drawn using advanced effects, such as blend modes.
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.
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const override
Stores map item state in DOM element.
virtual bool readXML(const QDomElement &elem, const QDomDocument &doc)=0
Sets the item stack&#39;s state from a DOM document.
QgsCoordinateReferenceSystem crs() const
Retrieves the CRS for the grid.
GridUnit
Unit for grid values.
AnnotationCoordinate
Annotation coordinate type.
void setAnnotationFrameDistance(const double distance)
Sets the distance between the map frame and annotations.
double frameWidth() const
Gets the grid frame width.