[vlc-devel] [PATCH 03/16] qml: enqueue in playlist using selected index

Pierre Lamot pierre at videolabs.io
Wed May 27 17:06:44 CEST 2020


  this allows decoupling from the DelegateModel
---
 .../gui/qt/medialibrary/qml/MusicAlbums.qml   |  7 ++--
 .../gui/qt/medialibrary/qml/MusicGenres.qml   |  5 +--
 .../qml/MusicTrackListDisplay.qml             |  8 +----
 .../gui/qt/medialibrary/qml/VideoDisplay.qml  |  5 +--
 .../qt/medialibrary/qml/VideoListDisplay.qml  |  9 ++----
 modules/gui/qt/network/networkmediamodel.cpp  | 32 ++++++++++++-------
 .../qt/network/qml/NetworkBrowseDisplay.qml   |  7 ++--
 .../qml/NetworksSectionSelectableDM.qml       | 22 ++-----------
 .../qt/util/qml/SelectableDelegateModel.qml   |  9 ++++++
 .../qt/widgets/qml/KeyNavigableTableView.qml  |  4 +--
 10 files changed, 45 insertions(+), 63 deletions(-)

diff --git a/modules/gui/qt/medialibrary/qml/MusicAlbums.qml b/modules/gui/qt/medialibrary/qml/MusicAlbums.qml
index a6f6785414..8d35835f7e 100644
--- a/modules/gui/qt/medialibrary/qml/MusicAlbums.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicAlbums.qml
@@ -117,12 +117,9 @@ Widgets.NavigableFocusScope {
 
         function actionAtIndex(index) {
             if (delegateModelId.selectedGroup.count > 1) {
-                var list = []
-                for (var i = 0; i < delegateModelId.selectedGroup.count; i++)
-                    list.push(delegateModelId.selectedGroup.get(i).model.id)
-                medialib.addAndPlay( list )
+                medialib.addAndPlay( model.getIdsForIndexes( delegateModelId.selectedIndexes() ) )
             } else {
-                medialib.addAndPlay( delegateModelId.items.get(index).model.id )
+                medialib.addAndPlay( model.getIdsForIndexes([index]) )
             }
         }
     }
diff --git a/modules/gui/qt/medialibrary/qml/MusicGenres.qml b/modules/gui/qt/medialibrary/qml/MusicGenres.qml
index 49f0bd493b..ca06179e5c 100644
--- a/modules/gui/qt/medialibrary/qml/MusicGenres.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicGenres.qml
@@ -133,10 +133,7 @@ Widgets.NavigableFocusScope {
 
         function actionAtIndex(index) {
             if (delegateModelId.selectedGroup.count > 1) {
-                var list = []
-                for (var i = 0; i < delegateModelId.selectedGroup.count; i++)
-                    list.push(delegateModelId.selectedGroup.get(i).model.id)
-                medialib.addAndPlay( list )
+                medialib.addAndPlay(model.getIdsForIndexes(delegateModelId.selectedIndexes()))
             } else if (delegateModelId.selectedGroup.count === 1) {
                 showAlbumView(delegateModelId.selectedGroup.get(0).model.id, delegateModelId.selectedGroup.get(0).model.name)
             }
diff --git a/modules/gui/qt/medialibrary/qml/MusicTrackListDisplay.qml b/modules/gui/qt/medialibrary/qml/MusicTrackListDisplay.qml
index be67b9c069..17fc26f7f8 100644
--- a/modules/gui/qt/medialibrary/qml/MusicTrackListDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicTrackListDisplay.qml
@@ -83,11 +83,5 @@ Widgets.KeyNavigableTableView {
         }
     }
 
-    onActionForSelection: {
-        var list = []
-        for (var i = 0; i < selection.count; i++ ) {
-            list.push(selection.get(i).model.id)
-        }
-        medialib.addAndPlay(list)
-    }
+    onActionForSelection:  medialib.addAndPlay(model.getIdsForIndexes( selection ))
 }
diff --git a/modules/gui/qt/medialibrary/qml/VideoDisplay.qml b/modules/gui/qt/medialibrary/qml/VideoDisplay.qml
index 23c370b7ea..d0fd13fe1c 100644
--- a/modules/gui/qt/medialibrary/qml/VideoDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/VideoDisplay.qml
@@ -131,10 +131,7 @@ Widgets.NavigableFocusScope {
         }
 
         function actionAtIndex(index) {
-            var list = []
-            for (var i = 0; i < videosDelegate.selectedGroup.count; i++)
-                list.push(videosDelegate.selectedGroup.get(i).model.id)
-            medialib.addAndPlay( list )
+            medialib.addAndPlay( videoModel.getIdsForIndexes( videosDelegate.selectedIndexes() ) )
             history.push(["player"])
         }
     }
diff --git a/modules/gui/qt/medialibrary/qml/VideoListDisplay.qml b/modules/gui/qt/medialibrary/qml/VideoListDisplay.qml
index aabfc6b579..8528b6f108 100644
--- a/modules/gui/qt/medialibrary/qml/VideoListDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/VideoListDisplay.qml
@@ -135,13 +135,8 @@ Widgets.KeyNavigableTableView {
     headerColor: VLCStyle.colors.bg
     spacing: VLCStyle.margin_small
 
-    onActionForSelection: {
-        var list = []
-        for (var i = 0; i < selection.count; i++ ) {
-            list.push(selection.get(i).model.id)
-        }
-        medialib.addAndPlay(list)
-    }
+    onActionForSelection: medialib.addAndPlay(model.getIdsForIndexes( selection ))
+
     navigationLeft:  function(index) {
         if (isFocusOnContextButton )
             isFocusOnContextButton = false
diff --git a/modules/gui/qt/network/networkmediamodel.cpp b/modules/gui/qt/network/networkmediamodel.cpp
index 1e44d7476d..1bfa2cfefc 100644
--- a/modules/gui/qt/network/networkmediamodel.cpp
+++ b/modules/gui/qt/network/networkmediamodel.cpp
@@ -198,11 +198,16 @@ bool NetworkMediaModel::addToPlaylist(const QVariantList &itemIdList)
     bool ret = false;
     for (const QVariant& varValue: itemIdList)
     {
+        int index = -1;
+
         if (varValue.canConvert<int>())
-        {
-            auto index = varValue.value<int>();
-            ret |= addToPlaylist(index);
-        }
+            index = varValue.value<int>();
+        else if (varValue.canConvert<QModelIndex>())
+            index = varValue.value<QModelIndex>().row();
+        else
+            continue;
+
+        ret |= addToPlaylist(index);
     }
     return ret;
 }
@@ -224,14 +229,19 @@ bool NetworkMediaModel::addAndPlay(const QVariantList& itemIdList)
     bool ret = false;
     for (const QVariant& varValue: itemIdList)
     {
+        int index = -1;
+
         if (varValue.canConvert<int>())
-        {
-            auto index = varValue.value<int>();
-            if (!ret)
-                ret |= addAndPlay(index);
-            else
-                ret |= addToPlaylist(index);
-        }
+            index = varValue.value<int>();
+        else if (varValue.canConvert<QModelIndex>())
+            index = varValue.value<QModelIndex>().row();
+        else
+            continue;
+
+        if (!ret)
+            ret |= addAndPlay(index);
+        else
+            ret |= addToPlaylist(index);
     }
     return ret;
 }
diff --git a/modules/gui/qt/network/qml/NetworkBrowseDisplay.qml b/modules/gui/qt/network/qml/NetworkBrowseDisplay.qml
index 305fed3307..effe203786 100644
--- a/modules/gui/qt/network/qml/NetworkBrowseDisplay.qml
+++ b/modules/gui/qt/network/qml/NetworkBrowseDisplay.qml
@@ -66,13 +66,14 @@ Widgets.NavigableFocusScope {
             property var modelActions: {
                 "play": function() {
                     if (delegateModelId) {
-                        delegateModelId.playSelection()
+                        providerModel.addAndPlay(delegateModelId.selectedIndexes())
                     }
                     contextMenu.close()
                 },
                 "enqueue": function() {
-                    if (delegateModelId)
-                        delegateModelId.enqueueSelection()
+                    if (delegateModelId) {
+                        providerModel.addToPlaylist(delegateModelId.selectedIndexes())
+                    }
                     contextMenu.close()
                 },
                 "index": function(index) {
diff --git a/modules/gui/qt/network/qml/NetworksSectionSelectableDM.qml b/modules/gui/qt/network/qml/NetworksSectionSelectableDM.qml
index 5d3da53551..8ca62b32ad 100644
--- a/modules/gui/qt/network/qml/NetworksSectionSelectableDM.qml
+++ b/modules/gui/qt/network/qml/NetworksSectionSelectableDM.qml
@@ -99,33 +99,15 @@ Util.SelectableDelegateModel {
         }
     }
 
-    function playSelection() {
-        var list = []
-        for (var i = 0; i < delegateModel.selectedGroup.count; i++) {
-            var index = delegateModel.selectedGroup.get(i).itemsIndex;
-            list.push(index)
-        }
-        delegateModel.model.addAndPlay( list )
-    }
-
-    function enqueueSelection() {
-        var list = []
-        for (var i = 0; i < delegateModel.selectedGroup.count; i++) {
-            var index = delegateModel.selectedGroup.get(i).itemsIndex;
-            list.push(index)
-        }
-        delegateModel.model.addToPlaylist( list )
-    }
-
     function actionAtIndex(index) {
         if ( delegateModel.selectedGroup.count > 1 ) {
-            playSelection()
+            model.addAndPlay( delegateModel.selectedIndexes() )
         } else {
             if (delegateModel.items.get(index).model.type === NetworkMediaModel.TYPE_DIRECTORY
                     || delegateModel.items.get(index).model.type === NetworkMediaModel.TYPE_NODE)  {
                 history.push(["mc", "network", { tree: delegateModel.items.get(index).model.tree }]);
             } else {
-                playSelection()
+                model.addAndPlay( delegateModel.selectedIndexes() )
             }
         }
     }
diff --git a/modules/gui/qt/util/qml/SelectableDelegateModel.qml b/modules/gui/qt/util/qml/SelectableDelegateModel.qml
index 6a18d39611..fa545f04eb 100644
--- a/modules/gui/qt/util/qml/SelectableDelegateModel.qml
+++ b/modules/gui/qt/util/qml/SelectableDelegateModel.qml
@@ -49,6 +49,15 @@ DelegateModel {
         delegateModel.items.addGroups(0, delegateModel.items.count, ["selected"])
     }
 
+    function selectedIndexes() {
+        var list = []
+        for (var i = 0; i < delegateModel.selectedGroup.count; i++) {
+            var index = delegateModel.selectedGroup.get(i).itemsIndex;
+            list.push(index)
+        }
+        return list
+    }
+
     function updateSelection( keymodifiers, oldIndex, newIndex ) {
         if ((keymodifiers & Qt.ShiftModifier)) {
             if ( shiftIndex === oldIndex) {
diff --git a/modules/gui/qt/widgets/qml/KeyNavigableTableView.qml b/modules/gui/qt/widgets/qml/KeyNavigableTableView.qml
index c96013f719..c5a7247ceb 100644
--- a/modules/gui/qt/widgets/qml/KeyNavigableTableView.qml
+++ b/modules/gui/qt/widgets/qml/KeyNavigableTableView.qml
@@ -94,7 +94,7 @@ NavigableFocusScope {
                     }
 
                     onDoubleClicked: {
-                        actionForSelection(delegateModel.selectedGroup)
+                        actionForSelection(delegateModel.selectedIndexes())
                     }
 
                         Row {
@@ -213,7 +213,7 @@ NavigableFocusScope {
 
         onSelectAll: delegateModel.selectAll()
         onSelectionUpdated: delegateModel.updateSelection( keyModifiers, oldIndex, newIndex )
-        onActionAtIndex: root.actionForSelection( delegateModel.selectedGroup )
+        onActionAtIndex: root.actionForSelection( delegateModel.selectedIndexes() )
 
         navigationParent: root
     }
-- 
2.25.1



More information about the vlc-devel mailing list