QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 }
QgsProjectViewSettings::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the settings.
Definition: qgsprojectviewsettings.cpp:122
QgsProjectViewSettings::mapScalesChanged
void mapScalesChanged()
Emitted when the list of custom project map scales changes.
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
QgsReferencedRectangle
A QgsRectangle with associated coordinate reference system.
Definition: qgsreferencedgeometry.h:74
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:51
QgsRectangle::yMinimum
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
qgis.h
QgsProjectViewSettings::setDefaultViewExtent
void setDefaultViewExtent(const QgsReferencedRectangle &extent)
Sets the default view extent, which should be used as the initial map extent when this project is ope...
Definition: qgsprojectviewsettings.cpp:42
QgsProjectViewSettings::setUseProjectScales
void setUseProjectScales(bool enabled)
Sets whether project mapScales() are enabled.
Definition: qgsprojectviewsettings.cpp:66
QgsCoordinateReferenceSystem::readXml
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
Definition: qgscoordinatereferencesystem.cpp:2018
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
qgsDoubleToString
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:275
QgsRectangle::xMaximum
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
QgsProjectViewSettings::defaultViewExtent
QgsReferencedRectangle defaultViewExtent() const
Returns the default view extent, which should be used as the initial map extent when this project is ...
Definition: qgsprojectviewsettings.cpp:37
QgsCoordinateReferenceSystem::writeXml
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
Definition: qgscoordinatereferencesystem.cpp:2123
QgsRectangle::xMinimum
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:206
QgsProjectViewSettings::reset
void reset()
Resets the settings to a default state.
Definition: qgsprojectviewsettings.cpp:26
QgsReferencedGeometryBase::crs
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
Definition: qgsreferencedgeometry.h:53
qgsprojectviewsettings.h
QgsRectangle::yMaximum
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
QgsProjectViewSettings::mapScales
QVector< double > mapScales() const
Returns the list of custom project map scales.
Definition: qgsprojectviewsettings.cpp:61
QgsProjectViewSettings::setMapScales
void setMapScales(const QVector< double > &scales)
Sets the list of custom project map scales.
Definition: qgsprojectviewsettings.cpp:47
QgsProjectViewSettings::readXml
bool readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the settings's state from a DOM element.
Definition: qgsprojectviewsettings.cpp:80
QgsRectangle::isNull
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:447
QgsProjectViewSettings::QgsProjectViewSettings
QgsProjectViewSettings(QObject *parent=nullptr)
Constructor for QgsProjectViewSettings with the specified parent object.
Definition: qgsprojectviewsettings.cpp:20
QgsProjectViewSettings::useProjectScales
bool useProjectScales() const
Returns true if project mapScales() are enabled.
Definition: qgsprojectviewsettings.cpp:75