36 const bool fullExtentChanged = !mPresetFullExtent.
isNull();
38 if ( fullExtentChanged )
41 if ( mUseProjectScales || !mMapScales.empty() )
43 mUseProjectScales =
false;
51 return mDefaultViewExtent;
56 mDefaultViewExtent = extent;
61 return mPresetFullExtent;
66 if ( extent == mPresetFullExtent )
69 mPresetFullExtent = extent;
76 return mPresetFullExtent;
78 if ( !mPresetFullExtent.
isNull() )
86 const QList< QgsMapLayer * > layers = mProject->
mapLayers(
true ).values();
88 QList< QgsMapLayer * > nonBaseMapLayers;
89 std::copy_if( layers.begin(), layers.end(),
90 std::back_inserter( nonBaseMapLayers ),
91 [](
const QgsMapLayer * layer ) { return !( layer->properties() & Qgis::MapLayerProperty::IsBasemapLayer ); } );
94 if ( !nonBaseMapLayers.empty( ) )
104 QVector< double > sorted = scales;
105 std::sort( sorted.begin(), sorted.end(), std::greater<double>() );
125 mUseProjectScales = enabled;
131 return mUseProjectScales;
136 return mDefaultRotation;
141 mDefaultRotation = rotation;
146 const bool useProjectScale = element.attribute( QStringLiteral(
"UseProjectScales" ), QStringLiteral(
"0" ) ).toInt();
148 QDomNodeList scalesNodes = element.elementsByTagName( QStringLiteral(
"Scales" ) );
149 QVector< double > newScales;
150 if ( !scalesNodes.isEmpty() )
152 const QDomElement scalesElement = scalesNodes.at( 0 ).toElement();
153 scalesNodes = scalesElement.elementsByTagName( QStringLiteral(
"Scale" ) );
154 for (
int i = 0; i < scalesNodes.count(); i++ )
156 const QDomElement scaleElement = scalesNodes.at( i ).toElement();
157 newScales.append( scaleElement.attribute( QStringLiteral(
"Value" ) ).toDouble() );
160 if ( useProjectScale != mUseProjectScales || newScales != mMapScales )
162 mMapScales = newScales;
163 mUseProjectScales = useProjectScale;
167 const QDomElement defaultViewElement = element.firstChildElement( QStringLiteral(
"DefaultViewExtent" ) );
168 if ( !defaultViewElement.isNull() )
170 const double xMin = defaultViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
171 const double yMin = defaultViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
172 const double xMax = defaultViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
173 const double yMax = defaultViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
183 const QDomElement presetViewElement = element.firstChildElement( QStringLiteral(
"PresetFullExtent" ) );
184 if ( !presetViewElement.isNull() )
186 const double xMin = presetViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
187 const double yMin = presetViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
188 const double xMax = presetViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
189 const double yMax = presetViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
199 mDefaultRotation = element.attribute( QStringLiteral(
"rotation" ), QStringLiteral(
"0" ) ).toDouble();
206 QDomElement element = doc.createElement( QStringLiteral(
"ProjectViewSettings" ) );
207 element.setAttribute( QStringLiteral(
"UseProjectScales" ), mUseProjectScales ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
209 element.setAttribute( QStringLiteral(
"rotation" ),
qgsDoubleToString( mDefaultRotation ) );
211 QDomElement scales = doc.createElement( QStringLiteral(
"Scales" ) );
212 for (
const double scale : mMapScales )
214 QDomElement scaleElement = doc.createElement( QStringLiteral(
"Scale" ) );
215 scaleElement.setAttribute( QStringLiteral(
"Value" ),
qgsDoubleToString( scale ) );
216 scales.appendChild( scaleElement );
218 element.appendChild( scales );
220 if ( !mDefaultViewExtent.
isNull() )
222 QDomElement defaultViewElement = doc.createElement( QStringLiteral(
"DefaultViewExtent" ) );
227 mDefaultViewExtent.
crs().
writeXml( defaultViewElement, doc );
228 element.appendChild( defaultViewElement );
231 if ( !mPresetFullExtent.
isNull() )
233 QDomElement presetViewElement = doc.createElement( QStringLiteral(
"PresetFullExtent" ) );
238 mPresetFullExtent.
crs().
writeXml( presetViewElement, doc );
239 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.
Base class for all map layer types.
double defaultRotation() const
Returns the default map rotation (in clockwise degrees) for maps in the project.
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 setDefaultRotation(double rotation)
Set the default rotation of maps in the project, in clockwise degrees.
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 xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
bool isNull() const
Test if the rectangle is null (holding no spatial information).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
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