Skip to content

Commit

Permalink
simplification of settings API
Browse files Browse the repository at this point in the history
always pass value by reference (drop of QgsSettingsEntryByValue)
undeprecate setVariantValue
  • Loading branch information
3nids committed Apr 23, 2023
1 parent 8ce4718 commit 3534cd8
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 283 deletions.
3 changes: 1 addition & 2 deletions python/sipify.yaml
Expand Up @@ -2,8 +2,7 @@

class_headerfile:
QgsAbstractFeatureIteratorFromSource: qgsfeatureiterator.h
QgsSettingsEntryByReference: qgssettingsentry.h
QgsSettingsEntryByValue: qgssettingsentry.h
QgsSettingsEntryBaseTemplate: qgssettingsentry.h


no_export_macro:
Expand Down
14 changes: 7 additions & 7 deletions src/core/settings/qgssettingsentry.cpp
Expand Up @@ -43,6 +43,11 @@ QgsSettingsEntryBase::~QgsSettingsEntryBase()
mParentTreeElement->unregisterChildSetting( this );
}

QString QgsSettingsEntryBase::typeId() const
{
return QString::fromUtf8( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( settingsType() ) ) );
}


QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const
{
Expand Down Expand Up @@ -139,15 +144,10 @@ int QgsSettingsEntryBase::section() const

bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QString &dynamicKeyPart ) const
{
return setVariantValuePrivate( value, dynamicKeyPartToList( dynamicKeyPart ) );
return setVariantValue( value, dynamicKeyPartToList( dynamicKeyPart ) );
}

bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const
{
return setVariantValuePrivate( value, dynamicKeyPartList );
}

bool QgsSettingsEntryBase::setVariantValuePrivate( const QVariant &value, const QStringList &dynamicKeyPartList ) const
{
if ( mOptions.testFlag( Qgis::SettingsOption::SaveFormerValue ) )
{
Expand Down Expand Up @@ -243,7 +243,7 @@ bool QgsSettingsEntryBase::copyValueFromKey( const QString &key, const QStringLi
if ( settings.contains( oldCompleteKey ) )
{
QVariant oldValue = settings.value( oldCompleteKey, mDefaultValue );
setVariantValuePrivate( oldValue, dynamicKeyPartList );
setVariantValue( oldValue, dynamicKeyPartList );
if ( removeSettingAtKey )
settings.remove( oldCompleteKey );
return true;
Expand Down
251 changes: 40 additions & 211 deletions src/core/settings/qgssettingsentry.h
Expand Up @@ -28,6 +28,9 @@
class QgsSettingsTreeNode;


static const inline QMetaEnum sSettingsTypeMetaEnum = QMetaEnum::fromType<Qgis::SettingsType>() SIP_SKIP;


/**
* \ingroup core
* \class QgsSettingsEntryBase
Expand Down Expand Up @@ -118,6 +121,13 @@ class CORE_EXPORT QgsSettingsEntryBase
*/
virtual ~QgsSettingsEntryBase();

/**
* Returns the id of the type of settings
* This can be re-implemented in a custom implementation of a setting
* \since QGIS 3.32
*/
virtual QString typeId() const;

/**
* Returns the name of the settings
* \since QGIS 3.30
Expand Down Expand Up @@ -213,18 +223,16 @@ class CORE_EXPORT QgsSettingsEntryBase
*
* \param value specifies the value to set.
* \param dynamicKeyPart specifies the dynamic part of the settings key.
* \deprecated since QGIS 3.26 use setVariantValuePrivate or an implementation setValue instead
*/
Q_DECL_DEPRECATED virtual bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const SIP_DEPRECATED;
bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;

/**
* Set settings value.
*
* \param value specifies the value to set.
* This should be called from any implementation as it takes care of actually calling QSettings
* \param value specifies the value to set.
* \param dynamicKeyPartList specifies the list of dynamic parts of the settings key.
* \deprecated since QGIS 3.26 use setVariantValuePrivate or an implementation setValue instead
*/
Q_DECL_DEPRECATED virtual bool setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const SIP_DEPRECATED;
bool setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const;

//! Returns settings value with \param dynamicKeyPart specifying the dynamic part of the settings key.
QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const;
Expand Down Expand Up @@ -323,14 +331,12 @@ class CORE_EXPORT QgsSettingsEntryBase
*/
QgsSettingsTreeNode *parent() const {return mParentTreeElement;}

protected:

/**
* Sets the settings value with a variant value.
* This should be called from any implementation as it takes care of actually calling QSettings
* \since QGIS 3.26
*/
bool setVariantValuePrivate( const QVariant &value, const QStringList &dynamicKeyPartList = QStringList() ) const;
//! Returns TRUE if the given \a value is valid towards the setting definition
virtual bool checkValueVariant( const QVariant &value ) const
{
Q_UNUSED( value )
return true;
}

private:
QString formerValuekey( const QStringList &dynamicKeyPartList ) const;
Expand All @@ -345,7 +351,6 @@ class CORE_EXPORT QgsSettingsEntryBase
Qgis::SettingsOptions mOptions;
};


/**
* \ingroup core
* \class QgsSettingsEntryByReference
Expand All @@ -357,7 +362,7 @@ class CORE_EXPORT QgsSettingsEntryBase
* \since QGIS 3.26
*/
template<class T>
class QgsSettingsEntryByReference : public QgsSettingsEntryBase
class QgsSettingsEntryBaseTemplate : public QgsSettingsEntryBase
{
public:

Expand All @@ -373,11 +378,11 @@ class QgsSettingsEntryByReference : public QgsSettingsEntryBase
*
* \since QGIS 3.30
*/
QgsSettingsEntryByReference( const QString &name,
QgsSettingsTreeNode *parent,
const T &defaultValue,
const QString &description = QString(),
Qgis::SettingsOptions options = Qgis::SettingsOptions() )
QgsSettingsEntryBaseTemplate( const QString &name,
QgsSettingsTreeNode *parent,
const QVariant &defaultValue,
const QString &description = QString(),
Qgis::SettingsOptions options = Qgis::SettingsOptions() )
: QgsSettingsEntryBase( name, parent, defaultValue, description, options )
{}

Expand All @@ -390,11 +395,11 @@ class QgsSettingsEntryByReference : public QgsSettingsEntryBase
* \param description specifies a description for the settings entry.
* \param options specifies the options for the settings entry.
*/
QgsSettingsEntryByReference( const QString &key,
const QString &section,
const T &defaultValue,
const QString &description = QString(),
Qgis::SettingsOptions options = Qgis::SettingsOptions() )
QgsSettingsEntryBaseTemplate( const QString &key,
const QString &section,
const QVariant &defaultValue,
const QString &description = QString(),
Qgis::SettingsOptions options = Qgis::SettingsOptions() )
: QgsSettingsEntryBase( key, section, defaultValue, description, options )
{}

Expand Down Expand Up @@ -428,30 +433,6 @@ class QgsSettingsEntryByReference : public QgsSettingsEntryBase
return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPartList ) );
}

/**
* Returns the settings value for the \a dynamicKeyPart and with a \a defaultValueOverride
* \deprecated since QGIS 3.26 use valueAsVariantWithDefaultOverride instead
*/
Q_DECL_DEPRECATED T value( const QString &dynamicKeyPart, bool useDefaultValueOverride, const T &defaultValueOverride ) const SIP_DEPRECATED
{
if ( useDefaultValueOverride )
return this->convertFromVariant( valueAsVariantWithDefaultOverride( defaultValueOverride, dynamicKeyPart ) );
else
return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );
}

/**
* Returns the settings value for the \a dynamicKeyPartList and with a \a defaultValueOverride
* \deprecated since QGIS 3.26 use valueAsVariantWithDefaultOverride instead
*/
Q_DECL_DEPRECATED T value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const T &defaultValueOverride ) const SIP_DEPRECATED
{
if ( useDefaultValueOverride )
return this->convertFromVariant( valueAsVariantWithDefaultOverride( defaultValueOverride, dynamicKeyPartList ) );
else
return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );
}

/**
* Set settings value.
*
Expand Down Expand Up @@ -489,191 +470,39 @@ class QgsSettingsEntryByReference : public QgsSettingsEntryBase
*/
T formerValue( const QStringList &dynamicKeyPartList ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPartList ) );}

protected:
//! Sets the settings value with an optional list of dynamic parts
bool setValuePrivate( const T &value, const QStringList &dynamicKeyPartList ) const
bool checkValueVariant( const QVariant &value ) const override
{
if ( checkValue( value ) )
return QgsSettingsEntryBase::setVariantValuePrivate( convertToVariant( value ), dynamicKeyPartList );
else
return false;
return checkValuePrivate( convertFromVariant( value ) );
}

//! Converts the variant value to the value type of the setting
virtual T convertFromVariant( const QVariant &value ) const = 0;

//! Converts the value to a variant
virtual QVariant convertToVariant( const T &value ) const
{
return QVariant::fromValue( value );
}

//! Check if the value is valid
virtual bool checkValue( const T &value ) const
{
Q_UNUSED( value )
return true;
}
};


/**
* \ingroup core
* \class QgsSettingsEntryByValue
*
* \brief Base abstract class for settings entry which are passed by value
* \see QgsSettingsEntryBase
* \see QgsSettingsEntryByReference
*
* \since QGIS 3.26
*/
template<class T>
class QgsSettingsEntryByValue : public QgsSettingsEntryBase
{
public:

/**
* Constructor for QgsSettingsEntryByValue.
*
* \param key specifies the key of the settings.
* \param parent specifies the parent in the tree of settings.
* \param defaultValue specifies the default value for the settings entry.
* \param description specifies a description for the settings entry.
* \param options specifies the options for the settings entry.
* \throws QgsSettingsException if the number of given parent named items doesn't match the complete key definition
*/
QgsSettingsEntryByValue( const QString &key, QgsSettingsTreeNode *parent, QVariant defaultValue, const QString &description = QString(), Qgis::SettingsOptions options = Qgis::SettingsOptions() )
: QgsSettingsEntryBase( key, parent, defaultValue, description, options )
{}

/**
* Constructor for QgsSettingsEntryByValue.
*
* \param key specifies the key of the settings.
* \param section specifies the section.
* \param defaultValue specifies the default value for the settings entry.
* \param description specifies a description for the settings entry.
* \param options specifies the options for the settings entry.
*/
QgsSettingsEntryByValue( const QString &key, const QString &section, QVariant defaultValue, const QString &description = QString(), Qgis::SettingsOptions options = Qgis::SettingsOptions() )
: QgsSettingsEntryBase( key, section, defaultValue, description, options )
{}

virtual Qgis::SettingsType settingsType() const override = 0;

/**
* Returns settings value.
*
* \param dynamicKeyPart specifies the dynamic part of the settings key.
*/
T value( const QString &dynamicKeyPart = QString() ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );}

/**
* Returns settings value.
*
* \param dynamicKeyPartList specifies the list of dynamic parts of the settings key.
*/
T value( const QStringList &dynamicKeyPartList ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );}

//! Returns the settings value with a \a defaultValueOverride and with an optional \a dynamicKeyPart
inline T valueWithDefaultOverride( T defaultValueOverride, const QString &dynamicKeyPart = QString() ) const
{
return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPart ) );
}

//! Returns the settings value with a \a defaultValueOverride for the \a dynamicKeyPartList
inline T valueWithDefaultOverride( T defaultValueOverride, const QStringList &dynamicKeyPartList ) const
{
return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPartList ) );
}

/**
* Returns the settings value for the \a dynamicKeyPart and with a \a defaultValueOverride
* \deprecated since QGIS 3.26 use valueWithDefaultOverride instead
*/
Q_DECL_DEPRECATED T value( const QString &dynamicKeyPart, bool useDefaultValueOverride, T defaultValueOverride ) const SIP_DEPRECATED
{
if ( useDefaultValueOverride )
return this->convertFromVariant( valueAsVariantWithDefaultOverride( defaultValueOverride, dynamicKeyPart ) );
else
return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );
}

/**
* Returns the settings value for the \a dynamicKeyPartList and with a \a defaultValueOverride
* \deprecated since QGIS 3.26 use valueWithDefaultOverride instead
*/
Q_DECL_DEPRECATED T value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, T defaultValueOverride ) const SIP_DEPRECATED
{
if ( useDefaultValueOverride )
return this->convertFromVariant( valueAsVariantWithDefaultOverride( defaultValueOverride, dynamicKeyPartList ) );
else
return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );
}

/**
* Set settings value.
*
* \param value specifies the value to set.
* \param dynamicKeyPart specifies the dynamic part of the settings key.
*/
bool setValue( T value, const QString &dynamicKeyPart = QString() ) const
{
return setValuePrivate( value, dynamicKeyPartToList( dynamicKeyPart ) );
}

/**
* Set settings value.
*
* \param value specifies the value to set.
* \param dynamicKeyPartList specifies the list of dynamic parts of the settings key.
*/
bool setValue( T value, const QStringList &dynamicKeyPartList ) const
{
return setValuePrivate( value, dynamicKeyPartList );
}

//! Returns settings default value.
T defaultValue() const {return convertFromVariant( defaultValueAsVariant() );}

/**
* Returns the former value
* Returns the current value (or default) if there is no former value.
*/
T formerValue( const QString &dynamicKeyPart = QString() ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPart ) );}

/**
* Returns the former value
* Returns the current value (or default) if there is no former value.
*/
T formerValue( const QStringList &dynamicKeyPartList ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPartList ) );}

protected:
//! Sets the settings value with an optional list of dynamic parts
virtual bool setValuePrivate( T value, const QStringList &dynamicKeyPartList ) const
virtual bool setValuePrivate( const T &value, const QStringList &dynamicKeyPartList ) const
{
if ( checkValue( value ) )
return QgsSettingsEntryBase::setVariantValuePrivate( convertToVariant( value ), dynamicKeyPartList );
if ( checkValuePrivate( value ) )
return QgsSettingsEntryBase::setVariantValue( convertToVariant( value ), dynamicKeyPartList );
else
return false;
}

//! Converts the variant value to the value type of the setting
virtual T convertFromVariant( const QVariant &value ) const = 0;

//! Converts the value to a variant
virtual QVariant convertToVariant( T value ) const
virtual QVariant convertToVariant( const T &value ) const
{
return QVariant::fromValue( value );
}

//! Check if the value is valid
virtual bool checkValue( T value ) const
virtual bool checkValuePrivate( const T &value ) const
{
Q_UNUSED( value )
return true;
}
};




#endif // QGSSETTINGSENTRY_H

0 comments on commit 3534cd8

Please sign in to comment.