[vlc-commits] qt: medialib: move implementation to cpp

Romain Vimont git at videolan.org
Tue Dec 8 14:19:21 UTC 2020


vlc | branch: master | Romain Vimont <rom1v at videolabs.io> | Wed Nov 25 14:53:26 2020 +0100| [69b13cf026372982b9982e87f4487858c774a8ba] | committer: Pierre Lamot

qt: medialib: move implementation to cpp

Now that MLSlidingWindowModel is not template anymore, move its
implementation to the cpp file.

Signed-off-by: Pierre Lamot <pierre at videolabs.io>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=69b13cf026372982b9982e87f4487858c774a8ba
---

 modules/gui/qt/medialibrary/mlbasemodel.cpp | 182 +++++++++++++++++++++++++++
 modules/gui/qt/medialibrary/mlbasemodel.hpp | 187 +++-------------------------
 2 files changed, 197 insertions(+), 172 deletions(-)

diff --git a/modules/gui/qt/medialibrary/mlbasemodel.cpp b/modules/gui/qt/medialibrary/mlbasemodel.cpp
index 63fb227c9b..9cc0df7391 100644
--- a/modules/gui/qt/medialibrary/mlbasemodel.cpp
+++ b/modules/gui/qt/medialibrary/mlbasemodel.cpp
@@ -21,6 +21,9 @@
 #include "medialib.hpp"
 #include <vlc_cxx_helpers.hpp>
 
+static constexpr ssize_t COUNT_UNINITIALIZED =
+    ListCache<std::unique_ptr<MLItem>>::COUNT_UNINITIALIZED;
+
 MLBaseModel::MLBaseModel(QObject *parent)
     : QAbstractListModel(parent)
     , m_ml(nullptr)
@@ -208,3 +211,182 @@ void MLBaseModel::unsetSortCriteria()
     endResetModel();
     emit sortCriteriaChanged();
 }
+
+MLSlidingWindowModel::MLSlidingWindowModel(QObject *parent)
+    : MLBaseModel(parent)
+{
+}
+
+int MLSlidingWindowModel::rowCount(const QModelIndex &parent) const
+{
+    if (parent.isValid())
+        return 0;
+
+    validateCache();
+
+    return m_cache->count();
+}
+
+void MLSlidingWindowModel::clear()
+{
+    invalidateCache();
+    emit countChanged( static_cast<unsigned int>(0) );
+}
+
+QVariant MLSlidingWindowModel::getIdForIndex(QVariant index) const
+{
+    MLItem* obj = nullptr;
+    if (index.canConvert<int>())
+        obj = item( index.toInt() );
+    else if ( index.canConvert<QModelIndex>() )
+        obj = item( index.value<QModelIndex>().row() );
+
+    if (!obj)
+        return {};
+
+    return QVariant::fromValue(obj->getId());
+}
+
+
+QVariantList MLSlidingWindowModel::getIdsForIndexes(QModelIndexList indexes) const
+{
+    QVariantList idList;
+    idList.reserve(indexes.length());
+    std::transform( indexes.begin(), indexes.end(),std::back_inserter(idList), [this](const QModelIndex& index) -> QVariant {
+        MLItem* obj = item( index.row() );
+        if (!obj)
+            return {};
+        return QVariant::fromValue(obj->getId());
+    });
+    return idList;
+}
+
+QVariantList MLSlidingWindowModel::getIdsForIndexes(QVariantList indexes) const
+{
+    QVariantList idList;
+
+    idList.reserve(indexes.length());
+    std::transform( indexes.begin(), indexes.end(),std::back_inserter(idList),
+                    [this](const QVariant& index) -> QVariant {
+        MLItem* obj = nullptr;
+        if (index.canConvert<int>())
+            obj = item( index.toInt() );
+        else if ( index.canConvert<QModelIndex>() )
+            obj = item( index.value<QModelIndex>().row() );
+
+        if (!obj)
+            return {};
+
+        return QVariant::fromValue(obj->getId());
+    });
+    return idList;
+}
+
+unsigned MLSlidingWindowModel::getCount() const
+{
+    if (!m_cache || m_cache->count() == COUNT_UNINITIALIZED)
+        return 0;
+    return static_cast<unsigned>(m_cache->count());
+}
+
+void MLSlidingWindowModel::validateCache() const
+{
+    if (m_cache)
+        return;
+
+    auto &threadPool = m_mediaLib->threadPool();
+    auto loader = createLoader();
+    m_cache.reset(new ListCache<std::unique_ptr<MLItem>>(threadPool, loader));
+    connect(&*m_cache, &BaseListCache::localSizeAboutToBeChanged,
+            this, &MLSlidingWindowModel::onLocalSizeAboutToBeChanged);
+    connect(&*m_cache, &BaseListCache::localSizeChanged,
+            this, &MLSlidingWindowModel::onLocalSizeChanged);
+    connect(&*m_cache, &BaseListCache::localDataChanged,
+            this, &MLSlidingWindowModel::onLocalDataChanged);
+
+    m_cache->initCount();
+}
+
+void MLSlidingWindowModel::invalidateCache()
+{
+    m_cache.reset();
+}
+
+MLItem *MLSlidingWindowModel::item(int signedidx) const
+{
+    validateCache();
+
+    ssize_t count = m_cache->count();
+    if (count == COUNT_UNINITIALIZED || signedidx < 0
+            || signedidx >= count)
+        return nullptr;
+
+    unsigned int idx = static_cast<unsigned int>(signedidx);
+    m_cache->refer(idx);
+
+    const std::unique_ptr<MLItem> *item = m_cache->get(idx);
+    if (!item)
+        /* Not in cache */
+        return nullptr;
+
+    /* Return raw pointer */
+    return item->get();
+}
+
+void MLSlidingWindowModel::onVlcMlEvent(const MLEvent &event)
+{
+    switch (event.i_type)
+    {
+        case VLC_ML_EVENT_MEDIA_THUMBNAIL_GENERATED:
+        {
+            if (event.media_thumbnail_generated.b_success) {
+                if (!m_cache)
+                    break;
+
+                ssize_t stotal = m_cache->count();
+                if (stotal == COUNT_UNINITIALIZED)
+                    break;
+
+                size_t total = static_cast<size_t>(stotal);
+                for (size_t i = 0; i < total; ++i)
+                {
+                    const std::unique_ptr<MLItem> *item = m_cache->get(i);
+                    if (!item)
+                        /* Only consider items available locally in cache */
+                        break;
+
+                    MLItem *localItem = item->get();
+                    if (localItem->getId().id == event.media_thumbnail_generated.i_media_id)
+                    {
+                        thumbnailUpdated(i);
+                        break;
+                    }
+                }
+            }
+            break;
+        }
+        default:
+            break;
+    }
+    MLBaseModel::onVlcMlEvent( event );
+}
+
+MLSlidingWindowModel::BaseLoader::BaseLoader(vlc_medialibrary_t *ml, MLItemId parent, QString searchPattern,
+                                             vlc_ml_sorting_criteria_t sort, bool sort_desc)
+    : m_ml(ml)
+    , m_parent(parent)
+    , m_searchPattern(searchPattern)
+    , m_sort(sort)
+    , m_sort_desc(sort_desc)
+{
+}
+
+MLSlidingWindowModel::BaseLoader::BaseLoader(const MLSlidingWindowModel &model)
+    : BaseLoader(model.m_ml, model.m_parent, model.m_search_pattern, model.m_sort, model.m_sort_desc)
+{
+}
+
+MLQueryParams MLSlidingWindowModel::BaseLoader::getParams(size_t index, size_t count) const
+{
+    return { m_searchPattern.toUtf8(), m_sort, m_sort_desc, index, count };
+}
diff --git a/modules/gui/qt/medialibrary/mlbasemodel.hpp b/modules/gui/qt/medialibrary/mlbasemodel.hpp
index 16a719e812..eac3f3a811 100644
--- a/modules/gui/qt/medialibrary/mlbasemodel.hpp
+++ b/modules/gui/qt/medialibrary/mlbasemodel.hpp
@@ -134,191 +134,34 @@ protected:
 class MLSlidingWindowModel : public MLBaseModel
 {
 public:
-    static constexpr ssize_t COUNT_UNINITIALIZED =
-        ListCache<std::unique_ptr<MLItem>>::COUNT_UNINITIALIZED;
+    MLSlidingWindowModel(QObject* parent = nullptr);
 
-    MLSlidingWindowModel(QObject* parent = nullptr)
-        : MLBaseModel(parent)
-    {
-    }
-
-    int rowCount(const QModelIndex &parent = {}) const override
-    {
-        if (parent.isValid())
-            return 0;
-
-        validateCache();
+    int rowCount(const QModelIndex &parent = {}) const override;
 
-        return m_cache->count();
-    }
+    void clear() override;
 
-    void clear() override
-    {
-        invalidateCache();
-        emit countChanged( static_cast<unsigned int>(0) );
-    }
+    QVariant getIdForIndex( QVariant index ) const override;
+    QVariantList getIdsForIndexes( QModelIndexList indexes ) const override;
+    QVariantList getIdsForIndexes( QVariantList indexes ) const override;
 
-
-    virtual QVariant getIdForIndex( QVariant index ) const override
-    {
-        MLItem* obj = nullptr;
-        if (index.canConvert<int>())
-            obj = item( index.toInt() );
-        else if ( index.canConvert<QModelIndex>() )
-            obj = item( index.value<QModelIndex>().row() );
-
-        if (!obj)
-            return {};
-
-        return QVariant::fromValue(obj->getId());
-    }
-
-    virtual QVariantList getIdsForIndexes( QModelIndexList indexes ) const override
-    {
-        QVariantList idList;
-        idList.reserve(indexes.length());
-        std::transform( indexes.begin(), indexes.end(),std::back_inserter(idList), [this](const QModelIndex& index) -> QVariant {
-            MLItem* obj = item( index.row() );
-            if (!obj)
-                return {};
-            return QVariant::fromValue(obj->getId());
-        });
-        return idList;
-    }
-
-    virtual QVariantList getIdsForIndexes( QVariantList indexes ) const override
-    {
-        QVariantList idList;
-
-        idList.reserve(indexes.length());
-        std::transform( indexes.begin(), indexes.end(),std::back_inserter(idList), [this](const QVariant& index) -> QVariant {
-            MLItem* obj = nullptr;
-            if (index.canConvert<int>())
-                obj = item( index.toInt() );
-            else if ( index.canConvert<QModelIndex>() )
-                obj = item( index.value<QModelIndex>().row() );
-
-            if (!obj)
-                return {};
-
-            return QVariant::fromValue(obj->getId());
-        });
-        return idList;
-    }
-
-    unsigned int getCount() const override {
-        if (!m_cache || m_cache->count() == COUNT_UNINITIALIZED)
-            return 0;
-        return static_cast<unsigned int>(m_cache->count());
-    }
+    unsigned getCount() const override;
 
 protected:
     virtual ListCacheLoader<std::unique_ptr<MLItem>> *createLoader() const = 0;
 
-    void validateCache() const
-    {
-        if (m_cache)
-            return;
-
-        auto &threadPool = m_mediaLib->threadPool();
-        auto loader = createLoader();
-        m_cache.reset(new ListCache<std::unique_ptr<MLItem>>(threadPool, loader));
-        connect(&*m_cache, &BaseListCache::localSizeAboutToBeChanged,
-                this, &MLSlidingWindowModel::onLocalSizeAboutToBeChanged);
-        connect(&*m_cache, &BaseListCache::localSizeChanged,
-                this, &MLSlidingWindowModel::onLocalSizeChanged);
-        connect(&*m_cache, &BaseListCache::localDataChanged,
-                this, &MLSlidingWindowModel::onLocalDataChanged);
-
-        m_cache->initCount();
-    }
-
-    void invalidateCache()
-    {
-        m_cache.reset();
-    }
-
-    MLItem* item(int signedidx) const
-    {
-        validateCache();
-
-        ssize_t count = m_cache->count();
-        if (count == COUNT_UNINITIALIZED || signedidx < 0
-                || signedidx >= count)
-            return nullptr;
-
-        unsigned int idx = static_cast<unsigned int>(signedidx);
-        m_cache->refer(idx);
-
-        const std::unique_ptr<MLItem> *item = m_cache->get(idx);
-        if (!item)
-            /* Not in cache */
-            return nullptr;
-
-        /* Return raw pointer */
-        return item->get();
-    }
-
-    virtual void onVlcMlEvent(const MLEvent &event) override
-    {
-        switch (event.i_type)
-        {
-            case VLC_ML_EVENT_MEDIA_THUMBNAIL_GENERATED:
-            {
-                if (event.media_thumbnail_generated.b_success) {
-                    if (!m_cache)
-                        break;
-
-                    ssize_t stotal = m_cache->count();
-                    if (stotal == COUNT_UNINITIALIZED)
-                        break;
-
-                    size_t total = static_cast<size_t>(stotal);
-                    for (size_t i = 0; i < total; ++i)
-                    {
-                        const std::unique_ptr<MLItem> *item = m_cache->get(i);
-                        if (!item)
-                            /* Only consider items available locally in cache */
-                            break;
-
-                        MLItem *localItem = item->get();
-                        if (localItem->getId().id == event.media_thumbnail_generated.i_media_id)
-                        {
-                            thumbnailUpdated(i);
-                            break;
-                        }
-                    }
-                }
-                break;
-            }
-            default:
-                break;
-        }
-        MLBaseModel::onVlcMlEvent( event );
-    }
+    void validateCache() const;
+    void invalidateCache();
+    MLItem* item(int signedidx) const;
+    void onVlcMlEvent(const MLEvent &event) override;
 
     /* Data loader for the cache */
     struct BaseLoader : public ListCacheLoader<std::unique_ptr<MLItem>>
     {
         BaseLoader(vlc_medialibrary_t *ml, MLItemId parent, QString searchPattern,
-                   vlc_ml_sorting_criteria_t sort, bool sort_desc)
-            : m_ml(ml)
-            , m_parent(parent)
-            , m_searchPattern(searchPattern)
-            , m_sort(sort)
-            , m_sort_desc(sort_desc)
-        {
-        }
-
-        BaseLoader(const MLSlidingWindowModel &model)
-            : BaseLoader(model.m_ml, model.m_parent, model.m_search_pattern, model.m_sort, model.m_sort_desc)
-        {
-        }
-
-        MLQueryParams getParams(size_t index = 0, size_t count = 0) const
-        {
-            return { m_searchPattern.toUtf8(), m_sort, m_sort_desc, index, count };
-        }
+                   vlc_ml_sorting_criteria_t sort, bool sort_desc);
+        BaseLoader(const MLSlidingWindowModel &model);
+
+        MLQueryParams getParams(size_t index = 0, size_t count = 0) const;
 
     protected:
         vlc_medialibrary_t *m_ml;



More information about the vlc-commits mailing list