QGIS API Documentation 3.99.0-Master (d270888f95f)
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 <QString>
23
24#include "moc_qgselevationprofilemanager.cpp"
25
26using namespace Qt::StringLiterals;
27
37
42
44{
45 return addObject( profile );
46}
47
49{
50 return removeObject( profile );
51}
52
57
58QList<QgsElevationProfile *> QgsElevationProfileManager::profiles() const
59{
60 return mObjects;
61}
62
64{
65 return objectByName( name );
66}
67
68bool QgsElevationProfileManager::readXml( const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context )
69{
70 clear();
71
72 QDomElement profilesElem = element;
73 if ( element.tagName() != "ElevationProfiles"_L1 )
74 {
75 profilesElem = element.firstChildElement( u"ElevationProfiles"_s );
76 }
77
78 QgsScopedRuntimeProfile runtimeProfile( tr( "Creating elevation profiles" ), u"projectload"_s );
79
80 // restore profiles
81 const QDomNodeList profileNodes = profilesElem.childNodes();
82 bool result = true;
83 for ( int i = 0; i < profileNodes.size(); ++i )
84 {
85 if ( profileNodes.at( i ).nodeName() != "ElevationProfile"_L1 )
86 continue;
87
88 const QString profileName = profileNodes.at( i ).toElement().attribute( u"name"_s );
89 QgsScopedRuntimeProfile profile( profileName, u"projectload"_s );
90
91 auto l = std::make_unique< QgsElevationProfile>( mProject );
92 if ( !l->readXml( profileNodes.at( i ).toElement(), doc, context ) )
93 {
94 result = false;
95 continue;
96 }
97 if ( !addProfile( l.release() ) )
98 {
99 result = false;
100 }
101 }
102
103 return result;
104}
105
106QDomElement QgsElevationProfileManager::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
107{
108 QDomElement profilesElem = doc.createElement( u"ElevationProfiles"_s );
109 for ( QgsElevationProfile *l : mObjects )
110 {
111 QDomElement profileElem = l->writeXml( doc, context );
112 profilesElem.appendChild( profileElem );
113 }
114 return profilesElem;
115}
116
118{
119 for ( QgsElevationProfile *l : mObjects )
120 {
121 l->resolveReferences( project );
122 }
123}
124
126{
127 QStringList names;
128 names.reserve( mObjects.size() );
129 for ( QgsElevationProfile *l : mObjects )
130 {
131 names << l->name();
132 }
133 QString name;
134 int id = 1;
135 while ( name.isEmpty() || names.contains( name ) )
136 {
137 name = tr( "Elevation Profile %1" ).arg( id );
138 id++;
139 }
140 return name;
141}
142
144{
145 if ( profile )
146 {
147 connect( profile, &QgsElevationProfile::nameChanged, this, [this, profile]( const QString & newName )
148 {
149 emit profileRenamed( profile, newName );
150 } );
151 }
152}
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:112
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.