QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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
17#include "qgslogger.h"
18#include "qgssettings.h"
19#include "qgssettingsproxy.h"
20
25
26
27bool QgsSettingsEntryString::checkValuePrivate( const QString &value ) const
28{
29 if ( value.length() < mMinLength )
30 {
31 QgsDebugError( QStringLiteral( "Can't set value for settings. String length '%1' is shorter than minimum length '%2'." )
32 .arg( value.length() )
33 .arg( mMinLength ) );
34 return false;
35 }
36
37 if ( mMaxLength >= 0
38 && value.length() > mMaxLength )
39 {
40 QgsDebugError( QStringLiteral( "Can't set value for settings. String length '%1' is longer than maximum length '%2'." )
41 .arg( value.length() )
42 .arg( mMinLength ) );
43 return false;
44 }
45
46 return true;
47}
48
49QString QgsSettingsEntryString::convertFromVariant( const QVariant &value ) const
50{
51 return value.toString();
52}
53
58
60{
61 return mMinLength;
62}
63
65{
66 return mMaxLength;
67}
68
69QStringList QgsSettingsEntryStringList::convertFromVariant( const QVariant &value ) const
70{
71 return value.toStringList();
72}
73
78
79
80bool QgsSettingsEntryBool::convertFromVariant( const QVariant &value ) const
81{
82 return value.toBool();
83}
84
85
90
91
92bool QgsSettingsEntryInteger::checkValuePrivate( const int &value ) const
93{
94 if ( value < mMinValue )
95 {
96 QgsDebugError( QObject::tr( "Can't set value for setting. Value '%1' is less than minimum value '%2'." )
97 .arg( QString::number( value ) )
98 .arg( QString::number( mMinValue ) ) );
99 return false;
100 }
101
102 if ( value > mMaxValue )
103 {
104 QgsDebugError( QObject::tr( "Can't set value for setting. Value '%1' is greater than maximum value '%2'." )
105 .arg( QString::number( value ) )
106 .arg( QString::number( mMaxValue ) ) );
107 return false;
108 }
109
110 return true;
111}
112
113int QgsSettingsEntryInteger::convertFromVariant( const QVariant &value ) const
114{
115 return value.toLongLong();
116}
117
122
124{
125 return mMaxValue;
126}
127
129{
130 return mMaxValue;
131}
132
133bool QgsSettingsEntryInteger64::checkValuePrivate( const qlonglong &value ) const
134{
135 if ( value < mMinValue )
136 {
137 QgsDebugError( QObject::tr( "Can't set value for setting. Value '%1' is less than minimum value '%2'." )
138 .arg( QString::number( value ) )
139 .arg( QString::number( mMinValue ) ) );
140 return false;
141 }
142
143 if ( value > mMaxValue )
144 {
145 QgsDebugError( QObject::tr( "Can't set value for setting. Value '%1' is greater than maximum value '%2'." )
146 .arg( QString::number( value ) )
147 .arg( QString::number( mMaxValue ) ) );
148 return false;
149 }
150
151 return true;
152}
153
154qlonglong QgsSettingsEntryInteger64::convertFromVariant( const QVariant &value ) const
155{
156 return value.toLongLong();
157}
158
163
165{
166 return mMaxValue;
167}
168
170{
171 return mMaxValue;
172}
173
174
175
176bool QgsSettingsEntryDouble::checkValuePrivate( const double &value ) const
177{
178 if ( value < mMinValue )
179 {
180 QgsDebugError( QObject::tr( "Can't set value for setting. Value '%1' is less than minimum value '%2'." )
181 .arg( QString::number( value ), QString::number( mMinValue ) ) );
182 return false;
183 }
184
185 if ( value > mMaxValue )
186 {
187 QgsDebugError( QObject::tr( "Can't set value for setting. Value '%1' is greater than maximum value '%2'." )
188 .arg( QString::number( value ), QString::number( mMaxValue ) ) );
189 return false;
190 }
191
192 return true;
193}
194
195double QgsSettingsEntryDouble::convertFromVariant( const QVariant &value ) const
196{
197 return value.toDouble();
198}
199
204
206{
207 return mMinValue;
208}
209
211{
212 return mMaxValue;
213}
214
216{
217 mDisplayHintDecimals = displayHintDecimals;
218}
219
221{
222 return mDisplayHintDecimals;
223}
224
225QColor QgsSettingsEntryColor::convertFromVariant( const QVariant &value ) const
226{
227 return value.value<QColor>();
228}
229
234
235bool QgsSettingsEntryColor::checkValuePrivate( const QColor &value ) const
236{
237 if ( !mAllowAlpha && value.alpha() != 255 )
238 {
239 QgsDebugError( QStringLiteral( "Setting %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 auto settings = QgsSettings::get();
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 if ( value() != oldValue )
266 setVariantValue( oldValue );
267 return true;
268 }
269 return false;
270}
271
272void QgsSettingsEntryColor::copyValueToKeys( const QString &redKey, const QString &greenKey, const QString &blueKey, const QString &alphaKey ) const
273{
274 auto settings = QgsSettings::get();
275 const QColor color = value();
276 settings->setValue( redKey, color.red() );
277 settings->setValue( greenKey, color.green() );
278 settings->setValue( blueKey, color.blue() );
279 if ( !alphaKey.isNull() )
280 settings->setValue( alphaKey, color.alpha() );
281}
282
283QVariantMap QgsSettingsEntryVariantMap::convertFromVariant( const QVariant &value ) const
284{
285 return value.value<QVariantMap>();
286}
287
292
SettingsType
Types of settings entries.
Definition qgis.h:599
@ Variant
Generic variant.
@ StringList
List of strings.
@ VariantMap
Map of strings.
@ Double
Double precision number.
QString value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
bool setVariantValue(const QVariant &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
QString definitionKey() const
Returns settings entry defining key.
bool convertFromVariant(const QVariant &value) const override
Converts the variant value to the value type of the setting.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
QColor convertFromVariant(const QVariant &value) const override
Converts the variant value to the value type of the setting.
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 convertFromVariant(const QVariant &value) const override
Converts the variant value to the value type of the setting.
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.
qlonglong convertFromVariant(const QVariant &value) const override
Converts the variant value to the value type of the setting.
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.
int convertFromVariant(const QVariant &value) const override
Converts the variant value to the value type of the setting.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
QStringList convertFromVariant(const QVariant &value) const override
Converts the variant value to the value type of the setting.
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.
QString convertFromVariant(const QVariant &value) const override
Converts the variant value to the value type of the setting.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
QVariantMap convertFromVariant(const QVariant &value) const override
Converts the variant value to the value type of the setting.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
static QgsSettingsProxy get()
Returns a proxy for a QgsSettings object.
#define QgsDebugError(str)
Definition qgslogger.h:38