[vlmc-devel] [PATCH] Library: Merge m_videoModel and m_audioModel

Yikai Lu luyikei.qmltu at gmail.com
Wed Mar 8 21:41:34 CET 2017


---
 src/Gui/library/MediaLibraryView.cpp |  4 +--
 src/Library/Library.cpp              | 55 ++++++------------------------------
 src/Library/Library.h                | 11 ++------
 src/Library/MediaLibraryModel.cpp    | 20 ++++---------
 src/Library/MediaLibraryModel.h      |  4 +--
 5 files changed, 19 insertions(+), 75 deletions(-)

diff --git a/src/Gui/library/MediaLibraryView.cpp b/src/Gui/library/MediaLibraryView.cpp
index 530cea2..d06e16d 100644
--- a/src/Gui/library/MediaLibraryView.cpp
+++ b/src/Gui/library/MediaLibraryView.cpp
@@ -52,7 +52,7 @@ MediaLibraryView::MediaLibraryView( QWidget* parent )
     m_container->setObjectName( objectName() );
 
     auto ctx = view->rootContext();
-    ctx->setContextProperty( QStringLiteral( "mlModel" ), Core::instance()->library()->model( Library::MediaType::Video ) );
+    ctx->setContextProperty( QStringLiteral( "mlModel" ), Core::instance()->library()->model() );
     ctx->setContextProperty( QStringLiteral( "view" ), this );
 
     view->setSource( QUrl( QStringLiteral( "qrc:/QML/MediaLibraryView.qml" ) ) );
@@ -90,7 +90,7 @@ MediaLibraryView::onMediaSelected( qint64 mediaId )
 {
     QSharedPointer<Media> media = Core::instance()->library()->media( mediaId );
     if ( media == nullptr ) {
-        media.reset( new Media( Core::instance()->library()->model( Library::MediaType::Video )->findMedia( mediaId ) ) );
+        media.reset( new Media( Core::instance()->library()->model()->findMedia( mediaId ) ) );
         Core::instance()->library()->addMedia( media );
     }
 
diff --git a/src/Library/Library.cpp b/src/Library/Library.cpp
index 8168870..5d150d1 100644
--- a/src/Library/Library.cpp
+++ b/src/Library/Library.cpp
@@ -51,8 +51,7 @@ Library::Library( Settings* vlmcSettings, Settings *projectSettings )
     // Setting up the external media library
     m_ml.reset( NewMediaLibrary() );
     m_ml->setVerbosity( medialibrary::LogLevel::Warning );
-    m_videoModel = new MediaLibraryModel( *m_ml, medialibrary::IMedia::Type::Video, this );
-    m_audioModel = new MediaLibraryModel( *m_ml, medialibrary::IMedia::Type::Audio, this );
+    m_model = new MediaLibraryModel( *m_ml, this );
 
     auto s = vlmcSettings->createVar( SettingValue::List, QStringLiteral( "vlmc/mlDirs" ), QVariantList(),
                         "Media Library folders", "List of folders VLMC will search for media files",
@@ -141,16 +140,9 @@ Library::mlMedia( qint64 mediaId )
 }
 
 MediaLibraryModel*
-Library::model(Library::MediaType type) const
+Library::model() const
 {
-    switch ( type )
-    {
-        case MediaType::Video:
-            return m_videoModel;
-        case MediaType::Audio:
-            return m_audioModel;
-    }
-    Q_UNREACHABLE();
+    return m_model;
 }
 
 QSharedPointer<Clip>
@@ -215,17 +207,7 @@ Library::onMediaAdded( std::vector<medialibrary::MediaPtr> mediaList )
 {
     for ( auto m : mediaList )
     {
-        switch ( m->type() )
-        {
-        case medialibrary::IMedia::Type::Video:
-            m_videoModel->addMedia( m );
-            break;
-        case medialibrary::IMedia::Type::Audio:
-            m_audioModel->addMedia( m );
-            break;
-        default:
-            Q_UNREACHABLE();
-        }
+        m_model->addMedia( m );
     }
 }
 
@@ -234,17 +216,7 @@ Library::onMediaUpdated( std::vector<medialibrary::MediaPtr> mediaList )
 {
     for ( auto m : mediaList )
     {
-        switch ( m->type() )
-        {
-        case medialibrary::IMedia::Type::Video:
-            m_videoModel->updateMedia( m );
-            break;
-        case medialibrary::IMedia::Type::Audio:
-            m_audioModel->updateMedia( m );
-            break;
-        default:
-            Q_UNREACHABLE();
-        }
+        m_model->updateMedia( m );
     }
 }
 
@@ -252,13 +224,7 @@ void
 Library::onMediaDeleted( std::vector<int64_t> mediaList )
 {
     for ( auto id : mediaList )
-    {
-        // We can't know the media type, however ID are unique regardless of the type
-        // so we are sure that we will remove the correct media.
-        if ( m_videoModel->removeMedia( id ) == true )
-            continue;
-        m_audioModel->removeMedia( id );
-    }
+        m_model->removeMedia( id );
 }
 
 void
@@ -316,10 +282,7 @@ void
 Library::onDiscoveryCompleted( const std::string& entryPoint )
 {
     if ( entryPoint.empty() == true )
-    {
-        m_videoModel->refresh();
-        m_audioModel->refresh();
-    }
+        m_model->refresh();
 
     emit discoveryCompleted( QString::fromStdString( entryPoint ) );
 }
@@ -356,10 +319,10 @@ Library::onReloadCompleted( const std::string& entryPoint )
     if ( entryPoint.empty() == true )
     {
         for ( auto media : m_ml->videoFiles() )
-            m_videoModel->addMedia( media );
+            m_model->addMedia( media );
 
         for ( auto media : m_ml->audioFiles() )
-            m_audioModel->addMedia( media );
+            m_model->addMedia( media );
     }
 }
 
diff --git a/src/Library/Library.h b/src/Library/Library.h
index 31617b8..2879745 100644
--- a/src/Library/Library.h
+++ b/src/Library/Library.h
@@ -54,12 +54,6 @@ class Library : public QObject, private medialibrary::IMediaLibraryCb
     Q_DISABLE_COPY( Library )
 
 public:
-    enum class MediaType
-    {
-        Video,
-        Audio
-    };
-
     Library( Settings* vlmcSettings, Settings* projectSettings );
     virtual ~Library();
     void            addMedia( QSharedPointer<Media> media );
@@ -69,7 +63,7 @@ public:
     //FIXME: This feels rather ugly
     medialibrary::MediaPtr mlMedia( qint64 mediaId);
 
-    MediaLibraryModel* model( MediaType type ) const;
+    MediaLibraryModel* model() const;
 
     /**
      * @brief clip returns an existing clip
@@ -115,8 +109,7 @@ private:
 
 private:
     std::unique_ptr<medialibrary::IMediaLibrary>    m_ml;
-    MediaLibraryModel*                              m_videoModel;
-    MediaLibraryModel*                              m_audioModel;
+    MediaLibraryModel*                              m_model;
     Settings*                                       m_settings;
     bool                                            m_initialized;
     bool                                            m_cleanState;
diff --git a/src/Library/MediaLibraryModel.cpp b/src/Library/MediaLibraryModel.cpp
index 5a52a89..b46ddf3 100644
--- a/src/Library/MediaLibraryModel.cpp
+++ b/src/Library/MediaLibraryModel.cpp
@@ -29,18 +29,15 @@
 #include <QPixmap>
 #endif
 
-MediaLibraryModel::MediaLibraryModel( medialibrary::IMediaLibrary& ml, medialibrary::IMedia::Type type, QObject *parent )
+MediaLibraryModel::MediaLibraryModel( medialibrary::IMediaLibrary& ml, QObject *parent )
     : QAbstractListModel(parent)
     , m_ml( ml )
-    , m_mediaType( type )
     , m_rowCount( 0 )
 {
 }
 
 void MediaLibraryModel::addMedia( medialibrary::MediaPtr media )
 {
-    if ( media->type() != m_mediaType )
-        return;
     std::lock_guard<std::mutex> lock( m_mediaMutex );
     auto size = m_media.size();
     beginInsertRows( QModelIndex(), size, size );
@@ -143,17 +140,10 @@ void MediaLibraryModel::refresh()
 
     beginResetModel();
 
-    switch ( m_mediaType )
-    {
-    case medialibrary::IMedia::Type::Audio:
-        m_media = m_ml.audioFiles();
-        break;
-    case medialibrary::IMedia::Type::Video:
-        m_media = m_ml.videoFiles();
-        break;
-    default:
-        Q_UNREACHABLE();
-    }
+    const auto& audioFiles = m_ml.audioFiles();
+    const auto& videoFiles = m_ml.videoFiles();
+    m_media.insert( m_media.end(), audioFiles.begin(), audioFiles.end() );
+    m_media.insert( m_media.end(), videoFiles.begin(), videoFiles.end() );
     m_rowCount = m_media.size();
     endResetModel();
 }
diff --git a/src/Library/MediaLibraryModel.h b/src/Library/MediaLibraryModel.h
index 0add020..7729ff0 100644
--- a/src/Library/MediaLibraryModel.h
+++ b/src/Library/MediaLibraryModel.h
@@ -44,8 +44,7 @@ public:
         Id,
     };
 
-    explicit MediaLibraryModel( medialibrary::IMediaLibrary& ml, medialibrary::IMedia::Type type,
-                                QObject *parent = 0 );
+    explicit MediaLibraryModel( medialibrary::IMediaLibrary& ml, QObject *parent = 0 );
 
     void addMedia( medialibrary::MediaPtr media );
     medialibrary::MediaPtr findMedia( qint64 mediaId );
@@ -59,7 +58,6 @@ public:
 
 private:
     medialibrary::IMediaLibrary& m_ml;
-    const medialibrary::IMedia::Type m_mediaType;
 
     // Use an atomic int to avoid locking m_media within rowCount()
     mutable std::mutex m_mediaMutex;
-- 
2.7.4



More information about the Vlmc-devel mailing list