|
| 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 "qgscoordinatereferencesystem.h" |
| 22 | +#include "qgscomposermap.h" |
| 23 | +#include <QString> |
| 24 | + |
| 25 | +class QgsCoordinateTransform; |
| 26 | +class QgsLineSymbolV2; |
| 27 | +class QDomDocument; |
| 28 | +class QDomElement; |
| 29 | +class QPainter; |
| 30 | + |
| 31 | +class CORE_EXPORT QgsComposerMapGrid |
| 32 | +{ |
| 33 | + public: |
| 34 | + |
| 35 | + enum GridUnit |
| 36 | + { |
| 37 | + MapUnit, |
| 38 | + MM, |
| 39 | + CM |
| 40 | + }; |
| 41 | + |
| 42 | + QgsComposerMapGrid( const QString& name, QgsComposerMap* map ); |
| 43 | + ~QgsComposerMapGrid(); |
| 44 | + |
| 45 | + /** \brief Reimplementation of QCanvasItem::paint*/ |
| 46 | + void drawGrid( QPainter* painter ) const; |
| 47 | + |
| 48 | + /**Draws grid if CRS is different to map CRS*/ |
| 49 | + void drawGridCRSTransform( QPainter* painter ) const; |
| 50 | + |
| 51 | + /** stores state in Dom element |
| 52 | + * @param elem is Dom element corresponding to 'ComposerMap' tag |
| 53 | + * @param doc Dom document |
| 54 | + */ |
| 55 | + bool writeXML( QDomElement& elem, QDomDocument & doc ) const; |
| 56 | + |
| 57 | + /** sets state from Dom document |
| 58 | + * @param itemElem is Dom node corresponding to item tag |
| 59 | + * @param doc is Dom document |
| 60 | + */ |
| 61 | + bool readXML( const QDomElement& itemElem, const QDomDocument& doc ); |
| 62 | + |
| 63 | + //setters and getters |
| 64 | + void setComposerMap( QgsComposerMap* map ); |
| 65 | + const QgsComposerMap* composerMap() const { return mComposerMap; } |
| 66 | + |
| 67 | + void setName( const QString& name ) { mName = name; } |
| 68 | + QString name() const { return mName; } |
| 69 | + |
| 70 | + QString id() const { return mUuid; } |
| 71 | + |
| 72 | + /**Enables a coordinate grid that is shown on top of this composermap. |
| 73 | + @note this function was added in version 1.4*/ |
| 74 | + void setGridEnabled( bool enabled ) {mGridEnabled = enabled;} |
| 75 | + bool gridEnabled() const { return mGridEnabled; } |
| 76 | + |
| 77 | + /**Sets coordinate grid style to solid or cross |
| 78 | + @note this function was added in version 1.4*/ |
| 79 | + void setGridStyle( QgsComposerMap::GridStyle style ) {mGridStyle = style;} |
| 80 | + QgsComposerMap::GridStyle gridStyle() const { return mGridStyle; } |
| 81 | + |
| 82 | + /**Sets coordinate interval in x-direction for composergrid. |
| 83 | + @note this function was added in version 1.4*/ |
| 84 | + void setGridIntervalX( double interval ) { mGridIntervalX = interval;} |
| 85 | + double gridIntervalX() const { return mGridIntervalX; } |
| 86 | + |
| 87 | + /**Sets coordinate interval in y-direction for composergrid. |
| 88 | + @note this function was added in version 1.4*/ |
| 89 | + void setGridIntervalY( double interval ) { mGridIntervalY = interval;} |
| 90 | + double gridIntervalY() const { return mGridIntervalY; } |
| 91 | + |
| 92 | + /**Sets x-coordinate offset for composer grid |
| 93 | + @note this function was added in version 1.4*/ |
| 94 | + void setGridOffsetX( double offset ) { mGridOffsetX = offset; } |
| 95 | + double gridOffsetX() const { return mGridOffsetX; } |
| 96 | + |
| 97 | + /**Sets y-coordinate offset for composer grid |
| 98 | + @note this function was added in version 1.4*/ |
| 99 | + void setGridOffsetY( double offset ) { mGridOffsetY = offset; } |
| 100 | + double gridOffsetY() const { return mGridOffsetY; } |
| 101 | + |
| 102 | + /**Sets the pen to draw composer grid |
| 103 | + @note this function was added in version 1.4*/ |
| 104 | + void setGridPen( const QPen& p ); |
| 105 | + QPen gridPen() const; |
| 106 | + |
| 107 | + /**Sets with of grid pen |
| 108 | + @note this function was added in version 1.4*/ |
| 109 | + void setGridPenWidth( double w ); |
| 110 | + |
| 111 | + /**Sets the color of the grid pen |
| 112 | + @note this function was added in version 1.4*/ |
| 113 | + void setGridPenColor( const QColor& c ); |
| 114 | + |
| 115 | + /**Sets font for grid annotations |
| 116 | + @note this function was added in version 1.4*/ |
| 117 | + void setGridAnnotationFont( const QFont& f ) { mGridAnnotationFont = f; } |
| 118 | + QFont gridAnnotationFont() const { return mGridAnnotationFont; } |
| 119 | + |
| 120 | + void setGridAnnotationFontColor( const QColor& c ) { mGridAnnotationFontColor = c; } |
| 121 | + QColor gridAnnotationFontColor() const { return mGridAnnotationFontColor; } |
| 122 | + |
| 123 | + /**Sets coordinate precision for grid annotations |
| 124 | + @note this function was added in version 1.4*/ |
| 125 | + void setGridAnnotationPrecision( int p ) {mGridAnnotationPrecision = p;} |
| 126 | + int gridAnnotationPrecision() const {return mGridAnnotationPrecision;} |
| 127 | + |
| 128 | + /**Sets flag if grid annotation should be shown |
| 129 | + @note this function was added in version 1.4*/ |
| 130 | + void setShowGridAnnotation( bool show ) {mShowGridAnnotation = show;} |
| 131 | + bool showGridAnnotation() const {return mShowGridAnnotation;} |
| 132 | + |
| 133 | + void setGridAnnotationPosition( QgsComposerMap::GridAnnotationPosition p, QgsComposerMap::Border border ); |
| 134 | + QgsComposerMap::GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const; |
| 135 | + |
| 136 | + /**Sets distance between map frame and annotations |
| 137 | + @note this function was added in version 1.4*/ |
| 138 | + void setAnnotationFrameDistance( double d ) {mAnnotationFrameDistance = d;} |
| 139 | + double annotationFrameDistance() const {return mAnnotationFrameDistance;} |
| 140 | + |
| 141 | + void setGridAnnotationDirection( QgsComposerMap::GridAnnotationDirection d, QgsComposerMap::Border border ); |
| 142 | + QgsComposerMap::GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const; |
| 143 | + |
| 144 | + /**Sets grid annotation direction. Can be horizontal, vertical, direction of axis and horizontal and vertical |
| 145 | + @note provides compatibility with 1.8 version*/ |
| 146 | + void setGridAnnotationDirection( QgsComposerMap::GridAnnotationDirection d ); |
| 147 | + QgsComposerMap::GridAnnotationDirection gridAnnotationDirection() const; |
| 148 | + |
| 149 | + void setGridAnnotationFormat( QgsComposerMap::GridAnnotationFormat f ) { mGridAnnotationFormat = f; } |
| 150 | + QgsComposerMap::GridAnnotationFormat gridAnnotationFormat() const { return mGridAnnotationFormat; } |
| 151 | + |
| 152 | + /**Set grid frame style (NoGridFrame or Zebra) |
| 153 | + @note: this function was added in version 1.9*/ |
| 154 | + void setGridFrameStyle( QgsComposerMap::GridFrameStyle style ) { mGridFrameStyle = style; } |
| 155 | + QgsComposerMap::GridFrameStyle gridFrameStyle() const { return mGridFrameStyle; } |
| 156 | + |
| 157 | + /**Set grid frame width |
| 158 | + @note: this function was added in version 1.9*/ |
| 159 | + void setGridFrameWidth( double w ) { mGridFrameWidth = w; } |
| 160 | + double gridFrameWidth() const { return mGridFrameWidth; } |
| 161 | + |
| 162 | + /**Sets length of the cros segments (if grid style is cross) |
| 163 | + @note this function was added in version 1.4*/ |
| 164 | + void setCrossLength( double l ) {mCrossLength = l;} |
| 165 | + double crossLength() const {return mCrossLength;} |
| 166 | + |
| 167 | + void setGridLineSymbol( QgsLineSymbolV2* symbol ); |
| 168 | + const QgsLineSymbolV2* gridLineSymbol() const { return mGridLineSymbol; } |
| 169 | + QgsLineSymbolV2* gridLineSymbol() { return mGridLineSymbol; } |
| 170 | + |
| 171 | + void setCrs( const QgsCoordinateReferenceSystem& crs ) { mCRS = crs; } |
| 172 | + QgsCoordinateReferenceSystem crs() const { return mCRS; } |
| 173 | + |
| 174 | + void setGridUnit( GridUnit u ) { mGridUnit = u; } |
| 175 | + GridUnit gridUnit() const { return mGridUnit; } |
| 176 | + |
| 177 | + double maxExtension() const; |
| 178 | + |
| 179 | + private: |
| 180 | + QgsComposerMapGrid(); //forbidden |
| 181 | + |
| 182 | + QgsComposerMap* mComposerMap; |
| 183 | + QString mName; |
| 184 | + QString mUuid; |
| 185 | + |
| 186 | + /**True if coordinate grid has to be displayed*/ |
| 187 | + bool mGridEnabled; |
| 188 | + /**Solid or crosses*/ |
| 189 | + QgsComposerMap::GridStyle mGridStyle; |
| 190 | + /**Grid line interval in x-direction (map units)*/ |
| 191 | + double mGridIntervalX; |
| 192 | + /**Grid line interval in y-direction (map units)*/ |
| 193 | + double mGridIntervalY; |
| 194 | + /**Grid line offset in x-direction*/ |
| 195 | + double mGridOffsetX; |
| 196 | + /**Grid line offset in y-direction*/ |
| 197 | + double mGridOffsetY; |
| 198 | + /**Font for grid line annotation*/ |
| 199 | + QFont mGridAnnotationFont; |
| 200 | + /**Font color for grid coordinates*/ |
| 201 | + QColor mGridAnnotationFontColor; |
| 202 | + /**Digits after the dot*/ |
| 203 | + int mGridAnnotationPrecision; |
| 204 | + /**True if coordinate values should be drawn*/ |
| 205 | + bool mShowGridAnnotation; |
| 206 | + |
| 207 | + /**Annotation position for left map side (inside / outside / not shown)*/ |
| 208 | + QgsComposerMap::GridAnnotationPosition mLeftGridAnnotationPosition; |
| 209 | + /**Annotation position for right map side (inside / outside / not shown)*/ |
| 210 | + QgsComposerMap::GridAnnotationPosition mRightGridAnnotationPosition; |
| 211 | + /**Annotation position for top map side (inside / outside / not shown)*/ |
| 212 | + QgsComposerMap::GridAnnotationPosition mTopGridAnnotationPosition; |
| 213 | + /**Annotation position for bottom map side (inside / outside / not shown)*/ |
| 214 | + QgsComposerMap::GridAnnotationPosition mBottomGridAnnotationPosition; |
| 215 | + |
| 216 | + /**Distance between map frame and annotation*/ |
| 217 | + double mAnnotationFrameDistance; |
| 218 | + |
| 219 | + /**Annotation direction on left side ( horizontal or vertical )*/ |
| 220 | + QgsComposerMap::GridAnnotationDirection mLeftGridAnnotationDirection; |
| 221 | + /**Annotation direction on right side ( horizontal or vertical )*/ |
| 222 | + QgsComposerMap::GridAnnotationDirection mRightGridAnnotationDirection; |
| 223 | + /**Annotation direction on top side ( horizontal or vertical )*/ |
| 224 | + QgsComposerMap::GridAnnotationDirection mTopGridAnnotationDirection; |
| 225 | + /**Annotation direction on bottom side ( horizontal or vertical )*/ |
| 226 | + QgsComposerMap::GridAnnotationDirection mBottomGridAnnotationDirection; |
| 227 | + QgsComposerMap::GridAnnotationFormat mGridAnnotationFormat; |
| 228 | + QgsComposerMap::GridFrameStyle mGridFrameStyle; |
| 229 | + double mGridFrameWidth; |
| 230 | + double mCrossLength; |
| 231 | + |
| 232 | + QgsLineSymbolV2* mGridLineSymbol; |
| 233 | + |
| 234 | + QgsCoordinateReferenceSystem mCRS; |
| 235 | + |
| 236 | + GridUnit mGridUnit; |
| 237 | + |
| 238 | + /**Draws the map grid*/ |
| 239 | + void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines ) const; |
| 240 | + /**Draw coordinates for mGridAnnotationType Coordinate |
| 241 | + @param p drawing painter |
| 242 | + @param hLines horizontal coordinate lines in item coordinates |
| 243 | + @param vLines vertical coordinate lines in item coordinates*/ |
| 244 | + void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines ) const; |
| 245 | + void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString ) const; |
| 246 | + /**Draws a single annotation |
| 247 | + @param p drawing painter |
| 248 | + @param pos item coordinates where to draw |
| 249 | + @param rotation text rotation |
| 250 | + @param annotationText the text to draw*/ |
| 251 | + void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText ) const; |
| 252 | + QString gridAnnotationString( double value, QgsComposerMap::AnnotationCoordinate coord ) const; |
| 253 | + /**Returns the grid lines with associated coordinate value |
| 254 | + @return 0 in case of success*/ |
| 255 | + int xGridLines( QList< QPair< double, QLineF > >& lines ) const; |
| 256 | + /**Returns the grid lines for the y-coordinates. Not vertical in case of rotation |
| 257 | + @return 0 in case of success*/ |
| 258 | + int yGridLines( QList< QPair< double, QLineF > >& lines ) const; |
| 259 | + int xGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const; |
| 260 | + int yGridLinesCRSTransform( const QgsRectangle& bbox, const QgsCoordinateTransform& t, QList< QPair< double, QPolygonF > >& lines ) const; |
| 261 | + void drawGridLine( const QLineF& line, QPainter* p ) const; |
| 262 | + void drawGridLine( const QPolygonF& line, QPainter* p ) const; |
| 263 | + void sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries, |
| 264 | + QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const; |
| 265 | + void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, QgsComposerMap::Border border ) const; |
| 266 | + /**Returns the item border of a point (in item coordinates)*/ |
| 267 | + QgsComposerMap::Border borderForLineCoord( const QPointF& p ) const; |
| 268 | + /**Get parameters for drawing grid in CRS different to map CRS*/ |
| 269 | + int crsGridParams( QgsRectangle& crsRect, QgsCoordinateTransform& inverseTransform ) const; |
| 270 | + static QPolygonF trimLineToMap( const QPolygonF& line, const QgsRectangle& rect ); |
| 271 | +}; |
| 272 | + |
| 273 | +#endif // QGSCOMPOSERMAPGRID_H |
0 commit comments