mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-17 00:09:36 -04:00
API to migrate settings from old key
This commit is contained in:
parent
77dc2bdde2
commit
a77d626991
@ -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:
|
||||
@ -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``
|
||||
@ -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``
|
||||
|
@ -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( '/' ) )
|
||||
@ -280,7 +285,28 @@ 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
|
||||
@ -288,3 +314,5 @@ QString QgsSettingsEntryBase::formerValuekey( const QStringList &dynamicKeyPartL
|
||||
return key( dynamicKeyPartList ) + QStringLiteral( "_formervalue" );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -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:
|
||||
|
||||
/**
|
||||
@ -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;
|
||||
@ -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 ) );}
|
||||
|
||||
@ -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 ) );}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user