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

Yikai Lu luyikei.qmltu at gmail.com
Sat Mar 12 06:32:37 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   | 163 ++++++++++++++++++++++++++++----------------
 src/Settings/Settings.h     |   5 +-
 3 files changed, 113 insertions(+), 58 deletions(-)

diff --git a/src/Settings/SettingValue.h b/src/Settings/SettingValue.h
index 7ff9486..6b6ac80 100644
--- a/src/Settings/SettingValue.h
+++ b/src/Settings/SettingValue.h
@@ -25,6 +25,7 @@
 
 #include <QObject>
 #include <QVariant>
+#include <QList>
 
 /**
  * 'class SettingValue
@@ -48,6 +49,7 @@ class   SettingValue : public QObject
             Path,
             List,
             Map,
+            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,
@@ -142,5 +144,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 abeaaa1..ac443e6 100644
--- a/src/Settings/Settings.cpp
+++ b/src/Settings/Settings.cpp
@@ -66,12 +66,12 @@ 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 );
 
-    for ( const auto val: m_settings )
+    for ( SettingValue* val: values )
     {
         if ( ( val->flags() & SettingValue::Runtime ) != 0 )
             continue ;
@@ -105,6 +105,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() );
@@ -114,74 +120,110 @@ Settings::save( QXmlStreamWriter& project )
         }
         project.writeEndElement();
     }
-    return true;
 }
 
 bool
-Settings::load( const QDomDocument& document )
+Settings::save( QXmlStreamWriter& project )
 {
-    QDomElement     element = document.firstChildElement( "settings" );
-    if ( element.isNull() == true )
-    {
-        vlmcWarning() << "Invalid settings node";
-        return false;
-    }
-    QDomElement s = element.firstChildElement( "setting" );
+    QReadLocker lock( &m_rwLock );
+
+    saveElement( project, m_settings.values() );
+    return true;
+}
+
+QList<SettingValue*>
+Settings::loadElement(const QDomElement &element)
+{
+    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);
-                if ( key.isEmpty() == true )
-                    vlmcWarning() << "Invalid setting node.";
-                s = s.firstChildElement( "setting" );
-                QList<QVariant> list;
-                while ( s.isNull() == false )
+                if ( key.endsWith("/List") )
                 {
-                    value = s.attribute( "value" );
-                    vlmcDebug() << "Loading" << key << "=>" << value;
-                    list << QVariant::fromValue( value );
-                    s = s.nextSiblingElement();
+                    key.chop(5);
+                    if ( key.isEmpty() == true )
+                        vlmcWarning() << "Invalid setting node.";
+                    s = s.firstChildElement( "setting" );
+                    QList<QVariant> vlist;
+                    while ( s.isNull() == false )
+                    {
+                        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;
                 }
-                if ( setValue( key, list ) == false )
-                    vlmcWarning() << "Loaded invalid project setting:" << key;
-            }
-            else if ( key.endsWith("/Map") )
-            {
-                key.chop(4);
-                if ( key.isEmpty() == true )
-                    vlmcWarning() << "Invalid setting node.";
-                s = s.firstChildElement( "setting" );
-                QMap<QString, QVariant> map;
-                QString mKey;
-                while ( s.isNull() == false )
+                else if ( key.endsWith("/Map") )
                 {
-                    value = s.attribute( "value" );
-                    mKey = s.attribute( "key" );
-                    vlmcDebug() << "Loading" << key << ":" << mKey << "=>" << value;
-                    map[ mKey ] = value;
-                    s = s.nextSiblingElement();
+                    key.chop(4);
+                    if ( key.isEmpty() == true )
+                        vlmcWarning() << "Invalid setting node.";
+                    s = s.firstChildElement( "setting" );
+                    QMap<QString, QVariant> map;
+                    QString mKey;
+                    while ( s.isNull() == false )
+                    {
+                        value = s.attribute( "value" );
+                        mKey = s.attribute( "key" );
+                        vlmcDebug() << "Loading" << key << ":" << mKey << "=>" << value;
+                        map[ mKey ] = value;
+                        s = s.nextSiblingElement();
+                    }
+                    if ( setValue( key, map ) == false )
+                        vlmcWarning() << "Loaded invalid project setting:" << key;
+                }
+                else
+                {
+                    vlmcDebug() << "Loading" << key << "=>" << value;
+                    SettingValue* val = setValue( key, value );
+                    if ( val == nullptr )
+                        vlmcWarning() << "Loaded invalid project setting:" << key;
+                    else
+                        list << val;
                 }
-                if ( setValue( key, map ) == 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;
 }
 
@@ -221,17 +263,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*
@@ -258,6 +300,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