QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgssettingsentry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssettingsentry.h
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 #ifndef QGSSETTINGSENTRY_H
17 #define QGSSETTINGSENTRY_H
18 
19 #include <QString>
20 #include <QColor>
21 #include <limits>
22 
23 #include "qgis_core.h"
24 #include "qgis_sip.h"
25 #include "qgssettings.h"
26 
37 class CORE_EXPORT QgsSettingsEntryBase
38 {
39 
40 #ifdef SIP_RUN
42  if ( dynamic_cast< QgsSettingsEntryVariant * >( sipCpp ) )
43  sipType = sipType_QgsSettingsEntryVariant;
44  else if ( dynamic_cast< QgsSettingsEntryString * >( sipCpp ) )
45  sipType = sipType_QgsSettingsEntryString;
46  else if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) )
47  sipType = sipType_QgsSettingsEntryStringList;
48  else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) )
49  sipType = sipType_QgsSettingsEntryBool;
50  else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) )
51  sipType = sipType_QgsSettingsEntryInteger;
52  else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) )
53  sipType = sipType_QgsSettingsEntryDouble;
54  else if ( dynamic_cast< QgsSettingsEntryColor * >( sipCpp ) )
55  sipType = sipType_QgsSettingsEntryColor;
56  else
57  sipType = NULL;
58  SIP_END
59 #endif
60 
61  public:
62 
64  enum class SettingsType : int
65  {
66  Variant,
67  String,
68  StringList,
69  Bool,
70  Integer,
71  Double,
72  EnumFlag,
73  Color
74  };
75 
76 #ifndef SIP_RUN
77 
86  QgsSettingsEntryBase( const QString &key,
87  QgsSettings::Section section,
88  const QVariant &defaultValue = QVariant(),
89  const QString &description = QString() );
90 
91 #endif
92 
102  QgsSettingsEntryBase( const QString &key,
103  const QString &pluginName,
104  const QVariant &defaultValue = QVariant(),
105  const QString &description = QString() );
106 
110  virtual ~QgsSettingsEntryBase();
111 
117  QString key( const QString &dynamicKeyPart = QString() ) const;
118 
124  QString key( const QStringList &dynamicKeyPartList ) const;
125 
135  bool keyIsValid( const QString &key ) const;
136 
142  QString definitionKey() const;
143 
147  bool hasDynamicKey() const;
148 
154  bool exists( const QString &dynamicKeyPart = QString() ) const;
155 
161  bool exists( const QStringList &dynamicKeyPartList ) const;
162 
168  void remove( const QString &dynamicKeyPart = QString() ) const;
169 
175  void remove( const QStringList &dynamicKeyPartList ) const;
176 
180  QgsSettings::Section section() const;
181 
188  virtual bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;
189 
196  virtual bool setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const;
197 
205  QVariant valueAsVariant( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const;
206 
214  QVariant valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const;
215 
219  QVariant defaultValueAsVariant() const;
220 
224  virtual SettingsType settingsType() const = 0;
225 
229  QString description() const;
230 
231  private:
232 
233  QString mKey;
234  QVariant mDefaultValue;
235  QgsSettings::Section mSection;
236  QString mDescription;
237  QString mPluginName;
238 };
239 
240 
249 {
250  public:
251 
252 #ifndef SIP_RUN
253 
262  QgsSettingsEntryVariant( const QString &key,
263  QgsSettings::Section section,
264  const QVariant &defaultValue = QVariant(),
265  const QString &description = QString() );
266 
267 #endif
268 
278  QgsSettingsEntryVariant( const QString &key,
279  const QString &pluginName,
280  const QVariant &defaultValue = QVariant(),
281  const QString &description = QString() );
282 
289  bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;
290 
297  bool setValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const;
298 
306  QVariant value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const;
307 
315  QVariant value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const;
316 
320  QVariant defaultValue() const;
321 
322  virtual SettingsType settingsType() const override;
323 };
324 
325 
334 {
335  public:
336 
337 #ifndef SIP_RUN
338 
350  QgsSettingsEntryString( const QString &key,
351  QgsSettings::Section section,
352  const QString &defaultValue = QString(),
353  const QString &description = QString(),
354  int minLength = 0,
355  int maxLength = -1 );
356 
357 #endif
358 
368  QgsSettingsEntryString( const QString &key,
369  const QString &pluginName,
370  const QString &defaultValue = QString(),
371  const QString &description = QString() );
372 
379  bool setValue( const QString &value, const QString &dynamicKeyPart = QString() ) const;
380 
387  bool setValue( const QString &value, const QStringList &dynamicKeyPartList ) const;
388 
396  QString value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QString &defaultValueOverride = QString() ) const;
397 
405  QString value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QString &defaultValueOverride = QString() ) const;
406 
410  QString defaultValue() const;
411 
412  virtual SettingsType settingsType() const override;
413 
419  void setMinLength( int minLength );
420 
424  int minLength();
425 
431  void setMaxLength( int maxLength );
432 
436  int maxLength();
437 
438  private:
439 
440  int mMinLength;
441  int mMaxLength;
442 
443 };
444 
445 
454 {
455  public:
456 
457 #ifndef SIP_RUN
458 
467  QgsSettingsEntryStringList( const QString &key,
468  QgsSettings::Section section,
469  const QStringList &defaultValue = QStringList(),
470  const QString &description = QString() );
471 
472 #endif
473 
483  QgsSettingsEntryStringList( const QString &key,
484  const QString &pluginName,
485  const QStringList &defaultValue = QStringList(),
486  const QString &description = QString() );
487 
494  bool setValue( const QStringList &value, const QString &dynamicKeyPart = QString() ) const;
495 
502  bool setValue( const QStringList &value, const QStringList &dynamicKeyPartList ) const;
503 
511  QStringList value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QStringList &defaultValueOverride = QStringList() ) const;
512 
520  QStringList value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QStringList &defaultValueOverride = QStringList() ) const;
521 
525  QStringList defaultValue() const;
526 
527  virtual SettingsType settingsType() const override;
528 
529 };
530 
531 
539 class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase
540 {
541  public:
542 
543 #ifndef SIP_RUN
544 
553  QgsSettingsEntryBool( const QString &key,
554  QgsSettings::Section section,
555  bool defaultValue = false,
556  const QString &description = QString() );
557 
558 #endif
559 
569  QgsSettingsEntryBool( const QString &key,
570  const QString &pluginName,
571  bool defaultValue = false,
572  const QString &description = QString() );
573 
580  bool setValue( bool value, const QString &dynamicKeyPart = QString() ) const;
581 
588  bool setValue( bool value, const QStringList &dynamicKeyPartList ) const;
589 
597  bool value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, bool defaultValueOverride = false ) const;
598 
606  bool value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, bool defaultValueOverride = false ) const;
607 
611  bool defaultValue() const;
612 
613  virtual SettingsType settingsType() const override;
614 };
615 
616 
625 {
626  public:
627 
628 
629 #ifndef SIP_RUN
630 
641  QgsSettingsEntryInteger( const QString &key,
642  QgsSettings::Section section,
643  qlonglong defaultValue = 0,
644  const QString &description = QString(),
645  qlonglong minValue = std::numeric_limits<qlonglong>::min(),
646  qlonglong maxValue = std::numeric_limits<qlonglong>::max() );
647 
648 #endif
649 
659  QgsSettingsEntryInteger( const QString &key,
660  const QString &pluginName,
661  qlonglong defaultValue = 0,
662  const QString &description = QString() );
663 
670  bool setValue( qlonglong value, const QString &dynamicKeyPart = QString() ) const;
671 
678  bool setValue( qlonglong value, const QStringList &dynamicKeyPartList ) const;
679 
687  qlonglong value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, qlonglong defaultValueOverride = 0 ) const;
688 
696  qlonglong value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, qlonglong defaultValueOverride = 0 ) const;
697 
701  qlonglong defaultValue() const;
702 
703  virtual SettingsType settingsType() const override;
704 
710  void setMinValue( qlonglong minValue );
711 
715  qlonglong minValue();
716 
722  void setMaxValue( qlonglong maxValue );
723 
727  qlonglong maxValue();
728 
729  private:
730 
731  qlonglong mMinValue;
732  qlonglong mMaxValue;
733 
734 };
735 
736 
745 {
746  public:
747 
748 #ifndef SIP_RUN
749 
762  QgsSettingsEntryDouble( const QString &key,
763  QgsSettings::Section section,
764  double defaultValue = 0.0,
765  const QString &description = QString(),
766  double minValue = std::numeric_limits<double>::lowest(),
767  double maxValue = std::numeric_limits<double>::max(),
768  int displayDecimals = 1 );
769 
770 #endif
771 
781  QgsSettingsEntryDouble( const QString &key,
782  const QString &pluginName,
783  double defaultValue,
784  const QString &description = QString() );
785 
792  bool setValue( double value, const QString &dynamicKeyPart = QString() ) const;
793 
800  bool setValue( double value, const QStringList &dynamicKeyPartList ) const;
801 
809  double value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, double defaultValueOverride = 0.0 ) const;
810 
818  double value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, double defaultValueOverride = 0.0 ) const;
819 
823  double defaultValue() const;
824 
825  virtual SettingsType settingsType() const override;
826 
832  void setMinValue( double minValue );
833 
837  double minValue() const;
838 
844  void setMaxValue( double maxValue );
845 
849  double maxValue() const;
850 
856  void setDisplayHintDecimals( int displayHintDecimals );
857 
861  int displayHintDecimals() const;
862 
863  private:
864 
865  double mMinValue;
866  double mMaxValue;
867 
868  int mDisplayHintDecimals;
869 
870 };
871 
872 
882 template <typename T>
884 {
885  public:
886 
898  QgsSettingsEntryEnumFlag( const QString &key, QgsSettings::Section section, const T &defaultValue, const QString &description = QString() )
899  : QgsSettingsEntryBase( key, section, QMetaEnum::fromType<T>().isFlag() ? QVariant( QMetaEnum::fromType<T>().valueToKeys( static_cast <int >( defaultValue ) ) ) : QVariant( QMetaEnum::fromType<T>().valueToKey( static_cast< int >( defaultValue ) ) ), description )
900  {
901  mMetaEnum = QMetaEnum::fromType<T>();
902  Q_ASSERT( mMetaEnum.isValid() );
903  if ( !mMetaEnum.isValid() )
904  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( QgsSettingsEntryBase::key() ) );
905  }
906 
914  T value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const T &defaultValueOverride = T() ) const
915  {
916  QStringList dynamicKeyPartList;
917  if ( !dynamicKeyPart.isEmpty() )
918  dynamicKeyPartList.append( dynamicKeyPart );
919 
920  return value( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride );
921  }
922 
930  T value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const T &defaultValueOverride = T() ) const
931  {
932  T defaultVal = defaultValue();
933  if ( useDefaultValueOverride )
934  defaultVal = defaultValueOverride;
935 
936  if ( !mMetaEnum.isFlag() )
937  return QgsSettings().enumValue( key( dynamicKeyPartList ),
938  defaultVal,
939  section() );
940  else
941  return QgsSettings().flagValue( key( dynamicKeyPartList ),
942  defaultVal,
943  section() );
944  }
945 
949  T defaultValue() const
950  {
951  if ( !mMetaEnum.isValid() )
952  {
953  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( key() ) );
954  return T();
955  }
956 
957  bool ok = false;
958  T defaultValue;
959  if ( !mMetaEnum.isFlag() )
960  defaultValue = static_cast<T>( mMetaEnum.keyToValue( defaultValueAsVariant().toByteArray(), &ok ) );
961  else
962  defaultValue = static_cast<T>( mMetaEnum.keysToValue( defaultValueAsVariant().toByteArray(), &ok ) );
963 
964  if ( !ok )
965  {
966  QgsDebugMsg( QStringLiteral( "Invalid enum/flag key/s '%1' for settings key '%2'" ).arg( defaultValueAsVariant().toString(), key() ) );
967  return T();
968  }
969 
970  return defaultValue;
971  }
972 
979  bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const
980  {
981  QStringList dynamicKeyPartList;
982  if ( !dynamicKeyPart.isEmpty() )
983  dynamicKeyPartList.append( dynamicKeyPart );
984 
985  return setValue( value, dynamicKeyPartList );
986  }
987 
994  bool setValue( const T &value, const QStringList &dynamicKeyPartList ) const
995  {
996  if ( !mMetaEnum.isValid() )
997  {
998  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( key( dynamicKeyPartList ) ) );
999  return false;
1000  }
1001 
1002  if ( !mMetaEnum.isFlag() )
1003  {
1004  const char *enumKey = mMetaEnum.valueToKey( static_cast< int >( value ) );
1005  if ( enumKey == nullptr )
1006  {
1007  QgsDebugMsg( QStringLiteral( "Invalid enum value '%1'." ).arg( static_cast< int >( value ) ) );
1008  return false;
1009  }
1010 
1011  return QgsSettingsEntryBase::setVariantValue( enumKey, dynamicKeyPartList );
1012  }
1013  else
1014  {
1015  const QByteArray flagKeys = mMetaEnum.valueToKeys( static_cast< int >( value ) );
1016  if ( flagKeys.isEmpty() )
1017  {
1018  QgsDebugMsg( QStringLiteral( "Invalid flag value '%1'." ).arg( static_cast< int >( value ) ) );
1019  return false;
1020  }
1021  return QgsSettingsEntryBase::setVariantValue( flagKeys, dynamicKeyPartList );
1022  }
1023  }
1024 
1026  {
1028  }
1029 
1030  private:
1031 
1032  QMetaEnum mMetaEnum;
1033 
1034 };
1035 
1036 
1045 {
1046  public:
1047 
1048 #ifndef SIP_RUN
1049 
1058  QgsSettingsEntryColor( const QString &key,
1059  QgsSettings::Section section,
1060  const QColor &defaultValue = QColor(),
1061  const QString &description = QString() );
1062 
1063 #endif
1064 
1074  QgsSettingsEntryColor( const QString &key,
1075  const QString &pluginName,
1076  const QColor &defaultValue = QColor(),
1077  const QString &description = QString() );
1078 
1085  bool setValue( const QColor &value, const QString &dynamicKeyPart = QString() ) const;
1086 
1093  bool setValue( const QColor &value, const QStringList &dynamicKeyPartList ) const;
1094 
1102  QColor value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QString &defaultValueOverride = QString() ) const;
1103 
1111  QColor value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QString &defaultValueOverride = QString() ) const;
1112 
1116  QColor defaultValue() const;
1117 
1118  virtual SettingsType settingsType() const override;
1119 
1120 };
1121 
1122 #endif // QGSSETTINGSENTRY_H
Represent settings entry and provides methods for reading and writing settings values.
virtual SettingsType settingsType() const =0
Returns the settings entry type.
virtual bool setVariantValue(const QVariant &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
SettingsType
Types of settings entries.
QString key(const QString &dynamicKeyPart=QString()) const
Returns settings entry key.
A boolean settings entry.
A color settings entry.
A double settings entry.
A template class for enum and flag settings entry.
T defaultValue() const
Returns settings default value.
QgsSettingsEntryEnumFlag(const QString &key, QgsSettings::Section section, const T &defaultValue, const QString &description=QString())
Constructor for QgsSettingsEntryEnumFlagBase.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
virtual QgsSettingsEntryBase::SettingsType settingsType() const override
Returns the settings entry type.
T value(const QStringList &dynamicKeyPartList, bool useDefaultValueOverride=false, const T &defaultValueOverride=T()) const
Returns settings value.
T value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, const T &defaultValueOverride=T()) const
Returns settings value.
bool setValue(const T &value, const QStringList &dynamicKeyPartList) const
Set settings value.
An integer settings entry.
A string list settings entry.
A string settings entry.
A variant settings entry.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
T flagValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on a flag.
Definition: qgssettings.h:331
Section
Sections for namespaced settings.
Definition: qgssettings.h:68
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
Definition: qgssettings.h:253
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_END
Definition: qgis_sip.h:194
#define QgsDebugMsg(str)
Definition: qgslogger.h:38