QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgssettingsentrygroup.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssettingsentrygroup.cpp
3 --------------------------------------
4 Date : February 2021
5 Copyright : (C) 2021 by Damiano Lombardi
6 Email : damiano at opengis dot 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
18
19#include "qgslogger.h"
20#include "qgssettings.h"
21#include "qgssettingsentry.h"
22
23#include <QDir>
24#include <QRegularExpression>
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
29QgsSettingsEntryGroup::QgsSettingsEntryGroup( QList<const QgsSettingsEntryBase *> settings )
31{
32
33}
34
35QgsSettingsEntryGroup::QgsSettingsEntryGroup( QList<const QgsSettingsEntryBase *> settings, bool fatalErrorIfInvalid )
36 : mSettings( settings )
37{
38 for ( const auto *setting : std::as_const( mSettings ) )
39 {
40 QString otherBaseKey = setting->definitionKey();
41 otherBaseKey = otherBaseKey.left( otherBaseKey.lastIndexOf( '/'_L1 ) );
42 if ( mDefinitionBaseKey.isEmpty() )
43 {
44 mDefinitionBaseKey = otherBaseKey;
45 }
46 else
47 {
48 if ( mDefinitionBaseKey != otherBaseKey )
49 {
50 QgsDebugError( "Settings do not share the same base definition key for this group. This will lead to unpredictable results." );
51 if ( fatalErrorIfInvalid )
52 Q_ASSERT( false );
53 mIsValid = false;
54 }
55 }
56 }
57}
58
59QString QgsSettingsEntryGroup::baseKey( const QStringList &dynamicKeyPartList ) const
60{
61 QString key = mDefinitionBaseKey;
62
63 if ( dynamicKeyPartList.isEmpty() )
64 {
65 if ( hasDynamicKey() )
66 QgsDebugError( u"Settings group '%1' have a dynamic key but the dynamic key part was not provided"_s.arg( key ) );
67
68 return key;
69 }
70 else
71 {
72 if ( !hasDynamicKey() )
73 {
74 QgsDebugError( u"Settings group '%1' don't have a dynamic key, the provided dynamic key part will be ignored"_s.arg( key ) );
75 return key;
76 }
77
78 for ( int i = 0; i < dynamicKeyPartList.size(); i++ )
79 {
80 key.replace( u"%"_s.append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
81 }
82 }
83
84 return key;
85}
86
87void QgsSettingsEntryGroup::removeAllSettingsAtBaseKey( const QStringList &dynamicKeyPartList ) const
88{
89 QString key = baseKey( dynamicKeyPartList );
90 // https://regex101.com/r/kICr42/1
91 const thread_local QRegularExpression regularExpression( u"^(\\/?(qgis\\/?)?)?$"_s );
92 if ( key.contains( regularExpression ) )
93 {
94 QgsDebugError( u"Preventing mass removal of settings at key %1"_s.arg( key ) );
95 return;
96 }
97
99 settings.remove( key );
100}
101
102void QgsSettingsEntryGroup::removeAllChildrenSettings( const QString &dynamicKeyPart ) const
103{
105}
106
107void QgsSettingsEntryGroup::removeAllChildrenSettings( const QStringList &dynamicKeyPartList ) const
108{
109 for ( const auto *setting : mSettings )
110 setting->remove( dynamicKeyPartList );
111}
112
113bool QgsSettingsEntryGroup::hasDynamicKey() const
114{
115 const thread_local QRegularExpression regularExpression( u"%\\d+"_s );
116 return mDefinitionBaseKey.contains( regularExpression );
117}
static QStringList dynamicKeyPartToList(const QString &dynamicKeyPart)
Transforms a dynamic key part string to list.
void removeAllChildrenSettings(const QString &dynamicKeyPart=QString()) const
Removes all the settings from this group The dynamicKeyPart argument specifies the dynamic part of th...
const QList< const QgsSettingsEntryBase * > settings() const
Returns all the settings.
void removeAllSettingsAtBaseKey(const QStringList &dynamicKeyPartList=QStringList()) const
Removes all the settings at the base key for the given dynamicKeyPartList This means it might remove ...
QgsSettingsEntryGroup(QList< const QgsSettingsEntryBase * > settings)
Constructor.
QString baseKey(const QStringList &dynamicKeyPartList=QStringList()) const
Returns the base key for the given dynamicKeyPartList.
Stores settings for use within QGIS.
Definition qgssettings.h:68
#define QgsDebugError(str)
Definition qgslogger.h:59