QGIS API Documentation 3.43.0-Master (58029bba303)
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
6 Email : denis@opengis.ch
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#include "moc_qgssettingstreenode.cpp"
19#include "qgsexception.h"
20#include "qgssettings.h"
21#include "qgssettingsproxy.h"
22
23#include <QDir>
24
26{
28 mParent->unregisterChildNode( this );
29
30 // do not use qDeleteAll
31 // the destructor of QgsSettingsTreeNode and QgsSettingsEntry
32 // will call unregister on the parent (see above)
33 // and will modify the containers at the same time
34 const auto nodes = mChildrenNodes;
35 for ( const auto *node : nodes )
36 delete node;
37 const auto settings = mChildrenSettings;
38 for ( const auto *setting : settings )
39 delete setting;
40}
41
43{
46 te->mKey = QString();
47 te->mCompleteKey = QStringLiteral( "/" );
48 return te;
49}
50
52{
54 if ( te )
55 return te;
56 if ( childSetting( key ) )
57 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
58
59 te = new QgsSettingsTreeNode();
61 te->init( this, key );
63 return te;
64}
65
67{
69 if ( nte )
70 {
72 return dynamic_cast<QgsSettingsTreeNamedListNode *>( nte );
73 else
74 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 ) );
75 }
76 if ( childSetting( key ) )
77 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
78
81 te->init( this, key );
82 te->initNamedList( options );
84 return te;
85}
86
87
89{
90 QList<QgsSettingsTreeNode *>::const_iterator it = mChildrenNodes.constBegin();
91 for ( ; it != mChildrenNodes.constEnd(); ++it )
92 {
93 if ( ( *it )->key() == key )
94 return *it;
95 }
96 return nullptr;
97}
98
100{
101 const QString testCompleteKey = QStringLiteral( "%1%2" ).arg( mCompleteKey, key );
102 QList<const QgsSettingsEntryBase *>::const_iterator it = mChildrenSettings.constBegin();
103 for ( ; it != mChildrenSettings.constEnd(); ++it )
104 {
105 if ( ( *it )->definitionKey() == testCompleteKey )
106 return *it;
107 }
108 return nullptr;
109}
110
111void QgsSettingsTreeNode::registerChildSetting( const QgsSettingsEntryBase *setting, const QString &key )
112{
113 if ( childNode( key ) )
114 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child tree node with key '%2'." ).arg( this->key(), key ) );
115 if ( childSetting( key ) )
116 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
117
118 mChildrenSettings.append( setting );
119}
120
121
123{
124 mChildrenNodes.append( node );
125}
126
127void QgsSettingsTreeNode::unregisterChildSetting( const QgsSettingsEntryBase *setting, bool deleteSettingValues, const QStringList &parentsNamedItems )
128{
129 if ( deleteSettingValues )
130 setting->remove( parentsNamedItems );
131
132 mChildrenSettings.removeAll( setting );
133}
134
136{
137 mChildrenNodes.removeAll( node );
138}
139
140void QgsSettingsTreeNode::init( QgsSettingsTreeNode *parent, const QString &key )
141{
142 mParent = parent;
143 mKey = key;
144 mCompleteKey = QDir::cleanPath( QStringLiteral( "%1/%2" ).arg( parent->completeKey(), key ) ) + '/';
145}
146
147QgsSettingsTreeNamedListNode::QgsSettingsTreeNamedListNode() = default;
148
150{
151 mOptions = options;
153 {
154 // this must be done before completing the key
155 mSelectedItemSetting = std::make_unique<QgsSettingsEntryString>( QStringLiteral( "%1/selected" ).arg( mCompleteKey ), nullptr );
156 }
157
158 mNamedNodesCount = mParent->namedNodesCount() + 1;
159 mItemsCompleteKey = QStringLiteral( "%1items/" ).arg( mCompleteKey );
160 mCompleteKey.append( QStringLiteral( "items/%%1/" ).arg( mNamedNodesCount ) );
161}
162
167
168
169QStringList QgsSettingsTreeNamedListNode::items( const QStringList &parentsNamedItems ) const
170{
171 return items( Qgis::SettingsOrigin::Any, parentsNamedItems );
172}
173
174QStringList QgsSettingsTreeNamedListNode::items( Qgis::SettingsOrigin origin, const QStringList &parentsNamedItems ) const
175{
176 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
177 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() ) ) );
178
179
180 const QString completeKeyParam = completeKeyWithNamedItems( mItemsCompleteKey, parentsNamedItems );
181 auto settings = QgsSettings::get();
182 settings->beginGroup( completeKeyParam );
183 const QStringList res = settings->childGroups( origin );
184 settings->endGroup();
185 return res;
186}
187
188void QgsSettingsTreeNamedListNode::setSelectedItem( const QString &item, const QStringList &parentsNamedItems )
189{
190 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
191 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() ) ) );
193 throw QgsSettingsException( QObject::tr( "The named list node '%1' has no option to set the current selected entry." ).arg( mCompleteKey ) );
194
195 mSelectedItemSetting->setValue( item, parentsNamedItems );
196}
197
198QString QgsSettingsTreeNamedListNode::selectedItem( const QStringList &parentsNamedItems )
199{
200 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
201 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() ) ) );
203 throw QgsSettingsException( QObject::tr( "The named list node '%1' has no option to set the current selected entry." ).arg( mCompleteKey ) );
204
205 return mSelectedItemSetting->value( parentsNamedItems );
206}
207
208void QgsSettingsTreeNamedListNode::deleteItem( const QString &item, const QStringList &parentsNamedItems )
209{
210 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
211 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() ) );
212
213 QStringList args = parentsNamedItems;
214 args << item;
215 QString key = completeKeyWithNamedItems( mCompleteKey, args );
217}
218
219void QgsSettingsTreeNamedListNode::deleteAllItems( const QStringList &parentsNamedItems )
220{
221 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
222 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() ) );
223
224 const QStringList children = items( parentsNamedItems );
225 auto settings = QgsSettings::get();
226 for ( const QString &child : children )
227 {
228 QStringList args = parentsNamedItems;
229 args << child;
230 QString key = completeKeyWithNamedItems( mCompleteKey, args );
231 settings->remove( key );
232 }
233}
234
235QString QgsSettingsTreeNamedListNode::completeKeyWithNamedItems( const QString &key, const QStringList &namedItems ) const
236{
237 switch ( namedItems.count() )
238 {
239 case 0:
240 return key;
241 case 1:
242 return key.arg( namedItems[0] );
243 case 2:
244 return key.arg( namedItems[0], namedItems[1] );
245 case 3:
246 return key.arg( namedItems[0], namedItems[1], namedItems[2] );
247 case 4:
248 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3] );
249 case 5:
250 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3], namedItems[4] );
251 default:
252 throw QgsSettingsException( QObject::tr( "Current implementation of QgsSettingsTreeNamedListNode::items doesn't handle more than 5 parent named items" ) );
253 break;
254 }
255}
@ NamedList
Named List Node.
QFlags< SettingsTreeNodeOption > SettingsTreeNodeOptions
Definition qgis.h:635
@ NamedListSelectedItemSetting
Creates a setting to store which is the current item.
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4315
@ Any
From any origin.
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.
A named list tree node for the settings tree to help organizing and introspecting the tree.
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.