QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
qgsprojectviewsettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprojectviewsettings.cpp
3  -----------------------------
4  begin : October 2019
5  copyright : (C) 2019 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsprojectviewsettings.h"
17 #include "qgis.h"
18 #include <QDomElement>
19 
21  : QObject( parent )
22 {
23 
24 }
25 
27 {
28  mDefaultViewExtent = QgsReferencedRectangle();
29  if ( mUseProjectScales || !mMapScales.empty() )
30  {
31  mUseProjectScales = false;
32  mMapScales.clear();
33  emit mapScalesChanged();
34  }
35 }
36 
38 {
39  return mDefaultViewExtent;
40 }
41 
43 {
44  mDefaultViewExtent = extent;
45 }
46 
47 void QgsProjectViewSettings::setMapScales( const QVector<double> &scales )
48 {
49  // sort scales in descending order
50  QVector< double > sorted = scales;
51  std::sort( sorted.begin(), sorted.end(), std::greater<double>() );
52 
53  if ( sorted == mapScales() )
54  return;
55 
56  mMapScales = sorted;
57 
58  emit mapScalesChanged();
59 }
60 
61 QVector<double> QgsProjectViewSettings::mapScales() const
62 {
63  return mMapScales;
64 }
65 
67 {
68  if ( enabled == useProjectScales() )
69  return;
70 
71  mUseProjectScales = enabled;
72  emit mapScalesChanged();
73 }
74 
76 {
77  return mUseProjectScales;
78 }
79 
80 bool QgsProjectViewSettings::readXml( const QDomElement &element, const QgsReadWriteContext & )
81 {
82  bool useProjectScale = element.attribute( QStringLiteral( "UseProjectScales" ), QStringLiteral( "0" ) ).toInt();
83 
84  QDomNodeList scalesNodes = element.elementsByTagName( QStringLiteral( "Scales" ) );
85  QVector< double > newScales;
86  if ( !scalesNodes.isEmpty() )
87  {
88  QDomElement scalesElement = scalesNodes.at( 0 ).toElement();
89  scalesNodes = scalesElement.elementsByTagName( QStringLiteral( "Scale" ) );
90  for ( int i = 0; i < scalesNodes.count(); i++ )
91  {
92  QDomElement scaleElement = scalesNodes.at( i ).toElement();
93  newScales.append( scaleElement.attribute( QStringLiteral( "Value" ) ).toDouble() );
94  }
95  }
96  if ( useProjectScale != mUseProjectScales || newScales != mMapScales )
97  {
98  mMapScales = newScales;
99  mUseProjectScales = useProjectScale;
100  emit mapScalesChanged();
101  }
102 
103  QDomElement defaultViewElement = element.firstChildElement( QStringLiteral( "DefaultViewExtent" ) );
104  if ( !defaultViewElement.isNull() )
105  {
106  double xMin = defaultViewElement.attribute( QStringLiteral( "xmin" ) ).toDouble();
107  double yMin = defaultViewElement.attribute( QStringLiteral( "ymin" ) ).toDouble();
108  double xMax = defaultViewElement.attribute( QStringLiteral( "xmax" ) ).toDouble();
109  double yMax = defaultViewElement.attribute( QStringLiteral( "ymax" ) ).toDouble();
111  crs.readXml( defaultViewElement );
112  mDefaultViewExtent = QgsReferencedRectangle( QgsRectangle( xMin, yMin, xMax, yMax ), crs );
113  }
114  else
115  {
116  mDefaultViewExtent = QgsReferencedRectangle();
117  }
118 
119  return true;
120 }
121 
122 QDomElement QgsProjectViewSettings::writeXml( QDomDocument &doc, const QgsReadWriteContext & ) const
123 {
124  QDomElement element = doc.createElement( QStringLiteral( "ProjectViewSettings" ) );
125  element.setAttribute( QStringLiteral( "UseProjectScales" ), mUseProjectScales ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
126 
127  QDomElement scales = doc.createElement( QStringLiteral( "Scales" ) );
128  for ( double scale : mMapScales )
129  {
130  QDomElement scaleElement = doc.createElement( QStringLiteral( "Scale" ) );
131  scaleElement.setAttribute( QStringLiteral( "Value" ), qgsDoubleToString( scale ) );
132  scales.appendChild( scaleElement );
133  }
134  element.appendChild( scales );
135 
136  if ( !mDefaultViewExtent.isNull() )
137  {
138  QDomElement defaultViewElement = doc.createElement( QStringLiteral( "DefaultViewExtent" ) );
139  defaultViewElement.setAttribute( QStringLiteral( "xmin" ), qgsDoubleToString( mDefaultViewExtent.xMinimum() ) );
140  defaultViewElement.setAttribute( QStringLiteral( "ymin" ), qgsDoubleToString( mDefaultViewExtent.yMinimum() ) );
141  defaultViewElement.setAttribute( QStringLiteral( "xmax" ), qgsDoubleToString( mDefaultViewExtent.xMaximum() ) );
142  defaultViewElement.setAttribute( QStringLiteral( "ymax" ), qgsDoubleToString( mDefaultViewExtent.yMaximum() ) );
143  mDefaultViewExtent.crs().writeXml( defaultViewElement, doc );
144  element.appendChild( defaultViewElement );
145  }
146 
147  return element;
148 }
The class is used as a container of context for various read/write operations on other objects...
A rectangle specified with double values.
Definition: qgsrectangle.h:41
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the settings&#39;s state from a DOM element.
QgsReferencedRectangle defaultViewExtent() const
Returns the default view extent, which should be used as the initial map extent when this project is ...
bool useProjectScales() const
Returns true if project mapScales() are enabled.
const QgsCoordinateReferenceSystem & crs
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the settings.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set...
void setMapScales(const QVector< double > &scales)
Sets the list of custom project map scales.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:275
QVector< double > mapScales() const
Returns the list of custom project map scales.
void setUseProjectScales(bool enabled)
Sets whether project mapScales() are enabled.
A QgsRectangle with associated coordinate reference system.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
QgsProjectViewSettings(QObject *parent=nullptr)
Constructor for QgsProjectViewSettings with the specified parent object.
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
This class represents a coordinate reference system (CRS).
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:436
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
void mapScalesChanged()
Emitted when the list of custom project map scales changes.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
void setDefaultViewExtent(const QgsReferencedRectangle &extent)
Sets the default view extent, which should be used as the initial map extent when this project is ope...
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
void reset()
Resets the settings to a default state.