[vlmc-devel] [PATCH 01/10] Basic Json settings

Yikai Lu luyikei.qmltu at gmail.com
Mon Apr 4 17:24:48 CEST 2016


---
 src/Gui/timeline/Timeline.cpp     |  3 ++-
 src/Gui/timeline/Timeline.h       |  4 +--
 src/Library/Library.cpp           |  4 +--
 src/Library/Library.h             |  6 ++---
 src/Project/ILoadSave.h           |  7 +++--
 src/Project/Project.cpp           | 16 ++++++------
 src/Renderer/WorkflowRenderer.cpp |  4 +--
 src/Renderer/WorkflowRenderer.h   |  4 +--
 src/Settings/Settings.cpp         | 54 +++++++++++++++------------------------
 src/Settings/Settings.h           |  6 ++---
 src/Workflow/MainWorkflow.cpp     |  3 ++-
 src/Workflow/MainWorkflow.h       |  4 +--
 12 files changed, 51 insertions(+), 64 deletions(-)

diff --git a/src/Gui/timeline/Timeline.cpp b/src/Gui/timeline/Timeline.cpp
index 1408e09..38d7243 100644
--- a/src/Gui/timeline/Timeline.cpp
+++ b/src/Gui/timeline/Timeline.cpp
@@ -173,7 +173,7 @@ Timeline::setTool( ToolButtons button )
 {
     tracksView()->setTool( button );
 }
-
+/*
 bool
 Timeline::save( QXmlStreamWriter &project )
 {
@@ -246,3 +246,4 @@ Timeline::load( const QDomDocument& root )
     }
     return true;
 }
+*/
diff --git a/src/Gui/timeline/Timeline.h b/src/Gui/timeline/Timeline.h
index cd2d983..4f914f6 100644
--- a/src/Gui/timeline/Timeline.h
+++ b/src/Gui/timeline/Timeline.h
@@ -87,8 +87,8 @@ protected:
 
 private:
     void                initialize();
-    virtual bool        save( QXmlStreamWriter& project );
-    virtual bool        load( const QDomDocument& root );
+    virtual bool        save( QJsonDocument& ) { return false; }
+    virtual bool        load( const QJsonDocument& ) { return false; }
 
 
 private:
diff --git a/src/Library/Library.cpp b/src/Library/Library.cpp
index db5e2bf..9901b07 100644
--- a/src/Library/Library.cpp
+++ b/src/Library/Library.cpp
@@ -45,7 +45,7 @@ Library::Library( Workspace *workspace )
     , m_workspace( workspace )
 {
 }
-
+/*
 bool
 Library::load(const QDomDocument& doc )
 {
@@ -108,7 +108,7 @@ Library::save( QXmlStreamWriter& project )
     setCleanState( true );
     return true;
 }
-
+*/
 void
 Library::mediaLoaded( const Media* media )
 {
diff --git a/src/Library/Library.h b/src/Library/Library.h
index 162305a..48af275 100644
--- a/src/Library/Library.h
+++ b/src/Library/Library.h
@@ -47,7 +47,7 @@ class Workspace;
  *  \class Library
  *  \brief Library Object that handles public Clips
  */
-class Library : public MediaContainer, public ILoadSave
+class Library : public MediaContainer/*, public ILoadSave*/
 {
     Q_OBJECT
     Q_DISABLE_COPY( Library );
@@ -62,8 +62,8 @@ public:
 
     //FIXME: Ugly workaround
 public:
-    bool            save( QXmlStreamWriter& project );
-    virtual bool    load( const QDomDocument& project );
+    bool            save( QJsonDocument& ) { return false; }
+    virtual bool    load( const QJsonDocument& ) { return false; }
 
 private:
     void            setCleanState( bool newState );
diff --git a/src/Project/ILoadSave.h b/src/Project/ILoadSave.h
index ec65562..5720941 100644
--- a/src/Project/ILoadSave.h
+++ b/src/Project/ILoadSave.h
@@ -24,15 +24,14 @@
 #ifndef ILOADSAVE_H
 #define ILOADSAVE_H
 
-class QDomDocument;
-class QXmlStreamWriter;
+class QJsonDocument;
 
 class ILoadSave
 {
     public:
         virtual ~ILoadSave() {}
-        virtual bool    load( const QDomDocument& project ) = 0;
-        virtual bool    save( QXmlStreamWriter& project ) = 0;
+        virtual bool    load( const QJsonDocument& project ) = 0;
+        virtual bool    save( QJsonDocument& project ) = 0;
 };
 
 #endif // ILOADSAVE_H
diff --git a/src/Project/Project.cpp b/src/Project/Project.cpp
index 7e01e4b..bb6a2a3 100644
--- a/src/Project/Project.cpp
+++ b/src/Project/Project.cpp
@@ -137,12 +137,12 @@ Project::load( const QString& path )
         doc.setContent( m_projectFile );
     }
 
-    m_settings->load( doc );
+    //m_settings->load( doc );
     auto projectName = m_settings->value( "vlmc/ProjectName" )->get().toString();
     emit projectLoading( projectName );
-    Core::instance()->library()->load( doc );
-    Core::instance()->workflow()->load( doc );
-    Core::instance()->workflowRenderer()->load( doc );
+    //Core::instance()->library()->load( doc );
+    //Core::instance()->workflow()->load( doc );
+    //Core::instance()->workflowRenderer()->load( doc );
     m_isClean = autoBackupFound == false;
     emit cleanStateChanged( m_isClean );
     if ( autoBackupFound == false )
@@ -230,10 +230,10 @@ Project::saveProject( const QString& fileName )
     project.writeStartDocument();
     project.writeStartElement( "vlmc" );
 
-    m_settings->save( project );
-    Core::instance()->workflow()->save( project );
-    Core::instance()->library()->save( project );
-    Core::instance()->workflowRenderer()->save( project );
+    //m_settings->save( project );
+    //Core::instance()->workflow()->save( project );
+    //Core::instance()->library()->save( project );
+    //Core::instance()->workflowRenderer()->save( project );
     //FIXME: Timeline configuration isn't saved anymore.
 
     project.writeEndElement();
diff --git a/src/Renderer/WorkflowRenderer.cpp b/src/Renderer/WorkflowRenderer.cpp
index f50838a..fbf63e5 100644
--- a/src/Renderer/WorkflowRenderer.cpp
+++ b/src/Renderer/WorkflowRenderer.cpp
@@ -331,7 +331,7 @@ Backend::ISourceRenderer::MemoryInputUnlockCallback WorkflowRenderer::getUnlockC
 {
     return WorkflowRenderer::unlock;
 }
-
+/*
 bool
 WorkflowRenderer::save( QXmlStreamWriter &project )
 {
@@ -350,7 +350,7 @@ WorkflowRenderer::load(const QDomDocument& project )
     loadEffects( renderer );
     return true;
 }
-
+*/
 /////////////////////////////////////////////////////////////////////
 /////SLOTS :
 /////////////////////////////////////////////////////////////////////
diff --git a/src/Renderer/WorkflowRenderer.h b/src/Renderer/WorkflowRenderer.h
index 89aace6..e5e57ab 100644
--- a/src/Renderer/WorkflowRenderer.h
+++ b/src/Renderer/WorkflowRenderer.h
@@ -213,8 +213,8 @@ class   WorkflowRenderer : public GenericRenderer, public ILoadSave
 
     //FIXME: Ugly workaround
     public:
-        bool                save( QXmlStreamWriter &project );
-        bool                load( const QDomDocument& project );
+        bool                save( QJsonDocument & ) { return false; }
+        bool                load( const QJsonDocument& ) { return false; }
 
     protected:
         MainWorkflow*       m_mainWorkflow;
diff --git a/src/Settings/Settings.cpp b/src/Settings/Settings.cpp
index 8819d9b..026a2d3 100644
--- a/src/Settings/Settings.cpp
+++ b/src/Settings/Settings.cpp
@@ -33,7 +33,8 @@
 #include <QFileInfo>
 #include <QDir>
 
-#include <QDomElement>
+#include <QJsonDocument>
+#include <QJsonObject>
 
 
 Settings::Settings(const QString &settingsFile)
@@ -68,53 +69,38 @@ Settings::setSettingsFile(const QString &settingsFile)
 }
 
 bool
-Settings::save( QXmlStreamWriter& project )
+Settings::save( QJsonDocument& doc )
 {
     QReadLocker lock( &m_rwLock );
 
     SettingMap::const_iterator     it = m_settings.begin();
     SettingMap::const_iterator     end = m_settings.end();
 
-    project.writeStartElement( "settings" );
+    QJsonObject top;
     for ( ; it != end; ++it )
     {
         if ( ( (*it)->flags() & SettingValue::Runtime ) != 0 )
             continue ;
-        project.writeStartElement( "setting" );
-        project.writeAttribute( "key", (*it)->key() );
-        if ( (*it)->get().canConvert<QString>() == false )
-            vlmcWarning() << "Can't serialize" << (*it)->key();
-        project.writeAttribute( "value", (*it)->get().toString() );
-        project.writeEndElement();
+        if ( top.insert( (*it)->key(), QJsonValue::fromVariant( (*it)->get() ) ) == top.end() )
+            vlmcWarning() << "Failed to set:" << (*it)->key();
     }
-    project.writeEndElement();
+    doc.setObject( top );
     return true;
 }
 
 bool
-Settings::load( const QDomDocument& document )
+Settings::load( const QJsonDocument& doc )
 {
-    QDomElement     element = document.firstChildElement( "settings" );
-    if ( element.isNull() == true )
+    if ( doc.isNull() == true )
     {
         vlmcWarning() << "Invalid settings node";
         return false;
     }
-    QDomElement s = element.firstChildElement( "setting" );
-    while ( s.isNull() == false )
+    for ( const auto& key: doc.object().keys() )
     {
-        QString     key = s.attribute( "key" );
-        QString     value = s.attribute( "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;
-        }
-        s = s.nextSiblingElement();
+        if ( setValue( key, doc.object()[ key ].toVariant() ) == false )
+            vlmcWarning() << "Loaded invalid project setting:" << key;
+        
     }
     return true;
 }
@@ -122,15 +108,17 @@ Settings::load( const QDomDocument& document )
 bool
 Settings::load()
 {
-    QDomDocument    doc("root");
     if ( m_settingsFile->open( QFile::ReadOnly ) == false )
     {
         vlmcWarning() << "Failed to open settings file" << m_settingsFile->fileName();
         return false;
     }
-    if ( doc.setContent( m_settingsFile ) == false )
+    QJsonParseError error;
+    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;
     }
     bool res = load( doc );
@@ -143,12 +131,10 @@ Settings::save()
 {
     if ( m_settingsFile == nullptr )
         return false;
-    QByteArray          settingsContent;
-    QXmlStreamWriter    streamWriter( &settingsContent );
-    streamWriter.setAutoFormatting( true );
-    save( streamWriter );
+    QJsonDocument doc;
+    save( doc );
     m_settingsFile->open( QFile::WriteOnly );
-    m_settingsFile->write( settingsContent );
+    m_settingsFile->write( doc.toJson( QJsonDocument::Compact ) );
     m_settingsFile->close();
     return true;
 }
diff --git a/src/Settings/Settings.h b/src/Settings/Settings.h
index 4885602..b089c64 100644
--- a/src/Settings/Settings.h
+++ b/src/Settings/Settings.h
@@ -38,7 +38,7 @@
 class SettingValue;
 
 class QFile;
-class QDomElement;
+class QJsonDocument;
 
 
 //Var helpers :
@@ -114,11 +114,11 @@ class   Settings : public ILoadSave
         SettingList                 group( const QString &groupName ) const;
         bool                        load();
         bool                        save();
-        bool                        save( QXmlStreamWriter& project );
+        bool                        save( QJsonDocument& project );
         void                        restoreDefaultValues();
         void                        setSettingsFile( const QString& settingsFile );
 
-        bool                        load( const QDomDocument& document );
+        bool                        load( const QJsonDocument& document );
 
     private:
         SettingMap                  m_settings;
diff --git a/src/Workflow/MainWorkflow.cpp b/src/Workflow/MainWorkflow.cpp
index 890670b..9cf9405 100644
--- a/src/Workflow/MainWorkflow.cpp
+++ b/src/Workflow/MainWorkflow.cpp
@@ -233,6 +233,7 @@ MainWorkflow::getClipHelper( const QUuid &uuid, unsigned int trackId,
 /**
  *  \warning    The mainworkflow is expected to be already cleared by the ProjectManager
  */
+/*
 bool
 MainWorkflow::load(const QDomDocument& root )
 {
@@ -316,7 +317,7 @@ MainWorkflow::save( QXmlStreamWriter& project )
     project.writeEndElement();
     return true;
 }
-
+*/
 void
 MainWorkflow::clear()
 {
diff --git a/src/Workflow/MainWorkflow.h b/src/Workflow/MainWorkflow.h
index 72b7291..34ff516 100644
--- a/src/Workflow/MainWorkflow.h
+++ b/src/Workflow/MainWorkflow.h
@@ -263,8 +263,8 @@ class   MainWorkflow : public QObject, public ILoadSave
         quint32                 trackCount() const;
 
         //FIXME: Ugly workaround
-        bool                    save( QXmlStreamWriter& project );
-        bool                    load( const QDomDocument& project );
+        bool                    save( QJsonDocument& ) { return false; }
+        bool                    load( const QJsonDocument& ) { return false; }
 
     private:
         /**
-- 
1.9.1



More information about the Vlmc-devel mailing list