QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgssettingstreenode.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssettingstreenode.cpp
3 --------------------------------------
4 Date : December 2022
5 Copyright : (C) 2022 by Denis Rouzaud
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
16#include "qgssettingstreenode.h"
17
18#include "qgsexception.h"
19#include "qgssettings.h"
21#include "qgssettingsproxy.h"
22
23#include <QDir>
24
25#include "moc_qgssettingstreenode.cpp"
26
28{
30 mParent->unregisterChildNode( this );
31
32 // do not use qDeleteAll
33 // the destructor of QgsSettingsTreeNode and QgsSettingsEntry
34 // will call unregister on the parent (see above)
35 // and will modify the containers at the same time
36 const auto nodes = mChildrenNodes;
37 for ( const auto *node : nodes )
38 delete node;
39 const auto settings = mChildrenSettings;
40 for ( const auto *setting : settings )
41 delete setting;
42}
43
45{
46 QgsSettingsTreeNode *te = new QgsSettingsTreeNode();
48 te->mKey = QString();
49 te->mCompleteKey = QStringLiteral( "/" );
50 return te;
51}
52
53QgsSettingsTreeNode *QgsSettingsTreeNode::createChildNode( const QString &key )
54{
55 QgsSettingsTreeNode *te = childNode( key );
56 if ( te )
57 return te;
58 if ( childSetting( key ) )
59 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
60
61 te = new QgsSettingsTreeNode();
63 te->init( this, key );
65 return te;
66}
67
69{
70 QgsSettingsTreeNode *nte = childNode( key );
71 if ( nte )
72 {
74 return dynamic_cast<QgsSettingsTreeNamedListNode *>( nte );
75 else
76 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child node with key '%2', but it is not a named list.." ).arg( this->key(), key ) );
77 }
78 if ( childSetting( key ) )
79 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
80
83 te->init( this, key );
84 te->initNamedList( options );
86 return te;
87}
88
89
90QgsSettingsTreeNode *QgsSettingsTreeNode::childNode( const QString &key ) const
91{
92 QList<QgsSettingsTreeNode *>::const_iterator it = mChildrenNodes.constBegin();
93 for ( ; it != mChildrenNodes.constEnd(); ++it )
94 {
95 if ( ( *it )->key() == key )
96 return *it;
97 }
98 return nullptr;
99}
100
102{
103 const QString testCompleteKey = QStringLiteral( "%1%2" ).arg( mCompleteKey, key );
104 QList<const QgsSettingsEntryBase *>::const_iterator it = mChildrenSettings.constBegin();
105 for ( ; it != mChildrenSettings.constEnd(); ++it )
106 {
107 if ( ( *it )->definitionKey() == testCompleteKey )
108 return *it;
109 }
110 return nullptr;
111}
112
114{
115 if ( childNode( key ) )
116 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child tree node with key '%2'." ).arg( this->key(), key ) );
117 if ( childSetting( key ) )
118 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
119
120 mChildrenSettings.append( setting );
121}
122
123
124void QgsSettingsTreeNode::registerChildNode( QgsSettingsTreeNode *node )
125{
126 mChildrenNodes.append( node );
127}
128
129void QgsSettingsTreeNode::unregisterChildSetting( const QgsSettingsEntryBase *setting, bool deleteSettingValues, const QStringList &parentsNamedItems )
130{
131 if ( deleteSettingValues )
132 setting->remove( parentsNamedItems );
133
134 mChildrenSettings.removeAll( setting );
135}
136
137void QgsSettingsTreeNode::unregisterChildNode( QgsSettingsTreeNode *node )
138{
139 mChildrenNodes.removeAll( node );
140}
141
142void QgsSettingsTreeNode::init( QgsSettingsTreeNode *parent, const QString &key )
143{
144 mParent = parent;
145 mKey = key;
146 mCompleteKey = QDir::cleanPath( QStringLiteral( "%1/%2" ).arg( parent->completeKey(), key ) ) + '/';
147}
148
150
152{
153 mOptions = options;
155 {
156 // this must be done before completing the key
157 mSelectedItemSetting = std::make_unique<QgsSettingsEntryString>( QStringLiteral( "%1/selected" ).arg( mCompleteKey ), nullptr );
158 }
159
160 mNamedNodesCount = mParent->namedNodesCount() + 1;
161 mItemsCompleteKey = QStringLiteral( "%1items/" ).arg( mCompleteKey );
162 mCompleteKey.append( QStringLiteral( "items/%%1/" ).arg( mNamedNodesCount ) );
163}
164
169
170
171QStringList QgsSettingsTreeNamedListNode::items( const QStringList &parentsNamedItems ) const
172{
173 return items( Qgis::SettingsOrigin::Any, parentsNamedItems );
174}
175
176QStringList QgsSettingsTreeNamedListNode::items( Qgis::SettingsOrigin origin, const QStringList &parentsNamedItems ) const
177{
178 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
179 throw QgsSettingsException( QObject::tr( "The number of given parent named items (%1) for the node '%2' doesn't match with the number of named items in the key (%3)." ).arg( QString::number( parentsNamedItems.count() ), mCompleteKey, QString::number( namedNodesCount() ) ) );
180
181
182 const QString completeKeyParam = completeKeyWithNamedItems( mItemsCompleteKey, parentsNamedItems );
183 auto settings = QgsSettings::get();
184 settings->beginGroup( completeKeyParam );
185 const QStringList res = settings->childGroups( origin );
186 settings->endGroup();
187 return res;
188}
189
190void QgsSettingsTreeNamedListNode::setSelectedItem( const QString &item, const QStringList &parentsNamedItems )
191{
192 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
193 throw QgsSettingsException( QObject::tr( "The number of given parent named items (%1) for the node '%2' doesn't match with the number of named items in the key (%3)." ).arg( QString::number( parentsNamedItems.count() ), mCompleteKey, QString::number( namedNodesCount() ) ) );
195 throw QgsSettingsException( QObject::tr( "The named list node '%1' has no option to set the current selected entry." ).arg( mCompleteKey ) );
196
197 mSelectedItemSetting->setValue( item, parentsNamedItems );
198}
199
200QString QgsSettingsTreeNamedListNode::selectedItem( const QStringList &parentsNamedItems )
201{
202 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
203 throw QgsSettingsException( QObject::tr( "The number of given parent named items (%1) for the node '%2' doesn't match with the number of named items in the key (%3)." ).arg( QString::number( parentsNamedItems.count() ), mCompleteKey, QString::number( namedNodesCount() ) ) );
205 throw QgsSettingsException( QObject::tr( "The named list node '%1' has no option to set the current selected entry." ).arg( mCompleteKey ) );
206
207 return mSelectedItemSetting->value( parentsNamedItems );
208}
209
210void QgsSettingsTreeNamedListNode::deleteItem( const QString &item, const QStringList &parentsNamedItems )
211{
212 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
213 throw QgsSettingsException( QObject::tr( "The number of given parent named items (%1) doesn't match with the number of named items in the key (%2)." ).arg( parentsNamedItems.count(), namedNodesCount() ) );
214
215 QStringList args = parentsNamedItems;
216 args << item;
217 QString key = completeKeyWithNamedItems( mCompleteKey, args );
219}
220
221void QgsSettingsTreeNamedListNode::deleteAllItems( const QStringList &parentsNamedItems )
222{
223 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
224 throw QgsSettingsException( QObject::tr( "The number of given parent named items (%1) doesn't match with the number of named items in the key (%2)." ).arg( parentsNamedItems.count(), namedNodesCount() ) );
225
226 const QStringList children = items( parentsNamedItems );
227 auto settings = QgsSettings::get();
228 for ( const QString &child : children )
229 {
230 QStringList args = parentsNamedItems;
231 args << child;
232 QString key = completeKeyWithNamedItems( mCompleteKey, args );
233 settings->remove( key );
234 }
235}
236
237QString QgsSettingsTreeNamedListNode::completeKeyWithNamedItems( const QString &key, const QStringList &namedItems ) const
238{
239 switch ( namedItems.count() )
240 {
241 case 0:
242 return key;
243 case 1:
244 return key.arg( namedItems[0] );
245 case 2:
246 return key.arg( namedItems[0], namedItems[1] );
247 case 3:
248 return key.arg( namedItems[0], namedItems[1], namedItems[2] );
249 case 4:
250 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3] );
251 case 5:
252 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3], namedItems[4] );
253 default:
254 throw QgsSettingsException( QObject::tr( "Current implementation of QgsSettingsTreeNamedListNode::items doesn't handle more than 5 parent named items" ) );
255 break;
256 }
257}
@ Standard
Normal Node.
Definition qgis.h:656
@ NamedList
Named List Node.
Definition qgis.h:657
QFlags< SettingsTreeNodeOption > SettingsTreeNodeOptions
Definition qgis.h:671
@ NamedListSelectedItemSetting
Creates a setting to store which is the current item.
Definition qgis.h:667
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4465
@ Any
From any origin.
Definition qgis.h:4466
Represents a settings entry and provides methods for reading and writing settings values.
void remove(const QString &dynamicKeyPart=QString()) const
Removes the settings from the underlying QSettings.
Custom exception class for settings related exceptions.
void deleteAllItems(const QStringList &parentsNamedItems=QStringList())
Deletes all items from the named list node.
void deleteItem(const QString &item, const QStringList &parentsNamedItems=QStringList())
Deletes a named item from the named list node.
void initNamedList(const Qgis::SettingsTreeNodeOptions &options)
Init the nodes with the specific options.
QString selectedItem(const QStringList &parentsNamedItems=QStringList())
Returns the selected named item from the named list node.
void setSelectedItem(const QString &item, const QStringList &parentsNamedItems=QStringList())
Sets the selected named item from the named list node.
QStringList items(const QStringList &parentsNamedItems=QStringList()) const
Returns the list of items.
A tree node for the settings tree to help organizing and introspecting the tree.
void registerChildNode(QgsSettingsTreeNode *node)
Registers a child nodes.
Qgis::SettingsTreeNodeType type() const
Returns the type of node.
QgsSettingsTreeNode * createChildNode(const QString &key)
Creates a normal tree node It will return the existing child node if it exists at the given key.
Qgis::SettingsTreeNodeType mType
QgsSettingsTreeNode * childNode(const QString &key) const
Returns the existing child node if it exists at the given key.
QString completeKey() const
Returns the complete key of the node (including its parents).
int namedNodesCount() const
Returns the number of named nodes in the complete key.
static QgsSettingsTreeNode * createRootNode()
Creates a tree root node.
QString key() const
Returns the key of the node (without its parents).
friend class QgsSettingsTreeNamedListNode
void unregisterChildSetting(const QgsSettingsEntryBase *setting, bool deleteSettingValues=false, const QStringList &parentsNamedItems=QStringList())
Unregisters the child setting.
QgsSettingsTreeNode * parent() const
Returns the parent of the node or nullptr if it does not exists.
void unregisterChildNode(QgsSettingsTreeNode *node)
Unregisters the child tree node.
void registerChildSetting(const QgsSettingsEntryBase *setting, const QString &key)
Registers a child setting.
const QgsSettingsEntryBase * childSetting(const QString &key) const
Returns the existing child settings if it exists at the given key.
QgsSettingsTreeNamedListNode * createNamedListNode(const QString &key, const Qgis::SettingsTreeNodeOptions &options=Qgis::SettingsTreeNodeOptions())
Creates a named list tree node.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
static QgsSettingsProxy get()
Returns a proxy for a QgsSettings object.