Commit 609e52a3 authored by Hugo Beauzee-Luyssen's avatar Hugo Beauzee-Luyssen

Added a SettingValue class.

So we can watch for configuration changes without polling the
configmanager.
parent b7078463
/*****************************************************************************
* SettingValue.cpp: A setting value that can broadcast its changes
*****************************************************************************
* Copyright (C) 2008-2009 the VLMC team
*
* Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "SettingValue.h"
SettingValue::SettingValue( const QVariant& val ) : m_val ( val )
{
}
void SettingValue::set( const QVariant& val )
{
if ( val != m_val )
{
m_val = val;
emit changed( m_val );
}
}
const QVariant& SettingValue::get() const
{
return m_val;
}
QVariant& SettingValue::get()
{
return m_val;
}
/*****************************************************************************
* SettingValue.h: A setting value that can broadcast its changes
*****************************************************************************
* Copyright (C) 2008-2009 the VLMC team
*
* Authors: Hugo Beauzee-Luyssen <hugo@vlmc.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef SETTINGVALUE_H
#define SETTINGVALUE_H
#include <QObject>
#include <QVariant>
class SettingValue : public QObject
{
Q_OBJECT
Q_DISABLE_COPY( SettingValue );
public:
SettingValue( const QVariant& val );
void set( const QVariant& val );
const QVariant& get() const;
QVariant& get();
private:
QVariant m_val;
signals:
void changed( const QVariant& );
};
#endif // SETTINGVALUE_H
......@@ -44,21 +44,21 @@ SettingsManager::~SettingsManager()
{
}
void SettingsManager::setValues( const QString& part, QHash<QString, QVariant> values )
{
if ( !m_tempData.contains( part ) )
addNewSettingsPart( part );
m_globalLock.lockForRead();
SettingsPart* sett = m_tempData[part];
m_globalLock.unlock();
QHash<QString, QVariant>::iterator it = values.begin();
QHash<QString, QVariant>::iterator end = values.end();
QWriteLocker lock( &sett->m_lock );
for ( ; it != end; ++it )
sett->m_data.insert( it.key(), it.value() );
return ;
}
//void SettingsManager::setValues( const QString& part, SettingsPart::ConfigPair values )
//{
// if ( !m_tempData.contains( part ) )
// addNewSettingsPart( part );
// m_globalLock.lockForRead();
// SettingsPart* sett = m_tempData[part];
// m_globalLock.unlock();
// SettingsPart::ConfigPair::iterator it = values.begin();
// SettingsPart::ConfigPair::iterator end = values.end();
//
// QWriteLocker lock( &sett->m_lock );
// for ( ; it != end; ++it )
// sett->m_data.insert( it.key(), it.value() );
// return ;
//}
void SettingsManager::setValue( const QString& part , const QString& key, const QVariant& value )
{
......@@ -70,7 +70,11 @@ void SettingsManager::setValue( const QString& part , const QString& key, const
m_globalLock.unlock();
QWriteLocker lock( &m_globalLock );
SettingsPart* tmp = m_tempData[part];
tmp->m_data.insert( key, value );
SettingsPart::ConfigPair::iterator it = tmp->m_data.find( key );
if ( it == tmp->m_data.end() )
tmp->m_data[key] = new SettingValue( value );
else
it.value()->set( value );
return ;
}
......@@ -80,7 +84,7 @@ const QVariant& SettingsManager::getValue( const QString& part, const QString&
return getValue( "default", key );
QReadLocker readLock( &m_globalLock );
QReadLocker rdLock( &m_data[part]->m_lock );
QVariant& value = m_data[part]->m_data[key];
QVariant& value = m_data[part]->m_data[key]->get();
return value;
}
......@@ -97,13 +101,13 @@ void SettingsManager::saveSettings( const QString& part, QDomDocument& xmlfile,
m_globalLock.unlock();
//SAVE SETTINGS TO DomDocument
QReadLocker lock( &sett->m_lock );
QHash<QString, QVariant>::iterator it = sett->m_data.begin();
QHash<QString, QVariant>::iterator end = sett->m_data.end();
SettingsPart::ConfigPair::const_iterator it = sett->m_data.begin();
SettingsPart::ConfigPair::const_iterator end = sett->m_data.end();
QDomElement settingsNode = xmlfile.createElement( part );
for ( ; it != end; ++it )
{
QDomElement elem = xmlfile.createElement( it.key() );
elem.setAttribute( "value", it.value().toString() );
elem.setAttribute( "value", it.value()->get().toString() );
settingsNode.appendChild( elem );
}
......@@ -140,7 +144,7 @@ void SettingsManager::loadSettings( const QString& part, const QDomElement& set
return ;
}
sett->m_data.insert( list.at( idx ).toElement().tagName(),
QVariant( attrMap.item( 0 ).nodeValue() ));
new SettingValue( QVariant( attrMap.item( 0 ).nodeValue() ) ) );
}
sett->m_lock.unlock();
m_globalLock.unlock();
......@@ -171,11 +175,17 @@ void SettingsManager::commit()
SettingsPart* sett = it.value();
QReadLocker rLock( &sett->m_lock );
QHash<QString, QVariant>::iterator iter = sett->m_data.begin();
QHash<QString, QVariant>::iterator ed = sett->m_data.end();
SettingsPart::ConfigPair::iterator iter = sett->m_data.begin();
SettingsPart::ConfigPair::iterator end = sett->m_data.end();
QWriteLocker wLock( &m_data[it.key()]->m_lock );
for ( ; iter != ed; ++iter )
m_data[it.key()]->m_data.insert( iter.key(), iter.value() );
for ( ; iter != end; ++iter )
{
SettingsPart::ConfigPair::iterator insert_it = m_data[it.key()]->m_data.find( iter.key() );
if ( insert_it == m_data[it.key()]->m_data.end() )
m_data[it.key()]->m_data.insert( iter.key(), new SettingValue( iter.value()->get() ) );
else
m_data[it.key()]->m_data[ iter.key() ]->set( iter.value()->get() );
}
}
lock.unlock();
flush();
......
......@@ -31,11 +31,12 @@
#include <QVariant>
#include <QDomDocument>
#include "SettingValue.h"
#include "QSingleton.hpp"
struct SettingsPart
{
typedef QHash<QString, QVariant> ConfigPair;
typedef QHash<QString, SettingValue*> ConfigPair;
SettingsPart() {}
ConfigPair m_data;
......@@ -55,7 +56,7 @@ class SettingsManager : public QObject, public QSingleton<SettingsManager>
friend class QSingleton<SettingsManager>;
public:
void setValues( const QString& part, QHash<QString, QVariant> );
// void setValues( const QString& part, SettingsPart::ConfigPair );
void setValue( const QString& part, const QString& key, const QVariant& value );
const QVariant& getValue( const QString& part, const QString& key ) const;
const SettingsPart* getConfigPart( const QString& part ) const;
......
HEADERS += ProjectSettingsDefault.h \
SettingsManager.h \
VLMCSettingsDefault.h
SOURCES += ProjectSettingsDefault.cpp \
SettingsManager.cpp \
VLMCSettingsDefault.cpp
HEADERS += ProjectSettingsDefault.h \
SettingsManager.h \
VLMCSettingsDefault.h \
SettingValue.h
SOURCES += ProjectSettingsDefault.cpp \
SettingsManager.cpp \
VLMCSettingsDefault.cpp \
SettingValue.cpp
......@@ -37,7 +37,7 @@ KeyboardShortcut::KeyboardShortcut( QWidget* parent ) :
SettingsPart::ConfigPair::const_iterator ite = parts->m_data.end();
while ( it != ite )
{
m_keySeq[it.key()] = new QKeySequence( it.value().toString() );
m_keySeq[it.key()] = new QKeySequence( it.value()->get().toString() );
m_layout->addRow( it.key(), new KeyboardShortcutInput( m_keySeq[it.key()]->toString(), this ) );
++it;
}
......
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