QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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#include "qgssettings.h"
19#include "qgssettingsentry.h"
20#include "qgslogger.h"
21
22#include <QRegularExpression>
23#include <QDir>
24
25
26
27QgsSettingsEntryGroup::QgsSettingsEntryGroup( QList<const QgsSettingsEntryBase *> settings, bool fatalErrorIfInvalid )
28 : mSettings( settings )
29{
30 for ( const auto *setting : std::as_const( mSettings ) )
31 {
32 QString otherBaseKey = setting->definitionKey();
33 otherBaseKey = otherBaseKey.left( otherBaseKey.lastIndexOf( QLatin1Char( '/' ) ) );
34 if ( mDefinitionBaseKey.isEmpty() )
35 {
36 mDefinitionBaseKey = otherBaseKey;
37 }
38 else
39 {
40 if ( mDefinitionBaseKey != otherBaseKey )
41 {
42 QgsDebugMsg( "Settings do not share the same base definition key for this group. This will lead to unpredictable results." );
43 if ( fatalErrorIfInvalid )
44 Q_ASSERT( false );
45 mIsValid = false;
46 }
47 }
48 }
49}
50
51QString QgsSettingsEntryGroup::baseKey( const QStringList &dynamicKeyPartList ) const
52{
53 QString key = mDefinitionBaseKey;
54
55 if ( dynamicKeyPartList.isEmpty() )
56 {
57 if ( hasDynamicKey() )
58 QgsDebugMsg( QStringLiteral( "Settings group '%1' have a dynamic key but the dynamic key part was not provided" ).arg( key ) );
59
60 return key;
61 }
62 else
63 {
64 if ( !hasDynamicKey() )
65 {
66 QgsDebugMsg( QStringLiteral( "Settings group '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( key ) );
67 return key;
68 }
69
70 for ( int i = 0; i < dynamicKeyPartList.size(); i++ )
71 {
72 key.replace( QStringLiteral( "%" ).append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
73 }
74 }
75
76 return key;
77}
78
79void QgsSettingsEntryGroup::removeAllSettingsAtBaseKey( const QStringList &dynamicKeyPartList ) const
80{
81 QString key = baseKey( dynamicKeyPartList );
82 // https://regex101.com/r/kICr42/1
83 const thread_local QRegularExpression regularExpression( QStringLiteral( "^(\\/?(qgis\\/?)?)?$" ) );
84 if ( key.contains( regularExpression ) )
85 {
86 QgsDebugMsg( QStringLiteral( "Preventing mass removal of settings at key %1" ).arg( key ) );
87 return;
88 }
89
91 settings.remove( key );
92}
93
94void QgsSettingsEntryGroup::removeAllChildrenSettings( const QString &dynamicKeyPart ) const
95{
97}
98
99void QgsSettingsEntryGroup::removeAllChildrenSettings( const QStringList &dynamicKeyPartList ) const
100{
101 for ( const auto *setting : mSettings )
102 setting->remove( dynamicKeyPartList );
103}
104
105bool QgsSettingsEntryGroup::hasDynamicKey() const
106{
107 const thread_local QRegularExpression regularExpression( QStringLiteral( "%\\d+" ) );
108 return mDefinitionBaseKey.contains( regularExpression );
109}
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.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:63
#define QgsDebugMsg(str)
Definition: qgslogger.h:38