1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-02 08:59:28 +02:00

ChoiceSetting default is changeable and can be unspecified

This commit is contained in:
Paul Licameli 2019-02-23 14:37:57 -05:00
parent 1236c5bfed
commit bc08f571dc
3 changed files with 31 additions and 12 deletions

View File

@ -276,6 +276,14 @@ const wxArrayStringEx &EnumValueSymbols::GetInternals() const
} }
////////// //////////
const EnumValueSymbol &ChoiceSetting::Default() const
{
if ( mDefaultSymbol >= 0 && mDefaultSymbol < mSymbols.size() )
return mSymbols[ mDefaultSymbol ];
static EnumValueSymbol empty;
return empty;
}
wxString ChoiceSetting::Read() const wxString ChoiceSetting::Read() const
{ {
const auto &defaultValue = Default().Internal(); const auto &defaultValue = Default().Internal();
@ -321,7 +329,7 @@ bool ChoiceSetting::Write( const wxString &value )
EnumSetting::EnumSetting( EnumSetting::EnumSetting(
const wxString &key, const wxString &key,
EnumValueSymbols symbols, EnumValueSymbols symbols,
size_t defaultSymbol, long defaultSymbol,
std::vector<int> intValues, // must have same size as symbols std::vector<int> intValues, // must have same size as symbols
const wxString &oldKey const wxString &oldKey
@ -337,6 +345,14 @@ EnumSetting::EnumSetting(
} }
} }
void ChoiceSetting::SetDefault( long value )
{
if ( value < (long)mSymbols.size() )
mDefaultSymbol = value;
else
wxASSERT( false );
}
int EnumSetting::ReadInt() const int EnumSetting::ReadInt() const
{ {
auto index = Find( Read() ); auto index = Find( Read() );
@ -361,12 +377,14 @@ void EnumSetting::Migrate( wxString &value )
// Do not DELETE the old key -- let that be read if user downgrades // Do not DELETE the old key -- let that be read if user downgrades
// Audacity. But further changes will be stored only to the NEW key // Audacity. But further changes will be stored only to the NEW key
// and won't be seen then. // and won't be seen then.
auto index = FindInt( intValue ); auto index = (long) FindInt( intValue );
if ( index >= mSymbols.size() ) if ( index >= mSymbols.size() )
index = mDefaultSymbol; index = mDefaultSymbol;
value = mSymbols[index].Internal(); if ( index >= 0 && index < mSymbols.size() ) {
Write(value); value = mSymbols[index].Internal();
gPrefs->Flush(); Write(value);
gPrefs->Flush();
}
} }
} }

View File

@ -127,7 +127,7 @@ public:
ChoiceSetting( ChoiceSetting(
const wxString &key, const wxString &key,
EnumValueSymbols symbols, EnumValueSymbols symbols,
size_t defaultSymbol long defaultSymbol = -1
) )
: mKey{ key } : mKey{ key }
@ -135,17 +135,18 @@ public:
, mDefaultSymbol{ defaultSymbol } , mDefaultSymbol{ defaultSymbol }
{ {
wxASSERT( defaultSymbol < mSymbols.size() ); wxASSERT( defaultSymbol < (long)mSymbols.size() );
} }
const wxString &Key() const { return mKey; } const wxString &Key() const { return mKey; }
const EnumValueSymbol &Default() const const EnumValueSymbol &Default() const;
{ return mSymbols[mDefaultSymbol]; }
const EnumValueSymbols &GetSymbols() const { return mSymbols; } const EnumValueSymbols &GetSymbols() const { return mSymbols; }
wxString Read() const; wxString Read() const;
bool Write( const wxString &value ); // you flush gPrefs afterward bool Write( const wxString &value ); // you flush gPrefs afterward
void SetDefault( long value );
protected: protected:
size_t Find( const wxString &value ) const; size_t Find( const wxString &value ) const;
virtual void Migrate( wxString& ); virtual void Migrate( wxString& );
@ -157,7 +158,7 @@ protected:
// stores an internal value // stores an internal value
mutable bool mMigrated { false }; mutable bool mMigrated { false };
const size_t mDefaultSymbol; long mDefaultSymbol;
}; };
/// Extends ChoiceSetting with a corresponding table of integer codes /// Extends ChoiceSetting with a corresponding table of integer codes
@ -170,7 +171,7 @@ public:
EnumSetting( EnumSetting(
const wxString &key, const wxString &key,
EnumValueSymbols symbols, EnumValueSymbols symbols,
size_t defaultSymbol, long defaultSymbol,
std::vector<int> intValues, // must have same size as symbols std::vector<int> intValues, // must have same size as symbols
const wxString &oldKey const wxString &oldKey

View File

@ -59,7 +59,7 @@ public:
TracksViewModeEnumSetting( TracksViewModeEnumSetting(
const wxString &key, const wxString &key,
EnumValueSymbols symbols, EnumValueSymbols symbols,
size_t defaultSymbol, long defaultSymbol,
std::vector<int> intValues, std::vector<int> intValues,
const wxString &oldKey const wxString &oldKey