From ffe680bad92b82e14e806e2f68cc9f81302010b1 Mon Sep 17 00:00:00 2001 From: Damiano Date: Wed, 10 Mar 2021 20:21:16 +0100 Subject: [PATCH 01/20] SettingsEntry new methods exists and remove --- src/app/locator/qgslocatoroptionswidget.cpp | 10 +- src/app/options/qgsoptions.cpp | 3 +- src/core/CMakeLists.txt | 9 + src/core/layout/qgslayout.h | 9 + src/core/locator/qgslocator.cpp | 13 +- src/core/locator/qgslocator.h | 17 + src/core/qgsapplication.cpp | 14 +- src/core/qgsapplication.h | 8 + src/core/settings/qgssettingsentry.cpp | 382 +++++++++++++++ src/core/settings/qgssettingsentry.h | 461 ++++++++++++++++++ src/core/settings/qgssettingsregistrycore.cpp | 36 ++ src/core/settings/qgssettingsregistrycore.h | 50 ++ 12 files changed, 997 insertions(+), 15 deletions(-) create mode 100644 src/core/settings/qgssettingsentry.cpp create mode 100644 src/core/settings/qgssettingsentry.h create mode 100644 src/core/settings/qgssettingsregistrycore.cpp create mode 100644 src/core/settings/qgssettingsregistrycore.h diff --git a/src/app/locator/qgslocatoroptionswidget.cpp b/src/app/locator/qgslocatoroptionswidget.cpp index 35c1bb73e6b8..66a079732f8d 100644 --- a/src/app/locator/qgslocatoroptionswidget.cpp +++ b/src/app/locator/qgslocatoroptionswidget.cpp @@ -322,8 +322,6 @@ QVariant QgsLocatorFiltersModel::headerData( int section, Qt::Orientation orient void QgsLocatorFiltersModel::commitChanges() { - QgsSettings settings; - QHash< QgsLocatorFilter *, QString >::const_iterator itp = mPrefixes.constBegin(); for ( ; itp != mPrefixes.constEnd(); ++itp ) { @@ -332,26 +330,26 @@ void QgsLocatorFiltersModel::commitChanges() if ( !activePrefix.isEmpty() && activePrefix != filter->prefix() ) { filter->setActivePrefix( activePrefix ); - settings.setValue( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), activePrefix, QgsSettings::Section::Gui ); + QgsLocator::Settings::LocatorFilterPrefix().setValue( activePrefix, filter->name() ); } else { filter->setActivePrefix( QString() ); - settings.remove( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), QgsSettings::Section::Gui ); + QgsLocator::Settings::LocatorFilterPrefix().remove( filter->name() ); } } QHash< QgsLocatorFilter *, bool >::const_iterator it = mEnabledChanges.constBegin(); for ( ; it != mEnabledChanges.constEnd(); ++it ) { QgsLocatorFilter *filter = it.key(); - settings.setValue( QStringLiteral( "locator_filters/enabled_%1" ).arg( filter->name() ), it.value(), QgsSettings::Section::Gui ); + QgsLocator::Settings::LocatorFilterEnabled().setValue( it.value(), filter->name() ); filter->setEnabled( it.value() ); } it = mDefaultChanges.constBegin(); for ( ; it != mDefaultChanges.constEnd(); ++it ) { QgsLocatorFilter *filter = it.key(); - settings.setValue( QStringLiteral( "locator_filters/default_%1" ).arg( filter->name() ), it.value(), QgsSettings::Section::Gui ); + QgsLocator::Settings::LocatorFilterDefault().setValue( it.value(), filter->name() ); filter->setUseWithoutPrefix( it.value() ); } } diff --git a/src/app/options/qgsoptions.cpp b/src/app/options/qgsoptions.cpp index c3d9956c32d3..7017da5fcf73 100644 --- a/src/app/options/qgsoptions.cpp +++ b/src/app/options/qgsoptions.cpp @@ -62,6 +62,7 @@ #include "qgsbearingnumericformat.h" #include "qgssublayersdialog.h" #include "options/qgsadvancedoptions.h" +#include "qgslayout.h" #ifdef HAVE_OPENCL #include "qgsopenclutils.h" @@ -1480,7 +1481,7 @@ void QgsOptions::saveOptions() { pathsList << mListComposerTemplatePaths->item( i )->text(); } - mSettings->setValue( QStringLiteral( "Layout/searchPathsForTemplates" ), pathsList, QgsSettings::Core ); + QgsLayout::Settings::SearchPathForTemplates().setValue( pathsList ); pathsList.clear(); for ( int r = 0; r < mLocalizedDataPathListWidget->count(); r++ ) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8b3c33cc91d6..998e686ffa9d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -716,6 +716,10 @@ set(QGIS_CORE_SRCS geocms/geonode/qgsgeonodeconnection.cpp geocms/geonode/qgsgeonoderequest.cpp + settings/qgssettingsentry.cpp + settings/qgssettingsregistry.cpp + settings/qgssettingsregistrycore.cpp + validity/qgsabstractvaliditycheck.cpp validity/qgsvaliditycheckcontext.cpp validity/qgsvaliditycheckregistry.cpp @@ -1555,6 +1559,10 @@ set(QGIS_CORE_HDRS textrenderer/qgstextrendererutils.h textrenderer/qgstextshadowsettings.h + settings/qgssettingsentry.h + settings/qgssettingsregistry.h + settings/qgssettingsregistrycore.h + validity/qgsabstractvaliditycheck.h validity/qgsvaliditycheckcontext.h validity/qgsvaliditycheckregistry.h @@ -1803,6 +1811,7 @@ target_include_directories(qgis_core PUBLIC raster renderer scalebar + settings symbology textrenderer validity diff --git a/src/core/layout/qgslayout.h b/src/core/layout/qgslayout.h index 12c2da188578..b49c31f28f82 100644 --- a/src/core/layout/qgslayout.h +++ b/src/core/layout/qgslayout.h @@ -24,6 +24,7 @@ #include "qgslayoutguidecollection.h" #include "qgslayoutexporter.h" #include "qgsmasterlayoutinterface.h" +#include "qgssettingsentry.h" class QgsLayoutItemMap; class QgsLayoutModel; @@ -657,6 +658,14 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext */ bool accept( QgsStyleEntityVisitorInterface *visitor ) const; + struct Settings + { + struct SearchPathForTemplates : public QgsSettingsEntryStringList + { + SearchPathForTemplates() : QgsSettingsEntryStringList( "Layout/searchPathsForTemplates", QgsSettings::Core, QStringList(), QObject::tr( "Search path for templates" ) ) {} + }; + }; + public slots: /** diff --git a/src/core/locator/qgslocator.cpp b/src/core/locator/qgslocator.cpp index f55b0f00ec71..c61819e75b2d 100644 --- a/src/core/locator/qgslocator.cpp +++ b/src/core/locator/qgslocator.cpp @@ -94,10 +94,15 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter ) filter->setParent( this ); // restore settings - QgsSettings settings; - bool enabled = settings.value( QStringLiteral( "locator_filters/enabled_%1" ).arg( filter->name() ), true, QgsSettings::Section::Gui ).toBool(); - bool byDefault = settings.value( QStringLiteral( "locator_filters/default_%1" ).arg( filter->name() ), filter->useWithoutPrefix(), QgsSettings::Section::Gui ).toBool(); - QString prefix = settings.value( QStringLiteral( "locator_filters/prefix_%1" ).arg( filter->name() ), filter->prefix(), QgsSettings::Section::Gui ).toString(); + bool enabled = true; + if ( QgsLocator::Settings::LocatorFilterEnabled().exists( filter->name() ) ) + enabled = QgsLocator::Settings::LocatorFilterEnabled().value( filter->name() ); + bool byDefault = filter->useWithoutPrefix(); + if ( QgsLocator::Settings::LocatorFilterDefault().exists( filter->name() ) ) + byDefault = QgsLocator::Settings::LocatorFilterDefault().value( filter->name() ); + QString prefix = filter->prefix(); + if ( QgsLocator::Settings::LocatorFilterPrefix().exists( filter->name() ) ) + prefix = QgsLocator::Settings::LocatorFilterPrefix().value( filter->name() ); if ( prefix.isEmpty() ) { prefix = filter->prefix(); diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index e554436f9dfa..8ccf5522779e 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -29,6 +29,7 @@ #include "qgslocatorfilter.h" #include "qgsfeedback.h" #include "qgslocatorcontext.h" +#include "qgssettingsentry.h" /** @@ -152,6 +153,22 @@ class CORE_EXPORT QgsLocator : public QObject */ QStringList completionList() const {return mAutocompletionList;} + struct Settings + { + struct LocatorFilterEnabled : public QgsSettingsEntryBool + { + LocatorFilterEnabled() : QgsSettingsEntryBool( "locator_filters/enabled_%1", QgsSettings::Gui, true, QObject::tr( "Enabled" ) ) {} + }; + struct LocatorFilterDefault : public QgsSettingsEntryBool + { + LocatorFilterDefault() : QgsSettingsEntryBool( "locator_filters/default_%1", QgsSettings::Gui, false, QObject::tr( "Default value" ) ) {} + }; + struct LocatorFilterPrefix : public QgsSettingsEntryString + { + LocatorFilterPrefix() : QgsSettingsEntryString( "locator_filters/prefix_%1", QgsSettings::Gui, QString(), QObject::tr( "Locator filter prefix" ) ) {} + }; + }; + signals: /** diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index 16d316026023..0a750be59f07 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -20,6 +20,7 @@ #include "qgsexception.h" #include "qgsgeometry.h" #include "qgsannotationitemregistry.h" +#include "qgslayout.h" #include "qgslayoutitemregistry.h" #include "qgslogger.h" #include "qgsproject.h" @@ -52,6 +53,7 @@ #include "qgsmessagelog.h" #include "qgsannotationregistry.h" #include "qgssettings.h" +#include "qgssettingsregistrycore.h" #include "qgstiledownloadmanager.h" #include "qgsunittypes.h" #include "qgsuserprofile.h" @@ -1105,10 +1107,7 @@ QStringList QgsApplication::layoutTemplatePaths() { //local directories to search when looking for an template with a given basename //defined by user in options dialog - QgsSettings settings; - QStringList pathList = settings.value( QStringLiteral( "Layout/searchPathsForTemplates" ), QVariant(), QgsSettings::Core ).toStringList(); - - return pathList; + return QgsLayout::Settings::SearchPathForTemplates().value(); } QMap QgsApplication::systemEnvVars() @@ -2173,6 +2172,11 @@ QgsTaskManager *QgsApplication::taskManager() return members()->mTaskManager; } +QgsSettingsRegistryCore *QgsApplication::settingsRegistryCore() +{ + return members()->mSettingsRegistryCore; +} + QgsColorSchemeRegistry *QgsApplication::colorSchemeRegistry() { return members()->mColorSchemeRegistry; @@ -2362,6 +2366,7 @@ QgsApplication::ApplicationMembers::ApplicationMembers() { // don't use initializer lists or scoped pointers - as more objects are added here we // will need to be careful with the order of creation/destruction + mSettingsRegistryCore = new QgsSettingsRegistryCore(); mLocalizedDataPathRegistry = new QgsLocalizedDataPathRegistry(); mMessageLog = new QgsMessageLog(); QgsRuntimeProfiler *profiler = QgsRuntimeProfiler::threadLocalInstance(); @@ -2563,6 +2568,7 @@ QgsApplication::ApplicationMembers::~ApplicationMembers() delete mConnectionRegistry; delete mLocalizedDataPathRegistry; delete mCrsRegistry; + delete mSettingsRegistryCore; } QgsApplication::ApplicationMembers *QgsApplication::members() diff --git a/src/core/qgsapplication.h b/src/core/qgsapplication.h index fee2c3eb1199..2ea07b19e720 100644 --- a/src/core/qgsapplication.h +++ b/src/core/qgsapplication.h @@ -25,6 +25,7 @@ #include "qgsconfig.h" #include "qgstranslationcontext.h" +class QgsSettingsRegistryCore; class Qgs3DRendererRegistry; class QgsActionScopeRegistry; class QgsAnnotationItemRegistry; @@ -617,6 +618,12 @@ class CORE_EXPORT QgsApplication : public QApplication */ static QgsTaskManager *taskManager(); + /** + * Returns the application's settings registry, used for managing application settings. + * \since QGIS 3.20 + */ + static QgsSettingsRegistryCore *settingsRegistryCore() SIP_KEEPREFERENCE; + /** * Returns the application's color scheme registry, used for managing color schemes. * \since QGIS 3.0 @@ -994,6 +1001,7 @@ class CORE_EXPORT QgsApplication : public QApplication struct ApplicationMembers { + QgsSettingsRegistryCore *mSettingsRegistryCore = nullptr; QgsCoordinateReferenceSystemRegistry *mCrsRegistry = nullptr; Qgs3DRendererRegistry *m3DRendererRegistry = nullptr; Qgs3DSymbolRegistry *m3DSymbolRegistry = nullptr; diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp new file mode 100644 index 000000000000..dc36173d4263 --- /dev/null +++ b/src/core/settings/qgssettingsentry.cpp @@ -0,0 +1,382 @@ +/*************************************************************************** + qgssettingsentry.cpp + -------------------------------------- + Date : February 2021 + Copyright : (C) 2021 by Damiano Lombardi + Email : damiano at opengis dot ch + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "qgssettingsentry.h" + +#include "qgslogger.h" + +QgsSettingsEntry::QgsSettingsEntry( QString key, + QgsSettings::Section section, + QVariant defaultValue, + QString description ) + : mKey( key ) + , mDefaultValue( defaultValue ) + , mSection( section ) + , mDescription( description ) +{ +} + +QgsSettingsEntry::~QgsSettingsEntry() +{ +} + +QString QgsSettingsEntry::key( const QString &dynamicKeyPart ) const +{ + if ( dynamicKeyPart.isEmpty() == false ) + { + if ( hasDynamicKey() == false ) + { + QgsLogger::warning( QStringLiteral( "Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( mKey ) ); + return mKey; + } + + QString completeKey = mKey; + return completeKey.replace( '%', dynamicKeyPart ); + } + else + { + if ( hasDynamicKey() == true ) + QgsLogger::warning( QStringLiteral( "Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( mKey ) ); + + return mKey; + } +} + +bool QgsSettingsEntry::hasDynamicKey() const +{ + return mKey.contains( '%' ); +} + +bool QgsSettingsEntry::exists( const QString &dynamicKeyPart ) const +{ + return QgsSettings().contains( key( dynamicKeyPart ), section() ); +} + +void QgsSettingsEntry::remove( const QString &dynamicKeyPart ) const +{ + QgsSettings().remove( key( dynamicKeyPart ), section() ); +} + +QgsSettings::Section QgsSettingsEntry::section() const +{ + return mSection; +} + +bool QgsSettingsEntry::setValue( const QVariant &value, const QString &dynamicKeyPart ) +{ + QgsSettings().setValue( key( dynamicKeyPart ), + value, + section() ); + return true; +} + +QgsSettingsEntry::SettingsType QgsSettingsEntry::settingsType() const +{ + return QgsSettingsEntry::Variant; +} + +QVariant QgsSettingsEntry::valueFromPython() const +{ + return value(); +} + +QVariant QgsSettingsEntry::defaultValueFromPython() const +{ + return defaultValue(); +} + +QString QgsSettingsEntry::description() const +{ + return mDescription; +} + +QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, + QgsSettings::Section section, + const QString &defaultValue, + const QString &description, + int minLength, + int maxLength ) + : QgsSettingsEntry( key, + section, + defaultValue, + description ) + , mMinLength( minLength ) + , mMaxLength( maxLength ) +{ + +} + +bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dynamicKeyPart ) +{ + if ( value.canConvert() == false ) + { + QgsDebugMsg( QObject::tr( "Can't convert value '%1' to string for settings with key '%2'" ) + .arg( value.toString() ) + .arg( QgsSettingsEntry::key() ) ); + return false; + } + + QString valueString = value.toString(); + if ( valueString.length() < mMinLength ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is shorter than minimum length '%3'." ) + .arg( QgsSettingsEntry::key() ) + .arg( valueString.length() ) + .arg( mMinLength ) ); + return false; + } + + if ( mMaxLength >= 0 + && valueString.length() > mMaxLength ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is longer than maximum length '%3'." ) + .arg( QgsSettingsEntry::key() ) + .arg( valueString.length() ) + .arg( mMinLength ) ); + return false; + } + + QgsSettingsEntry::setValue( value, dynamicKeyPart ); + return true; +} + +QgsSettingsEntry::SettingsType QgsSettingsEntryString::settingsType() const +{ + return QgsSettingsEntry::String; +} + +int QgsSettingsEntryString::minLength() +{ + return mMinLength; +} + +int QgsSettingsEntryString::maxLength() +{ + return mMaxLength; +} + +QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, + QgsSettings::Section section, + const QStringList &defaultValue, + const QString &description ) + : QgsSettingsEntry( key, + section, + defaultValue, + description ) +{ + +} + +bool QgsSettingsEntryStringList::setValue( const QVariant &value, const QString &dynamicKeyPart ) +{ + if ( value.canConvert() == false ) + { + QgsDebugMsg( QObject::tr( "Can't convert value '%1' to string list for settings with key '%2'" ) + .arg( value.toString() ) + .arg( QgsSettingsEntry::key() ) ); + return false; + } + + QgsSettingsEntry::setValue( value, dynamicKeyPart ); + return true; +} + +QgsSettingsEntry::SettingsType QgsSettingsEntryStringList::settingsType() const +{ + return QgsSettingsEntry::StringList; +} + +QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, + QgsSettings::Section section, + bool defaultValue, + const QString &description ) + : QgsSettingsEntry( key, + section, + defaultValue, + description ) +{ + +} + +bool QgsSettingsEntryBool::setValue( const QVariant &value, const QString &dynamicKeyPart ) +{ + if ( value.canConvert() == false ) + { + QgsDebugMsg( QObject::tr( "Can't convert value '%1' to bool for settings with key '%2'" ) + .arg( value.toString() ) + .arg( QgsSettingsEntry::key() ) ); + return false; + } + + QgsSettingsEntry::setValue( value, dynamicKeyPart ); + return true; +} + +QgsSettingsEntry::SettingsType QgsSettingsEntryBool::settingsType() const +{ + return QgsSettingsEntry::Bool; +} + +QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, + QgsSettings::Section section, + qlonglong defaultValue, + const QString &description, + qlonglong minValue, + qlonglong maxValue ) + : QgsSettingsEntry( key, + section, + defaultValue, + description ) + , mMinValue( minValue ) + , mMaxValue( maxValue ) +{ + +} + +bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dynamicKeyPart ) +{ + if ( value.canConvert() == false ) + { + QgsDebugMsg( QObject::tr( "Can't convert value '%1' to qlonglong for settings with key '%2'" ) + .arg( value.toString() ) + .arg( QgsSettingsEntry::key() ) ); + return false; + } + + qlonglong valueLongLong = value.toLongLong(); + if ( valueLongLong < mMinValue ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) + .arg( QgsSettingsEntry::key() ) + .arg( valueLongLong ) + .arg( mMinValue ) ); + return false; + } + + if ( valueLongLong > mMaxValue ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) + .arg( QgsSettingsEntry::key() ) + .arg( valueLongLong ) + .arg( mMinValue ) ); + return false; + } + + QgsSettingsEntry::setValue( value, dynamicKeyPart ); + return true; +} + +QgsSettingsEntry::SettingsType QgsSettingsEntryInteger::settingsType() const +{ + return QgsSettingsEntry::Integer; +} + +qlonglong QgsSettingsEntryInteger::minValue() +{ + return mMinValue; +} + +qlonglong QgsSettingsEntryInteger::maxValue() +{ + return mMaxValue; +} + +QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, + QgsSettings::Section section, + double defaultValue, + const QString &description, + double minValue, + double maxValue, + double displayDecimals ) + : QgsSettingsEntry( key, + section, + defaultValue, + description ) + , mMinValue( minValue ) + , mMaxValue( maxValue ) + , mDisplayHintDecimals( displayDecimals ) +{ + +} + +bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dynamicKeyPart ) +{ + if ( value.canConvert() == false ) + { + QgsDebugMsg( QObject::tr( "Can't convert value '%1' to double for settings with key '%2'" ) + .arg( value.toString() ) + .arg( QgsSettingsEntry::key() ) ); + return false; + } + + double valueDouble = value.toDouble(); + if ( valueDouble < mMinValue ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) + .arg( QgsSettingsEntry::key() ) + .arg( valueDouble ) + .arg( mMinValue ) ); + return false; + } + + if ( valueDouble > mMaxValue ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) + .arg( QgsSettingsEntry::key() ) + .arg( valueDouble ) + .arg( mMinValue ) ); + return false; + } + + QgsSettingsEntry::setValue( value, dynamicKeyPart ); + return true; +} + +QgsSettingsEntry::SettingsType QgsSettingsEntryDouble::settingsType() const +{ + return QgsSettingsEntry::Double; +} + +double QgsSettingsEntryDouble::minValue() const +{ + return mMinValue; +} + +double QgsSettingsEntryDouble::maxValue() const +{ + return mMaxValue; +} + +int QgsSettingsEntryDouble::displayHintDecimals() const +{ + return mDisplayHintDecimals; +} + +bool QgsSettingsEntryEnum::setValue( const QVariant &value, const QString &dynamicKeyPart ) +{ + if ( mMetaEnum.isValid() == false ) + { + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); + return false; + } + + QgsSettingsEntry::setValue( mMetaEnum.valueToKey( value.toInt() ), dynamicKeyPart ); + return true; +} + +QgsSettingsEntry::SettingsType QgsSettingsEntryEnum::settingsType() const +{ + return QgsSettingsEntry::Enum; +} diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h new file mode 100644 index 000000000000..900ff76234bb --- /dev/null +++ b/src/core/settings/qgssettingsentry.h @@ -0,0 +1,461 @@ +/*************************************************************************** + qgssettingsentry.h + -------------------------------------- + Date : February 2021 + Copyright : (C) 2021 by Damiano Lombardi + Email : damiano at opengis dot ch + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef QGSSETTINGSENTRY_H +#define QGSSETTINGSENTRY_H + +#include +#include + +#include "qgis_core.h" +#include "qgis_sip.h" +#include "qgssettings.h" + +/** + * \ingroup core + * \class QgsSettingsEntry + * + * Represent settings entry and provides methods for reading and writing settings values. + * Different subclasses are provided for differents settings types with metainformations + * to validate set values and provide more accurate settings description for the gui. + * + * \since QGIS 3.18 + */ +class CORE_EXPORT QgsSettingsEntry +{ + +#ifdef SIP_RUN + SIP_CONVERT_TO_SUBCLASS_CODE + if ( dynamic_cast< QgsSettingsEntryString * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryString; + if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryStringList; + else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryInteger; + else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryDouble; + else + sipType = NULL; + SIP_END +#endif + + public: + + enum SettingsType + { + Variant, + String, + StringList, + Bool, + Integer, + Double, + Enum + }; + + /** + * Constructor for QgsSettingsEntry. + * + * The \a key argument specifies the key of the settings. + * The \a section argument specifies the section of the settings. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntry( QString key, + QgsSettings::Section section, + QVariant defaultValue = QVariant(), + QString description = QString() ); + + /** + * Destructor for QgsSettingsEntry. + */ + virtual ~QgsSettingsEntry(); + + /** + * Get settings entry key. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + QString key( const QString &dynamicKeyPart = QString() ) const; + + /** + * Returns true if a part of the settings key is built dynamically. + */ + bool hasDynamicKey() const; + + /** + * Returns true if the settings is contained in the underlying QSettings. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + bool exists( const QString &dynamicKeyPart = QString() ) const; + + /** + * Removes the settings from the underlying QSettings. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + void remove( const QString &dynamicKeyPart = QString() ) const; + + /** + * Get settings section. The settings section of the parent group is returned if available. + */ + QgsSettings::Section section() const; + + /** + * Set settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); + + /** + * Get settings value. + */ + QVariant valueFromPython() const SIP_PYNAME( value ); + +#ifndef SIP_RUN + template + T value( const QString &dynamicKeyPart = QString() ) const + { + QVariant variantValue = QgsSettings().value( key( dynamicKeyPart ), + mDefaultValue, + mSection ); + if ( variantValue.canConvert() == false ) + QgsDebugMsg( QObject::tr( "Can't convert setting '%1' to type '%2'" ) + .arg( key( dynamicKeyPart ) ) + .arg( typeid( T ).name() ) ); + + return variantValue.value(); + } +#endif + + /** + * Get settings default value. + */ + QVariant defaultValueFromPython() const SIP_PYNAME( defaultValue ); + +#ifndef SIP_RUN + template + T defaultValue() const + { + if ( mDefaultValue.canConvert() == false ) + QgsDebugMsg( QObject::tr( "Can't convert default value of setting '%1' to type '%2'" ) + .arg( mKey ) + .arg( typeid( T ).name() ) ); + + return mDefaultValue.value(); + } +#endif + + /** + * Get the settings entry type. + */ + virtual SettingsType settingsType() const; + + /** + * Get the settings entry description. + */ + QString description() const; + + private: + + QString mKey; + QVariant mDefaultValue; + QgsSettings::Section mSection; + QString mDescription; + +}; + +/** + * \class QgsSettingsEntryString + * \ingroup core + * A string settings entry. + * \since QGIS 3.18 + */ +class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry +{ + public: + + /** + * Constructor for QgsSettingsEntryString. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + * The \a minLength argument specifies the minimal length of the string value. + * The \a maxLength argument specifies the maximal lenght of the string value. + * By -1 the there is no limit + */ + QgsSettingsEntryString( const QString &key, + QgsSettings::Section section, + const QString &defaultValue = QString(), + const QString &description = QString(), + int minLength = 0, + int maxLength = -1 ); + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override; + + /** + * Returns the string minimum length. + */ + int minLength(); + + /** + * Returns the string maximum length. By -1 there is no limitation. + */ + int maxLength(); + + private: + + int mMinLength; + int mMaxLength; + +}; + +/** + * \class QgsSettingsEntryStringList + * \ingroup core + * A string list settings entry. + * \since QGIS 3.18 + */ +class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry +{ + public: + + /** + * Constructor for QgsSettingsEntryStringList. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryStringList( const QString &key, + QgsSettings::Section section, + const QStringList &defaultValue = QStringList(), + const QString &description = QString() ); + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override; + +}; + +/** + * \class QgsSettingsEntryBool + * \ingroup core + * A boolean settings entry. + * \since QGIS 3.18 + */ +class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry +{ + public: + + /** + * Constructor for QgsSettingsEntryBool. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryBool( const QString &key, + QgsSettings::Section section, + bool defaultValue = false, + const QString &description = QString() ); + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override; + +}; + +/** + * \class QgsSettingsEntryInteger + * \ingroup core + * An integer settings entry. + * \since QGIS 3.18 + */ +class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry +{ + public: + + /** + * Constructor for QgsSettingsEntryInteger. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + * The \a minValue argument specifies the minimal value. + * The \a maxValue argument specifies the maximal value. + */ + QgsSettingsEntryInteger( const QString &key, + QgsSettings::Section section, + qlonglong defaultValue = 0, + const QString &description = QString(), + qlonglong minValue = -__LONG_LONG_MAX__ + 1, + qlonglong maxValue = __LONG_LONG_MAX__ ); + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override; + + /** + * Returns the minimum value. + */ + qlonglong minValue(); + + /** + * Returns the maximum value. + */ + qlonglong maxValue(); + + private: + + qlonglong mMinValue; + qlonglong mMaxValue; + +}; + +/** + * \class QgsSettingsEntryDouble + * \ingroup core + * A double settings entry. + * \since QGIS 3.18 + */ +class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry +{ + public: + + /** + * Constructor for QgsSettingsEntryDouble. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + * The \a minValue argument specifies the minimal value. + * The \a maxValue argument specifies the maximal value. + */ + QgsSettingsEntryDouble( const QString &key, + QgsSettings::Section section, + double defaultValue = 0.0, + const QString &description = QString(), + double minValue = __DBL_MIN__, + double maxValue = __DBL_MAX__, + double displayDecimals = 1 ); + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override; + + /** + * Returns the minimum value. + */ + double minValue() const; + + /** + * Returns the maximum value. + */ + double maxValue() const; + + /** + * Returns how much decimals should be shown in the Gui. + */ + int displayHintDecimals() const; + + private: + + double mMinValue; + double mMaxValue; + + int mDisplayHintDecimals; + +}; + +#ifndef SIP_RUN + +/** + * \class QgsSettingsEntryEnum + * \ingroup core + * An enum settings entry. + * \since QGIS 3.18 + */ +class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntry +{ + public: + + /** + * Constructor for QgsSettingsEntryEnum. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + template + QgsSettingsEntryEnum( const QString &key, + QgsSettings::Section *section, + const T &defaultValue, + const QString &description = QString() ) + : QgsSettingsEntry( key, + section, + defaultValue, + description ) + { + mMetaEnum = QMetaEnum::fromType(); + Q_ASSERT( mMetaEnum.isValid() ); + if ( !mMetaEnum.isValid() ) + { + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); + } + } + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override; + + private: + + QMetaEnum mMetaEnum; + +}; +#endif + + + +#endif // QGSSETTINGSENTRY_H diff --git a/src/core/settings/qgssettingsregistrycore.cpp b/src/core/settings/qgssettingsregistrycore.cpp new file mode 100644 index 000000000000..d874212c9827 --- /dev/null +++ b/src/core/settings/qgssettingsregistrycore.cpp @@ -0,0 +1,36 @@ +/*************************************************************************** + qgssettingsregistrycore.cpp + -------------------------------------- + Date : February 2021 + Copyright : (C) 2021 by Damiano Lombardi + Email : damiano at opengis dot ch + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "qgssettingsregistrycore.h" + +#include "qgslayout.h" +#include "qgslocator.h" + +QgsSettingsRegistryCore::QgsSettingsRegistryCore() + : mSettingsEntries() +{ + mSettingsEntries.append( new QgsLayout::Settings::SearchPathForTemplates() ); + + mSettingsEntries.append( new QgsLocator::Settings::LocatorFilterEnabled() ); + mSettingsEntries.append( new QgsLocator::Settings::LocatorFilterDefault() ); + mSettingsEntries.append( new QgsLocator::Settings::LocatorFilterPrefix() ); +} + +QgsSettingsRegistryCore::~QgsSettingsRegistryCore() +{ + qDeleteAll( mSettingsEntries ); +} + + diff --git a/src/core/settings/qgssettingsregistrycore.h b/src/core/settings/qgssettingsregistrycore.h new file mode 100644 index 000000000000..7bc8bdf23c96 --- /dev/null +++ b/src/core/settings/qgssettingsregistrycore.h @@ -0,0 +1,50 @@ +/*************************************************************************** + qgssettingsregistrycore.h + -------------------------------------- + Date : February 2021 + Copyright : (C) 2021 by Damiano Lombardi + Email : damiano at opengis dot ch + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + + +#ifndef QGSSETTINGSREGISTRYCORE_H +#define QGSSETTINGSREGISTRYCORE_H + +#include "qgis_core.h" +#include "qgis_sip.h" +#include "qgssettingsentry.h" + +#include "qgslayout.h" +#include "qgslocator.h" + +#include + +class QgsSettingsEntryStringList; + +/** + * \ingroup core + * \class QgsSettingsRegistryCore + * + * \since QGIS 3.18 + */ +class CORE_EXPORT QgsSettingsRegistryCore +{ + public: + + QgsSettingsRegistryCore(); + ~QgsSettingsRegistryCore(); + + private: + + QList mSettingsEntries; + +}; + +#endif // QGSSETTINGSREGISTRYCORE_H From d605257c54a9a687e85d720af664d5248309c4b8 Mon Sep 17 00:00:00 2001 From: Damiano Date: Wed, 10 Mar 2021 22:08:26 +0100 Subject: [PATCH 02/20] Corrected keys placeholders --- src/core/locator/qgslocator.h | 6 +++--- src/core/settings/qgssettingsentry.h | 14 +++++++------- src/core/settings/qgssettingsregistrycore.h | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index 8ccf5522779e..815d1b0f84b4 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -157,15 +157,15 @@ class CORE_EXPORT QgsLocator : public QObject { struct LocatorFilterEnabled : public QgsSettingsEntryBool { - LocatorFilterEnabled() : QgsSettingsEntryBool( "locator_filters/enabled_%1", QgsSettings::Gui, true, QObject::tr( "Enabled" ) ) {} + LocatorFilterEnabled() : QgsSettingsEntryBool( "locator_filters/enabled_%", QgsSettings::Gui, true, QObject::tr( "Enabled" ) ) {} }; struct LocatorFilterDefault : public QgsSettingsEntryBool { - LocatorFilterDefault() : QgsSettingsEntryBool( "locator_filters/default_%1", QgsSettings::Gui, false, QObject::tr( "Default value" ) ) {} + LocatorFilterDefault() : QgsSettingsEntryBool( "locator_filters/default_%", QgsSettings::Gui, false, QObject::tr( "Default value" ) ) {} }; struct LocatorFilterPrefix : public QgsSettingsEntryString { - LocatorFilterPrefix() : QgsSettingsEntryString( "locator_filters/prefix_%1", QgsSettings::Gui, QString(), QObject::tr( "Locator filter prefix" ) ) {} + LocatorFilterPrefix() : QgsSettingsEntryString( "locator_filters/prefix_%", QgsSettings::Gui, QString(), QObject::tr( "Locator filter prefix" ) ) {} }; }; diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 900ff76234bb..71c1c77828d4 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -31,7 +31,7 @@ * Different subclasses are provided for differents settings types with metainformations * to validate set values and provide more accurate settings description for the gui. * - * \since QGIS 3.18 + * \since QGIS 3.20 */ class CORE_EXPORT QgsSettingsEntry { @@ -182,7 +182,7 @@ class CORE_EXPORT QgsSettingsEntry * \class QgsSettingsEntryString * \ingroup core * A string settings entry. - * \since QGIS 3.18 + * \since QGIS 3.20 */ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry { @@ -234,7 +234,7 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry * \class QgsSettingsEntryStringList * \ingroup core * A string list settings entry. - * \since QGIS 3.18 + * \since QGIS 3.20 */ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry { @@ -266,7 +266,7 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry * \class QgsSettingsEntryBool * \ingroup core * A boolean settings entry. - * \since QGIS 3.18 + * \since QGIS 3.20 */ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry { @@ -298,7 +298,7 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry * \class QgsSettingsEntryInteger * \ingroup core * An integer settings entry. - * \since QGIS 3.18 + * \since QGIS 3.20 */ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry { @@ -349,7 +349,7 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry * \class QgsSettingsEntryDouble * \ingroup core * A double settings entry. - * \since QGIS 3.18 + * \since QGIS 3.20 */ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry { @@ -410,7 +410,7 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry * \class QgsSettingsEntryEnum * \ingroup core * An enum settings entry. - * \since QGIS 3.18 + * \since QGIS 3.20 */ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntry { diff --git a/src/core/settings/qgssettingsregistrycore.h b/src/core/settings/qgssettingsregistrycore.h index 7bc8bdf23c96..d8e35cd18a12 100644 --- a/src/core/settings/qgssettingsregistrycore.h +++ b/src/core/settings/qgssettingsregistrycore.h @@ -32,7 +32,7 @@ class QgsSettingsEntryStringList; * \ingroup core * \class QgsSettingsRegistryCore * - * \since QGIS 3.18 + * \since QGIS 3.20 */ class CORE_EXPORT QgsSettingsRegistryCore { From ed0b45d03d8817761f7ee5f75facf18ff4e2a15e Mon Sep 17 00:00:00 2001 From: Damiano Lombardi Date: Fri, 12 Mar 2021 07:41:25 +0100 Subject: [PATCH 03/20] Added macros for settings definition --- src/core/CMakeLists.txt | 2 -- src/core/layout/qgslayout.h | 5 +---- src/core/locator/qgslocator.h | 15 +++------------ src/core/settings/qgssettingsentry.h | 26 ++++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 998e686ffa9d..2f08298a669b 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -717,7 +717,6 @@ set(QGIS_CORE_SRCS geocms/geonode/qgsgeonoderequest.cpp settings/qgssettingsentry.cpp - settings/qgssettingsregistry.cpp settings/qgssettingsregistrycore.cpp validity/qgsabstractvaliditycheck.cpp @@ -1560,7 +1559,6 @@ set(QGIS_CORE_HDRS textrenderer/qgstextshadowsettings.h settings/qgssettingsentry.h - settings/qgssettingsregistry.h settings/qgssettingsregistrycore.h validity/qgsabstractvaliditycheck.h diff --git a/src/core/layout/qgslayout.h b/src/core/layout/qgslayout.h index b49c31f28f82..b71668a80733 100644 --- a/src/core/layout/qgslayout.h +++ b/src/core/layout/qgslayout.h @@ -660,10 +660,7 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext struct Settings { - struct SearchPathForTemplates : public QgsSettingsEntryStringList - { - SearchPathForTemplates() : QgsSettingsEntryStringList( "Layout/searchPathsForTemplates", QgsSettings::Core, QStringList(), QObject::tr( "Search path for templates" ) ) {} - }; + QGS_SETTING_ENTRY_STRINGLIST( SearchPathForTemplates, "Layout/searchPathsForTemplates", QgsSettings::Core, QStringList(), "Search path for templates" ) }; public slots: diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index 815d1b0f84b4..c5c1b92e343e 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -155,18 +155,9 @@ class CORE_EXPORT QgsLocator : public QObject struct Settings { - struct LocatorFilterEnabled : public QgsSettingsEntryBool - { - LocatorFilterEnabled() : QgsSettingsEntryBool( "locator_filters/enabled_%", QgsSettings::Gui, true, QObject::tr( "Enabled" ) ) {} - }; - struct LocatorFilterDefault : public QgsSettingsEntryBool - { - LocatorFilterDefault() : QgsSettingsEntryBool( "locator_filters/default_%", QgsSettings::Gui, false, QObject::tr( "Default value" ) ) {} - }; - struct LocatorFilterPrefix : public QgsSettingsEntryString - { - LocatorFilterPrefix() : QgsSettingsEntryString( "locator_filters/prefix_%", QgsSettings::Gui, QString(), QObject::tr( "Locator filter prefix" ) ) {} - }; + QGS_SETTING_ENTRY_BOOL( LocatorFilterEnabled, "locator_filters/enabled_%", QgsSettings::Gui, true, "Enabled" ) + QGS_SETTING_ENTRY_BOOL( LocatorFilterDefault, "locator_filters/default_%", QgsSettings::Gui, false, "Default value" ) + QGS_SETTING_ENTRY_STRING( LocatorFilterPrefix, "locator_filters/prefix_%", QgsSettings::Gui, QString(), "Locator filter prefix", 0, -1 ) }; signals: diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 71c1c77828d4..3a7f961f82a2 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -23,6 +23,30 @@ #include "qgis_sip.h" #include "qgssettings.h" +# define QGS_SETTING_ENTRY_STRING(name, path, section, defaultValue, description, minLength, maxLength) \ + struct name : public QgsSettingsEntryString \ + { name() : QgsSettingsEntryString( path, section, defaultValue, QObject::tr( description ), minLength, maxLength ) {} }; + +# define QGS_SETTING_ENTRY_STRINGLIST(name, path, section, defaultValue, description) \ + struct name : public QgsSettingsEntryStringList \ + { name() : QgsSettingsEntryStringList( path, section, defaultValue, QObject::tr( description ) ) {} }; + +# define QGS_SETTING_ENTRY_BOOL(name, path, section, defaultValue, description) \ + struct name : public QgsSettingsEntryBool \ + { name() : QgsSettingsEntryBool( path, section, defaultValue, QObject::tr( description ) ) {} }; + +# define QGS_SETTING_ENTRY_INTEGER(name, path, section, defaultValue, description, minValue, maxValue) \ + struct name : public QgsSettingsEntryInteger \ + { name() : QgsSettingsEntryInteger( path, section, defaultValue, QObject::tr( description ), minValue, maxValue ) {} }; + +# define QGS_SETTING_ENTRY_DOUBLE(name, path, section, defaultValue, description, minValue, maxValue, displayDecimals) \ + struct name : public QgsSettingsEntryDouble \ + { name() : QgsSettingsEntryDouble( path, section, defaultValue, QObject::tr( description ), minValue, maxValue, displayDecimals ) {} }; + +# define QGS_SETTING_ENTRY_ENUM(name, path, section, defaultValue, description) \ + struct name : public QgsSettingsEntryEnum \ + { name() : QgsSettingsEntryEnum( path, section, defaultValue, QObject::tr( description ) ) {} }; + /** * \ingroup core * \class QgsSettingsEntry @@ -42,6 +66,8 @@ class CORE_EXPORT QgsSettingsEntry sipType = sipType_QgsSettingsEntryString; if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryStringList; + else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryBool; else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryInteger; else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) ) From 9d4c665651e12985998a262afe91b8f92736c4e0 Mon Sep 17 00:00:00 2001 From: Damiano Lombardi Date: Sun, 14 Mar 2021 20:17:38 +0100 Subject: [PATCH 04/20] QgsSettingsEntry macros with variable/default arguments --- src/core/layout/qgslayout.h | 2 +- src/core/locator/qgslocator.h | 6 +-- src/core/settings/qgssettingsentry.h | 56 +++++++++++++++------------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/core/layout/qgslayout.h b/src/core/layout/qgslayout.h index b71668a80733..d0b2e7661254 100644 --- a/src/core/layout/qgslayout.h +++ b/src/core/layout/qgslayout.h @@ -660,7 +660,7 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext struct Settings { - QGS_SETTING_ENTRY_STRINGLIST( SearchPathForTemplates, "Layout/searchPathsForTemplates", QgsSettings::Core, QStringList(), "Search path for templates" ) + QGS_SETTING_ENTRY_STRINGLIST( SearchPathForTemplates, QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), tr( "Search path for templates" ) ) }; public slots: diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index c5c1b92e343e..640906fd5c5b 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -155,9 +155,9 @@ class CORE_EXPORT QgsLocator : public QObject struct Settings { - QGS_SETTING_ENTRY_BOOL( LocatorFilterEnabled, "locator_filters/enabled_%", QgsSettings::Gui, true, "Enabled" ) - QGS_SETTING_ENTRY_BOOL( LocatorFilterDefault, "locator_filters/default_%", QgsSettings::Gui, false, "Default value" ) - QGS_SETTING_ENTRY_STRING( LocatorFilterPrefix, "locator_filters/prefix_%", QgsSettings::Gui, QString(), "Locator filter prefix", 0, -1 ) + QGS_SETTING_ENTRY_BOOL( LocatorFilterEnabled, QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, tr( "Enabled" ) ) + QGS_SETTING_ENTRY_BOOL( LocatorFilterDefault, QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, tr( "Default value" ) ) + QGS_SETTING_ENTRY_STRING( LocatorFilterPrefix, QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), tr( "Locator filter prefix" ) ) }; signals: diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 3a7f961f82a2..64cddcd4cb45 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -23,30 +23,6 @@ #include "qgis_sip.h" #include "qgssettings.h" -# define QGS_SETTING_ENTRY_STRING(name, path, section, defaultValue, description, minLength, maxLength) \ - struct name : public QgsSettingsEntryString \ - { name() : QgsSettingsEntryString( path, section, defaultValue, QObject::tr( description ), minLength, maxLength ) {} }; - -# define QGS_SETTING_ENTRY_STRINGLIST(name, path, section, defaultValue, description) \ - struct name : public QgsSettingsEntryStringList \ - { name() : QgsSettingsEntryStringList( path, section, defaultValue, QObject::tr( description ) ) {} }; - -# define QGS_SETTING_ENTRY_BOOL(name, path, section, defaultValue, description) \ - struct name : public QgsSettingsEntryBool \ - { name() : QgsSettingsEntryBool( path, section, defaultValue, QObject::tr( description ) ) {} }; - -# define QGS_SETTING_ENTRY_INTEGER(name, path, section, defaultValue, description, minValue, maxValue) \ - struct name : public QgsSettingsEntryInteger \ - { name() : QgsSettingsEntryInteger( path, section, defaultValue, QObject::tr( description ), minValue, maxValue ) {} }; - -# define QGS_SETTING_ENTRY_DOUBLE(name, path, section, defaultValue, description, minValue, maxValue, displayDecimals) \ - struct name : public QgsSettingsEntryDouble \ - { name() : QgsSettingsEntryDouble( path, section, defaultValue, QObject::tr( description ), minValue, maxValue, displayDecimals ) {} }; - -# define QGS_SETTING_ENTRY_ENUM(name, path, section, defaultValue, description) \ - struct name : public QgsSettingsEntryEnum \ - { name() : QgsSettingsEntryEnum( path, section, defaultValue, QObject::tr( description ) ) {} }; - /** * \ingroup core * \class QgsSettingsEntry @@ -204,6 +180,11 @@ class CORE_EXPORT QgsSettingsEntry }; + +#define QGS_SETTING_ENTRY_STRING(name, path, section, defaultValue, ...) \ + struct name : public QgsSettingsEntryString \ + { name() : QgsSettingsEntryString( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + /** * \class QgsSettingsEntryString * \ingroup core @@ -256,6 +237,11 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry }; + +# define QGS_SETTING_ENTRY_STRINGLIST(name, path, section, defaultValue, ...) \ + struct name : public QgsSettingsEntryStringList \ + { name() : QgsSettingsEntryStringList( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + /** * \class QgsSettingsEntryStringList * \ingroup core @@ -288,6 +274,11 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry }; + +# define QGS_SETTING_ENTRY_BOOL(name, path, section, defaultValue, ...) \ + struct name : public QgsSettingsEntryBool \ + { name() : QgsSettingsEntryBool( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + /** * \class QgsSettingsEntryBool * \ingroup core @@ -320,6 +311,11 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry }; + +# define QGS_SETTING_ENTRY_INTEGER(name, path, section, defaultValue, ...) \ + struct name : public QgsSettingsEntryInteger \ + { name() : QgsSettingsEntryInteger( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + /** * \class QgsSettingsEntryInteger * \ingroup core @@ -371,6 +367,11 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry }; + +# define QGS_SETTING_ENTRY_DOUBLE(name, path, section, defaultValue, ...) \ + struct name : public QgsSettingsEntryDouble \ + { name() : QgsSettingsEntryDouble( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + /** * \class QgsSettingsEntryDouble * \ingroup core @@ -430,8 +431,13 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry }; + #ifndef SIP_RUN +# define QGS_SETTING_ENTRY_ENUM(name, path, section, defaultValue, ...) \ + struct name : public QgsSettingsEntryEnum \ + { name() : QgsSettingsEntryEnum( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + /** * \class QgsSettingsEntryEnum * \ingroup core @@ -453,7 +459,7 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntry */ template QgsSettingsEntryEnum( const QString &key, - QgsSettings::Section *section, + QgsSettings::Section section, const T &defaultValue, const QString &description = QString() ) : QgsSettingsEntry( key, From 2db776ec35df0670544b5ef3200ed22b255c89f3 Mon Sep 17 00:00:00 2001 From: Damiano Date: Mon, 15 Mar 2021 10:31:58 +0100 Subject: [PATCH 05/20] Fixes typos and banned keywords --- src/core/settings/qgssettingsentry.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 64cddcd4cb45..99ff91edf430 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -23,12 +23,16 @@ #include "qgis_sip.h" #include "qgssettings.h" +#define QGS_SETTING_ENTRY_VARIANT(name, path, section, defaultValue, ...) \ + struct name : public QgsSettingsEntry\ + { name() : QgsSettingsEntry( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + /** * \ingroup core * \class QgsSettingsEntry * * Represent settings entry and provides methods for reading and writing settings values. - * Different subclasses are provided for differents settings types with metainformations + * Different subclasses are provided for different settings types with metainformations * to validate set values and provide more accurate settings description for the gui. * * \since QGIS 3.20 @@ -204,7 +208,7 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. * The \a minLength argument specifies the minimal length of the string value. - * The \a maxLength argument specifies the maximal lenght of the string value. + * The \a maxLength argument specifies the maximal length of the string value. * By -1 the there is no limit */ QgsSettingsEntryString( const QString &key, @@ -341,8 +345,8 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry QgsSettings::Section section, qlonglong defaultValue = 0, const QString &description = QString(), - qlonglong minValue = -__LONG_LONG_MAX__ + 1, - qlonglong maxValue = __LONG_LONG_MAX__ ); + qlonglong minValue = std::numeric_limits::min(), + qlonglong maxValue = std::numeric_limits::max() ); //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; @@ -397,8 +401,8 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry QgsSettings::Section section, double defaultValue = 0.0, const QString &description = QString(), - double minValue = __DBL_MIN__, - double maxValue = __DBL_MAX__, + double minValue = std::numeric_limits::min(), + double maxValue = std::numeric_limits::max(), double displayDecimals = 1 ); //! \copydoc QgsSettingsEntry::setValue From 21c19aba9cc9d14897fd9150f0036b81df6706cb Mon Sep 17 00:00:00 2001 From: Damiano Date: Tue, 30 Mar 2021 17:08:50 +0200 Subject: [PATCH 06/20] static inline settings --- .../auto_generated/layout/qgslayout.sip.in | 8 + .../auto_generated/locator/qgslocator.sip.in | 5 + .../core/auto_generated/qgsapplication.sip.in | 7 + .../settings/qgssettingsentry.sip.in | 306 ++++++++++++++++++ .../settings/qgssettingsregistrycore.sip.in | 39 +++ python/core/core_auto.sip | 2 + src/app/locator/qgslocatoroptionswidget.cpp | 12 +- src/app/options/qgsoptions.cpp | 3 +- src/core/layout/qgslayout.h | 12 +- src/core/locator/qgslocator.cpp | 18 +- src/core/locator/qgslocator.h | 10 +- src/core/qgsapplication.cpp | 3 +- src/core/settings/qgssettingsentry.cpp | 268 +++++++-------- src/core/settings/qgssettingsentry.h | 260 +++++++-------- src/core/settings/qgssettingsregistrycore.cpp | 11 +- 15 files changed, 672 insertions(+), 292 deletions(-) create mode 100644 python/core/auto_generated/settings/qgssettingsentry.sip.in create mode 100644 python/core/auto_generated/settings/qgssettingsregistrycore.sip.in diff --git a/python/core/auto_generated/layout/qgslayout.sip.in b/python/core/auto_generated/layout/qgslayout.sip.in index cd416a440139..dee470e2146b 100644 --- a/python/core/auto_generated/layout/qgslayout.sip.in +++ b/python/core/auto_generated/layout/qgslayout.sip.in @@ -614,6 +614,14 @@ should be canceled. .. versionadded:: 3.10 %End + + struct Settings + { + static QgsSettingsEntryStringList searchPathForTemplates = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), "Search path for templates" ); + + + }; + public slots: void refresh(); diff --git a/python/core/auto_generated/locator/qgslocator.sip.in b/python/core/auto_generated/locator/qgslocator.sip.in index 65b99f05b808..e9225a8135d1 100644 --- a/python/core/auto_generated/locator/qgslocator.sip.in +++ b/python/core/auto_generated/locator/qgslocator.sip.in @@ -141,6 +141,11 @@ This list is updated when preparing the search .. versionadded:: 3.16 %End + struct Settings + { + + }; + signals: void foundResult( const QgsLocatorResult &result ); diff --git a/python/core/auto_generated/qgsapplication.sip.in b/python/core/auto_generated/qgsapplication.sip.in index 7f595dd2b2f7..830ac64ea32b 100644 --- a/python/core/auto_generated/qgsapplication.sip.in +++ b/python/core/auto_generated/qgsapplication.sip.in @@ -657,6 +657,13 @@ Returns the application's task manager, used for managing application wide background task handling. .. versionadded:: 3.0 +%End + + static QgsSettingsRegistryCore *settingsRegistryCore() /KeepReference/; +%Docstring +Returns the application's settings registry, used for managing application settings. + +.. versionadded:: 3.20 %End static QgsColorSchemeRegistry *colorSchemeRegistry() /KeepReference/; diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in new file mode 100644 index 000000000000..167277fa31f3 --- /dev/null +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -0,0 +1,306 @@ +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/settings/qgssettingsentry.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ + + + + + +class QgsSettingsEntry +{ +%Docstring(signature="appended") + +Represent settings entry and provides methods for reading and writing settings values. +Different subclasses are provided for different settings types with metainformations +to validate set values and provide more accurate settings description for the gui. + +.. versionadded:: 3.20 +%End + +%TypeHeaderCode +#include "qgssettingsentry.h" +%End +%ConvertToSubClassCode + if ( dynamic_cast< QgsSettingsEntryString * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryString; + if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryStringList; + else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryBool; +// else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) ) +// sipType = sipType_QgsSettingsEntryInteger; +// else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) ) +// sipType = sipType_QgsSettingsEntryDouble; + else + sipType = NULL; +%End + public: + + enum SettingsType + { + Variant, + String, + StringList, + Bool, + Integer, + Double, + Enum + }; + + QgsSettingsEntry( QString key, + QgsSettings::Section section, + QVariant defaultValue = QVariant(), + QString description = QString() ); +%Docstring +Constructor for QgsSettingsEntry. + +The ``key`` argument specifies the key of the settings. +The ``section`` argument specifies the section of the settings. +The ``default`` value argument specifies the default value for the settings entry. +The ``description`` argument specifies a description for the settings entry. +%End + + virtual ~QgsSettingsEntry(); + + QString key( const QString &dynamicKeyPart = QString() ) const; +%Docstring +Get settings entry key. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool hasDynamicKey() const; +%Docstring +Returns true if a part of the settings key is built dynamically. +%End + + bool exists( const QString &dynamicKeyPart = QString() ) const; +%Docstring +Returns true if the settings is contained in the underlying QSettings. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + void remove( const QString &dynamicKeyPart = QString() ) const; +%Docstring +Removes the settings from the underlying QSettings. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QgsSettings::Section section() const; +%Docstring +Get settings section. The settings section of the parent group is returned if available. +%End + + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); +%Docstring +Set settings value. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QVariant valueFromPython() const /PyName=value/; +%Docstring +Get settings value. +%End + + + QVariant defaultValueFromPython() const /PyName=defaultValue/; +%Docstring +Get settings default value. +%End + + + virtual SettingsType settingsType() const; +%Docstring +Get the settings entry type. +%End + + QString description() const; +%Docstring +Get the settings entry description. +%End + +}; + + + +class QgsSettingsEntryString : QgsSettingsEntry +{ +%Docstring(signature="appended") +A string settings entry. + +.. versionadded:: 3.20 +%End + +%TypeHeaderCode +#include "qgssettingsentry.h" +%End + public: + + QgsSettingsEntryString( const QString &key, + QgsSettings::Section section, + const QString &defaultValue = QString(), + const QString &description = QString(), + int minLength = 0, + int maxLength = -1 ); +%Docstring +Constructor for QgsSettingsEntryString. + +The ``key`` argument specifies the final part of the settings key. +The ``parentGroup`` argument specifies a parent group which is used to rebuild +the entiere settings key and to determine the settings section. +The ``default`` value argument specifies the default value for the settings entry. +The ``description`` argument specifies a description for the settings entry. +The ``minLength`` argument specifies the minimal length of the string value. +The ``maxLength`` argument specifies the maximal length of the string value. +By -1 the there is no limit +%End + + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); + +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + virtual SettingsType settingsType() const; +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.settingsType +%End + + int minLength(); +%Docstring +Returns the string minimum length. +%End + + int maxLength(); +%Docstring +Returns the string maximum length. By -1 there is no limitation. +%End + +}; + + + +class QgsSettingsEntryStringList : QgsSettingsEntry +{ +%Docstring(signature="appended") +A string list settings entry. + +.. versionadded:: 3.20 +%End + +%TypeHeaderCode +#include "qgssettingsentry.h" +%End + public: + + QgsSettingsEntryStringList( const QString &key, + QgsSettings::Section section, + const QStringList &defaultValue = QStringList(), + const QString &description = QString() ); +%Docstring +Constructor for QgsSettingsEntryStringList. + +The ``key`` argument specifies the final part of the settings key. +The ``parentGroup`` argument specifies a parent group which is used to rebuild +the entiere settings key and to determine the settings section. +The ``default`` value argument specifies the default value for the settings entry. +The ``description`` argument specifies a description for the settings entry. +%End + + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); + +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + virtual SettingsType settingsType() const; +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.settingsType +%End + +}; + + + +class QgsSettingsEntryBool : QgsSettingsEntry +{ +%Docstring(signature="appended") +A boolean settings entry. + +.. versionadded:: 3.20 +%End + +%TypeHeaderCode +#include "qgssettingsentry.h" +%End + public: + + QgsSettingsEntryBool( const QString &key, + QgsSettings::Section section, + bool defaultValue = false, + const QString &description = QString() ); +%Docstring +Constructor for QgsSettingsEntryBool. + +The ``key`` argument specifies the final part of the settings key. +The ``parentGroup`` argument specifies a parent group which is used to rebuild +the entiere settings key and to determine the settings section. +The ``default`` value argument specifies the default value for the settings entry. +The ``description`` argument specifies a description for the settings entry. +%End + + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); + +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + virtual SettingsType settingsType() const; +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.settingsType +%End + +}; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/settings/qgssettingsentry.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ diff --git a/python/core/auto_generated/settings/qgssettingsregistrycore.sip.in b/python/core/auto_generated/settings/qgssettingsregistrycore.sip.in new file mode 100644 index 000000000000..531e9332b0c8 --- /dev/null +++ b/python/core/auto_generated/settings/qgssettingsregistrycore.sip.in @@ -0,0 +1,39 @@ +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/settings/qgssettingsregistrycore.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ + + + + + + + +class QgsSettingsRegistryCore +{ +%Docstring(signature="appended") + + +.. versionadded:: 3.20 +%End + +%TypeHeaderCode +#include "qgssettingsregistrycore.h" +%End + public: + + QgsSettingsRegistryCore(); + ~QgsSettingsRegistryCore(); + +}; + +/************************************************************************ + * This file has been generated automatically from * + * * + * src/core/settings/qgssettingsregistrycore.h * + * * + * Do not edit manually ! Edit header and run scripts/sipify.pl again * + ************************************************************************/ diff --git a/python/core/core_auto.sip b/python/core/core_auto.sip index caed1f41264f..f318b2fbe541 100644 --- a/python/core/core_auto.sip +++ b/python/core/core_auto.sip @@ -602,6 +602,8 @@ %Include auto_generated/textrenderer/qgstextrenderer.sip %Include auto_generated/textrenderer/qgstextrendererutils.sip %Include auto_generated/textrenderer/qgstextshadowsettings.sip +%Include auto_generated/settings/qgssettingsentry.sip +%Include auto_generated/settings/qgssettingsregistrycore.sip %Include auto_generated/validity/qgsabstractvaliditycheck.sip %Include auto_generated/validity/qgsvaliditycheckcontext.sip %Include auto_generated/validity/qgsvaliditycheckregistry.sip diff --git a/src/app/locator/qgslocatoroptionswidget.cpp b/src/app/locator/qgslocatoroptionswidget.cpp index 66a079732f8d..4ae17f2cf233 100644 --- a/src/app/locator/qgslocatoroptionswidget.cpp +++ b/src/app/locator/qgslocatoroptionswidget.cpp @@ -330,26 +330,30 @@ void QgsLocatorFiltersModel::commitChanges() if ( !activePrefix.isEmpty() && activePrefix != filter->prefix() ) { filter->setActivePrefix( activePrefix ); - QgsLocator::Settings::LocatorFilterPrefix().setValue( activePrefix, filter->name() ); +// QgsLocator::Settings::LocatorFilterPrefix().setValue( activePrefix, filter->name() ); +// QgsLocator::Settings::locatorFilterPrefix.setValue( activePrefix, filter->name() ); } else { filter->setActivePrefix( QString() ); - QgsLocator::Settings::LocatorFilterPrefix().remove( filter->name() ); +// QgsLocator::Settings::LocatorFilterPrefix().remove( filter->name() ); +// QgsLocator::Settings::locatorFilterPrefix.remove( filter->name() ); } } QHash< QgsLocatorFilter *, bool >::const_iterator it = mEnabledChanges.constBegin(); for ( ; it != mEnabledChanges.constEnd(); ++it ) { QgsLocatorFilter *filter = it.key(); - QgsLocator::Settings::LocatorFilterEnabled().setValue( it.value(), filter->name() ); +// QgsLocator::Settings::LocatorFilterEnabled().setValue( it.value(), filter->name() ); +// QgsLocator::Settings::locatorFilterEnabled.setValue( it.value(), filter->name() ); filter->setEnabled( it.value() ); } it = mDefaultChanges.constBegin(); for ( ; it != mDefaultChanges.constEnd(); ++it ) { QgsLocatorFilter *filter = it.key(); - QgsLocator::Settings::LocatorFilterDefault().setValue( it.value(), filter->name() ); +// QgsLocator::Settings::LocatorFilterDefault().setValue( it.value(), filter->name() ); +// QgsLocator::Settings::locatorFilterDefault.setValue( it.value(), filter->name() ); filter->setUseWithoutPrefix( it.value() ); } } diff --git a/src/app/options/qgsoptions.cpp b/src/app/options/qgsoptions.cpp index 7017da5fcf73..3d07ff8bc562 100644 --- a/src/app/options/qgsoptions.cpp +++ b/src/app/options/qgsoptions.cpp @@ -1481,7 +1481,8 @@ void QgsOptions::saveOptions() { pathsList << mListComposerTemplatePaths->item( i )->text(); } - QgsLayout::Settings::SearchPathForTemplates().setValue( pathsList ); +// QgsLayout::Settings::searchPathForTemplates.setValue( pathsList ); +// QgsLayout::Settings::SearchPathForTemplates().setValue( pathsList ); pathsList.clear(); for ( int r = 0; r < mLocalizedDataPathListWidget->count(); r++ ) diff --git a/src/core/layout/qgslayout.h b/src/core/layout/qgslayout.h index d0b2e7661254..9d694faddc33 100644 --- a/src/core/layout/qgslayout.h +++ b/src/core/layout/qgslayout.h @@ -658,9 +658,19 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext */ bool accept( QgsStyleEntityVisitorInterface *visitor ) const; + struct Settings { - QGS_SETTING_ENTRY_STRINGLIST( SearchPathForTemplates, QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), tr( "Search path for templates" ) ) + static inline QgsSettingsEntryStringList searchPathForTemplates = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), "Search path for templates" ); + + // QGS_SETTING_ENTRY_STRINGLIST( SearchPathForTemplates, QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), tr( "Search path for templates" ) ) + +// struct SearchPathForTemplates : public QgsSettingsEntryStringList +// { +// SearchPathForTemplates() +// : QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), tr( "Search path for templates" ) ) +// {} +// }; }; public slots: diff --git a/src/core/locator/qgslocator.cpp b/src/core/locator/qgslocator.cpp index c61819e75b2d..abb495537346 100644 --- a/src/core/locator/qgslocator.cpp +++ b/src/core/locator/qgslocator.cpp @@ -95,14 +95,20 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter ) // restore settings bool enabled = true; - if ( QgsLocator::Settings::LocatorFilterEnabled().exists( filter->name() ) ) - enabled = QgsLocator::Settings::LocatorFilterEnabled().value( filter->name() ); +// if ( QgsLocator::Settings::LocatorFilterEnabled().exists( filter->name() ) ) +// enabled = QgsLocator::Settings::LocatorFilterEnabled().value( filter->name() ); +// if ( QgsLocator::Settings::locatorFilterEnabled.exists( filter->name() ) ) +// enabled = QgsLocator::Settings::locatorFilterEnabled.value( filter->name() ); bool byDefault = filter->useWithoutPrefix(); - if ( QgsLocator::Settings::LocatorFilterDefault().exists( filter->name() ) ) - byDefault = QgsLocator::Settings::LocatorFilterDefault().value( filter->name() ); +// if ( QgsLocator::Settings::LocatorFilterDefault().exists( filter->name() ) ) +// byDefault = QgsLocator::Settings::LocatorFilterDefault().value( filter->name() ); +// if ( QgsLocator::Settings::locatorFilterDefault.exists( filter->name() ) ) +// byDefault = QgsLocator::Settings::locatorFilterDefault.value( filter->name() ); QString prefix = filter->prefix(); - if ( QgsLocator::Settings::LocatorFilterPrefix().exists( filter->name() ) ) - prefix = QgsLocator::Settings::LocatorFilterPrefix().value( filter->name() ); +// if ( QgsLocator::Settings::LocatorFilterPrefix().exists( filter->name() ) ) +// prefix = QgsLocator::Settings::LocatorFilterPrefix().value( filter->name() ); +// if ( QgsLocator::Settings::locatorFilterPrefix.exists( filter->name() ) ) +// prefix = QgsLocator::Settings::locatorFilterPrefix.value( filter->name() ); if ( prefix.isEmpty() ) { prefix = filter->prefix(); diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index 640906fd5c5b..47e8237aeed0 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -155,9 +155,13 @@ class CORE_EXPORT QgsLocator : public QObject struct Settings { - QGS_SETTING_ENTRY_BOOL( LocatorFilterEnabled, QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, tr( "Enabled" ) ) - QGS_SETTING_ENTRY_BOOL( LocatorFilterDefault, QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, tr( "Default value" ) ) - QGS_SETTING_ENTRY_STRING( LocatorFilterPrefix, QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), tr( "Locator filter prefix" ) ) +// static inline QgsSettingsEntryBool locatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); +// static inline QgsSettingsEntryBool locatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); +// static inline QgsSettingsEntryString locatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); + +// QGS_SETTING_ENTRY_BOOL( LocatorFilterEnabled, QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, tr( "Enabled" ) ) +// QGS_SETTING_ENTRY_BOOL( LocatorFilterDefault, QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, tr( "Default value" ) ) +// QGS_SETTING_ENTRY_STRING( LocatorFilterPrefix, QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), tr( "Locator filter prefix" ) ) }; signals: diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index 0a750be59f07..20bbee2737fb 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -1107,7 +1107,8 @@ QStringList QgsApplication::layoutTemplatePaths() { //local directories to search when looking for an template with a given basename //defined by user in options dialog - return QgsLayout::Settings::SearchPathForTemplates().value(); + return QgsLayout::Settings::searchPathForTemplates.value(); +// return QgsLayout::Settings::SearchPathForTemplates().value(); } QMap QgsApplication::systemEnvVars() diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index dc36173d4263..a7de6283b156 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -229,140 +229,140 @@ QgsSettingsEntry::SettingsType QgsSettingsEntryBool::settingsType() const return QgsSettingsEntry::Bool; } -QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, - QgsSettings::Section section, - qlonglong defaultValue, - const QString &description, - qlonglong minValue, - qlonglong maxValue ) - : QgsSettingsEntry( key, - section, - defaultValue, - description ) - , mMinValue( minValue ) - , mMaxValue( maxValue ) -{ - -} - -bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dynamicKeyPart ) -{ - if ( value.canConvert() == false ) - { - QgsDebugMsg( QObject::tr( "Can't convert value '%1' to qlonglong for settings with key '%2'" ) - .arg( value.toString() ) - .arg( QgsSettingsEntry::key() ) ); - return false; - } - - qlonglong valueLongLong = value.toLongLong(); - if ( valueLongLong < mMinValue ) - { - QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) - .arg( QgsSettingsEntry::key() ) - .arg( valueLongLong ) - .arg( mMinValue ) ); - return false; - } - - if ( valueLongLong > mMaxValue ) - { - QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) - .arg( QgsSettingsEntry::key() ) - .arg( valueLongLong ) - .arg( mMinValue ) ); - return false; - } - - QgsSettingsEntry::setValue( value, dynamicKeyPart ); - return true; -} - -QgsSettingsEntry::SettingsType QgsSettingsEntryInteger::settingsType() const -{ - return QgsSettingsEntry::Integer; -} - -qlonglong QgsSettingsEntryInteger::minValue() -{ - return mMinValue; -} - -qlonglong QgsSettingsEntryInteger::maxValue() -{ - return mMaxValue; -} - -QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, - QgsSettings::Section section, - double defaultValue, - const QString &description, - double minValue, - double maxValue, - double displayDecimals ) - : QgsSettingsEntry( key, - section, - defaultValue, - description ) - , mMinValue( minValue ) - , mMaxValue( maxValue ) - , mDisplayHintDecimals( displayDecimals ) -{ - -} - -bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dynamicKeyPart ) -{ - if ( value.canConvert() == false ) - { - QgsDebugMsg( QObject::tr( "Can't convert value '%1' to double for settings with key '%2'" ) - .arg( value.toString() ) - .arg( QgsSettingsEntry::key() ) ); - return false; - } - - double valueDouble = value.toDouble(); - if ( valueDouble < mMinValue ) - { - QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) - .arg( QgsSettingsEntry::key() ) - .arg( valueDouble ) - .arg( mMinValue ) ); - return false; - } - - if ( valueDouble > mMaxValue ) - { - QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) - .arg( QgsSettingsEntry::key() ) - .arg( valueDouble ) - .arg( mMinValue ) ); - return false; - } - - QgsSettingsEntry::setValue( value, dynamicKeyPart ); - return true; -} - -QgsSettingsEntry::SettingsType QgsSettingsEntryDouble::settingsType() const -{ - return QgsSettingsEntry::Double; -} - -double QgsSettingsEntryDouble::minValue() const -{ - return mMinValue; -} - -double QgsSettingsEntryDouble::maxValue() const -{ - return mMaxValue; -} - -int QgsSettingsEntryDouble::displayHintDecimals() const -{ - return mDisplayHintDecimals; -} +//QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, +// QgsSettings::Section section, +// qlonglong defaultValue, +// const QString &description, +// qlonglong minValue, +// qlonglong maxValue ) +// : QgsSettingsEntry( key, +// section, +// defaultValue, +// description ) +// , mMinValue( minValue ) +// , mMaxValue( maxValue ) +//{ + +//} + +//bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dynamicKeyPart ) +//{ +// if ( value.canConvert() == false ) +// { +// QgsDebugMsg( QObject::tr( "Can't convert value '%1' to qlonglong for settings with key '%2'" ) +// .arg( value.toString() ) +// .arg( QgsSettingsEntry::key() ) ); +// return false; +// } + +// qlonglong valueLongLong = value.toLongLong(); +// if ( valueLongLong < mMinValue ) +// { +// QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) +// .arg( QgsSettingsEntry::key() ) +// .arg( valueLongLong ) +// .arg( mMinValue ) ); +// return false; +// } + +// if ( valueLongLong > mMaxValue ) +// { +// QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) +// .arg( QgsSettingsEntry::key() ) +// .arg( valueLongLong ) +// .arg( mMinValue ) ); +// return false; +// } + +// QgsSettingsEntry::setValue( value, dynamicKeyPart ); +// return true; +//} + +//QgsSettingsEntry::SettingsType QgsSettingsEntryInteger::settingsType() const +//{ +// return QgsSettingsEntry::Integer; +//} + +//qlonglong QgsSettingsEntryInteger::minValue() +//{ +// return mMinValue; +//} + +//qlonglong QgsSettingsEntryInteger::maxValue() +//{ +// return mMaxValue; +//} + +//QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, +// QgsSettings::Section section, +// double defaultValue, +// const QString &description, +// double minValue, +// double maxValue, +// double displayDecimals ) +// : QgsSettingsEntry( key, +// section, +// defaultValue, +// description ) +// , mMinValue( minValue ) +// , mMaxValue( maxValue ) +// , mDisplayHintDecimals( displayDecimals ) +//{ + +//} + +//bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dynamicKeyPart ) +//{ +// if ( value.canConvert() == false ) +// { +// QgsDebugMsg( QObject::tr( "Can't convert value '%1' to double for settings with key '%2'" ) +// .arg( value.toString() ) +// .arg( QgsSettingsEntry::key() ) ); +// return false; +// } + +// double valueDouble = value.toDouble(); +// if ( valueDouble < mMinValue ) +// { +// QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) +// .arg( QgsSettingsEntry::key() ) +// .arg( valueDouble ) +// .arg( mMinValue ) ); +// return false; +// } + +// if ( valueDouble > mMaxValue ) +// { +// QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) +// .arg( QgsSettingsEntry::key() ) +// .arg( valueDouble ) +// .arg( mMinValue ) ); +// return false; +// } + +// QgsSettingsEntry::setValue( value, dynamicKeyPart ); +// return true; +//} + +//QgsSettingsEntry::SettingsType QgsSettingsEntryDouble::settingsType() const +//{ +// return QgsSettingsEntry::Double; +//} + +//double QgsSettingsEntryDouble::minValue() const +//{ +// return mMinValue; +//} + +//double QgsSettingsEntryDouble::maxValue() const +//{ +// return mMaxValue; +//} + +//int QgsSettingsEntryDouble::displayHintDecimals() const +//{ +// return mDisplayHintDecimals; +//} bool QgsSettingsEntryEnum::setValue( const QVariant &value, const QString &dynamicKeyPart ) { diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 99ff91edf430..f0c607d7138b 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -23,9 +23,7 @@ #include "qgis_sip.h" #include "qgssettings.h" -#define QGS_SETTING_ENTRY_VARIANT(name, path, section, defaultValue, ...) \ - struct name : public QgsSettingsEntry\ - { name() : QgsSettingsEntry( path, section, defaultValue, ##__VA_ARGS__ ) {} }; +#define QGS_SETTING_ENTRY_VARIANT(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntry { name() : QgsSettingsEntry( path, section, defaultValue, ##__VA_ARGS__ ) {} }; /** * \ingroup core @@ -48,10 +46,10 @@ class CORE_EXPORT QgsSettingsEntry sipType = sipType_QgsSettingsEntryStringList; else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryBool; - else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) ) - sipType = sipType_QgsSettingsEntryInteger; - else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) ) - sipType = sipType_QgsSettingsEntryDouble; +// else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) ) +// sipType = sipType_QgsSettingsEntryInteger; +// else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) ) +// sipType = sipType_QgsSettingsEntryDouble; else sipType = NULL; SIP_END @@ -185,9 +183,7 @@ class CORE_EXPORT QgsSettingsEntry }; -#define QGS_SETTING_ENTRY_STRING(name, path, section, defaultValue, ...) \ - struct name : public QgsSettingsEntryString \ - { name() : QgsSettingsEntryString( path, section, defaultValue, ##__VA_ARGS__ ) {} }; +#define QGS_SETTING_ENTRY_STRING(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryString { name() : QgsSettingsEntryString( path, section, defaultValue, ##__VA_ARGS__ ) {} }; /** * \class QgsSettingsEntryString @@ -242,9 +238,7 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry }; -# define QGS_SETTING_ENTRY_STRINGLIST(name, path, section, defaultValue, ...) \ - struct name : public QgsSettingsEntryStringList \ - { name() : QgsSettingsEntryStringList( path, section, defaultValue, ##__VA_ARGS__ ) {} }; +#define QGS_SETTING_ENTRY_STRINGLIST(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryStringList { name() : QgsSettingsEntryStringList( path, section, defaultValue, ##__VA_ARGS__ ) {} }; /** * \class QgsSettingsEntryStringList @@ -279,9 +273,7 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry }; -# define QGS_SETTING_ENTRY_BOOL(name, path, section, defaultValue, ...) \ - struct name : public QgsSettingsEntryBool \ - { name() : QgsSettingsEntryBool( path, section, defaultValue, ##__VA_ARGS__ ) {} }; +#define QGS_SETTING_ENTRY_BOOL(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryBool { name() : QgsSettingsEntryBool( path, section, defaultValue, ##__VA_ARGS__ ) {} }; /** * \class QgsSettingsEntryBool @@ -316,131 +308,125 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry }; -# define QGS_SETTING_ENTRY_INTEGER(name, path, section, defaultValue, ...) \ - struct name : public QgsSettingsEntryInteger \ - { name() : QgsSettingsEntryInteger( path, section, defaultValue, ##__VA_ARGS__ ) {} }; - -/** - * \class QgsSettingsEntryInteger - * \ingroup core - * An integer settings entry. - * \since QGIS 3.20 - */ -class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry -{ - public: - - /** - * Constructor for QgsSettingsEntryInteger. - * - * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. - * The \a default value argument specifies the default value for the settings entry. - * The \a description argument specifies a description for the settings entry. - * The \a minValue argument specifies the minimal value. - * The \a maxValue argument specifies the maximal value. - */ - QgsSettingsEntryInteger( const QString &key, - QgsSettings::Section section, - qlonglong defaultValue = 0, - const QString &description = QString(), - qlonglong minValue = std::numeric_limits::min(), - qlonglong maxValue = std::numeric_limits::max() ); - - //! \copydoc QgsSettingsEntry::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; - - //! \copydoc QgsSettingsEntry::settingsType - virtual SettingsType settingsType() const override; - - /** - * Returns the minimum value. - */ - qlonglong minValue(); - - /** - * Returns the maximum value. - */ - qlonglong maxValue(); - - private: - - qlonglong mMinValue; - qlonglong mMaxValue; - -}; - - -# define QGS_SETTING_ENTRY_DOUBLE(name, path, section, defaultValue, ...) \ - struct name : public QgsSettingsEntryDouble \ - { name() : QgsSettingsEntryDouble( path, section, defaultValue, ##__VA_ARGS__ ) {} }; - -/** - * \class QgsSettingsEntryDouble - * \ingroup core - * A double settings entry. - * \since QGIS 3.20 - */ -class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry -{ - public: - - /** - * Constructor for QgsSettingsEntryDouble. - * - * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. - * The \a default value argument specifies the default value for the settings entry. - * The \a description argument specifies a description for the settings entry. - * The \a minValue argument specifies the minimal value. - * The \a maxValue argument specifies the maximal value. - */ - QgsSettingsEntryDouble( const QString &key, - QgsSettings::Section section, - double defaultValue = 0.0, - const QString &description = QString(), - double minValue = std::numeric_limits::min(), - double maxValue = std::numeric_limits::max(), - double displayDecimals = 1 ); - - //! \copydoc QgsSettingsEntry::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; - - //! \copydoc QgsSettingsEntry::settingsType - virtual SettingsType settingsType() const override; - - /** - * Returns the minimum value. - */ - double minValue() const; - - /** - * Returns the maximum value. - */ - double maxValue() const; - - /** - * Returns how much decimals should be shown in the Gui. - */ - int displayHintDecimals() const; - - private: - - double mMinValue; - double mMaxValue; - - int mDisplayHintDecimals; - -}; +//#define QGS_SETTING_ENTRY_INTEGER(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryInteger { name() : QgsSettingsEntryInteger( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + +///** +// * \class QgsSettingsEntryInteger +// * \ingroup core +// * An integer settings entry. +// * \since QGIS 3.20 +// */ +//class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry +//{ +// public: + +// /** +// * Constructor for QgsSettingsEntryInteger. +// * +// * The \a key argument specifies the final part of the settings key. +// * The \a parentGroup argument specifies a parent group which is used to rebuild +// * the entiere settings key and to determine the settings section. +// * The \a default value argument specifies the default value for the settings entry. +// * The \a description argument specifies a description for the settings entry. +// * The \a minValue argument specifies the minimal value. +// * The \a maxValue argument specifies the maximal value. +// */ +// QgsSettingsEntryInteger( const QString &key, +// QgsSettings::Section section, +// qlonglong defaultValue = 0, +// const QString &description = QString(), +// qlonglong minValue = std::numeric_limits::min(), +// qlonglong maxValue = std::numeric_limits::max() ); + +// //! \copydoc QgsSettingsEntry::setValue +// bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + +// //! \copydoc QgsSettingsEntry::settingsType +// virtual SettingsType settingsType() const override; + +// /** +// * Returns the minimum value. +// */ +// qlonglong minValue(); + +// /** +// * Returns the maximum value. +// */ +// qlonglong maxValue(); + +// private: + +// qlonglong mMinValue; +// qlonglong mMaxValue; + +//}; + + +//#define QGS_SETTING_ENTRY_DOUBLE(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryDouble { name() : QgsSettingsEntryDouble( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + +///** +// * \class QgsSettingsEntryDouble +// * \ingroup core +// * A double settings entry. +// * \since QGIS 3.20 +// */ +//class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry +//{ +// public: + +// /** +// * Constructor for QgsSettingsEntryDouble. +// * +// * The \a key argument specifies the final part of the settings key. +// * The \a parentGroup argument specifies a parent group which is used to rebuild +// * the entiere settings key and to determine the settings section. +// * The \a default value argument specifies the default value for the settings entry. +// * The \a description argument specifies a description for the settings entry. +// * The \a minValue argument specifies the minimal value. +// * The \a maxValue argument specifies the maximal value. +// */ +// QgsSettingsEntryDouble( const QString &key, +// QgsSettings::Section section, +// double defaultValue = 0.0, +// const QString &description = QString(), +// double minValue = std::numeric_limits::min(), +// double maxValue = std::numeric_limits::max(), +// double displayDecimals = 1 ); + +// //! \copydoc QgsSettingsEntry::setValue +// bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + +// //! \copydoc QgsSettingsEntry::settingsType +// virtual SettingsType settingsType() const override; + +// /** +// * Returns the minimum value. +// */ +// double minValue() const; + +// /** +// * Returns the maximum value. +// */ +// double maxValue() const; + +// /** +// * Returns how much decimals should be shown in the Gui. +// */ +// int displayHintDecimals() const; + +// private: + +// double mMinValue; +// double mMaxValue; + +// int mDisplayHintDecimals; + +//}; #ifndef SIP_RUN -# define QGS_SETTING_ENTRY_ENUM(name, path, section, defaultValue, ...) \ - struct name : public QgsSettingsEntryEnum \ - { name() : QgsSettingsEntryEnum( path, section, defaultValue, ##__VA_ARGS__ ) {} }; +#define QGS_SETTING_ENTRY_ENUM(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryEnum { name() : QgsSettingsEntryEnum( path, section, defaultValue, ##__VA_ARGS__ ) {} }; /** * \class QgsSettingsEntryEnum diff --git a/src/core/settings/qgssettingsregistrycore.cpp b/src/core/settings/qgssettingsregistrycore.cpp index d874212c9827..ec6601901a09 100644 --- a/src/core/settings/qgssettingsregistrycore.cpp +++ b/src/core/settings/qgssettingsregistrycore.cpp @@ -21,16 +21,17 @@ QgsSettingsRegistryCore::QgsSettingsRegistryCore() : mSettingsEntries() { - mSettingsEntries.append( new QgsLayout::Settings::SearchPathForTemplates() ); + mSettingsEntries.append( &QgsLayout::Settings::searchPathForTemplates ); +// mSettingsEntries.append( new QgsLayout::Settings::SearchPathForTemplates() ); - mSettingsEntries.append( new QgsLocator::Settings::LocatorFilterEnabled() ); - mSettingsEntries.append( new QgsLocator::Settings::LocatorFilterDefault() ); - mSettingsEntries.append( new QgsLocator::Settings::LocatorFilterPrefix() ); +// mSettingsEntries.append( &QgsLocator::Settings::locatorFilterEnabled ); +// mSettingsEntries.append( &QgsLocator::Settings::locatorFilterDefault ); +// mSettingsEntries.append( &QgsLocator::Settings::locatorFilterPrefix ); } QgsSettingsRegistryCore::~QgsSettingsRegistryCore() { - qDeleteAll( mSettingsEntries ); +// qDeleteAll( mSettingsEntries ); } From cee0e310f8da7ca94164dff6cd98df674bbba099 Mon Sep 17 00:00:00 2001 From: Damiano Date: Wed, 31 Mar 2021 14:04:44 +0200 Subject: [PATCH 07/20] Sip part running --- .../auto_generated/layout/qgslayout.sip.in | 4 +- .../auto_generated/locator/qgslocator.sip.in | 4 +- .../settings/qgssettingsentry.sip.in | 143 +++++++- src/core/layout/qgslayout.h | 11 +- src/core/locator/qgslocator.cpp | 18 +- src/core/locator/qgslocator.h | 10 +- src/core/qgsapplication.cpp | 1 - src/core/settings/qgssettingsentry.cpp | 291 +++++++++------- src/core/settings/qgssettingsentry.h | 327 +++++++++++------- src/core/settings/qgssettingsregistrycore.cpp | 8 +- src/core/settings/qgssettingsregistrycore.h | 2 +- 11 files changed, 508 insertions(+), 311 deletions(-) diff --git a/python/core/auto_generated/layout/qgslayout.sip.in b/python/core/auto_generated/layout/qgslayout.sip.in index dee470e2146b..5ae6316c3b2c 100644 --- a/python/core/auto_generated/layout/qgslayout.sip.in +++ b/python/core/auto_generated/layout/qgslayout.sip.in @@ -617,9 +617,7 @@ should be canceled. struct Settings { - static QgsSettingsEntryStringList searchPathForTemplates = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), "Search path for templates" ); - - + static const QgsSettingsEntryStringList searchPathForTemplates; // = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), QObject::tr( "Search path for templates" ) ); }; public slots: diff --git a/python/core/auto_generated/locator/qgslocator.sip.in b/python/core/auto_generated/locator/qgslocator.sip.in index e9225a8135d1..ee1ecc8e0250 100644 --- a/python/core/auto_generated/locator/qgslocator.sip.in +++ b/python/core/auto_generated/locator/qgslocator.sip.in @@ -143,7 +143,9 @@ This list is updated when preparing the search struct Settings { - + static QgsSettingsEntryBool locatorFilterEnabled; // = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); + static QgsSettingsEntryBool locatorFilterDefault; // = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); + static QgsSettingsEntryString locatorFilterPrefix; // = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); }; signals: diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index 167277fa31f3..9ca9c3668341 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -9,7 +9,6 @@ - class QgsSettingsEntry { %Docstring(signature="appended") @@ -31,10 +30,10 @@ to validate set values and provide more accurate settings description for the gu sipType = sipType_QgsSettingsEntryStringList; else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryBool; -// else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) ) -// sipType = sipType_QgsSettingsEntryInteger; -// else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) ) -// sipType = sipType_QgsSettingsEntryDouble; + else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryInteger; + else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryDouble; else sipType = NULL; %End @@ -97,7 +96,7 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Get settings section. The settings section of the parent group is returned if available. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring Set settings value. @@ -129,7 +128,6 @@ Get the settings entry description. }; - class QgsSettingsEntryString : QgsSettingsEntry { %Docstring(signature="appended") @@ -162,7 +160,7 @@ The ``maxLength`` argument specifies the maximal length of the string value. By -1 the there is no limit %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring \copydoc :py:class:`QgsSettingsEntry`.setValue @@ -186,7 +184,6 @@ Returns the string maximum length. By -1 there is no limitation. }; - class QgsSettingsEntryStringList : QgsSettingsEntry { %Docstring(signature="appended") @@ -214,7 +211,7 @@ The ``default`` value argument specifies the default value for the settings entr The ``description`` argument specifies a description for the settings entry. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring \copydoc :py:class:`QgsSettingsEntry`.setValue @@ -228,7 +225,6 @@ The ``description`` argument specifies a description for the settings entry. }; - class QgsSettingsEntryBool : QgsSettingsEntry { %Docstring(signature="appended") @@ -256,7 +252,7 @@ The ``default`` value argument specifies the default value for the settings entr The ``description`` argument specifies a description for the settings entry. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring \copydoc :py:class:`QgsSettingsEntry`.setValue @@ -270,28 +266,151 @@ The ``description`` argument specifies a description for the settings entry. }; +class QgsSettingsEntryInteger : QgsSettingsEntry +{ +%Docstring(signature="appended") +An integer settings entry. +.. versionadded:: 3.20 +%End + +%TypeHeaderCode +#include "qgssettingsentry.h" +%End + public: + QgsSettingsEntryInteger( const QString &key, + QgsSettings::Section section, + qlonglong defaultValue = 0, + const QString &description = QString() ); +%Docstring +Constructor for QgsSettingsEntryInteger. +The ``key`` argument specifies the final part of the settings key. +The ``parentGroup`` argument specifies a parent group which is used to rebuild +the entiere settings key and to determine the settings section. +The ``default`` value argument specifies the default value for the settings entry. +The ``description`` argument specifies a description for the settings entry. +%End + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.setValue +%End + virtual SettingsType settingsType() const; +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.settingsType +%End + void setMinValue( qlonglong minValue ); +%Docstring +Set the minimum value. +minValue The minimum value. +%End + qlonglong minValue(); +%Docstring +Returns the minimum value. +%End + void setMaxValue( qlonglong maxValue ); +%Docstring +Set the maximum value. +maxValue The maximum value. +%End + qlonglong maxValue(); +%Docstring +Returns the maximum value. +%End +}; +class QgsSettingsEntryDouble : QgsSettingsEntry +{ +%Docstring(signature="appended") +A double settings entry. +.. versionadded:: 3.20 +%End +%TypeHeaderCode +#include "qgssettingsentry.h" +%End + public: + QgsSettingsEntryDouble( const QString &key, + QgsSettings::Section section, + double defaultValue = 0.0, + const QString &description = QString() ); +%Docstring +Constructor for QgsSettingsEntryDouble. + +The ``key`` argument specifies the final part of the settings key. +The ``parentGroup`` argument specifies a parent group which is used to rebuild +the entiere settings key and to determine the settings section. +The ``default`` value argument specifies the default value for the settings entry. +The ``description`` argument specifies a description for the settings entry. +%End + + + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; + +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + virtual SettingsType settingsType() const; +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.settingsType +%End + + void setMinValue( double minValue ); +%Docstring +Set the minimum value. + +minValue The minimum value. +%End + + double minValue() const; +%Docstring +Returns the minimum value. +%End + + void setMaxValue( double maxValue ); +%Docstring +Set the maximum value. + +maxValue The maximum value. +%End + + double maxValue() const; +%Docstring +Returns the maximum value. +%End + + void setDisplayHintDecimals( int displayHintDecimals ); +%Docstring +Set the display hint decimals. + +displayHintDecimals The number of decimals that should be shown in the Gui. +%End + + int displayHintDecimals() const; +%Docstring +Returns how much decimals should be shown in the Gui. +%End + +}; diff --git a/src/core/layout/qgslayout.h b/src/core/layout/qgslayout.h index 9d694faddc33..ffb82b05fd6e 100644 --- a/src/core/layout/qgslayout.h +++ b/src/core/layout/qgslayout.h @@ -661,16 +661,7 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext struct Settings { - static inline QgsSettingsEntryStringList searchPathForTemplates = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), "Search path for templates" ); - - // QGS_SETTING_ENTRY_STRINGLIST( SearchPathForTemplates, QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), tr( "Search path for templates" ) ) - -// struct SearchPathForTemplates : public QgsSettingsEntryStringList -// { -// SearchPathForTemplates() -// : QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), tr( "Search path for templates" ) ) -// {} -// }; + static const inline QgsSettingsEntryStringList searchPathForTemplates = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), QObject::tr( "Search path for templates" ) ); }; public slots: diff --git a/src/core/locator/qgslocator.cpp b/src/core/locator/qgslocator.cpp index abb495537346..bbae4a5ff091 100644 --- a/src/core/locator/qgslocator.cpp +++ b/src/core/locator/qgslocator.cpp @@ -95,20 +95,14 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter ) // restore settings bool enabled = true; -// if ( QgsLocator::Settings::LocatorFilterEnabled().exists( filter->name() ) ) -// enabled = QgsLocator::Settings::LocatorFilterEnabled().value( filter->name() ); -// if ( QgsLocator::Settings::locatorFilterEnabled.exists( filter->name() ) ) -// enabled = QgsLocator::Settings::locatorFilterEnabled.value( filter->name() ); + if ( QgsLocator::Settings::locatorFilterEnabled.exists( filter->name() ) ) + enabled = QgsLocator::Settings::locatorFilterEnabled.value( filter->name() ); bool byDefault = filter->useWithoutPrefix(); -// if ( QgsLocator::Settings::LocatorFilterDefault().exists( filter->name() ) ) -// byDefault = QgsLocator::Settings::LocatorFilterDefault().value( filter->name() ); -// if ( QgsLocator::Settings::locatorFilterDefault.exists( filter->name() ) ) -// byDefault = QgsLocator::Settings::locatorFilterDefault.value( filter->name() ); + if ( QgsLocator::Settings::locatorFilterDefault.exists( filter->name() ) ) + byDefault = QgsLocator::Settings::locatorFilterDefault.value( filter->name() ); QString prefix = filter->prefix(); -// if ( QgsLocator::Settings::LocatorFilterPrefix().exists( filter->name() ) ) -// prefix = QgsLocator::Settings::LocatorFilterPrefix().value( filter->name() ); -// if ( QgsLocator::Settings::locatorFilterPrefix.exists( filter->name() ) ) -// prefix = QgsLocator::Settings::locatorFilterPrefix.value( filter->name() ); + if ( QgsLocator::Settings::locatorFilterPrefix.exists( filter->name() ) ) + prefix = QgsLocator::Settings::locatorFilterPrefix.value( filter->name() ); if ( prefix.isEmpty() ) { prefix = filter->prefix(); diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index 47e8237aeed0..dd2d1f528257 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -155,13 +155,9 @@ class CORE_EXPORT QgsLocator : public QObject struct Settings { -// static inline QgsSettingsEntryBool locatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); -// static inline QgsSettingsEntryBool locatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); -// static inline QgsSettingsEntryString locatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); - -// QGS_SETTING_ENTRY_BOOL( LocatorFilterEnabled, QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, tr( "Enabled" ) ) -// QGS_SETTING_ENTRY_BOOL( LocatorFilterDefault, QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, tr( "Default value" ) ) -// QGS_SETTING_ENTRY_STRING( LocatorFilterPrefix, QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), tr( "Locator filter prefix" ) ) + static inline QgsSettingsEntryBool locatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); + static inline QgsSettingsEntryBool locatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); + static inline QgsSettingsEntryString locatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); }; signals: diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index 20bbee2737fb..cd06c45f5d76 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -1108,7 +1108,6 @@ QStringList QgsApplication::layoutTemplatePaths() //local directories to search when looking for an template with a given basename //defined by user in options dialog return QgsLayout::Settings::searchPathForTemplates.value(); -// return QgsLayout::Settings::SearchPathForTemplates().value(); } QMap QgsApplication::systemEnvVars() diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index a7de6283b156..82c0e56bdec8 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -74,7 +74,7 @@ QgsSettings::Section QgsSettingsEntry::section() const return mSection; } -bool QgsSettingsEntry::setValue( const QVariant &value, const QString &dynamicKeyPart ) +bool QgsSettingsEntry::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { QgsSettings().setValue( key( dynamicKeyPart ), value, @@ -118,13 +118,13 @@ QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, } -bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dynamicKeyPart ) +bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( value.canConvert() == false ) { QgsDebugMsg( QObject::tr( "Can't convert value '%1' to string for settings with key '%2'" ) - .arg( value.toString() ) - .arg( QgsSettingsEntry::key() ) ); + .arg( value.toString(), + QgsSettingsEntry::key() ) ); return false; } @@ -132,9 +132,9 @@ bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dyn if ( valueString.length() < mMinLength ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is shorter than minimum length '%3'." ) - .arg( QgsSettingsEntry::key() ) - .arg( valueString.length() ) - .arg( mMinLength ) ); + .arg( QgsSettingsEntry::key(), + valueString.length(), + mMinLength ) ); return false; } @@ -142,9 +142,9 @@ bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dyn && valueString.length() > mMaxLength ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is longer than maximum length '%3'." ) - .arg( QgsSettingsEntry::key() ) - .arg( valueString.length() ) - .arg( mMinLength ) ); + .arg( QgsSettingsEntry::key(), + valueString.length(), + mMinLength ) ); return false; } @@ -179,13 +179,13 @@ QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, } -bool QgsSettingsEntryStringList::setValue( const QVariant &value, const QString &dynamicKeyPart ) +bool QgsSettingsEntryStringList::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( value.canConvert() == false ) { QgsDebugMsg( QObject::tr( "Can't convert value '%1' to string list for settings with key '%2'" ) - .arg( value.toString() ) - .arg( QgsSettingsEntry::key() ) ); + .arg( value.toString(), + QgsSettingsEntry::key() ) ); return false; } @@ -210,13 +210,13 @@ QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, } -bool QgsSettingsEntryBool::setValue( const QVariant &value, const QString &dynamicKeyPart ) +bool QgsSettingsEntryBool::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( value.canConvert() == false ) { QgsDebugMsg( QObject::tr( "Can't convert value '%1' to bool for settings with key '%2'" ) - .arg( value.toString() ) - .arg( QgsSettingsEntry::key() ) ); + .arg( value.toString(), + QgsSettingsEntry::key() ) ); return false; } @@ -229,142 +229,179 @@ QgsSettingsEntry::SettingsType QgsSettingsEntryBool::settingsType() const return QgsSettingsEntry::Bool; } -//QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, -// QgsSettings::Section section, -// qlonglong defaultValue, -// const QString &description, -// qlonglong minValue, -// qlonglong maxValue ) +//QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, QgsSettings::Section section, qlonglong defaultValue, const QString &description ) // : QgsSettingsEntry( key, // section, // defaultValue, // description ) -// , mMinValue( minValue ) -// , mMaxValue( maxValue ) +// , mMinValue( std::numeric_limits::min() ) +// , mMaxValue( std::numeric_limits::max() ) //{ //} -//bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dynamicKeyPart ) -//{ -// if ( value.canConvert() == false ) -// { -// QgsDebugMsg( QObject::tr( "Can't convert value '%1' to qlonglong for settings with key '%2'" ) -// .arg( value.toString() ) -// .arg( QgsSettingsEntry::key() ) ); -// return false; -// } - -// qlonglong valueLongLong = value.toLongLong(); -// if ( valueLongLong < mMinValue ) -// { -// QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) -// .arg( QgsSettingsEntry::key() ) -// .arg( valueLongLong ) -// .arg( mMinValue ) ); -// return false; -// } - -// if ( valueLongLong > mMaxValue ) -// { -// QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) -// .arg( QgsSettingsEntry::key() ) -// .arg( valueLongLong ) -// .arg( mMinValue ) ); -// return false; -// } - -// QgsSettingsEntry::setValue( value, dynamicKeyPart ); -// return true; -//} +QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, QgsSettings::Section section, qlonglong defaultValue, const QString &description, qlonglong minValue, qlonglong maxValue ) + : QgsSettingsEntry( key, + section, + defaultValue, + description ) + , mMinValue( minValue ) + , mMaxValue( maxValue ) +{ -//QgsSettingsEntry::SettingsType QgsSettingsEntryInteger::settingsType() const -//{ -// return QgsSettingsEntry::Integer; -//} +} -//qlonglong QgsSettingsEntryInteger::minValue() -//{ -// return mMinValue; -//} +bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +{ + if ( value.canConvert() == false ) + { + QgsDebugMsg( QObject::tr( "Can't convert value '%1' to qlonglong for settings with key '%2'" ) + .arg( value.toString(), + QgsSettingsEntry::key() ) ); + return false; + } -//qlonglong QgsSettingsEntryInteger::maxValue() -//{ -// return mMaxValue; -//} + qlonglong valueLongLong = value.toLongLong(); + if ( valueLongLong < mMinValue ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) + .arg( QgsSettingsEntry::key(), + QString::number( valueLongLong ), + QString::number( mMinValue ) ) ); + return false; + } + + if ( valueLongLong > mMaxValue ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) + .arg( QgsSettingsEntry::key(), + QString::number( valueLongLong ), + QString::number( mMinValue ) ) ); + return false; + } + + QgsSettingsEntry::setValue( value, dynamicKeyPart ); + return true; +} + +QgsSettingsEntry::SettingsType QgsSettingsEntryInteger::settingsType() const +{ + return QgsSettingsEntry::Integer; +} + +void QgsSettingsEntryInteger::setMinValue( qlonglong minValue ) +{ + mMinValue = minValue; +} + +qlonglong QgsSettingsEntryInteger::minValue() +{ + return mMinValue; +} + +void QgsSettingsEntryInteger::setMaxValue( qlonglong maxValue ) +{ + mMaxValue = maxValue; +} + +qlonglong QgsSettingsEntryInteger::maxValue() +{ + return mMaxValue; +} -//QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, -// QgsSettings::Section section, -// double defaultValue, -// const QString &description, -// double minValue, -// double maxValue, -// double displayDecimals ) +//QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, QgsSettings::Section section, double defaultValue, const QString &description ) // : QgsSettingsEntry( key, // section, // defaultValue, // description ) -// , mMinValue( minValue ) -// , mMaxValue( maxValue ) -// , mDisplayHintDecimals( displayDecimals ) +// , mMinValue( std::numeric_limits::min() ) +// , mMaxValue( std::numeric_limits::max() ) +// , mDisplayHintDecimals( 1 ) //{ //} -//bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dynamicKeyPart ) -//{ -// if ( value.canConvert() == false ) -// { -// QgsDebugMsg( QObject::tr( "Can't convert value '%1' to double for settings with key '%2'" ) -// .arg( value.toString() ) -// .arg( QgsSettingsEntry::key() ) ); -// return false; -// } - -// double valueDouble = value.toDouble(); -// if ( valueDouble < mMinValue ) -// { -// QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) -// .arg( QgsSettingsEntry::key() ) -// .arg( valueDouble ) -// .arg( mMinValue ) ); -// return false; -// } - -// if ( valueDouble > mMaxValue ) -// { -// QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) -// .arg( QgsSettingsEntry::key() ) -// .arg( valueDouble ) -// .arg( mMinValue ) ); -// return false; -// } - -// QgsSettingsEntry::setValue( value, dynamicKeyPart ); -// return true; -//} +QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, QgsSettings::Section section, double defaultValue, const QString &description, double minValue, double maxValue, int displayDecimals ) + : QgsSettingsEntry( key, + section, + defaultValue, + description ) + , mMinValue( minValue ) + , mMaxValue( maxValue ) + , mDisplayHintDecimals( displayDecimals ) +{ -//QgsSettingsEntry::SettingsType QgsSettingsEntryDouble::settingsType() const -//{ -// return QgsSettingsEntry::Double; -//} +} -//double QgsSettingsEntryDouble::minValue() const -//{ -// return mMinValue; -//} +bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +{ + if ( value.canConvert() == false ) + { + QgsDebugMsg( QObject::tr( "Can't convert value '%1' to double for settings with key '%2'" ) + .arg( value.toString(), + QgsSettingsEntry::key() ) ); + return false; + } -//double QgsSettingsEntryDouble::maxValue() const -//{ -// return mMaxValue; -//} + double valueDouble = value.toDouble(); + if ( valueDouble < mMinValue ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) + .arg( QgsSettingsEntry::key(), + QString::number( valueDouble ), + QString::number( mMinValue ) ) ); + return false; + } -//int QgsSettingsEntryDouble::displayHintDecimals() const -//{ -// return mDisplayHintDecimals; -//} + if ( valueDouble > mMaxValue ) + { + QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) + .arg( QgsSettingsEntry::key(), + QString::number( valueDouble ), + QString::number( mMinValue ) ) ); + return false; + } + + QgsSettingsEntry::setValue( value, dynamicKeyPart ); + return true; +} + +QgsSettingsEntry::SettingsType QgsSettingsEntryDouble::settingsType() const +{ + return QgsSettingsEntry::Double; +} + +void QgsSettingsEntryDouble::setMinValue( double minValue ) +{ + mMinValue = minValue; +} + +double QgsSettingsEntryDouble::minValue() const +{ + return mMinValue; +} + +void QgsSettingsEntryDouble::setMaxValue( double maxValue ) +{ + mMaxValue = maxValue; +} + +double QgsSettingsEntryDouble::maxValue() const +{ + return mMaxValue; +} + +void QgsSettingsEntryDouble::setDisplayHintDecimals( int displayHintDecimals ) +{ + mDisplayHintDecimals = displayHintDecimals; +} + +int QgsSettingsEntryDouble::displayHintDecimals() const +{ + return mDisplayHintDecimals; +} -bool QgsSettingsEntryEnum::setValue( const QVariant &value, const QString &dynamicKeyPart ) +bool QgsSettingsEntryEnum::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( mMetaEnum.isValid() == false ) { diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index f0c607d7138b..eb288541517c 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -23,8 +23,6 @@ #include "qgis_sip.h" #include "qgssettings.h" -#define QGS_SETTING_ENTRY_VARIANT(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntry { name() : QgsSettingsEntry( path, section, defaultValue, ##__VA_ARGS__ ) {} }; - /** * \ingroup core * \class QgsSettingsEntry @@ -46,10 +44,10 @@ class CORE_EXPORT QgsSettingsEntry sipType = sipType_QgsSettingsEntryStringList; else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryBool; -// else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) ) -// sipType = sipType_QgsSettingsEntryInteger; -// else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) ) -// sipType = sipType_QgsSettingsEntryDouble; + else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryInteger; + else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryDouble; else sipType = NULL; SIP_END @@ -122,7 +120,7 @@ class CORE_EXPORT QgsSettingsEntry * * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. */ - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ); + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; /** * Get settings value. @@ -183,8 +181,6 @@ class CORE_EXPORT QgsSettingsEntry }; -#define QGS_SETTING_ENTRY_STRING(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryString { name() : QgsSettingsEntryString( path, section, defaultValue, ##__VA_ARGS__ ) {} }; - /** * \class QgsSettingsEntryString * \ingroup core @@ -215,7 +211,7 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry int maxLength = -1 ); //! \copydoc QgsSettingsEntry::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; @@ -238,8 +234,6 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry }; -#define QGS_SETTING_ENTRY_STRINGLIST(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryStringList { name() : QgsSettingsEntryStringList( path, section, defaultValue, ##__VA_ARGS__ ) {} }; - /** * \class QgsSettingsEntryStringList * \ingroup core @@ -265,7 +259,7 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry const QString &description = QString() ); //! \copydoc QgsSettingsEntry::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; @@ -273,8 +267,6 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry }; -#define QGS_SETTING_ENTRY_BOOL(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryBool { name() : QgsSettingsEntryBool( path, section, defaultValue, ##__VA_ARGS__ ) {} }; - /** * \class QgsSettingsEntryBool * \ingroup core @@ -300,7 +292,7 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry const QString &description = QString() ); //! \copydoc QgsSettingsEntry::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; @@ -308,125 +300,196 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry }; -//#define QGS_SETTING_ENTRY_INTEGER(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryInteger { name() : QgsSettingsEntryInteger( path, section, defaultValue, ##__VA_ARGS__ ) {} }; - -///** -// * \class QgsSettingsEntryInteger -// * \ingroup core -// * An integer settings entry. -// * \since QGIS 3.20 -// */ -//class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry -//{ -// public: - -// /** -// * Constructor for QgsSettingsEntryInteger. -// * -// * The \a key argument specifies the final part of the settings key. -// * The \a parentGroup argument specifies a parent group which is used to rebuild -// * the entiere settings key and to determine the settings section. -// * The \a default value argument specifies the default value for the settings entry. -// * The \a description argument specifies a description for the settings entry. -// * The \a minValue argument specifies the minimal value. -// * The \a maxValue argument specifies the maximal value. -// */ -// QgsSettingsEntryInteger( const QString &key, -// QgsSettings::Section section, -// qlonglong defaultValue = 0, -// const QString &description = QString(), -// qlonglong minValue = std::numeric_limits::min(), -// qlonglong maxValue = std::numeric_limits::max() ); - -// //! \copydoc QgsSettingsEntry::setValue -// bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; - -// //! \copydoc QgsSettingsEntry::settingsType -// virtual SettingsType settingsType() const override; - -// /** -// * Returns the minimum value. -// */ -// qlonglong minValue(); - -// /** -// * Returns the maximum value. -// */ -// qlonglong maxValue(); - -// private: - -// qlonglong mMinValue; -// qlonglong mMaxValue; - -//}; - - -//#define QGS_SETTING_ENTRY_DOUBLE(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryDouble { name() : QgsSettingsEntryDouble( path, section, defaultValue, ##__VA_ARGS__ ) {} }; - -///** -// * \class QgsSettingsEntryDouble -// * \ingroup core -// * A double settings entry. -// * \since QGIS 3.20 -// */ -//class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry -//{ -// public: - -// /** -// * Constructor for QgsSettingsEntryDouble. -// * -// * The \a key argument specifies the final part of the settings key. -// * The \a parentGroup argument specifies a parent group which is used to rebuild -// * the entiere settings key and to determine the settings section. -// * The \a default value argument specifies the default value for the settings entry. -// * The \a description argument specifies a description for the settings entry. -// * The \a minValue argument specifies the minimal value. -// * The \a maxValue argument specifies the maximal value. -// */ -// QgsSettingsEntryDouble( const QString &key, -// QgsSettings::Section section, -// double defaultValue = 0.0, -// const QString &description = QString(), -// double minValue = std::numeric_limits::min(), -// double maxValue = std::numeric_limits::max(), -// double displayDecimals = 1 ); - -// //! \copydoc QgsSettingsEntry::setValue -// bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; - -// //! \copydoc QgsSettingsEntry::settingsType -// virtual SettingsType settingsType() const override; - -// /** -// * Returns the minimum value. -// */ -// double minValue() const; - -// /** -// * Returns the maximum value. -// */ -// double maxValue() const; - -// /** -// * Returns how much decimals should be shown in the Gui. -// */ -// int displayHintDecimals() const; - -// private: - -// double mMinValue; -// double mMaxValue; - -// int mDisplayHintDecimals; - -//}; +/** + * \class QgsSettingsEntryInteger + * \ingroup core + * An integer settings entry. + * \since QGIS 3.20 + */ +class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry +{ + public: + + +#ifndef SIP_RUN + + /** + * Constructor for QgsSettingsEntryInteger. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + * The \a minValue argument specifies the minimal value. + * The \a maxValue argument specifies the maximal value. + */ + QgsSettingsEntryInteger( const QString &key, + QgsSettings::Section section, + qlonglong defaultValue = 0, + const QString &description = QString(), + qlonglong minValue = std::numeric_limits::min(), + qlonglong maxValue = std::numeric_limits::max() ); +#else + + /** + * Constructor for QgsSettingsEntryInteger. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryInteger( const QString &key, + QgsSettings::Section section, + qlonglong defaultValue = 0, + const QString &description = QString() ); +#endif + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override; + + /** + * Set the minimum value. + * + * minValue The minimum value. + */ + void setMinValue( qlonglong minValue ); + + /** + * Returns the minimum value. + */ + qlonglong minValue(); + + /** + * Set the maximum value. + * + * maxValue The maximum value. + */ + void setMaxValue( qlonglong maxValue ); + + /** + * Returns the maximum value. + */ + qlonglong maxValue(); + + private: + + qlonglong mMinValue; + qlonglong mMaxValue; + +}; + + +/** + * \class QgsSettingsEntryDouble + * \ingroup core + * A double settings entry. + * \since QGIS 3.20 + */ +class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry +{ + public: #ifndef SIP_RUN -#define QGS_SETTING_ENTRY_ENUM(name, path, section, defaultValue, ...) struct name : public QgsSettingsEntryEnum { name() : QgsSettingsEntryEnum( path, section, defaultValue, ##__VA_ARGS__ ) {} }; + /** + * Constructor for QgsSettingsEntryDouble. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + * The \a minValue argument specifies the minimal value. + * The \a maxValue argument specifies the maximal value. + * The \a displayDecimals specifies an hint for the gui about how much decimals to show + * for example for a QDoubleSpinBox. + */ + QgsSettingsEntryDouble( const QString &key, + QgsSettings::Section section, + double defaultValue = 0.0, + const QString &description = QString(), + double minValue = std::numeric_limits::min(), + double maxValue = std::numeric_limits::max(), + int displayDecimals = 1 ); + +#else + + /** + * Constructor for QgsSettingsEntryDouble. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryDouble( const QString &key, + QgsSettings::Section section, + double defaultValue = 0.0, + const QString &description = QString() ); + +#endif + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override; + + /** + * Set the minimum value. + * + * minValue The minimum value. + */ + void setMinValue( double minValue ); + + /** + * Returns the minimum value. + */ + double minValue() const; + + /** + * Set the maximum value. + * + * maxValue The maximum value. + */ + void setMaxValue( double maxValue ); + + /** + * Returns the maximum value. + */ + double maxValue() const; + + /** + * Set the display hint decimals. + * + * displayHintDecimals The number of decimals that should be shown in the Gui. + */ + void setDisplayHintDecimals( int displayHintDecimals ); + + /** + * Returns how much decimals should be shown in the Gui. + */ + int displayHintDecimals() const; + + private: + + double mMinValue; + double mMaxValue; + + int mDisplayHintDecimals; + +}; + + +#ifndef SIP_RUN /** * \class QgsSettingsEntryEnum @@ -466,7 +529,7 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntry } //! \copydoc QgsSettingsEntry::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) override; + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; diff --git a/src/core/settings/qgssettingsregistrycore.cpp b/src/core/settings/qgssettingsregistrycore.cpp index ec6601901a09..e2a2d1ae1789 100644 --- a/src/core/settings/qgssettingsregistrycore.cpp +++ b/src/core/settings/qgssettingsregistrycore.cpp @@ -22,16 +22,14 @@ QgsSettingsRegistryCore::QgsSettingsRegistryCore() : mSettingsEntries() { mSettingsEntries.append( &QgsLayout::Settings::searchPathForTemplates ); -// mSettingsEntries.append( new QgsLayout::Settings::SearchPathForTemplates() ); -// mSettingsEntries.append( &QgsLocator::Settings::locatorFilterEnabled ); -// mSettingsEntries.append( &QgsLocator::Settings::locatorFilterDefault ); -// mSettingsEntries.append( &QgsLocator::Settings::locatorFilterPrefix ); + mSettingsEntries.append( &QgsLocator::Settings::locatorFilterEnabled ); + mSettingsEntries.append( &QgsLocator::Settings::locatorFilterDefault ); + mSettingsEntries.append( &QgsLocator::Settings::locatorFilterPrefix ); } QgsSettingsRegistryCore::~QgsSettingsRegistryCore() { -// qDeleteAll( mSettingsEntries ); } diff --git a/src/core/settings/qgssettingsregistrycore.h b/src/core/settings/qgssettingsregistrycore.h index d8e35cd18a12..a99eada1b34c 100644 --- a/src/core/settings/qgssettingsregistrycore.h +++ b/src/core/settings/qgssettingsregistrycore.h @@ -43,7 +43,7 @@ class CORE_EXPORT QgsSettingsRegistryCore private: - QList mSettingsEntries; + QList mSettingsEntries; }; From 628f9dab2963f6adfe73ec501100a1ab49157c7f Mon Sep 17 00:00:00 2001 From: Damiano Date: Wed, 31 Mar 2021 16:45:17 +0200 Subject: [PATCH 08/20] Added QgsSettingsEntryBase and value() is now typed according to setting --- .../auto_generated/locator/qgslocator.sip.in | 6 +- .../settings/qgssettingsentry.sip.in | 152 +++++++++-- src/core/locator/qgslocator.cpp | 6 +- src/core/locator/qgslocator.h | 6 +- src/core/qgsapplication.cpp | 2 +- src/core/settings/qgssettingsentry.cpp | 248 ++++++++++-------- src/core/settings/qgssettingsentry.h | 179 +++++++++---- src/core/settings/qgssettingsregistrycore.h | 2 +- 8 files changed, 417 insertions(+), 184 deletions(-) diff --git a/python/core/auto_generated/locator/qgslocator.sip.in b/python/core/auto_generated/locator/qgslocator.sip.in index ee1ecc8e0250..f6aa6230e8e6 100644 --- a/python/core/auto_generated/locator/qgslocator.sip.in +++ b/python/core/auto_generated/locator/qgslocator.sip.in @@ -143,9 +143,9 @@ This list is updated when preparing the search struct Settings { - static QgsSettingsEntryBool locatorFilterEnabled; // = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); - static QgsSettingsEntryBool locatorFilterDefault; // = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); - static QgsSettingsEntryString locatorFilterPrefix; // = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); + static const QgsSettingsEntryBool locatorFilterEnabled;// = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); + static const QgsSettingsEntryBool locatorFilterDefault;// = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); + static const QgsSettingsEntryString locatorFilterPrefix;// = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); }; signals: diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index 9ca9c3668341..9bd6dff76b86 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -9,7 +9,7 @@ -class QgsSettingsEntry +class QgsSettingsEntryBase { %Docstring(signature="appended") @@ -24,9 +24,11 @@ to validate set values and provide more accurate settings description for the gu #include "qgssettingsentry.h" %End %ConvertToSubClassCode - if ( dynamic_cast< QgsSettingsEntryString * >( sipCpp ) ) + if ( dynamic_cast< QgsSettingsEntryVariant * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryVariant; + else if ( dynamic_cast< QgsSettingsEntryString * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryString; - if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) ) + else if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryStringList; else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryBool; @@ -50,12 +52,12 @@ to validate set values and provide more accurate settings description for the gu Enum }; - QgsSettingsEntry( QString key, - QgsSettings::Section section, - QVariant defaultValue = QVariant(), - QString description = QString() ); + QgsSettingsEntryBase( QString key, + QgsSettings::Section section, + QVariant defaultValue = QVariant(), + QString description = QString() ); %Docstring -Constructor for QgsSettingsEntry. +Constructor for :py:class:`QgsSettingsEntry`. The ``key`` argument specifies the key of the settings. The ``section`` argument specifies the section of the settings. @@ -63,7 +65,7 @@ The ``default`` value argument specifies the default value for the settings entr The ``description`` argument specifies a description for the settings entry. %End - virtual ~QgsSettingsEntry(); + virtual ~QgsSettingsEntryBase(); QString key( const QString &dynamicKeyPart = QString() ) const; %Docstring @@ -103,19 +105,19 @@ Set settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. %End - QVariant valueFromPython() const /PyName=value/; + QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const; %Docstring Get settings value. -%End +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End - QVariant defaultValueFromPython() const /PyName=defaultValue/; + QVariant defaultValueAsVariant() const; %Docstring Get settings default value. %End - - virtual SettingsType settingsType() const; + virtual SettingsType settingsType() const = 0; %Docstring Get the settings entry type. %End @@ -128,7 +130,59 @@ Get the settings entry description. }; -class QgsSettingsEntryString : QgsSettingsEntry +class QgsSettingsEntryVariant : QgsSettingsEntryBase +{ +%Docstring(signature="appended") +A variant settings entry. + +.. versionadded:: 3.20 +%End + +%TypeHeaderCode +#include "qgssettingsentry.h" +%End + public: + + QgsSettingsEntryVariant( const QString &key, + QgsSettings::Section section, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); +%Docstring +Constructor for QgsSettingsEntryVariant. + +The ``key`` argument specifies the final part of the settings key. +The ``parentGroup`` argument specifies a parent group which is used to rebuild +the entiere settings key and to determine the settings section. +The ``default`` value argument specifies the default value for the settings entry. +The ``description`` argument specifies a description for the settings entry. +%End + + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; + +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + QVariant value( const QString &dynamicKeyPart = QString() ) const; +%Docstring +Get settings value. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QVariant defaultValue() const; +%Docstring +Get settings default value. +%End + + virtual SettingsType settingsType() const; +%Docstring +\copydoc :py:class:`QgsSettingsEntry`.settingsType +%End +}; + + +class QgsSettingsEntryString : QgsSettingsEntryBase { %Docstring(signature="appended") A string settings entry. @@ -164,6 +218,18 @@ By -1 the there is no limit %Docstring \copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + QString value( const QString &dynamicKeyPart = QString() ) const; +%Docstring +Get settings value. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QString defaultValue() const; +%Docstring +Get settings default value. %End virtual SettingsType settingsType() const; @@ -184,7 +250,7 @@ Returns the string maximum length. By -1 there is no limitation. }; -class QgsSettingsEntryStringList : QgsSettingsEntry +class QgsSettingsEntryStringList : QgsSettingsEntryBase { %Docstring(signature="appended") A string list settings entry. @@ -215,6 +281,18 @@ The ``description`` argument specifies a description for the settings entry. %Docstring \copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + QStringList value( const QString &dynamicKeyPart = QString() ) const; +%Docstring +Get settings value. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QStringList defaultValue() const; +%Docstring +Get settings default value. %End virtual SettingsType settingsType() const; @@ -225,7 +303,7 @@ The ``description`` argument specifies a description for the settings entry. }; -class QgsSettingsEntryBool : QgsSettingsEntry +class QgsSettingsEntryBool : QgsSettingsEntryBase { %Docstring(signature="appended") A boolean settings entry. @@ -256,6 +334,18 @@ The ``description`` argument specifies a description for the settings entry. %Docstring \copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + bool value( const QString &dynamicKeyPart = QString() ) const; +%Docstring +Get settings value. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool defaultValue() const; +%Docstring +Get settings default value. %End virtual SettingsType settingsType() const; @@ -266,7 +356,7 @@ The ``description`` argument specifies a description for the settings entry. }; -class QgsSettingsEntryInteger : QgsSettingsEntry +class QgsSettingsEntryInteger : QgsSettingsEntryBase { %Docstring(signature="appended") An integer settings entry. @@ -300,6 +390,18 @@ The ``description`` argument specifies a description for the settings entry. %Docstring \copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + qlonglong value( const QString &dynamicKeyPart = QString() ) const; +%Docstring +Get settings value. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + qlonglong defaultValue() const; +%Docstring +Get settings default value. %End virtual SettingsType settingsType() const; @@ -334,7 +436,7 @@ Returns the maximum value. }; -class QgsSettingsEntryDouble : QgsSettingsEntry +class QgsSettingsEntryDouble : QgsSettingsEntryBase { %Docstring(signature="appended") A double settings entry. @@ -367,6 +469,18 @@ The ``description`` argument specifies a description for the settings entry. %Docstring \copydoc :py:class:`QgsSettingsEntry`.setValue +%End + + double value( const QString &dynamicKeyPart = QString() ) const; +%Docstring +Get settings value. + +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + double defaultValue() const; +%Docstring +Get settings default value. %End virtual SettingsType settingsType() const; diff --git a/src/core/locator/qgslocator.cpp b/src/core/locator/qgslocator.cpp index bbae4a5ff091..5bed9a07910f 100644 --- a/src/core/locator/qgslocator.cpp +++ b/src/core/locator/qgslocator.cpp @@ -96,13 +96,13 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter ) // restore settings bool enabled = true; if ( QgsLocator::Settings::locatorFilterEnabled.exists( filter->name() ) ) - enabled = QgsLocator::Settings::locatorFilterEnabled.value( filter->name() ); + enabled = QgsLocator::Settings::locatorFilterEnabled.value( filter->name() ); bool byDefault = filter->useWithoutPrefix(); if ( QgsLocator::Settings::locatorFilterDefault.exists( filter->name() ) ) - byDefault = QgsLocator::Settings::locatorFilterDefault.value( filter->name() ); + byDefault = QgsLocator::Settings::locatorFilterDefault.value( filter->name() ); QString prefix = filter->prefix(); if ( QgsLocator::Settings::locatorFilterPrefix.exists( filter->name() ) ) - prefix = QgsLocator::Settings::locatorFilterPrefix.value( filter->name() ); + prefix = QgsLocator::Settings::locatorFilterPrefix.value( filter->name() ); if ( prefix.isEmpty() ) { prefix = filter->prefix(); diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index dd2d1f528257..0407ae0ca922 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -155,9 +155,9 @@ class CORE_EXPORT QgsLocator : public QObject struct Settings { - static inline QgsSettingsEntryBool locatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); - static inline QgsSettingsEntryBool locatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); - static inline QgsSettingsEntryString locatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); + static const inline QgsSettingsEntryBool locatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); + static const inline QgsSettingsEntryBool locatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); + static const inline QgsSettingsEntryString locatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); }; signals: diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index cd06c45f5d76..521797402193 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -1107,7 +1107,7 @@ QStringList QgsApplication::layoutTemplatePaths() { //local directories to search when looking for an template with a given basename //defined by user in options dialog - return QgsLayout::Settings::searchPathForTemplates.value(); + return QgsLayout::Settings::searchPathForTemplates.value(); } QMap QgsApplication::systemEnvVars() diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index 82c0e56bdec8..3bf485b68485 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -17,10 +17,10 @@ #include "qgslogger.h" -QgsSettingsEntry::QgsSettingsEntry( QString key, - QgsSettings::Section section, - QVariant defaultValue, - QString description ) +QgsSettingsEntryBase::QgsSettingsEntryBase( QString key, + QgsSettings::Section section, + QVariant defaultValue, + QString description ) : mKey( key ) , mDefaultValue( defaultValue ) , mSection( section ) @@ -28,11 +28,11 @@ QgsSettingsEntry::QgsSettingsEntry( QString key, { } -QgsSettingsEntry::~QgsSettingsEntry() +QgsSettingsEntryBase::~QgsSettingsEntryBase() { } -QString QgsSettingsEntry::key( const QString &dynamicKeyPart ) const +QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const { if ( dynamicKeyPart.isEmpty() == false ) { @@ -54,27 +54,27 @@ QString QgsSettingsEntry::key( const QString &dynamicKeyPart ) const } } -bool QgsSettingsEntry::hasDynamicKey() const +bool QgsSettingsEntryBase::hasDynamicKey() const { return mKey.contains( '%' ); } -bool QgsSettingsEntry::exists( const QString &dynamicKeyPart ) const +bool QgsSettingsEntryBase::exists( const QString &dynamicKeyPart ) const { return QgsSettings().contains( key( dynamicKeyPart ), section() ); } -void QgsSettingsEntry::remove( const QString &dynamicKeyPart ) const +void QgsSettingsEntryBase::remove( const QString &dynamicKeyPart ) const { QgsSettings().remove( key( dynamicKeyPart ), section() ); } -QgsSettings::Section QgsSettingsEntry::section() const +QgsSettings::Section QgsSettingsEntryBase::section() const { return mSection; } -bool QgsSettingsEntry::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +bool QgsSettingsEntryBase::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { QgsSettings().setValue( key( dynamicKeyPart ), value, @@ -82,40 +82,60 @@ bool QgsSettingsEntry::setValue( const QVariant &value, const QString &dynamicKe return true; } -QgsSettingsEntry::SettingsType QgsSettingsEntry::settingsType() const +QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart ) const { - return QgsSettingsEntry::Variant; + return QgsSettings().value( key( dynamicKeyPart ), + mDefaultValue, + mSection ); } -QVariant QgsSettingsEntry::valueFromPython() const +QVariant QgsSettingsEntryBase::defaultValueAsVariant() const { - return value(); + return mDefaultValue; } -QVariant QgsSettingsEntry::defaultValueFromPython() const +QString QgsSettingsEntryBase::description() const { - return defaultValue(); + return mDescription; } -QString QgsSettingsEntry::description() const + +QgsSettingsEntryVariant::QgsSettingsEntryVariant( const QString &key, QgsSettings::Section section, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + section, + defaultValue, + description ) { - return mDescription; } -QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, - QgsSettings::Section section, - const QString &defaultValue, - const QString &description, - int minLength, - int maxLength ) - : QgsSettingsEntry( key, - section, - defaultValue, - description ) +bool QgsSettingsEntryVariant::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +{ + return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); +} + +QVariant QgsSettingsEntryVariant::value( const QString &dynamicKeyPart ) const +{ + return valueAsVariant( dynamicKeyPart ); +} + +QVariant QgsSettingsEntryVariant::defaultValue() const +{ + return defaultValueAsVariant(); +} + +QgsSettingsEntryBase::SettingsType QgsSettingsEntryVariant::settingsType() const +{ + return QgsSettingsEntryBase::Variant; +} + +QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, QgsSettings::Section section, const QString &defaultValue, const QString &description, int minLength, int maxLength ) + : QgsSettingsEntryBase( key, + section, + defaultValue, + description ) , mMinLength( minLength ) , mMaxLength( maxLength ) { - } bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dynamicKeyPart ) const @@ -124,7 +144,7 @@ bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dyn { QgsDebugMsg( QObject::tr( "Can't convert value '%1' to string for settings with key '%2'" ) .arg( value.toString(), - QgsSettingsEntry::key() ) ); + QgsSettingsEntryBase::key() ) ); return false; } @@ -132,7 +152,7 @@ bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dyn if ( valueString.length() < mMinLength ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is shorter than minimum length '%3'." ) - .arg( QgsSettingsEntry::key(), + .arg( QgsSettingsEntryBase::key(), valueString.length(), mMinLength ) ); return false; @@ -142,19 +162,28 @@ bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dyn && valueString.length() > mMaxLength ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is longer than maximum length '%3'." ) - .arg( QgsSettingsEntry::key(), + .arg( QgsSettingsEntryBase::key(), valueString.length(), mMinLength ) ); return false; } - QgsSettingsEntry::setValue( value, dynamicKeyPart ); - return true; + return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); +} + +QString QgsSettingsEntryString::value( const QString &dynamicKeyPart ) const +{ + return valueAsVariant( dynamicKeyPart ).toString(); } -QgsSettingsEntry::SettingsType QgsSettingsEntryString::settingsType() const +QString QgsSettingsEntryString::defaultValue() const { - return QgsSettingsEntry::String; + return defaultValueAsVariant().toString(); +} + +QgsSettingsEntryBase::SettingsType QgsSettingsEntryString::settingsType() const +{ + return QgsSettingsEntryBase::String; } int QgsSettingsEntryString::minLength() @@ -171,12 +200,11 @@ QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, QgsSettings::Section section, const QStringList &defaultValue, const QString &description ) - : QgsSettingsEntry( key, - section, - defaultValue, - description ) + : QgsSettingsEntryBase( key, + section, + defaultValue, + description ) { - } bool QgsSettingsEntryStringList::setValue( const QVariant &value, const QString &dynamicKeyPart ) const @@ -185,29 +213,37 @@ bool QgsSettingsEntryStringList::setValue( const QVariant &value, const QString { QgsDebugMsg( QObject::tr( "Can't convert value '%1' to string list for settings with key '%2'" ) .arg( value.toString(), - QgsSettingsEntry::key() ) ); + QgsSettingsEntryBase::key() ) ); return false; } - QgsSettingsEntry::setValue( value, dynamicKeyPart ); - return true; + return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); } -QgsSettingsEntry::SettingsType QgsSettingsEntryStringList::settingsType() const +QStringList QgsSettingsEntryStringList::value( const QString &dynamicKeyPart ) const { - return QgsSettingsEntry::StringList; + return valueAsVariant( dynamicKeyPart ).toStringList(); +} + +QStringList QgsSettingsEntryStringList::defaultValue() const +{ + return defaultValueAsVariant().toStringList(); +} + +QgsSettingsEntryBase::SettingsType QgsSettingsEntryStringList::settingsType() const +{ + return QgsSettingsEntryBase::StringList; } QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, QgsSettings::Section section, bool defaultValue, const QString &description ) - : QgsSettingsEntry( key, - section, - defaultValue, - description ) + : QgsSettingsEntryBase( key, + section, + defaultValue, + description ) { - } bool QgsSettingsEntryBool::setValue( const QVariant &value, const QString &dynamicKeyPart ) const @@ -216,35 +252,33 @@ bool QgsSettingsEntryBool::setValue( const QVariant &value, const QString &dynam { QgsDebugMsg( QObject::tr( "Can't convert value '%1' to bool for settings with key '%2'" ) .arg( value.toString(), - QgsSettingsEntry::key() ) ); + QgsSettingsEntryBase::key() ) ); return false; } - QgsSettingsEntry::setValue( value, dynamicKeyPart ); - return true; + return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); } -QgsSettingsEntry::SettingsType QgsSettingsEntryBool::settingsType() const +bool QgsSettingsEntryBool::value( const QString &dynamicKeyPart ) const { - return QgsSettingsEntry::Bool; + return valueAsVariant( dynamicKeyPart ).toBool(); } -//QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, QgsSettings::Section section, qlonglong defaultValue, const QString &description ) -// : QgsSettingsEntry( key, -// section, -// defaultValue, -// description ) -// , mMinValue( std::numeric_limits::min() ) -// , mMaxValue( std::numeric_limits::max() ) -//{ +bool QgsSettingsEntryBool::defaultValue() const +{ + return defaultValueAsVariant().toBool(); +} -//} +QgsSettingsEntryBase::SettingsType QgsSettingsEntryBool::settingsType() const +{ + return QgsSettingsEntryBase::Bool; +} QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, QgsSettings::Section section, qlonglong defaultValue, const QString &description, qlonglong minValue, qlonglong maxValue ) - : QgsSettingsEntry( key, - section, - defaultValue, - description ) + : QgsSettingsEntryBase( key, + section, + defaultValue, + description ) , mMinValue( minValue ) , mMaxValue( maxValue ) { @@ -257,7 +291,7 @@ bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dy { QgsDebugMsg( QObject::tr( "Can't convert value '%1' to qlonglong for settings with key '%2'" ) .arg( value.toString(), - QgsSettingsEntry::key() ) ); + QgsSettingsEntryBase::key() ) ); return false; } @@ -265,7 +299,7 @@ bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dy if ( valueLongLong < mMinValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) - .arg( QgsSettingsEntry::key(), + .arg( QgsSettingsEntryBase::key(), QString::number( valueLongLong ), QString::number( mMinValue ) ) ); return false; @@ -274,19 +308,28 @@ bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dy if ( valueLongLong > mMaxValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) - .arg( QgsSettingsEntry::key(), + .arg( QgsSettingsEntryBase::key(), QString::number( valueLongLong ), QString::number( mMinValue ) ) ); return false; } - QgsSettingsEntry::setValue( value, dynamicKeyPart ); - return true; + return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); +} + +qlonglong QgsSettingsEntryInteger::value( const QString &dynamicKeyPart ) const +{ + return valueAsVariant( dynamicKeyPart ).toLongLong(); } -QgsSettingsEntry::SettingsType QgsSettingsEntryInteger::settingsType() const +qlonglong QgsSettingsEntryInteger::defaultValue() const { - return QgsSettingsEntry::Integer; + return defaultValueAsVariant().toLongLong(); +} + +QgsSettingsEntryBase::SettingsType QgsSettingsEntryInteger::settingsType() const +{ + return QgsSettingsEntryBase::Integer; } void QgsSettingsEntryInteger::setMinValue( qlonglong minValue ) @@ -309,23 +352,11 @@ qlonglong QgsSettingsEntryInteger::maxValue() return mMaxValue; } -//QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, QgsSettings::Section section, double defaultValue, const QString &description ) -// : QgsSettingsEntry( key, -// section, -// defaultValue, -// description ) -// , mMinValue( std::numeric_limits::min() ) -// , mMaxValue( std::numeric_limits::max() ) -// , mDisplayHintDecimals( 1 ) -//{ - -//} - QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, QgsSettings::Section section, double defaultValue, const QString &description, double minValue, double maxValue, int displayDecimals ) - : QgsSettingsEntry( key, - section, - defaultValue, - description ) + : QgsSettingsEntryBase( key, + section, + defaultValue, + description ) , mMinValue( minValue ) , mMaxValue( maxValue ) , mDisplayHintDecimals( displayDecimals ) @@ -339,7 +370,7 @@ bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dyn { QgsDebugMsg( QObject::tr( "Can't convert value '%1' to double for settings with key '%2'" ) .arg( value.toString(), - QgsSettingsEntry::key() ) ); + QgsSettingsEntryBase::key() ) ); return false; } @@ -347,7 +378,7 @@ bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dyn if ( valueDouble < mMinValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) - .arg( QgsSettingsEntry::key(), + .arg( QgsSettingsEntryBase::key(), QString::number( valueDouble ), QString::number( mMinValue ) ) ); return false; @@ -356,19 +387,28 @@ bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dyn if ( valueDouble > mMaxValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) - .arg( QgsSettingsEntry::key(), + .arg( QgsSettingsEntryBase::key(), QString::number( valueDouble ), QString::number( mMinValue ) ) ); return false; } - QgsSettingsEntry::setValue( value, dynamicKeyPart ); - return true; + return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); +} + +double QgsSettingsEntryDouble::value( const QString &dynamicKeyPart ) const +{ + return valueAsVariant( dynamicKeyPart ).toDouble(); } -QgsSettingsEntry::SettingsType QgsSettingsEntryDouble::settingsType() const +double QgsSettingsEntryDouble::defaultValue() const { - return QgsSettingsEntry::Double; + return defaultValueAsVariant().toDouble(); +} + +QgsSettingsEntryBase::SettingsType QgsSettingsEntryDouble::settingsType() const +{ + return QgsSettingsEntryBase::Double; } void QgsSettingsEntryDouble::setMinValue( double minValue ) @@ -409,11 +449,11 @@ bool QgsSettingsEntryEnum::setValue( const QVariant &value, const QString &dynam return false; } - QgsSettingsEntry::setValue( mMetaEnum.valueToKey( value.toInt() ), dynamicKeyPart ); - return true; + return QgsSettingsEntryBase::setValue( mMetaEnum.valueToKey( value.toInt() ), dynamicKeyPart ); } -QgsSettingsEntry::SettingsType QgsSettingsEntryEnum::settingsType() const +QgsSettingsEntryBase::SettingsType QgsSettingsEntryEnum::settingsType() const { - return QgsSettingsEntry::Enum; + return QgsSettingsEntryBase::Enum; } + diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index eb288541517c..06b5a349e017 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -33,14 +33,16 @@ * * \since QGIS 3.20 */ -class CORE_EXPORT QgsSettingsEntry +class CORE_EXPORT QgsSettingsEntryBase { #ifdef SIP_RUN SIP_CONVERT_TO_SUBCLASS_CODE - if ( dynamic_cast< QgsSettingsEntryString * >( sipCpp ) ) + if ( dynamic_cast< QgsSettingsEntryVariant * >( sipCpp ) ) + sipType = sipType_QgsSettingsEntryVariant; + else if ( dynamic_cast< QgsSettingsEntryString * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryString; - if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) ) + else if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryStringList; else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) ) sipType = sipType_QgsSettingsEntryBool; @@ -74,15 +76,15 @@ class CORE_EXPORT QgsSettingsEntry * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ - QgsSettingsEntry( QString key, - QgsSettings::Section section, - QVariant defaultValue = QVariant(), - QString description = QString() ); + QgsSettingsEntryBase( QString key, + QgsSettings::Section section, + QVariant defaultValue = QVariant(), + QString description = QString() ); /** * Destructor for QgsSettingsEntry. */ - virtual ~QgsSettingsEntry(); + virtual ~QgsSettingsEntryBase(); /** * Get settings entry key. @@ -124,47 +126,20 @@ class CORE_EXPORT QgsSettingsEntry /** * Get settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. */ - QVariant valueFromPython() const SIP_PYNAME( value ); - -#ifndef SIP_RUN - template - T value( const QString &dynamicKeyPart = QString() ) const - { - QVariant variantValue = QgsSettings().value( key( dynamicKeyPart ), - mDefaultValue, - mSection ); - if ( variantValue.canConvert() == false ) - QgsDebugMsg( QObject::tr( "Can't convert setting '%1' to type '%2'" ) - .arg( key( dynamicKeyPart ) ) - .arg( typeid( T ).name() ) ); - - return variantValue.value(); - } -#endif + QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const; /** * Get settings default value. */ - QVariant defaultValueFromPython() const SIP_PYNAME( defaultValue ); - -#ifndef SIP_RUN - template - T defaultValue() const - { - if ( mDefaultValue.canConvert() == false ) - QgsDebugMsg( QObject::tr( "Can't convert default value of setting '%1' to type '%2'" ) - .arg( mKey ) - .arg( typeid( T ).name() ) ); - - return mDefaultValue.value(); - } -#endif + QVariant defaultValueAsVariant() const; /** * Get the settings entry type. */ - virtual SettingsType settingsType() const; + virtual SettingsType settingsType() const = 0; /** * Get the settings entry description. @@ -181,13 +156,57 @@ class CORE_EXPORT QgsSettingsEntry }; +/** + * \class QgsSettingsEntryVariant + * \ingroup core + * A variant settings entry. + * \since QGIS 3.20 + */ +class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase +{ + public: + + /** + * Constructor for QgsSettingsEntryVariant. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryVariant( const QString &key, + QgsSettings::Section section, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + + /** + * Get settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + QVariant value( const QString &dynamicKeyPart = QString() ) const; + + /** + * Get settings default value. + */ + QVariant defaultValue() const; + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override; +}; + + /** * \class QgsSettingsEntryString * \ingroup core * A string settings entry. * \since QGIS 3.20 */ -class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry +class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase { public: @@ -213,6 +232,18 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Get settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + QString value( const QString &dynamicKeyPart = QString() ) const; + + /** + * Get settings default value. + */ + QString defaultValue() const; + //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; @@ -240,7 +271,7 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntry * A string list settings entry. * \since QGIS 3.20 */ -class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry +class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase { public: @@ -261,6 +292,18 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Get settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + QStringList value( const QString &dynamicKeyPart = QString() ) const; + + /** + * Get settings default value. + */ + QStringList defaultValue() const; + //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; @@ -273,7 +316,7 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntry * A boolean settings entry. * \since QGIS 3.20 */ -class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry +class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase { public: @@ -294,6 +337,18 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Get settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + bool value( const QString &dynamicKeyPart = QString() ) const; + + /** + * Get settings default value. + */ + bool defaultValue() const; + //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; @@ -306,7 +361,7 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntry * An integer settings entry. * \since QGIS 3.20 */ -class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry +class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase { public: @@ -351,6 +406,18 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Get settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + qlonglong value( const QString &dynamicKeyPart = QString() ) const; + + /** + * Get settings default value. + */ + qlonglong defaultValue() const; + //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; @@ -392,7 +459,7 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntry * A double settings entry. * \since QGIS 3.20 */ -class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry +class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase { public: @@ -440,6 +507,18 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Get settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + double value( const QString &dynamicKeyPart = QString() ) const; + + /** + * Get settings default value. + */ + double defaultValue() const; + //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; @@ -497,7 +576,7 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntry * An enum settings entry. * \since QGIS 3.20 */ -class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntry +class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase { public: @@ -515,10 +594,10 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntry QgsSettings::Section section, const T &defaultValue, const QString &description = QString() ) - : QgsSettingsEntry( key, - section, - defaultValue, - description ) + : QgsSettingsEntryBase( key, + section, + defaultValue, + description ) { mMetaEnum = QMetaEnum::fromType(); Q_ASSERT( mMetaEnum.isValid() ); diff --git a/src/core/settings/qgssettingsregistrycore.h b/src/core/settings/qgssettingsregistrycore.h index a99eada1b34c..dcd286a83c4a 100644 --- a/src/core/settings/qgssettingsregistrycore.h +++ b/src/core/settings/qgssettingsregistrycore.h @@ -43,7 +43,7 @@ class CORE_EXPORT QgsSettingsRegistryCore private: - QList mSettingsEntries; + QList mSettingsEntries; }; From d16702dfbccc222677a2551dee01a2aa1cef7bed Mon Sep 17 00:00:00 2001 From: Damiano Date: Thu, 1 Apr 2021 10:13:08 +0200 Subject: [PATCH 09/20] no more hand modified sip files --- python/core/auto_generated/layout/qgslayout.sip.in | 2 +- python/core/auto_generated/locator/qgslocator.sip.in | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/core/auto_generated/layout/qgslayout.sip.in b/python/core/auto_generated/layout/qgslayout.sip.in index 5ae6316c3b2c..0949ed2aa827 100644 --- a/python/core/auto_generated/layout/qgslayout.sip.in +++ b/python/core/auto_generated/layout/qgslayout.sip.in @@ -617,7 +617,7 @@ should be canceled. struct Settings { - static const QgsSettingsEntryStringList searchPathForTemplates; // = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), QObject::tr( "Search path for templates" ) ); + const QgsSettingsEntryStringList searchPathForTemplates; }; public slots: diff --git a/python/core/auto_generated/locator/qgslocator.sip.in b/python/core/auto_generated/locator/qgslocator.sip.in index f6aa6230e8e6..858c57f5b270 100644 --- a/python/core/auto_generated/locator/qgslocator.sip.in +++ b/python/core/auto_generated/locator/qgslocator.sip.in @@ -143,9 +143,9 @@ This list is updated when preparing the search struct Settings { - static const QgsSettingsEntryBool locatorFilterEnabled;// = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); - static const QgsSettingsEntryBool locatorFilterDefault;// = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); - static const QgsSettingsEntryString locatorFilterPrefix;// = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); + const QgsSettingsEntryBool locatorFilterEnabled; + const QgsSettingsEntryBool locatorFilterDefault; + const QgsSettingsEntryString locatorFilterPrefix; }; signals: From fe0dcee2846936e834f204c2900e2d9e57ca85ed Mon Sep 17 00:00:00 2001 From: Damiano Lombardi Date: Thu, 1 Apr 2021 21:09:36 +0200 Subject: [PATCH 10/20] Added new QgsSettingsEntryFlag and tests for enum and flag --- .../settings/qgssettingsentry.sip.in | 3 +- src/core/settings/qgssettingsentry.cpp | 14 -- src/core/settings/qgssettingsentry.h | 172 +++++++++++++++++- tests/src/core/CMakeLists.txt | 1 + tests/src/core/testqgssettingsentry.cpp | 126 +++++++++++++ 5 files changed, 296 insertions(+), 20 deletions(-) create mode 100644 tests/src/core/testqgssettingsentry.cpp diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index 9bd6dff76b86..aa7714e9a116 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -49,7 +49,8 @@ to validate set values and provide more accurate settings description for the gu Bool, Integer, Double, - Enum + Enum, + Flag }; QgsSettingsEntryBase( QString key, diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index 3bf485b68485..12014bccb034 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -441,19 +441,5 @@ int QgsSettingsEntryDouble::displayHintDecimals() const return mDisplayHintDecimals; } -bool QgsSettingsEntryEnum::setValue( const QVariant &value, const QString &dynamicKeyPart ) const -{ - if ( mMetaEnum.isValid() == false ) - { - QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); - return false; - } - return QgsSettingsEntryBase::setValue( mMetaEnum.valueToKey( value.toInt() ), dynamicKeyPart ); -} - -QgsSettingsEntryBase::SettingsType QgsSettingsEntryEnum::settingsType() const -{ - return QgsSettingsEntryBase::Enum; -} diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 06b5a349e017..4add21198a59 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -65,7 +65,8 @@ class CORE_EXPORT QgsSettingsEntryBase Bool, Integer, Double, - Enum + Enum, + Flag }; /** @@ -576,6 +577,7 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase * An enum settings entry. * \since QGIS 3.20 */ +template class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase { public: @@ -589,14 +591,13 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ - template QgsSettingsEntryEnum( const QString &key, QgsSettings::Section section, const T &defaultValue, const QString &description = QString() ) : QgsSettingsEntryBase( key, section, - defaultValue, + QMetaEnum::fromType().valueToKey( defaultValue ), description ) { mMetaEnum = QMetaEnum::fromType(); @@ -607,11 +608,66 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase } } + /** + * Get settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + T value( const QString &dynamicKeyPart = QString() ) const + { + return QgsSettings().enumValue( key( dynamicKeyPart ), + defaultValueAsVariant().value(), + section() ); + } + + /** + * Get settings default value. + */ + T defaultValue() const + { + if ( !mMetaEnum.isValid() ) + { + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); + return T(); + } + + QByteArray byteArray = defaultValueAsVariant().toByteArray(); + bool ok = false; + T defaultValue = static_cast( mMetaEnum.keyToValue( byteArray, &ok ) ); + if ( !ok ) + { + QgsDebugMsg( QStringLiteral( "Invalid enum key '%1'." ).arg( defaultValueAsVariant().toString() ) ); + return T(); + } + + return defaultValue; + } + //! \copydoc QgsSettingsEntry::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override + { + if ( !mMetaEnum.isValid() ) + { + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); + return false; + } + + const char *enumKey = mMetaEnum.valueToKey( value.toInt() ); + if ( enumKey == nullptr ) + { + QgsDebugMsg( QStringLiteral( "Invalid enum value '%1'." ).arg( value.toInt() ) ); + return false; + } + + QgsSettingsEntryBase::setValue( enumKey, dynamicKeyPart ); + return true; + } //! \copydoc QgsSettingsEntry::settingsType - virtual SettingsType settingsType() const override; + virtual SettingsType settingsType() const override + { + return QgsSettingsEntryBase::Enum; + } private: @@ -621,5 +677,111 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase #endif +#ifndef SIP_RUN + +/** + * \class QgsSettingsEntryFlag + * \ingroup core + * An flag settings entry. + * \since QGIS 3.20 + */ +template +class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryBase +{ + public: + + /** + * Constructor for QgsSettingsEntryFlag. + * + * The \a key argument specifies the final part of the settings key. + * The \a parentGroup argument specifies a parent group which is used to rebuild + * the entiere settings key and to determine the settings section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryFlag( const QString &key, + QgsSettings::Section section, + const T &defaultValue, + const QString &description = QString() ) + : QgsSettingsEntryBase( key, + section, + QMetaEnum::fromType().valueToKeys( defaultValue ), + description ) + { + mMetaEnum = QMetaEnum::fromType(); + Q_ASSERT( mMetaEnum.isValid() ); + if ( !mMetaEnum.isValid() ) + { + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Flag probably misses Q_ENUM or Q_FLAG declaration." ) ); + } + } + + /** + * Get settings value. + * + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + T value( const QString &dynamicKeyPart = QString() ) const + { + return QgsSettings().flagValue( key( dynamicKeyPart ), + defaultValueAsVariant().value(), + section() ); + } + + /** + * Get settings default value. + */ + T defaultValue() const + { + if ( !mMetaEnum.isValid() ) + { + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); + return T(); + } + + QByteArray byteArray = defaultValueAsVariant().toByteArray(); + bool ok = false; + T defaultValue = static_cast( mMetaEnum.keysToValue( byteArray, &ok ) ); + if ( !ok ) + { + QgsDebugMsg( QStringLiteral( "Invalid flag keys '%1'." ).arg( defaultValueAsVariant().toString() ) ); + return T(); + } + + return defaultValue; + } + + //! \copydoc QgsSettingsEntry::setValue + bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const + { + if ( !mMetaEnum.isValid() ) + { + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Flag probably misses Q_ENUM or Q_FLAG declaration." ) ); + return false; + } + + QByteArray flagKeys = mMetaEnum.valueToKeys( value ); + if ( flagKeys.isEmpty() ) + { + QgsDebugMsg( QStringLiteral( "Invalid flag value '%1'." ).arg( value ) ); + return false; + } + + QgsSettingsEntryBase::setValue( flagKeys, dynamicKeyPart ); + return true; + } + + //! \copydoc QgsSettingsEntry::settingsType + virtual SettingsType settingsType() const override + { + return QgsSettingsEntryBase::Flag; + } + + private: + + QMetaEnum mMetaEnum; + +}; +#endif #endif // QGSSETTINGSENTRY_H diff --git a/tests/src/core/CMakeLists.txt b/tests/src/core/CMakeLists.txt index 2804be878141..61acef4e54f0 100644 --- a/tests/src/core/CMakeLists.txt +++ b/tests/src/core/CMakeLists.txt @@ -188,6 +188,7 @@ set(TESTS testqgsrulebasedrenderer.cpp testqgsruntimeprofiler.cpp testqgssettings.cpp + testqgssettingsentry.cpp testqgsshapeburst.cpp testqgssimplemarker.cpp testqgssnappingutils.cpp diff --git a/tests/src/core/testqgssettingsentry.cpp b/tests/src/core/testqgssettingsentry.cpp new file mode 100644 index 000000000000..612b1e8e0319 --- /dev/null +++ b/tests/src/core/testqgssettingsentry.cpp @@ -0,0 +1,126 @@ +/*************************************************************************** + testqgssettingsentry.cpp + -------------------------------------- + Date : 01.04.2021 + Copyright : (C) 2021 by Damiano Lombardi + Email : damiano@opengis.ch + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +#include + + +#include "qgssettings.h" +#include "qgssettingsentry.h" +#include "qgsunittypes.h" +#include "qgsmaplayerproxymodel.h" +#include "qgstest.h" + + +/** + * \ingroup UnitTests + * This is a unit test for the operations on curve geometries + */ +class TestQgsSettingsEntry : public QObject +{ + Q_OBJECT + + private slots: + void enumValue(); + void flagValue(); +}; + + +void TestQgsSettingsEntry::enumValue() +{ + QString settingsKey( "qgis/testing/my_enum_value_for_units" ); + + // Make sure the setting is not existing + QgsSettings().remove( settingsKey, QgsSettings::NoSection ); + + QgsSettingsEntryEnum settingsEntryEnum( settingsKey, QgsSettings::NoSection, QgsUnitTypes::LayoutMeters, "Layout unit" ); + + // Check default value + QCOMPARE( settingsEntryEnum.defaultValue(), QgsUnitTypes::LayoutMeters ); + + // Check set value + { + bool success = settingsEntryEnum.setValue( QgsUnitTypes::LayoutFeet ); + QCOMPARE( success, true ); + QgsUnitTypes::LayoutUnit qgsSettingsValue = QgsSettings().enumValue( settingsKey, QgsUnitTypes::LayoutMeters, QgsSettings::NoSection ); + QCOMPARE( qgsSettingsValue, QgsUnitTypes::LayoutFeet ); + } + + // Check get value + QgsSettings().setEnumValue( settingsKey, QgsUnitTypes::LayoutPicas, QgsSettings::NoSection ); + QCOMPARE( settingsEntryEnum.value(), QgsUnitTypes::LayoutPicas ); + + // Check settings type + QCOMPARE( settingsEntryEnum.settingsType(), QgsSettingsEntryBase::Enum ); + + // assign to inexisting value + { + bool success = settingsEntryEnum.setValue( -1 ); + QCOMPARE( success, false ); + + // Current value should not have changed + QgsUnitTypes::LayoutUnit qgsSettingsValue = QgsSettings().enumValue( settingsKey, QgsUnitTypes::LayoutMeters, QgsSettings::NoSection ); + QCOMPARE( qgsSettingsValue, QgsUnitTypes::LayoutPicas ); + } + + // check that value is stored as string + QCOMPARE( settingsEntryEnum.valueAsVariant().toString(), QMetaEnum::fromType().key( QgsUnitTypes::LayoutPicas ) ); + + // auto conversion of old settings (int to str) + QSettings().setValue( settingsKey, static_cast( QgsUnitTypes::LayoutCentimeters ) ); + QCOMPARE( settingsEntryEnum.valueAsVariant().toInt(), QgsUnitTypes::LayoutCentimeters ); + QCOMPARE( settingsEntryEnum.value(), QgsUnitTypes::LayoutCentimeters ); +} + +void TestQgsSettingsEntry::flagValue() +{ + QString settingsKey( "qgis/testing/my_flag_value_for_units" ); + QgsMapLayerProxyModel::Filters pointAndLine = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::PointLayer | QgsMapLayerProxyModel::LineLayer ); + QgsMapLayerProxyModel::Filters pointAndPolygon = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::PointLayer | QgsMapLayerProxyModel::PolygonLayer ); + QgsMapLayerProxyModel::Filters hasGeometry = QgsMapLayerProxyModel::Filters( QgsMapLayerProxyModel::HasGeometry ); + + // Make sure the setting is not existing + QgsSettings().remove( settingsKey, QgsSettings::NoSection ); + + QgsSettingsEntryFlag settingsEntryFlag( settingsKey, QgsSettings::NoSection, pointAndLine, "Filters" ); + + // Check default value + QCOMPARE( settingsEntryFlag.defaultValue(), pointAndLine ); + + // Check set value + { + bool success = settingsEntryFlag.setValue( hasGeometry ); + QCOMPARE( success, true ); + QgsMapLayerProxyModel::Filters qgsSettingsValue = QgsSettings().flagValue( settingsKey, pointAndLine, QgsSettings::NoSection ); + QCOMPARE( qgsSettingsValue, hasGeometry ); + } + + // Check get value + QgsSettings().setFlagValue( settingsKey, pointAndLine, QgsSettings::NoSection ); + QCOMPARE( settingsEntryFlag.value(), pointAndLine ); + + // Check settings type + QCOMPARE( settingsEntryFlag.settingsType(), QgsSettingsEntryBase::Flag ); + + // check that value is stored as string + QCOMPARE( settingsEntryFlag.valueAsVariant().toByteArray(), QMetaEnum::fromType().valueToKeys( pointAndLine ) ); + + // auto conversion of old settings (int to str) + QSettings().setValue( settingsKey, static_cast( pointAndPolygon ) ); + QCOMPARE( settingsEntryFlag.valueAsVariant().toInt(), pointAndPolygon ); + QCOMPARE( settingsEntryFlag.value(), pointAndPolygon ); +} + + +QGSTEST_MAIN( TestQgsSettingsEntry ) +#include "testqgssettingsentry.moc" From 1e45ad64652fd3b53bec81756117719e511c7262 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 2 Apr 2021 21:29:13 +0200 Subject: [PATCH 11/20] fix warnings --- src/core/settings/qgssettingsentry.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 4add21198a59..96ca0be35473 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -616,7 +616,7 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase T value( const QString &dynamicKeyPart = QString() ) const { return QgsSettings().enumValue( key( dynamicKeyPart ), - defaultValueAsVariant().value(), + defaultValueAsVariant().template value(), section() ); } @@ -724,7 +724,7 @@ class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryBase T value( const QString &dynamicKeyPart = QString() ) const { return QgsSettings().flagValue( key( dynamicKeyPart ), - defaultValueAsVariant().value(), + defaultValueAsVariant().template value(), section() ); } From 767923937fe0a046fc166e5fa56f49dd1f735e08 Mon Sep 17 00:00:00 2001 From: Damiano Date: Mon, 5 Apr 2021 00:14:14 +0200 Subject: [PATCH 12/20] Added python tests for settings entries classes --- .../settings/qgssettingsentry.sip.in | 98 ++++--- src/core/settings/qgssettingsentry.cpp | 113 ++++++-- src/core/settings/qgssettingsentry.h | 146 ++++++++-- tests/src/python/CMakeLists.txt | 1 + tests/src/python/test_qgssettingsentry.py | 252 ++++++++++++++++++ 5 files changed, 523 insertions(+), 87 deletions(-) create mode 100644 tests/src/python/test_qgssettingsentry.py diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index aa7714e9a116..14787c3ec76d 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -53,15 +53,17 @@ to validate set values and provide more accurate settings description for the gu Flag }; - QgsSettingsEntryBase( QString key, - QgsSettings::Section section, - QVariant defaultValue = QVariant(), - QString description = QString() ); + + QgsSettingsEntryBase( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); %Docstring Constructor for :py:class:`QgsSettingsEntry`. +This constructor is intended to be used from plugins. The ``key`` argument specifies the key of the settings. -The ``section`` argument specifies the section of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End @@ -144,20 +146,22 @@ A variant settings entry. %End public: + QgsSettingsEntryVariant( const QString &key, - QgsSettings::Section section, + const QString &pluginName, const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryVariant. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End + virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring @@ -196,23 +200,19 @@ A string settings entry. %End public: + QgsSettingsEntryString( const QString &key, - QgsSettings::Section section, - const QString &defaultValue = QString(), - const QString &description = QString(), - int minLength = 0, - int maxLength = -1 ); + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryString. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. -The ``minLength`` argument specifies the minimal length of the string value. -The ``maxLength`` argument specifies the maximal length of the string value. -By -1 the there is no limit %End virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; @@ -236,11 +236,25 @@ Get settings default value. virtual SettingsType settingsType() const; %Docstring \copydoc :py:class:`QgsSettingsEntry`.settingsType +%End + + void setMinLength( int minLength ); +%Docstring +Set the string minimum length. + +minLength The string minimum length. %End int minLength(); %Docstring Returns the string minimum length. +%End + + void setMaxLength( int maxLength ); +%Docstring +Set the string maximum length. + +maxLength The string maximum length. %End int maxLength(); @@ -264,16 +278,17 @@ A string list settings entry. %End public: + QgsSettingsEntryStringList( const QString &key, - QgsSettings::Section section, - const QStringList &defaultValue = QStringList(), + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryStringList. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End @@ -317,16 +332,17 @@ A boolean settings entry. %End public: + QgsSettingsEntryBool( const QString &key, - QgsSettings::Section section, - bool defaultValue = false, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryBool. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End @@ -353,7 +369,6 @@ Get settings default value. %Docstring \copydoc :py:class:`QgsSettingsEntry`.settingsType %End - }; @@ -373,15 +388,15 @@ An integer settings entry. QgsSettingsEntryInteger( const QString &key, - QgsSettings::Section section, - qlonglong defaultValue = 0, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryInteger. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End @@ -452,20 +467,19 @@ A double settings entry. QgsSettingsEntryDouble( const QString &key, - QgsSettings::Section section, - double defaultValue = 0.0, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryDouble. +This constructor is intended to be used from plugins. -The ``key`` argument specifies the final part of the settings key. -The ``parentGroup`` argument specifies a parent group which is used to rebuild -the entiere settings key and to determine the settings section. +The ``key`` argument specifies the key of the settings. +The ``pluginName`` argument is inserted in the key after the section. The ``default`` value argument specifies the default value for the settings entry. The ``description`` argument specifies a description for the settings entry. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index 12014bccb034..2146cf33577d 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -17,14 +17,21 @@ #include "qgslogger.h" -QgsSettingsEntryBase::QgsSettingsEntryBase( QString key, - QgsSettings::Section section, - QVariant defaultValue, - QString description ) +QgsSettingsEntryBase::QgsSettingsEntryBase( const QString &key, QgsSettings::Section section, const QVariant &defaultValue, const QString &description ) : mKey( key ) , mDefaultValue( defaultValue ) , mSection( section ) , mDescription( description ) + , mPluginName() +{ +} + +QgsSettingsEntryBase::QgsSettingsEntryBase( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : mKey( key ) + , mDefaultValue( defaultValue ) + , mSection( QgsSettings::Plugins ) + , mDescription( description ) + , mPluginName( pluginName ) { } @@ -34,23 +41,26 @@ QgsSettingsEntryBase::~QgsSettingsEntryBase() QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const { - if ( dynamicKeyPart.isEmpty() == false ) + QString completeKey = mKey; + if ( !mPluginName.isEmpty() ) + completeKey.prepend( mPluginName + "/" ); + + if ( dynamicKeyPart.isEmpty() ) { - if ( hasDynamicKey() == false ) - { - QgsLogger::warning( QStringLiteral( "Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( mKey ) ); - return mKey; - } + if ( hasDynamicKey() ) + QgsLogger::warning( QStringLiteral( "Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( completeKey ) ); - QString completeKey = mKey; - return completeKey.replace( '%', dynamicKeyPart ); + return completeKey; } else { - if ( hasDynamicKey() == true ) - QgsLogger::warning( QStringLiteral( "Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( mKey ) ); + if ( !hasDynamicKey() ) + { + QgsLogger::warning( QStringLiteral( "Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( completeKey ) ); + return completeKey; + } - return mKey; + return completeKey.replace( '%', dynamicKeyPart ); } } @@ -108,6 +118,14 @@ QgsSettingsEntryVariant::QgsSettingsEntryVariant( const QString &key, QgsSetting { } +QgsSettingsEntryVariant::QgsSettingsEntryVariant( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) +{ +} + bool QgsSettingsEntryVariant::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); @@ -138,6 +156,16 @@ QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, QgsSettings: { } +QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) + , mMinLength( 0 ) + , mMaxLength( -1 ) +{ +} + bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( value.canConvert() == false ) @@ -186,20 +214,27 @@ QgsSettingsEntryBase::SettingsType QgsSettingsEntryString::settingsType() const return QgsSettingsEntryBase::String; } +void QgsSettingsEntryString::setMinLength( int minLength ) +{ + mMinLength = minLength; +} + int QgsSettingsEntryString::minLength() { return mMinLength; } +void QgsSettingsEntryString::setMaxLength( int maxLength ) +{ + mMaxLength = maxLength; +} + int QgsSettingsEntryString::maxLength() { return mMaxLength; } -QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, - QgsSettings::Section section, - const QStringList &defaultValue, - const QString &description ) +QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, QgsSettings::Section section, const QStringList &defaultValue, const QString &description ) : QgsSettingsEntryBase( key, section, defaultValue, @@ -207,6 +242,14 @@ QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, { } +QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) +{ +} + bool QgsSettingsEntryStringList::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( value.canConvert() == false ) @@ -235,10 +278,7 @@ QgsSettingsEntryBase::SettingsType QgsSettingsEntryStringList::settingsType() co return QgsSettingsEntryBase::StringList; } -QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, - QgsSettings::Section section, - bool defaultValue, - const QString &description ) +QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, QgsSettings::Section section, bool defaultValue, const QString &description ) : QgsSettingsEntryBase( key, section, defaultValue, @@ -246,6 +286,14 @@ QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, { } +QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) +{ +} + bool QgsSettingsEntryBool::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { if ( value.canConvert() == false ) @@ -282,7 +330,16 @@ QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, QgsSetting , mMinValue( minValue ) , mMaxValue( maxValue ) { +} +QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) + , mMinValue( std::numeric_limits::min() ) + , mMaxValue( std::numeric_limits::max() ) +{ } bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dynamicKeyPart ) const @@ -361,7 +418,17 @@ QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, QgsSettings: , mMaxValue( maxValue ) , mDisplayHintDecimals( displayDecimals ) { +} +QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) + : QgsSettingsEntryBase( key, + pluginName, + defaultValue, + description ) + , mMinValue( std::numeric_limits::min() ) + , mMaxValue( std::numeric_limits::max() ) + , mDisplayHintDecimals( 1 ) +{ } bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dynamicKeyPart ) const diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 96ca0be35473..810cd3281cc4 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -69,6 +69,8 @@ class CORE_EXPORT QgsSettingsEntryBase Flag }; +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntry. * @@ -77,10 +79,26 @@ class CORE_EXPORT QgsSettingsEntryBase * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ - QgsSettingsEntryBase( QString key, + QgsSettingsEntryBase( const QString &key, QgsSettings::Section section, - QVariant defaultValue = QVariant(), - QString description = QString() ); + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + +#endif + + /** + * Constructor for QgsSettingsEntry. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryBase( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); /** * Destructor for QgsSettingsEntry. @@ -153,7 +171,7 @@ class CORE_EXPORT QgsSettingsEntryBase QVariant mDefaultValue; QgsSettings::Section mSection; QString mDescription; - + QString mPluginName; }; @@ -167,6 +185,8 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase { public: +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntryVariant. * @@ -181,6 +201,23 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase const QVariant &defaultValue = QVariant(), const QString &description = QString() ); +#endif + + /** + * Constructor for QgsSettingsEntryVariant. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryVariant( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + + //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -211,6 +248,8 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase { public: +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntryString. * @@ -230,6 +269,22 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase int minLength = 0, int maxLength = -1 ); +#endif + + /** + * Constructor for QgsSettingsEntryString. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryString( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -248,11 +303,25 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; + /** + * Set the string minimum length. + * + * minLength The string minimum length. + */ + void setMinLength( int minLength ); + /** * Returns the string minimum length. */ int minLength(); + /** + * Set the string maximum length. + * + * maxLength The string maximum length. + */ + void setMaxLength( int maxLength ); + /** * Returns the string maximum length. By -1 there is no limitation. */ @@ -276,6 +345,8 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase { public: +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntryStringList. * @@ -290,6 +361,22 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase const QStringList &defaultValue = QStringList(), const QString &description = QString() ); +#endif + + /** + * Constructor for QgsSettingsEntryStringList. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryStringList( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -321,6 +408,8 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase { public: +#ifndef SIP_RUN + /** * Constructor for QgsSettingsEntryBool. * @@ -335,6 +424,22 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase bool defaultValue = false, const QString &description = QString() ); +#endif + + /** + * Constructor for QgsSettingsEntryBool. + * This constructor is intended to be used from plugins. + * + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + */ + QgsSettingsEntryBool( const QString &key, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), + const QString &description = QString() ); + //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -352,7 +457,6 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase //! \copydoc QgsSettingsEntry::settingsType virtual SettingsType settingsType() const override; - }; @@ -386,23 +490,23 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase const QString &description = QString(), qlonglong minValue = std::numeric_limits::min(), qlonglong maxValue = std::numeric_limits::max() ); -#else + +#endif /** * Constructor for QgsSettingsEntryInteger. + * This constructor is intended to be used from plugins. * - * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ QgsSettingsEntryInteger( const QString &key, - QgsSettings::Section section, - qlonglong defaultValue = 0, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); -#endif //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -487,24 +591,22 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase double maxValue = std::numeric_limits::max(), int displayDecimals = 1 ); -#else +#endif /** * Constructor for QgsSettingsEntryDouble. + * This constructor is intended to be used from plugins. * - * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a key argument specifies the key of the settings. + * The \a pluginName argument is inserted in the key after the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ QgsSettingsEntryDouble( const QString &key, - QgsSettings::Section section, - double defaultValue = 0.0, + const QString &pluginName, + const QVariant &defaultValue = QVariant(), const QString &description = QString() ); -#endif - //! \copydoc QgsSettingsEntry::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; @@ -616,7 +718,7 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase T value( const QString &dynamicKeyPart = QString() ) const { return QgsSettings().enumValue( key( dynamicKeyPart ), - defaultValueAsVariant().template value(), + defaultValueAsVariant().value(), section() ); } @@ -724,7 +826,7 @@ class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryBase T value( const QString &dynamicKeyPart = QString() ) const { return QgsSettings().flagValue( key( dynamicKeyPart ), - defaultValueAsVariant().template value(), + defaultValueAsVariant().value(), section() ); } diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index b4110721387a..4ef757ed589e 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -340,6 +340,7 @@ ADD_PYTHON_TEST(PyQgsDBManagerGpkg test_db_manager_gpkg.py) ADD_PYTHON_TEST(PyQgsDBManagerSpatialite test_db_manager_spatialite.py) ADD_PYTHON_TEST(PyQgsFileDownloader test_qgsfiledownloader.py) ADD_PYTHON_TEST(PyQgsSettings test_qgssettings.py) +ADD_PYTHON_TEST(PyQgsSettingsEntry test_qgssettingsentry.py) ADD_PYTHON_TEST(PyQgsZipUtils test_qgsziputils.py) ADD_PYTHON_TEST(PyQgsSourceSelectProvider test_qgssourceselectprovider.py) ADD_PYTHON_TEST(PyQgsAuthManagerProxy test_authmanager_proxy.py) diff --git a/tests/src/python/test_qgssettingsentry.py b/tests/src/python/test_qgssettingsentry.py new file mode 100644 index 000000000000..195bc8269020 --- /dev/null +++ b/tests/src/python/test_qgssettingsentry.py @@ -0,0 +1,252 @@ +# -*- coding: utf-8 -*- +""" +Test the QgsSettingsEntry classes + +Run with: ctest -V -R PyQgsSettingsEntry + +.. note:: This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. +""" + +import os +import tempfile +from qgis.core import QgsSettings, QgsSettingsEntryBase, QgsSettingsEntryVariant, QgsSettingsEntryString, QgsSettingsEntryStringList, QgsSettingsEntryBool, QgsSettingsEntryInteger, QgsSettingsEntryDouble, QgsTolerance, QgsMapLayerProxyModel +from qgis.testing import start_app, unittest +from qgis.PyQt.QtCore import QSettings, QVariant +from pathlib import Path + +__author__ = 'Damiano Lombardi' +__date__ = '02/04/2021' +__copyright__ = 'Copyright 2021, The QGIS Project' + + +start_app() + + +class TestQgsSettingsEntry(unittest.TestCase): + + cnt = 0 + + def setUp(self): + self.pluginName = "UnitTest" + + def tearDown(self): + pass + + def test_settings_entry_base(self): + settingsKey = "settingsEntryBase/variantValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 42 + description = "Variant value for basic functionality test" + settingsEntryVariant = QgsSettingsEntryVariant(settingsKey, self.pluginName, defaultValue, description) + + # Check key + self.assertEqual(settingsEntryVariant.key(), settingsKeyComplete) + + # Passing dynamicKeyPart to a non dynamic settings has no effect + self.assertEqual(settingsEntryVariant.key("gugus"), settingsKeyComplete) + + self.assertEqual(settingsEntryVariant.hasDynamicKey(), False) + + # At this point settings should still not exists in underlyng QSettings as it was still not written (setValue) + self.assertEqual(settingsEntryVariant.exists(), False) + settingsEntryVariant.setValue(43) + self.assertEqual(settingsEntryVariant.exists(), True) + settingsEntryVariant.remove() + self.assertEqual(settingsEntryVariant.exists(), False) + + # Section + self.assertEqual(settingsEntryVariant.section(), QgsSettings.Plugins) + + # DefaultValue + self.assertEqual(settingsEntryVariant.defaultValueAsVariant(), defaultValue) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryVariant.valueAsVariant(), defaultValue) + settingsEntryVariant.setValue(43) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), 43) + self.assertEqual(settingsEntryVariant.valueAsVariant(), 43) + + # Settings type + self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.Variant) + + # Description + self.assertEqual(settingsEntryVariant.description(), description) + + def test_settings_entry_base_dynamic_key(self): + settingsKeyDynamic = "settingsEntryBase/%/variantValue" + dynamicKeyPart1 = "first" + dynamicKeyPart2 = "second" + settingsKeyComplete1 = self.pluginName + "/" + settingsKeyDynamic.replace("%", dynamicKeyPart1) + settingsKeyComplete2 = self.pluginName + "/" + settingsKeyDynamic.replace("%", dynamicKeyPart2) + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete1, QgsSettings.Plugins) + QgsSettings().remove(settingsKeyComplete2, QgsSettings.Plugins) + + defaultValue = 42 + settingsEntryVariantDynamic = QgsSettingsEntryVariant(settingsKeyDynamic, self.pluginName, defaultValue, "Variant value for dynamic key functionality test") + + # Check key + self.assertEqual(settingsEntryVariantDynamic.key(dynamicKeyPart1), settingsKeyComplete1) + self.assertEqual(settingsEntryVariantDynamic.key(dynamicKeyPart2), settingsKeyComplete2) + + # Get set values + settingsEntryVariantDynamic.setValue(43, dynamicKeyPart1) + settingsEntryVariantDynamic.setValue(44, dynamicKeyPart2) + self.assertEqual(QgsSettings().value(settingsKeyComplete1, defaultValue, section=QgsSettings.Plugins), 43) + self.assertEqual(QgsSettings().value(settingsKeyComplete2, defaultValue, section=QgsSettings.Plugins), 44) + self.assertEqual(settingsEntryVariantDynamic.value(dynamicKeyPart1), 43) + self.assertEqual(settingsEntryVariantDynamic.value(dynamicKeyPart2), 44) + + def test_settings_entry_variant(self): + settingsKey = "settingsEntryVariant/variantValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 42 + description = "Variant value functionality test" + settingsEntryVariant = QgsSettingsEntryVariant(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryVariant.valueAsVariant(), defaultValue) + settingsEntryVariant.setValue("abc") + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), "abc") + self.assertEqual(settingsEntryVariant.valueAsVariant(), "abc") + + # Settings type + self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.Variant) + + def test_settings_entry_string(self): + settingsKey = "settingsEntryString/stringValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = "abc" + description = "String value functionality test" + settingsEntryString = QgsSettingsEntryString(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryString.valueAsVariant(), defaultValue) + settingsEntryString.setValue("xyz") + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), "xyz") + self.assertEqual(settingsEntryString.valueAsVariant(), "xyz") + + # Settings type + self.assertEqual(settingsEntryString.settingsType(), QgsSettingsEntryBase.String) + + def test_settings_entry_stringlist(self): + settingsKey = "settingsEntryStringList/stringListValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = ["abc", "def"] + description = "String list value functionality test" + settingsEntryStringList = QgsSettingsEntryStringList(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryStringList.valueAsVariant(), defaultValue) + settingsEntryStringList.setValue(["uvw", "xyz"]) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), ["uvw", "xyz"]) + self.assertEqual(settingsEntryStringList.valueAsVariant(), ["uvw", "xyz"]) + + # Settings type + self.assertEqual(settingsEntryStringList.settingsType(), QgsSettingsEntryBase.StringList) + + def test_settings_entry_bool(self): + settingsKey = "settingsEntryBool/boolValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = False + description = "Bool value functionality test" + settingsEntryBool = QgsSettingsEntryBool(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryBool.valueAsVariant(), defaultValue) + settingsEntryBool.setValue(True) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), True) + self.assertEqual(settingsEntryBool.valueAsVariant(), True) + + # Settings type + self.assertEqual(settingsEntryBool.settingsType(), QgsSettingsEntryBase.Bool) + + def test_settings_entry_integer(self): + settingsKey = "settingsEntryInteger/integerValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 42 + description = "Integer value functionality test" + settingsEntryInteger = QgsSettingsEntryInteger(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryInteger.valueAsVariant(), defaultValue) + settingsEntryInteger.setValue(43) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), 43) + self.assertEqual(settingsEntryInteger.valueAsVariant(), 43) + + # Settings type + self.assertEqual(settingsEntryInteger.settingsType(), QgsSettingsEntryBase.Integer) + + def test_settings_entry_double(self): + settingsKey = "settingsEntryDouble/doubleValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 3.14 + description = "Double value functionality test" + settingsEntryDouble = QgsSettingsEntryDouble(settingsKey, self.pluginName, defaultValue, description) + + # Set/Get value + # as settings still does not exists return default value + self.assertEqual(settingsEntryDouble.valueAsVariant(), defaultValue) + settingsEntryDouble.setValue(1.618) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), 1.618) + self.assertEqual(settingsEntryDouble.valueAsVariant(), 1.618) + + # Settings type + self.assertEqual(settingsEntryDouble.settingsType(), QgsSettingsEntryBase.Double) + + def test_settings_entry_enum(self): + # Todo implement QgsSettingsEntryEnum for python + pass + + def test_settings_entry_flag(self): + # Todo implement QgsSettingsEntryFlag for python + pass + + +if __name__ == '__main__': + unittest.main() From 4e959f95bf0129ce185c84efa32d5af25b3286b5 Mon Sep 17 00:00:00 2001 From: Damiano Date: Tue, 6 Apr 2021 10:52:33 +0200 Subject: [PATCH 13/20] Fix default value --- src/core/settings/qgssettingsentry.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 810cd3281cc4..85e2b9d10d67 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -718,7 +718,7 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase T value( const QString &dynamicKeyPart = QString() ) const { return QgsSettings().enumValue( key( dynamicKeyPart ), - defaultValueAsVariant().value(), + defaultValue(), section() ); } @@ -826,7 +826,7 @@ class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryBase T value( const QString &dynamicKeyPart = QString() ) const { return QgsSettings().flagValue( key( dynamicKeyPart ), - defaultValueAsVariant().value(), + defaultValue(), section() ); } From 3c5f9b1e9193521ad9a9fd8434f8de94050f69bb Mon Sep 17 00:00:00 2001 From: Damiano Date: Wed, 7 Apr 2021 14:02:08 +0200 Subject: [PATCH 14/20] Python implementation for QgsSettingsEntryEnum/Flag and fixed docstrings --- python/core/__init__.py.in | 5 + python/core/additions/qgssettings.py | 2 +- python/core/additions/qgssettingsentry.py | 200 ++++++++++++++++++ .../auto_generated/layout/qgslayout.sip.in | 1 - .../settings/qgssettingsentry.sip.in | 24 +-- .../settings/qgssettingsregistrycore.sip.in | 9 +- src/app/locator/qgslocatoroptionswidget.cpp | 12 +- src/core/layout/qgslayout.h | 6 +- src/core/locator/qgslocator.cpp | 4 +- src/core/locator/qgslocator.h | 11 +- src/core/settings/qgssettingsentry.h | 67 +++--- src/core/settings/qgssettingsregistrycore.h | 11 +- tests/src/python/test_qgssettingsentry.py | 69 +++++- 13 files changed, 349 insertions(+), 72 deletions(-) create mode 100644 python/core/additions/qgssettingsentry.py diff --git a/python/core/__init__.py.in b/python/core/__init__.py.in index 77e240a55595..76eb01d58c74 100644 --- a/python/core/__init__.py.in +++ b/python/core/__init__.py.in @@ -34,6 +34,7 @@ from .additions.qgsfeature import mapping_feature from .additions.qgsfunction import register_function, qgsfunction from .additions.qgsgeometry import _geometryNonZero, mapping_geometry from .additions.qgssettings import _qgssettings_enum_value, _qgssettings_set_enum_value, _qgssettings_flag_value +from .additions.qgssettingsentry import PyQgsSettingsEntryEnum, PyQgsSettingsEntryFlag from .additions.qgstaskwrapper import QgsTaskWrapper from .additions.readwritecontextentercategory import ReadWriteContextEnterCategory from .additions.runtimeprofiler import ScopedRuntimeProfileContextManager @@ -57,6 +58,10 @@ QgsTask.fromFunction = fromFunction QgsDateTimeRange.__repr__ = datetime_range_repr QgsDateRange.__repr__ = date_range_repr +# Classes patched +QgsSettingsEntryEnum = PyQgsSettingsEntryEnum +QgsSettingsEntryFlag = PyQgsSettingsEntryFlag + # Classes patched using a derived class QgsProviderMetadata = PyProviderMetadata diff --git a/python/core/additions/qgssettings.py b/python/core/additions/qgssettings.py index 655cddffa04d..570e92febdc2 100644 --- a/python/core/additions/qgssettings.py +++ b/python/core/additions/qgssettings.py @@ -110,7 +110,7 @@ def _qgssettings_flag_value(self, key, flagDefaultValue, section=QgsSettings.NoS # this should not happen raise ValueError("could not get the meta enum for given enum default value (type: {})".format(type(flagDefaultValue))) - str_val = self.value(key, meta_enum.valueToKey(flagDefaultValue), str, section) + str_val = self.value(key, meta_enum.valueToKeys(flagDefaultValue), str, section) # need a new meta enum as QgsSettings.value is making a copy and leads to seg fault (probably a PyQt issue) meta_enum_2 = metaEnumFromValue(flagDefaultValue) (flag_val, ok) = meta_enum_2.keysToValue(str_val) diff --git a/python/core/additions/qgssettingsentry.py b/python/core/additions/qgssettingsentry.py new file mode 100644 index 000000000000..aa61b4983f85 --- /dev/null +++ b/python/core/additions/qgssettingsentry.py @@ -0,0 +1,200 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + qgssettingsentry.py + --------------------- + Date : April 2021 + Copyright : (C) 2021 by Damiano Lombardi + Email : damiano@opengis.ch +*************************************************************************** +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +*************************************************************************** +""" + +from .metaenum import metaEnumFromValue +from qgis.core import QgsSettings, QgsSettingsEntryBase, QgsLogger +import qgis # required to get base class of enums + + +class PyQgsSettingsEntryEnum(QgsSettingsEntryBase): + """ class QgsSettingsEntryEnum + ingroup core + An enum settings entry. + since QGIS 3.20 + """ + + def __init__(self, key, pluginName, defaultValue, description=str()): + """ Constructor for QgsSettingsEntryEnum. + + :param self: the QgsSettingsEntryEnum object + :param key: argument specifies the final part of the settings key. + :param pluginName: argument is inserted in the key after the section. + :param defaultValue: argument specifies the default value for the settings entry. + :param description: argument specifies a description for the settings entry. + + .. note:: The enum needs to be declared with Q_ENUM. + """ + + defaultValueStr = str() + self.__metaEnum = metaEnumFromValue(defaultValue) + if self.__metaEnum is None or not self.__metaEnum.isValid(): + QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.") + else: + defaultValueStr = self.__metaEnum.valueToKey(defaultValue) + self.__enumClass = defaultValue.__class__ + + super().__init__(key, pluginName, defaultValueStr, description) + + def value(self, dynamicKeyPart=str()): + """ Get settings value. + + :param self: the QgsSettingsEntryEnum object + :param dynamicKeyPart: argument specifies the dynamic part of the settings key. + """ + + return QgsSettings().enumValue(self.key(dynamicKeyPart), + self.defaultValue(), + self.section()) + + def defaultValue(self): + """ Get settings default value. + + :param self: the QgsSettingsEntryEnum object + """ + + if self.__metaEnum is None or not self.__metaEnum.isValid(): + QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.") + return -1 + + defaultValueString = self.defaultValueAsVariant() + (defaultValue, ok) = self.__metaEnum.keyToValue(defaultValueString) + if not ok: + QgsLogger.debug("Invalid enum key '{0}'.".format(self.defaultValueAsVariant())) + return -1 + + # cast to the enum class + defaultValue = self.__enumClass(defaultValue) + return defaultValue + + def setValue(self, value, dynamicKeyPart=str()): + """ Set settings value. + + :param self: the QgsSettingsEntryEnum object + :param dynamicKeyPart: argument specifies the dynamic part of the settings key. + """ + + if self.__metaEnum is None or not self.__metaEnum.isValid(): + QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.") + return False + + enumKey = str() + enumKey = self.__metaEnum.valueToKey(value) + if not enumKey: + QgsLogger.debug("Invalid enum value '{0}'.".format(value)) + return False + + super().setValue(enumKey, dynamicKeyPart) + return True + + def settingsType(self): + """ Get the settings entry type. + + :param self: the QgsSettingsEntryEnum object + """ + + return self.Enum + + +class PyQgsSettingsEntryFlag(QgsSettingsEntryBase): + """ class QgsSettingsEntryFlag + ingroup core + A flag settings entry. + since QGIS 3.20 + """ + + def __init__(self, key, pluginName, defaultValue, description=str()): + """ Constructor for QgsSettingsEntryFlag. + + :param self: the QgsSettingsEntryFlag object + :param key: argument specifies the final part of the settings key. + :param pluginName: argument is inserted in the key after the section. + :param defaultValue: argument specifies the default value for the settings entry. + :param description: argument specifies a description for the settings entry. + + .. note:: The flag needs to be declared with Q_FLAG (not Q_FLAGS). + """ + + defaultValueStr = str() + self.__metaEnum = metaEnumFromValue(defaultValue) + if self.__metaEnum is None or not self.__metaEnum.isValid(): + QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.") + else: + defaultValueStr = self.__metaEnum.valueToKeys(defaultValue) + self.__flagClass = defaultValue.__class__ + + super().__init__(key, pluginName, defaultValueStr, description) + + def value(self, dynamicKeyPart=str()): + """ Get settings value. + + :param self: the QgsSettingsEntryFlag object + :param dynamicKeyPart: argument specifies the dynamic part of the settings key. + """ + + return QgsSettings().flagValue(self.key(dynamicKeyPart), + self.defaultValue(), + self.section()) + + def defaultValue(self): + """ Get settings default value. + + :param self: the QgsSettingsEntryFlag object + """ + + if self.__metaEnum is None or not self.__metaEnum.isValid(): + QgsLogger.debug("Invalid metaenum. Flag probably misses Q_ENUM or Q_FLAG declaration.") + return -1 + + defaultValueString = self.defaultValueAsVariant() + (defaultValue, ok) = self.__metaEnum.keysToValue(defaultValueString) + if not ok: + QgsLogger.debug("Invalid flag keys '{0}'.".format(self.defaultValueAsVariant())) + return -1 + + # cast to the flag class + defaultValue = self.__flagClass(defaultValue) + return defaultValue + + def setValue(self, value, dynamicKeyPart=str()): + """ Set settings value. + + :param self: the QgsSettingsEntryFlag object + :param dynamicKeyPart: argument specifies the dynamic part of the settings key. + """ + + if self.__metaEnum is None or not self.__metaEnum.isValid(): + QgsLogger.debug("Invalid metaenum. Flag probably misses Q_ENUM or Q_FLAG declaration.") + return False + + flagKeys = str() + flagKeys = self.__metaEnum.valueToKeys(value) + if not flagKeys: + QgsLogger.debug("Invalid flag value '{0}'.".format(value)) + return False + + super().setValue(flagKeys, dynamicKeyPart) + return True + + def settingsType(self): + """ Get the settings entry type. + + :param self: the QgsSettingsEntryFlag object + """ + + return self.Flag diff --git a/python/core/auto_generated/layout/qgslayout.sip.in b/python/core/auto_generated/layout/qgslayout.sip.in index 0949ed2aa827..274d45f8e589 100644 --- a/python/core/auto_generated/layout/qgslayout.sip.in +++ b/python/core/auto_generated/layout/qgslayout.sip.in @@ -614,7 +614,6 @@ should be canceled. .. versionadded:: 3.10 %End - struct Settings { const QgsSettingsEntryStringList searchPathForTemplates; diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index 14787c3ec76d..f84c7539d0dc 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -165,7 +165,7 @@ The ``description`` argument specifies a description for the settings entry. virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.setValue +\copydoc :py:class:`QgsSettingsEntryBase`.setValue %End QVariant value( const QString &dynamicKeyPart = QString() ) const; @@ -182,7 +182,7 @@ Get settings default value. virtual SettingsType settingsType() const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.settingsType +\copydoc :py:class:`QgsSettingsEntryBase`.settingsType %End }; @@ -218,7 +218,7 @@ The ``description`` argument specifies a description for the settings entry. virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.setValue +\copydoc :py:class:`QgsSettingsEntryBase`.setValue %End QString value( const QString &dynamicKeyPart = QString() ) const; @@ -235,7 +235,7 @@ Get settings default value. virtual SettingsType settingsType() const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.settingsType +\copydoc :py:class:`QgsSettingsEntryBase`.settingsType %End void setMinLength( int minLength ); @@ -296,7 +296,7 @@ The ``description`` argument specifies a description for the settings entry. virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.setValue +\copydoc :py:class:`QgsSettingsEntryBase`.setValue %End QStringList value( const QString &dynamicKeyPart = QString() ) const; @@ -313,7 +313,7 @@ Get settings default value. virtual SettingsType settingsType() const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.settingsType +\copydoc :py:class:`QgsSettingsEntryBase`.settingsType %End }; @@ -350,7 +350,7 @@ The ``description`` argument specifies a description for the settings entry. virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.setValue +\copydoc :py:class:`QgsSettingsEntryBase`.setValue %End bool value( const QString &dynamicKeyPart = QString() ) const; @@ -367,7 +367,7 @@ Get settings default value. virtual SettingsType settingsType() const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.settingsType +\copydoc :py:class:`QgsSettingsEntryBase`.settingsType %End }; @@ -405,7 +405,7 @@ The ``description`` argument specifies a description for the settings entry. virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.setValue +\copydoc :py:class:`QgsSettingsEntryBase`.setValue %End qlonglong value( const QString &dynamicKeyPart = QString() ) const; @@ -422,7 +422,7 @@ Get settings default value. virtual SettingsType settingsType() const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.settingsType +\copydoc :py:class:`QgsSettingsEntryBase`.settingsType %End void setMinValue( qlonglong minValue ); @@ -483,7 +483,7 @@ The ``description`` argument specifies a description for the settings entry. virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.setValue +\copydoc :py:class:`QgsSettingsEntryBase`.setValue %End double value( const QString &dynamicKeyPart = QString() ) const; @@ -500,7 +500,7 @@ Get settings default value. virtual SettingsType settingsType() const; %Docstring -\copydoc :py:class:`QgsSettingsEntry`.settingsType +\copydoc :py:class:`QgsSettingsEntryBase`.settingsType %End void setMinValue( double minValue ); diff --git a/python/core/auto_generated/settings/qgssettingsregistrycore.sip.in b/python/core/auto_generated/settings/qgssettingsregistrycore.sip.in index 531e9332b0c8..5f77a9252559 100644 --- a/python/core/auto_generated/settings/qgssettingsregistrycore.sip.in +++ b/python/core/auto_generated/settings/qgssettingsregistrycore.sip.in @@ -15,7 +15,8 @@ class QgsSettingsRegistryCore { %Docstring(signature="appended") - +:py:class:`QgsSettingsRegistryCore` is used for settings introspection and collects all +:py:class:`QgsSettingsEntry` instances of core. .. versionadded:: 3.20 %End @@ -26,7 +27,11 @@ class QgsSettingsRegistryCore public: QgsSettingsRegistryCore(); - ~QgsSettingsRegistryCore(); +%Docstring +Constructor for QgsSettingsRegistryCore. +%End + + virtual ~QgsSettingsRegistryCore(); }; diff --git a/src/app/locator/qgslocatoroptionswidget.cpp b/src/app/locator/qgslocatoroptionswidget.cpp index 4ae17f2cf233..e0188faa656f 100644 --- a/src/app/locator/qgslocatoroptionswidget.cpp +++ b/src/app/locator/qgslocatoroptionswidget.cpp @@ -330,30 +330,26 @@ void QgsLocatorFiltersModel::commitChanges() if ( !activePrefix.isEmpty() && activePrefix != filter->prefix() ) { filter->setActivePrefix( activePrefix ); -// QgsLocator::Settings::LocatorFilterPrefix().setValue( activePrefix, filter->name() ); -// QgsLocator::Settings::locatorFilterPrefix.setValue( activePrefix, filter->name() ); + QgsLocator::Settings::locatorFilterPrefix.setValue( activePrefix, filter->name() ); } else { filter->setActivePrefix( QString() ); -// QgsLocator::Settings::LocatorFilterPrefix().remove( filter->name() ); -// QgsLocator::Settings::locatorFilterPrefix.remove( filter->name() ); + QgsLocator::Settings::locatorFilterPrefix.remove( filter->name() ); } } QHash< QgsLocatorFilter *, bool >::const_iterator it = mEnabledChanges.constBegin(); for ( ; it != mEnabledChanges.constEnd(); ++it ) { QgsLocatorFilter *filter = it.key(); -// QgsLocator::Settings::LocatorFilterEnabled().setValue( it.value(), filter->name() ); -// QgsLocator::Settings::locatorFilterEnabled.setValue( it.value(), filter->name() ); + QgsLocator::Settings::locatorFilterEnabled.setValue( it.value(), filter->name() ); filter->setEnabled( it.value() ); } it = mDefaultChanges.constBegin(); for ( ; it != mDefaultChanges.constEnd(); ++it ) { QgsLocatorFilter *filter = it.key(); -// QgsLocator::Settings::LocatorFilterDefault().setValue( it.value(), filter->name() ); -// QgsLocator::Settings::locatorFilterDefault.setValue( it.value(), filter->name() ); + QgsLocator::Settings::locatorFilterDefault.setValue( it.value(), filter->name() ); filter->setUseWithoutPrefix( it.value() ); } } diff --git a/src/core/layout/qgslayout.h b/src/core/layout/qgslayout.h index ffb82b05fd6e..c896a29a8036 100644 --- a/src/core/layout/qgslayout.h +++ b/src/core/layout/qgslayout.h @@ -658,9 +658,13 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext */ bool accept( QgsStyleEntityVisitorInterface *visitor ) const; - + /** + * Contains QgsLayout settings + * \since QGIS 3.20 + */ struct Settings { + //! Settings entry search path for templates static const inline QgsSettingsEntryStringList searchPathForTemplates = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), QObject::tr( "Search path for templates" ) ); }; diff --git a/src/core/locator/qgslocator.cpp b/src/core/locator/qgslocator.cpp index 5bed9a07910f..d4ed7518f83e 100644 --- a/src/core/locator/qgslocator.cpp +++ b/src/core/locator/qgslocator.cpp @@ -94,9 +94,7 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter ) filter->setParent( this ); // restore settings - bool enabled = true; - if ( QgsLocator::Settings::locatorFilterEnabled.exists( filter->name() ) ) - enabled = QgsLocator::Settings::locatorFilterEnabled.value( filter->name() ); + bool enabled = QgsLocator::Settings::locatorFilterEnabled.value( filter->name() ); bool byDefault = filter->useWithoutPrefix(); if ( QgsLocator::Settings::locatorFilterDefault.exists( filter->name() ) ) byDefault = QgsLocator::Settings::locatorFilterDefault.value( filter->name() ); diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index 0407ae0ca922..d55424c48d4c 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -153,10 +153,17 @@ class CORE_EXPORT QgsLocator : public QObject */ QStringList completionList() const {return mAutocompletionList;} + /** + * Contains QgsLocator settings + * \since QGIS 3.20 + */ struct Settings { - static const inline QgsSettingsEntryBool locatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Enabled" ); - static const inline QgsSettingsEntryBool locatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Default value" ); + //! Settings entry locator filter enabled + static const inline QgsSettingsEntryBool locatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Locator filter enabled" ); + //! Settings entry locator filter default value + static const inline QgsSettingsEntryBool locatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Locator filter default value" ); + //! Settings entry locator filter prefix static const inline QgsSettingsEntryString locatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); }; diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 85e2b9d10d67..84fd681a960b 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -75,7 +75,7 @@ class CORE_EXPORT QgsSettingsEntryBase * Constructor for QgsSettingsEntry. * * The \a key argument specifies the key of the settings. - * The \a section argument specifies the section of the settings. + * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ @@ -191,8 +191,7 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase * Constructor for QgsSettingsEntryVariant. * * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ @@ -218,7 +217,7 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase const QString &description = QString() ); - //! \copydoc QgsSettingsEntry::setValue + //! \copydoc QgsSettingsEntryBase::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; /** @@ -233,7 +232,7 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase */ QVariant defaultValue() const; - //! \copydoc QgsSettingsEntry::settingsType + //! \copydoc QgsSettingsEntryBase::settingsType virtual SettingsType settingsType() const override; }; @@ -254,8 +253,7 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase * Constructor for QgsSettingsEntryString. * * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. * The \a minLength argument specifies the minimal length of the string value. @@ -285,7 +283,7 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase const QVariant &defaultValue = QVariant(), const QString &description = QString() ); - //! \copydoc QgsSettingsEntry::setValue + //! \copydoc QgsSettingsEntryBase::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; /** @@ -300,7 +298,7 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase */ QString defaultValue() const; - //! \copydoc QgsSettingsEntry::settingsType + //! \copydoc QgsSettingsEntryBase::settingsType virtual SettingsType settingsType() const override; /** @@ -351,8 +349,7 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase * Constructor for QgsSettingsEntryStringList. * * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ @@ -377,7 +374,7 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase const QVariant &defaultValue = QVariant(), const QString &description = QString() ); - //! \copydoc QgsSettingsEntry::setValue + //! \copydoc QgsSettingsEntryBase::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; /** @@ -392,7 +389,7 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase */ QStringList defaultValue() const; - //! \copydoc QgsSettingsEntry::settingsType + //! \copydoc QgsSettingsEntryBase::settingsType virtual SettingsType settingsType() const override; }; @@ -414,8 +411,7 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase * Constructor for QgsSettingsEntryBool. * * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. */ @@ -440,7 +436,7 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase const QVariant &defaultValue = QVariant(), const QString &description = QString() ); - //! \copydoc QgsSettingsEntry::setValue + //! \copydoc QgsSettingsEntryBase::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; /** @@ -455,7 +451,7 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase */ bool defaultValue() const; - //! \copydoc QgsSettingsEntry::settingsType + //! \copydoc QgsSettingsEntryBase::settingsType virtual SettingsType settingsType() const override; }; @@ -477,8 +473,7 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase * Constructor for QgsSettingsEntryInteger. * * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. * The \a minValue argument specifies the minimal value. @@ -508,7 +503,7 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase const QString &description = QString() ); - //! \copydoc QgsSettingsEntry::setValue + //! \copydoc QgsSettingsEntryBase::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; /** @@ -523,7 +518,7 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase */ qlonglong defaultValue() const; - //! \copydoc QgsSettingsEntry::settingsType + //! \copydoc QgsSettingsEntryBase::settingsType virtual SettingsType settingsType() const override; /** @@ -574,8 +569,7 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase * Constructor for QgsSettingsEntryDouble. * * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. * The \a minValue argument specifies the minimal value. @@ -607,7 +601,7 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase const QVariant &defaultValue = QVariant(), const QString &description = QString() ); - //! \copydoc QgsSettingsEntry::setValue + //! \copydoc QgsSettingsEntryBase::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; /** @@ -622,7 +616,7 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase */ double defaultValue() const; - //! \copydoc QgsSettingsEntry::settingsType + //! \copydoc QgsSettingsEntryBase::settingsType virtual SettingsType settingsType() const override; /** @@ -688,10 +682,12 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase * Constructor for QgsSettingsEntryEnum. * * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. + * + * \note The enum needs to be declared with Q_ENUM, and flags with Q_FLAG (not Q_FLAGS). + * \note for Python bindings, a custom implementation is achieved in Python directly */ QgsSettingsEntryEnum( const QString &key, QgsSettings::Section section, @@ -745,7 +741,7 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase return defaultValue; } - //! \copydoc QgsSettingsEntry::setValue + //! \copydoc QgsSettingsEntryBase::setValue bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override { if ( !mMetaEnum.isValid() ) @@ -761,11 +757,10 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase return false; } - QgsSettingsEntryBase::setValue( enumKey, dynamicKeyPart ); - return true; + return QgsSettingsEntryBase::setValue( enumKey, dynamicKeyPart ); } - //! \copydoc QgsSettingsEntry::settingsType + //! \copydoc QgsSettingsEntryBase::settingsType virtual SettingsType settingsType() const override { return QgsSettingsEntryBase::Enum; @@ -796,10 +791,12 @@ class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryBase * Constructor for QgsSettingsEntryFlag. * * The \a key argument specifies the final part of the settings key. - * The \a parentGroup argument specifies a parent group which is used to rebuild - * the entiere settings key and to determine the settings section. + * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. + * + * \note The flag needs to be declared with Q_FLAG (not Q_FLAGS). + * \note for Python bindings, a custom implementation is achieved in Python directly. */ QgsSettingsEntryFlag( const QString &key, QgsSettings::Section section, @@ -853,7 +850,7 @@ class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryBase return defaultValue; } - //! \copydoc QgsSettingsEntry::setValue + //! \copydoc QgsSettingsEntryBase::setValue bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const { if ( !mMetaEnum.isValid() ) @@ -873,7 +870,7 @@ class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryBase return true; } - //! \copydoc QgsSettingsEntry::settingsType + //! \copydoc QgsSettingsEntryBase::settingsType virtual SettingsType settingsType() const override { return QgsSettingsEntryBase::Flag; diff --git a/src/core/settings/qgssettingsregistrycore.h b/src/core/settings/qgssettingsregistrycore.h index dcd286a83c4a..933b5d4bfc8c 100644 --- a/src/core/settings/qgssettingsregistrycore.h +++ b/src/core/settings/qgssettingsregistrycore.h @@ -31,6 +31,8 @@ class QgsSettingsEntryStringList; /** * \ingroup core * \class QgsSettingsRegistryCore + * QgsSettingsRegistryCore is used for settings introspection and collects all + * QgsSettingsEntry instances of core. * * \since QGIS 3.20 */ @@ -38,8 +40,15 @@ class CORE_EXPORT QgsSettingsRegistryCore { public: + /** + * Constructor for QgsSettingsRegistryCore. + */ QgsSettingsRegistryCore(); - ~QgsSettingsRegistryCore(); + + /** + * Destructor for QgsSettingsRegistryCore. + */ + virtual ~QgsSettingsRegistryCore(); private: diff --git a/tests/src/python/test_qgssettingsentry.py b/tests/src/python/test_qgssettingsentry.py index 195bc8269020..028a21d21183 100644 --- a/tests/src/python/test_qgssettingsentry.py +++ b/tests/src/python/test_qgssettingsentry.py @@ -12,9 +12,9 @@ import os import tempfile -from qgis.core import QgsSettings, QgsSettingsEntryBase, QgsSettingsEntryVariant, QgsSettingsEntryString, QgsSettingsEntryStringList, QgsSettingsEntryBool, QgsSettingsEntryInteger, QgsSettingsEntryDouble, QgsTolerance, QgsMapLayerProxyModel +from qgis.core import QgsSettings, QgsSettingsEntryBase, QgsSettingsEntryVariant, QgsSettingsEntryString, QgsSettingsEntryStringList, QgsSettingsEntryBool, QgsSettingsEntryInteger, QgsSettingsEntryDouble, QgsSettingsEntryEnum, QgsSettingsEntryFlag, QgsUnitTypes, QgsMapLayerProxyModel from qgis.testing import start_app, unittest -from qgis.PyQt.QtCore import QSettings, QVariant +from qgis.PyQt.QtCore import QSettings, QVariant, QMetaEnum from pathlib import Path __author__ = 'Damiano Lombardi' @@ -240,12 +240,69 @@ def test_settings_entry_double(self): self.assertEqual(settingsEntryDouble.settingsType(), QgsSettingsEntryBase.Double) def test_settings_entry_enum(self): - # Todo implement QgsSettingsEntryEnum for python - pass + settingsKey = "settingsEntryEnum/enumValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = QgsUnitTypes.LayoutMeters + description = "Enum value functionality test" + settingsEntryEnum = QgsSettingsEntryEnum(settingsKey, self.pluginName, defaultValue, description) + + # Check default value + self.assertEqual(settingsEntryEnum.defaultValue(), QgsUnitTypes.LayoutMeters) + + # Check set value + success = settingsEntryEnum.setValue(QgsUnitTypes.LayoutFeet) + self.assertEqual(success, True) + qgsSettingsValue = QgsSettings().enumValue(settingsKeyComplete, QgsUnitTypes.LayoutMeters, QgsSettings.Plugins) + self.assertEqual(qgsSettingsValue, QgsUnitTypes.LayoutFeet) + + # Check get value + QgsSettings().setEnumValue(settingsKeyComplete, QgsUnitTypes.LayoutPicas, QgsSettings.Plugins) + self.assertEqual(settingsEntryEnum.value(), QgsUnitTypes.LayoutPicas) + + # Check settings type + self.assertEqual(settingsEntryEnum.settingsType(), QgsSettingsEntryBase.Enum) + + # assign to inexisting value + success = settingsEntryEnum.setValue(-1) + self.assertEqual(success, False) + + # Current value should not have changed + qgsSettingsValue = QgsSettings().enumValue(settingsKeyComplete, QgsUnitTypes.LayoutMeters, QgsSettings.Plugins) + self.assertEqual(qgsSettingsValue, QgsUnitTypes.LayoutPicas) def test_settings_entry_flag(self): - # Todo implement QgsSettingsEntryFlag for python - pass + settingsKey = "settingsEntryFlag/flagValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + pointAndLine = QgsMapLayerProxyModel.Filters(QgsMapLayerProxyModel.PointLayer | QgsMapLayerProxyModel.LineLayer) + pointAndPolygon = QgsMapLayerProxyModel.Filters(QgsMapLayerProxyModel.PointLayer | QgsMapLayerProxyModel.PolygonLayer) + hasGeometry = QgsMapLayerProxyModel.Filters(QgsMapLayerProxyModel.HasGeometry) + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + description = "Flag value functionality test" + settingsEntryFlag = QgsSettingsEntryFlag(settingsKey, self.pluginName, pointAndLine, description) + + # Check default value + self.assertEqual(settingsEntryFlag.defaultValue(), pointAndLine) + + # Check set value + success = settingsEntryFlag.setValue(hasGeometry) + self.assertEqual(success, True) + qgsSettingsValue = QgsSettings().flagValue(settingsKeyComplete, pointAndLine, QgsSettings.Plugins) + self.assertEqual(qgsSettingsValue, hasGeometry) + + # Check get value + QgsSettings().setValue(settingsKeyComplete, 'PointLayer|PolygonLayer', QgsSettings.Plugins) + self.assertEqual(settingsEntryFlag.value(), pointAndPolygon) + + # Check settings type + self.assertEqual(settingsEntryFlag.settingsType(), QgsSettingsEntryBase.Flag) if __name__ == '__main__': From be0488887c08e8f3035d1cdb305b0b9a13eaa00e Mon Sep 17 00:00:00 2001 From: Damiano Date: Thu, 8 Apr 2021 13:30:28 +0200 Subject: [PATCH 15/20] Fixed min negative value for QgsSettingsEntryDouble --- src/core/settings/qgssettingsentry.cpp | 2 +- src/core/settings/qgssettingsentry.h | 2 +- tests/src/core/testqgssettingsentry.cpp | 57 +++++++++++++++++++++++ tests/src/python/test_qgssettingsentry.py | 12 +++++ 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index 2146cf33577d..11a2089005e3 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -425,7 +425,7 @@ QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, const QStrin pluginName, defaultValue, description ) - , mMinValue( std::numeric_limits::min() ) + , mMinValue( std::numeric_limits::lowest() ) , mMaxValue( std::numeric_limits::max() ) , mDisplayHintDecimals( 1 ) { diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 84fd681a960b..b4eaf9a61ee4 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -581,7 +581,7 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase QgsSettings::Section section, double defaultValue = 0.0, const QString &description = QString(), - double minValue = std::numeric_limits::min(), + double minValue = std::numeric_limits::lowest(), double maxValue = std::numeric_limits::max(), int displayDecimals = 1 ); diff --git a/tests/src/core/testqgssettingsentry.cpp b/tests/src/core/testqgssettingsentry.cpp index 612b1e8e0319..16149a502170 100644 --- a/tests/src/core/testqgssettingsentry.cpp +++ b/tests/src/core/testqgssettingsentry.cpp @@ -31,10 +31,67 @@ class TestQgsSettingsEntry : public QObject Q_OBJECT private slots: + void settingsKey(); void enumValue(); void flagValue(); }; +void TestQgsSettingsEntry::settingsKey() +{ + QgsSettings settings; + + { + QString key( QStringLiteral( "/qgis/testing/settingsKey" ) ); + + // Be sure that settings does not exist already + settings.remove( key ); + + // Check that keys are handled same way for QgsSettings and QgsSettingsEntry + settings.setValue( key, 42 ); + + QgsSettingsEntryInteger settingsEntryInteger( key, QgsSettings::NoSection, 0 ); + QCOMPARE( settingsEntryInteger.value(), 42 ); + } + + { + QString key( QStringLiteral( "qgis/testing/settingsKey" ) ); + + // Be sure that settings does not exist already + settings.remove( key ); + + // Check that keys are handled same way for QgsSettings and QgsSettingsEntry + settings.setValue( key, 43 ); + + QgsSettingsEntryInteger settingsEntryInteger( key, QgsSettings::NoSection, 0 ); + QCOMPARE( settingsEntryInteger.value(), 43 ); + } + + { + QString key( QStringLiteral( "/qgis/testing/settingsKey" ) ); + + // Be sure that settings does not exist already + settings.remove( key, QgsSettings::Core ); + + // Check that keys are handled same way for QgsSettings and QgsSettingsEntry + settings.setValue( key, 44, QgsSettings::Core ); + + QgsSettingsEntryInteger settingsEntryInteger( key, QgsSettings::Core, 0 ); + QCOMPARE( settingsEntryInteger.value(), 44 ); + } + + { + QString key( QStringLiteral( "qgis/testing/settingsKey" ) ); + + // Be sure that settings does not exist already + settings.remove( key, QgsSettings::Core ); + + // Check that keys are handled same way for QgsSettings and QgsSettingsEntry + settings.setValue( key, 45, QgsSettings::Core ); + + QgsSettingsEntryInteger settingsEntryInteger( key, QgsSettings::Core, 0 ); + QCOMPARE( settingsEntryInteger.value(), 45 ); + } +} void TestQgsSettingsEntry::enumValue() { diff --git a/tests/src/python/test_qgssettingsentry.py b/tests/src/python/test_qgssettingsentry.py index 028a21d21183..87f6354ac66b 100644 --- a/tests/src/python/test_qgssettingsentry.py +++ b/tests/src/python/test_qgssettingsentry.py @@ -214,6 +214,12 @@ def test_settings_entry_integer(self): self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), 43) self.assertEqual(settingsEntryInteger.valueAsVariant(), 43) + # Set/Get negative value + settingsEntryInteger.setValue(-42) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), -42) + self.assertEqual(settingsEntryInteger.valueAsVariant(), -42) + # Settings type self.assertEqual(settingsEntryInteger.settingsType(), QgsSettingsEntryBase.Integer) @@ -236,6 +242,12 @@ def test_settings_entry_double(self): self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), 1.618) self.assertEqual(settingsEntryDouble.valueAsVariant(), 1.618) + # Set/Get negative value + settingsEntryDouble.setValue(-273.15) + # Verify setValue using QgsSettings + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), -273.15) + self.assertEqual(settingsEntryDouble.valueAsVariant(), -273.15) + # Settings type self.assertEqual(settingsEntryDouble.settingsType(), QgsSettingsEntryBase.Double) From 9762206731590faba536070ed74859133fde38b8 Mon Sep 17 00:00:00 2001 From: Damiano Date: Wed, 14 Apr 2021 15:41:49 +0200 Subject: [PATCH 16/20] Removed Settings struct / unified Enum/Flag logic in one base class --- python/core/additions/qgssettingsentry.py | 125 ++++----- .../auto_generated/layout/qgslayout.sip.in | 5 +- .../auto_generated/locator/qgslocator.sip.in | 9 +- .../settings/qgssettingsentry.sip.in | 64 +++-- src/app/locator/qgslocatoroptionswidget.cpp | 8 +- src/app/options/qgsoptions.cpp | 3 +- src/core/layout/qgslayout.h | 11 +- src/core/locator/qgslocator.cpp | 10 +- src/core/locator/qgslocator.h | 19 +- src/core/qgsapplication.cpp | 2 +- src/core/settings/qgssettingsentry.cpp | 101 ++----- src/core/settings/qgssettingsentry.h | 264 ++++++++++-------- src/core/settings/qgssettingsregistrycore.cpp | 8 +- tests/src/core/testqgssettingsentry.cpp | 2 +- 14 files changed, 299 insertions(+), 332 deletions(-) diff --git a/python/core/additions/qgssettingsentry.py b/python/core/additions/qgssettingsentry.py index aa61b4983f85..963a1a6749bd 100644 --- a/python/core/additions/qgssettingsentry.py +++ b/python/core/additions/qgssettingsentry.py @@ -22,7 +22,7 @@ import qgis # required to get base class of enums -class PyQgsSettingsEntryEnum(QgsSettingsEntryBase): +class _PyQgsSettingsEntryEnumFlag(QgsSettingsEntryBase): """ class QgsSettingsEntryEnum ingroup core An enum settings entry. @@ -44,10 +44,13 @@ def __init__(self, key, pluginName, defaultValue, description=str()): defaultValueStr = str() self.__metaEnum = metaEnumFromValue(defaultValue) if self.__metaEnum is None or not self.__metaEnum.isValid(): - QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.") + QgsLogger.debug("Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '{0}'".format(self.key())) else: - defaultValueStr = self.__metaEnum.valueToKey(defaultValue) - self.__enumClass = defaultValue.__class__ + if self.settingsType() is self.Enum: + defaultValueStr = self.__metaEnum.valueToKey(defaultValue) + else: + defaultValueStr = self.__metaEnum.valueToKeys(defaultValue) + self.__enumFlagClass = defaultValue.__class__ super().__init__(key, pluginName, defaultValueStr, description) @@ -69,17 +72,20 @@ def defaultValue(self): """ if self.__metaEnum is None or not self.__metaEnum.isValid(): - QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.") + QgsLogger.debug("Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '{0}'".format(self.key())) return -1 defaultValueString = self.defaultValueAsVariant() - (defaultValue, ok) = self.__metaEnum.keyToValue(defaultValueString) + if self.settingsType() is self.Enum: + (defaultValue, ok) = self.__metaEnum.keyToValue(defaultValueString) + else: + (defaultValue, ok) = self.__metaEnum.keysToValue(defaultValueString) if not ok: - QgsLogger.debug("Invalid enum key '{0}'.".format(self.defaultValueAsVariant())) + QgsLogger.debug("Invalid enum/flag key/s '{0}'.".format(self.defaultValueAsVariant())) return -1 # cast to the enum class - defaultValue = self.__enumClass(defaultValue) + defaultValue = self.__enumFlagClass(defaultValue) return defaultValue def setValue(self, value, dynamicKeyPart=str()): @@ -87,20 +93,55 @@ def setValue(self, value, dynamicKeyPart=str()): :param self: the QgsSettingsEntryEnum object :param dynamicKeyPart: argument specifies the dynamic part of the settings key. - """ + """ if self.__metaEnum is None or not self.__metaEnum.isValid(): - QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.") + QgsLogger.debug("Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '{0}'".format(self.key())) return False - enumKey = str() - enumKey = self.__metaEnum.valueToKey(value) - if not enumKey: - QgsLogger.debug("Invalid enum value '{0}'.".format(value)) + enumFlagKey = str() + if self.settingsType() is self.Enum: + enumFlagKey = self.__metaEnum.valueToKey(value) + else: + enumFlagKey = self.__metaEnum.valueToKeys(value) + if not enumFlagKey: + QgsLogger.debug("Invalid enum/flag value '{0}'.".format(value)) return False - super().setValue(enumKey, dynamicKeyPart) - return True + return super().setVariantValue(enumFlagKey, dynamicKeyPart) + + +class PyQgsSettingsEntryEnum(_PyQgsSettingsEntryEnumFlag): + """ class QgsSettingsEntryEnum + ingroup core + An enum settings entry. + since QGIS 3.20 + """ + + def __init__(self, key, pluginName, defaultValue, description=str()): + """ Constructor for QgsSettingsEntryEnum. + + :param self: the QgsSettingsEntryEnum object + :param key: argument specifies the final part of the settings key. + :param pluginName: argument is inserted in the key after the section. + :param defaultValue: argument specifies the default value for the settings entry. + :param description: argument specifies a description for the settings entry. + + .. note:: The enum needs to be declared with Q_ENUM. + """ + + super().__init__(key, pluginName, defaultValue, description) + + def value(self, dynamicKeyPart=str()): + """ Get settings value. + + :param self: the QgsSettingsEntryEnum object + :param dynamicKeyPart: argument specifies the dynamic part of the settings key. + """ + + return QgsSettings().enumValue(self.key(dynamicKeyPart), + self.defaultValue(), + self.section()) def settingsType(self): """ Get the settings entry type. @@ -111,7 +152,7 @@ def settingsType(self): return self.Enum -class PyQgsSettingsEntryFlag(QgsSettingsEntryBase): +class PyQgsSettingsEntryFlag(_PyQgsSettingsEntryEnumFlag): """ class QgsSettingsEntryFlag ingroup core A flag settings entry. @@ -130,15 +171,7 @@ def __init__(self, key, pluginName, defaultValue, description=str()): .. note:: The flag needs to be declared with Q_FLAG (not Q_FLAGS). """ - defaultValueStr = str() - self.__metaEnum = metaEnumFromValue(defaultValue) - if self.__metaEnum is None or not self.__metaEnum.isValid(): - QgsLogger.debug("Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration.") - else: - defaultValueStr = self.__metaEnum.valueToKeys(defaultValue) - self.__flagClass = defaultValue.__class__ - - super().__init__(key, pluginName, defaultValueStr, description) + super().__init__(key, pluginName, defaultValue, description) def value(self, dynamicKeyPart=str()): """ Get settings value. @@ -151,46 +184,6 @@ def value(self, dynamicKeyPart=str()): self.defaultValue(), self.section()) - def defaultValue(self): - """ Get settings default value. - - :param self: the QgsSettingsEntryFlag object - """ - - if self.__metaEnum is None or not self.__metaEnum.isValid(): - QgsLogger.debug("Invalid metaenum. Flag probably misses Q_ENUM or Q_FLAG declaration.") - return -1 - - defaultValueString = self.defaultValueAsVariant() - (defaultValue, ok) = self.__metaEnum.keysToValue(defaultValueString) - if not ok: - QgsLogger.debug("Invalid flag keys '{0}'.".format(self.defaultValueAsVariant())) - return -1 - - # cast to the flag class - defaultValue = self.__flagClass(defaultValue) - return defaultValue - - def setValue(self, value, dynamicKeyPart=str()): - """ Set settings value. - - :param self: the QgsSettingsEntryFlag object - :param dynamicKeyPart: argument specifies the dynamic part of the settings key. - """ - - if self.__metaEnum is None or not self.__metaEnum.isValid(): - QgsLogger.debug("Invalid metaenum. Flag probably misses Q_ENUM or Q_FLAG declaration.") - return False - - flagKeys = str() - flagKeys = self.__metaEnum.valueToKeys(value) - if not flagKeys: - QgsLogger.debug("Invalid flag value '{0}'.".format(value)) - return False - - super().setValue(flagKeys, dynamicKeyPart) - return True - def settingsType(self): """ Get the settings entry type. diff --git a/python/core/auto_generated/layout/qgslayout.sip.in b/python/core/auto_generated/layout/qgslayout.sip.in index 274d45f8e589..7918a46f736f 100644 --- a/python/core/auto_generated/layout/qgslayout.sip.in +++ b/python/core/auto_generated/layout/qgslayout.sip.in @@ -614,10 +614,7 @@ should be canceled. .. versionadded:: 3.10 %End - struct Settings - { - const QgsSettingsEntryStringList searchPathForTemplates; - }; + const QgsSettingsEntryStringList settingsSearchPathForTemplates; public slots: diff --git a/python/core/auto_generated/locator/qgslocator.sip.in b/python/core/auto_generated/locator/qgslocator.sip.in index 858c57f5b270..2c6dfcace923 100644 --- a/python/core/auto_generated/locator/qgslocator.sip.in +++ b/python/core/auto_generated/locator/qgslocator.sip.in @@ -141,12 +141,9 @@ This list is updated when preparing the search .. versionadded:: 3.16 %End - struct Settings - { - const QgsSettingsEntryBool locatorFilterEnabled; - const QgsSettingsEntryBool locatorFilterDefault; - const QgsSettingsEntryString locatorFilterPrefix; - }; + const QgsSettingsEntryBool settingsLocatorFilterEnabled; + const QgsSettingsEntryBool settingsLocatorFilterDefault; + const QgsSettingsEntryString settingsLocatorFilterPrefix; signals: diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index f84c7539d0dc..fc81a22346fc 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -101,10 +101,11 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Get settings section. The settings section of the parent group is returned if available. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; + virtual bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring Set settings value. +The ``value`` to set. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. %End @@ -162,10 +163,12 @@ The ``description`` argument specifies a description for the settings entry. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; - + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntryBase`.setValue +Set settings value. + +The ``value`` to set. +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. %End QVariant value( const QString &dynamicKeyPart = QString() ) const; @@ -203,7 +206,7 @@ A string settings entry. QgsSettingsEntryString( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + const QString &defaultValue = QString(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryString. @@ -215,10 +218,12 @@ The ``default`` value argument specifies the default value for the settings entr The ``description`` argument specifies a description for the settings entry. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; - + bool setValue( const QString &value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntryBase`.setValue +Set settings value. + +The ``value`` to set. +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. %End QString value( const QString &dynamicKeyPart = QString() ) const; @@ -281,7 +286,7 @@ A string list settings entry. QgsSettingsEntryStringList( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + const QStringList &defaultValue = QStringList(), const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryStringList. @@ -293,10 +298,12 @@ The ``default`` value argument specifies the default value for the settings entr The ``description`` argument specifies a description for the settings entry. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; - + bool setValue( const QStringList &value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntryBase`.setValue +Set settings value. + +The ``value`` to set. +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. %End QStringList value( const QString &dynamicKeyPart = QString() ) const; @@ -335,7 +342,7 @@ A boolean settings entry. QgsSettingsEntryBool( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + bool defaultValue = false, const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryBool. @@ -347,10 +354,12 @@ The ``default`` value argument specifies the default value for the settings entr The ``description`` argument specifies a description for the settings entry. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; - + bool setValue( bool value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntryBase`.setValue +Set settings value. + +The ``value`` to set. +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. %End bool value( const QString &dynamicKeyPart = QString() ) const; @@ -389,7 +398,7 @@ An integer settings entry. QgsSettingsEntryInteger( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + qlonglong defaultValue = 0, const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryInteger. @@ -401,11 +410,12 @@ The ``default`` value argument specifies the default value for the settings entr The ``description`` argument specifies a description for the settings entry. %End - - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; - + bool setValue( qlonglong value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntryBase`.setValue +Set settings value. + +The ``value`` to set. +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. %End qlonglong value( const QString &dynamicKeyPart = QString() ) const; @@ -468,7 +478,7 @@ A double settings entry. QgsSettingsEntryDouble( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + double defaultValue, const QString &description = QString() ); %Docstring Constructor for QgsSettingsEntryDouble. @@ -480,10 +490,12 @@ The ``default`` value argument specifies the default value for the settings entr The ``description`` argument specifies a description for the settings entry. %End - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; - + bool setValue( double value, const QString &dynamicKeyPart = QString() ) const; %Docstring -\copydoc :py:class:`QgsSettingsEntryBase`.setValue +Set settings value. + +The ``value`` to set. +The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. %End double value( const QString &dynamicKeyPart = QString() ) const; @@ -545,6 +557,8 @@ Returns how much decimals should be shown in the Gui. + + /************************************************************************ * This file has been generated automatically from * * * diff --git a/src/app/locator/qgslocatoroptionswidget.cpp b/src/app/locator/qgslocatoroptionswidget.cpp index e0188faa656f..bd6ba70140c9 100644 --- a/src/app/locator/qgslocatoroptionswidget.cpp +++ b/src/app/locator/qgslocatoroptionswidget.cpp @@ -330,26 +330,26 @@ void QgsLocatorFiltersModel::commitChanges() if ( !activePrefix.isEmpty() && activePrefix != filter->prefix() ) { filter->setActivePrefix( activePrefix ); - QgsLocator::Settings::locatorFilterPrefix.setValue( activePrefix, filter->name() ); + QgsLocator::settingsLocatorFilterPrefix.setValue( activePrefix, filter->name() ); } else { filter->setActivePrefix( QString() ); - QgsLocator::Settings::locatorFilterPrefix.remove( filter->name() ); + QgsLocator::settingsLocatorFilterPrefix.remove( filter->name() ); } } QHash< QgsLocatorFilter *, bool >::const_iterator it = mEnabledChanges.constBegin(); for ( ; it != mEnabledChanges.constEnd(); ++it ) { QgsLocatorFilter *filter = it.key(); - QgsLocator::Settings::locatorFilterEnabled.setValue( it.value(), filter->name() ); + QgsLocator::settingsLocatorFilterEnabled.setValue( it.value(), filter->name() ); filter->setEnabled( it.value() ); } it = mDefaultChanges.constBegin(); for ( ; it != mDefaultChanges.constEnd(); ++it ) { QgsLocatorFilter *filter = it.key(); - QgsLocator::Settings::locatorFilterDefault.setValue( it.value(), filter->name() ); + QgsLocator::settingsLocatorFilterDefault.setValue( it.value(), filter->name() ); filter->setUseWithoutPrefix( it.value() ); } } diff --git a/src/app/options/qgsoptions.cpp b/src/app/options/qgsoptions.cpp index 3d07ff8bc562..7c5894328731 100644 --- a/src/app/options/qgsoptions.cpp +++ b/src/app/options/qgsoptions.cpp @@ -1481,8 +1481,7 @@ void QgsOptions::saveOptions() { pathsList << mListComposerTemplatePaths->item( i )->text(); } -// QgsLayout::Settings::searchPathForTemplates.setValue( pathsList ); -// QgsLayout::Settings::SearchPathForTemplates().setValue( pathsList ); + QgsLayout::settingsSearchPathForTemplates.setValue( pathsList ); pathsList.clear(); for ( int r = 0; r < mLocalizedDataPathListWidget->count(); r++ ) diff --git a/src/core/layout/qgslayout.h b/src/core/layout/qgslayout.h index c896a29a8036..d045d0a34887 100644 --- a/src/core/layout/qgslayout.h +++ b/src/core/layout/qgslayout.h @@ -658,15 +658,8 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext */ bool accept( QgsStyleEntityVisitorInterface *visitor ) const; - /** - * Contains QgsLayout settings - * \since QGIS 3.20 - */ - struct Settings - { - //! Settings entry search path for templates - static const inline QgsSettingsEntryStringList searchPathForTemplates = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), QObject::tr( "Search path for templates" ) ); - }; + //! Settings entry search path for templates + static const inline QgsSettingsEntryStringList settingsSearchPathForTemplates = QgsSettingsEntryStringList( QStringLiteral( "Layout/searchPathsForTemplates" ), QgsSettings::Core, QStringList(), QObject::tr( "Search path for templates" ) ); public slots: diff --git a/src/core/locator/qgslocator.cpp b/src/core/locator/qgslocator.cpp index d4ed7518f83e..c145e57066cb 100644 --- a/src/core/locator/qgslocator.cpp +++ b/src/core/locator/qgslocator.cpp @@ -94,13 +94,13 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter ) filter->setParent( this ); // restore settings - bool enabled = QgsLocator::Settings::locatorFilterEnabled.value( filter->name() ); + bool enabled = QgsLocator::settingsLocatorFilterEnabled.value( filter->name() ); bool byDefault = filter->useWithoutPrefix(); - if ( QgsLocator::Settings::locatorFilterDefault.exists( filter->name() ) ) - byDefault = QgsLocator::Settings::locatorFilterDefault.value( filter->name() ); + if ( QgsLocator::settingsLocatorFilterDefault.exists( filter->name() ) ) + byDefault = QgsLocator::settingsLocatorFilterDefault.value( filter->name() ); QString prefix = filter->prefix(); - if ( QgsLocator::Settings::locatorFilterPrefix.exists( filter->name() ) ) - prefix = QgsLocator::Settings::locatorFilterPrefix.value( filter->name() ); + if ( QgsLocator::settingsLocatorFilterPrefix.exists( filter->name() ) ) + prefix = QgsLocator::settingsLocatorFilterPrefix.value( filter->name() ); if ( prefix.isEmpty() ) { prefix = filter->prefix(); diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index d55424c48d4c..c0bf261f63c7 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -153,19 +153,12 @@ class CORE_EXPORT QgsLocator : public QObject */ QStringList completionList() const {return mAutocompletionList;} - /** - * Contains QgsLocator settings - * \since QGIS 3.20 - */ - struct Settings - { - //! Settings entry locator filter enabled - static const inline QgsSettingsEntryBool locatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Locator filter enabled" ); - //! Settings entry locator filter default value - static const inline QgsSettingsEntryBool locatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Locator filter default value" ); - //! Settings entry locator filter prefix - static const inline QgsSettingsEntryString locatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); - }; + //! Settings entry locator filter enabled + static const inline QgsSettingsEntryBool settingsLocatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Locator filter enabled" ); + //! Settings entry locator filter default value + static const inline QgsSettingsEntryBool settingsLocatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Locator filter default value" ); + //! Settings entry locator filter prefix + static const inline QgsSettingsEntryString settingsLocatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); signals: diff --git a/src/core/qgsapplication.cpp b/src/core/qgsapplication.cpp index 521797402193..33ca1e027fed 100644 --- a/src/core/qgsapplication.cpp +++ b/src/core/qgsapplication.cpp @@ -1107,7 +1107,7 @@ QStringList QgsApplication::layoutTemplatePaths() { //local directories to search when looking for an template with a given basename //defined by user in options dialog - return QgsLayout::Settings::searchPathForTemplates.value(); + return QgsLayout::settingsSearchPathForTemplates.value(); } QMap QgsApplication::systemEnvVars() diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index 11a2089005e3..25b326ec7630 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -84,7 +84,7 @@ QgsSettings::Section QgsSettingsEntryBase::section() const return mSection; } -bool QgsSettingsEntryBase::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QString &dynamicKeyPart ) const { QgsSettings().setValue( key( dynamicKeyPart ), value, @@ -128,7 +128,7 @@ QgsSettingsEntryVariant::QgsSettingsEntryVariant( const QString &key, const QStr bool QgsSettingsEntryVariant::setValue( const QVariant &value, const QString &dynamicKeyPart ) const { - return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); } QVariant QgsSettingsEntryVariant::value( const QString &dynamicKeyPart ) const @@ -156,7 +156,7 @@ QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, QgsSettings: { } -QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) +QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, const QString &pluginName, const QString &defaultValue, const QString &description ) : QgsSettingsEntryBase( key, pluginName, defaultValue, @@ -166,37 +166,28 @@ QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, const QStrin { } -bool QgsSettingsEntryString::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +bool QgsSettingsEntryString::setValue( const QString &value, const QString &dynamicKeyPart ) const { - if ( value.canConvert() == false ) - { - QgsDebugMsg( QObject::tr( "Can't convert value '%1' to string for settings with key '%2'" ) - .arg( value.toString(), - QgsSettingsEntryBase::key() ) ); - return false; - } - - QString valueString = value.toString(); - if ( valueString.length() < mMinLength ) + if ( value.length() < mMinLength ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is shorter than minimum length '%3'." ) .arg( QgsSettingsEntryBase::key(), - valueString.length(), + value.length(), mMinLength ) ); return false; } if ( mMaxLength >= 0 - && valueString.length() > mMaxLength ) + && value.length() > mMaxLength ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is longer than maximum length '%3'." ) .arg( QgsSettingsEntryBase::key(), - valueString.length(), + value.length(), mMinLength ) ); return false; } - return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); } QString QgsSettingsEntryString::value( const QString &dynamicKeyPart ) const @@ -242,7 +233,7 @@ QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, QgsS { } -QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) +QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, const QString &pluginName, const QStringList &defaultValue, const QString &description ) : QgsSettingsEntryBase( key, pluginName, defaultValue, @@ -250,17 +241,9 @@ QgsSettingsEntryStringList::QgsSettingsEntryStringList( const QString &key, cons { } -bool QgsSettingsEntryStringList::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +bool QgsSettingsEntryStringList::setValue( const QStringList &value, const QString &dynamicKeyPart ) const { - if ( value.canConvert() == false ) - { - QgsDebugMsg( QObject::tr( "Can't convert value '%1' to string list for settings with key '%2'" ) - .arg( value.toString(), - QgsSettingsEntryBase::key() ) ); - return false; - } - - return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); } QStringList QgsSettingsEntryStringList::value( const QString &dynamicKeyPart ) const @@ -286,7 +269,7 @@ QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, QgsSettings::Sec { } -QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) +QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, const QString &pluginName, bool defaultValue, const QString &description ) : QgsSettingsEntryBase( key, pluginName, defaultValue, @@ -294,17 +277,9 @@ QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, const QString &p { } -bool QgsSettingsEntryBool::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +bool QgsSettingsEntryBool::setValue( bool value, const QString &dynamicKeyPart ) const { - if ( value.canConvert() == false ) - { - QgsDebugMsg( QObject::tr( "Can't convert value '%1' to bool for settings with key '%2'" ) - .arg( value.toString(), - QgsSettingsEntryBase::key() ) ); - return false; - } - - return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); } bool QgsSettingsEntryBool::value( const QString &dynamicKeyPart ) const @@ -332,7 +307,7 @@ QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, QgsSetting { } -QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) +QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, const QString &pluginName, qlonglong defaultValue, const QString &description ) : QgsSettingsEntryBase( key, pluginName, defaultValue, @@ -342,36 +317,27 @@ QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, const QStr { } -bool QgsSettingsEntryInteger::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +bool QgsSettingsEntryInteger::setValue( qlonglong value, const QString &dynamicKeyPart ) const { - if ( value.canConvert() == false ) - { - QgsDebugMsg( QObject::tr( "Can't convert value '%1' to qlonglong for settings with key '%2'" ) - .arg( value.toString(), - QgsSettingsEntryBase::key() ) ); - return false; - } - - qlonglong valueLongLong = value.toLongLong(); - if ( valueLongLong < mMinValue ) + if ( value < mMinValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) .arg( QgsSettingsEntryBase::key(), - QString::number( valueLongLong ), + QString::number( value ), QString::number( mMinValue ) ) ); return false; } - if ( valueLongLong > mMaxValue ) + if ( value > mMaxValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) .arg( QgsSettingsEntryBase::key(), - QString::number( valueLongLong ), + QString::number( value ), QString::number( mMinValue ) ) ); return false; } - return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); } qlonglong QgsSettingsEntryInteger::value( const QString &dynamicKeyPart ) const @@ -420,7 +386,7 @@ QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, QgsSettings: { } -QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, const QString &pluginName, const QVariant &defaultValue, const QString &description ) +QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, const QString &pluginName, double defaultValue, const QString &description ) : QgsSettingsEntryBase( key, pluginName, defaultValue, @@ -431,36 +397,27 @@ QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, const QStrin { } -bool QgsSettingsEntryDouble::setValue( const QVariant &value, const QString &dynamicKeyPart ) const +bool QgsSettingsEntryDouble::setValue( double value, const QString &dynamicKeyPart ) const { - if ( value.canConvert() == false ) - { - QgsDebugMsg( QObject::tr( "Can't convert value '%1' to double for settings with key '%2'" ) - .arg( value.toString(), - QgsSettingsEntryBase::key() ) ); - return false; - } - - double valueDouble = value.toDouble(); - if ( valueDouble < mMinValue ) + if ( value < mMinValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) .arg( QgsSettingsEntryBase::key(), - QString::number( valueDouble ), + QString::number( value ), QString::number( mMinValue ) ) ); return false; } - if ( valueDouble > mMaxValue ) + if ( value > mMaxValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) .arg( QgsSettingsEntryBase::key(), - QString::number( valueDouble ), + QString::number( value ), QString::number( mMinValue ) ) ); return false; } - return QgsSettingsEntryBase::setValue( value, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); } double QgsSettingsEntryDouble::value( const QString &dynamicKeyPart ) const diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index b4eaf9a61ee4..4e27b9f8f730 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -139,9 +139,10 @@ class CORE_EXPORT QgsSettingsEntryBase /** * Set settings value. * + * The \a value to set. * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. */ - virtual bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; + virtual bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; /** * Get settings value. @@ -217,8 +218,13 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase const QString &description = QString() ); - //! \copydoc QgsSettingsEntryBase::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; /** * Get settings value. @@ -280,11 +286,16 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase */ QgsSettingsEntryString( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + const QString &defaultValue = QString(), const QString &description = QString() ); - //! \copydoc QgsSettingsEntryBase::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + bool setValue( const QString &value, const QString &dynamicKeyPart = QString() ) const; /** * Get settings value. @@ -371,11 +382,16 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase */ QgsSettingsEntryStringList( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + const QStringList &defaultValue = QStringList(), const QString &description = QString() ); - //! \copydoc QgsSettingsEntryBase::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + bool setValue( const QStringList &value, const QString &dynamicKeyPart = QString() ) const; /** * Get settings value. @@ -433,11 +449,16 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase */ QgsSettingsEntryBool( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + bool defaultValue = false, const QString &description = QString() ); - //! \copydoc QgsSettingsEntryBase::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + bool setValue( bool value, const QString &dynamicKeyPart = QString() ) const; /** * Get settings value. @@ -499,12 +520,16 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase */ QgsSettingsEntryInteger( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + qlonglong defaultValue = 0, const QString &description = QString() ); - - //! \copydoc QgsSettingsEntryBase::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + bool setValue( qlonglong value, const QString &dynamicKeyPart = QString() ) const; /** * Get settings value. @@ -598,11 +623,16 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase */ QgsSettingsEntryDouble( const QString &key, const QString &pluginName, - const QVariant &defaultValue = QVariant(), + double defaultValue, const QString &description = QString() ); - //! \copydoc QgsSettingsEntryBase::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + */ + bool setValue( double value, const QString &dynamicKeyPart = QString() ) const; /** * Get settings value. @@ -670,16 +700,16 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase /** * \class QgsSettingsEntryEnum * \ingroup core - * An enum settings entry. + * A base class for enum and flag settings entry. * \since QGIS 3.20 */ template -class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase +class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase { public: /** - * Constructor for QgsSettingsEntryEnum. + * Constructor for QgsSettingsEntryEnumFlagBase. * * The \a key argument specifies the final part of the settings key. * The \a section argument specifies the section. @@ -689,21 +719,21 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase * \note The enum needs to be declared with Q_ENUM, and flags with Q_FLAG (not Q_FLAGS). * \note for Python bindings, a custom implementation is achieved in Python directly */ - QgsSettingsEntryEnum( const QString &key, - QgsSettings::Section section, - const T &defaultValue, - const QString &description = QString() ) + QgsSettingsEntryEnumFlagBase( const QString &key, + QgsSettings::Section section, + const T &defaultValue, + const QString &description = QString() ) : QgsSettingsEntryBase( key, section, - QMetaEnum::fromType().valueToKey( defaultValue ), + QMetaEnum::fromType().isFlag() ? + QVariant( QMetaEnum::fromType().valueToKeys( defaultValue ) ) : + QVariant( QMetaEnum::fromType().valueToKey( defaultValue ) ), description ) { mMetaEnum = QMetaEnum::fromType(); Q_ASSERT( mMetaEnum.isValid() ); if ( !mMetaEnum.isValid() ) - { - QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); - } + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( QgsSettingsEntryBase::key() ) ); } /** @@ -713,9 +743,14 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase */ T value( const QString &dynamicKeyPart = QString() ) const { - return QgsSettings().enumValue( key( dynamicKeyPart ), - defaultValue(), - section() ); + if ( settingsType() == QgsSettingsEntryBase::Enum ) + return QgsSettings().enumValue( key( dynamicKeyPart ), + defaultValue(), + section() ); + else + return QgsSettings().flagValue( key( dynamicKeyPart ), + defaultValue(), + section() ); } /** @@ -725,16 +760,20 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase { if ( !mMetaEnum.isValid() ) { - QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( key() ) ); return T(); } - QByteArray byteArray = defaultValueAsVariant().toByteArray(); bool ok = false; - T defaultValue = static_cast( mMetaEnum.keyToValue( byteArray, &ok ) ); + T defaultValue; + if ( settingsType() == QgsSettingsEntryBase::Enum ) + defaultValue = static_cast( mMetaEnum.keyToValue( defaultValueAsVariant().toByteArray(), &ok ) ); + else + defaultValue = static_cast( mMetaEnum.keysToValue( defaultValueAsVariant().toByteArray(), &ok ) ); + if ( !ok ) { - QgsDebugMsg( QStringLiteral( "Invalid enum key '%1'." ).arg( defaultValueAsVariant().toString() ) ); + QgsDebugMsg( QStringLiteral( "Invalid enum/flag key/s '%1' for settings key '%2'" ).arg( defaultValueAsVariant().toString(), key() ) ); return T(); } @@ -742,28 +781,35 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase } //! \copydoc QgsSettingsEntryBase::setValue - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const override + bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const { if ( !mMetaEnum.isValid() ) { - QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( key() ) ); return false; } - const char *enumKey = mMetaEnum.valueToKey( value.toInt() ); - if ( enumKey == nullptr ) + if ( settingsType() == QgsSettingsEntryBase::Enum ) { - QgsDebugMsg( QStringLiteral( "Invalid enum value '%1'." ).arg( value.toInt() ) ); - return false; + const char *enumKey = mMetaEnum.valueToKey( value ); + if ( enumKey == nullptr ) + { + QgsDebugMsg( QStringLiteral( "Invalid enum value '%1'." ).arg( value ) ); + return false; + } + + return QgsSettingsEntryBase::setVariantValue( enumKey, dynamicKeyPart ); + } + else + { + QByteArray flagKeys = mMetaEnum.valueToKeys( value ); + if ( flagKeys.isEmpty() ) + { + QgsDebugMsg( QStringLiteral( "Invalid flag value '%1'." ).arg( value ) ); + return false; + } + return QgsSettingsEntryBase::setVariantValue( flagKeys, dynamicKeyPart ); } - - return QgsSettingsEntryBase::setValue( enumKey, dynamicKeyPart ); - } - - //! \copydoc QgsSettingsEntryBase::settingsType - virtual SettingsType settingsType() const override - { - return QgsSettingsEntryBase::Enum; } private: @@ -777,109 +823,87 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryBase #ifndef SIP_RUN /** - * \class QgsSettingsEntryFlag + * \class QgsSettingsEntryEnum * \ingroup core - * An flag settings entry. + * An enum settings entry. * \since QGIS 3.20 */ template -class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryBase +class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryEnumFlagBase { public: /** - * Constructor for QgsSettingsEntryFlag. + * Constructor for QgsSettingsEntryEnum. * * The \a key argument specifies the final part of the settings key. * The \a section argument specifies the section. * The \a default value argument specifies the default value for the settings entry. * The \a description argument specifies a description for the settings entry. * - * \note The flag needs to be declared with Q_FLAG (not Q_FLAGS). - * \note for Python bindings, a custom implementation is achieved in Python directly. + * \note The enum needs to be declared with Q_ENUM, and flags with Q_FLAG (not Q_FLAGS). + * \note for Python bindings, a custom implementation is achieved in Python directly */ - QgsSettingsEntryFlag( const QString &key, + QgsSettingsEntryEnum( const QString &key, QgsSettings::Section section, const T &defaultValue, const QString &description = QString() ) - : QgsSettingsEntryBase( key, - section, - QMetaEnum::fromType().valueToKeys( defaultValue ), - description ) + : QgsSettingsEntryEnumFlagBase( key, + section, + defaultValue, + description ) { - mMetaEnum = QMetaEnum::fromType(); - Q_ASSERT( mMetaEnum.isValid() ); - if ( !mMetaEnum.isValid() ) - { - QgsDebugMsg( QStringLiteral( "Invalid metaenum. Flag probably misses Q_ENUM or Q_FLAG declaration." ) ); - } } - /** - * Get settings value. - * - * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. - */ - T value( const QString &dynamicKeyPart = QString() ) const + //! \copydoc QgsSettingsEntryBase::settingsType + virtual QgsSettingsEntryBase::SettingsType settingsType() const override { - return QgsSettings().flagValue( key( dynamicKeyPart ), - defaultValue(), - section() ); + return QgsSettingsEntryBase::Enum; } +}; +#endif - /** - * Get settings default value. - */ - T defaultValue() const - { - if ( !mMetaEnum.isValid() ) - { - QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum probably misses Q_ENUM or Q_FLAG declaration." ) ); - return T(); - } - QByteArray byteArray = defaultValueAsVariant().toByteArray(); - bool ok = false; - T defaultValue = static_cast( mMetaEnum.keysToValue( byteArray, &ok ) ); - if ( !ok ) - { - QgsDebugMsg( QStringLiteral( "Invalid flag keys '%1'." ).arg( defaultValueAsVariant().toString() ) ); - return T(); - } +#ifndef SIP_RUN - return defaultValue; - } +/** + * \class QgsSettingsEntryFlag + * \ingroup core + * An flag settings entry. + * \since QGIS 3.20 + */ +template +class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryEnumFlagBase +{ + public: - //! \copydoc QgsSettingsEntryBase::setValue - bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const + /** + * Constructor for QgsSettingsEntryFlag. + * + * The \a key argument specifies the final part of the settings key. + * The \a section argument specifies the section. + * The \a default value argument specifies the default value for the settings entry. + * The \a description argument specifies a description for the settings entry. + * + * \note The flag needs to be declared with Q_FLAG (not Q_FLAGS). + * \note for Python bindings, a custom implementation is achieved in Python directly. + */ + QgsSettingsEntryFlag( const QString &key, + QgsSettings::Section section, + const T &defaultValue, + const QString &description = QString() ) + : QgsSettingsEntryEnumFlagBase( key, + section, + defaultValue, + description ) { - if ( !mMetaEnum.isValid() ) - { - QgsDebugMsg( QStringLiteral( "Invalid metaenum. Flag probably misses Q_ENUM or Q_FLAG declaration." ) ); - return false; - } - - QByteArray flagKeys = mMetaEnum.valueToKeys( value ); - if ( flagKeys.isEmpty() ) - { - QgsDebugMsg( QStringLiteral( "Invalid flag value '%1'." ).arg( value ) ); - return false; - } - - QgsSettingsEntryBase::setValue( flagKeys, dynamicKeyPart ); - return true; } //! \copydoc QgsSettingsEntryBase::settingsType - virtual SettingsType settingsType() const override + virtual QgsSettingsEntryBase::SettingsType settingsType() const override { return QgsSettingsEntryBase::Flag; } - - private: - - QMetaEnum mMetaEnum; - }; #endif diff --git a/src/core/settings/qgssettingsregistrycore.cpp b/src/core/settings/qgssettingsregistrycore.cpp index e2a2d1ae1789..188028a23f8d 100644 --- a/src/core/settings/qgssettingsregistrycore.cpp +++ b/src/core/settings/qgssettingsregistrycore.cpp @@ -21,11 +21,11 @@ QgsSettingsRegistryCore::QgsSettingsRegistryCore() : mSettingsEntries() { - mSettingsEntries.append( &QgsLayout::Settings::searchPathForTemplates ); + mSettingsEntries.append( &QgsLayout::settingsSearchPathForTemplates ); - mSettingsEntries.append( &QgsLocator::Settings::locatorFilterEnabled ); - mSettingsEntries.append( &QgsLocator::Settings::locatorFilterDefault ); - mSettingsEntries.append( &QgsLocator::Settings::locatorFilterPrefix ); + mSettingsEntries.append( &QgsLocator::settingsLocatorFilterEnabled ); + mSettingsEntries.append( &QgsLocator::settingsLocatorFilterDefault ); + mSettingsEntries.append( &QgsLocator::settingsLocatorFilterPrefix ); } QgsSettingsRegistryCore::~QgsSettingsRegistryCore() diff --git a/tests/src/core/testqgssettingsentry.cpp b/tests/src/core/testqgssettingsentry.cpp index 16149a502170..d06b610851fd 100644 --- a/tests/src/core/testqgssettingsentry.cpp +++ b/tests/src/core/testqgssettingsentry.cpp @@ -122,7 +122,7 @@ void TestQgsSettingsEntry::enumValue() // assign to inexisting value { - bool success = settingsEntryEnum.setValue( -1 ); + bool success = settingsEntryEnum.setValue( static_cast( -1 ) ); QCOMPARE( success, false ); // Current value should not have changed From 4891bd6ebb65cc18e19f39a03e7b20bc6621aea0 Mon Sep 17 00:00:00 2001 From: Damiano Date: Wed, 14 Apr 2021 18:05:41 +0200 Subject: [PATCH 17/20] Added support for settings with keys with multiple dynamic parts --- python/core/additions/qgssettingsentry.py | 44 ++--- .../settings/qgssettingsentry.sip.in | 127 +++++++++++++- src/core/locator/qgslocator.h | 6 +- src/core/settings/qgssettingsentry.cpp | 150 ++++++++++++++-- src/core/settings/qgssettingsentry.h | 161 +++++++++++++++++- tests/src/python/test_qgssettingsentry.py | 27 ++- 6 files changed, 457 insertions(+), 58 deletions(-) diff --git a/python/core/additions/qgssettingsentry.py b/python/core/additions/qgssettingsentry.py index 963a1a6749bd..1823f13c97b4 100644 --- a/python/core/additions/qgssettingsentry.py +++ b/python/core/additions/qgssettingsentry.py @@ -30,7 +30,7 @@ class _PyQgsSettingsEntryEnumFlag(QgsSettingsEntryBase): """ def __init__(self, key, pluginName, defaultValue, description=str()): - """ Constructor for QgsSettingsEntryEnum. + """ Constructor for _PyQgsSettingsEntryEnumFlag. :param self: the QgsSettingsEntryEnum object :param key: argument specifies the final part of the settings key. @@ -38,7 +38,8 @@ def __init__(self, key, pluginName, defaultValue, description=str()): :param defaultValue: argument specifies the default value for the settings entry. :param description: argument specifies a description for the settings entry. - .. note:: The enum needs to be declared with Q_ENUM. + .. note:: This constructor should not be used. Use PyQgsSettingsEntryEnum + or PyQgsSettingsEntryFlag instead. """ defaultValueStr = str() @@ -57,18 +58,23 @@ def __init__(self, key, pluginName, defaultValue, description=str()): def value(self, dynamicKeyPart=str()): """ Get settings value. - :param self: the QgsSettingsEntryEnum object + :param self: the _PyQgsSettingsEntryEnumFlag object :param dynamicKeyPart: argument specifies the dynamic part of the settings key. """ - return QgsSettings().enumValue(self.key(dynamicKeyPart), - self.defaultValue(), - self.section()) + if self.settingsType() is self.Enum: + return QgsSettings().enumValue(self.key(dynamicKeyPart), + self.defaultValue(), + self.section()) + else: + return QgsSettings().flagValue(self.key(dynamicKeyPart), + self.defaultValue(), + self.section()) def defaultValue(self): """ Get settings default value. - :param self: the QgsSettingsEntryEnum object + :param self: the _PyQgsSettingsEntryEnumFlag object """ if self.__metaEnum is None or not self.__metaEnum.isValid(): @@ -91,7 +97,7 @@ def defaultValue(self): def setValue(self, value, dynamicKeyPart=str()): """ Set settings value. - :param self: the QgsSettingsEntryEnum object + :param self: the _PyQgsSettingsEntryEnumFlag object :param dynamicKeyPart: argument specifies the dynamic part of the settings key. """ @@ -132,17 +138,6 @@ def __init__(self, key, pluginName, defaultValue, description=str()): super().__init__(key, pluginName, defaultValue, description) - def value(self, dynamicKeyPart=str()): - """ Get settings value. - - :param self: the QgsSettingsEntryEnum object - :param dynamicKeyPart: argument specifies the dynamic part of the settings key. - """ - - return QgsSettings().enumValue(self.key(dynamicKeyPart), - self.defaultValue(), - self.section()) - def settingsType(self): """ Get the settings entry type. @@ -173,17 +168,6 @@ def __init__(self, key, pluginName, defaultValue, description=str()): super().__init__(key, pluginName, defaultValue, description) - def value(self, dynamicKeyPart=str()): - """ Get settings value. - - :param self: the QgsSettingsEntryFlag object - :param dynamicKeyPart: argument specifies the dynamic part of the settings key. - """ - - return QgsSettings().flagValue(self.key(dynamicKeyPart), - self.defaultValue(), - self.section()) - def settingsType(self): """ Get the settings entry type. diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index fc81a22346fc..0ffee71b59fb 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -75,6 +75,13 @@ The ``description`` argument specifies a description for the settings entry. Get settings entry key. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QString key( const QStringList &dynamicKeyPartList ) const; +%Docstring +Get settings entry key. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End bool hasDynamicKey() const; @@ -87,6 +94,13 @@ Returns true if a part of the settings key is built dynamically. Returns true if the settings is contained in the underlying QSettings. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool exists( const QStringList &dynamicKeyPartList ) const; +%Docstring +Returns true if the settings is contained in the underlying QSettings. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End void remove( const QString &dynamicKeyPart = QString() ) const; @@ -94,6 +108,13 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Removes the settings from the underlying QSettings. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + void remove( const QStringList &dynamicKeyPartList ) const; +%Docstring +Removes the settings from the underlying QSettings. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End QgsSettings::Section section() const; @@ -107,6 +128,14 @@ Set settings value. The ``value`` to set. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + virtual bool setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const; +%Docstring +Set settings value. + +The ``value`` to set. +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const; @@ -114,6 +143,13 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QVariant valueAsVariant( const QStringList &dynamicKeyPartList ) const; +%Docstring +Get settings value. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End QVariant defaultValueAsVariant() const; @@ -162,13 +198,20 @@ The ``default`` value argument specifies the default value for the settings entr The ``description`` argument specifies a description for the settings entry. %End - bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; %Docstring Set settings value. The ``value`` to set. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool setValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const; +%Docstring +Set settings value. + +The ``value`` to set. +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End QVariant value( const QString &dynamicKeyPart = QString() ) const; @@ -176,6 +219,13 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QVariant value( const QStringList &dynamicKeyPartList ) const; +%Docstring +Get settings value. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End QVariant defaultValue() const; @@ -224,6 +274,14 @@ Set settings value. The ``value`` to set. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool setValue( const QString &value, const QStringList &dynamicKeyPartList ) const; +%Docstring +Set settings value. + +The ``value`` to set. +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End QString value( const QString &dynamicKeyPart = QString() ) const; @@ -231,6 +289,13 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QString value( const QStringList &dynamicKeyPartList ) const; +%Docstring +Get settings value. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End QString defaultValue() const; @@ -304,6 +369,14 @@ Set settings value. The ``value`` to set. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool setValue( const QStringList &value, const QStringList &dynamicKeyPartList ) const; +%Docstring +Set settings value. + +The ``value`` to set. +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End QStringList value( const QString &dynamicKeyPart = QString() ) const; @@ -311,6 +384,13 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + QStringList value( const QStringList &dynamicKeyPartList ) const; +%Docstring +Get settings value. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End QStringList defaultValue() const; @@ -360,6 +440,14 @@ Set settings value. The ``value`` to set. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool setValue( bool value, const QStringList &dynamicKeyPartList ) const; +%Docstring +Set settings value. + +The ``value`` to set. +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End bool value( const QString &dynamicKeyPart = QString() ) const; @@ -367,6 +455,13 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool value( const QStringList &dynamicKeyPartList ) const; +%Docstring +Get settings value. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End bool defaultValue() const; @@ -416,6 +511,14 @@ Set settings value. The ``value`` to set. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool setValue( qlonglong value, const QStringList &dynamicKeyPartList ) const; +%Docstring +Set settings value. + +The ``value`` to set. +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End qlonglong value( const QString &dynamicKeyPart = QString() ) const; @@ -423,6 +526,13 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + qlonglong value( const QStringList &dynamicKeyPartList ) const; +%Docstring +Get settings value. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End qlonglong defaultValue() const; @@ -496,6 +606,14 @@ Set settings value. The ``value`` to set. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + bool setValue( double value, const QStringList &dynamicKeyPartList ) const; +%Docstring +Set settings value. + +The ``value`` to set. +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End double value( const QString &dynamicKeyPart = QString() ) const; @@ -503,6 +621,13 @@ The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +%End + + double value( const QStringList &dynamicKeyPartList ) const; +%Docstring +Get settings value. + +The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End double defaultValue() const; diff --git a/src/core/locator/qgslocator.h b/src/core/locator/qgslocator.h index c0bf261f63c7..b5eb3629bfcd 100644 --- a/src/core/locator/qgslocator.h +++ b/src/core/locator/qgslocator.h @@ -154,11 +154,11 @@ class CORE_EXPORT QgsLocator : public QObject QStringList completionList() const {return mAutocompletionList;} //! Settings entry locator filter enabled - static const inline QgsSettingsEntryBool settingsLocatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%" ), QgsSettings::Gui, true, "Locator filter enabled" ); + static const inline QgsSettingsEntryBool settingsLocatorFilterEnabled = QgsSettingsEntryBool( QStringLiteral( "locator_filters/enabled_%1" ), QgsSettings::Gui, true, "Locator filter enabled" ); //! Settings entry locator filter default value - static const inline QgsSettingsEntryBool settingsLocatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%" ), QgsSettings::Gui, false, "Locator filter default value" ); + static const inline QgsSettingsEntryBool settingsLocatorFilterDefault = QgsSettingsEntryBool( QStringLiteral( "locator_filters/default_%1" ), QgsSettings::Gui, false, "Locator filter default value" ); //! Settings entry locator filter prefix - static const inline QgsSettingsEntryString settingsLocatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); + static const inline QgsSettingsEntryString settingsLocatorFilterPrefix = QgsSettingsEntryString( QStringLiteral( "locator_filters/prefix_%1" ), QgsSettings::Gui, QString(), "Locator filter prefix" ); signals: diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index 25b326ec7630..10716ec246a7 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -17,6 +17,8 @@ #include "qgslogger.h" +#include + QgsSettingsEntryBase::QgsSettingsEntryBase( const QString &key, QgsSettings::Section section, const QVariant &defaultValue, const QString &description ) : mKey( key ) , mDefaultValue( defaultValue ) @@ -40,12 +42,25 @@ QgsSettingsEntryBase::~QgsSettingsEntryBase() } QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const +{ + QStringList dynamicKeyPartList; + if ( !dynamicKeyPart.isEmpty() ) + dynamicKeyPartList.append( dynamicKeyPart ); + + return key( dynamicKeyPartList ); +} + +QString QgsSettingsEntryBase::key( const QStringList &dynamicKeyPartList ) const { QString completeKey = mKey; if ( !mPluginName.isEmpty() ) - completeKey.prepend( mPluginName + "/" ); + { + if ( !completeKey.startsWith( "/" ) ) + completeKey.prepend( "/" ); + completeKey.prepend( mPluginName ); + } - if ( dynamicKeyPart.isEmpty() ) + if ( dynamicKeyPartList.isEmpty() ) { if ( hasDynamicKey() ) QgsLogger::warning( QStringLiteral( "Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( completeKey ) ); @@ -60,13 +75,18 @@ QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const return completeKey; } - return completeKey.replace( '%', dynamicKeyPart ); + for ( int i = 0; i < dynamicKeyPartList.size(); i++ ) + { + completeKey.replace( QLatin1String( "%%1" ).arg( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) ); + } } + return completeKey; } bool QgsSettingsEntryBase::hasDynamicKey() const { - return mKey.contains( '%' ); + static const QRegularExpression regularExpression( "%\\d+" ); + return mKey.contains( regularExpression ); } bool QgsSettingsEntryBase::exists( const QString &dynamicKeyPart ) const @@ -74,11 +94,21 @@ bool QgsSettingsEntryBase::exists( const QString &dynamicKeyPart ) const return QgsSettings().contains( key( dynamicKeyPart ), section() ); } +bool QgsSettingsEntryBase::exists( const QStringList &dynamicKeyPartList ) const +{ + return QgsSettings().contains( key( dynamicKeyPartList ), section() ); +} + void QgsSettingsEntryBase::remove( const QString &dynamicKeyPart ) const { QgsSettings().remove( key( dynamicKeyPart ), section() ); } +void QgsSettingsEntryBase::remove( const QStringList &dynamicKeyPartList ) const +{ + QgsSettings().remove( key( dynamicKeyPartList ), section() ); +} + QgsSettings::Section QgsSettingsEntryBase::section() const { return mSection; @@ -86,7 +116,16 @@ QgsSettings::Section QgsSettingsEntryBase::section() const bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QString &dynamicKeyPart ) const { - QgsSettings().setValue( key( dynamicKeyPart ), + QStringList dynamicKeyPartList; + if ( !dynamicKeyPart.isEmpty() ) + dynamicKeyPartList.append( dynamicKeyPart ); + + return setVariantValue( value, dynamicKeyPartList ); +} + +bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const +{ + QgsSettings().setValue( key( dynamicKeyPartList ), value, section() ); return true; @@ -94,7 +133,16 @@ bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QString QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart ) const { - return QgsSettings().value( key( dynamicKeyPart ), + QStringList dynamicKeyPartList; + if ( !dynamicKeyPart.isEmpty() ) + dynamicKeyPartList.append( dynamicKeyPart ); + + return valueAsVariant( dynamicKeyPartList ); +} + +QVariant QgsSettingsEntryBase::valueAsVariant( const QStringList &dynamicKeyPartList ) const +{ + return QgsSettings().value( key( dynamicKeyPartList ), mDefaultValue, mSection ); } @@ -131,11 +179,21 @@ bool QgsSettingsEntryVariant::setValue( const QVariant &value, const QString &dy return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); } +bool QgsSettingsEntryVariant::setValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const +{ + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); +} + QVariant QgsSettingsEntryVariant::value( const QString &dynamicKeyPart ) const { return valueAsVariant( dynamicKeyPart ); } +QVariant QgsSettingsEntryVariant::value( const QStringList &dynamicKeyPartList ) const +{ + return valueAsVariant( dynamicKeyPartList ); +} + QVariant QgsSettingsEntryVariant::defaultValue() const { return defaultValueAsVariant(); @@ -167,11 +225,20 @@ QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, const QStrin } bool QgsSettingsEntryString::setValue( const QString &value, const QString &dynamicKeyPart ) const +{ + QStringList dynamicKeyPartList; + if ( !dynamicKeyPart.isEmpty() ) + dynamicKeyPartList.append( dynamicKeyPart ); + + return setValue( value, dynamicKeyPartList ); +} + +bool QgsSettingsEntryString::setValue( const QString &value, const QStringList &dynamicKeyPartList ) const { if ( value.length() < mMinLength ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is shorter than minimum length '%3'." ) - .arg( QgsSettingsEntryBase::key(), + .arg( QgsSettingsEntryBase::key( dynamicKeyPartList ), value.length(), mMinLength ) ); return false; @@ -181,13 +248,13 @@ bool QgsSettingsEntryString::setValue( const QString &value, const QString &dyna && value.length() > mMaxLength ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. String length '%2' is longer than maximum length '%3'." ) - .arg( QgsSettingsEntryBase::key(), + .arg( QgsSettingsEntryBase::key( dynamicKeyPartList ), value.length(), mMinLength ) ); return false; } - return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); } QString QgsSettingsEntryString::value( const QString &dynamicKeyPart ) const @@ -195,6 +262,11 @@ QString QgsSettingsEntryString::value( const QString &dynamicKeyPart ) const return valueAsVariant( dynamicKeyPart ).toString(); } +QString QgsSettingsEntryString::value( const QStringList &dynamicKeyPartList ) const +{ + return valueAsVariant( dynamicKeyPartList ).toString(); +} + QString QgsSettingsEntryString::defaultValue() const { return defaultValueAsVariant().toString(); @@ -246,11 +318,21 @@ bool QgsSettingsEntryStringList::setValue( const QStringList &value, const QStri return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); } +bool QgsSettingsEntryStringList::setValue( const QStringList &value, const QStringList &dynamicKeyPartList ) const +{ + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); +} + QStringList QgsSettingsEntryStringList::value( const QString &dynamicKeyPart ) const { return valueAsVariant( dynamicKeyPart ).toStringList(); } +QStringList QgsSettingsEntryStringList::value( const QStringList &dynamicKeyPartList ) const +{ + return valueAsVariant( dynamicKeyPartList ).toStringList(); +} + QStringList QgsSettingsEntryStringList::defaultValue() const { return defaultValueAsVariant().toStringList(); @@ -282,11 +364,21 @@ bool QgsSettingsEntryBool::setValue( bool value, const QString &dynamicKeyPart ) return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); } +bool QgsSettingsEntryBool::setValue( bool value, const QStringList &dynamicKeyPartList ) const +{ + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); +} + bool QgsSettingsEntryBool::value( const QString &dynamicKeyPart ) const { return valueAsVariant( dynamicKeyPart ).toBool(); } +bool QgsSettingsEntryBool::value( const QStringList &dynamicKeyPartList ) const +{ + return valueAsVariant( dynamicKeyPartList ).toBool(); +} + bool QgsSettingsEntryBool::defaultValue() const { return defaultValueAsVariant().toBool(); @@ -318,11 +410,20 @@ QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, const QStr } bool QgsSettingsEntryInteger::setValue( qlonglong value, const QString &dynamicKeyPart ) const +{ + QStringList dynamicKeyPartList; + if ( !dynamicKeyPart.isEmpty() ) + dynamicKeyPartList.append( dynamicKeyPart ); + + return setValue( value, dynamicKeyPartList ); +} + +bool QgsSettingsEntryInteger::setValue( qlonglong value, const QStringList &dynamicKeyPartList ) const { if ( value < mMinValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) - .arg( QgsSettingsEntryBase::key(), + .arg( QgsSettingsEntryBase::key( dynamicKeyPartList ), QString::number( value ), QString::number( mMinValue ) ) ); return false; @@ -331,13 +432,13 @@ bool QgsSettingsEntryInteger::setValue( qlonglong value, const QString &dynamicK if ( value > mMaxValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) - .arg( QgsSettingsEntryBase::key(), + .arg( QgsSettingsEntryBase::key( dynamicKeyPartList ), QString::number( value ), QString::number( mMinValue ) ) ); return false; } - return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); } qlonglong QgsSettingsEntryInteger::value( const QString &dynamicKeyPart ) const @@ -345,6 +446,11 @@ qlonglong QgsSettingsEntryInteger::value( const QString &dynamicKeyPart ) const return valueAsVariant( dynamicKeyPart ).toLongLong(); } +qlonglong QgsSettingsEntryInteger::value( const QStringList &dynamicKeyPartList ) const +{ + return valueAsVariant( dynamicKeyPartList ).toLongLong(); +} + qlonglong QgsSettingsEntryInteger::defaultValue() const { return defaultValueAsVariant().toLongLong(); @@ -398,11 +504,20 @@ QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, const QStrin } bool QgsSettingsEntryDouble::setValue( double value, const QString &dynamicKeyPart ) const +{ + QStringList dynamicKeyPartList; + if ( !dynamicKeyPart.isEmpty() ) + dynamicKeyPartList.append( dynamicKeyPart ); + + return setValue( value, dynamicKeyPartList ); +} + +bool QgsSettingsEntryDouble::setValue( double value, const QStringList &dynamicKeyPartList ) const { if ( value < mMinValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." ) - .arg( QgsSettingsEntryBase::key(), + .arg( QgsSettingsEntryBase::key( dynamicKeyPartList ), QString::number( value ), QString::number( mMinValue ) ) ); return false; @@ -411,13 +526,13 @@ bool QgsSettingsEntryDouble::setValue( double value, const QString &dynamicKeyPa if ( value > mMaxValue ) { QgsDebugMsg( QObject::tr( "Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." ) - .arg( QgsSettingsEntryBase::key(), + .arg( QgsSettingsEntryBase::key( dynamicKeyPartList ), QString::number( value ), QString::number( mMinValue ) ) ); return false; } - return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); } double QgsSettingsEntryDouble::value( const QString &dynamicKeyPart ) const @@ -425,6 +540,11 @@ double QgsSettingsEntryDouble::value( const QString &dynamicKeyPart ) const return valueAsVariant( dynamicKeyPart ).toDouble(); } +double QgsSettingsEntryDouble::value( const QStringList &dynamicKeyPartList ) const +{ + return valueAsVariant( dynamicKeyPartList ).toDouble(); +} + double QgsSettingsEntryDouble::defaultValue() const { return defaultValueAsVariant().toDouble(); diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 4e27b9f8f730..9b713fdea236 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -112,6 +112,13 @@ class CORE_EXPORT QgsSettingsEntryBase */ QString key( const QString &dynamicKeyPart = QString() ) const; + /** + * Get settings entry key. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + QString key( const QStringList &dynamicKeyPartList ) const; + /** * Returns true if a part of the settings key is built dynamically. */ @@ -124,6 +131,13 @@ class CORE_EXPORT QgsSettingsEntryBase */ bool exists( const QString &dynamicKeyPart = QString() ) const; + /** + * Returns true if the settings is contained in the underlying QSettings. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + bool exists( const QStringList &dynamicKeyPartList ) const; + /** * Removes the settings from the underlying QSettings. * @@ -131,6 +145,13 @@ class CORE_EXPORT QgsSettingsEntryBase */ void remove( const QString &dynamicKeyPart = QString() ) const; + /** + * Removes the settings from the underlying QSettings. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + void remove( const QStringList &dynamicKeyPartList ) const; + /** * Get settings section. The settings section of the parent group is returned if available. */ @@ -144,6 +165,14 @@ class CORE_EXPORT QgsSettingsEntryBase */ virtual bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + virtual bool setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const; + /** * Get settings value. * @@ -151,6 +180,13 @@ class CORE_EXPORT QgsSettingsEntryBase */ QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const; + /** + * Get settings value. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + QVariant valueAsVariant( const QStringList &dynamicKeyPartList ) const; + /** * Get settings default value. */ @@ -217,7 +253,6 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase const QVariant &defaultValue = QVariant(), const QString &description = QString() ); - /** * Set settings value. * @@ -226,6 +261,14 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase */ bool setValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + bool setValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const; + /** * Get settings value. * @@ -233,6 +276,13 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase */ QVariant value( const QString &dynamicKeyPart = QString() ) const; + /** + * Get settings value. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + QVariant value( const QStringList &dynamicKeyPartList ) const; + /** * Get settings default value. */ @@ -297,6 +347,14 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase */ bool setValue( const QString &value, const QString &dynamicKeyPart = QString() ) const; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + bool setValue( const QString &value, const QStringList &dynamicKeyPartList ) const; + /** * Get settings value. * @@ -304,6 +362,13 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase */ QString value( const QString &dynamicKeyPart = QString() ) const; + /** + * Get settings value. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + QString value( const QStringList &dynamicKeyPartList ) const; + /** * Get settings default value. */ @@ -393,6 +458,14 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase */ bool setValue( const QStringList &value, const QString &dynamicKeyPart = QString() ) const; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + bool setValue( const QStringList &value, const QStringList &dynamicKeyPartList ) const; + /** * Get settings value. * @@ -400,6 +473,13 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase */ QStringList value( const QString &dynamicKeyPart = QString() ) const; + /** + * Get settings value. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + QStringList value( const QStringList &dynamicKeyPartList ) const; + /** * Get settings default value. */ @@ -460,6 +540,14 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase */ bool setValue( bool value, const QString &dynamicKeyPart = QString() ) const; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + bool setValue( bool value, const QStringList &dynamicKeyPartList ) const; + /** * Get settings value. * @@ -467,6 +555,13 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase */ bool value( const QString &dynamicKeyPart = QString() ) const; + /** + * Get settings value. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + bool value( const QStringList &dynamicKeyPartList ) const; + /** * Get settings default value. */ @@ -531,6 +626,14 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase */ bool setValue( qlonglong value, const QString &dynamicKeyPart = QString() ) const; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + bool setValue( qlonglong value, const QStringList &dynamicKeyPartList ) const; + /** * Get settings value. * @@ -538,6 +641,13 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase */ qlonglong value( const QString &dynamicKeyPart = QString() ) const; + /** + * Get settings value. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + qlonglong value( const QStringList &dynamicKeyPartList ) const; + /** * Get settings default value. */ @@ -634,6 +744,14 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase */ bool setValue( double value, const QString &dynamicKeyPart = QString() ) const; + /** + * Set settings value. + * + * The \a value to set. + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + bool setValue( double value, const QStringList &dynamicKeyPartList ) const; + /** * Get settings value. * @@ -641,6 +759,13 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase */ double value( const QString &dynamicKeyPart = QString() ) const; + /** + * Get settings value. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + double value( const QStringList &dynamicKeyPartList ) const; + /** * Get settings default value. */ @@ -742,13 +867,27 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. */ T value( const QString &dynamicKeyPart = QString() ) const + { + QStringList dynamicKeyPartList; + if ( !dynamicKeyPart.isEmpty() ) + dynamicKeyPartList.append( dynamicKeyPart ); + + return value( dynamicKeyPartList ); + } + + /** + * Get settings value. + * + * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + */ + T value( const QStringList &dynamicKeyPartList ) const { if ( settingsType() == QgsSettingsEntryBase::Enum ) - return QgsSettings().enumValue( key( dynamicKeyPart ), + return QgsSettings().enumValue( key( dynamicKeyPartList ), defaultValue(), section() ); else - return QgsSettings().flagValue( key( dynamicKeyPart ), + return QgsSettings().flagValue( key( dynamicKeyPartList ), defaultValue(), section() ); } @@ -782,10 +921,20 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase //! \copydoc QgsSettingsEntryBase::setValue bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const + { + QStringList dynamicKeyPartList; + if ( !dynamicKeyPart.isEmpty() ) + dynamicKeyPartList.append( dynamicKeyPart ); + + return setValue( value, dynamicKeyPartList ); + } + + //! \copydoc QgsSettingsEntryBase::setValue + bool setValue( const T &value, const QStringList &dynamicKeyPartList ) const { if ( !mMetaEnum.isValid() ) { - QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( key() ) ); + QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( key( dynamicKeyPartList ) ) ); return false; } @@ -798,7 +947,7 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase return false; } - return QgsSettingsEntryBase::setVariantValue( enumKey, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( enumKey, dynamicKeyPartList ); } else { @@ -808,7 +957,7 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase QgsDebugMsg( QStringLiteral( "Invalid flag value '%1'." ).arg( value ) ); return false; } - return QgsSettingsEntryBase::setVariantValue( flagKeys, dynamicKeyPart ); + return QgsSettingsEntryBase::setVariantValue( flagKeys, dynamicKeyPartList ); } } diff --git a/tests/src/python/test_qgssettingsentry.py b/tests/src/python/test_qgssettingsentry.py index 87f6354ac66b..2480430095d1 100644 --- a/tests/src/python/test_qgssettingsentry.py +++ b/tests/src/python/test_qgssettingsentry.py @@ -82,11 +82,11 @@ def test_settings_entry_base(self): self.assertEqual(settingsEntryVariant.description(), description) def test_settings_entry_base_dynamic_key(self): - settingsKeyDynamic = "settingsEntryBase/%/variantValue" + settingsKeyDynamic = "settingsEntryBase/%1/variantValue" dynamicKeyPart1 = "first" dynamicKeyPart2 = "second" - settingsKeyComplete1 = self.pluginName + "/" + settingsKeyDynamic.replace("%", dynamicKeyPart1) - settingsKeyComplete2 = self.pluginName + "/" + settingsKeyDynamic.replace("%", dynamicKeyPart2) + settingsKeyComplete1 = self.pluginName + "/" + settingsKeyDynamic.replace("%1", dynamicKeyPart1) + settingsKeyComplete2 = self.pluginName + "/" + settingsKeyDynamic.replace("%1", dynamicKeyPart2) # Make sure settings does not exists QgsSettings().remove(settingsKeyComplete1, QgsSettings.Plugins) @@ -107,6 +107,27 @@ def test_settings_entry_base_dynamic_key(self): self.assertEqual(settingsEntryVariantDynamic.value(dynamicKeyPart1), 43) self.assertEqual(settingsEntryVariantDynamic.value(dynamicKeyPart2), 44) + def test_settings_entry_base_dynamic_multiple_keys(self): + settingsKeyDynamic = "settingsEntryBase/%1/anotherPart_%2/variantValue" + dynamicKeyPart1 = "first" + dynamicKeyPart2 = "second" + settingsKeyComplete = self.pluginName + "/" + settingsKeyDynamic.replace("%1", dynamicKeyPart1) + settingsKeyComplete = settingsKeyComplete.replace("%2", dynamicKeyPart2) + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 42 + settingsEntryVariantDynamic = QgsSettingsEntryVariant(settingsKeyDynamic, self.pluginName, defaultValue, "Variant value for dynamic multiple keys functionality test") + + # Check key + self.assertEqual(settingsEntryVariantDynamic.key([dynamicKeyPart1, dynamicKeyPart2]), settingsKeyComplete) + + # Get set values + settingsEntryVariantDynamic.setValue(43, [dynamicKeyPart1, dynamicKeyPart2]) + self.assertEqual(QgsSettings().value(settingsKeyComplete, defaultValue, section=QgsSettings.Plugins), 43) + self.assertEqual(settingsEntryVariantDynamic.value([dynamicKeyPart1, dynamicKeyPart2]), 43) + def test_settings_entry_variant(self): settingsKey = "settingsEntryVariant/variantValue" settingsKeyComplete = self.pluginName + "/" + settingsKey From 9082cfa26823e8dd6e5f6cb8d64ec814a60b7a0a Mon Sep 17 00:00:00 2001 From: Damiano Date: Wed, 14 Apr 2021 18:13:51 +0200 Subject: [PATCH 18/20] Fixed trailing space --- python/core/additions/qgssettingsentry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/core/additions/qgssettingsentry.py b/python/core/additions/qgssettingsentry.py index 1823f13c97b4..d5267d41a9df 100644 --- a/python/core/additions/qgssettingsentry.py +++ b/python/core/additions/qgssettingsentry.py @@ -38,7 +38,7 @@ def __init__(self, key, pluginName, defaultValue, description=str()): :param defaultValue: argument specifies the default value for the settings entry. :param description: argument specifies a description for the settings entry. - .. note:: This constructor should not be used. Use PyQgsSettingsEntryEnum + .. note:: This constructor should not be used. Use PyQgsSettingsEntryEnum or PyQgsSettingsEntryFlag instead. """ From abb8e80c1f006933eb05969c9fa88be69144c34e Mon Sep 17 00:00:00 2001 From: Damiano Date: Wed, 14 Apr 2021 19:49:57 +0200 Subject: [PATCH 19/20] Added functionality to override default value for settings entries --- python/core/additions/qgssettingsentry.py | 14 +++- .../settings/qgssettingsentry.sip.in | 56 +++++++++---- src/core/locator/qgslocator.cpp | 8 +- src/core/settings/qgssettingsentry.cpp | 79 ++++++++++--------- src/core/settings/qgssettingsentry.h | 74 ++++++++++++----- tests/src/python/test_qgssettingsentry.py | 21 +++++ 6 files changed, 172 insertions(+), 80 deletions(-) diff --git a/python/core/additions/qgssettingsentry.py b/python/core/additions/qgssettingsentry.py index d5267d41a9df..016486e00f8d 100644 --- a/python/core/additions/qgssettingsentry.py +++ b/python/core/additions/qgssettingsentry.py @@ -55,20 +55,26 @@ def __init__(self, key, pluginName, defaultValue, description=str()): super().__init__(key, pluginName, defaultValueStr, description) - def value(self, dynamicKeyPart=str()): + def value(self, dynamicKeyPart=None, useDefaultValueOverride=False, defaultValueOverride=None): """ Get settings value. :param self: the _PyQgsSettingsEntryEnumFlag object :param dynamicKeyPart: argument specifies the dynamic part of the settings key. + :param useDefaultValueOverride: argument specifies if defaultValueOverride should be used. + :param defaultValueOverride: argument if valid is used instead of the normal default value. """ + defaultValue = self.defaultValue() + if useDefaultValueOverride: + defaultValue = defaultValueOverride + if self.settingsType() is self.Enum: return QgsSettings().enumValue(self.key(dynamicKeyPart), - self.defaultValue(), + defaultValue, self.section()) else: return QgsSettings().flagValue(self.key(dynamicKeyPart), - self.defaultValue(), + defaultValue, self.section()) def defaultValue(self): @@ -94,7 +100,7 @@ def defaultValue(self): defaultValue = self.__enumFlagClass(defaultValue) return defaultValue - def setValue(self, value, dynamicKeyPart=str()): + def setValue(self, value, dynamicKeyPart=None): """ Set settings value. :param self: the _PyQgsSettingsEntryEnumFlag object diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index 0ffee71b59fb..4d1ef5c8233b 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -138,18 +138,22 @@ The ``value`` to set. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End - QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const; + QVariant valueAsVariant( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const; %Docstring Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End - QVariant valueAsVariant( const QStringList &dynamicKeyPartList ) const; + QVariant valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const; %Docstring Get settings value. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End QVariant defaultValueAsVariant() const; @@ -214,18 +218,22 @@ The ``value`` to set. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End - QVariant value( const QString &dynamicKeyPart = QString() ) const; + QVariant value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const; %Docstring Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End - QVariant value( const QStringList &dynamicKeyPartList ) const; + QVariant value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const; %Docstring Get settings value. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End QVariant defaultValue() const; @@ -284,18 +292,22 @@ The ``value`` to set. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End - QString value( const QString &dynamicKeyPart = QString() ) const; + QString value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QString &defaultValueOverride = QString() ) const; %Docstring Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End - QString value( const QStringList &dynamicKeyPartList ) const; + QString value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QString &defaultValueOverride = QString() ) const; %Docstring Get settings value. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End QString defaultValue() const; @@ -379,18 +391,22 @@ The ``value`` to set. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End - QStringList value( const QString &dynamicKeyPart = QString() ) const; + QStringList value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QStringList &defaultValueOverride = QStringList() ) const; %Docstring Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End - QStringList value( const QStringList &dynamicKeyPartList ) const; + QStringList value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QStringList &defaultValueOverride = QStringList() ) const; %Docstring Get settings value. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End QStringList defaultValue() const; @@ -450,18 +466,22 @@ The ``value`` to set. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End - bool value( const QString &dynamicKeyPart = QString() ) const; + bool value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, bool defaultValueOverride = false ) const; %Docstring Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End - bool value( const QStringList &dynamicKeyPartList ) const; + bool value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, bool defaultValueOverride = false ) const; %Docstring Get settings value. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End bool defaultValue() const; @@ -521,18 +541,22 @@ The ``value`` to set. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End - qlonglong value( const QString &dynamicKeyPart = QString() ) const; + qlonglong value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, qlonglong defaultValueOverride = 0 ) const; %Docstring Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End - qlonglong value( const QStringList &dynamicKeyPartList ) const; + qlonglong value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, qlonglong defaultValueOverride = 0 ) const; %Docstring Get settings value. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End qlonglong defaultValue() const; @@ -616,18 +640,22 @@ The ``value`` to set. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. %End - double value( const QString &dynamicKeyPart = QString() ) const; + double value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, double defaultValueOverride = 0.0 ) const; %Docstring Get settings value. The ``dynamicKeyPart`` argument specifies the dynamic part of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End - double value( const QStringList &dynamicKeyPartList ) const; + double value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, double defaultValueOverride = 0.0 ) const; %Docstring Get settings value. The ``dynamicKeyParts`` argument specifies the list of dynamic parts of the settings key. +The ``useDefaultValueOverride`` argument specifies if defaultValueOverride should be used. +The ``defaultValueOverride`` argument if valid is used instead of the normal default value. %End double defaultValue() const; diff --git a/src/core/locator/qgslocator.cpp b/src/core/locator/qgslocator.cpp index c145e57066cb..761ea2724225 100644 --- a/src/core/locator/qgslocator.cpp +++ b/src/core/locator/qgslocator.cpp @@ -95,12 +95,8 @@ void QgsLocator::registerFilter( QgsLocatorFilter *filter ) // restore settings bool enabled = QgsLocator::settingsLocatorFilterEnabled.value( filter->name() ); - bool byDefault = filter->useWithoutPrefix(); - if ( QgsLocator::settingsLocatorFilterDefault.exists( filter->name() ) ) - byDefault = QgsLocator::settingsLocatorFilterDefault.value( filter->name() ); - QString prefix = filter->prefix(); - if ( QgsLocator::settingsLocatorFilterPrefix.exists( filter->name() ) ) - prefix = QgsLocator::settingsLocatorFilterPrefix.value( filter->name() ); + bool byDefault = QgsLocator::settingsLocatorFilterDefault.value( filter->name(), true, filter->useWithoutPrefix() ); + QString prefix = QgsLocator::settingsLocatorFilterPrefix.value( filter->name(), true, filter->prefix() ); if ( prefix.isEmpty() ) { prefix = filter->prefix(); diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index 10716ec246a7..1fa82eed0c67 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -44,7 +44,7 @@ QgsSettingsEntryBase::~QgsSettingsEntryBase() QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const { QStringList dynamicKeyPartList; - if ( !dynamicKeyPart.isEmpty() ) + if ( !dynamicKeyPart.isNull() ) dynamicKeyPartList.append( dynamicKeyPart ); return key( dynamicKeyPartList ); @@ -77,7 +77,7 @@ QString QgsSettingsEntryBase::key( const QStringList &dynamicKeyPartList ) const for ( int i = 0; i < dynamicKeyPartList.size(); i++ ) { - completeKey.replace( QLatin1String( "%%1" ).arg( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) ); + completeKey.replace( QString( "%%1" ).arg( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) ); } } return completeKey; @@ -117,7 +117,7 @@ QgsSettings::Section QgsSettingsEntryBase::section() const bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QString &dynamicKeyPart ) const { QStringList dynamicKeyPartList; - if ( !dynamicKeyPart.isEmpty() ) + if ( !dynamicKeyPart.isNull() ) dynamicKeyPartList.append( dynamicKeyPart ); return setVariantValue( value, dynamicKeyPartList ); @@ -131,20 +131,25 @@ bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QString return true; } -QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart ) const +QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const { QStringList dynamicKeyPartList; - if ( !dynamicKeyPart.isEmpty() ) + if ( !dynamicKeyPart.isNull() ) dynamicKeyPartList.append( dynamicKeyPart ); - return valueAsVariant( dynamicKeyPartList ); + return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ); } -QVariant QgsSettingsEntryBase::valueAsVariant( const QStringList &dynamicKeyPartList ) const +QVariant QgsSettingsEntryBase::valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const { - return QgsSettings().value( key( dynamicKeyPartList ), - mDefaultValue, - mSection ); + if ( useDefaultValueOverride ) + return QgsSettings().value( key( dynamicKeyPartList ), + defaultValueOverride, + mSection ); + else + return QgsSettings().value( key( dynamicKeyPartList ), + mDefaultValue, + mSection ); } QVariant QgsSettingsEntryBase::defaultValueAsVariant() const @@ -184,14 +189,14 @@ bool QgsSettingsEntryVariant::setValue( const QVariant &value, const QStringList return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); } -QVariant QgsSettingsEntryVariant::value( const QString &dynamicKeyPart ) const +QVariant QgsSettingsEntryVariant::value( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPart ); + return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ); } -QVariant QgsSettingsEntryVariant::value( const QStringList &dynamicKeyPartList ) const +QVariant QgsSettingsEntryVariant::value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPartList ); + return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ); } QVariant QgsSettingsEntryVariant::defaultValue() const @@ -227,7 +232,7 @@ QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, const QStrin bool QgsSettingsEntryString::setValue( const QString &value, const QString &dynamicKeyPart ) const { QStringList dynamicKeyPartList; - if ( !dynamicKeyPart.isEmpty() ) + if ( !dynamicKeyPart.isNull() ) dynamicKeyPartList.append( dynamicKeyPart ); return setValue( value, dynamicKeyPartList ); @@ -257,14 +262,14 @@ bool QgsSettingsEntryString::setValue( const QString &value, const QStringList & return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); } -QString QgsSettingsEntryString::value( const QString &dynamicKeyPart ) const +QString QgsSettingsEntryString::value( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QString &defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPart ).toString(); + return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toString(); } -QString QgsSettingsEntryString::value( const QStringList &dynamicKeyPartList ) const +QString QgsSettingsEntryString::value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QString &defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPartList ).toString(); + return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toString(); } QString QgsSettingsEntryString::defaultValue() const @@ -323,14 +328,14 @@ bool QgsSettingsEntryStringList::setValue( const QStringList &value, const QStri return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); } -QStringList QgsSettingsEntryStringList::value( const QString &dynamicKeyPart ) const +QStringList QgsSettingsEntryStringList::value( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QStringList &defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPart ).toStringList(); + return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toStringList(); } -QStringList QgsSettingsEntryStringList::value( const QStringList &dynamicKeyPartList ) const +QStringList QgsSettingsEntryStringList::value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QStringList &defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPartList ).toStringList(); + return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toStringList(); } QStringList QgsSettingsEntryStringList::defaultValue() const @@ -369,14 +374,14 @@ bool QgsSettingsEntryBool::setValue( bool value, const QStringList &dynamicKeyPa return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); } -bool QgsSettingsEntryBool::value( const QString &dynamicKeyPart ) const +bool QgsSettingsEntryBool::value( const QString &dynamicKeyPart, bool useDefaultValueOverride, bool defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPart ).toBool(); + return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toBool(); } -bool QgsSettingsEntryBool::value( const QStringList &dynamicKeyPartList ) const +bool QgsSettingsEntryBool::value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, bool defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPartList ).toBool(); + return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toBool(); } bool QgsSettingsEntryBool::defaultValue() const @@ -412,7 +417,7 @@ QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, const QStr bool QgsSettingsEntryInteger::setValue( qlonglong value, const QString &dynamicKeyPart ) const { QStringList dynamicKeyPartList; - if ( !dynamicKeyPart.isEmpty() ) + if ( !dynamicKeyPart.isNull() ) dynamicKeyPartList.append( dynamicKeyPart ); return setValue( value, dynamicKeyPartList ); @@ -441,14 +446,14 @@ bool QgsSettingsEntryInteger::setValue( qlonglong value, const QStringList &dyna return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); } -qlonglong QgsSettingsEntryInteger::value( const QString &dynamicKeyPart ) const +qlonglong QgsSettingsEntryInteger::value( const QString &dynamicKeyPart, bool useDefaultValueOverride, qlonglong defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPart ).toLongLong(); + return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toLongLong(); } -qlonglong QgsSettingsEntryInteger::value( const QStringList &dynamicKeyPartList ) const +qlonglong QgsSettingsEntryInteger::value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, qlonglong defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPartList ).toLongLong(); + return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toLongLong(); } qlonglong QgsSettingsEntryInteger::defaultValue() const @@ -506,7 +511,7 @@ QgsSettingsEntryDouble::QgsSettingsEntryDouble( const QString &key, const QStrin bool QgsSettingsEntryDouble::setValue( double value, const QString &dynamicKeyPart ) const { QStringList dynamicKeyPartList; - if ( !dynamicKeyPart.isEmpty() ) + if ( !dynamicKeyPart.isNull() ) dynamicKeyPartList.append( dynamicKeyPart ); return setValue( value, dynamicKeyPartList ); @@ -535,14 +540,14 @@ bool QgsSettingsEntryDouble::setValue( double value, const QStringList &dynamicK return QgsSettingsEntryBase::setVariantValue( value, dynamicKeyPartList ); } -double QgsSettingsEntryDouble::value( const QString &dynamicKeyPart ) const +double QgsSettingsEntryDouble::value( const QString &dynamicKeyPart, bool useDefaultValueOverride, double defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPart ).toDouble(); + return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toDouble(); } -double QgsSettingsEntryDouble::value( const QStringList &dynamicKeyPartList ) const +double QgsSettingsEntryDouble::value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, double defaultValueOverride ) const { - return valueAsVariant( dynamicKeyPartList ).toDouble(); + return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toDouble(); } double QgsSettingsEntryDouble::defaultValue() const diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index 9b713fdea236..a992d67e3c05 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -177,15 +177,19 @@ class CORE_EXPORT QgsSettingsEntryBase * Get settings value. * * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const; + QVariant valueAsVariant( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const; /** * Get settings value. * * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - QVariant valueAsVariant( const QStringList &dynamicKeyPartList ) const; + QVariant valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const; /** * Get settings default value. @@ -273,15 +277,19 @@ class CORE_EXPORT QgsSettingsEntryVariant : public QgsSettingsEntryBase * Get settings value. * * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - QVariant value( const QString &dynamicKeyPart = QString() ) const; + QVariant value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const; /** * Get settings value. * * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - QVariant value( const QStringList &dynamicKeyPartList ) const; + QVariant value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QVariant &defaultValueOverride = QVariant() ) const; /** * Get settings default value. @@ -359,15 +367,19 @@ class CORE_EXPORT QgsSettingsEntryString : public QgsSettingsEntryBase * Get settings value. * * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - QString value( const QString &dynamicKeyPart = QString() ) const; + QString value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QString &defaultValueOverride = QString() ) const; /** * Get settings value. * * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - QString value( const QStringList &dynamicKeyPartList ) const; + QString value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QString &defaultValueOverride = QString() ) const; /** * Get settings default value. @@ -470,15 +482,19 @@ class CORE_EXPORT QgsSettingsEntryStringList : public QgsSettingsEntryBase * Get settings value. * * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - QStringList value( const QString &dynamicKeyPart = QString() ) const; + QStringList value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const QStringList &defaultValueOverride = QStringList() ) const; /** * Get settings value. * * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - QStringList value( const QStringList &dynamicKeyPartList ) const; + QStringList value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const QStringList &defaultValueOverride = QStringList() ) const; /** * Get settings default value. @@ -552,15 +568,19 @@ class CORE_EXPORT QgsSettingsEntryBool : public QgsSettingsEntryBase * Get settings value. * * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - bool value( const QString &dynamicKeyPart = QString() ) const; + bool value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, bool defaultValueOverride = false ) const; /** * Get settings value. * * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - bool value( const QStringList &dynamicKeyPartList ) const; + bool value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, bool defaultValueOverride = false ) const; /** * Get settings default value. @@ -638,15 +658,19 @@ class CORE_EXPORT QgsSettingsEntryInteger : public QgsSettingsEntryBase * Get settings value. * * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - qlonglong value( const QString &dynamicKeyPart = QString() ) const; + qlonglong value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, qlonglong defaultValueOverride = 0 ) const; /** * Get settings value. * * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - qlonglong value( const QStringList &dynamicKeyPartList ) const; + qlonglong value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, qlonglong defaultValueOverride = 0 ) const; /** * Get settings default value. @@ -756,15 +780,19 @@ class CORE_EXPORT QgsSettingsEntryDouble : public QgsSettingsEntryBase * Get settings value. * * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - double value( const QString &dynamicKeyPart = QString() ) const; + double value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, double defaultValueOverride = 0.0 ) const; /** * Get settings value. * * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - double value( const QStringList &dynamicKeyPartList ) const; + double value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, double defaultValueOverride = 0.0 ) const; /** * Get settings default value. @@ -865,30 +893,38 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase * Get settings value. * * The \a dynamicKeyPart argument specifies the dynamic part of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - T value( const QString &dynamicKeyPart = QString() ) const + T value( const QString &dynamicKeyPart = QString(), bool useDefaultValueOverride = false, const T &defaultValueOverride = T() ) const { QStringList dynamicKeyPartList; if ( !dynamicKeyPart.isEmpty() ) dynamicKeyPartList.append( dynamicKeyPart ); - return value( dynamicKeyPartList ); + return value( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ); } /** * Get settings value. * * The \a dynamicKeyParts argument specifies the list of dynamic parts of the settings key. + * The \a useDefaultValueOverride argument specifies if defaultValueOverride should be used. + * The \a defaultValueOverride argument if valid is used instead of the normal default value. */ - T value( const QStringList &dynamicKeyPartList ) const + T value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride = false, const T &defaultValueOverride = T() ) const { + T defaultVal = defaultValue(); + if ( useDefaultValueOverride ) + defaultVal = defaultValueOverride; + if ( settingsType() == QgsSettingsEntryBase::Enum ) return QgsSettings().enumValue( key( dynamicKeyPartList ), - defaultValue(), + defaultVal, section() ); else return QgsSettings().flagValue( key( dynamicKeyPartList ), - defaultValue(), + defaultVal, section() ); } diff --git a/tests/src/python/test_qgssettingsentry.py b/tests/src/python/test_qgssettingsentry.py index 2480430095d1..3bfb0908192d 100644 --- a/tests/src/python/test_qgssettingsentry.py +++ b/tests/src/python/test_qgssettingsentry.py @@ -81,6 +81,27 @@ def test_settings_entry_base(self): # Description self.assertEqual(settingsEntryVariant.description(), description) + def test_settings_entry_base_default_value_override(self): + settingsKey = "settingsEntryBase/defaultValueOverride/variantValue" + settingsKeyComplete = self.pluginName + "/" + settingsKey + + # Make sure settings does not exists + QgsSettings().remove(settingsKeyComplete, QgsSettings.Plugins) + + defaultValue = 42 + defaultValueOverride = 123 + description = "Variant value for default override functionality test" + settingsEntryVariant = QgsSettingsEntryVariant(settingsKey, self.pluginName, defaultValue, description) + + # Normal default value + self.assertEqual(settingsEntryVariant.value(), defaultValue) + + # Normal default value + self.assertEqual(settingsEntryVariant.value(None, False, defaultValueOverride), defaultValue) + + # Overridden default value + self.assertEqual(settingsEntryVariant.value(None, True, defaultValueOverride), defaultValueOverride) + def test_settings_entry_base_dynamic_key(self): settingsKeyDynamic = "settingsEntryBase/%1/variantValue" dynamicKeyPart1 = "first" From 97f1a645f0793ed593107feb1e98fa3c8442f001 Mon Sep 17 00:00:00 2001 From: Damiano Date: Thu, 15 Apr 2021 09:58:39 +0200 Subject: [PATCH 20/20] Changed SettingsType to scoped enum --- python/core/additions/qgssettingsentry.py | 12 ++++++------ python/core/auto_additions/qgssettingsentry.py | 12 ++++++++++++ .../settings/qgssettingsentry.sip.in | 2 +- src/core/settings/qgssettingsentry.cpp | 12 ++++++------ src/core/settings/qgssettingsentry.h | 12 ++++++------ tests/src/core/testqgssettingsentry.cpp | 4 ++-- tests/src/python/test_qgssettingsentry.py | 18 +++++++++--------- 7 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 python/core/auto_additions/qgssettingsentry.py diff --git a/python/core/additions/qgssettingsentry.py b/python/core/additions/qgssettingsentry.py index 016486e00f8d..447a8228b3ab 100644 --- a/python/core/additions/qgssettingsentry.py +++ b/python/core/additions/qgssettingsentry.py @@ -47,7 +47,7 @@ def __init__(self, key, pluginName, defaultValue, description=str()): if self.__metaEnum is None or not self.__metaEnum.isValid(): QgsLogger.debug("Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '{0}'".format(self.key())) else: - if self.settingsType() is self.Enum: + if self.settingsType() is self.SettingsType.Enum: defaultValueStr = self.__metaEnum.valueToKey(defaultValue) else: defaultValueStr = self.__metaEnum.valueToKeys(defaultValue) @@ -68,7 +68,7 @@ def value(self, dynamicKeyPart=None, useDefaultValueOverride=False, defaultValue if useDefaultValueOverride: defaultValue = defaultValueOverride - if self.settingsType() is self.Enum: + if self.settingsType() is self.SettingsType.Enum: return QgsSettings().enumValue(self.key(dynamicKeyPart), defaultValue, self.section()) @@ -88,7 +88,7 @@ def defaultValue(self): return -1 defaultValueString = self.defaultValueAsVariant() - if self.settingsType() is self.Enum: + if self.settingsType() is self.SettingsType.Enum: (defaultValue, ok) = self.__metaEnum.keyToValue(defaultValueString) else: (defaultValue, ok) = self.__metaEnum.keysToValue(defaultValueString) @@ -112,7 +112,7 @@ def setValue(self, value, dynamicKeyPart=None): return False enumFlagKey = str() - if self.settingsType() is self.Enum: + if self.settingsType() is self.SettingsType.Enum: enumFlagKey = self.__metaEnum.valueToKey(value) else: enumFlagKey = self.__metaEnum.valueToKeys(value) @@ -150,7 +150,7 @@ def settingsType(self): :param self: the QgsSettingsEntryEnum object """ - return self.Enum + return self.SettingsType.Enum class PyQgsSettingsEntryFlag(_PyQgsSettingsEntryEnumFlag): @@ -180,4 +180,4 @@ def settingsType(self): :param self: the QgsSettingsEntryFlag object """ - return self.Flag + return self.SettingsType.Flag diff --git a/python/core/auto_additions/qgssettingsentry.py b/python/core/auto_additions/qgssettingsentry.py new file mode 100644 index 000000000000..246bc2767354 --- /dev/null +++ b/python/core/auto_additions/qgssettingsentry.py @@ -0,0 +1,12 @@ +# The following has been generated automatically from src/core/settings/qgssettingsentry.h +# monkey patching scoped based enum +QgsSettingsEntryBase.SettingsType.Variant.__doc__ = "" +QgsSettingsEntryBase.SettingsType.String.__doc__ = "" +QgsSettingsEntryBase.SettingsType.StringList.__doc__ = "" +QgsSettingsEntryBase.SettingsType.Bool.__doc__ = "" +QgsSettingsEntryBase.SettingsType.Integer.__doc__ = "" +QgsSettingsEntryBase.SettingsType.Double.__doc__ = "" +QgsSettingsEntryBase.SettingsType.Enum.__doc__ = "" +QgsSettingsEntryBase.SettingsType.Flag.__doc__ = "" +QgsSettingsEntryBase.SettingsType.__doc__ = '\n\n' + '* ``Variant``: ' + QgsSettingsEntryBase.SettingsType.Variant.__doc__ + '\n' + '* ``String``: ' + QgsSettingsEntryBase.SettingsType.String.__doc__ + '\n' + '* ``StringList``: ' + QgsSettingsEntryBase.SettingsType.StringList.__doc__ + '\n' + '* ``Bool``: ' + QgsSettingsEntryBase.SettingsType.Bool.__doc__ + '\n' + '* ``Integer``: ' + QgsSettingsEntryBase.SettingsType.Integer.__doc__ + '\n' + '* ``Double``: ' + QgsSettingsEntryBase.SettingsType.Double.__doc__ + '\n' + '* ``Enum``: ' + QgsSettingsEntryBase.SettingsType.Enum.__doc__ + '\n' + '* ``Flag``: ' + QgsSettingsEntryBase.SettingsType.Flag.__doc__ +# -- diff --git a/python/core/auto_generated/settings/qgssettingsentry.sip.in b/python/core/auto_generated/settings/qgssettingsentry.sip.in index 4d1ef5c8233b..617bcd4cf5a5 100644 --- a/python/core/auto_generated/settings/qgssettingsentry.sip.in +++ b/python/core/auto_generated/settings/qgssettingsentry.sip.in @@ -41,7 +41,7 @@ to validate set values and provide more accurate settings description for the gu %End public: - enum SettingsType + enum class SettingsType { Variant, String, diff --git a/src/core/settings/qgssettingsentry.cpp b/src/core/settings/qgssettingsentry.cpp index 1fa82eed0c67..0f6510d33631 100644 --- a/src/core/settings/qgssettingsentry.cpp +++ b/src/core/settings/qgssettingsentry.cpp @@ -206,7 +206,7 @@ QVariant QgsSettingsEntryVariant::defaultValue() const QgsSettingsEntryBase::SettingsType QgsSettingsEntryVariant::settingsType() const { - return QgsSettingsEntryBase::Variant; + return QgsSettingsEntryBase::SettingsType::Variant; } QgsSettingsEntryString::QgsSettingsEntryString( const QString &key, QgsSettings::Section section, const QString &defaultValue, const QString &description, int minLength, int maxLength ) @@ -279,7 +279,7 @@ QString QgsSettingsEntryString::defaultValue() const QgsSettingsEntryBase::SettingsType QgsSettingsEntryString::settingsType() const { - return QgsSettingsEntryBase::String; + return QgsSettingsEntryBase::SettingsType::String; } void QgsSettingsEntryString::setMinLength( int minLength ) @@ -345,7 +345,7 @@ QStringList QgsSettingsEntryStringList::defaultValue() const QgsSettingsEntryBase::SettingsType QgsSettingsEntryStringList::settingsType() const { - return QgsSettingsEntryBase::StringList; + return QgsSettingsEntryBase::SettingsType::StringList; } QgsSettingsEntryBool::QgsSettingsEntryBool( const QString &key, QgsSettings::Section section, bool defaultValue, const QString &description ) @@ -391,7 +391,7 @@ bool QgsSettingsEntryBool::defaultValue() const QgsSettingsEntryBase::SettingsType QgsSettingsEntryBool::settingsType() const { - return QgsSettingsEntryBase::Bool; + return QgsSettingsEntryBase::SettingsType::Bool; } QgsSettingsEntryInteger::QgsSettingsEntryInteger( const QString &key, QgsSettings::Section section, qlonglong defaultValue, const QString &description, qlonglong minValue, qlonglong maxValue ) @@ -463,7 +463,7 @@ qlonglong QgsSettingsEntryInteger::defaultValue() const QgsSettingsEntryBase::SettingsType QgsSettingsEntryInteger::settingsType() const { - return QgsSettingsEntryBase::Integer; + return QgsSettingsEntryBase::SettingsType::Integer; } void QgsSettingsEntryInteger::setMinValue( qlonglong minValue ) @@ -557,7 +557,7 @@ double QgsSettingsEntryDouble::defaultValue() const QgsSettingsEntryBase::SettingsType QgsSettingsEntryDouble::settingsType() const { - return QgsSettingsEntryBase::Double; + return QgsSettingsEntryBase::SettingsType::Double; } void QgsSettingsEntryDouble::setMinValue( double minValue ) diff --git a/src/core/settings/qgssettingsentry.h b/src/core/settings/qgssettingsentry.h index a992d67e3c05..2b11e0d712c6 100644 --- a/src/core/settings/qgssettingsentry.h +++ b/src/core/settings/qgssettingsentry.h @@ -57,7 +57,7 @@ class CORE_EXPORT QgsSettingsEntryBase public: - enum SettingsType + enum class SettingsType : int { Variant, String, @@ -918,7 +918,7 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase if ( useDefaultValueOverride ) defaultVal = defaultValueOverride; - if ( settingsType() == QgsSettingsEntryBase::Enum ) + if ( settingsType() == QgsSettingsEntryBase::SettingsType::Enum ) return QgsSettings().enumValue( key( dynamicKeyPartList ), defaultVal, section() ); @@ -941,7 +941,7 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase bool ok = false; T defaultValue; - if ( settingsType() == QgsSettingsEntryBase::Enum ) + if ( settingsType() == QgsSettingsEntryBase::SettingsType::Enum ) defaultValue = static_cast( mMetaEnum.keyToValue( defaultValueAsVariant().toByteArray(), &ok ) ); else defaultValue = static_cast( mMetaEnum.keysToValue( defaultValueAsVariant().toByteArray(), &ok ) ); @@ -974,7 +974,7 @@ class CORE_EXPORT QgsSettingsEntryEnumFlagBase : public QgsSettingsEntryBase return false; } - if ( settingsType() == QgsSettingsEntryBase::Enum ) + if ( settingsType() == QgsSettingsEntryBase::SettingsType::Enum ) { const char *enumKey = mMetaEnum.valueToKey( value ); if ( enumKey == nullptr ) @@ -1043,7 +1043,7 @@ class CORE_EXPORT QgsSettingsEntryEnum : public QgsSettingsEntryEnumFlagBase //! \copydoc QgsSettingsEntryBase::settingsType virtual QgsSettingsEntryBase::SettingsType settingsType() const override { - return QgsSettingsEntryBase::Enum; + return QgsSettingsEntryBase::SettingsType::Enum; } }; #endif @@ -1087,7 +1087,7 @@ class CORE_EXPORT QgsSettingsEntryFlag : public QgsSettingsEntryEnumFlagBase //! \copydoc QgsSettingsEntryBase::settingsType virtual QgsSettingsEntryBase::SettingsType settingsType() const override { - return QgsSettingsEntryBase::Flag; + return QgsSettingsEntryBase::SettingsType::Flag; } }; #endif diff --git a/tests/src/core/testqgssettingsentry.cpp b/tests/src/core/testqgssettingsentry.cpp index d06b610851fd..f060258a02c0 100644 --- a/tests/src/core/testqgssettingsentry.cpp +++ b/tests/src/core/testqgssettingsentry.cpp @@ -118,7 +118,7 @@ void TestQgsSettingsEntry::enumValue() QCOMPARE( settingsEntryEnum.value(), QgsUnitTypes::LayoutPicas ); // Check settings type - QCOMPARE( settingsEntryEnum.settingsType(), QgsSettingsEntryBase::Enum ); + QCOMPARE( settingsEntryEnum.settingsType(), QgsSettingsEntryBase::SettingsType::Enum ); // assign to inexisting value { @@ -167,7 +167,7 @@ void TestQgsSettingsEntry::flagValue() QCOMPARE( settingsEntryFlag.value(), pointAndLine ); // Check settings type - QCOMPARE( settingsEntryFlag.settingsType(), QgsSettingsEntryBase::Flag ); + QCOMPARE( settingsEntryFlag.settingsType(), QgsSettingsEntryBase::SettingsType::Flag ); // check that value is stored as string QCOMPARE( settingsEntryFlag.valueAsVariant().toByteArray(), QMetaEnum::fromType().valueToKeys( pointAndLine ) ); diff --git a/tests/src/python/test_qgssettingsentry.py b/tests/src/python/test_qgssettingsentry.py index 3bfb0908192d..547002e02508 100644 --- a/tests/src/python/test_qgssettingsentry.py +++ b/tests/src/python/test_qgssettingsentry.py @@ -76,7 +76,7 @@ def test_settings_entry_base(self): self.assertEqual(settingsEntryVariant.valueAsVariant(), 43) # Settings type - self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.Variant) + self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.SettingsType.Variant) # Description self.assertEqual(settingsEntryVariant.description(), description) @@ -169,7 +169,7 @@ def test_settings_entry_variant(self): self.assertEqual(settingsEntryVariant.valueAsVariant(), "abc") # Settings type - self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.Variant) + self.assertEqual(settingsEntryVariant.settingsType(), QgsSettingsEntryBase.SettingsType.Variant) def test_settings_entry_string(self): settingsKey = "settingsEntryString/stringValue" @@ -191,7 +191,7 @@ def test_settings_entry_string(self): self.assertEqual(settingsEntryString.valueAsVariant(), "xyz") # Settings type - self.assertEqual(settingsEntryString.settingsType(), QgsSettingsEntryBase.String) + self.assertEqual(settingsEntryString.settingsType(), QgsSettingsEntryBase.SettingsType.String) def test_settings_entry_stringlist(self): settingsKey = "settingsEntryStringList/stringListValue" @@ -213,7 +213,7 @@ def test_settings_entry_stringlist(self): self.assertEqual(settingsEntryStringList.valueAsVariant(), ["uvw", "xyz"]) # Settings type - self.assertEqual(settingsEntryStringList.settingsType(), QgsSettingsEntryBase.StringList) + self.assertEqual(settingsEntryStringList.settingsType(), QgsSettingsEntryBase.SettingsType.StringList) def test_settings_entry_bool(self): settingsKey = "settingsEntryBool/boolValue" @@ -235,7 +235,7 @@ def test_settings_entry_bool(self): self.assertEqual(settingsEntryBool.valueAsVariant(), True) # Settings type - self.assertEqual(settingsEntryBool.settingsType(), QgsSettingsEntryBase.Bool) + self.assertEqual(settingsEntryBool.settingsType(), QgsSettingsEntryBase.SettingsType.Bool) def test_settings_entry_integer(self): settingsKey = "settingsEntryInteger/integerValue" @@ -263,7 +263,7 @@ def test_settings_entry_integer(self): self.assertEqual(settingsEntryInteger.valueAsVariant(), -42) # Settings type - self.assertEqual(settingsEntryInteger.settingsType(), QgsSettingsEntryBase.Integer) + self.assertEqual(settingsEntryInteger.settingsType(), QgsSettingsEntryBase.SettingsType.Integer) def test_settings_entry_double(self): settingsKey = "settingsEntryDouble/doubleValue" @@ -291,7 +291,7 @@ def test_settings_entry_double(self): self.assertEqual(settingsEntryDouble.valueAsVariant(), -273.15) # Settings type - self.assertEqual(settingsEntryDouble.settingsType(), QgsSettingsEntryBase.Double) + self.assertEqual(settingsEntryDouble.settingsType(), QgsSettingsEntryBase.SettingsType.Double) def test_settings_entry_enum(self): settingsKey = "settingsEntryEnum/enumValue" @@ -318,7 +318,7 @@ def test_settings_entry_enum(self): self.assertEqual(settingsEntryEnum.value(), QgsUnitTypes.LayoutPicas) # Check settings type - self.assertEqual(settingsEntryEnum.settingsType(), QgsSettingsEntryBase.Enum) + self.assertEqual(settingsEntryEnum.settingsType(), QgsSettingsEntryBase.SettingsType.Enum) # assign to inexisting value success = settingsEntryEnum.setValue(-1) @@ -356,7 +356,7 @@ def test_settings_entry_flag(self): self.assertEqual(settingsEntryFlag.value(), pointAndPolygon) # Check settings type - self.assertEqual(settingsEntryFlag.settingsType(), QgsSettingsEntryBase.Flag) + self.assertEqual(settingsEntryFlag.settingsType(), QgsSettingsEntryBase.SettingsType.Flag) if __name__ == '__main__':