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