26#include "moc_qgsprojectviewsettings.cpp"
31 , mRestoreProjectExtentOnProjectLoad( false )
42 const bool fullExtentChanged = !mPresetFullExtent.isNull();
44 if ( fullExtentChanged )
47 if ( mUseProjectScales || !mMapScales.empty() )
49 mUseProjectScales =
false;
57 return mDefaultViewExtent;
62 mDefaultViewExtent = extent;
67 return mPresetFullExtent;
72 if ( extent == mPresetFullExtent )
75 mPresetFullExtent = extent;
81 mRestoreProjectExtentOnProjectLoad = projectExtentCheckboxState;
86 return mRestoreProjectExtentOnProjectLoad;
93 return mPresetFullExtent;
95 if ( !mPresetFullExtent.isNull() )
105 QgsDebugError( QStringLiteral(
"Transform error encountered while determining project extent: %1" ).arg( e.
what() ) );
111 const QList< QgsMapLayer * > layers = mProject->mapLayers(
true ).values();
113 QList< QgsMapLayer * > nonBaseMapLayers;
114 std::copy_if( layers.begin(), layers.end(),
115 std::back_inserter( nonBaseMapLayers ),
116 [](
const QgsMapLayer * layer ) { return !( layer->properties() & Qgis::MapLayerProperty::IsBasemapLayer ) && !( layer->properties() & Qgis::MapLayerProperty::Is3DBasemapLayer ); } );
119 if ( !nonBaseMapLayers.empty( ) )
129 QVector< double > sorted = scales;
130 std::sort( sorted.begin(), sorted.end(), std::greater<double>() );
150 mUseProjectScales = enabled;
156 return mUseProjectScales;
161 return mDefaultRotation;
166 mDefaultRotation = rotation;
171 const bool useProjectScale = element.attribute( QStringLiteral(
"UseProjectScales" ), QStringLiteral(
"0" ) ).toInt();
173 QDomNodeList scalesNodes = element.elementsByTagName( QStringLiteral(
"Scales" ) );
174 QVector< double > newScales;
175 if ( !scalesNodes.isEmpty() )
177 const QDomElement scalesElement = scalesNodes.at( 0 ).toElement();
178 scalesNodes = scalesElement.elementsByTagName( QStringLiteral(
"Scale" ) );
179 for (
int i = 0; i < scalesNodes.count(); i++ )
181 const QDomElement scaleElement = scalesNodes.at( i ).toElement();
182 newScales.append( scaleElement.attribute( QStringLiteral(
"Value" ) ).toDouble() );
185 if ( useProjectScale != mUseProjectScales || newScales != mMapScales )
187 mMapScales = newScales;
188 mUseProjectScales = useProjectScale;
192 const QDomElement defaultViewElement = element.firstChildElement( QStringLiteral(
"DefaultViewExtent" ) );
193 if ( !defaultViewElement.isNull() )
195 const double xMin = defaultViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
196 const double yMin = defaultViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
197 const double xMax = defaultViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
198 const double yMax = defaultViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
200 crs.
readXml( defaultViewElement );
208 const QDomElement presetViewElement = element.firstChildElement( QStringLiteral(
"PresetFullExtent" ) );
209 if ( !presetViewElement.isNull() )
211 const double xMin = presetViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
212 const double yMin = presetViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
213 const double xMax = presetViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
214 const double yMax = presetViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
216 crs.
readXml( presetViewElement );
224 mDefaultRotation = element.attribute( QStringLiteral(
"rotation" ), QStringLiteral(
"0" ) ).toDouble();
225 mRestoreProjectExtentOnProjectLoad = element.attribute( QStringLiteral(
"LoadProjectExtent" ), QStringLiteral(
"0" ) ).toInt();
232 QDomElement element = doc.createElement( QStringLiteral(
"ProjectViewSettings" ) );
233 element.setAttribute( QStringLiteral(
"UseProjectScales" ), mUseProjectScales ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
235 if ( mRestoreProjectExtentOnProjectLoad )
237 element.setAttribute( QStringLiteral(
"LoadProjectExtent" ), QStringLiteral(
"1" ) );
240 element.setAttribute( QStringLiteral(
"rotation" ),
qgsDoubleToString( mDefaultRotation ) );
242 QDomElement scales = doc.createElement( QStringLiteral(
"Scales" ) );
243 for (
const double scale : mMapScales )
245 QDomElement scaleElement = doc.createElement( QStringLiteral(
"Scale" ) );
246 scaleElement.setAttribute( QStringLiteral(
"Value" ),
qgsDoubleToString( scale ) );
247 scales.appendChild( scaleElement );
249 element.appendChild( scales );
251 if ( !mDefaultViewExtent.isNull() )
253 QDomElement defaultViewElement = doc.createElement( QStringLiteral(
"DefaultViewExtent" ) );
254 defaultViewElement.setAttribute( QStringLiteral(
"xmin" ),
qgsDoubleToString( mDefaultViewExtent.xMinimum() ) );
255 defaultViewElement.setAttribute( QStringLiteral(
"ymin" ),
qgsDoubleToString( mDefaultViewExtent.yMinimum() ) );
256 defaultViewElement.setAttribute( QStringLiteral(
"xmax" ),
qgsDoubleToString( mDefaultViewExtent.xMaximum() ) );
257 defaultViewElement.setAttribute( QStringLiteral(
"ymax" ),
qgsDoubleToString( mDefaultViewExtent.yMaximum() ) );
258 mDefaultViewExtent.crs().writeXml( defaultViewElement, doc );
259 element.appendChild( defaultViewElement );
262 if ( !mPresetFullExtent.isNull() )
264 QDomElement presetViewElement = doc.createElement( QStringLiteral(
"PresetFullExtent" ) );
265 presetViewElement.setAttribute( QStringLiteral(
"xmin" ),
qgsDoubleToString( mPresetFullExtent.xMinimum() ) );
266 presetViewElement.setAttribute( QStringLiteral(
"ymin" ),
qgsDoubleToString( mPresetFullExtent.yMinimum() ) );
267 presetViewElement.setAttribute( QStringLiteral(
"xmax" ),
qgsDoubleToString( mPresetFullExtent.xMaximum() ) );
268 presetViewElement.setAttribute( QStringLiteral(
"ymax" ),
qgsDoubleToString( mPresetFullExtent.yMaximum() ) );
269 mPresetFullExtent.crs().writeXml( presetViewElement, doc );
270 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)