QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgselevationprofilemanager.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgselevationprofilemanager.cpp
3 ------------------
4 Date : July 2025
5 Copyright : (C) 2025 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
16
17#include "qgselevationprofile.h"
18#include "qgsproject.h"
19#include "qgsreadwritecontext.h"
20#include "qgsruntimeprofiler.h"
21
22#include "moc_qgselevationprofilemanager.cpp"
23
33
38
40{
41 return addObject( profile );
42}
43
45{
46 return removeObject( profile );
47}
48
53
54QList<QgsElevationProfile *> QgsElevationProfileManager::profiles() const
55{
56 return mObjects;
57}
58
60{
61 return objectByName( name );
62}
63
64bool QgsElevationProfileManager::readXml( const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context )
65{
66 clear();
67
68 QDomElement profilesElem = element;
69 if ( element.tagName() != QLatin1String( "ElevationProfiles" ) )
70 {
71 profilesElem = element.firstChildElement( QStringLiteral( "ElevationProfiles" ) );
72 }
73
74 QgsScopedRuntimeProfile runtimeProfile( tr( "Creating elevation profiles" ), QStringLiteral( "projectload" ) );
75
76 // restore profiles
77 const QDomNodeList profileNodes = profilesElem.childNodes();
78 bool result = true;
79 for ( int i = 0; i < profileNodes.size(); ++i )
80 {
81 if ( profileNodes.at( i ).nodeName() != QLatin1String( "ElevationProfile" ) )
82 continue;
83
84 const QString profileName = profileNodes.at( i ).toElement().attribute( QStringLiteral( "name" ) );
85 QgsScopedRuntimeProfile profile( profileName, QStringLiteral( "projectload" ) );
86
87 auto l = std::make_unique< QgsElevationProfile>( mProject );
88 if ( !l->readXml( profileNodes.at( i ).toElement(), doc, context ) )
89 {
90 result = false;
91 continue;
92 }
93 if ( !addProfile( l.release() ) )
94 {
95 result = false;
96 }
97 }
98
99 return result;
100}
101
102QDomElement QgsElevationProfileManager::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
103{
104 QDomElement profilesElem = doc.createElement( QStringLiteral( "ElevationProfiles" ) );
105 for ( QgsElevationProfile *l : mObjects )
106 {
107 QDomElement profileElem = l->writeXml( doc, context );
108 profilesElem.appendChild( profileElem );
109 }
110 return profilesElem;
111}
112
114{
115 for ( QgsElevationProfile *l : mObjects )
116 {
117 l->resolveReferences( project );
118 }
119}
120
122{
123 QStringList names;
124 names.reserve( mObjects.size() );
125 for ( QgsElevationProfile *l : mObjects )
126 {
127 names << l->name();
128 }
129 QString name;
130 int id = 1;
131 while ( name.isEmpty() || names.contains( name ) )
132 {
133 name = tr( "Elevation Profile %1" ).arg( id );
134 id++;
135 }
136 return name;
137}
138
140{
141 if ( profile )
142 {
143 connect( profile, &QgsElevationProfile::nameChanged, this, [this, profile]( const QString & newName )
144 {
145 emit profileRenamed( profile, newName );
146 } );
147 }
148}
void profileRenamed(QgsElevationProfile *profile, const QString &newName)
Emitted when a profile is renamed.
void profileRemoved(const QString &name)
Emitted when a profile was removed from the manager.
QgsElevationProfile * profileByName(const QString &name) const
Returns the profile with a matching name, or nullptr if no matching profiles were found.
void setupObjectConnections(QgsElevationProfile *profile) override
bool readXml(const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context)
Reads the manager's state from a DOM element, restoring all profiles present in the XML document.
QString generateUniqueTitle() const
Generates a unique title for a new profile, which does not clash with any already contained by the ma...
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the state of the manager.
void profileAdded(const QString &name)
Emitted when a profile has been added to the manager.
bool removeProfile(QgsElevationProfile *profile)
Removes a profile from the manager.
void profileAboutToBeAdded(const QString &name)
Emitted when a profile is about to be added to the manager.
void clear()
Removes and deletes all profiles from the manager.
bool addProfile(QgsElevationProfile *profile)
Adds a profile to the manager.
QList< QgsElevationProfile * > profiles() const
Returns a list of all profiles contained in the manager.
void profileAboutToBeRemoved(const QString &name)
Emitted when a profile is about to be removed from the manager.
QgsElevationProfileManager(QgsProject *project=nullptr)
Constructor for QgsElevationProfileManager.
void resolveReferences(const QgsProject *project)
After reading settings from XML, resolves references to any layers in a project that have been read a...
Represents an elevation profile attached to a project.
void nameChanged(const QString &newName)
Emitted when the profile is renamed.
void objectAdded(const QString &name)
Emitted when an object has been added to the manager.
void objectRemoved(const QString &name)
Emitted when an object was removed from the manager.
void objectAboutToBeAdded(const QString &name)
Emitted when an object is about to be added to the manager.
void objectAboutToBeRemoved(const QString &name)
Emitted when an object is about to be removed from the manager.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109
A container for the context for various read/write operations on objects.
Scoped object for logging of the runtime for a single operation or group of operations.