QGIS API Documentation 4.1.0-Master (376402f9aeb)
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"
24#include "qgssettingstree.h"
25#include "qgsunittypes.h"
26
27#include <QString>
28
29using namespace Qt::StringLiterals;
30
32 : mGridResolution( QgsLayoutMeasurement( 10 ) )
33 , mLayout( layout )
34{
35 mGridPen = QPen( QColor( 190, 190, 190, 100 ), 0 );
36 mGridPen.setCosmetic( true );
38}
39
41{
42 return mLayout;
43}
44
46{
47 mLayout->undoStack()->beginCommand( this, QObject::tr( "Change Grid Resolution" ), UndoGridResolution );
48 mGridResolution = resolution;
49 mLayout->undoStack()->endCommand();
50}
51
53{
54 mLayout->undoStack()->beginCommand( this, QObject::tr( "Change Grid Offset" ), UndoGridOffset );
55 mGridOffset = offset;
56 mLayout->undoStack()->endCommand();
57}
58
60 = new QgsSettingsEntryString( u"grid-style"_s, QgsSettingsTree::sTreeLayout, u"Dots"_s, u"Default rendering style for the layout designer grid. Accepted values are \"Solid\", \"Dots\" or \"Crosses\"."_s );
62 = new QgsSettingsEntryColor( u"grid-color"_s, QgsSettingsTree::sTreeLayout, QColor( 190, 190, 190, 100 ), u"Default color used to draw the layout designer grid."_s );
64 = new QgsSettingsEntryDouble( u"resolution"_s, QgsSettingsTree::sTreeLayoutGrid, 10.0, u"Default grid resolution (in millimeters) for newly created layouts."_s );
66 = new QgsSettingsEntryDouble( u"offset-x"_s, QgsSettingsTree::sTreeLayoutGrid, 0, u"Default grid horizontal offset (in millimeters) for newly created layouts."_s );
68 = new QgsSettingsEntryDouble( u"offset-y"_s, QgsSettingsTree::sTreeLayoutGrid, 0, u"Default grid vertical offset (in millimeters) for newly created layouts."_s );
69
71{
72 //read grid style, grid color and pen width from settings
73 const QString gridStyleString = settingsGridStyle->value();
74 const QColor gridColor = settingsGridColor->value();
75
76 mGridPen.setColor( gridColor );
77 mGridPen.setWidthF( 0 );
78 mGridPen.setCosmetic( true );
79
80 if ( gridStyleString == "Dots"_L1 )
81 {
82 mGridStyle = StyleDots;
83 }
84 else if ( gridStyleString == "Crosses"_L1 )
85 {
86 mGridStyle = StyleCrosses;
87 }
88 else
89 {
90 mGridStyle = StyleLines;
91 }
92
95}
96
97bool QgsLayoutGridSettings::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext & ) const
98{
99 QDomElement element = document.createElement( u"Grid"_s );
100
101 element.setAttribute( u"resolution"_s, mGridResolution.length() );
102 element.setAttribute( u"resUnits"_s, QgsUnitTypes::encodeUnit( mGridResolution.units() ) );
103
104 element.setAttribute( u"offsetX"_s, mGridOffset.x() );
105 element.setAttribute( u"offsetY"_s, mGridOffset.y() );
106 element.setAttribute( u"offsetUnits"_s, QgsUnitTypes::encodeUnit( mGridOffset.units() ) );
107
108 parentElement.appendChild( element );
109 return true;
110}
111
112bool QgsLayoutGridSettings::readXml( const QDomElement &e, const QDomDocument &, const QgsReadWriteContext & )
113{
114 QDomElement element = e;
115 if ( element.nodeName() != "Grid"_L1 )
116 {
117 element = element.firstChildElement( u"Grid"_s );
118 }
119
120 if ( element.nodeName() != "Grid"_L1 )
121 {
122 return false;
123 }
124
125 const double res = element.attribute( u"resolution"_s, u"10"_s ).toDouble();
126 const Qgis::LayoutUnit resUnit = QgsUnitTypes::decodeLayoutUnit( element.attribute( u"resUnits"_s ) );
127 mGridResolution = QgsLayoutMeasurement( res, resUnit );
128
129 const double offsetX = element.attribute( u"offsetX"_s ).toDouble();
130 const double offsetY = element.attribute( u"offsetY"_s ).toDouble();
131 const Qgis::LayoutUnit offsetUnit = QgsUnitTypes::decodeLayoutUnit( element.attribute( u"offsetUnits"_s ) );
132 mGridOffset = QgsLayoutPoint( offsetX, offsetY, offsetUnit );
133
134 mLayout->pageCollection()->redraw();
135
136 return true;
137}
LayoutUnit
Layout measurement units.
Definition qgis.h:5516
@ Millimeters
Millimeters.
Definition qgis.h:5517
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
static const QgsSettingsEntryDouble * settingsGridOffsetX
QgsLayout * layout() override
Returns the layout the object belongs to.
static const QgsSettingsEntryColor * settingsGridColor
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.
static const QgsSettingsEntryDouble * settingsGridResolution
static const QgsSettingsEntryDouble * settingsGridOffsetY
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.
static const QgsSettingsEntryString * settingsGridStyle
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:51
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.
A color settings entry.
A double settings entry.
A string settings entry.
static QgsSettingsTreeNode * sTreeLayout
static QgsSettingsTreeNode * sTreeLayoutGrid
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.