QGIS API Documentation  2.14.0-Essen
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() : top( 0.0 ), right( 0.0 ), bottom( 0.0 ), left( 0.0 ) {}
839  double top;
840  double right;
841  double bottom;
842  double left;
843  };
844 
845  /*True if a re-transformation of grid lines is required*/
846  bool mTransformDirty;
847 
849  GridStyle mGridStyle;
851  double mGridIntervalX;
853  double mGridIntervalY;
855  double mGridOffsetX;
857  double mGridOffsetY;
859  QFont mGridAnnotationFont;
861  QColor mGridAnnotationFontColor;
863  int mGridAnnotationPrecision;
865  bool mShowGridAnnotation;
866 
868  DisplayMode mLeftGridAnnotationDisplay;
870  DisplayMode mRightGridAnnotationDisplay;
872  DisplayMode mTopGridAnnotationDisplay;
874  DisplayMode mBottomGridAnnotationDisplay;
875 
877  AnnotationPosition mLeftGridAnnotationPosition;
879  AnnotationPosition mRightGridAnnotationPosition;
881  AnnotationPosition mTopGridAnnotationPosition;
883  AnnotationPosition mBottomGridAnnotationPosition;
884 
886  double mAnnotationFrameDistance;
887 
889  AnnotationDirection mLeftGridAnnotationDirection;
891  AnnotationDirection mRightGridAnnotationDirection;
893  AnnotationDirection mTopGridAnnotationDirection;
895  AnnotationDirection mBottomGridAnnotationDirection;
896  AnnotationFormat mGridAnnotationFormat;
897 
898  QString mGridAnnotationExpressionString;
899  mutable QScopedPointer< QgsExpression > mGridAnnotationExpression;
900 
901  FrameStyle mGridFrameStyle;
902  FrameSideFlags mGridFrameSides;
903  double mGridFrameWidth;
904  double mGridFramePenThickness;
905  QColor mGridFramePenColor;
906  QColor mGridFrameFillColor1;
907  QColor mGridFrameFillColor2;
908  double mCrossLength;
909 
911  DisplayMode mLeftFrameDivisions;
913  DisplayMode mRightFrameDivisions;
915  DisplayMode mTopFrameDivisions;
917  DisplayMode mBottomFrameDivisions;
918 
919  QgsLineSymbolV2* mGridLineSymbol;
920  QgsMarkerSymbolV2* mGridMarkerSymbol;
921 
923 
924  GridUnit mGridUnit;
925 
926  QPainter::CompositionMode mBlendMode;
927 
928  QList< QPair< double, QPolygonF > > mTransformedXLines;
929  QList< QPair< double, QPolygonF > > mTransformedYLines;
930  QList< QgsPoint > mTransformedIntersections;
931  QRectF mPrevPaintRect;
932  QPolygonF mPrevMapPolygon;
933 
934  class QgsMapAnnotation
935  {
936  public:
937  double coordinate;
938  QPointF itemPosition;
940  };
941 
942  void init();
943 
947  void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, GridExtension* extension = nullptr ) const;
948 
957  void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QgsExpressionContext& expressionContext, GridExtension* extension = nullptr ) const;
958 
962  void drawCoordinateAnnotation( QPainter* p, QPointF pos, QString annotationString, const AnnotationCoordinate coordinateType, GridExtension* extension = nullptr ) const;
963 
970  void drawAnnotation( QPainter* p, QPointF pos, int rotation, const QString& annotationText ) const;
971 
972  QString gridAnnotationString( double value, AnnotationCoordinate coord, QgsExpressionContext& expressionContext ) const;
973 
976  int xGridLines( QList< QPair< double, QLineF > >& lines ) const;
977 
980  int yGridLines( QList< QPair< double, QLineF > >& lines ) const;
981 
982  int xGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
983 
984  int yGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
985 
986  void drawGridLine( const QLineF& line, QgsRenderContext &context ) const;
987 
988  void drawGridLine( const QPolygonF& line, QgsRenderContext &context ) const;
989 
990  void sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
991  QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const;
992 
996  void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, BorderSide border, double* extension = nullptr ) const;
997 
1002  BorderSide borderForLineCoord( QPointF p, const AnnotationCoordinate coordinateType ) const;
1003 
1005  int crsGridParams( QgsRectangle& crsRect, QgsCoordinateTransform& inverseTransform ) const;
1006 
1007  static QList<QPolygonF> trimLinesToMap( const QPolygonF &line, const QgsRectangle &rect );
1008 
1009  QPolygonF scalePolygon( const QPolygonF &polygon, const double scale ) const;
1010 
1012  void drawGridCRSTransform( QgsRenderContext &context, double dotsPerMM, QList< QPair< double, QLineF > > &horizontalLines,
1013  QList< QPair< double, QLineF > > &verticalLines , bool calculateLinesOnly = false );
1014 
1015  void drawGridNoTransform( QgsRenderContext &context, double dotsPerMM, QList<QPair<double, QLineF> > &horizontalLines, QList<QPair<double, QLineF> > &verticalLines, bool calculateLinesOnly = false ) const;
1016 
1017  void createDefaultGridLineSymbol();
1018 
1019  void createDefaultGridMarkerSymbol();
1020 
1021  void drawGridMarker( QPointF point, QgsRenderContext &context ) const;
1022 
1023  void drawGridFrameZebraBorder( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double* extension = nullptr ) const;
1024 
1025  void drawGridFrameTicks( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double* extension = nullptr ) const;
1026 
1027  void drawGridFrameLineBorder( QPainter *p, BorderSide border, double* extension = nullptr ) const;
1028 
1029  void calculateCRSTransformLines();
1030 
1031  bool shouldShowDivisionForSide( AnnotationCoordinate coordinate, BorderSide side ) const;
1032  bool shouldShowDivisionForDisplayMode( AnnotationCoordinate coordinate, DisplayMode mode ) const;
1033 
1034  friend class TestQgsComposerMapGrid;
1035 };
1036 
1037 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsComposerMapGrid::FrameSideFlags )
1038 
1039 #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.