QGIS API Documentation  2.8.2-Wien
 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 ) 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  {
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  ShowAll = 0, /*< show both latitude and longitude annotations/divisions */
182  LatitudeOnly, /*< show latitude/y annotations/divisions only */
183  LongitudeOnly, /*< show longitude/x annotations/divisions only */
184  HideAll /*< no annotations */
185  };
186 
190  {
191  InsideMapFrame = 0,
192  OutsideMapFrame, /*< draw annotations outside the map frame */
193  Disabled /*< disable annotation
194  * @deprecated in QGIS 2.7, use QgsComposerMapGrid::HideAll instead */
195  };
196 
200  {
201  Horizontal = 0, /*< draw annotations horizontally */
202  Vertical, /*< draw annotations vertically, ascending */
203  VerticalDescending, /*< draw annotations vertically, descending */
204  BoundaryDirection /*< annotations follow the boundary direction */
205  };
206 
210  {
211  Decimal = 0, /*< decimal degrees, use - for S/W coordinates */
212  DegreeMinute, /*< degree/minutes, use NSEW suffix */
213  DegreeMinuteSecond, /*< degree/minutes/seconds, use NSEW suffix */
214  DecimalWithSuffix, /*< decimal degrees, use NSEW suffix */
215  DegreeMinuteNoSuffix, /*< degree/minutes, use - for S/W coordinates */
216  DegreeMinutePadded, /*< degree/minutes, with minutes using leading zeros were required */
217  DegreeMinuteSecondNoSuffix, /*< degree/minutes/seconds, use - for S/W coordinates */
218  DegreeMinuteSecondPadded /*< degree/minutes/seconds, with minutes using leading zeros were required */
219  };
220 
224  {
226  Right, /*< right border */
227  Bottom, /*< bottom border */
228  Top /*< top border */
229  };
230 
234  {
235  NoFrame = 0, /*< disable grid frame */
236  Zebra, /*< black/white pattern */
237  InteriorTicks, /*< tick markers drawn inside map frame */
238  ExteriorTicks, /*< tick markers drawn outside map frame */
239  InteriorExteriorTicks, /*< tick markers drawn both inside and outside the map frame */
240  LineBorder /*< simple solid line frame */
241  };
242 
246  {
247  FrameLeft = 0x01, /*< left side of map */
248  FrameRight = 0x02, /*< right side of map */
249  FrameTop = 0x04, /*< top side of map */
250  FrameBottom = 0x08 /*< bottom side of map */
251  };
252  Q_DECLARE_FLAGS( FrameSideFlags, FrameSideFlag )
253 
254 
257  {
258  Longitude = 0, /*< coordinate is a longitude value */
259  Latitude /*< coordinate is a latitude value */
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