QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
settings
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
17
#include "
qgssettingsentrygroup.h
"
18
19
#include "
qgslogger.h
"
20
#include "
qgssettings.h
"
21
#include "
qgssettingsentry.h
"
22
23
#include <QDir>
24
#include <QRegularExpression>
25
#include <QString>
26
27
using namespace
Qt::StringLiterals;
28
29
QgsSettingsEntryGroup::QgsSettingsEntryGroup
( QList<const QgsSettingsEntryBase *>
settings
)
30
:
QgsSettingsEntryGroup
(
settings
, true )
31
{}
32
33
QgsSettingsEntryGroup::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
57
QString
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
85
void
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
96
QgsSettings
settings
;
97
settings
.remove( key );
98
}
99
100
void
QgsSettingsEntryGroup::removeAllChildrenSettings
(
const
QString &dynamicKeyPart )
const
101
{
102
removeAllChildrenSettings
(
QgsSettingsEntryBase::dynamicKeyPartToList
( dynamicKeyPart ) );
103
}
104
105
void
QgsSettingsEntryGroup::removeAllChildrenSettings
(
const
QStringList &dynamicKeyPartList )
const
106
{
107
for
(
const
auto
*setting : mSettings )
108
setting->remove( dynamicKeyPartList );
109
}
110
111
bool
QgsSettingsEntryGroup::hasDynamicKey()
const
112
{
113
const
thread_local
QRegularExpression regularExpression( u
"%\\d+"
_s );
114
return
mDefinitionBaseKey.contains( regularExpression );
115
}
QgsSettingsEntryBase::dynamicKeyPartToList
static QStringList dynamicKeyPartToList(const QString &dynamicKeyPart)
Transforms a dynamic key part string to list.
Definition
qgssettingsentry.cpp:218
QgsSettingsEntryGroup::removeAllChildrenSettings
void removeAllChildrenSettings(const QString &dynamicKeyPart=QString()) const
Removes all the settings from this group The dynamicKeyPart argument specifies the dynamic part of th...
Definition
qgssettingsentrygroup.cpp:100
QgsSettingsEntryGroup::settings
const QList< const QgsSettingsEntryBase * > settings() const
Returns all the settings.
Definition
qgssettingsentrygroup.h:67
QgsSettingsEntryGroup::removeAllSettingsAtBaseKey
void removeAllSettingsAtBaseKey(const QStringList &dynamicKeyPartList=QStringList()) const
Removes all the settings at the base key for the given dynamicKeyPartList This means it might remove ...
Definition
qgssettingsentrygroup.cpp:85
QgsSettingsEntryGroup::QgsSettingsEntryGroup
QgsSettingsEntryGroup(QList< const QgsSettingsEntryBase * > settings)
Constructor.
Definition
qgssettingsentrygroup.cpp:29
QgsSettingsEntryGroup::baseKey
QString baseKey(const QStringList &dynamicKeyPartList=QStringList()) const
Returns the base key for the given dynamicKeyPartList.
Definition
qgssettingsentrygroup.cpp:57
QgsSettings
Stores settings for use within QGIS.
Definition
qgssettings.h:68
qgslogger.h
QgsDebugError
#define QgsDebugError(str)
Definition
qgslogger.h:71
qgssettings.h
qgssettingsentry.h
qgssettingsentrygroup.h
Generated on
for QGIS API Documentation by
1.17.0