QGIS API Documentation  2.11.0-Master
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 
136  double maxGridExtension() const;
137 
138 };
139 
140 //
141 // QgsComposerMapGrid
142 //
143 
151 class CORE_EXPORT QgsComposerMapGrid : public QgsComposerMapItem
152 {
153 
154  Q_OBJECT
155 
156  public:
157 
160  enum GridUnit
161  {
163  MM,
164  CM
165  };
166 
170  {
171  Solid = 0,
174  FrameAnnotationsOnly
175  };
176 
180  {
181  ShowAll = 0,
184  HideAll
185  };
186 
190  {
191  InsideMapFrame = 0,
193  Disabled
195  };
196 
200  {
201  Horizontal = 0,
204  BoundaryDirection
205  };
206 
210  {
211  Decimal = 0,
218  DegreeMinuteSecondPadded
219  };
220 
224  {
228  Top
229  };
230 
234  {
235  NoFrame = 0,
240  LineBorder
241  };
242 
246  {
247  FrameLeft = 0x01,
248  FrameRight = 0x02,
249  FrameTop = 0x04,
250  FrameBottom = 0x08
251  };
252  Q_DECLARE_FLAGS( FrameSideFlags, FrameSideFlag )
253 
254 
257  {
258  Longitude = 0,
259  Latitude
260  };
261 
266  QgsComposerMapGrid( const QString& name, QgsComposerMap* map );
267 
268  virtual ~QgsComposerMapGrid();
269 
273  void draw( QPainter* painter ) override;
274 
280  bool writeXML( QDomElement& elem, QDomDocument & doc ) const override;
281 
287  bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) override;
288 
293  void setCrs( const QgsCoordinateReferenceSystem& crs );
294 
299  QgsCoordinateReferenceSystem crs() const { return mCRS; }
300 
305  void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
306 
311  QPainter::CompositionMode blendMode() const { return mBlendMode; }
312 
313  bool usesAdvancedEffects() const override;
314 
319  double maxExtension() const;
320 
321  //
322  // GRID UNITS
323  //
324 
330  void setUnits( const GridUnit unit );
331 
337  GridUnit units() const { return mGridUnit; }
338 
345  void setIntervalX( const double interval );
346 
353  double intervalX() const { return mGridIntervalX; }
354 
361  void setIntervalY( const double interval );
362 
369  double intervalY() const { return mGridIntervalY; }
370 
377  void setOffsetX( const double offset );
378 
385  double offsetX() const { return mGridOffsetX; }
386 
393  void setOffsetY( const double offset );
394 
401  double offsetY() const { return mGridOffsetY; }
402 
403  //
404  // GRID APPEARANCE
405  //
406 
412  void setStyle( const GridStyle style );
413 
419  GridStyle style() const { return mGridStyle; }
420 
426  void setCrossLength( const double length ) { mCrossLength = length; }
427 
433  double crossLength() const { return mCrossLength; }
434 
442  void setGridLineWidth( const double width );
443 
451  void setGridLineColor( const QColor& color );
452 
460  void setLineSymbol( QgsLineSymbolV2* symbol );
461 
469  const QgsLineSymbolV2* lineSymbol() const { return mGridLineSymbol; }
470 
478  QgsLineSymbolV2* lineSymbol() { return mGridLineSymbol; }
479 
487  void setMarkerSymbol( QgsMarkerSymbolV2* symbol );
488 
496  const QgsMarkerSymbolV2* markerSymbol() const { return mGridMarkerSymbol; }
497 
505  QgsMarkerSymbolV2* markerSymbol() { return mGridMarkerSymbol; }
506 
507  //
508  // ANNOTATIONS
509  //
510 
515  void setAnnotationEnabled( const bool enabled ) { mShowGridAnnotation = enabled; }
516 
521  bool annotationEnabled() const { return mShowGridAnnotation; }
522 
527  void setAnnotationFont( const QFont& font ) { mGridAnnotationFont = font; }
528 
533  QFont annotationFont() const { return mGridAnnotationFont; }
534 
539  void setAnnotationFontColor( const QColor& color ) { mGridAnnotationFontColor = color; }
540 
545  QColor annotationFontColor() const { return mGridAnnotationFontColor; }
546 
551  void setAnnotationPrecision( const int precision ) { mGridAnnotationPrecision = precision; }
552 
557  int annotationPrecision() const { return mGridAnnotationPrecision; }
558 
566  void setAnnotationDisplay( const DisplayMode display, const BorderSide border );
567 
576  DisplayMode annotationDisplay( const BorderSide border ) const;
577 
584  void setAnnotationPosition( const AnnotationPosition position, const BorderSide border );
585 
592  AnnotationPosition annotationPosition( const BorderSide border ) const;
593 
598  void setAnnotationFrameDistance( const double distance ) { mAnnotationFrameDistance = distance; }
599 
604  double annotationFrameDistance() const { return mAnnotationFrameDistance; }
605 
611  void setAnnotationDirection( const AnnotationDirection direction, const BorderSide border );
612 
617  void setAnnotationDirection( const AnnotationDirection direction );
618 
624  AnnotationDirection annotationDirection( const BorderSide border ) const;
625 
630  void setAnnotationFormat( const AnnotationFormat format ) { mGridAnnotationFormat = format; }
631 
636  AnnotationFormat annotationFormat() const { return mGridAnnotationFormat; }
637 
638  //
639  // GRID FRAME
640  //
641 
646  void setFrameStyle( const FrameStyle style ) { mGridFrameStyle = style; }
647 
652  FrameStyle frameStyle() const { return mGridFrameStyle; }
653 
660  void setFrameDivisions( const DisplayMode divisions, const BorderSide border );
661 
668  DisplayMode frameDivisions( const BorderSide border ) const;
669 
677  void setFrameSideFlags( const FrameSideFlags flags );
678 
686  void setFrameSideFlag( const FrameSideFlag flag, bool on = true );
687 
695  FrameSideFlags frameSideFlags() const;
696 
705  bool testFrameSideFlag( const FrameSideFlag flag ) const;
706 
713  void setFrameWidth( const double width ) { mGridFrameWidth = width; }
714 
721  double frameWidth() const { return mGridFrameWidth; }
722 
728  void setFramePenSize( const double width ) { mGridFramePenThickness = width; }
729 
735  double framePenSize() const { return mGridFramePenThickness; }
736 
744  void setFramePenColor( const QColor& color ) { mGridFramePenColor = color; }
745 
753  QColor framePenColor() const {return mGridFramePenColor;}
754 
761  void setFrameFillColor1( const QColor& color ) { mGridFrameFillColor1 = color; }
762 
769  QColor frameFillColor1() const { return mGridFrameFillColor1; }
770 
777  void setFrameFillColor2( const QColor& color ) { mGridFrameFillColor2 = color; }
778 
785  QColor frameFillColor2() const { return mGridFrameFillColor2; }
786 
787  private:
788 
789  QgsComposerMapGrid(); //forbidden
790 
791  /*True if a re-transformation of grid lines is required*/
792  bool mTransformDirty;
793 
795  GridStyle mGridStyle;
797  double mGridIntervalX;
799  double mGridIntervalY;
801  double mGridOffsetX;
803  double mGridOffsetY;
805  QFont mGridAnnotationFont;
807  QColor mGridAnnotationFontColor;
809  int mGridAnnotationPrecision;
811  bool mShowGridAnnotation;
812 
814  DisplayMode mLeftGridAnnotationDisplay;
816  DisplayMode mRightGridAnnotationDisplay;
818  DisplayMode mTopGridAnnotationDisplay;
820  DisplayMode mBottomGridAnnotationDisplay;
821 
823  AnnotationPosition mLeftGridAnnotationPosition;
825  AnnotationPosition mRightGridAnnotationPosition;
827  AnnotationPosition mTopGridAnnotationPosition;
829  AnnotationPosition mBottomGridAnnotationPosition;
830 
832  double mAnnotationFrameDistance;
833 
835  AnnotationDirection mLeftGridAnnotationDirection;
837  AnnotationDirection mRightGridAnnotationDirection;
839  AnnotationDirection mTopGridAnnotationDirection;
841  AnnotationDirection mBottomGridAnnotationDirection;
842  AnnotationFormat mGridAnnotationFormat;
843  FrameStyle mGridFrameStyle;
844  FrameSideFlags mGridFrameSides;
845  double mGridFrameWidth;
846  double mGridFramePenThickness;
847  QColor mGridFramePenColor;
848  QColor mGridFrameFillColor1;
849  QColor mGridFrameFillColor2;
850  double mCrossLength;
851 
853  DisplayMode mLeftFrameDivisions;
855  DisplayMode mRightFrameDivisions;
857  DisplayMode mTopFrameDivisions;
859  DisplayMode mBottomFrameDivisions;
860 
861  QgsLineSymbolV2* mGridLineSymbol;
862  QgsMarkerSymbolV2* mGridMarkerSymbol;
863 
865 
866  GridUnit mGridUnit;
867 
868  QPainter::CompositionMode mBlendMode;
869 
870  QList< QPair< double, QPolygonF > > mTransformedXLines;
871  QList< QPair< double, QPolygonF > > mTransformedYLines;
872  QList< QgsPoint > mTransformedIntersections;
873  QRectF mPrevPaintRect;
874  QPolygonF mPrevMapPolygon;
875 
876  class QgsMapAnnotation
877  {
878  public:
879  double coordinate;
880  QPointF itemPosition;
882  };
883 
884  void init();
885 
887  void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines ) const;
888 
893  void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines ) const;
894 
895  void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString, const AnnotationCoordinate coordinateType ) const;
896 
903  void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText ) const;
904 
905  QString gridAnnotationString( double value, AnnotationCoordinate coord ) const;
906 
909  int xGridLines( QList< QPair< double, QLineF > >& lines ) const;
910 
913  int yGridLines( QList< QPair< double, QLineF > >& lines ) const;
914 
915  int xGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
916 
917  int yGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
918 
919  void drawGridLine( const QLineF& line, QgsRenderContext &context ) const;
920 
921  void drawGridLine( const QPolygonF& line, QgsRenderContext &context ) const;
922 
923  void sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
924  QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const;
925 
926  void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, BorderSide border ) const;
927 
932  BorderSide borderForLineCoord( const QPointF& p, const AnnotationCoordinate coordinateType ) const;
933 
935  int crsGridParams( QgsRectangle& crsRect, QgsCoordinateTransform& inverseTransform ) const;
936 
937  static QList<QPolygonF> trimLinesToMap( const QPolygonF &line, const QgsRectangle &rect );
938 
939  QPolygonF scalePolygon( const QPolygonF &polygon, const double scale ) const;
940 
942  void drawGridCRSTransform( QgsRenderContext &context, double dotsPerMM, QList< QPair< double, QLineF > > &horizontalLines,
943  QList< QPair< double, QLineF > > &verticalLines );
944 
945  void drawGridNoTransform( QgsRenderContext &context, double dotsPerMM, QList<QPair<double, QLineF> > &horizontalLines, QList<QPair<double, QLineF> > &verticalLines ) const;
946 
947  void createDefaultGridLineSymbol();
948 
949  void createDefaultGridMarkerSymbol();
950 
951  void drawGridMarker( const QPointF &point, QgsRenderContext &context ) const;
952 
953  void drawGridFrameZebraBorder( QPainter *p, const QMap<double, double> &borderPos, BorderSide border ) const;
954 
955  void drawGridFrameTicks( QPainter *p, const QMap<double, double> &borderPos, BorderSide border ) const;
956 
957  void drawGridFrameLineBorder( QPainter *p, BorderSide border ) const;
958 
959  void calculateCRSTransformLines();
960 
961  bool shouldShowDivisionForSide( const AnnotationCoordinate &coordinate, const QgsComposerMapGrid::BorderSide& side ) const;
962  bool shouldShowDivisionForDisplayMode( const QgsComposerMapGrid::AnnotationCoordinate &coordinate, const QgsComposerMapGrid::DisplayMode &mode ) const;
963 
964  friend class TestQgsComposerMapGrid;
965 };
966 
967 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsComposerMapGrid::FrameSideFlags )
968 
969 #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.
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.
QList< QgsComposerMapItem * > asList() const
Returns a list of QgsComposerMapItems contained by the stack.
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.
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.