QGIS API Documentation 3.39.0-Master (bca3cdb6021)
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"
18#include "qgsexception.h"
19#include "qgssettings.h"
20#include "qgssettingsproxy.h"
21
22#include <QDir>
23
25{
27 mParent->unregisterChildNode( this );
28
29 // do not use qDeleteAll
30 // the destructor of QgsSettingsTreeNode and QgsSettingsEntry
31 // will call unregister on the parent (see above)
32 // and will modify the containers at the same time
33 const auto nodes = mChildrenNodes;
34 for ( const auto *node : nodes )
35 delete node;
36 const auto settings = mChildrenSettings;
37 for ( const auto *setting : settings )
38 delete setting;
39}
40
42{
45 te->mKey = QString();
46 te->mCompleteKey = QStringLiteral( "/" );
47 return te;
48}
49
51{
53 if ( te )
54 return te;
55 if ( childSetting( key ) )
56 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
57
58 te = new QgsSettingsTreeNode();
60 te->init( this, key );
62 return te;
63}
64
66{
68 if ( nte )
69 {
71 return dynamic_cast<QgsSettingsTreeNamedListNode *>( nte );
72 else
73 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 ) );
74 }
75 if ( childSetting( key ) )
76 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
77
80 te->init( this, key );
81 te->initNamedList( options );
83 return te;
84}
85
86
88{
89 QList<QgsSettingsTreeNode *>::const_iterator it = mChildrenNodes.constBegin();
90 for ( ; it != mChildrenNodes.constEnd(); ++it )
91 {
92 if ( ( *it )->key() == key )
93 return *it;
94 }
95 return nullptr;
96}
97
99{
100 const QString testCompleteKey = QStringLiteral( "%1%2" ).arg( mCompleteKey, key );
101 QList<const QgsSettingsEntryBase *>::const_iterator it = mChildrenSettings.constBegin();
102 for ( ; it != mChildrenSettings.constEnd(); ++it )
103 {
104 if ( ( *it )->definitionKey() == testCompleteKey )
105 return *it;
106 }
107 return nullptr;
108}
109
110void QgsSettingsTreeNode::registerChildSetting( const QgsSettingsEntryBase *setting, const QString &key )
111{
112 if ( childNode( key ) )
113 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child tree node with key '%2'." ).arg( this->key(), key ) );
114 if ( childSetting( key ) )
115 throw QgsSettingsException( QObject::tr( "Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->key(), key ) );
116
117 mChildrenSettings.append( setting );
118}
119
120
122{
123 mChildrenNodes.append( node );
124}
125
126void QgsSettingsTreeNode::unregisterChildSetting( const QgsSettingsEntryBase *setting, bool deleteSettingValues, const QStringList &parentsNamedItems )
127{
128 if ( deleteSettingValues )
129 setting->remove( parentsNamedItems );
130
131 mChildrenSettings.removeAll( setting );
132}
133
135{
136 mChildrenNodes.removeAll( node );
137}
138
139void QgsSettingsTreeNode::init( QgsSettingsTreeNode *parent, const QString &key )
140{
141 mParent = parent;
142 mKey = key;
143 mCompleteKey = QDir::cleanPath( QStringLiteral( "%1/%2" ).arg( parent->completeKey(), key ) ) + '/';
144}
145
146
148{
149 mOptions = options;
151 {
152 // this must be done before completing the key
153 mSelectedItemSetting = new QgsSettingsEntryString( QStringLiteral( "%1/selected" ).arg( mCompleteKey ), nullptr );
154 }
155
156 mNamedNodesCount = mParent->namedNodesCount() + 1;
157 mItemsCompleteKey = QStringLiteral( "%1items/" ).arg( mCompleteKey );
158 mCompleteKey.append( QStringLiteral( "items/%%1/" ).arg( mNamedNodesCount ) );
159}
160
162{
163 delete mSelectedItemSetting;
164}
165
166
167QStringList QgsSettingsTreeNamedListNode::items( const QStringList &parentsNamedItems ) const
168{
169 return items( Qgis::SettingsOrigin::Any, parentsNamedItems );
170}
171
172QStringList QgsSettingsTreeNamedListNode::items( Qgis::SettingsOrigin origin, const QStringList &parentsNamedItems ) const
173{
174 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
175 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() ) ) );
176
177
178 const QString completeKeyParam = completeKeyWithNamedItems( mItemsCompleteKey, parentsNamedItems );
179 auto settings = QgsSettings::get();
180 settings->beginGroup( completeKeyParam );
181 const QStringList res = settings->childGroups( origin );
182 settings->endGroup();
183 return res;
184}
185
186void QgsSettingsTreeNamedListNode::setSelectedItem( const QString &item, 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 mSelectedItemSetting->setValue( item, parentsNamedItems );
194}
195
196QString QgsSettingsTreeNamedListNode::selectedItem( const QStringList &parentsNamedItems )
197{
198 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
199 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() ) ) );
201 throw QgsSettingsException( QObject::tr( "The named list node '%1' has no option to set the current selected entry." ).arg( mCompleteKey ) );
202
203 return mSelectedItemSetting->value( parentsNamedItems );
204}
205
206void QgsSettingsTreeNamedListNode::deleteItem( const QString &item, const QStringList &parentsNamedItems )
207{
208 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
209 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() ) );
210
211 QStringList args = parentsNamedItems;
212 args << item;
213 QString key = completeKeyWithNamedItems( mCompleteKey, args );
215}
216
217void QgsSettingsTreeNamedListNode::deleteAllItems( const QStringList &parentsNamedItems )
218{
219 if ( namedNodesCount() - 1 != parentsNamedItems.count() )
220 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() ) );
221
222 const QStringList children = items( parentsNamedItems );
223 auto settings = QgsSettings::get();
224 for ( const QString &child : children )
225 {
226 QStringList args = parentsNamedItems;
227 args << child;
228 QString key = completeKeyWithNamedItems( mCompleteKey, args );
229 settings->remove( key );
230 }
231}
232
233QString QgsSettingsTreeNamedListNode::completeKeyWithNamedItems( const QString &key, const QStringList &namedItems ) const
234{
235 switch ( namedItems.count() )
236 {
237 case 0:
238 return key;
239 case 1:
240 return key.arg( namedItems[0] );
241 case 2:
242 return key.arg( namedItems[0], namedItems[1] );
243 case 3:
244 return key.arg( namedItems[0], namedItems[1], namedItems[2] );
245 case 4:
246 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3] );
247 case 5:
248 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3], namedItems[4] );
249 default:
250 throw QgsSettingsException( QObject::tr( "Current implementation of QgsSettingsTreeNamedListNode::items doesn't handle more than 5 parent named items" ) );
251 break;
252 }
253}
@ 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:4140
@ Any
From any origin.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
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.
A string settings entry.
Custom exception class for settings related exceptions.
QgsSettingsTreeNamedListNode is a named list tree node for the settings tree to help organizing and i...
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.
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
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.