26#include "moc_qgsprojectviewsettings.cpp"
41 const bool fullExtentChanged = !mPresetFullExtent.isNull();
43 if ( fullExtentChanged )
46 if ( mUseProjectScales || !mMapScales.empty() )
48 mUseProjectScales =
false;
56 return mDefaultViewExtent;
61 mDefaultViewExtent = extent;
66 return mPresetFullExtent;
71 if ( extent == mPresetFullExtent )
74 mPresetFullExtent = extent;
80 mRestoreProjectExtentOnProjectLoad = projectExtentCheckboxState;
85 return mRestoreProjectExtentOnProjectLoad;
92 return mPresetFullExtent;
94 if ( !mPresetFullExtent.isNull() )
104 QgsDebugError( QStringLiteral(
"Transform error encountered while determining project extent: %1" ).arg( e.
what() ) );
110 const QList< QgsMapLayer * > layers = mProject->mapLayers(
true ).values();
112 QList< QgsMapLayer * > nonBaseMapLayers;
113 std::copy_if( layers.begin(), layers.end(),
114 std::back_inserter( nonBaseMapLayers ),
115 [](
const QgsMapLayer * layer ) { return !( layer->properties() & Qgis::MapLayerProperty::IsBasemapLayer ) && !( layer->properties() & Qgis::MapLayerProperty::Is3DBasemapLayer ); } );
118 if ( !nonBaseMapLayers.empty( ) )
128 QVector< double > sorted = scales;
129 std::sort( sorted.begin(), sorted.end(), std::greater<double>() );
149 mUseProjectScales = enabled;
155 return mUseProjectScales;
160 return mDefaultRotation;
165 mDefaultRotation = rotation;
170 const bool useProjectScale = element.attribute( QStringLiteral(
"UseProjectScales" ), QStringLiteral(
"0" ) ).toInt();
172 QDomNodeList scalesNodes = element.elementsByTagName( QStringLiteral(
"Scales" ) );
173 QVector< double > newScales;
174 if ( !scalesNodes.isEmpty() )
176 const QDomElement scalesElement = scalesNodes.at( 0 ).toElement();
177 scalesNodes = scalesElement.elementsByTagName( QStringLiteral(
"Scale" ) );
178 for (
int i = 0; i < scalesNodes.count(); i++ )
180 const QDomElement scaleElement = scalesNodes.at( i ).toElement();
181 newScales.append( scaleElement.attribute( QStringLiteral(
"Value" ) ).toDouble() );
184 if ( useProjectScale != mUseProjectScales || newScales != mMapScales )
186 mMapScales = newScales;
187 mUseProjectScales = useProjectScale;
191 const QDomElement defaultViewElement = element.firstChildElement( QStringLiteral(
"DefaultViewExtent" ) );
192 if ( !defaultViewElement.isNull() )
194 const double xMin = defaultViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
195 const double yMin = defaultViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
196 const double xMax = defaultViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
197 const double yMax = defaultViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
199 crs.
readXml( defaultViewElement );
207 const QDomElement presetViewElement = element.firstChildElement( QStringLiteral(
"PresetFullExtent" ) );
208 if ( !presetViewElement.isNull() )
210 const double xMin = presetViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
211 const double yMin = presetViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
212 const double xMax = presetViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
213 const double yMax = presetViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
215 crs.
readXml( presetViewElement );
223 mDefaultRotation = element.attribute( QStringLiteral(
"rotation" ), QStringLiteral(
"0" ) ).toDouble();
224 mRestoreProjectExtentOnProjectLoad = element.attribute( QStringLiteral(
"LoadProjectExtent" ), QStringLiteral(
"0" ) ).toInt();
231 QDomElement element = doc.createElement( QStringLiteral(
"ProjectViewSettings" ) );
232 element.setAttribute( QStringLiteral(
"UseProjectScales" ), mUseProjectScales ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
234 if ( mRestoreProjectExtentOnProjectLoad )
236 element.setAttribute( QStringLiteral(
"LoadProjectExtent" ), QStringLiteral(
"1" ) );
239 element.setAttribute( QStringLiteral(
"rotation" ),
qgsDoubleToString( mDefaultRotation ) );
241 QDomElement scales = doc.createElement( QStringLiteral(
"Scales" ) );
242 for (
const double scale : mMapScales )
244 QDomElement scaleElement = doc.createElement( QStringLiteral(
"Scale" ) );
245 scaleElement.setAttribute( QStringLiteral(
"Value" ),
qgsDoubleToString( scale ) );
246 scales.appendChild( scaleElement );
248 element.appendChild( scales );
250 if ( !mDefaultViewExtent.isNull() )
252 QDomElement defaultViewElement = doc.createElement( QStringLiteral(
"DefaultViewExtent" ) );
253 defaultViewElement.setAttribute( QStringLiteral(
"xmin" ),
qgsDoubleToString( mDefaultViewExtent.xMinimum() ) );
254 defaultViewElement.setAttribute( QStringLiteral(
"ymin" ),
qgsDoubleToString( mDefaultViewExtent.yMinimum() ) );
255 defaultViewElement.setAttribute( QStringLiteral(
"xmax" ),
qgsDoubleToString( mDefaultViewExtent.xMaximum() ) );
256 defaultViewElement.setAttribute( QStringLiteral(
"ymax" ),
qgsDoubleToString( mDefaultViewExtent.yMaximum() ) );
257 mDefaultViewExtent.crs().writeXml( defaultViewElement, doc );
258 element.appendChild( defaultViewElement );
261 if ( !mPresetFullExtent.isNull() )
263 QDomElement presetViewElement = doc.createElement( QStringLiteral(
"PresetFullExtent" ) );
264 presetViewElement.setAttribute( QStringLiteral(
"xmin" ),
qgsDoubleToString( mPresetFullExtent.xMinimum() ) );
265 presetViewElement.setAttribute( QStringLiteral(
"ymin" ),
qgsDoubleToString( mPresetFullExtent.yMinimum() ) );
266 presetViewElement.setAttribute( QStringLiteral(
"xmax" ),
qgsDoubleToString( mPresetFullExtent.xMaximum() ) );
267 presetViewElement.setAttribute( QStringLiteral(
"ymax" ),
qgsDoubleToString( mPresetFullExtent.yMaximum() ) );
268 mPresetFullExtent.crs().writeXml( presetViewElement, doc );
269 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)