[vlmc-devel] Settings: SettingsChildren

Yikai Lu git at videolan.org
Sat Apr 9 22:18:01 CEST 2016


vlmc | branch: master | Yikai Lu <luyikei.qmltu at gmail.com> | Sat Apr  9 11:44:00 2016 +0900| [c880a85e8d4c3eaefe3b2695540e3b1159f0ba72] | committer: Hugo Beauzée-Luyssen

Settings: SettingsChildren

Make able to save child settings with their name

Signed-off-by: Hugo Beauzée-Luyssen <hugo at beauzee.fr>

> https://code.videolan.org/videolan/vlmc/commit/c880a85e8d4c3eaefe3b2695540e3b1159f0ba72
---

 src/Settings/Settings.cpp | 92 ++++++++++++++++++++++++++++++++++++-----------
 src/Settings/Settings.h   |  9 +++++
 2 files changed, 80 insertions(+), 21 deletions(-)

diff --git a/src/Settings/Settings.cpp b/src/Settings/Settings.cpp
index 06aca75..6d0a0c0 100644
--- a/src/Settings/Settings.cpp
+++ b/src/Settings/Settings.cpp
@@ -68,33 +68,38 @@ Settings::setSettingsFile(const QString &settingsFile)
         m_settingsFile = nullptr;
 }
 
-bool
-Settings::load()
+QJsonDocument
+Settings::readSettingsFromFile()
 {
     if ( m_settingsFile->open( QFile::ReadOnly ) == false )
     {
         vlmcWarning() << "Failed to open settings file" << m_settingsFile->fileName();
-        return false;
+        return QJsonDocument( QJsonObject() );
     }
     QJsonParseError error;
-    m_jsonObject = QJsonDocument::fromJson( m_settingsFile->readAll(), &error ).object();
+    QJsonDocument doc = QJsonDocument::fromJson( m_settingsFile->readAll(), &error );
     if ( error.error != QJsonParseError::NoError )
     {
         vlmcWarning() << "Failed to load settings file" << m_settingsFile->fileName();
         vlmcWarning() << error.errorString();
-        return false;
+        return QJsonDocument( QJsonObject() );
     }
+    return 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();
+bool
+Settings::load()
+{
+    if ( m_settingsFile == nullptr )
+        return false;
 
+    QJsonObject top = readSettingsFromFile().object();
+
+    loadJsonFrom( top );
+
+    for ( const auto& child : m_settingsChildren )
+    {
+        child.second->loadJsonFrom( top[ child.first ].toObject() );
     }
 
     m_settingsFile->close();
@@ -106,18 +111,21 @@ Settings::save()
 {
     if ( m_settingsFile == nullptr )
         return false;
-    QJsonDocument doc;
 
     QReadLocker lock( &m_rwLock );
 
-    QJsonObject top;
-    for ( const auto& val : m_settings )
+    QJsonDocument doc = readSettingsFromFile();
+    QJsonObject top = doc.object();
+
+    saveJsonTo( top );
+
+    for ( const auto& child : m_settingsChildren )
     {
-        if ( ( val->flags() & SettingValue::Runtime ) != 0 )
-            continue ;
-        if ( top.insert( val->key(), QJsonValue::fromVariant( val->get() ) ) == top.end() )
-            vlmcWarning() << "Failed to set:" << val->key();
+        QJsonObject object;
+        child.second->saveJsonTo( object );
+        top.insert( child.first, QJsonValue( object ) );
     }
+
     doc.setObject( top );
 
     m_settingsFile->open( QFile::WriteOnly );
@@ -126,6 +134,48 @@ Settings::save()
     return true;
 }
 
+void
+Settings::loadJsonFrom( const QJsonObject &object )
+{
+    for ( auto it = object.constBegin();
+          it != object.constEnd();
+          ++it
+          )
+    {
+        // Check if the key is a child settings'
+        bool isChildSettings = false;
+        if ( (*it).type() == QJsonValue::Object )
+            for ( const auto& pair : m_settingsChildren )
+                if ( pair.first == it.key() )
+                {
+                    isChildSettings = true;
+                    break;
+                }
+        if ( isChildSettings == true )
+            continue;
+
+        if ( setValue( it.key(), (*it).toVariant() ) == false )
+            vlmcWarning() << "Loaded invalid project setting:" << it.key();
+    }
+}
+
+void
+Settings::saveJsonTo( QJsonObject &object )
+{
+    for ( const auto& val : m_settings )
+    {
+        if ( ( val->flags() & SettingValue::Runtime ) != 0 )
+            continue ;
+        object.insert( val->key(), QJsonValue::fromVariant( val->get() ) );
+    }
+}
+
+void
+Settings::addSettings(const QString &name, Settings &settings)
+{
+    m_settingsChildren << qMakePair( name, &settings );
+}
+
 bool
 Settings::setValue(const QString &key, const QVariant &value)
 {
diff --git a/src/Settings/Settings.h b/src/Settings/Settings.h
index f41f844..8be0586 100644
--- a/src/Settings/Settings.h
+++ b/src/Settings/Settings.h
@@ -29,11 +29,13 @@
 
 #include <QString>
 #include <QMap>
+#include <QPair>
 #include <QObject>
 #include <QReadWriteLock>
 #include <QVariant>
 #include <QXmlStreamWriter>
 #include <QJsonObject>
+#include <QJsonDocument>
 
 class SettingValue;
 
@@ -114,6 +116,7 @@ class   Settings
         SettingList                 group( const QString &groupName ) const;
         bool                        load();
         bool                        save();
+        void                        addSettings( const QString& name, Settings& settings );
         void                        restoreDefaultValues();
         void                        setSettingsFile( const QString& settingsFile );
 
@@ -122,6 +125,12 @@ class   Settings
         QJsonObject                 m_jsonObject;
         mutable QReadWriteLock      m_rwLock;
         QFile*                      m_settingsFile;
+
+        QList<QPair<QString, Settings*>>                 m_settingsChildren;
+
+        QJsonDocument               readSettingsFromFile();
+        void                        loadJsonFrom( const QJsonObject& object );
+        void                        saveJsonTo( QJsonObject& object );
 };
 
 #endif



More information about the Vlmc-devel mailing list