QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
18
19#include "qgslayout.h"
21#include "qgslayoutundostack.h"
22#include "qgsreadwritecontext.h"
23#include "qgssettings.h"
24#include "qgsunittypes.h"
25
27 : mGridResolution( QgsLayoutMeasurement( 10 ) )
28 , mLayout( layout )
29{
30 mGridPen = QPen( QColor( 190, 190, 190, 100 ), 0 );
31 mGridPen.setCosmetic( true );
33}
34
36{
37 return mLayout;
38}
39
41{
42 mLayout->undoStack()->beginCommand( this, QObject::tr( "Change Grid Resolution" ), UndoGridResolution );
43 mGridResolution = resolution;
44 mLayout->undoStack()->endCommand();
45}
46
48{
49 mLayout->undoStack()->beginCommand( this, QObject::tr( "Change Grid Offset" ), UndoGridOffset );
50 mGridOffset = offset;
51 mLayout->undoStack()->endCommand();
52}
53
55{
56 //read grid style, grid color and pen width from settings
57 const QgsSettings s;
58
59 QString gridStyleString;
60 gridStyleString = s.value( QStringLiteral( "LayoutDesigner/gridStyle" ), "Dots", QgsSettings::Gui ).toString();
61
62 int gridRed, gridGreen, gridBlue, gridAlpha;
63 gridRed = s.value( QStringLiteral( "LayoutDesigner/gridRed" ), 190, QgsSettings::Gui ).toInt();
64 gridGreen = s.value( QStringLiteral( "LayoutDesigner/gridGreen" ), 190, QgsSettings::Gui ).toInt();
65 gridBlue = s.value( QStringLiteral( "LayoutDesigner/gridBlue" ), 190, QgsSettings::Gui ).toInt();
66 gridAlpha = s.value( QStringLiteral( "LayoutDesigner/gridAlpha" ), 100, QgsSettings::Gui ).toInt();
67 const QColor gridColor = QColor( gridRed, gridGreen, gridBlue, gridAlpha );
68
69 mGridPen.setColor( gridColor );
70 mGridPen.setWidthF( 0 );
71 mGridPen.setCosmetic( true );
72
73 if ( gridStyleString == QLatin1String( "Dots" ) )
74 {
75 mGridStyle = StyleDots;
76 }
77 else if ( gridStyleString == QLatin1String( "Crosses" ) )
78 {
79 mGridStyle = StyleCrosses;
80 }
81 else
82 {
83 mGridStyle = StyleLines;
84 }
85
86 mGridResolution = QgsLayoutMeasurement( s.value( QStringLiteral( "LayoutDesigner/defaultSnapGridResolution" ), 10.0, QgsSettings::Gui ).toDouble(), Qgis::LayoutUnit::Millimeters );
87// mSnapToleranceSpinBox->setValue( mSettings->value( QStringLiteral( "LayoutDesigner/defaultSnapTolerancePixels" ), 5, QgsSettings::Gui ).toInt() );
88 mGridOffset = QgsLayoutPoint( s.value( QStringLiteral( "LayoutDesigner/defaultSnapGridOffsetX" ), 0, QgsSettings::Gui ).toDouble(),
89 s.value( QStringLiteral( "LayoutDesigner/defaultSnapGridOffsetY" ), 0, QgsSettings::Gui ).toDouble(), Qgis::LayoutUnit::Millimeters );
90}
91
92bool QgsLayoutGridSettings::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext & ) const
93{
94 QDomElement element = document.createElement( QStringLiteral( "Grid" ) );
95
96 element.setAttribute( QStringLiteral( "resolution" ), mGridResolution.length() );
97 element.setAttribute( QStringLiteral( "resUnits" ), QgsUnitTypes::encodeUnit( mGridResolution.units() ) );
98
99 element.setAttribute( QStringLiteral( "offsetX" ), mGridOffset.x() );
100 element.setAttribute( QStringLiteral( "offsetY" ), mGridOffset.y() );
101 element.setAttribute( QStringLiteral( "offsetUnits" ), QgsUnitTypes::encodeUnit( mGridOffset.units() ) );
102
103 parentElement.appendChild( element );
104 return true;
105}
106
107bool QgsLayoutGridSettings::readXml( const QDomElement &e, const QDomDocument &, const QgsReadWriteContext & )
108{
109 QDomElement element = e;
110 if ( element.nodeName() != QLatin1String( "Grid" ) )
111 {
112 element = element.firstChildElement( QStringLiteral( "Grid" ) );
113 }
114
115 if ( element.nodeName() != QLatin1String( "Grid" ) )
116 {
117 return false;
118 }
119
120 const double res = element.attribute( QStringLiteral( "resolution" ), QStringLiteral( "10" ) ).toDouble();
121 const Qgis::LayoutUnit resUnit = QgsUnitTypes::decodeLayoutUnit( element.attribute( QStringLiteral( "resUnits" ) ) );
122 mGridResolution = QgsLayoutMeasurement( res, resUnit );
123
124 const double offsetX = element.attribute( QStringLiteral( "offsetX" ) ).toDouble();
125 const double offsetY = element.attribute( QStringLiteral( "offsetY" ) ).toDouble();
126 const Qgis::LayoutUnit offsetUnit = QgsUnitTypes::decodeLayoutUnit( element.attribute( QStringLiteral( "offsetUnits" ) ) );
127 mGridOffset = QgsLayoutPoint( offsetX, offsetY, offsetUnit );
128
129 mLayout->pageCollection()->redraw();
130
131 return true;
132}
133
LayoutUnit
Layout measurement units.
Definition qgis.h:5203
@ Millimeters
Millimeters.
Definition qgis.h:5204
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.
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
Provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
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:50
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
A container for the context for various read/write operations on objects.
Stores settings for use within QGIS.
Definition qgssettings.h:65
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static Q_INVOKABLE Qgis::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.