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