QGIS API Documentation  3.2.0-Bonn (bc43194)
qgslayoutgridsettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutgridsettings.cpp
3  -------------------------
4  begin : July 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgslayoutgridsettings.h"
18 #include "qgsreadwritecontext.h"
19 #include "qgslayout.h"
20 #include "qgsproject.h"
21 #include "qgslayoutundostack.h"
23 #include "qgssettings.h"
24 
26  : mGridResolution( QgsLayoutMeasurement( 10 ) )
27  , mLayout( layout )
28 {
29  mGridPen = QPen( QColor( 190, 190, 190, 100 ), 0 );
30  mGridPen.setCosmetic( true );
32 }
33 
35 {
36  return mLayout;
37 }
38 
40 {
41  mLayout->undoStack()->beginCommand( this, QObject::tr( "Change Grid Resolution" ), UndoGridResolution );
42  mGridResolution = resolution;
43  mLayout->undoStack()->endCommand();
44 }
45 
47 {
48  mLayout->undoStack()->beginCommand( this, QObject::tr( "Change Grid Offset" ), UndoGridOffset );
49  mGridOffset = offset;
50  mLayout->undoStack()->endCommand();
51 }
52 
54 {
55  //read grid style, grid color and pen width from settings
56  QgsSettings s;
57 
58  QString gridStyleString;
59  gridStyleString = s.value( QStringLiteral( "LayoutDesigner/gridStyle" ), "Dots", QgsSettings::Gui ).toString();
60 
61  int gridRed, gridGreen, gridBlue, gridAlpha;
62  gridRed = s.value( QStringLiteral( "LayoutDesigner/gridRed" ), 190, QgsSettings::Gui ).toInt();
63  gridGreen = s.value( QStringLiteral( "LayoutDesigner/gridGreen" ), 190, QgsSettings::Gui ).toInt();
64  gridBlue = s.value( QStringLiteral( "LayoutDesigner/gridBlue" ), 190, QgsSettings::Gui ).toInt();
65  gridAlpha = s.value( QStringLiteral( "LayoutDesigner/gridAlpha" ), 100, QgsSettings::Gui ).toInt();
66  QColor gridColor = QColor( gridRed, gridGreen, gridBlue, gridAlpha );
67 
68  mGridPen.setColor( gridColor );
69  mGridPen.setWidthF( 0 );
70  mGridPen.setCosmetic( true );
71 
72  if ( gridStyleString == QLatin1String( "Dots" ) )
73  {
74  mGridStyle = StyleDots;
75  }
76  else if ( gridStyleString == QLatin1String( "Crosses" ) )
77  {
78  mGridStyle = StyleCrosses;
79  }
80  else
81  {
82  mGridStyle = StyleLines;
83  }
84 
85  mGridResolution = QgsLayoutMeasurement( s.value( QStringLiteral( "LayoutDesigner/defaultSnapGridResolution" ), 10.0, QgsSettings::Gui ).toDouble(), QgsUnitTypes::LayoutMillimeters );
86 // mSnapToleranceSpinBox->setValue( mSettings->value( QStringLiteral( "LayoutDesigner/defaultSnapTolerancePixels" ), 5, QgsSettings::Gui ).toInt() );
87  mGridOffset = QgsLayoutPoint( s.value( QStringLiteral( "LayoutDesigner/defaultSnapGridOffsetX" ), 0, QgsSettings::Gui ).toDouble(),
88  s.value( QStringLiteral( "LayoutDesigner/defaultSnapGridOffsetY" ), 0, QgsSettings::Gui ).toDouble(), QgsUnitTypes::LayoutMillimeters );
89 }
90 
91 bool QgsLayoutGridSettings::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext & ) const
92 {
93  QDomElement element = document.createElement( QStringLiteral( "Grid" ) );
94 
95  element.setAttribute( QStringLiteral( "resolution" ), mGridResolution.length() );
96  element.setAttribute( QStringLiteral( "resUnits" ), QgsUnitTypes::encodeUnit( mGridResolution.units() ) );
97 
98  element.setAttribute( QStringLiteral( "offsetX" ), mGridOffset.x() );
99  element.setAttribute( QStringLiteral( "offsetY" ), mGridOffset.y() );
100  element.setAttribute( QStringLiteral( "offsetUnits" ), QgsUnitTypes::encodeUnit( mGridOffset.units() ) );
101 
102  parentElement.appendChild( element );
103  return true;
104 }
105 
106 bool QgsLayoutGridSettings::readXml( const QDomElement &e, const QDomDocument &, const QgsReadWriteContext & )
107 {
108  QDomElement element = e;
109  if ( element.nodeName() != QStringLiteral( "Grid" ) )
110  {
111  element = element.firstChildElement( QStringLiteral( "Grid" ) );
112  }
113 
114  if ( element.nodeName() != QStringLiteral( "Grid" ) )
115  {
116  return false;
117  }
118 
119  double res = element.attribute( QStringLiteral( "resolution" ), QStringLiteral( "10" ) ).toDouble();
120  QgsUnitTypes::LayoutUnit resUnit = QgsUnitTypes::decodeLayoutUnit( element.attribute( QStringLiteral( "resUnits" ) ) );
121  mGridResolution = QgsLayoutMeasurement( res, resUnit );
122 
123  double offsetX = element.attribute( QStringLiteral( "offsetX" ) ).toDouble();
124  double offsetY = element.attribute( QStringLiteral( "offsetY" ) ).toDouble();
125  QgsUnitTypes::LayoutUnit offsetUnit = QgsUnitTypes::decodeLayoutUnit( element.attribute( QStringLiteral( "offsetUnits" ) ) );
126  mGridOffset = QgsLayoutPoint( offsetX, offsetY, offsetUnit );
127 
128  mLayout->pageCollection()->redraw();
129 
130  return true;
131 }
132 
The class is used as a container of context for various read/write operations on other objects...
QgsUnitTypes::LayoutUnit units() const
Returns the units for the measurement.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the grid's state in a DOM element.
bool readXml(const QDomElement &gridElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the grid's state from a DOM element.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
QgsLayoutGridSettings(QgsLayout *layout)
Constructor for QgsLayoutGridSettings.
void loadFromSettings()
Loads grid settings from the application layout settings.
void setResolution(const QgsLayoutMeasurement &resolution)
Sets the page/snap grid resolution.
QgsLayout * layout() override
Returns the layout the object belongs to.
This class provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the point.
const QgsLayout * layout() const
Returns the layout the object is attached to.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
static Q_INVOKABLE QgsUnitTypes::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
double y() const
Returns y coordinate of point.
QPointer< QgsLayout > mLayout
double x() const
Returns x coordinate of point.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
QgsLayoutPoint offset() const
Returns the offset of the page/snap grid.
double length() const
Returns the length of the measurement.
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:114
void setOffset(QgsLayoutPoint offset)
Sets the offset of the page/snap grid.