17#include "moc_qgsprojectviewsettings.cpp"
37 const bool fullExtentChanged = !mPresetFullExtent.
isNull();
39 if ( fullExtentChanged )
42 if ( mUseProjectScales || !mMapScales.empty() )
44 mUseProjectScales =
false;
52 return mDefaultViewExtent;
57 mDefaultViewExtent = extent;
62 return mPresetFullExtent;
67 if ( extent == mPresetFullExtent )
70 mPresetFullExtent = extent;
77 return mPresetFullExtent;
79 if ( !mPresetFullExtent.
isNull() )
87 const QList< QgsMapLayer * > layers = mProject->
mapLayers(
true ).values();
89 QList< QgsMapLayer * > nonBaseMapLayers;
90 std::copy_if( layers.begin(), layers.end(),
91 std::back_inserter( nonBaseMapLayers ),
92 [](
const QgsMapLayer * layer ) { return !( layer->properties() & Qgis::MapLayerProperty::IsBasemapLayer ); } );
95 if ( !nonBaseMapLayers.empty( ) )
105 QVector< double > sorted = scales;
106 std::sort( sorted.begin(), sorted.end(), std::greater<double>() );
126 mUseProjectScales = enabled;
132 return mUseProjectScales;
137 return mDefaultRotation;
142 mDefaultRotation = rotation;
147 const bool useProjectScale = element.attribute( QStringLiteral(
"UseProjectScales" ), QStringLiteral(
"0" ) ).toInt();
149 QDomNodeList scalesNodes = element.elementsByTagName( QStringLiteral(
"Scales" ) );
150 QVector< double > newScales;
151 if ( !scalesNodes.isEmpty() )
153 const QDomElement scalesElement = scalesNodes.at( 0 ).toElement();
154 scalesNodes = scalesElement.elementsByTagName( QStringLiteral(
"Scale" ) );
155 for (
int i = 0; i < scalesNodes.count(); i++ )
157 const QDomElement scaleElement = scalesNodes.at( i ).toElement();
158 newScales.append( scaleElement.attribute( QStringLiteral(
"Value" ) ).toDouble() );
161 if ( useProjectScale != mUseProjectScales || newScales != mMapScales )
163 mMapScales = newScales;
164 mUseProjectScales = useProjectScale;
168 const QDomElement defaultViewElement = element.firstChildElement( QStringLiteral(
"DefaultViewExtent" ) );
169 if ( !defaultViewElement.isNull() )
171 const double xMin = defaultViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
172 const double yMin = defaultViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
173 const double xMax = defaultViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
174 const double yMax = defaultViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
184 const QDomElement presetViewElement = element.firstChildElement( QStringLiteral(
"PresetFullExtent" ) );
185 if ( !presetViewElement.isNull() )
187 const double xMin = presetViewElement.attribute( QStringLiteral(
"xmin" ) ).toDouble();
188 const double yMin = presetViewElement.attribute( QStringLiteral(
"ymin" ) ).toDouble();
189 const double xMax = presetViewElement.attribute( QStringLiteral(
"xmax" ) ).toDouble();
190 const double yMax = presetViewElement.attribute( QStringLiteral(
"ymax" ) ).toDouble();
200 mDefaultRotation = element.attribute( QStringLiteral(
"rotation" ), QStringLiteral(
"0" ) ).toDouble();
207 QDomElement element = doc.createElement( QStringLiteral(
"ProjectViewSettings" ) );
208 element.setAttribute( QStringLiteral(
"UseProjectScales" ), mUseProjectScales ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
210 element.setAttribute( QStringLiteral(
"rotation" ),
qgsDoubleToString( mDefaultRotation ) );
212 QDomElement scales = doc.createElement( QStringLiteral(
"Scales" ) );
213 for (
const double scale : mMapScales )
215 QDomElement scaleElement = doc.createElement( QStringLiteral(
"Scale" ) );
216 scaleElement.setAttribute( QStringLiteral(
"Value" ),
qgsDoubleToString( scale ) );
217 scales.appendChild( scaleElement );
219 element.appendChild( scales );
221 if ( !mDefaultViewExtent.
isNull() )
223 QDomElement defaultViewElement = doc.createElement( QStringLiteral(
"DefaultViewExtent" ) );
228 mDefaultViewExtent.
crs().
writeXml( defaultViewElement, doc );
229 element.appendChild( defaultViewElement );
232 if ( !mPresetFullExtent.
isNull() )
234 QDomElement presetViewElement = doc.createElement( QStringLiteral(
"PresetFullExtent" ) );
239 mPresetFullExtent.
crs().
writeXml( presetViewElement, doc );
240 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