QGIS API Documentation  2.6.0-Brighton
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
122  QList< QgsComposerMapGrid* > asList() const;
123 
130  bool readXML( const QDomElement& elem, const QDomDocument& doc );
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  {
162  MapUnit, /*< grid units follow map units */
163  MM, /*< grid units in millimetres */
164  CM /*< grid units in centimetres */
165  };
166 
170  {
171  Solid = 0,
172  Cross, /*< draw line crosses at intersections of grid lines */
173  Markers, /*< draw markers at intersections of grid lines */
174  FrameAnnotationsOnly /*< no grid lines over the map, only draw frame and annotations */
175  };
176 
180  {
181  InsideMapFrame = 0,
182  OutsideMapFrame, /*< draw annotations outside the map frame */
183  Disabled /*< disable annotation */
184  };
185 
189  {
190  Horizontal = 0, /*< draw annotations horizontally */
191  Vertical, /*< draw annotations vertically */
192  BoundaryDirection /*< annotations follow the boundary direction */
193  };
194 
198  {
199  Decimal = 0, /*< decimal degrees, use - for S/W coordinates */
200  DegreeMinute, /*< degree/minutes, use NSEW suffix */
201  DegreeMinuteSecond, /*< degree/minutes/seconds, use NSEW suffix */
202  DecimalWithSuffix, /*< decimal degrees, use NSEW suffix */
203  DegreeMinuteNoSuffix, /*< degree/minutes, use - for S/W coordinates */
204  DegreeMinutePadded, /*< degree/minutes, with minutes using leading zeros were required */
205  DegreeMinuteSecondNoSuffix, /*< degree/minutes/seconds, use - for S/W coordinates */
206  DegreeMinuteSecondPadded /*< degree/minutes/seconds, with minutes using leading zeros were required */
207  };
208 
212  {
214  Right, /*< right border */
215  Bottom, /*< bottom border */
216  Top /*< top border */
217  };
218 
222  {
223  NoFrame = 0, /*< disable grid frame */
224  Zebra, /*< black/white pattern */
225  InteriorTicks, /*< tick markers drawn inside map frame */
226  ExteriorTicks, /*< tick markers drawn outside map frame */
227  InteriorExteriorTicks, /*< tick markers drawn both inside and outside the map frame */
228  LineBorder /*< simple solid line frame */
229  };
230 
234  {
235  FrameLeft = 0x01, /*< left side of map */
236  FrameRight = 0x02, /*< right side of map */
237  FrameTop = 0x04, /*< top side of map */
238  FrameBottom = 0x08 /*< bottom side of map */
239  };
240  Q_DECLARE_FLAGS( FrameSideFlags, FrameSideFlag )
241 
242 
245  {
246  Longitude = 0, /*< coordinate is a longitude value */
247  Latitude /*< coordinate is a latitude value */
248  };
249 
254  QgsComposerMapGrid( const QString& name, QgsComposerMap* map );
255 
256  virtual ~QgsComposerMapGrid();
257 
261  void draw( QPainter* painter );
262 
268  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
269 
275  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
276 
281  void setCrs( const QgsCoordinateReferenceSystem& crs );
282 
287  QgsCoordinateReferenceSystem crs() const { return mCRS; }
288 
293  void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
294 
299  QPainter::CompositionMode blendMode() const { return mBlendMode; }
300 
301  bool usesAdvancedEffects() const;
302 
307  double maxExtension() const;
308 
309  //
310  // GRID UNITS
311  //
312 
318  void setUnits( const GridUnit unit );
319 
325  GridUnit units() const { return mGridUnit; }
326 
333  void setIntervalX( const double interval );
334 
341  double intervalX() const { return mGridIntervalX; }
342 
349  void setIntervalY( const double interval );
350 
357  double intervalY() const { return mGridIntervalY; }
358 
365  void setOffsetX( const double offset );
366 
373  double offsetX() const { return mGridOffsetX; }
374 
381  void setOffsetY( const double offset );
382 
389  double offsetY() const { return mGridOffsetY; }
390 
391  //
392  // GRID APPEARANCE
393  //
394 
400  void setStyle( const GridStyle style );
401 
407  GridStyle style() const { return mGridStyle; }
408 
414  void setCrossLength( const double length ) { mCrossLength = length; }
415 
421  double crossLength() const { return mCrossLength; }
422 
430  void setGridLineWidth( const double width );
431 
439  void setGridLineColor( const QColor& color );
440 
448  void setLineSymbol( QgsLineSymbolV2* symbol );
449 
457  const QgsLineSymbolV2* lineSymbol() const { return mGridLineSymbol; }
458 
466  QgsLineSymbolV2* lineSymbol() { return mGridLineSymbol; }
467 
475  void setMarkerSymbol( QgsMarkerSymbolV2* symbol );
476 
484  const QgsMarkerSymbolV2* markerSymbol() const { return mGridMarkerSymbol; }
485 
493  QgsMarkerSymbolV2* markerSymbol() { return mGridMarkerSymbol; }
494 
495  //
496  // ANNOTATIONS
497  //
498 
503  void setAnnotationEnabled( const bool enabled ) { mShowGridAnnotation = enabled; }
504 
509  bool annotationEnabled() const { return mShowGridAnnotation; }
510 
515  void setAnnotationFont( const QFont& font ) { mGridAnnotationFont = font; }
516 
521  QFont annotationFont() const { return mGridAnnotationFont; }
522 
527  void setAnnotationFontColor( const QColor& color ) { mGridAnnotationFontColor = color; }
528 
533  QColor annotationFontColor() const { return mGridAnnotationFontColor; }
534 
539  void setAnnotationPrecision( const int precision ) { mGridAnnotationPrecision = precision; }
540 
545  int annotationPrecision() const { return mGridAnnotationPrecision; }
546 
553  void setAnnotationPosition( const AnnotationPosition position, const BorderSide border );
554 
561  AnnotationPosition annotationPosition( const BorderSide border ) const;
562 
567  void setAnnotationFrameDistance( const double distance ) { mAnnotationFrameDistance = distance; }
568 
573  double annotationFrameDistance() const { return mAnnotationFrameDistance; }
574 
580  void setAnnotationDirection( const AnnotationDirection direction, const BorderSide border );
581 
586  void setAnnotationDirection( const AnnotationDirection direction );
587 
593  AnnotationDirection annotationDirection( const BorderSide border ) const;
594 
599  void setAnnotationFormat( const AnnotationFormat format ) { mGridAnnotationFormat = format; }
600 
605  AnnotationFormat annotationFormat() const { return mGridAnnotationFormat; }
606 
607  //
608  // GRID FRAME
609  //
610 
615  void setFrameStyle( const FrameStyle style ) { mGridFrameStyle = style; }
616 
621  FrameStyle frameStyle() const { return mGridFrameStyle; }
622 
630  void setFrameSideFlags( const FrameSideFlags flags );
631 
639  void setFrameSideFlag( const FrameSideFlag flag, bool on = true );
640 
648  FrameSideFlags frameSideFlags() const;
649 
658  bool testFrameSideFlag( const FrameSideFlag flag ) const;
659 
666  void setFrameWidth( const double width ) { mGridFrameWidth = width; }
667 
674  double frameWidth() const { return mGridFrameWidth; }
675 
681  void setFramePenSize( const double width ) { mGridFramePenThickness = width; }
682 
688  double framePenSize() const { return mGridFramePenThickness; }
689 
697  void setFramePenColor( const QColor& color ) { mGridFramePenColor = color; }
698 
706  QColor framePenColor() const {return mGridFramePenColor;}
707 
714  void setFrameFillColor1( const QColor& color ) { mGridFrameFillColor1 = color; }
715 
722  QColor frameFillColor1() const { return mGridFrameFillColor1; }
723 
730  void setFrameFillColor2( const QColor& color ) { mGridFrameFillColor2 = color; }
731 
738  QColor frameFillColor2() const { return mGridFrameFillColor2; }
739 
740  private:
741 
742  QgsComposerMapGrid(); //forbidden
743 
744  /*True if a re-transformation of grid lines is required*/
745  bool mTransformDirty;
746 
748  GridStyle mGridStyle;
750  double mGridIntervalX;
752  double mGridIntervalY;
754  double mGridOffsetX;
756  double mGridOffsetY;
758  QFont mGridAnnotationFont;
760  QColor mGridAnnotationFontColor;
762  int mGridAnnotationPrecision;
764  bool mShowGridAnnotation;
765 
767  AnnotationPosition mLeftGridAnnotationPosition;
769  AnnotationPosition mRightGridAnnotationPosition;
771  AnnotationPosition mTopGridAnnotationPosition;
773  AnnotationPosition mBottomGridAnnotationPosition;
774 
776  double mAnnotationFrameDistance;
777 
779  AnnotationDirection mLeftGridAnnotationDirection;
781  AnnotationDirection mRightGridAnnotationDirection;
783  AnnotationDirection mTopGridAnnotationDirection;
785  AnnotationDirection mBottomGridAnnotationDirection;
786  AnnotationFormat mGridAnnotationFormat;
787  FrameStyle mGridFrameStyle;
788  FrameSideFlags mGridFrameSides;
789  double mGridFrameWidth;
790  double mGridFramePenThickness;
791  QColor mGridFramePenColor;
792  QColor mGridFrameFillColor1;
793  QColor mGridFrameFillColor2;
794  double mCrossLength;
795 
796  QgsLineSymbolV2* mGridLineSymbol;
797  QgsMarkerSymbolV2* mGridMarkerSymbol;
798 
800 
801  GridUnit mGridUnit;
802 
803  QPainter::CompositionMode mBlendMode;
804 
805  QList< QPair< double, QPolygonF > > mTransformedXLines;
806  QList< QPair< double, QPolygonF > > mTransformedYLines;
807  QList< QgsPoint > mTransformedIntersections;
808  QRectF mPrevPaintRect;
809  QPolygonF mPrevMapPolygon;
810 
811  class QgsMapAnnotation
812  {
813  public:
814  double coordinate;
815  QPointF itemPosition;
817  };
818 
820  void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines ) const;
821 
826  void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines ) const;
827 
828  void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString, const AnnotationCoordinate coordinateType ) const;
829 
836  void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText ) const;
837 
838  QString gridAnnotationString( double value, AnnotationCoordinate coord ) const;
839 
842  int xGridLines( QList< QPair< double, QLineF > >& lines ) const;
843 
846  int yGridLines( QList< QPair< double, QLineF > >& lines ) const;
847 
848  int xGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
849 
850  int yGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const;
851 
852  void drawGridLine( const QLineF& line, QgsRenderContext &context ) const;
853 
854  void drawGridLine( const QPolygonF& line, QgsRenderContext &context ) const;
855 
856  void sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
857  QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const;
858 
859  void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, BorderSide border ) const;
860 
865  BorderSide borderForLineCoord( const QPointF& p, const AnnotationCoordinate coordinateType ) const;
866 
868  int crsGridParams( QgsRectangle& crsRect, QgsCoordinateTransform& inverseTransform ) const;
869 
870  static QPolygonF trimLineToMap( const QPolygonF& line, const QgsRectangle& rect );
871 
872  QPolygonF scalePolygon( const QPolygonF &polygon, const double scale ) const;
873 
875  void drawGridCRSTransform( QgsRenderContext &context, double dotsPerMM, QList< QPair< double, QLineF > > &horizontalLines,
876  QList< QPair< double, QLineF > > &verticalLines );
877 
878  void drawGridNoTransform( QgsRenderContext &context, double dotsPerMM, QList<QPair<double, QLineF> > &horizontalLines, QList<QPair<double, QLineF> > &verticalLines ) const;
879 
880  void createDefaultGridLineSymbol();
881 
882  void createDefaultGridMarkerSymbol();
883 
884  void drawGridMarker( const QPointF &point, QgsRenderContext &context ) const;
885 
886  void drawGridFrameZebraBorder( QPainter *p, const QMap<double, double> &borderPos, BorderSide border ) const;
887 
888  void drawGridFrameTicks( QPainter *p, const QMap<double, double> &borderPos, BorderSide border ) const;
889 
890  void drawGridFrameLineBorder( QPainter *p, BorderSide border ) const;
891 
892  void calculateCRSTransformLines();
893 
894  friend class TestQgsComposerMapGrid;
895 };
896 
897 #endif // QGSCOMPOSERMAPGRID_H