QGIS API Documentation  2.12.0-Lyon
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 
334 
345  void calculateMaxExtension( double& top, double& right, double& bottom, double& left );
346 
347  //
348  // GRID UNITS
349  //
350 
356  void setUnits( const GridUnit unit );
357 
363  GridUnit units() const { return mGridUnit; }
364 
371  void setIntervalX( const double interval );
372 
379  double intervalX() const { return mGridIntervalX; }
380 
387  void setIntervalY( const double interval );
388 
395  double intervalY() const { return mGridIntervalY; }
396 
403  void setOffsetX( const double offset );
404 
411  double offsetX() const { return mGridOffsetX; }
412 
419  void setOffsetY( const double offset );
420 
427  double offsetY() const { return mGridOffsetY; }
428 
429  //
430  // GRID APPEARANCE
431  //
432 
438  void setStyle( const GridStyle style );
439 
445  GridStyle style() const { return mGridStyle; }
446 
452  void setCrossLength( const double length ) { mCrossLength = length; }
453 
459  double crossLength() const { return mCrossLength; }
460 
468  void setGridLineWidth( const double width );
469 
477  void setGridLineColor( const QColor& color );
478 
486  void setLineSymbol( QgsLineSymbolV2* symbol );
487 
495  const QgsLineSymbolV2* lineSymbol() const { return mGridLineSymbol; }
496 
504  QgsLineSymbolV2* lineSymbol() { return mGridLineSymbol; }
505 
513  void setMarkerSymbol( QgsMarkerSymbolV2* symbol );
514 
522  const QgsMarkerSymbolV2* markerSymbol() const { return mGridMarkerSymbol; }
523 
531  QgsMarkerSymbolV2* markerSymbol() { return mGridMarkerSymbol; }
532 
533  //
534  // ANNOTATIONS
535  //
536 
541  void setAnnotationEnabled( const bool enabled ) { mShowGridAnnotation = enabled; }
542 
547  bool annotationEnabled() const { return mShowGridAnnotation; }
548 
553  void setAnnotationFont( const QFont& font ) { mGridAnnotationFont = font; }
554 
559  QFont annotationFont() const { return mGridAnnotationFont; }
560 
565  void setAnnotationFontColor( const QColor& color ) { mGridAnnotationFontColor = color; }
566 
571  QColor annotationFontColor() const { return mGridAnnotationFontColor; }
572 
577  void setAnnotationPrecision( const int precision ) { mGridAnnotationPrecision = precision; }
578 
583  int annotationPrecision() const { return mGridAnnotationPrecision; }
584 
592  void setAnnotationDisplay( const DisplayMode display, const BorderSide border );
593 
602  DisplayMode annotationDisplay( const BorderSide border ) const;
603 
610  void setAnnotationPosition( const AnnotationPosition position, const BorderSide border );
611 
618  AnnotationPosition annotationPosition( const BorderSide border ) const;
619 
624  void setAnnotationFrameDistance( const double distance ) { mAnnotationFrameDistance = distance; }
625 
630  double annotationFrameDistance() const { return mAnnotationFrameDistance; }
631 
637  void setAnnotationDirection( const AnnotationDirection direction, const BorderSide border );
638 
643  void setAnnotationDirection( const AnnotationDirection direction );
644 
650  AnnotationDirection annotationDirection( const BorderSide border ) const;
651 
656  void setAnnotationFormat( const AnnotationFormat format ) { mGridAnnotationFormat = format; }
657 
662  AnnotationFormat annotationFormat() const { return mGridAnnotationFormat; }
663 
670  void setAnnotationExpression( const QString& expression ) { mGridAnnotationExpressionString = expression; mGridAnnotationExpression.reset(); }
671 
678  QString annotationExpression() const { return mGridAnnotationExpressionString; }
679 
680  //
681  // GRID FRAME
682  //
683 
688  void setFrameStyle( const FrameStyle style ) { mGridFrameStyle = style; }
689 
694  FrameStyle frameStyle() const { return mGridFrameStyle; }
695 
702  void setFrameDivisions( const DisplayMode divisions, const BorderSide border );
703 
710  DisplayMode frameDivisions( const BorderSide border ) const;
711 
719  void setFrameSideFlags( const FrameSideFlags& flags );
720 
728  void setFrameSideFlag( const FrameSideFlag flag, bool on = true );
729 
737  FrameSideFlags frameSideFlags() const;
738 
747  bool testFrameSideFlag( const FrameSideFlag flag ) const;
748 
755  void setFrameWidth( const double width ) { mGridFrameWidth = width; }
756 
763  double frameWidth() const { return mGridFrameWidth; }
764 
770  void setFramePenSize( const double width ) { mGridFramePenThickness = width; }
771 
777  double framePenSize() const { return mGridFramePenThickness; }
778 
786  void setFramePenColor( const QColor& color ) { mGridFramePenColor = color; }
787 
795  QColor framePenColor() const {return mGridFramePenColor;}
796 
803  void setFrameFillColor1( const QColor& color ) { mGridFrameFillColor1 = color; }
804 
811  QColor frameFillColor1() const { return mGridFrameFillColor1; }
812 
819  void setFrameFillColor2( const QColor& color ) { mGridFrameFillColor2 = color; }
820 
827  QColor frameFillColor2() const { return mGridFrameFillColor2; }
828 
829  virtual QgsExpressionContext* createExpressionContext() const override;
830 
831  private:
832 
833  QgsComposerMapGrid(); //forbidden
834 
835  struct GridExtension
836  {
837  GridExtension() : top( 0.0 ), right( 0.0 ), bottom( 0.0 ), left( 0.0 ) {}
838  double top;
839  double right;
840  double bottom;
841  double left;
842  };
843 
844  /*True if a re-transformation of grid lines is required*/
845  bool mTransformDirty;
846 
848  GridStyle mGridStyle;
850  double mGridIntervalX;
852  double mGridIntervalY;
854  double mGridOffsetX;
856  double mGridOffsetY;
858  QFont mGridAnnotationFont;
860  QColor mGridAnnotationFontColor;
862  int mGridAnnotationPrecision;
864  bool mShowGridAnnotation;
865 
867  DisplayMode mLeftGridAnnotationDisplay;
869  DisplayMode mRightGridAnnotationDisplay;
871  DisplayMode mTopGridAnnotationDisplay;
873  DisplayMode mBottomGridAnnotationDisplay;
874 
876  AnnotationPosition mLeftGridAnnotationPosition;
878  AnnotationPosition mRightGridAnnotationPosition;
880  AnnotationPosition mTopGridAnnotationPosition;
882  AnnotationPosition mBottomGridAnnotationPosition;
883 
885  double mAnnotationFrameDistance;
886 
888  AnnotationDirection mLeftGridAnnotationDirection;
890  AnnotationDirection mRightGridAnnotationDirection;
892  AnnotationDirection mTopGridAnnotationDirection;
894  AnnotationDirection mBottomGridAnnotationDirection;
895  AnnotationFormat mGridAnnotationFormat;
896 
897  QString mGridAnnotationExpressionString;
898  mutable QScopedPointer< QgsExpression > mGridAnnotationExpression;
899 
900  FrameStyle mGridFrameStyle;
901  FrameSideFlags mGridFrameSides;
902  double mGridFrameWidth;
903  double mGridFramePenThickness;
904  QColor mGridFramePenColor;
905  QColor mGridFrameFillColor1;
906  QColor mGridFrameFillColor2;
907  double mCrossLength;
908 
910  DisplayMode mLeftFrameDivisions;
912  DisplayMode mRightFrameDivisions;
914  DisplayMode mTopFrameDivisions;
916  DisplayMode mBottomFrameDivisions;
917 
918  QgsLineSymbolV2* mGridLineSymbol;
919  QgsMarkerSymbolV2* mGridMarkerSymbol;
920 
922 
923  GridUnit mGridUnit;
924 
925  QPainter::CompositionMode mBlendMode;
926 
927  QList< QPair< double, QPolygonF > > mTransformedXLines;
928  QList< QPair< double, QPolygonF > > mTransformedYLines;
929  QList< QgsPoint > mTransformedIntersections;
930  QRectF mPrevPaintRect;
931  QPolygonF mPrevMapPolygon;
932 
933  class QgsMapAnnotation
934  {
935  public:
936  double coordinate;
937  QPointF itemPosition;
939  };
940 
941  void init();
942 
946  void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, GridExtension* extension = 0 ) const;
947 
956  void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QgsExpressionContext& expressionContext, GridExtension* extension = 0 ) const;
957 
961  void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString, const AnnotationCoordinate coordinateType, GridExtension* extension = 0 ) const;
962 
969  void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText ) const;
970 
971  QString gridAnnotationString( double value, AnnotationCoordinate coord, QgsExpressionContext& expressionContext ) const;
972 
975  int xGridLines( QList< QPair< double, QLineF > >& lines ) const;
976 
979  int yGridLines( QList< QPair< double, QLineF > >& lines ) const;
980 
981  int xGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
982 
983  int yGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
984 
985  void drawGridLine( const QLineF& line, QgsRenderContext &context ) const;
986 
987  void drawGridLine( const QPolygonF& line, QgsRenderContext &context ) const;
988 
989  void sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
990  QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const;
991 
995  void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, BorderSide border, double* extension = 0 ) const;
996 
1001  BorderSide borderForLineCoord( const QPointF& p, const AnnotationCoordinate coordinateType ) const;
1002 
1004  int crsGridParams( QgsRectangle& crsRect, QgsCoordinateTransform& inverseTransform ) const;
1005 
1006  static QList<QPolygonF> trimLinesToMap( const QPolygonF &line, const QgsRectangle &rect );
1007 
1008  QPolygonF scalePolygon( const QPolygonF &polygon, const double scale ) const;
1009 
1011  void drawGridCRSTransform( QgsRenderContext &context, double dotsPerMM, QList< QPair< double, QLineF > > &horizontalLines,
1012  QList< QPair< double, QLineF > > &verticalLines , bool calculateLinesOnly = false );
1013 
1014  void drawGridNoTransform( QgsRenderContext &context, double dotsPerMM, QList<QPair<double, QLineF> > &horizontalLines, QList<QPair<double, QLineF> > &verticalLines, bool calculateLinesOnly = false ) const;
1015 
1016  void createDefaultGridLineSymbol();
1017 
1018  void createDefaultGridMarkerSymbol();
1019 
1020  void drawGridMarker( const QPointF &point, QgsRenderContext &context ) const;
1021 
1022  void drawGridFrameZebraBorder( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double* extension = 0 ) const;
1023 
1024  void drawGridFrameTicks( QPainter *p, const QMap<double, double> &borderPos, BorderSide border, double* extension = 0 ) const;
1025 
1026  void drawGridFrameLineBorder( QPainter *p, BorderSide border, double* extension = 0 ) const;
1027 
1028  void calculateCRSTransformLines();
1029 
1030  bool shouldShowDivisionForSide( const AnnotationCoordinate &coordinate, const QgsComposerMapGrid::BorderSide& side ) const;
1031  bool shouldShowDivisionForDisplayMode( const QgsComposerMapGrid::AnnotationCoordinate &coordinate, const QgsComposerMapGrid::DisplayMode &mode ) const;
1032 
1033  friend class TestQgsComposerMapGrid;
1034 };
1035 
1036 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsComposerMapGrid::FrameSideFlags )
1037 
1038 #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's 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's style, which controls how the grid is drawn over the map'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'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.