QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsselectivemaskingsourcesetmanager.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsselectivemaskingsourcesetmanager.cpp
3 ------------------
4 Date : January 2026
5 Copyright : (C) 2026 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 "qgsproject.h"
18#include "qgsreadwritecontext.h"
19#include "qgsruntimeprofiler.h"
21
22#include <QString>
23
24#include "moc_qgsselectivemaskingsourcesetmanager.cpp"
25
26using namespace Qt::StringLiterals;
27
39
44
49
51{
52 if ( set.id().isEmpty() )
53 return false;
54
56 {
57 if ( l->id() == set.id() )
58 {
59 l->setSources( set.sources() );
60 emit changed();
61 return true;
62 }
63 }
64 return false;
65}
66
68{
70 if ( !set )
71 return false;
72
73 return removeObject( set );
74}
75
80
81bool QgsSelectiveMaskingSourceSetManager::renameSet( const QString &oldName, const QString &newName )
82{
83 if ( objectByName( newName ) )
84 return false;
85
87 if ( !set )
88 return false;
89 set->setName( newName );
90 emit setRenamed( oldName, newName );
91 emit changed();
92 return true;
93}
94
95QVector<QgsSelectiveMaskingSourceSet> QgsSelectiveMaskingSourceSetManager::sets() const
96{
97 QVector<QgsSelectiveMaskingSourceSet> res;
98 res.reserve( mObjects.size() );
99 for ( const QgsSelectiveMaskingSourceSet *set : mObjects )
100 {
101 res.append( *set );
102 }
103 return res;
104}
105
107{
109 {
110 if ( l->id() == id )
111 return *l;
112 }
114}
115
117{
119 if ( set )
120 return *set;
121
123}
124
125bool QgsSelectiveMaskingSourceSetManager::readXml( const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context )
126{
127 clear();
128
129 QDomElement setsElem = element;
130 if ( element.tagName() != "SelectiveMaskingSourceSets"_L1 )
131 {
132 setsElem = element.firstChildElement( u"SelectiveMaskingSourceSets"_s );
133 }
134
135 QgsScopedRuntimeProfile runtimeProfile( tr( "Creating selective masking source sets" ), u"projectload"_s );
136
137 // restore sets
138 const QDomNodeList setsNodes = setsElem.childNodes();
139 bool result = true;
140 for ( int i = 0; i < setsNodes.size(); ++i )
141 {
142 if ( setsNodes.at( i ).nodeName() != "selectiveMaskingSourceSet"_L1 )
143 continue;
144
146 if ( !set.readXml( setsNodes.at( i ).toElement(), doc, context ) )
147 {
148 result = false;
149 continue;
150 }
151 if ( !addSet( set ) )
152 {
153 result = false;
154 }
155 }
156
157 emit changed();
158
159 return result;
160}
161
162QDomElement QgsSelectiveMaskingSourceSetManager::writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const
163{
164 QDomElement setsElem = doc.createElement( u"SelectiveMaskingSourceSets"_s );
165 for ( const QgsSelectiveMaskingSourceSet *l : mObjects )
166 {
167 QDomElement setElem = l->writeXml( doc, context );
168 setsElem.appendChild( setElem );
169 }
170 return setsElem;
171}
172
174{
175 QStringList names;
176 names.reserve( mObjects.size() );
178 {
179 names << l->name();
180 }
181 QString name;
182 int id = 1;
183 while ( name.isEmpty() || names.contains( name ) )
184 {
185 name = tr( "Masking Source Set %1" ).arg( id );
186 id++;
187 }
188 return name;
189}
QgsSelectiveMaskingSourceSet * objectByName(const QString &name) const
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:113
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.
bool renameSet(const QString &oldName, const QString &newName)
Renames a set in the manager from oldName to newName.
QVector< QgsSelectiveMaskingSourceSet > sets() const
Returns a list of all sets contained in the manager.
void setRemoved(const QString &name)
Emitted when a set was removed from the manager.
QgsSelectiveMaskingSourceSet setByName(const QString &name) const
Returns the set with a matching name, or an invalid set if no matching sets were found.
void setRenamed(const QString &oldName, const QString &newName)
Emitted when a set is renamed.
void clear()
Removes and deletes all sets from the manager.
bool readXml(const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context)
Reads the manager's state from a DOM element, restoring all sets present in the XML document.
QgsSelectiveMaskingSourceSet setById(const QString &id) const
Returns the set with a matching id, or an invalid set if no matching sets were found.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Returns a DOM element representing the state of the manager.
void setAdded(const QString &name)
Emitted when a set has been added to the manager.
QgsSelectiveMaskingSourceSetManager(QgsProject *project=nullptr)
Constructor for QgsSelectiveMaskingSourceSetManager.
bool addSet(const QgsSelectiveMaskingSourceSet &set)
Adds a set to the manager.
QString generateUniqueTitle() const
Generates a unique title for a new set, which does not clash with any already contained by the manage...
void setAboutToBeAdded(const QString &name)
Emitted when a set is about to be added to the manager.
bool removeSet(const QString &name)
Removes the set with matching name from the manager.
void setAboutToBeRemoved(const QString &name)
Emitted when a set is about to be removed from the manager.
void changed()
Emitted whenever sets stored within the manager are changed.
bool updateSet(const QgsSelectiveMaskingSourceSet &set)
Updates the definition of a set already in the manager.
Represents a named set of selective masking sources (QgsSelectiveMaskSource).
void setName(const QString &name)
Sets the set's unique name.
QString id() const
Returns a unique identifier for the set.
QVector< QgsSelectiveMaskSource > sources() const
Returns the list of selective mask sources configured in this set.
bool readXml(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets the set's state from a DOM element.