21 #include <QDomElement>
34 const bool fullExtentChanged = !mPresetFullExtent.
isNull();
36 if ( fullExtentChanged )
39 if ( mUseProjectScales || !mMapScales.empty() )
41 mUseProjectScales =
false;
49 return mDefaultViewExtent;
54 mDefaultViewExtent = extent;
59 return mPresetFullExtent;
64 if ( extent == mPresetFullExtent )
67 mPresetFullExtent = extent;
74 return mPresetFullExtent;
76 if ( !mPresetFullExtent.
isNull() )
84 const QList< QgsMapLayer * > layers = mProject->
mapLayers(
true ).values();
92 QVector< double > sorted = scales;
93 std::sort( sorted.begin(), sorted.end(), std::greater<double>() );
113 mUseProjectScales = enabled;
119 return mUseProjectScales;
124 bool useProjectScale = element.attribute( QStringLiteral(
"UseProjectScales" ), QStringLiteral(
"0" ) ).toInt();
126 QDomNodeList scalesNodes = element.elementsByTagName( QStringLiteral(
"Scales" ) );
127 QVector< double > newScales;
128 if ( !scalesNodes.isEmpty() )
130 QDomElement scalesElement = scalesNodes.at( 0 ).toElement();
131 scalesNodes = scalesElement.elementsByTagName( QStringLiteral(
"Scale" ) );
132 for (
int i = 0; i < scalesNodes.count(); i++ )
134 QDomElement scaleElement = scalesNodes.at( i ).toElement();
135 newScales.append( scaleElement.attribute( QStringLiteral(
"Value" ) ).toDouble() );
138 if ( useProjectScale != mUseProjectScales || newScales != mMapScales )
140 mMapScales = newScales;
141 mUseProjectScales = useProjectScale;
145 QDomElement defaultViewElement = element.firstChildElement( QStringLiteral(
"DefaultViewExtent" ) );
146 if ( !defaultViewElement.isNull() )
148 double xMin = defaultViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
149 double yMin = defaultViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
150 double xMax = defaultViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
151 double yMax = defaultViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
161 QDomElement presetViewElement = element.firstChildElement( QStringLiteral(
"PresetFullExtent" ) );
162 if ( !presetViewElement.isNull() )
164 double xMin = presetViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
165 double yMin = presetViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
166 double xMax = presetViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
167 double yMax = presetViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
182 QDomElement element = doc.createElement( QStringLiteral(
"ProjectViewSettings" ) );
183 element.setAttribute( QStringLiteral(
"UseProjectScales" ), mUseProjectScales ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
185 QDomElement scales = doc.createElement( QStringLiteral(
"Scales" ) );
186 for (
double scale : mMapScales )
188 QDomElement scaleElement = doc.createElement( QStringLiteral(
"Scale" ) );
189 scaleElement.setAttribute( QStringLiteral(
"Value" ),
qgsDoubleToString( scale ) );
190 scales.appendChild( scaleElement );
192 element.appendChild( scales );
194 if ( !mDefaultViewExtent.
isNull() )
196 QDomElement defaultViewElement = doc.createElement( QStringLiteral(
"DefaultViewExtent" ) );
201 mDefaultViewExtent.
crs().
writeXml( defaultViewElement, doc );
202 element.appendChild( defaultViewElement );
205 if ( !mPresetFullExtent.
isNull() )
207 QDomElement presetViewElement = doc.createElement( QStringLiteral(
"PresetFullExtent" ) );
212 mPresetFullExtent.
crs().
writeXml( presetViewElement, doc );
213 element.appendChild( presetViewElement );
This class represents a coordinate reference system (CRS).
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
static QgsRectangle combinedExtent(const QList< QgsMapLayer * > &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext)
Returns the combined extent of a list of layers.
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.
QgsReferencedRectangle presetFullExtent() const
Returns the project's preset full extent.
void setPresetFullExtent(const QgsReferencedRectangle &extent)
Sets the project's preset full extent.
void reset()
Resets the settings to a default state.
void presetFullExtentChanged()
Emitted whenever the presetFullExtent() is changed.
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 readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the settings's state from a DOM element.
void setMapScales(const QVector< double > &scales)
Sets the list of custom project map scales.
void setUseProjectScales(bool enabled)
Sets whether project mapScales() are enabled.
QVector< double > mapScales() const
Returns the list of custom project map scales.
QgsProjectViewSettings(QgsProject *project=nullptr)
Constructor for QgsProjectViewSettings for the specified project.
void mapScalesChanged()
Emitted when the list of custom project map scales changes.
QgsReferencedRectangle fullExtent() const
Returns the full extent of the project, which represents the maximal limits of the project.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the settings.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QgsCoordinateTransformContext transformContext
QgsCoordinateReferenceSystem crs
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsRectangle with associated coordinate reference system.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
const QgsCoordinateReferenceSystem & crs