[vlmc-devel] [PATCH 2/4] Implement SettingValue::Type::List

Yikai Lu luyikei.qmltu at gmail.com
Sat Mar 12 06:32:35 CET 2016


---
 src/Settings/SettingValue.h |  1 +
 src/Settings/Settings.cpp   | 56 +++++++++++++++++++++++++++++++++++++--------
 2 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/src/Settings/SettingValue.h b/src/Settings/SettingValue.h
index ebc225e..cf9932f 100644
--- a/src/Settings/SettingValue.h
+++ b/src/Settings/SettingValue.h
@@ -46,6 +46,7 @@ class   SettingValue : public QObject
             Language,
             KeyboardShortcut,
             Path,
+            List,
             ByteArray, // For now this is only for private variables, and is not expected to be used at any time
             //For effect engine settings:
             Color,
diff --git a/src/Settings/Settings.cpp b/src/Settings/Settings.cpp
index e888b78..e317b8c 100644
--- a/src/Settings/Settings.cpp
+++ b/src/Settings/Settings.cpp
@@ -71,19 +71,32 @@ Settings::save( QXmlStreamWriter& project )
 {
     QReadLocker lock( &m_rwLock );
 
-    project.writeStartElement( "settings" );
     for ( const auto val: m_settings )
     {
         if ( ( val->flags() & SettingValue::Runtime ) != 0 )
             continue ;
         project.writeStartElement( "setting" );
-        project.writeAttribute( "key", val->key() );
-        if ( val->get().canConvert<QString>() == false )
-            vlmcWarning() << "Can't serialize" << val->key();
-        project.writeAttribute( "value", val->get().toString() );
+        if ( val->type() == SettingValue::List )
+        {
+            project.writeAttribute( "key", val->key() + "/List" );
+            for ( const QVariant& var: val->get().toList() )
+            {
+                project.writeStartElement( "setting" );
+                if ( var.canConvert<QString>() == false )
+                    vlmcWarning() << "Can't serialize" << val->key();
+                project.writeAttribute( "value", var.toString() );
+                project.writeEndElement();
+            }
+        }
+        else
+        {
+            project.writeAttribute( "key", val->key() );
+            if ( val->get().canConvert<QString>() == false )
+                vlmcWarning() << "Can't serialize" << val->key();
+            project.writeAttribute( "value", val->get().toString() );
+        }
         project.writeEndElement();
     }
-    project.writeEndElement();
     return true;
 }
 
@@ -100,15 +113,36 @@ Settings::load( const QDomDocument& document )
     while ( s.isNull() == false )
     {
         QString     key = s.attribute( "key" );
-        QString     value = s.attribute( "value" );
+        QString     value;
 
         if ( key.isEmpty() == true )
             vlmcWarning() << "Invalid setting node.";
         else
         {
-            vlmcDebug() << "Loading" << key << "=>" << value;
-            if ( setValue( key, value ) == false )
-                vlmcWarning() << "Loaded invalid project setting:" << key;
+            if ( key.endsWith("/List") )
+            {
+                key.chop(5);
+                if ( key.isEmpty() == true )
+                    vlmcWarning() << "Invalid setting node.";
+                s = s.firstChildElement( "setting" );
+                QList<QVariant> list;
+                while ( s.isNull() == false )
+                {
+                    value = s.attribute( "value" );
+                    vlmcDebug() << "Loading" << key << "=>" << value;
+                    list << QVariant::fromValue( value );
+                    s = s.nextSiblingElement();
+                }
+                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;
+            }
         }
         s = s.nextSiblingElement();
     }
@@ -142,7 +176,9 @@ Settings::save()
     QByteArray          settingsContent;
     QXmlStreamWriter    streamWriter( &settingsContent );
     streamWriter.setAutoFormatting( true );
+    streamWriter.writeStartElement( "settings" );
     save( streamWriter );
+    streamWriter.writeEndElement();
     m_settingsFile->open( QFile::WriteOnly );
     m_settingsFile->write( settingsContent );
     m_settingsFile->close();
-- 
1.9.1



More information about the Vlmc-devel mailing list