25#include "moc_qgssettingstreenode.cpp"
27using namespace Qt::StringLiterals;
32 mParent->unregisterChildNode(
this );
38 const auto nodes = mChildrenNodes;
39 for (
const auto *node : nodes )
41 const auto settings = mChildrenSettings;
42 for (
const auto *setting : settings )
48 QgsSettingsTreeNode *te =
new QgsSettingsTreeNode();
51 te->mCompleteKey = u
"/"_s;
61 throw QgsSettingsException( QObject::tr(
"Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->
key(), key ) );
63 te =
new QgsSettingsTreeNode();
65 te->init(
this,
key );
78 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 ) );
81 throw QgsSettingsException( QObject::tr(
"Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->
key(), key ) );
85 te->init(
this,
key );
94 QList<QgsSettingsTreeNode *>::const_iterator it = mChildrenNodes.constBegin();
95 for ( ; it != mChildrenNodes.constEnd(); ++it )
97 if ( ( *it )->key() ==
key )
105 const QString testCompleteKey = u
"%1%2"_s.arg( mCompleteKey,
key );
106 QList<const QgsSettingsEntryBase *>::const_iterator it = mChildrenSettings.constBegin();
107 for ( ; it != mChildrenSettings.constEnd(); ++it )
109 if ( ( *it )->definitionKey() == testCompleteKey )
118 throw QgsSettingsException( QObject::tr(
"Settings tree node '%1' already holds a child tree node with key '%2'." ).arg( this->
key(), key ) );
120 throw QgsSettingsException( QObject::tr(
"Settings tree node '%1' already holds a child setting with key '%2'." ).arg( this->
key(), key ) );
122 mChildrenSettings.append( setting );
128 mChildrenNodes.append( node );
133 if ( deleteSettingValues )
134 setting->
remove( parentsNamedItems );
136 mChildrenSettings.removeAll( setting );
141 mChildrenNodes.removeAll( node );
159 mSelectedItemSetting = std::make_unique<QgsSettingsEntryString>( u
"%1/selected"_s.arg( mCompleteKey ),
nullptr );
162 mNamedNodesCount = mParent->namedNodesCount() + 1;
163 mItemsCompleteKey = u
"%1items/"_s.arg( mCompleteKey );
164 mCompleteKey.append( u
"items/%%1/"_s.arg( mNamedNodesCount ) );
182 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)." )
183 .arg( QString::number( parentsNamedItems.count() ), mCompleteKey, QString::number(
namedNodesCount() ) )
186 const QString completeKeyParam = completeKeyWithNamedItems( mItemsCompleteKey, parentsNamedItems );
189 settings.beginGroup( completeKeyParam );
190 const QStringList res = settings.childGroups();
199 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)." )
200 .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 ) );
205 mSelectedItemSetting->setValue( item, parentsNamedItems );
212 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)." )
213 .arg( QString::number( parentsNamedItems.count() ), mCompleteKey, QString::number(
namedNodesCount() ) )
216 throw QgsSettingsException( QObject::tr(
"The named list node '%1' has no option to set the current selected entry." ).arg( mCompleteKey ) );
218 return mSelectedItemSetting->value( parentsNamedItems );
225 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() )
228 QStringList args = parentsNamedItems;
230 QString
key = completeKeyWithNamedItems( mCompleteKey, args );
238 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() )
241 const QStringList children =
items( parentsNamedItems );
243 for (
const QString &child : children )
245 QStringList args = parentsNamedItems;
247 QString
key = completeKeyWithNamedItems( mCompleteKey, args );
248 settings.remove(
key );
252QString QgsSettingsTreeNamedListNode::completeKeyWithNamedItems(
const QString &key,
const QStringList &namedItems )
const
254 switch ( namedItems.count() )
259 return key.arg( namedItems[0] );
261 return key.arg( namedItems[0], namedItems[1] );
263 return key.arg( namedItems[0], namedItems[1], namedItems[2] );
265 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3] );
267 return key.arg( namedItems[0], namedItems[1], namedItems[2], namedItems[3], namedItems[4] );
269 throw QgsSettingsException( QObject::tr(
"Current implementation of QgsSettingsTreeNamedListNode::items doesn't handle more than 5 parent named items" ) );
@ NamedList
Named List Node.
QFlags< SettingsTreeNodeOption > SettingsTreeNodeOptions
@ NamedListSelectedItemSetting
Creates a setting to store which is the current item.
SettingsOrigin
The setting origin describes where a setting is stored.
Represents a settings entry and provides methods for reading and writing settings values.
static QSettings & userSettings()
Returns a reference to the thread-local QSettings instance used internally by all QgsSettingsEntry op...
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.
~QgsSettingsTreeNamedListNode() override
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.
virtual ~QgsSettingsTreeNode()
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.