QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsprojectstoredobjectmanager.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojectstoredobjectmanager.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 ***************************************************************************/
15
17
18#include "qgsproject.h"
19
20#include "moc_qgsprojectstoredobjectmanager.cpp"
21
22//
23// QgsProjectStoredObjectManagerBase
24//
25
27 : QObject( project )
28 , mProject( project )
29{
30
31}
32
37
38//
39// QgsAbstractProjectStoredObjectManager
40//
41
42template<class T>
48
49template<class T>
51{
52 Q_ASSERT_X( mObjects.isEmpty(), "~QgsAbstractProjectStoredObjectManager", "Subclasses of QgsAbstractProjectStoredObjectManager MUST explicitly call clearObjects() in their class destructor." );
54}
55
56template<class T>
58{
59 return mObjects;
60}
61
62template<class T>
64{
65 for ( T *l : mObjects )
66 {
67 if ( l->name() == name )
68 return l;
69 }
70 return nullptr;
71}
72
73template<class T>
75{
76 const QList< T * > objects = mObjects;
77 for ( T *l : objects )
78 {
79 removeObject( l );
80 }
81}
82
83template<class T>
85{
86 if ( !object || mObjects.contains( object ) )
87 return false;
88
89 // check for duplicate name
90 const QList<T *> constObjects = mObjects;
91 for ( T *l : constObjects )
92 {
93 if ( l->name() == object->name() )
94 {
95 delete object;
96 return false;
97 }
98 }
99
101
102 emit objectAboutToBeAdded( object->name() );
103 mObjects << object;
104 emit objectAdded( object->name() );
106 return true;
107}
108
109template<class T>
112 if ( !object )
113 return false;
114
115 if ( !mObjects.contains( object ) )
116 return false;
117
118 QString name = object->name();
119 emit objectAboutToBeRemoved( name );
120 mObjects.removeAll( object );
121 delete object;
122 emit objectRemoved( name );
124 return true;
125}
126
127template<class T>
133
134template class QgsAbstractProjectStoredObjectManager<QgsMasterLayoutInterface>; // clazy:exclude=missing-qobject-macro
135
136#include "qgselevationprofile.h"
137
138template class QgsAbstractProjectStoredObjectManager<QgsElevationProfile>; // clazy:exclude=missing-qobject-macro
139
T * objectByName(const QString &name) const
Returns the object with a matching name, or nullptr if no matching objects were found.
QList< T * > objects() const
Returns the list of objects contained within the manager.
QList< T * > mObjects
Attached objects, owned by the manager.
bool addObject(T *object)
Adds an object to the manager.
bool removeObject(T *object)
Removes an object from the manager.
QgsAbstractProjectStoredObjectManager(QgsProject *project=nullptr)
Constructor for QgsAbstractProjectStoredObjectManager, for objects attached to the specified project.
void clearObjects()
Removes and deletes all objects from the manager.
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.
void markProjectDirty()
Marks the project as dirty.
QgsProjectStoredObjectManagerBase(QgsProject *project=nullptr)
Constructor for QgsProjectStoredObjectManagerBase, for objects attached to the specified project.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:109