QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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  const 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  const 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() != QLatin1String( "Grid" ) )
110  {
111  element = element.firstChildElement( QStringLiteral( "Grid" ) );
112  }
113 
114  if ( element.nodeName() != QLatin1String( "Grid" ) )
115  {
116  return false;
117  }
118 
119  const double res = element.attribute( QStringLiteral( "resolution" ), QStringLiteral( "10" ) ).toDouble();
120  const QgsUnitTypes::LayoutUnit resUnit = QgsUnitTypes::decodeLayoutUnit( element.attribute( QStringLiteral( "resUnits" ) ) );
121  mGridResolution = QgsLayoutMeasurement( res, resUnit );
122 
123  const double offsetX = element.attribute( QStringLiteral( "offsetX" ) ).toDouble();
124  const double offsetY = element.attribute( QStringLiteral( "offsetY" ) ).toDouble();
125  const 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 
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
QgsLayout * layout() override
Returns the layout the object belongs to.
QgsLayoutGridSettings(QgsLayout *layout)
Constructor for QgsLayoutGridSettings.
QgsLayoutPoint offset() const
Returns the offset of the page/snap grid.
void setOffset(const QgsLayoutPoint &offset)
Sets the offset of the page/snap grid.
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.
void loadFromSettings()
Loads grid settings from the application layout settings.
void setResolution(QgsLayoutMeasurement resolution)
Sets the page/snap grid resolution.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
double length() const
Returns the length of the measurement.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the measurement.
void redraw()
Triggers a redraw for all pages.
This class provides a method of storing points, consisting of an x and y coordinate,...
double x() const
Returns x coordinate of point.
QgsUnitTypes::LayoutUnit units() const
Returns the units for the point.
double y() const
Returns y coordinate of point.
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:51
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
Definition: qgslayout.cpp:459
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
Definition: qgslayout.cpp:686
The class is used as a container of context for various read/write operations on other objects.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:182
@ LayoutMillimeters
Millimeters.
Definition: qgsunittypes.h:183
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE QgsUnitTypes::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.