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