[vlmc-devel] Complete save/load of Library

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


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

Complete save/load of Library

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

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

 src/Library/Library.cpp        | 30 +++++++++++++++++++++++++++++-
 src/Library/Library.h          |  2 ++
 src/Library/MediaContainer.cpp | 26 ++++++++++++++++++++++++++
 src/Library/MediaContainer.h   |  3 +++
 src/Media/Clip.cpp             | 36 ++++++++++++++++++++++++++++++++++++
 src/Media/Clip.h               |  3 +++
 src/Media/Media.cpp            |  8 ++++++++
 src/Media/Media.h              |  2 ++
 8 files changed, 109 insertions(+), 1 deletion(-)

diff --git a/src/Library/Library.cpp b/src/Library/Library.cpp
index 89c0353..fdd1fe8 100644
--- a/src/Library/Library.cpp
+++ b/src/Library/Library.cpp
@@ -36,7 +36,7 @@
 #include "Tools/VlmcDebug.h"
 #include "Project/Workspace.h"
 
-#include <QDomElement>
+#include <QVariant>
 #include <QHash>
 #include <QUuid>
 
@@ -45,9 +45,36 @@ Library::Library( Settings *projectSettings )
     , m_settings( new Settings )
 {
     m_settings->createVar( SettingValue::List, QString( "medias" ), QVariantList(), "", "", SettingValue::Nothing );
+    m_settings->createVar( SettingValue::List, QString( "clips" ), QVariantList(), "", "", SettingValue::Nothing );
+    connect( m_settings, &Settings::postLoad, this, &Library::postLoad, Qt::DirectConnection );
+    connect( m_settings, &Settings::preSave, this, &Library::preSave, Qt::DirectConnection );
+
     projectSettings->addSettings( "Library", *m_settings );
 }
 
+void
+Library::preSave()
+{
+    QVariantList l;
+    for ( auto val : m_medias )
+        l << val->toVariant();
+    m_settings->value( "medias" )->set( l );
+    l.clear();
+    for ( auto val : m_clips )
+        l << val->toVariantFull();
+    m_settings->value( "clips" )->set( l );
+}
+
+void
+Library::postLoad()
+{
+    for ( const auto& var : m_settings->value( "medias" )->get().toList() )
+        createMediaFromVariant( var );
+
+    for ( const auto& var : m_settings->value( "clips" )->get().toList() )
+        createClipFromVariant( var, nullptr );
+}
+
 Library::~Library()
 {
     delete m_settings;
@@ -95,6 +122,7 @@ Library::addClip( Clip *clip )
     bool    ret = MediaContainer::addClip( clip );
     if ( ret != false )
         setCleanState( false );
+    m_medias[clip->getMedia()->fileInfo()->absoluteFilePath()] = clip->getMedia();
     return ret;
 }
 
diff --git a/src/Library/Library.h b/src/Library/Library.h
index 3a0fd79..7412ff1 100644
--- a/src/Library/Library.h
+++ b/src/Library/Library.h
@@ -69,6 +69,8 @@ private:
     Workspace*  m_workspace;
 
     Settings*   m_settings;
+    void        preSave();
+    void        postLoad();
 
 private slots:
     void    mediaLoaded( const Media* m );
diff --git a/src/Library/MediaContainer.cpp b/src/Library/MediaContainer.cpp
index 14c0bae..6eacaa8 100644
--- a/src/Library/MediaContainer.cpp
+++ b/src/Library/MediaContainer.cpp
@@ -194,3 +194,29 @@ MediaContainer::count() const
 {
     return m_clips.size();
 }
+
+Media*
+MediaContainer::createMediaFromVariant( const QVariant& var )
+{
+    Media* m = addMedia( QFileInfo( var.toString() ) );
+    return m;
+}
+
+Clip*
+MediaContainer::createClipFromVariant( const QVariant &var, Clip* parent )
+{
+    QVariantMap h = var.toMap();
+    Clip* c = nullptr;
+
+    if ( h.contains( "parent" ) )
+        c = new Clip( parent, h["begin"].toULongLong(),
+                h["end"].toULongLong(), h["uuid"].toString() );
+    else {
+        c = new Clip( m_medias[ h["media"].toString() ], 0, -1, h["uuid"].toString() );
+        addClip( c );
+    }
+    if ( h.contains( "subClips" ) )
+        for ( auto& var : h["subClips"].toList() )
+            c->addSubclip( createClipFromVariant( var, c ) );
+    return c;
+}
diff --git a/src/Library/MediaContainer.h b/src/Library/MediaContainer.h
index 97f0b12..2325ad9 100644
--- a/src/Library/MediaContainer.h
+++ b/src/Library/MediaContainer.h
@@ -121,6 +121,9 @@ protected:
 
     Clip*                   m_parent;
 
+    Media*          createMediaFromVariant( const QVariant& var );
+    Clip*           createClipFromVariant( const QVariant& var, Clip* parent );
+
 public slots:
     /**
      *  \brief  Removes a Clip from the container and delete it
diff --git a/src/Media/Clip.cpp b/src/Media/Clip.cpp
index 59ef6ac..fe85c3e 100644
--- a/src/Media/Clip.cpp
+++ b/src/Media/Clip.cpp
@@ -25,10 +25,12 @@
   */
 
 #include "Clip.h"
+#include "Main/Core.h"
 #include "Backend/ISource.h"
 #include "Library/Library.h"
 #include "Media/Media.h"
 #include "Project/Workspace.h"
+#include <QVariant>
 
 const int   Clip::DefaultFPS = 30;
 
@@ -243,6 +245,40 @@ Clip::isChild( const QUuid &uuid) const
     return false;
 }
 
+QVariant
+Clip::toVariant() const
+{
+    QVariantHash h = {
+        { "uuid", m_uuid.toString() },
+        { "metatags", m_metaTags },
+        { "notes", m_notes }
+    };
+    if ( isRootClip() )
+        h.insert( "media", m_media->toVariant() );
+    else
+    {
+        h.insert( "parent", m_parent->uuid().toString() );
+        h.insert( "begin", m_begin );
+        h.insert( "end", m_end );
+    }
+    return QVariant( h );
+
+}
+
+QVariant
+Clip::toVariantFull() const
+{
+    QVariantHash h = toVariant().toHash();
+    if ( m_childs->count() > 0 )
+    {
+        QVariantList l;
+        for ( const auto& c : m_childs->clips() )
+            l << c->toVariant();
+        h.insert( "subClips", l );
+    }
+    return h;
+}
+
 void
 Clip::mediaMetadataUpdated()
 {
diff --git a/src/Media/Clip.h b/src/Media/Clip.h
index a6e6c52..ec63682 100644
--- a/src/Media/Clip.h
+++ b/src/Media/Clip.h
@@ -143,6 +143,9 @@ class   Clip : public QObject
          */
         bool                isChild( const QUuid& uuid ) const;
 
+        QVariant            toVariant() const;
+        QVariant            toVariantFull() const;
+
     private:
         Media               *m_media;
         /**
diff --git a/src/Media/Media.cpp b/src/Media/Media.cpp
index d6dea28..9f708f1 100644
--- a/src/Media/Media.cpp
+++ b/src/Media/Media.cpp
@@ -31,6 +31,8 @@
 #include "Media.h"
 
 #include "Clip.h"
+#include "Main/Core.h"
+#include "Library/Library.h"
 #include "Metadata/MetaDataManager.h"
 #include "Tools/VlmcDebug.h"
 #include "Project/Workspace.h"
@@ -146,6 +148,12 @@ Media::onMetaDataComputed()
     }
 }
 
+QVariant
+Media::toVariant() const
+{
+    return QVariant( m_fileInfo->absoluteFilePath() );
+}
+
 void
 Media::setFilePath( const QString &filePath )
 {
diff --git a/src/Media/Media.h b/src/Media/Media.h
index 7957562..b6d3ed6 100644
--- a/src/Media/Media.h
+++ b/src/Media/Media.h
@@ -97,6 +97,8 @@ public:
 
     void                        onMetaDataComputed();
 
+    QVariant                    toVariant() const;
+
     // This has to be called from the GUI thread.
     QPixmap&                    snapshot();
 



More information about the Vlmc-devel mailing list