20 #include <QDomElement>
33 const bool fullExtentChanged = !mPresetFullExtent.
isNull();
35 if ( fullExtentChanged )
38 if ( mUseProjectScales || !mMapScales.empty() )
40 mUseProjectScales =
false;
48 return mDefaultViewExtent;
53 mDefaultViewExtent = extent;
58 return mPresetFullExtent;
63 if ( extent == mPresetFullExtent )
66 mPresetFullExtent = extent;
73 return mPresetFullExtent;
75 if ( !mPresetFullExtent.
isNull() )
90 const QMap<QString, QgsMapLayer *> layers = mProject->
mapLayers(
true );
91 QgsDebugMsgLevel( QStringLiteral(
"Layer count: %1" ).arg( layers.count() ), 5 );
92 for (
auto it = layers.constBegin(); it != layers.constEnd(); ++it )
97 if ( it.value()->extent().isNull() )
113 QgsDebugMsg( QStringLiteral(
"Could not reproject layer extent" ) );
130 const double padFactor = 1e-8;
149 QVector< double > sorted = scales;
150 std::sort( sorted.begin(), sorted.end(), std::greater<double>() );
170 mUseProjectScales = enabled;
176 return mUseProjectScales;
181 bool useProjectScale = element.attribute( QStringLiteral(
"UseProjectScales" ), QStringLiteral(
"0" ) ).toInt();
183 QDomNodeList scalesNodes = element.elementsByTagName( QStringLiteral(
"Scales" ) );
184 QVector< double > newScales;
185 if ( !scalesNodes.isEmpty() )
187 QDomElement scalesElement = scalesNodes.at( 0 ).toElement();
188 scalesNodes = scalesElement.elementsByTagName( QStringLiteral(
"Scale" ) );
189 for (
int i = 0; i < scalesNodes.count(); i++ )
191 QDomElement scaleElement = scalesNodes.at( i ).toElement();
192 newScales.append( scaleElement.attribute( QStringLiteral(
"Value" ) ).toDouble() );
195 if ( useProjectScale != mUseProjectScales || newScales != mMapScales )
197 mMapScales = newScales;
198 mUseProjectScales = useProjectScale;
202 QDomElement defaultViewElement = element.firstChildElement( QStringLiteral(
"DefaultViewExtent" ) );
203 if ( !defaultViewElement.isNull() )
205 double xMin = defaultViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
206 double yMin = defaultViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
207 double xMax = defaultViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
208 double yMax = defaultViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
218 QDomElement presetViewElement = element.firstChildElement( QStringLiteral(
"PresetFullExtent" ) );
219 if ( !presetViewElement.isNull() )
221 double xMin = presetViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
222 double yMin = presetViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
223 double xMax = presetViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
224 double yMax = presetViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
239 QDomElement element = doc.createElement( QStringLiteral(
"ProjectViewSettings" ) );
240 element.setAttribute( QStringLiteral(
"UseProjectScales" ), mUseProjectScales ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
242 QDomElement scales = doc.createElement( QStringLiteral(
"Scales" ) );
243 for (
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" ) );
258 mDefaultViewExtent.
crs().
writeXml( defaultViewElement, doc );
259 element.appendChild( defaultViewElement );
262 if ( !mPresetFullExtent.
isNull() )
264 QDomElement presetViewElement = doc.createElement( QStringLiteral(
"PresetFullExtent" ) );
269 mPresetFullExtent.
crs().
writeXml( presetViewElement, doc );
270 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.
Custom exception class for Coordinate Reference System related exceptions.
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.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
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()).
double height() const SIP_HOLDGIL
Returns the height of the rectangle.
void set(const QgsPointXY &p1, const QgsPointXY &p2)
Sets the rectangle from two QgsPoints.
void setMinimal() SIP_HOLDGIL
Set a rectangle so that min corner is at max and max corner is at min.
double width() const SIP_HOLDGIL
Returns the width of the rectangle.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given 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.
#define QgsDebugMsgLevel(str, level)
const QgsCoordinateReferenceSystem & crs