22 #include <QDomElement>
35 const bool fullExtentChanged = !mPresetFullExtent.
isNull();
37 if ( fullExtentChanged )
40 if ( mUseProjectScales || !mMapScales.empty() )
42 mUseProjectScales =
false;
50 return mDefaultViewExtent;
55 mDefaultViewExtent = extent;
60 return mPresetFullExtent;
65 if ( extent == mPresetFullExtent )
68 mPresetFullExtent = extent;
75 return mPresetFullExtent;
77 if ( !mPresetFullExtent.
isNull() )
85 const QList< QgsMapLayer * > layers = mProject->
mapLayers(
true ).values();
93 QVector< double > sorted = scales;
94 std::sort( sorted.begin(), sorted.end(), std::greater<double>() );
114 mUseProjectScales = enabled;
120 return mUseProjectScales;
125 const bool useProjectScale = element.attribute( QStringLiteral(
"UseProjectScales" ), QStringLiteral(
"0" ) ).toInt();
127 QDomNodeList scalesNodes = element.elementsByTagName( QStringLiteral(
"Scales" ) );
128 QVector< double > newScales;
129 if ( !scalesNodes.isEmpty() )
131 const QDomElement scalesElement = scalesNodes.at( 0 ).toElement();
132 scalesNodes = scalesElement.elementsByTagName( QStringLiteral(
"Scale" ) );
133 for (
int i = 0; i < scalesNodes.count(); i++ )
135 const QDomElement scaleElement = scalesNodes.at( i ).toElement();
136 newScales.append( scaleElement.attribute( QStringLiteral(
"Value" ) ).toDouble() );
139 if ( useProjectScale != mUseProjectScales || newScales != mMapScales )
141 mMapScales = newScales;
142 mUseProjectScales = useProjectScale;
146 const QDomElement defaultViewElement = element.firstChildElement( QStringLiteral(
"DefaultViewExtent" ) );
147 if ( !defaultViewElement.isNull() )
149 const double xMin = defaultViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
150 const double yMin = defaultViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
151 const double xMax = defaultViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
152 const double yMax = defaultViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
162 const QDomElement presetViewElement = element.firstChildElement( QStringLiteral(
"PresetFullExtent" ) );
163 if ( !presetViewElement.isNull() )
165 const double xMin = presetViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
166 const double yMin = presetViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
167 const double xMax = presetViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
168 const double yMax = presetViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
183 QDomElement element = doc.createElement( QStringLiteral(
"ProjectViewSettings" ) );
184 element.setAttribute( QStringLiteral(
"UseProjectScales" ), mUseProjectScales ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
186 QDomElement scales = doc.createElement( QStringLiteral(
"Scales" ) );
187 for (
const double scale : mMapScales )
189 QDomElement scaleElement = doc.createElement( QStringLiteral(
"Scale" ) );
190 scaleElement.setAttribute( QStringLiteral(
"Value" ),
qgsDoubleToString( scale ) );
191 scales.appendChild( scaleElement );
193 element.appendChild( scales );
195 if ( !mDefaultViewExtent.
isNull() )
197 QDomElement defaultViewElement = doc.createElement( QStringLiteral(
"DefaultViewExtent" ) );
202 mDefaultViewExtent.
crs().
writeXml( defaultViewElement, doc );
203 element.appendChild( defaultViewElement );
206 if ( !mPresetFullExtent.
isNull() )
208 QDomElement presetViewElement = doc.createElement( QStringLiteral(
"PresetFullExtent" ) );
213 mPresetFullExtent.
crs().
writeXml( presetViewElement, doc );
214 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