QGIS API Documentation 3.29.0-Master (19d7edcfed)
qgssettingsentryimpl.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssettingsentryimpl.cpp
3 --------------------------------------
4 Date : February 2022
5 Copyright : (C) 2022 by Denis Rouzaud
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
21{
23}
24
25
26bool QgsSettingsEntryString::checkValue( const QString &value ) const
27{
28 if ( value.length() < mMinLength )
29 {
30 QgsDebugMsg( QObject::tr( "Can't set value for settings. String length '%1' is shorter than minimum length '%2'." )
31 .arg( value.length() )
32 .arg( mMinLength ) );
33 return false;
34 }
35
36 if ( mMaxLength >= 0
37 && value.length() > mMaxLength )
38 {
39 QgsDebugMsg( QObject::tr( "Can't set value for settings. String length '%1' is longer than maximum length '%2'." )
40 .arg( value.length() )
41 .arg( mMinLength ) );
42 return false;
43 }
44
45 return true;
46}
47
48QString QgsSettingsEntryString::convertFromVariant( const QVariant &value ) const
49{
50 return value.toString();
51}
52
54{
56}
57
59{
60 return mMinLength;
61}
62
64{
65 return mMaxLength;
66}
67
68QStringList QgsSettingsEntryStringList::convertFromVariant( const QVariant &value ) const
69{
70 return value.toStringList();
71}
72
74{
76}
77
78
79bool QgsSettingsEntryBool::convertFromVariant( const QVariant &value ) const
80{
81 return value.toBool();
82}
83
84
86{
88}
89
90
91bool QgsSettingsEntryInteger::checkValue( int value ) const
92{
93 if ( value < mMinValue )
94 {
95 QgsDebugMsg( QObject::tr( "Can't set value for setting. Value '%1' is less than minimum value '%2'." )
96 .arg( QString::number( value ) )
97 .arg( QString::number( mMinValue ) ) );
98 return false;
99 }
100
101 if ( value > mMaxValue )
102 {
103 QgsDebugMsg( QObject::tr( "Can't set value for setting. Value '%1' is greather than maximum value '%2'." )
104 .arg( QString::number( value ) )
105 .arg( QString::number( mMaxValue ) ) );
106 return false;
107 }
108
109 return true;
110}
111
112int QgsSettingsEntryInteger::convertFromVariant( const QVariant &value ) const
113{
114 return value.toLongLong();
115}
116
118{
120}
121
123{
124 return mMaxValue;
125}
126
128{
129 return mMaxValue;
130}
131
132bool QgsSettingsEntryInteger64::checkValue( qlonglong value ) const
133{
134 if ( value < mMinValue )
135 {
136 QgsDebugMsg( QObject::tr( "Can't set value for setting. Value '%1' is less than minimum value '%2'." )
137 .arg( QString::number( value ) )
138 .arg( QString::number( mMinValue ) ) );
139 return false;
140 }
141
142 if ( value > mMaxValue )
143 {
144 QgsDebugMsg( QObject::tr( "Can't set value for setting. Value '%1' is greather than maximum value '%2'." )
145 .arg( QString::number( value ) )
146 .arg( QString::number( mMaxValue ) ) );
147 return false;
148 }
149
150 return true;
151}
152
153qlonglong QgsSettingsEntryInteger64::convertFromVariant( const QVariant &value ) const
154{
155 return value.toLongLong();
156}
157
159{
161}
162
164{
165 return mMaxValue;
166}
167
169{
170 return mMaxValue;
171}
172
173
174
175bool QgsSettingsEntryDouble::checkValue( double value ) const
176{
177 if ( value < mMinValue )
178 {
179 QgsDebugMsg( QObject::tr( "Can't set value for setting. Value '%1' is less than minimum value '%2'." )
180 .arg( QString::number( value ), QString::number( mMinValue ) ) );
181 return false;
182 }
183
184 if ( value > mMaxValue )
185 {
186 QgsDebugMsg( QObject::tr( "Can't set value for setting. Value '%1' is greather than maximum value '%2'." )
187 .arg( QString::number( value ), QString::number( mMaxValue ) ) );
188 return false;
189 }
190
191 return true;
192}
193
194double QgsSettingsEntryDouble::convertFromVariant( const QVariant &value ) const
195{
196 return value.toDouble();
197}
198
200{
202}
203
205{
206 return mMinValue;
207}
208
210{
211 return mMaxValue;
212}
213
215{
216 mDisplayHintDecimals = displayHintDecimals;
217}
218
220{
221 return mDisplayHintDecimals;
222}
223
224
225QColor QgsSettingsEntryColor::convertFromVariant( const QVariant &value ) const
226{
227 return value.value<QColor>();
228}
229
231{
233}
234
235bool QgsSettingsEntryColor::checkValue( const QColor &value ) const
236{
237 if ( !mAllowAlpha && value.alpha() != 255 )
238 {
239 QgsDebugMsg( QObject::tr( "Settings %1 doesn't allow transparency and the given color has transparency." ).arg( definitionKey() ) );
240 return false;
241 }
242
243 return true;
244}
245
246bool QgsSettingsEntryColor::copyValueFromKeys( const QString &redKey, const QString &greenKey, const QString &blueKey, const QString &alphaKey, bool removeSettingAtKey ) const
247{
248 QgsSettings settings;
249 if ( settings.contains( redKey ) && settings.contains( greenKey ) && settings.contains( blueKey ) && ( alphaKey.isNull() || settings.contains( alphaKey ) ) )
250 {
251 QVariant oldValue;
252 if ( alphaKey.isNull() )
253 oldValue = QColor( settings.value( redKey ).toInt(), settings.value( greenKey ).toInt(), settings.value( blueKey ).toInt() );
254 else
255 oldValue = QColor( settings.value( redKey ).toInt(), settings.value( greenKey ).toInt(), settings.value( blueKey ).toInt(), settings.value( alphaKey ).toInt() );
256
257 if ( removeSettingAtKey )
258 {
259 settings.remove( redKey );
260 settings.remove( greenKey );
261 settings.remove( blueKey );
262 settings.remove( alphaKey );
263 }
264
265 setVariantValuePrivate( oldValue );
266 return true;
267 }
268 return false;
269}
270
271void QgsSettingsEntryColor::copyValueToKeys( const QString &redKey, const QString &greenKey, const QString &blueKey, const QString &alphaKey ) const
272{
273 QgsSettings settings;
274 const QColor color = value();
275 settings.setValue( redKey, color.red() );
276 settings.setValue( greenKey, color.green() );
277 settings.setValue( blueKey, color.blue() );
278 if ( !alphaKey.isNull() )
279 settings.setValue( alphaKey, color.alpha() );
280}
281
282QVariantMap QgsSettingsEntryVariantMap::convertFromVariant( const QVariant &value ) const
283{
284 return value.value<QVariantMap>();
285}
286
288{
290}
291
SettingsType
Types of settings entries.
Definition: qgis.h:231
@ Variant
Generic variant.
@ StringList
List of strings.
@ VariantMap
Map of strings.
@ Double
Double precision number.
QString definitionKey() const
Returns settings entry defining key.
bool setVariantValuePrivate(const QVariant &value, const QStringList &dynamicKeyPartList=QStringList()) const
Sets the settings value with a variant value.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
QString value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
bool value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
bool copyValueFromKeys(const QString &redKey, const QString &greenKey, const QString &blueKey, const QString &alphaKey=QString(), bool removeSettingAtKey=false) const
Copies the value from the given keys if they exist.
void copyValueToKeys(const QString &redKey, const QString &greenKey, const QString &blueKey, const QString &alphaKey=QString()) const
Copies the settings to the given keys.
void setDisplayHintDecimals(int displayHintDecimals)
Set the display hint decimals.
int displayHintDecimals() const
Returns how much decimals should be shown in the Gui.
double minValue() const
Returns the minimum value.
double maxValue() const
Returns the maximum value.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
qlonglong maxValue() const
Returns the maximum value.
qlonglong minValue() const
Returns the minimum value.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
int minValue() const
Returns the minimum value.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
int maxValue() const
Returns the maximum value.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
int maxLength() const
Returns the string maximum length.
int minLength() const
Returns the string minimum length.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:63
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38