Commit 29db3abb authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

SettingsManager: A list that contained Pair ordered by key... let's call it a map.

parent 93675a6c
......@@ -58,7 +58,9 @@ EffectInstance::settingValueFactory( Effect::Parameter *info, quint32 index )
if ( info->type == F0R_PARAM_DOUBLE )
flags = SettingValue::Clamped;
EffectSettingValue *val = new EffectSettingValue( EffectSettingValue::frei0rToVlmc( info->type ),
//FIXME: Do something smart about the key here, it feels we're not saving
// the settings value
EffectSettingValue *val = new EffectSettingValue( info->name, EffectSettingValue::frei0rToVlmc( info->type ),
this, index, info->name, info->desc, flags );
if ( info->type == F0R_PARAM_DOUBLE )
val->setLimits( 0.0, 1.0 );
......
......@@ -28,9 +28,9 @@
#include <QColor>
#include <QPoint>
EffectSettingValue::EffectSettingValue( Type type, EffectInstance* instance, quint32 index,
EffectSettingValue::EffectSettingValue( const QString& key, Type type, EffectInstance* instance, quint32 index,
const char *name, const char *desc, Flags flags ) :
SettingValue( type, QVariant(), name, desc, flags ),
SettingValue( key, type, QVariant(), name, desc, flags ),
m_paramBuff( NULL ),
m_buffSize( 0 ),
m_effectInstance( instance ),
......
......@@ -33,7 +33,7 @@ class EffectSettingValue : public SettingValue
Q_OBJECT
public:
EffectSettingValue( Type type, EffectInstance* instance, quint32 index,
EffectSettingValue(const QString &key, Type type, EffectInstance* instance, quint32 index,
const char* name, const char* desc, Flags flags = Nothing );
virtual ~EffectSettingValue();
......
......@@ -52,9 +52,8 @@ PreferenceWidget::PreferenceWidget( const QString &name, const char *label, Sett
QFormLayout *layout = new QFormLayout( container );
layout->setFieldGrowthPolicy( QFormLayout::AllNonFixedFieldsGrow );
foreach ( const SettingsManager::Pair p, settings )
foreach ( SettingValue* s, settings )
{
SettingValue *s = p.value;
//Do not display private variables
if ( ( s->flags() & SettingValue::Private ) != 0 )
continue ;
......
......@@ -22,10 +22,11 @@
#include "SettingValue.h"
SettingValue::SettingValue( SettingValue::Type type, const QVariant& defaultValue,
SettingValue::SettingValue( const QString& key, SettingValue::Type type, const QVariant& defaultValue,
const char* name, const char* desc, SettingValue::Flags flags ) :
m_val( defaultValue ),
m_defaultVal( defaultValue ),
m_key( key ),
m_name( name ),
m_desc( desc ),
m_type( type ),
......@@ -71,6 +72,12 @@ SettingValue::restoreDefault()
set( m_defaultVal );
}
const QString&
SettingValue::key() const
{
return m_key;
}
const char*
SettingValue::name() const
{
......
......@@ -58,17 +58,22 @@ class SettingValue : public QObject
Password = 1 << 1,
Clamped = 1 << 2, ///< When used, the m_min and m_max will be used
EightMultiple = 1 << 3, ///< Forces the value to be a multiple of 8
NotEmpty = 1 << 4, ///< Forces the value not to be empty (likely to be user only with Strings)
NotEmpty = 1 << 4, ///< Forces the value not to be empty (likely to be used only with Strings)
};
Q_DECLARE_FLAGS( Flags, Flag );
/**
* \brief Constructs a setting value with its default value and description
*
* \param key The parameter key - ie. the value used
* to fetch this setting in SettingsManager
* \param name The name of this setting. Not his key.
* This is used when generating the settings GUI
* and will be translated.
* \param defaultValue The setting default value.
* \param desc The setting description
*/
SettingValue( Type type, const QVariant& defaultValue, const char* name,
SettingValue( const QString& key, Type type, const QVariant& defaultValue, const char* name,
const char* desc, Flags flags = Nothing );
/**
......@@ -91,6 +96,18 @@ class SettingValue : public QObject
*/
void restoreDefault();
/**
* @brief key Returns the key for this setting
*
* @warning This is NOT the name to be used when generating settings GUI
*/
const QString& key() const;
/**
* @brief name The name of this setting.
*
* @warning This is NOT the key to be used when fetching the setting.
*/
const char *name() const;
Type type() const;
Flags flags() const;
......@@ -105,6 +122,7 @@ class SettingValue : public QObject
*/
QVariant m_val;
QVariant m_defaultVal;
const QString m_key;
const char* m_name;
const char* m_desc;
Type m_type;
......
......@@ -31,36 +31,6 @@
#include <QDomElement>
static SettingsManager::SettingList::iterator
getPair( SettingsManager::SettingList &list, const QString &key )
{
SettingsManager::SettingList::iterator it = list.begin();
SettingsManager::SettingList::iterator end = list.end();
while ( it != end )
{
if ( *it == key )
return it;
++it;
}
return end;
}
static bool
contains( const SettingsManager::SettingList &list, const QString &key )
{
SettingsManager::SettingList::const_iterator it = list.constBegin();
SettingsManager::SettingList::const_iterator end = list.constEnd();
while ( it != end )
{
if ( *it == key )
return true;
++it;
}
return false;
}
void
SettingsManager::setValue( const QString &key,
const QVariant &value,
......@@ -68,22 +38,24 @@ SettingsManager::setValue( const QString &key,
{
if ( type == Project )
{
SettingList::iterator it = getPair( m_xmlSettings, key );
SettingMap::iterator it = m_xmlSettings.find( key );
if ( it != m_xmlSettings.end() )
{
(*it).value->set( value );
(*it)->set( value );
return ;
}
}
else// ( type == Vlmc && m_classicSettings.contains( key) == true )
{
SettingList::iterator it = getPair( m_classicSettings, key );
SettingMap::iterator it = m_classicSettings.find( key );
if ( it != m_classicSettings.end() )
{
SettingValue* v = (*it).value;
SettingValue* v = (*it);
// We don't want private values in our QSettings, that would be
// saved in the preference files, and they're called private for a reason
// FIXME: For now we have only one private variable which is for runtime stuff
// (logging level) We might want to split this in Private & Runtime at some point.
v->set( value );
if ( v->flags().testFlag( SettingValue::Private ) )
return;
......@@ -106,15 +78,15 @@ SettingsManager::value( const QString &key,
if ( type == Project )
{
SettingList::iterator it = getPair( m_xmlSettings, key );
SettingMap::iterator it = m_xmlSettings.find( key );
if ( it != m_xmlSettings.end() )
return (*it).value;
return (*it);
}
else
{
SettingList::iterator it = getPair( m_classicSettings, key );
SettingMap::iterator it = m_classicSettings.find( key );
if ( it != m_classicSettings.end() )
return (*it).value;
return (*it);
}
vlmcWarning() << "Setting" << key << "does not exist.";
Q_ASSERT_X( false, __FILE__, "get value without a created variable" );
......@@ -130,23 +102,23 @@ SettingsManager::group( const QString &groupName, SettingsManager::Type type )
QString grp = groupName + '/';
if ( type == Project )
{
SettingList::const_iterator it = m_xmlSettings.begin();
SettingList::const_iterator ed = m_xmlSettings.end();
SettingMap::const_iterator it = m_xmlSettings.begin();
SettingMap::const_iterator ed = m_xmlSettings.end();
for ( ; it != ed; ++it )
{
if ( (*it).key.startsWith( grp ) )
if ( (*it)->key().startsWith( grp ) )
ret.push_back( *it );
}
}
else if ( type == Vlmc )
{
SettingList::const_iterator it = m_classicSettings.begin();
SettingList::const_iterator ed = m_classicSettings.end();
SettingMap::const_iterator it = m_classicSettings.begin();
SettingMap::const_iterator ed = m_classicSettings.end();
for ( ; it != ed; ++it )
{
if ( (*it).key.startsWith( grp ) )
if ( (*it)->key().startsWith( grp ) )
ret.push_back( *it );
}
}
......@@ -164,20 +136,20 @@ SettingsManager::watchValue( const QString &key,
if ( type == Project )
{
SettingList::iterator it = getPair( m_xmlSettings, key );
SettingMap::iterator it = m_xmlSettings.find( key );
if ( it != m_xmlSettings.end() )
{
connect( (*it).value, SIGNAL( changed( const QVariant& ) ),
connect( (*it), SIGNAL( changed( const QVariant& ) ),
receiver, method );
return true;
}
}
else if ( type == Vlmc )
{
SettingList::iterator it = getPair( m_classicSettings, key );
SettingMap::iterator it = m_classicSettings.find( key );
if ( it != m_classicSettings.end() )
{
connect( (*it).value, SIGNAL( changed( const QVariant& ) ),
connect( (*it), SIGNAL( changed( const QVariant& ) ),
receiver, method, cType );
return true;
}
......@@ -191,14 +163,14 @@ SettingsManager::save() const
{
QReadLocker rl( &m_rwLock );
QSettings sett;
SettingList::const_iterator it = m_classicSettings.begin();
SettingList::const_iterator ed = m_classicSettings.end();
SettingMap::const_iterator it = m_classicSettings.begin();
SettingMap::const_iterator ed = m_classicSettings.end();
for ( ; it != ed; ++it )
{
if ( ( (*it).value->flags() & SettingValue::Private ) != 0 )
if ( ( (*it)->flags() & SettingValue::Private ) != 0 )
continue ;
sett.setValue( (*it).key, (*it).value->get() );
sett.setValue( (*it)->key(), (*it)->get() );
}
sett.sync();
}
......@@ -206,17 +178,17 @@ SettingsManager::save() const
void
SettingsManager::save( QXmlStreamWriter& project ) const
{
SettingList::const_iterator it = m_xmlSettings.begin();
SettingList::const_iterator end = m_xmlSettings.end();
SettingMap::const_iterator it = m_xmlSettings.begin();
SettingMap::const_iterator end = m_xmlSettings.end();
project.writeStartElement( "project" );
for ( ; it != end; ++it )
{
if ( ( (*it).value->flags() & SettingValue::Private ) != 0 )
if ( ( (*it)->flags() & SettingValue::Private ) != 0 )
continue ;
project.writeStartElement( "property" );
project.writeAttribute( "key", (*it).key );
project.writeAttribute( "value", (*it).value->get().toString() );
project.writeAttribute( "key", (*it)->key() );
project.writeAttribute( "value", (*it)->get().toString() );
project.writeEndElement();
}
project.writeEndElement();
......@@ -243,7 +215,7 @@ SettingsManager::load( const QDomElement &root )
vlmcWarning() << "Invalid setting node.";
else
{
if ( contains( m_xmlSettings, key ) == true )
if ( m_xmlSettings.contains( key ) == true )
setValue( key, value, SettingsManager::Project );
else
vlmcWarning() << "Invalid setting node in project file:" << key;
......@@ -262,29 +234,17 @@ SettingsManager::createVar( SettingValue::Type type, const QString &key,
QWriteLocker wlock( &m_rwLock );
SettingValue *val = NULL;
if ( varType == Vlmc && getPair( m_classicSettings, key ) == m_classicSettings.end() )
if ( varType == Vlmc && m_classicSettings.contains( key ) == false )
{
val = new SettingValue( type, defaultValue, name, desc, flags );
m_classicSettings.push_back( Pair( key, val ) );
val = new SettingValue( key, type, defaultValue, name, desc, flags );
m_classicSettings.insert( key, val );
}
else if ( varType == Project && getPair( m_xmlSettings, key ) == m_xmlSettings.end() )
else if ( varType == Project && m_xmlSettings.contains( key ) == false )
{
val = new SettingValue( type, defaultValue, name, desc, flags );
m_xmlSettings.push_back( Pair( key, val ) );
val = new SettingValue( key, type, defaultValue, name, desc, flags );
m_xmlSettings.insert( key, val );
}
else
Q_ASSERT_X( false, __FILE__, "creating an already created variable" );
return val;
}
SettingsManager::Pair::Pair( const QString &_key, SettingValue *_value ) :
key( _key ),
value( _value )
{
}
bool
SettingsManager::Pair::operator ==( const QString &_key ) const
{
return key == _key;
}
......@@ -109,15 +109,7 @@ class SettingsManager : public QObject, public Singleton<SettingsManager>
Vlmc
};
struct Pair
{
Pair( const QString& key, SettingValue *value );
bool operator==( const QString& key ) const;
QString key;
SettingValue *value;
};
//We store this as a list to preserve order.
typedef QList<Pair> SettingList;
typedef QList<SettingValue*> SettingList;
void setValue( const QString &key,
const QVariant &value,
......@@ -142,12 +134,13 @@ class SettingsManager : public QObject, public Singleton<SettingsManager>
bool load( const QDomElement &element );
private:
typedef QMap<QString, SettingValue*> SettingMap;
friend class Singleton<SettingsManager>;
SettingsManager(){}
~SettingsManager(){}
SettingList m_classicSettings;
SettingList m_xmlSettings;
SettingMap m_classicSettings;
SettingMap m_xmlSettings;
mutable QReadWriteLock m_rwLock;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment