608 changes: 608 additions & 0 deletions src/app/qgsdecorationgrid.cpp

Large diffs are not rendered by default.

227 changes: 227 additions & 0 deletions src/app/qgsdecorationgrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/***************************************************************************
qgsdecorationgrid.h
----------------------
begin : May 10, 2012
copyright : (C) 2012 by Etienne Tourigny
email : etourigny.dev at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSDECORATIONGRID_H
#define QGSDECORATIONGRID_H

class QPainter;

#include <QColor>
#include <QObject>
#include <QPen>
#include <QFont>

class QgsDecorationGrid: public QObject
{
Q_OBJECT
public:
//! Constructor
QgsDecorationGrid( QObject* parent = NULL );
//! Destructor
virtual ~ QgsDecorationGrid();

enum GridStyle
{
Solid = 0, //solid lines
Cross //only draw line crossings
};

enum GridAnnotationPosition
{
InsideMapFrame = 0,
OutsideMapFrame
};

enum GridAnnotationDirection
{
Horizontal = 0,
Vertical,
HorizontalAndVertical,
BoundaryDirection
};

/**Enables a coordinate grid that is shown on top of this composermap.
@note this function was added in version 1.4*/
void setGridEnabled( bool enabled ) {mGridEnabled = enabled;}
bool gridEnabled() const { return mGridEnabled; }

/**Sets coordinate grid style to solid or cross
@note this function was added in version 1.4*/
void setGridStyle( GridStyle style ) {mGridStyle = style;}
GridStyle gridStyle() const { return mGridStyle; }

/**Sets coordinate interval in x-direction for composergrid.
@note this function was added in version 1.4*/
void setGridIntervalX( double interval ) { mGridIntervalX = interval;}
double gridIntervalX() const { return mGridIntervalX; }

/**Sets coordinate interval in y-direction for composergrid.
@note this function was added in version 1.4*/
void setGridIntervalY( double interval ) { mGridIntervalY = interval;}
double gridIntervalY() const { return mGridIntervalY; }

/**Sets x-coordinate offset for composer grid
@note this function was added in version 1.4*/
void setGridOffsetX( double offset ) { mGridOffsetX = offset; }
double gridOffsetX() const { return mGridOffsetX; }

/**Sets y-coordinate offset for composer grid
@note this function was added in version 1.4*/
void setGridOffsetY( double offset ) { mGridOffsetY = offset; }
double gridOffsetY() const { return mGridOffsetY; }

/**Sets the pen to draw composer grid
@note this function was added in version 1.4*/
void setGridPen( const QPen& p ) { mGridPen = p; }
QPen gridPen() const { return mGridPen; }
/**Sets with of grid pen
@note this function was added in version 1.4*/
void setGridPenWidth( double w ) { mGridPen.setWidthF( w ); }
/**Sets the color of the grid pen
@note this function was added in version 1.4*/
void setGridPenColor( const QColor& c ) { mGridPen.setColor( c ); }

/**Sets font for grid annotations
@note this function was added in version 1.4*/
void setGridAnnotationFont( const QFont& f ) { mGridAnnotationFont = f; }
QFont gridAnnotationFont() const { return mGridAnnotationFont; }

/**Sets coordinate precision for grid annotations
@note this function was added in version 1.4*/
void setGridAnnotationPrecision( int p ) {mGridAnnotationPrecision = p;}
int gridAnnotationPrecision() const {return mGridAnnotationPrecision;}

/**Sets flag if grid annotation should be shown
@note this function was added in version 1.4*/
void setShowGridAnnotation( bool show ) {mShowGridAnnotation = show;}
bool showGridAnnotation() const {return mShowGridAnnotation;}

/**Sets position of grid annotations. Possibilities are inside or outside of the map frame
@note this function was added in version 1.4*/
void setGridAnnotationPosition( GridAnnotationPosition p ) {mGridAnnotationPosition = p;}
GridAnnotationPosition gridAnnotationPosition() const {return mGridAnnotationPosition;}

/**Sets distance between map frame and annotations
@note this function was added in version 1.4*/
void setAnnotationFrameDistance( double d ) {mAnnotationFrameDistance = d;}
double annotationFrameDistance() const {return mAnnotationFrameDistance;}

/**Sets grid annotation direction. Can be horizontal, vertical, direction of axis and horizontal and vertical
@note this function was added in version 1.4*/
void setGridAnnotationDirection( GridAnnotationDirection d ) {mGridAnnotationDirection = d;}
GridAnnotationDirection gridAnnotationDirection() const {return mGridAnnotationDirection;}

/**Sets length of the cros segments (if grid style is cross)
@note this function was added in version 1.4*/
void setCrossLength( double l ) {mCrossLength = l;}
double crossLength() {return mCrossLength;}

void update();

public slots:
//! set values on the gui when a project is read or the gui first loaded
void projectRead();
//! save values to the project
void saveToProject();

//! this does the meaty bit of the work
void renderGrid( QPainter * );
//! Show the dialog box
void run();

private:

/**Enum for different frame borders*/
enum Border
{
Left,
Right,
Bottom,
Top
};

/**True if coordinate grid has to be displayed*/
bool mGridEnabled;
/**Solid or crosses*/
GridStyle mGridStyle;
/**Grid line interval in x-direction (map units)*/
double mGridIntervalX;
/**Grid line interval in y-direction (map units)*/
double mGridIntervalY;
/**Grid line offset in x-direction*/
double mGridOffsetX;
/**Grid line offset in y-direction*/
double mGridOffsetY;
/**Grid line pen*/
QPen mGridPen;
/**Font for grid line annotation*/
QFont mGridAnnotationFont;
/**Digits after the dot*/
int mGridAnnotationPrecision;
/**True if coordinate values should be drawn*/
bool mShowGridAnnotation;
/**Annotation position inside or outside of map frame*/
GridAnnotationPosition mGridAnnotationPosition;
/**Distance between map frame and annotation*/
double mAnnotationFrameDistance;
/**Annotation can be horizontal / vertical or different for axes*/
GridAnnotationDirection mGridAnnotationDirection;
/**The length of the cross sides for mGridStyle Cross*/
double mCrossLength;

/**Draw coordinates for mGridAnnotationType Coordinate
@param p drawing painter
@param hLines horizontal coordinate lines in item coordinates
@param vLines vertical coordinate lines in item coordinates*/
void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines );
void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString );
/**Draws a single annotation
@param p drawing painter
@param pos item coordinates where to draw
@param rotation text rotation
@param annotationText the text to draw*/
void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText );
/**Returns the grid lines with associated coordinate value
@return 0 in case of success*/
int xGridLines( QList< QPair< double, QLineF > >& lines, QPainter* p ) const;
/**Returns the grid lines for the y-coordinates. Not vertical in case of rotation
@return 0 in case of success*/
int yGridLines( QList< QPair< double, QLineF > >& lines, QPainter* p ) const;
/**Returns the item border of a point (in item coordinates)*/
Border borderForLineCoord( const QPointF& point, QPainter* p ) const;

/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
to work around the Qt font bug)*/
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
/**Like the above, but with a rectangle for multiline text*/
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignement = Qt::AlignLeft, Qt::AlignmentFlag valignement = Qt::AlignTop ) const;
/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double textWidthMillimeters( const QFont& font, const QString& text ) const;
/**Returns the font height of a character in millimeters
@note this method was added in version 1.7*/
double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double fontAscentMillimeters( const QFont& font ) const;
/**Calculates font to from point size to pixel size*/
double pixelFontSize( double pointSize ) const;
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
QFont scaledFontPixelSize( const QFont& font ) const;

/* friend class QgsDecorationGridDialog; */
};

#endif
18 changes: 17 additions & 1 deletion src/ui/qgisapp.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<x>0</x>
<y>0</y>
<width>1052</width>
<height>28</height>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="mEditMenu">
Expand Down Expand Up @@ -106,6 +106,7 @@
<addaction name="mActionDecorationCopyright"/>
<addaction name="mActionDecorationNorthArrow"/>
<addaction name="mActionDecorationScaleBar"/>
<addaction name="mActionDecorationGrid"/>
</widget>
<addaction name="mActionPan"/>
<addaction name="mActionPanToSelected"/>
Expand Down Expand Up @@ -1681,6 +1682,21 @@
<string>Add WCS Layer...</string>
</property>
</action>
<action name="mActionDecorationGrid">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/transformed.png</normaloff>:/images/themes/default/transformed.png</iconset>
</property>
<property name="text">
<string>&amp;Grid</string>
</property>
<property name="toolTip">
<string>Grid</string>
</property>
<property name="whatsThis">
<string>Creates a scale bar that is displayed on the map canvas</string>
</property>
</action>
</widget>
<resources>
<include location="../../images/images.qrc"/>
Expand Down
322 changes: 322 additions & 0 deletions src/ui/qgsdecorationgriddialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QgsDecorationGridDialog</class>
<widget class="QDialog" name="QgsDecorationGridDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>511</width>
<height>353</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="1" column="0">
<widget class="QLabel" name="mGridTypeLabel">
<property name="accessibleName">
<string extracomment="Hello translotor"/>
</property>
<property name="text">
<string>Grid type</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QComboBox" name="mGridTypeComboBox"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="mIntervalXLabel">
<property name="text">
<string>Interval X</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QDoubleSpinBox" name="mIntervalXSpinBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="decimals">
<number>5</number>
</property>
<property name="maximum">
<double>9999999.000000000000000</double>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="mIntervalYLabel">
<property name="text">
<string>Interval Y</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QDoubleSpinBox" name="mIntervalYSpinBox">
<property name="decimals">
<number>5</number>
</property>
<property name="maximum">
<double>9999999.000000000000000</double>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="mOffsetXLabel">
<property name="text">
<string>Offset X</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QDoubleSpinBox" name="mOffsetXSpinBox">
<property name="decimals">
<number>5</number>
</property>
<property name="maximum">
<double>9999999.000000000000000</double>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="mOffsetYLabel">
<property name="text">
<string>Offset Y</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QDoubleSpinBox" name="mOffsetYSpinBox">
<property name="decimals">
<number>5</number>
</property>
<property name="maximum">
<double>9999999.000000000000000</double>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="mCrossWidthLabel">
<property name="text">
<string>Cross width</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="6" column="2">
<widget class="QDoubleSpinBox" name="mCrossWidthSpinBox">
<property name="decimals">
<number>5</number>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="mLineWidthLabel">
<property name="text">
<string>Line width</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="7" column="2">
<widget class="QDoubleSpinBox" name="mLineWidthSpinBox">
<property name="decimals">
<number>5</number>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QLabel" name="mLineColorLabel">
<property name="text">
<string>Line color</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="8" column="2">
<widget class="QgsColorButton" name="mLineColorButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="5" column="3">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="chkEnable">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>Enable grid</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="4" rowspan="9">
<widget class="QGroupBox" name="mDrawAnnotationCheckBox">
<property name="title">
<string>Draw annotation</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="mAnnotationPositionLabel">
<property name="text">
<string>Annotation position</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="mAnnotationPositionComboBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="mAnnotationDirectionLabel">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="text">
<string>Annotation direction</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="mAnnotationDirectionComboBox"/>
</item>
<item row="2" column="0" colspan="2">
<widget class="QPushButton" name="mAnnotationFontButton">
<property name="text">
<string>Font...</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="mDistanceToFrameLabel">
<property name="text">
<string>Distance to map frame</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QDoubleSpinBox" name="mDistanceToMapFrameSpinBox"/>
</item>
<item row="4" column="0">
<widget class="QLabel" name="mCoordinatePrecisionLabel">
<property name="text">
<string>Coordinate precision</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QSpinBox" name="mCoordinatePrecisionSpinBox"/>
</item>
</layout>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsColorButton</class>
<extends>QToolButton</extends>
<header>qgscolorbutton.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>