[vlc-commits] [Git][videolan/vlc][master] 3 commits: qml/mlrecentsvideomodel: Update implementation to inherit from MLVideoModel

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Tue Dec 14 18:01:29 UTC 2021



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
3f0379ee by Benjamin Arnaud at 2021-12-14T17:48:01+00:00
qml/mlrecentsvideomodel: Update implementation to inherit from MLVideoModel

- - - - -
447c4e0b by Benjamin Arnaud at 2021-12-14T17:48:01+00:00
qt/qml_menu_wrapper: Add 'player-options' support

- - - - -
914a05ff by Benjamin Arnaud at 2021-12-14T17:48:01+00:00
qml/VideoDisplayRecentVideos: Add contextual menu support

fix #26275

- - - - -


6 changed files:

- modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
- modules/gui/qt/medialibrary/mlrecentsvideomodel.hpp
- modules/gui/qt/medialibrary/mlvideomodel.cpp
- modules/gui/qt/medialibrary/mlvideomodel.hpp
- modules/gui/qt/medialibrary/qml/VideoDisplayRecentVideos.qml
- modules/gui/qt/menus/qml_menu_wrapper.cpp


Changes:

=====================================
modules/gui/qt/medialibrary/mlrecentsvideomodel.cpp
=====================================
@@ -18,147 +18,101 @@
 
 #include "mlrecentsvideomodel.hpp"
 
-namespace {
-
-enum Role {
-    VIDEO_ID = Qt::UserRole + 1,
-    VIDEO_IS_NEW,
-    VIDEO_TITLE,
-    VIDEO_THUMBNAIL,
-    VIDEO_DURATION,
-    VIDEO_PROGRESS,
-    VIDEO_PLAYCOUNT,
-    VIDEO_RESOLUTION,
-    VIDEO_CHANNEL,
-    VIDEO_MRL,
-    VIDEO_VIDEO_TRACK,
-    VIDEO_AUDIO_TRACK,
-};
+// Ctor / dtor
 
-}
+MLRecentsVideoModel::MLRecentsVideoModel(QObject * parent) : MLVideoModel(parent) {}
+
+// Protected MLBaseModel implementation
 
-MLRecentsVideoModel::MLRecentsVideoModel( QObject* parent )
-    : MLBaseModel( parent )
+ListCacheLoader<std::unique_ptr<MLItem>> * MLRecentsVideoModel::createLoader() const
+/* override */
 {
+    return new Loader(*this, m_numberOfItemsToShow);
 }
 
-QVariant MLRecentsVideoModel::itemRoleData(MLItem *item , const int role) const
-{
-    const auto video = static_cast<MLVideo *>( item );
-    if ( video == nullptr )
-        return {};
+// Private functions
 
-    switch ( role )
-    {
-        case VIDEO_ID:
-            return QVariant::fromValue( video->getId() );
-        case VIDEO_IS_NEW:
-            return QVariant::fromValue( video->isNew() );
-        case VIDEO_TITLE:
-            return QVariant::fromValue( video->getTitle() );
-        case VIDEO_THUMBNAIL:
-            return QVariant::fromValue( video->getThumbnail() );
-        case VIDEO_DURATION:
-            return QVariant::fromValue( video->getDuration() );
-        case VIDEO_PROGRESS:
-            return QVariant::fromValue( video->getProgress() );
-        case VIDEO_PLAYCOUNT:
-            return QVariant::fromValue( video->getPlayCount() );
-        case VIDEO_RESOLUTION:
-            return QVariant::fromValue( video->getResolutionName() );
-        case VIDEO_CHANNEL:
-            return QVariant::fromValue( video->getChannel() );
-        case VIDEO_MRL:
-            return QVariant::fromValue( video->getMRL() );
-        case VIDEO_VIDEO_TRACK:
-            return QVariant::fromValue( video->getVideoDesc() );
-        case VIDEO_AUDIO_TRACK:
-            return QVariant::fromValue( video->getAudioDesc() );
-        default:
-            return {};
-    }
+int MLRecentsVideoModel::getNumberOfItemsToShow()
+{
+    return m_numberOfItemsToShow;
 }
 
-QHash<int, QByteArray> MLRecentsVideoModel::roleNames() const
+void MLRecentsVideoModel::setNumberOfItemsToShow(int number)
 {
-    return {
-        { VIDEO_ID, "id" },
-        { VIDEO_IS_NEW, "isNew" },
-        { VIDEO_TITLE, "title" },
-        { VIDEO_THUMBNAIL, "thumbnail" },
-        { VIDEO_DURATION, "duration" },
-        { VIDEO_PROGRESS, "progress" },
-        { VIDEO_PLAYCOUNT, "playcount" },
-        { VIDEO_RESOLUTION, "resolution_name" },
-        { VIDEO_CHANNEL, "channel" },
-        { VIDEO_MRL, "mrl" },
-        { VIDEO_AUDIO_TRACK, "audioDesc" },
-        { VIDEO_VIDEO_TRACK, "videoDesc" },
-    };
+    m_numberOfItemsToShow = number;
+
+    invalidateCache();
 }
 
-void MLRecentsVideoModel::onVlcMlEvent( const MLEvent &event )
+// Private MLVideoModel reimplementation
+
+void MLRecentsVideoModel::onVlcMlEvent(const MLEvent & event) /* override */
 {
-    switch ( event.i_type )
+    switch (event.i_type)
     {
         case VLC_ML_EVENT_HISTORY_CHANGED:
             emit resetRequested();
             break;
-        case VLC_ML_EVENT_MEDIA_ADDED:
-        case VLC_ML_EVENT_MEDIA_UPDATED:
-        case VLC_ML_EVENT_MEDIA_DELETED:
-            m_need_reset = true;
-            break;
         default:
             break;
     }
-    MLBaseModel::onVlcMlEvent( event );
-}
-void MLRecentsVideoModel::setNumberOfItemsToShow( int n ){
-    numberOfItemsToShow = n;
-    invalidateCache();
-}
-int MLRecentsVideoModel::getNumberOfItemsToShow(){
-    return numberOfItemsToShow;
-}
 
-ListCacheLoader<std::unique_ptr<MLItem>> *
-MLRecentsVideoModel::createLoader() const
-{
-    return new Loader(*this, numberOfItemsToShow);
+    MLVideoModel::onVlcMlEvent(event);
 }
 
-size_t MLRecentsVideoModel::Loader::count() const
+// Loader
+
+MLRecentsVideoModel::Loader::Loader(const MLRecentsVideoModel & model, int numberOfItemsToShow)
+    : MLBaseModel::BaseLoader(model)
+    , m_numberOfItemsToShow(numberOfItemsToShow) {}
+
+size_t MLRecentsVideoModel::Loader::count() const /* override */
 {
     MLQueryParams params = getParams();
+
     auto queryParams = params.toCQueryParams();
 
-    size_t realCount = vlc_ml_count_history_by_type( m_ml, &queryParams, VLC_ML_MEDIA_TYPE_VIDEO );
+    size_t realCount = vlc_ml_count_history_by_type(m_ml, &queryParams, VLC_ML_MEDIA_TYPE_VIDEO);
+
     if (m_numberOfItemsToShow >= 0)
-        return std::min( realCount, static_cast<size_t>(m_numberOfItemsToShow) );
+    {
+        return std::min(realCount, static_cast<size_t> (m_numberOfItemsToShow));
+    }
+
     return realCount;
 }
 
 std::vector<std::unique_ptr<MLItem>>
-MLRecentsVideoModel::Loader::load(size_t index, size_t count) const
+MLRecentsVideoModel::Loader::load(size_t index, size_t count) const /* override */
 {
     MLQueryParams params = getParams(index, count);
+
     auto queryParams = params.toCQueryParams();
 
     std::vector<std::unique_ptr<MLItem>> res;
+
     if (m_numberOfItemsToShow >= 0)
     {
-        if (queryParams.i_offset <= static_cast<uint32_t>(m_numberOfItemsToShow))
-           queryParams.i_nbResults = static_cast<uint32_t>(m_numberOfItemsToShow) - queryParams.i_offset;
-        else
+        uint32_t count = static_cast<uint32_t> (m_numberOfItemsToShow);
+
+        if (queryParams.i_offset > count)
             return res;
+
+        queryParams.i_nbResults = count - queryParams.i_offset;
     }
 
-    ml_unique_ptr<vlc_ml_media_list_t> media_list{ vlc_ml_list_history_by_type(
-                m_ml, &queryParams, VLC_ML_MEDIA_TYPE_VIDEO ) };
-    if ( media_list == nullptr )
+    ml_unique_ptr<vlc_ml_media_list_t> media_list
+    {
+        vlc_ml_list_history_by_type(m_ml, &queryParams, VLC_ML_MEDIA_TYPE_VIDEO)
+    };
+
+    if (media_list == nullptr)
         return {};
-    for( vlc_ml_media_t &media: ml_range_iterate<vlc_ml_media_t>( media_list ) )
-            res.emplace_back( std::make_unique<MLVideo>( m_ml, &media ) );
+
+    for (const vlc_ml_media_t & media : ml_range_iterate<vlc_ml_media_t>(media_list))
+    {
+        res.emplace_back( std::make_unique<MLVideo>(m_ml, &media));
+    }
+
     return res;
 }


=====================================
modules/gui/qt/medialibrary/mlrecentsvideomodel.hpp
=====================================
@@ -23,51 +23,40 @@
 #include "config.h"
 #endif
 
-#include <vlc_common.h>
-#include <vlc_media_library.h>
+#include "mlvideomodel.hpp"
 
-#include "mlbasemodel.hpp"
-#include "mlvideo.hpp"
-
-#include <QObject>
-
-class MLRecentsVideoModel : public MLBaseModel
+class MLRecentsVideoModel : public MLVideoModel
 {
     Q_OBJECT
-    Q_PROPERTY(int numberOfItemsToShow READ getNumberOfItemsToShow WRITE setNumberOfItemsToShow FINAL)
+
+    Q_PROPERTY(int numberOfItemsToShow READ getNumberOfItemsToShow
+               WRITE setNumberOfItemsToShow FINAL)
 
 public:
-    explicit MLRecentsVideoModel( QObject* parent = nullptr );
+    explicit MLRecentsVideoModel(QObject * parent = nullptr);
+
     virtual ~MLRecentsVideoModel() = default;
 
-    QHash<int, QByteArray> roleNames() const override;
-    int numberOfItemsToShow = 10;
+protected: // MLBaseModel implementation
+    ListCacheLoader<std::unique_ptr<MLItem>> * createLoader() const override;
 
-protected:
-    QVariant itemRoleData( MLItem *item, int role ) const override;
+private: // Functions
+    int  getNumberOfItemsToShow();
+    void setNumberOfItemsToShow(int number);
 
-    ListCacheLoader<std::unique_ptr<MLItem>> *createLoader() const override;
+protected: // MLVideoModel reimplementation
+    void onVlcMlEvent(const MLEvent & event) override;
 
-private:
-    vlc_ml_sorting_criteria_t roleToCriteria( int /* role */ ) const override{
-        return VLC_ML_SORTING_DEFAULT;
-    }
-    vlc_ml_sorting_criteria_t nameToCriteria( QByteArray /* name */ ) const override{
-        return VLC_ML_SORTING_DEFAULT;
-    }
-    virtual void onVlcMlEvent( const MLEvent &event ) override;
-    void setNumberOfItemsToShow(int);
-    int getNumberOfItemsToShow();
+private: // Variables
+    int m_numberOfItemsToShow = 10;
 
+private:
     struct Loader : public BaseLoader
     {
-        Loader(const MLRecentsVideoModel &model, int numberOfItemsToShow)
-            : BaseLoader(model)
-            , m_numberOfItemsToShow(numberOfItemsToShow)
-        {
-        }
+        Loader(const MLRecentsVideoModel & model, int numberOfItemsToShow);
 
         size_t count() const override;
+
         std::vector<std::unique_ptr<MLItem>> load(size_t index, size_t count) const override;
 
     private:


=====================================
modules/gui/qt/medialibrary/mlvideomodel.cpp
=====================================
@@ -133,6 +133,19 @@ QByteArray MLVideoModel::criteriaToName(vlc_ml_sorting_criteria_t criteria) cons
     return M_names_to_criteria.key(criteria, "");
 }
 
+void MLVideoModel::thumbnailUpdated(int idx)
+{
+    emit dataChanged(index(idx), index(idx), {VIDEO_THUMBNAIL});
+}
+
+ListCacheLoader<std::unique_ptr<MLItem>> *
+MLVideoModel::createLoader() const
+{
+    return new Loader(*this);
+}
+
+// Protected MLBaseModel reimplementation
+
 void MLVideoModel::onVlcMlEvent(const MLEvent &event)
 {
     switch (event.i_type)
@@ -148,17 +161,6 @@ void MLVideoModel::onVlcMlEvent(const MLEvent &event)
     MLBaseModel::onVlcMlEvent( event );
 }
 
-void MLVideoModel::thumbnailUpdated(int idx)
-{
-    emit dataChanged(index(idx), index(idx), {VIDEO_THUMBNAIL});
-}
-
-ListCacheLoader<std::unique_ptr<MLItem>> *
-MLVideoModel::createLoader() const
-{
-    return new Loader(*this);
-}
-
 size_t MLVideoModel::Loader::count() const /* override */
 {
     vlc_ml_query_params_t params = getParams().toCQueryParams();


=====================================
modules/gui/qt/medialibrary/mlvideomodel.hpp
=====================================
@@ -67,10 +67,12 @@ protected:
 
     ListCacheLoader<std::unique_ptr<MLItem>> *createLoader() const override;
 
+protected: // MLBaseModel reimplementation
+    virtual void onVlcMlEvent( const MLEvent &event ) override;
+
 private:
     vlc_ml_sorting_criteria_t roleToCriteria(int role) const override;
     vlc_ml_sorting_criteria_t nameToCriteria(QByteArray name) const override;
-    virtual void onVlcMlEvent( const MLEvent &event ) override;
     virtual void thumbnailUpdated( int ) override;
 
     static QHash<QByteArray, vlc_ml_sorting_criteria_t> M_names_to_criteria;


=====================================
modules/gui/qt/medialibrary/qml/VideoDisplayRecentVideos.qml
=====================================
@@ -32,11 +32,11 @@ FocusScope {
 
     // Properties
 
-    property Item focusItem: recentVideosListView
+    property Item focusItem: listView
 
     property int currentIndex: -1
 
-    property var model: undefined;
+    property var model: undefined
 
     // Properties
 
@@ -60,7 +60,7 @@ FocusScope {
     // Functions
 
     function setCurrentItemFocus(reason) {
-        recentVideosListView.setCurrentItemFocus(reason);
+        listView.setCurrentItemFocus(reason);
     }
 
     function _actionAtIndex(index, model, selectionModel) {
@@ -75,6 +75,12 @@ FocusScope {
         model: root.model
     }
 
+    VideoContextMenu {
+        id: contextMenu
+
+        model: root.model
+    }
+
     Column {
         id: recentVideosColumn
 
@@ -95,7 +101,7 @@ FocusScope {
         }
 
         Widgets.KeyNavigableListView {
-            id: recentVideosListView
+            id: listView
 
             width: parent.width
 
@@ -125,10 +131,11 @@ FocusScope {
             }
 
             delegate: VideoGridItem {
-                id: recentVideoGridItem
+                id: gridItem
 
                 x: selectedBorderWidth
                 y: selectedBorderWidth
+
                 pictureWidth: VLCStyle.gridCover_video_width
                 pictureHeight: VLCStyle.gridCover_video_height
 
@@ -137,12 +144,19 @@ FocusScope {
                 unselectedUnderlay: shadows.unselected
                 selectedUnderlay: shadows.selected
 
-                onItemDoubleClicked: recentVideoGridItem.play()
+                onItemDoubleClicked: gridItem.play()
 
                 onItemClicked: {
                     recentVideoSelection.updateSelection( modifier , root.model.currentIndex, index )
-                    recentVideosListView.currentIndex = index
-                    recentVideosListView.forceActiveFocus()
+                    listView.currentIndex = index
+                    listView.forceActiveFocus()
+                }
+
+                // NOTE: contextMenu.popup wants a list of indexes.
+                onContextMenuButtonClicked: {
+                    contextMenu.popup([root.model.index(index, 0)],
+                                      globalMousePos,
+                                      { "player-options": [":restore-playback-pos=2"] })
                 }
 
                 Connections {


=====================================
modules/gui/qt/menus/qml_menu_wrapper.cpp
=====================================
@@ -450,8 +450,8 @@ void VideoContextMenu::popup(const QModelIndexList& selected, QPoint pos, QVaria
 
     action = m_menu->addAction( qtr("Add and play") );
 
-    connect(action, &QAction::triggered, [ml, itemIdList]( ) {
-        ml->addAndPlay(itemIdList);
+    connect(action, &QAction::triggered, [ml, itemIdList, options]( ) {
+        ml->addAndPlay(itemIdList, options["player-options"].toStringList());
     });
 
     action = m_menu->addAction( qtr("Enqueue") );
@@ -465,8 +465,10 @@ void VideoContextMenu::popup(const QModelIndexList& selected, QPoint pos, QVaria
     });
 
     action = m_menu->addAction( qtr("Play as audio") );
-    connect(action, &QAction::triggered, [ml, itemIdList]( ) {
-        ml->addAndPlay(itemIdList, {":no-video"});
+    connect(action, &QAction::triggered, [ml, itemIdList, options]( ) {
+        QStringList list = options["player-options"].toStringList();
+        list.prepend(":no-video");
+        ml->addAndPlay(itemIdList, list);
     });
 
     if (options.contains("information") && options["information"].type() == QVariant::Int) {



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/c580054e4f3a166f93c91f94416f97d89be83c16...914a05ff4c714a76f932cc3623019bf090946519

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/c580054e4f3a166f93c91f94416f97d89be83c16...914a05ff4c714a76f932cc3623019bf090946519
You're receiving this email because of your account on code.videolan.org.




More information about the vlc-commits mailing list