27#include "moc_qgsprojectviewsettings.cpp"
29using namespace Qt::StringLiterals;
44 const bool fullExtentChanged = !mPresetFullExtent.isNull();
46 if ( fullExtentChanged )
49 if ( mUseProjectScales || !mMapScales.empty() )
51 mUseProjectScales =
false;
59 return mDefaultViewExtent;
64 mDefaultViewExtent = extent;
69 return mPresetFullExtent;
74 if ( extent == mPresetFullExtent )
77 mPresetFullExtent = extent;
83 mRestoreProjectExtentOnProjectLoad = projectExtentCheckboxState;
88 return mRestoreProjectExtentOnProjectLoad;
95 return mPresetFullExtent;
97 if ( !mPresetFullExtent.isNull() )
107 QgsDebugError( u
"Transform error encountered while determining project extent: %1"_s.arg( e.
what() ) );
113 const QList< QgsMapLayer * > layers = mProject->mapLayers(
true ).values();
115 QList< QgsMapLayer * > nonBaseMapLayers;
116 std::copy_if( layers.begin(), layers.end(),
117 std::back_inserter( nonBaseMapLayers ),
118 [](
const QgsMapLayer * layer ) { return !( layer->properties() & Qgis::MapLayerProperty::IsBasemapLayer ) && !( layer->properties() & Qgis::MapLayerProperty::Is3DBasemapLayer ); } );
121 if ( !nonBaseMapLayers.empty( ) )
131 QVector< double > sorted = scales;
132 std::sort( sorted.begin(), sorted.end(), std::greater<double>() );
152 mUseProjectScales = enabled;
158 return mUseProjectScales;
163 return mDefaultRotation;
168 mDefaultRotation = rotation;
173 const bool useProjectScale = element.attribute( u
"UseProjectScales"_s, u
"0"_s ).toInt();
175 QDomNodeList scalesNodes = element.elementsByTagName( u
"Scales"_s );
176 QVector< double > newScales;
177 if ( !scalesNodes.isEmpty() )
179 const QDomElement scalesElement = scalesNodes.at( 0 ).toElement();
180 scalesNodes = scalesElement.elementsByTagName( u
"Scale"_s );
181 for (
int i = 0; i < scalesNodes.count(); i++ )
183 const QDomElement scaleElement = scalesNodes.at( i ).toElement();
184 newScales.append( scaleElement.attribute( u
"Value"_s ).toDouble() );
187 if ( useProjectScale != mUseProjectScales || newScales != mMapScales )
189 mMapScales = newScales;
190 mUseProjectScales = useProjectScale;
194 const QDomElement defaultViewElement = element.firstChildElement( u
"DefaultViewExtent"_s );
195 if ( !defaultViewElement.isNull() )
197 const double xMin = defaultViewElement.attribute( u
"xmin"_s ).toDouble();
198 const double yMin = defaultViewElement.attribute( u
"ymin"_s ).toDouble();
199 const double xMax = defaultViewElement.attribute( u
"xmax"_s ).toDouble();
200 const double yMax = defaultViewElement.attribute( u
"ymax"_s ).toDouble();
202 crs.
readXml( defaultViewElement );
210 const QDomElement presetViewElement = element.firstChildElement( u
"PresetFullExtent"_s );
211 if ( !presetViewElement.isNull() )
213 const double xMin = presetViewElement.attribute( u
"xmin"_s ).toDouble();
214 const double yMin = presetViewElement.attribute( u
"ymin"_s ).toDouble();
215 const double xMax = presetViewElement.attribute( u
"xmax"_s ).toDouble();
216 const double yMax = presetViewElement.attribute( u
"ymax"_s ).toDouble();
218 crs.
readXml( presetViewElement );
226 mDefaultRotation = element.attribute( u
"rotation"_s, u
"0"_s ).toDouble();
227 mRestoreProjectExtentOnProjectLoad = element.attribute( u
"LoadProjectExtent"_s, u
"0"_s ).toInt();
234 QDomElement element = doc.createElement( u
"ProjectViewSettings"_s );
235 element.setAttribute( u
"UseProjectScales"_s, mUseProjectScales ? u
"1"_s : u
"0"_s );
237 if ( mRestoreProjectExtentOnProjectLoad )
239 element.setAttribute( u
"LoadProjectExtent"_s, u
"1"_s );
244 QDomElement scales = doc.createElement( u
"Scales"_s );
245 for (
const double scale : mMapScales )
247 QDomElement scaleElement = doc.createElement( u
"Scale"_s );
249 scales.appendChild( scaleElement );
251 element.appendChild( scales );
253 if ( !mDefaultViewExtent.isNull() )
255 QDomElement defaultViewElement = doc.createElement( u
"DefaultViewExtent"_s );
256 defaultViewElement.setAttribute( u
"xmin"_s,
qgsDoubleToString( mDefaultViewExtent.xMinimum() ) );
257 defaultViewElement.setAttribute( u
"ymin"_s,
qgsDoubleToString( mDefaultViewExtent.yMinimum() ) );
258 defaultViewElement.setAttribute( u
"xmax"_s,
qgsDoubleToString( mDefaultViewExtent.xMaximum() ) );
259 defaultViewElement.setAttribute( u
"ymax"_s,
qgsDoubleToString( mDefaultViewExtent.yMaximum() ) );
260 mDefaultViewExtent.crs().writeXml( defaultViewElement, doc );
261 element.appendChild( defaultViewElement );
264 if ( !mPresetFullExtent.isNull() )
266 QDomElement presetViewElement = doc.createElement( u
"PresetFullExtent"_s );
267 presetViewElement.setAttribute( u
"xmin"_s,
qgsDoubleToString( mPresetFullExtent.xMinimum() ) );
268 presetViewElement.setAttribute( u
"ymin"_s,
qgsDoubleToString( mPresetFullExtent.yMinimum() ) );
269 presetViewElement.setAttribute( u
"xmax"_s,
qgsDoubleToString( mPresetFullExtent.xMaximum() ) );
270 presetViewElement.setAttribute( u
"ymax"_s,
qgsDoubleToString( mPresetFullExtent.yMaximum() ) );
271 mPresetFullExtent.crs().writeXml( presetViewElement, doc );
272 element.appendChild( presetViewElement );
Represents a coordinate reference system (CRS).
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
Custom exception class for Coordinate Reference System related exceptions.
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 restoreProjectExtentOnProjectLoad()
Returns whether the project's preset full extent should be restored when the project is loaded.
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.
void setRestoreProjectExtentOnProjectLoad(bool state)
Sets whether the project's preset full extent should be restored when the project is loaded.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
A QgsRectangle with associated coordinate reference system.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
#define QgsDebugError(str)