[vlmc-devel] [PATCH 3/3] Implement getting SettingValues recursively

Yikai Lu luyikei.qmltu at gmail.com
Fri Mar 11 10:23:13 CET 2016


This patch will make vlmc be able to create SettingValueList, which will make a root tag and SettingValue children.

Also, this patch will not break backward compatibility, therefore fixing the loading project files problem.
---
 src/Settings/SettingValue.h |   3 +
 src/Settings/Settings.cpp   | 131 ++++++++++++++++++++++++++++++--------------
 src/Settings/Settings.h     |   5 +-
 3 files changed, 96 insertions(+), 43 deletions(-)

diff --git a/src/Settings/SettingValue.h b/src/Settings/SettingValue.h
index cf9932f..f5cb053 100644
--- a/src/Settings/SettingValue.h
+++ b/src/Settings/SettingValue.h
@@ -25,6 +25,7 @@
 
 #include <QObject>
 #include <QVariant>
+#include <QList>
 
 /**
  * 'class SettingValue
@@ -47,6 +48,7 @@ class   SettingValue : public QObject
             KeyboardShortcut,
             Path,
             List,
+            SettingValueList,
             ByteArray, // For now this is only for private variables, and is not expected to be used at any time
             //For effect engine settings:
             Color,
@@ -141,5 +143,6 @@ class   SettingValue : public QObject
 };
 
 Q_DECLARE_OPERATORS_FOR_FLAGS(SettingValue::Flags)
+Q_DECLARE_METATYPE(QList<SettingValue*>)
 
 #endif // SETTINGVALUE_H
diff --git a/src/Settings/Settings.cpp b/src/Settings/Settings.cpp
index 2b5dc26..ad4d7c7 100644
--- a/src/Settings/Settings.cpp
+++ b/src/Settings/Settings.cpp
@@ -66,13 +66,10 @@ Settings::setSettingsFile(const QString &settingsFile)
         m_settingsFile = NULL;
 }
 
-bool
-Settings::save( QXmlStreamWriter& project )
+void
+Settings::saveElement( QXmlStreamWriter& project, QList<SettingValue*> values )
 {
-    QReadLocker lock( &m_rwLock );
-
-    project.writeStartElement( "settings" );
-    for ( SettingValue* val: m_settings.values() )
+    for ( SettingValue* val: values )
     {
         if ( ( val->flags() & SettingValue::Runtime ) != 0 )
             continue ;
@@ -89,6 +86,12 @@ Settings::save( QXmlStreamWriter& project )
                 project.writeEndElement();
             }
         }
+        else if ( val->type() == SettingValue::SettingValueList )
+        {
+            project.writeStartElement( val->key() );
+            saveElement( project, val->get().value<QList<SettingValue*> >() );
+            project.writeEndElement();
+        }
         else
         {
             project.writeAttribute( "key", val->key() );
@@ -98,56 +101,93 @@ Settings::save( QXmlStreamWriter& project )
         }
         project.writeEndElement();
     }
+}
+
+bool
+Settings::save( QXmlStreamWriter& project )
+{
+    QReadLocker lock( &m_rwLock );
+
+    project.writeStartElement( "settings" );
+    saveElement( project, m_settings.values() );
     project.writeEndElement();
     return true;
 }
 
-bool
-Settings::load( const QDomDocument& document )
+QList<SettingValue*>
+Settings::loadElement(const QDomElement &element)
 {
-    QDomElement     element = document.firstChildElement( "settings" );
-    if ( element.isNull() == true )
-    {
-        vlmcWarning() << "Invalid settings node";
-        return false;
-    }
-    QDomElement s = element.firstChildElement( "setting" );
+    QList<SettingValue*> list;
+
+    QDomElement s = element;
     while ( s.isNull() == false )
     {
-        QString     key = s.attribute( "key" );
-        QString     value;
-
-        if ( key.isEmpty() == true )
-            vlmcWarning() << "Invalid setting node.";
-        else
+        if ( s.tagName() == "setting" )
         {
-            if ( key.endsWith("/List") )
+            QString     key = s.attribute( "key" );
+            QString     value = s.attribute( "value" );
+
+            if ( key.isEmpty() == true )
+                vlmcWarning() << "Invalid setting node.";
+            else
             {
-                key.chop(5);
-                s = s.firstChildElement( "setting" );
-                QList<QVariant> list;
-                while ( s.isNull() == false )
+                if ( key.endsWith("/List") )
+                {
+                    key.chop(5);
+                    s = s.firstChildElement( "setting" );
+                    QList<QVariant> vlist;
+                    while ( s.isNull() == false )
+                    {
+                        if ( key.isEmpty() == true )
+                            vlmcWarning() << "Invalid setting node.";
+                        value = s.attribute( "value" );
+                        vlmcDebug() << "Loading" << key << "=>" << value;
+                        vlist << QVariant::fromValue( value );
+                        s = s.nextSiblingElement();
+                    }
+                    SettingValue* val = setValue( key, vlist );
+                    if ( val == nullptr )
+                        vlmcWarning() << "Loaded invalid project setting:" << key;
+                    else
+                        list << val;
+                }
+                else
                 {
-                    if ( key.isEmpty() == true )
-                        vlmcWarning() << "Invalid setting node.";
-                    value = s.attribute( "value" );
                     vlmcDebug() << "Loading" << key << "=>" << value;
-                    list << QVariant::fromValue( value );
-                    s = s.nextSiblingElement();
+                    SettingValue* val = setValue( key, value );
+                    if ( val == nullptr )
+                        vlmcWarning() << "Loaded invalid project setting:" << key;
+                    else
+                        list << val;
                 }
-                if ( setValue( key, list ) == false )
-                    vlmcWarning() << "Loaded invalid project setting:" << key;
             }
-            else
-            {
-                value = s.attribute( "value" );
-                vlmcDebug() << "Loading" << key << "=>" << value;
-                if ( setValue( key, value ) == false )
-                    vlmcWarning() << "Loaded invalid project setting:" << key;
+        }
+        else
+        {
+            QList<SettingValue*> sList = loadElement( s.firstChildElement() );
+            SettingValue* val = createVar( SettingValue::SettingValueList, s.tagName(), sList, "", "", SettingValue::Nothing);
+            if ( val == nullptr ) {
+                vlmcWarning() << "Loaded invalid project setting:" << s.tagName();
             }
+            list << val;
         }
         s = s.nextSiblingElement();
     }
+
+    return list;
+}
+
+bool
+Settings::load( const QDomDocument& document )
+{
+    //QDomElement     element = document.firstChildElement( "settings" );
+    QDomElement     element = document.firstChildElement();
+    if ( element.isNull() == true )
+    {
+        vlmcWarning() << "Invalid settings node";
+        return false;
+    }
+    loadElement( element );
     return true;
 }
 
@@ -185,17 +225,17 @@ Settings::save()
     return true;
 }
 
-bool
+SettingValue*
 Settings::setValue(const QString &key, const QVariant &value)
 {
     SettingMap::iterator   it = m_settings.find( key );
     if ( it != m_settings.end() )
     {
         (*it)->set( value );
-        return true;
+        return *it;
     }
     Q_ASSERT_X( false, __FILE__, "setting value without a created variable" );
-    return false;
+    return nullptr;
 }
 
 SettingValue*
@@ -222,6 +262,13 @@ Settings::createVar(SettingValue::Type type, const QString &key, const QVariant
     return val;
 }
 
+SettingValue*
+Settings::createVar(SettingValue::Type type, const QString &key, const QList<SettingValue *> &defaultValue, const char *name, const char *desc, SettingValue::Flags flags)
+{
+    type = SettingValue::SettingValueList;
+    return createVar( type, key, QVariant::fromValue( defaultValue ), name, desc, flags );
+}
+
 Settings::SettingList
 Settings::group(const QString &groupName) const
 {
diff --git a/src/Settings/Settings.h b/src/Settings/Settings.h
index 65d368a..6546d72 100644
--- a/src/Settings/Settings.h
+++ b/src/Settings/Settings.h
@@ -115,12 +115,15 @@ class   Settings : public ILoadSave
 
         Settings( const QString& settingsFile );
         ~Settings();
-        bool                        setValue(const QString &key, const QVariant &value );
+        SettingValue*               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 );
+        SettingValue*               createVar( SettingValue::Type type, const QString &key, const QList<SettingValue*> &defaultValue, const char *name, const char *desc, SettingValue::Flags flags );
         SettingList                 group( const QString &groupName ) const;
+        QList<SettingValue*>             loadElement( const QDomElement& element );
         bool                        load();
         bool                        save();
+        void                        saveElement( QXmlStreamWriter& project, QList<SettingValue*> values );
         bool                        save( QXmlStreamWriter& project );
         void                        setSettingsFile( const QString& settingsFile );
 
-- 
1.9.1



More information about the Vlmc-devel mailing list