Skip to content

Commit

Permalink
API to migrate settings from old key
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Dec 5, 2022
1 parent 77dc2bd commit a77d626
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
20 changes: 20 additions & 0 deletions python/core/auto_generated/settings/qgssettingsentry.sip.in
Expand Up @@ -304,6 +304,24 @@ Returns the former value of the settings if it has been enabled in the options
Returns the current value (or default) if there is no former value.

.. versionadded:: 3.26
%End

bool migrateFromKey( const QString &oldKey, const QString &oldSection = QString(), const QString &dynamicKeyPart = QString() ) const;
%Docstring
Check if the settings does not exist and try to set it from a given ``oldKey`` and ``oldSection``

.. versionadded:: 3.30
%End

bool migrateFromKey( const QString &oldKey, const QString &oldSection, const QStringList &dynamicKeyPartList ) const;
%Docstring
Check if the settings does not exist and try to set it from a given ``oldKey``

.. note::

The old key must contain its section (the section from the current setting will not be prepended)

.. versionadded:: 3.30
%End

protected:
Expand Down Expand Up @@ -375,6 +393,7 @@ The ``defaultValueOverride`` argument if valid is used instead of the normal def
%Docstring
Returns the settings value with a ``defaultValueOverride`` and with an optional ``dynamicKeyPart``
%End

T valueWithDefaultOverride( const T &defaultValueOverride, const QStringList &dynamicKeyPartList ) const;
%Docstring
Returns the settings value with a ``defaultValueOverride`` for the ``dynamicKeyPartList``
Expand Down Expand Up @@ -505,6 +524,7 @@ The ``defaultValueOverride`` argument if valid is used instead of the normal def
%Docstring
Returns the settings value with a ``defaultValueOverride`` and with an optional ``dynamicKeyPart``
%End

T valueWithDefaultOverride( T defaultValueOverride, const QStringList &dynamicKeyPartList ) const;
%Docstring
Returns the settings value with a ``defaultValueOverride`` for the ``dynamicKeyPartList``
Expand Down
32 changes: 30 additions & 2 deletions src/core/settings/qgssettingsentry.cpp
Expand Up @@ -113,7 +113,12 @@ QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const

QString QgsSettingsEntryBase::key( const QStringList &dynamicKeyPartList ) const
{
QString completeKey = mKey;
return completeKeyPrivate( mKey, dynamicKeyPartList );
}

QString QgsSettingsEntryBase::completeKeyPrivate( const QString &key, const QStringList &dynamicKeyPartList ) const
{
QString completeKey = key;
if ( !mPluginName.isEmpty() )
{
if ( !completeKey.startsWith( '/' ) )
Expand Down Expand Up @@ -280,11 +285,34 @@ QVariant QgsSettingsEntryBase::formerValueAsVariant( const QStringList &dynamicK
{
Q_ASSERT( mOptions.testFlag( Qgis::SettingsOption::SaveFormerValue ) );
QVariant defaultValueOverride = valueAsVariant( key( dynamicKeyPartList ) );
return QgsSettings().value( formerValuekey( dynamicKeyPartList ), defaultValueOverride );
return QgsSettings().value( formerValuekey( dynamicKeyPartList ), defaultValueOverride );
}

bool QgsSettingsEntryBase::migrateFromKey( const QString &oldKey, const QString &oldSection, const QString &dynamicKeyPart ) const
{
return migrateFromKey( oldKey, oldSection, dynamicKeyPartToList( dynamicKeyPart ) );
}

bool QgsSettingsEntryBase::migrateFromKey( const QString &oldKey, const QString &oldSection, const QStringList &dynamicKeyPartList ) const
{
if ( exists( dynamicKeyPartList ) )
return false;

const QString oldCompleteKey = completeKeyPrivate( QStringLiteral( "%1/%2" ).arg( oldSection, oldKey ), dynamicKeyPartList );

if ( QgsSettings().contains( oldCompleteKey ) )
{
QVariant oldValue = QgsSettings().value( oldCompleteKey, mDefaultValue );
setVariantValuePrivate( oldValue );
return true;
}
return false;
}

QString QgsSettingsEntryBase::formerValuekey( const QStringList &dynamicKeyPartList ) const
{
return key( dynamicKeyPartList ) + QStringLiteral( "_formervalue" );
}



17 changes: 17 additions & 0 deletions src/core/settings/qgssettingsentry.h
Expand Up @@ -316,6 +316,19 @@ class CORE_EXPORT QgsSettingsEntryBase
*/
QVariant formerValueAsVariant( const QStringList &dynamicKeyPartList ) const;

/**
* Check if the settings does not exist and try to set it from a given \a oldKey and \a oldSection
* \since QGIS 3.30
*/
bool migrateFromKey( const QString &oldKey, const QString &oldSection = QString(), const QString &dynamicKeyPart = QString() ) const;

/**
* Check if the settings does not exist and try to set it from a given \a oldKey
* \note The old key must contain its section (the section from the current setting will not be prepended)
* \since QGIS 3.30
*/
bool migrateFromKey( const QString &oldKey, const QString &oldSection, const QStringList &dynamicKeyPartList ) const;

protected:

/**
Expand All @@ -328,6 +341,8 @@ class CORE_EXPORT QgsSettingsEntryBase
private:
QString formerValuekey( const QStringList &dynamicKeyPartList ) const;

QString completeKeyPrivate( const QString &key, const QStringList &dynamicKeyPartList ) const;

QString mKey;
QVariant mDefaultValue;
QString mDescription;
Expand Down Expand Up @@ -388,6 +403,7 @@ class QgsSettingsEntryByReference : public QgsSettingsEntryBase

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

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

Expand Down Expand Up @@ -529,6 +545,7 @@ class QgsSettingsEntryByValue : public QgsSettingsEntryBase

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

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

Expand Down

0 comments on commit a77d626

Please sign in to comment.