Skip to content
Snippets Groups Projects
Commit fe2ee93a authored by luyikei's avatar luyikei Committed by Hugo Beauzée-Luyssen
Browse files

Remove load/set( QJsonDocument ) in Settings


Since we don't have to pass QJsonDocument directly, we don't have to have those functions

Signed-off-by: default avatarHugo Beauzée-Luyssen <hugo@beauzee.fr>
parent 8a4f348c
No related branches found
No related tags found
No related merge requests found
......@@ -68,45 +68,6 @@ Settings::setSettingsFile(const QString &settingsFile)
m_settingsFile = nullptr;
}
bool
Settings::save( QJsonDocument& doc )
{
QReadLocker lock( &m_rwLock );
SettingMap::const_iterator it = m_settings.begin();
SettingMap::const_iterator end = m_settings.end();
QJsonObject top;
for ( ; it != end; ++it )
{
if ( ( (*it)->flags() & SettingValue::Runtime ) != 0 )
continue ;
if ( top.insert( (*it)->key(), QJsonValue::fromVariant( (*it)->get() ) ) == top.end() )
vlmcWarning() << "Failed to set:" << (*it)->key();
}
doc.setObject( top );
return true;
}
bool
Settings::load( const QJsonDocument& doc )
{
if ( doc.isNull() == true )
{
vlmcWarning() << "Invalid settings node";
return false;
}
for ( auto it = doc.object().constBegin();
it != doc.object().constEnd();
++it )
{
if ( setValue( it.key(), (*it).toVariant() ) == false )
vlmcWarning() << "Loaded invalid project setting:" << it.key();
}
return true;
}
bool
Settings::load()
{
......@@ -116,16 +77,28 @@ Settings::load()
return false;
}
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson( m_settingsFile->readAll(), &error );
m_jsonObject = QJsonDocument::fromJson( m_settingsFile->readAll(), &error ).object();
if ( error.error != QJsonParseError::NoError )
{
vlmcWarning() << "Failed to load settings file" << m_settingsFile->fileName();
vlmcWarning() << error.errorString();
return false;
}
bool res = load( doc );
for ( auto it = m_jsonObject.constBegin();
it != m_jsonObject.constEnd();
++it
)
{
if ( (*it).type() == QJsonValue::Object )
continue ;
if ( setValue( it.key(), (*it).toVariant() ) == false )
vlmcWarning() << "Loaded invalid project setting:" << it.key();
}
m_settingsFile->close();
return res;
return true;
}
bool
......@@ -134,7 +107,19 @@ Settings::save()
if ( m_settingsFile == nullptr )
return false;
QJsonDocument doc;
save( doc );
QReadLocker lock( &m_rwLock );
QJsonObject top;
for ( const auto& val : m_settings )
{
if ( ( val->flags() & SettingValue::Runtime ) != 0 )
continue ;
if ( top.insert( val->key(), QJsonValue::fromVariant( val->get() ) ) == top.end() )
vlmcWarning() << "Failed to set:" << val->key();
}
doc.setObject( top );
m_settingsFile->open( QFile::WriteOnly );
m_settingsFile->write( doc.toJson( QJsonDocument::Compact ) );
m_settingsFile->close();
......
......@@ -33,6 +33,7 @@
#include <QReadWriteLock>
#include <QVariant>
#include <QXmlStreamWriter>
#include <QJsonObject>
class SettingValue;
......@@ -113,14 +114,12 @@ class Settings
SettingList group( const QString &groupName ) const;
bool load();
bool save();
bool save( QJsonDocument& project );
void restoreDefaultValues();
void setSettingsFile( const QString& settingsFile );
bool load( const QJsonDocument& document );
private:
SettingMap m_settings;
QJsonObject m_jsonObject;
mutable QReadWriteLock m_rwLock;
QFile* m_settingsFile;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment