QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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"
18#include "qgsexception.h"
19#include "qgssettings.h"
20
21#include <QDir>
22
23
25{
27 mParent->unregisterChildNode( this );
28
29 qDeleteAll( mChildrenNodes );
30 qDeleteAll( mChildrenSettings );
31}
32
34{
37 te->mKey = QString();
38 te->mCompleteKey = QStringLiteral( "/" );
39 return te;
40}
41
43{
45 if ( te )
46 return te;
47 if ( childSetting( key ) )
48 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
49
50 te = new QgsSettingsTreeNode();
52 te->init( this, key );
54 return te;
55}
56
57QgsSettingsTreeNamedListNode *QgsSettingsTreeNode::createNamedListNode( const QString &key, const Qgis::SettingsTreeNodeOptions &options )
58{
60 if ( nte )
61 {
63 return dynamic_cast<QgsSettingsTreeNamedListNode *>( nte );
64 else
65 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 ) );
66 }
67 if ( childSetting( key ) )
68 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
69
72 te->init( this, key );
73 te->initNamedList( options );
75 return te;
76}
77
78
80{
81 QList<QgsSettingsTreeNode *>::const_iterator it = mChildrenNodes.constBegin();
82 for ( ; it != mChildrenNodes.constEnd(); ++it )
83 {
84 if ( ( *it )->key() == key )
85 return *it;
86 }
87 return nullptr;
88}
89
91{
92 const QString testCompleteKey = QStringLiteral( "%1%2" ).arg( mCompleteKey, key );
93 QList<const QgsSettingsEntryBase *>::const_iterator it = mChildrenSettings.constBegin();
94 for ( ; it != mChildrenSettings.constEnd(); ++it )
95 {
96 if ( ( *it )->definitionKey() == testCompleteKey )
97 return *it;
98 }
99 return nullptr;
100}
101
102void QgsSettingsTreeNode::registerChildSetting( const QgsSettingsEntryBase *setting, const QString &key )
103{
104 if ( childNode( key ) )
105 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child tree node with key '%2'." ).arg( this->key(), key ) );
106 if ( childSetting( key ) )
107 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
108
109 mChildrenSettings.append( setting );
110}
111
112
114{
115 mChildrenNodes.append( node );
116}
117
118void QgsSettingsTreeNode::unregisterChildSetting( const QgsSettingsEntryBase *setting, bool deleteSettingValues, const QStringList &parentsNamedItems )
119{
120 if ( deleteSettingValues )
121 setting->remove( parentsNamedItems );
122
123 mChildrenSettings.removeAll( setting );
124}
125
127{
128 mChildrenNodes.removeAll( node );
129}
130
131void QgsSettingsTreeNode::init( QgsSettingsTreeNode *parent, const QString &key )
132{
133 mParent = parent;
134 mKey = key;
135 mCompleteKey = QDir::cleanPath( QStringLiteral( "%1/%2" ).arg( parent->completeKey(), key ) ) + '/';
136}
137
138
139void QgsSettingsTreeNamedListNode::initNamedList( const Qgis::SettingsTreeNodeOptions &options )
140{
141 mOptions = options;
143 {
144 // this must be done before completing the key
145 mSelectedItemSetting = new QgsSettingsEntryString( QStringLiteral( "%1/selected" ).arg( mCompleteKey ), nullptr );
146 }
147
148 mNamedNodesCount = mParent->namedNodesCount() + 1;
149 mItemsCompleteKey = QStringLiteral( "%1items/" ).arg( mCompleteKey );
150 mCompleteKey.append( QStringLiteral( "items/%%1/" ).arg( mNamedNodesCount ) );
151}
152
154{
155 delete mSelectedItemSetting;
156}
157
158
159QStringList QgsSettingsTreeNamedListNode::items( const QStringList &parentsNamedItems ) const
160{
161 return items( Qgis::SettingsOrigin::Any, parentsNamedItems );
162}
163
164QStringList QgsSettingsTreeNamedListNode::items( Qgis::SettingsOrigin origin, const QStringList &parentsNamedItems ) const
165{
166 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
167 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() ) ) );
168
169
170 const QString completeKeyParam = completeKeyWithNamedItems( mItemsCompleteKey, parentsNamedItems );
171 QgsSettings settings;
172 settings.beginGroup( completeKeyParam );
173 return settings.childGroups( origin );
174}
175
176void QgsSettingsTreeNamedListNode::setSelectedItem( const QString &item, const QStringList &parentsNamedItems )
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() ) ) );
181 throw QgsSettingsException( QObject::tr( "The named list node '%1' has no option to set the current selected entry." ).arg( mCompleteKey ) );
182
183 mSelectedItemSetting->setValue( item, parentsNamedItems );
184}
185
186QString QgsSettingsTreeNamedListNode::selectedItem( const QStringList &parentsNamedItems )
187{
188 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
189 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() ) ) );
191 throw QgsSettingsException( QObject::tr( "The named list node '%1' has no option to set the current selected entry." ).arg( mCompleteKey ) );
192
193 return mSelectedItemSetting->value( parentsNamedItems );
194}
195
196void QgsSettingsTreeNamedListNode::deleteItem( const QString &item, const QStringList &parentsNamedItems )
197{
198 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
199 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() ) );
200
201 QStringList args = parentsNamedItems;
202 args << item;
203 QString key = completeKeyWithNamedItems( mCompleteKey, args );
205}
206
207QString QgsSettingsTreeNamedListNode::completeKeyWithNamedItems( const QString &key, const QStringList &namedItems ) const
208{
209 switch ( namedItems.count() )
210 {
211 case 0:
212 return key;
213 case 1:
214 return key.arg( namedItems[0] );
215 case 2:
216 return key.arg( namedItems[0], namedItems[1] );
217 case 3:
218 return key.arg( namedItems[0], namedItems[1], namedItems[2] );
219 case 4:
220 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3] );
221 case 5:
222 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3], namedItems[4] );
223 default:
224 throw QgsSettingsException( QObject::tr( "Current implementation of QgsSettingsTreeNamedListNode::items doesn't handle more than 5 parent named items" ) );
225 break;
226 }
227}
@ NamedListSelectedItemSetting
Creates a setting to store which is the current item.
SettingsOrigin
The setting origin describes where a setting is stored.
Definition: qgis.h:2702
@ Any
From any origin.
Represent 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.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
A string settings entry.
Custom exception class for settings related exceptions.
Definition: qgsexception.h:134
QgsSettingsTreeNamedListNode is a named list tree node for the settings tree to help organizing and i...
QString selectedItem(const QStringList &parentsNamedItems=QStringList()) SIP_THROW(QgsSettingsException)
Returns the selected named item from the named list node.
QStringList items(const QStringList &parentsNamedItems=QStringList()) const SIP_THROW(QgsSettingsException)
Returns the list of items.
void initNamedList(const Qgis::SettingsTreeNodeOptions &options)
Init the nodes with the specific options.
void deleteItem(const QString &item, const QStringList &parentsNamedItems=QStringList()) SIP_THROW(QgsSettingsException)
Deletes a named item from the named list node.
void setSelectedItem(const QString &item, const QStringList &parentsNamedItems=QStringList()) SIP_THROW(QgsSettingsException)
Sets the selected named item from the named list node.
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
void registerChildNode(QgsSettingsTreeNode *node)
Registers a child nodes.
QgsSettingsTreeNode * createChildNode(const QString &key) SIP_THROW(QgsSettingsException)
Creates a normal tree node It will return the existing child node if it exists at the given key.
QgsSettingsTreeNamedListNode * createNamedListNode(const QString &key, const Qgis::SettingsTreeNodeOptions &options=Qgis::SettingsTreeNodeOptions()) SIP_THROW(QgsSettingsException)
Creates a named list tree node.
Qgis::SettingsTreeNodeType type() const
Returns the type of node.
const QgsSettingsEntryBase * childSetting(const QString &key)
Returns the existing child settings if it exists at the given key.
Qgis::SettingsTreeNodeType mType
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.
void registerChildSetting(const QgsSettingsEntryBase *setting, const QString &key) SIP_THROW(QgsSettingsException)
Registers a child setting.
QgsSettingsTreeNode * childNode(const QString &key)
Returns the existing child node if it exists at the given key.
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.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:63
QStringList childGroups(Qgis::SettingsOrigin origin=Qgis::SettingsOrigin::Any) const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:89
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.