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

SettingsManager: Remove duplicated code for project/vlmc settings

parent e47aa89f
...@@ -31,98 +31,62 @@ ...@@ -31,98 +31,62 @@
#include <QDomElement> #include <QDomElement>
void bool
SettingsManager::setValue( const QString &key, SettingsManager::setValue( const QString &key,
const QVariant &value, const QVariant &value,
SettingsManager::Type type ) SettingsManager::Type type )
{ {
if ( type == Project ) if ( type == Project )
{ {
SettingMap::iterator it = m_xmlSettings.find( key ); if ( m_xmlSettings.setValue( key, value ) )
if ( it != m_xmlSettings.end() ) return true;
{
(*it)->set( value );
return ;
}
} }
else// ( type == Vlmc && m_classicSettings.contains( key) == true ) else// ( type == Vlmc && m_classicSettings.contains( key) == true )
{ {
SettingMap::iterator it = m_classicSettings.find( key ); SettingValue* v = m_classicSettings.value( key );
if ( it != m_classicSettings.end() ) if ( v != NULL )
{ {
SettingValue* v = (*it);
// We don't want private values in our QSettings, that would be // 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 // 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 // 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. // (logging level) We might want to split this in Private & Runtime at some point.
v->set( value ); v->set( value );
if ( v->flags().testFlag( SettingValue::Private ) ) if ( v->flags().testFlag( SettingValue::Private ) )
return; return true;
QSettings sett; QSettings sett;
sett.setValue( key, value ); sett.setValue( key, value );
sett.sync(); sett.sync();
return ; return true;
} }
} }
vlmcWarning() << "Setting" << key << "does not exist."; vlmcWarning() << "Setting" << key << "does not exist.";
Q_ASSERT_X( false, __FILE__, "set value without a created variable" ); Q_ASSERT_X( false, __FILE__, "set value without a created variable" );
return false;
} }
SettingValue* SettingValue*
SettingsManager::value( const QString &key, SettingsManager::value( const QString &key,
SettingsManager::Type type ) SettingsManager::Type type )
{ {
QReadLocker rl( &m_rwLock ); SettingValue* result = NULL;
if ( type == Project ) if ( type == Project )
{ result = m_xmlSettings.value( key );
SettingMap::iterator it = m_xmlSettings.find( key );
if ( it != m_xmlSettings.end() )
return (*it);
}
else else
{ result = m_classicSettings.value( key );
SettingMap::iterator it = m_classicSettings.find( key ); Q_ASSERT_X( result != NULL, __FILE__, "get value without a created variable" );
if ( it != m_classicSettings.end() ) return result;
return (*it);
}
vlmcWarning() << "Setting" << key << "does not exist.";
Q_ASSERT_X( false, __FILE__, "get value without a created variable" );
return NULL;
} }
SettingsManager::SettingList SettingsManager::SettingList
SettingsManager::group( const QString &groupName, SettingsManager::Type type ) SettingsManager::group( const QString &groupName, SettingsManager::Type type )
{ {
SettingsManager::SettingList ret;
QReadLocker rl( &m_rwLock );
QString grp = groupName + '/';
if ( type == Project ) if ( type == Project )
{ return m_xmlSettings.group( groupName );
SettingMap::const_iterator it = m_xmlSettings.begin();
SettingMap::const_iterator ed = m_xmlSettings.end();
for ( ; it != ed; ++it )
{
if ( (*it)->key().startsWith( grp ) )
ret.push_back( *it );
}
}
else if ( type == Vlmc ) else if ( type == Vlmc )
{ return m_classicSettings.group( groupName );
SettingMap::const_iterator it = m_classicSettings.begin(); Q_ASSERT_X( false, __FILE__, "Unknown setting type" );
SettingMap::const_iterator ed = m_classicSettings.end(); return SettingList();
for ( ; it != ed; ++it )
{
if ( (*it)->key().startsWith( grp ) )
ret.push_back( *it );
}
}
return ret;
} }
bool bool
...@@ -132,27 +96,12 @@ SettingsManager::watchValue( const QString &key, ...@@ -132,27 +96,12 @@ SettingsManager::watchValue( const QString &key,
SettingsManager::Type type, SettingsManager::Type type,
Qt::ConnectionType cType ) Qt::ConnectionType cType )
{ {
QReadLocker rl( &m_rwLock ); SettingValue* s = value( key, type );
if ( s != NULL )
if ( type == Project )
{
SettingMap::iterator it = m_xmlSettings.find( key );
if ( it != m_xmlSettings.end() )
{
connect( (*it), SIGNAL( changed( const QVariant& ) ),
receiver, method );
return true;
}
}
else if ( type == Vlmc )
{ {
SettingMap::iterator it = m_classicSettings.find( key ); connect( s, SIGNAL( changed( const QVariant& ) ),
if ( it != m_classicSettings.end() ) receiver, method , cType );
{ return true;
connect( (*it), SIGNAL( changed( const QVariant& ) ),
receiver, method, cType );
return true;
}
} }
Q_ASSERT_X( false, __FILE__, "watching value without a created variable" ); Q_ASSERT_X( false, __FILE__, "watching value without a created variable" );
return false; return false;
...@@ -161,10 +110,11 @@ SettingsManager::watchValue( const QString &key, ...@@ -161,10 +110,11 @@ SettingsManager::watchValue( const QString &key,
void void
SettingsManager::save() const SettingsManager::save() const
{ {
QReadLocker rl( &m_rwLock ); m_classicSettings.lockForRead();
QSettings sett; QSettings sett;
SettingMap::const_iterator it = m_classicSettings.begin(); SettingsContainer::SettingMap::const_iterator it = m_classicSettings.settings().begin();
SettingMap::const_iterator ed = m_classicSettings.end(); SettingsContainer::SettingMap::const_iterator ed = m_classicSettings.settings().end();
for ( ; it != ed; ++it ) for ( ; it != ed; ++it )
{ {
...@@ -178,8 +128,10 @@ SettingsManager::save() const ...@@ -178,8 +128,10 @@ SettingsManager::save() const
void void
SettingsManager::save( QXmlStreamWriter& project ) const SettingsManager::save( QXmlStreamWriter& project ) const
{ {
SettingMap::const_iterator it = m_xmlSettings.begin(); m_xmlSettings.lockForRead();
SettingMap::const_iterator end = m_xmlSettings.end();
SettingsContainer::SettingMap::const_iterator it = m_xmlSettings.settings().begin();
SettingsContainer::SettingMap::const_iterator end = m_xmlSettings.settings().end();
project.writeStartElement( "project" ); project.writeStartElement( "project" );
for ( ; it != end; ++it ) for ( ; it != end; ++it )
...@@ -204,7 +156,6 @@ SettingsManager::load( const QDomElement &root ) ...@@ -204,7 +156,6 @@ SettingsManager::load( const QDomElement &root )
vlmcWarning() << "Invalid settings node"; vlmcWarning() << "Invalid settings node";
return false ; return false ;
} }
QWriteLocker wLock( &m_rwLock );
QDomElement s = element.firstChildElement(); QDomElement s = element.firstChildElement();
while ( s.isNull() == false ) while ( s.isNull() == false )
{ {
...@@ -214,12 +165,8 @@ SettingsManager::load( const QDomElement &root ) ...@@ -214,12 +165,8 @@ SettingsManager::load( const QDomElement &root )
if ( key.isEmpty() == true || value.isEmpty() == true ) if ( key.isEmpty() == true || value.isEmpty() == true )
vlmcWarning() << "Invalid setting node."; vlmcWarning() << "Invalid setting node.";
else else
{ if ( setValue( key, value, SettingsManager::Project ) == false )
if ( m_xmlSettings.contains( key ) == true ) vlmcWarning() << "Loaded invalid project setting:" << key;
setValue( key, value, SettingsManager::Project );
else
vlmcWarning() << "Invalid setting node in project file:" << key;
}
s = s.nextSiblingElement(); s = s.nextSiblingElement();
} }
return true; return true;
...@@ -231,20 +178,82 @@ SettingsManager::createVar( SettingValue::Type type, const QString &key, ...@@ -231,20 +178,82 @@ SettingsManager::createVar( SettingValue::Type type, const QString &key,
const char *desc, SettingsManager::Type varType /*= Vlmc*/, const char *desc, SettingsManager::Type varType /*= Vlmc*/,
SettingValue::Flags flags /*= SettingValue::Nothing*/ ) SettingValue::Flags flags /*= SettingValue::Nothing*/ )
{ {
QWriteLocker wlock( &m_rwLock );
SettingValue *val = NULL; SettingValue *val = NULL;
if ( varType == Vlmc && m_classicSettings.contains( key ) == false ) if ( varType == Vlmc )
val = m_classicSettings.createVar( type, key, defaultValue, name, desc, flags );
else if ( varType == Project )
val = m_xmlSettings.createVar( type, key, defaultValue, name, desc, flags );
Q_ASSERT_X( val != NULL, __FILE__, "creating an already created variable" );
return val;
}
bool
SettingsManager::SettingsContainer::setValue(const QString &key, const QVariant &value)
{
SettingMap::iterator it = m_settings.find( key );
if ( it != m_settings.end() )
{ {
val = new SettingValue( key, type, defaultValue, name, desc, flags ); (*it)->set( value );
m_classicSettings.insert( key, val ); return true;
} }
else if ( varType == Project && m_xmlSettings.contains( key ) == false ) return false;
}
SettingValue*
SettingsManager::SettingsContainer::value(const QString &key)
{
QReadLocker lock( &m_rwLock );
SettingMap::iterator it = m_settings.find( key );
if ( it != m_settings.end() )
return *it;
return NULL;
}
SettingValue*
SettingsManager::SettingsContainer::createVar(SettingValue::Type type, const QString &key, const QVariant &defaultValue, const char *name, const char *desc, SettingValue::Flags flags)
{
QWriteLocker lock( &m_rwLock );
if ( m_settings.contains( key ) )
return NULL;
SettingValue* val = new SettingValue( key, type, defaultValue, name, desc, flags );
m_settings.insert( key, val );
return val;
}
const SettingsManager::SettingsContainer::SettingMap&
SettingsManager::SettingsContainer::settings() const
{
return m_settings;
}
SettingsManager::SettingList
SettingsManager::SettingsContainer::group(const QString &groupName) const
{
QReadLocker lock( &m_rwLock );
SettingMap::const_iterator it = m_settings.begin();
SettingMap::const_iterator ed = m_settings.end();
SettingsManager::SettingList ret;
QString grp = groupName + '/';
for ( ; it != ed; ++it )
{ {
val = new SettingValue( key, type, defaultValue, name, desc, flags ); if ( (*it)->key().startsWith( grp ) )
m_xmlSettings.insert( key, val ); ret.push_back( *it );
} }
else return ret;
Q_ASSERT_X( false, __FILE__, "creating an already created variable" ); }
return val;
void
SettingsManager::SettingsContainer::lockForRead() const
{
m_rwLock.lockForRead();
}
void
SettingsManager::SettingsContainer::unlock() const
{
m_rwLock.unlock();
} }
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
#include "Singleton.hpp" #include "Singleton.hpp"
#include <QString> #include <QString>
#include <QHash> #include <QMap>
#include <QObject> #include <QObject>
#include <QReadWriteLock> #include <QReadWriteLock>
#include <QVariant> #include <QVariant>
...@@ -101,17 +101,36 @@ SettingsManager::getInstance()->createVar( type, key, defaultValue, name, \ ...@@ -101,17 +101,36 @@ SettingsManager::getInstance()->createVar( type, key, defaultValue, name, \
class SettingsManager : public QObject, public Singleton<SettingsManager> class SettingsManager : public QObject, public Singleton<SettingsManager>
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY( SettingsManager ) Q_DISABLE_COPY( SettingsManager );
public:
typedef QList<SettingValue*> SettingList;
private:
class SettingsContainer
{
public:
typedef QMap<QString, SettingValue*> SettingMap;
bool setValue(const QString &key, const QVariant &value );
SettingValue* value( const QString &key );
SettingValue* createVar( SettingValue::Type type, const QString &key, const QVariant &defaultValue, const char *name, const char *desc, SettingValue::Flags flags );
const SettingMap& settings() const;
SettingList group( const QString &groupName ) const;
void lockForRead() const;
void unlock() const;
private:
SettingMap m_settings;
mutable QReadWriteLock m_rwLock;
};
public: public:
enum Type enum Type
{ {
Project, Project,
Vlmc Vlmc
}; };
bool setValue( const QString &key,
typedef QList<SettingValue*> SettingList;
void setValue( const QString &key,
const QVariant &value, const QVariant &value,
SettingsManager::Type type = Vlmc); SettingsManager::Type type = Vlmc);
SettingValue *value( const QString &key, SettingValue *value( const QString &key,
...@@ -134,15 +153,14 @@ class SettingsManager : public QObject, public Singleton<SettingsManager> ...@@ -134,15 +153,14 @@ class SettingsManager : public QObject, public Singleton<SettingsManager>
bool load( const QDomElement &element ); bool load( const QDomElement &element );
private: private:
typedef QMap<QString, SettingValue*> SettingMap;
friend class Singleton<SettingsManager>; friend class Singleton<SettingsManager>;
SettingsManager(){} SettingsManager(){}
~SettingsManager(){} ~SettingsManager(){}
SettingMap m_classicSettings; SettingsContainer m_classicSettings;
SettingMap m_xmlSettings; SettingsContainer m_xmlSettings;
mutable QReadWriteLock m_rwLock;
}; };
#endif #endif
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