QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
qgslayoutundostack.h
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsLayoutGridSettings::QgsLayoutGridSettings
QgsLayoutGridSettings(QgsLayout *layout)
Constructor for QgsLayoutGridSettings.
Definition: qgslayoutgridsettings.cpp:25
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
QgsLayoutMeasurement::length
double length() const
Returns the length of the measurement.
Definition: qgslayoutmeasurement.h:48
qgsreadwritecontext.h
QgsLayoutGridSettings::StyleLines
@ StyleLines
Solid lines.
Definition: qgslayoutgridsettings.h:43
QgsUnitTypes::LayoutUnit
LayoutUnit
Layout measurement units.
Definition: qgsunittypes.h:180
QgsLayoutPoint::y
double y() const
Returns y coordinate of point.
Definition: qgslayoutpoint.h:86
QgsLayoutGridSettings::setResolution
void setResolution(QgsLayoutMeasurement resolution)
Sets the page/snap grid resolution.
Definition: qgslayoutgridsettings.cpp:39
QgsSettings
Definition: qgssettings.h:61
QgsLayout::pageCollection
QgsLayoutPageCollection * pageCollection()
Returns a pointer to the layout's page collection, which stores and manages page items in the layout.
Definition: qgslayout.cpp:458
QgsLayoutGridSettings::setOffset
void setOffset(const QgsLayoutPoint &offset)
Sets the offset of the page/snap grid.
Definition: qgslayoutgridsettings.cpp:46
QgsUnitTypes::encodeUnit
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
Definition: qgsunittypes.cpp:122
QgsLayoutGridSettings::readXml
bool readXml(const QDomElement &gridElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the grid's state from a DOM element.
Definition: qgslayoutgridsettings.cpp:106
QgsLayoutGridSettings::writeXml
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the grid's state in a DOM element.
Definition: qgslayoutgridsettings.cpp:91
QgsLayoutMeasurement::units
QgsUnitTypes::LayoutUnit units() const
Returns the units for the measurement.
Definition: qgslayoutmeasurement.h:60
QgsLayoutGridSettings::StyleCrosses
@ StyleCrosses
Crosses.
Definition: qgslayoutgridsettings.h:45
qgslayoutgridsettings.h
QgsLayoutGridSettings::offset
QgsLayoutPoint offset() const
Returns the offset of the page/snap grid.
Definition: qgslayoutgridsettings.h:82
qgslayout.h
QgsLayoutGridSettings::loadFromSettings
void loadFromSettings()
Loads grid settings from the application layout settings.
Definition: qgslayoutgridsettings.cpp:53
QgsLayoutUndoStack::endCommand
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
Definition: qgslayoutundostack.cpp:53
QgsLayoutPageCollection::redraw
void redraw()
Triggers a redraw for all pages.
Definition: qgslayoutpagecollection.cpp:436
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
qgssettings.h
QgsLayoutUndoStack::beginCommand
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
Definition: qgslayoutundostack.cpp:42
qgslayoutpagecollection.h
QgsUnitTypes::LayoutMillimeters
@ LayoutMillimeters
Millimeters.
Definition: qgsunittypes.h:182
QgsLayoutPoint
This class provides a method of storing points, consisting of an x and y coordinate,...
Definition: qgslayoutpoint.h:39
QgsUnitTypes::decodeLayoutUnit
static Q_INVOKABLE QgsUnitTypes::LayoutUnit decodeLayoutUnit(const QString &string, bool *ok=nullptr)
Decodes a layout unit from a string.
Definition: qgsunittypes.cpp:2995
QgsLayoutGridSettings::StyleDots
@ StyleDots
Dots.
Definition: qgslayoutgridsettings.h:44
QgsLayoutPoint::x
double x() const
Returns x coordinate of point.
Definition: qgslayoutpoint.h:72
qgsproject.h
QgsLayoutMeasurement
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
Definition: qgslayoutmeasurement.h:33
QgsSettings::Gui
@ Gui
Definition: qgssettings.h:71
QgsLayoutPoint::units
QgsUnitTypes::LayoutUnit units() const
Returns the units for the point.
Definition: qgslayoutpoint.h:99
QgsLayoutGridSettings::resolution
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
Definition: qgslayoutgridsettings.h:68
QgsLayoutGridSettings::layout
QgsLayout * layout() override
Returns the layout the object belongs to.
Definition: qgslayoutgridsettings.cpp:34
QgsLayout::undoStack
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:685