[vlc-devel] [PATCH 08/16] qml: no longer use Delegatemodel package as delegate

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


---
 .../gui/qt/medialibrary/qml/MusicAlbums.qml   |  73 +++++-----
 .../medialibrary/qml/MusicArtistsDisplay.qml  |  84 +++++------
 .../gui/qt/medialibrary/qml/MusicGenres.qml   |  93 ++++++------
 .../gui/qt/medialibrary/qml/VideoDisplay.qml  |   3 +-
 .../qt/medialibrary/qml/VideoListDisplay.qml  |   2 +-
 .../qt/widgets/qml/KeyNavigableTableView.qml  | 135 +++++++++---------
 modules/gui/qt/widgets/qml/ListItem.qml       |   2 -
 7 files changed, 188 insertions(+), 204 deletions(-)

diff --git a/modules/gui/qt/medialibrary/qml/MusicAlbums.qml b/modules/gui/qt/medialibrary/qml/MusicAlbums.qml
index b152d964ae..6ee01252a5 100644
--- a/modules/gui/qt/medialibrary/qml/MusicAlbums.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicAlbums.qml
@@ -36,8 +36,8 @@ Widgets.NavigableFocusScope {
         { text: i18n.qtr("Artist"),      criteria: "main_artist" },
     ]
 
-    property alias model: delegateModelId.model
-    property alias parentId: delegateModelId.parentId
+    property alias model: albumModelId
+    property alias parentId: albumModelId.parentId
     readonly property var currentIndex: view.currentItem.currentIndex
     //the index to "go to" when the view is loaded
     property var initialIndex: 0
@@ -80,41 +80,7 @@ Widgets.NavigableFocusScope {
             ml: medialib
         }
 
-
-        delegate: Package {
-            id: element
-
-            Widgets.ListItem {
-                id: listDelegate
-
-                Package.name: "list"
-
-                width: root.width
-                height: VLCStyle.icon_normal + VLCStyle.margin_small
-
-                selected: delegateModelId.isSelected(index)
-                Connections {
-                   target: delegateModelId
-                   onSelectionChanged: listDelegate.selected = delegateModelId.isSelected(index)
-                }
-
-                cover: Image {
-                    id: cover_obj
-                    fillMode: Image.PreserveAspectFit
-                    source: model.cover || VLCStyle.noArtAlbum
-                    sourceSize: Qt.size(width, height)
-                }
-                line1: (model.title || i18n.qtr("Unknown title"))+" ["+model.duration+"]"
-                line2: model.main_artist || i18n.qtr("Unknown artist")
-
-                onItemClicked : {
-                    delegateModelId.updateSelection( modifier, view.currentItem.currentIndex, index )
-                    view.currentItem.currentIndex = index
-                    this.forceActiveFocus()
-                }
-                onPlayClicked: medialib.addAndPlay( model.id )
-                onAddToPlaylistClicked : medialib.addToPlaylist( model.id )
-            }
+        delegate: Item {
         }
 
         onCountChanged: {
@@ -199,8 +165,37 @@ Widgets.NavigableFocusScope {
 
             spacing: VLCStyle.margin_xxxsmall
 
-            model: delegateModelId.parts.list
-            modelCount: delegateModelId.items.count
+            model: albumModelId
+
+            delegate: Widgets.ListItem {
+                id: listDelegate
+
+                width: root.width
+                height: VLCStyle.icon_normal + VLCStyle.margin_small
+
+                selected: delegateModelId.isSelected(index)
+                Connections {
+                   target: delegateModelId
+                   onSelectionChanged: listDelegate.selected = delegateModelId.isSelected(index)
+                }
+
+                cover: Image {
+                    id: cover_obj
+                    fillMode: Image.PreserveAspectFit
+                    source: model.cover || VLCStyle.noArtAlbum
+                    sourceSize: Qt.size(width, height)
+                }
+                line1: (model.title || i18n.qtr("Unknown title"))+" ["+model.duration+"]"
+                line2: model.main_artist || i18n.qtr("Unknown artist")
+
+                onItemClicked : {
+                    delegateModelId.updateSelection( modifier, view.currentItem.currentIndex, index )
+                    view.currentItem.currentIndex = index
+                    this.forceActiveFocus()
+                }
+                onPlayClicked: medialib.addAndPlay( model.id )
+                onAddToPlaylistClicked : medialib.addToPlaylist( model.id )
+            }
 
             onActionAtIndex: delegateModelId.actionAtIndex(index)
             onSelectAll: delegateModelId.selectAll()
diff --git a/modules/gui/qt/medialibrary/qml/MusicArtistsDisplay.qml b/modules/gui/qt/medialibrary/qml/MusicArtistsDisplay.qml
index d07731b514..877d75f243 100644
--- a/modules/gui/qt/medialibrary/qml/MusicArtistsDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicArtistsDisplay.qml
@@ -28,7 +28,7 @@ import "qrc:///style/"
 
 Widgets.NavigableFocusScope {
     id: root
-    property alias model: delegateModel.model
+    property alias model: artistModel
     property var sortModel: [
         { text: i18n.qtr("Alphabetic"),  criteria: "title" }
     ]
@@ -63,6 +63,7 @@ Widgets.NavigableFocusScope {
     Util.SelectableDelegateModel {
         id: delegateModel
         model: MLArtistModel {
+            id: artistModel
             ml: medialib
         }
 
@@ -75,43 +76,7 @@ Widgets.NavigableFocusScope {
             }
         }
 
-        delegate: Widgets.ListItem {
-            height: VLCStyle.icon_normal + VLCStyle.margin_small
-            width: artistList.width
-
-            property bool selected: delegateModel.isSelected(index)
-            Connections {
-               target: delegateModel
-               onSelectionChanged: selected = delegateModel.isSelected(index)
-            }
-
-            color: VLCStyle.colors.getBgColor(selected, this.hovered, this.activeFocus)
-
-            cover: Widgets.RoundImage {
-                source: model.cover || VLCStyle.noArtArtistSmall
-                height: VLCStyle.icon_normal
-                width: VLCStyle.icon_normal
-                radius: VLCStyle.icon_normal
-            }
-
-            line1: model.name || i18n.qtr("Unknown artist")
-
-            actionButtons: []
-
-            onItemClicked: {
-                artistId = model.id
-                delegateModel.updateSelection( modifier , artistList.currentIndex, index)
-                artistList.currentIndex = index
-                artistList.forceActiveFocus()
-            }
-
-            onItemDoubleClicked: {
-                if (keys === Qt.RightButton)
-                    medialib.addAndPlay( model.id )
-                else
-                    view.forceActiveFocus()
-            }
-        }
+        delegate: Item {}
 
         function actionAtIndex(index) {
             view.forceActiveFocus()
@@ -133,8 +98,7 @@ Widgets.NavigableFocusScope {
             height: parent.height
 
             spacing: 4
-            model: delegateModel
-            modelCount: delegateModel.items.count
+            model: artistModel
             currentIndex: -1
 
             focus: true
@@ -158,6 +122,44 @@ Widgets.NavigableFocusScope {
                     artistList.currentIndex = 0;
             }
 
+            delegate: Widgets.ListItem {
+                height: VLCStyle.icon_normal + VLCStyle.margin_small
+                width: artistList.width
+
+                property bool selected: delegateModel.isSelected(index)
+                Connections {
+                   target: delegateModel
+                   onSelectionChanged: selected = delegateModel.isSelected(index)
+                }
+
+                color: VLCStyle.colors.getBgColor(selected, this.hovered, this.activeFocus)
+
+                cover: Widgets.RoundImage {
+                    source: model.cover || VLCStyle.noArtArtistSmall
+                    height: VLCStyle.icon_normal
+                    width: VLCStyle.icon_normal
+                    radius: VLCStyle.icon_normal
+                }
+
+                line1: model.name || i18n.qtr("Unknown artist")
+
+                actionButtons: []
+
+                onItemClicked: {
+                    artistId = model.id
+                    delegateModel.updateSelection( modifier , artistList.currentIndex, index)
+                    artistList.currentIndex = index
+                    artistList.forceActiveFocus()
+                }
+
+                onItemDoubleClicked: {
+                    if (keys === Qt.RightButton)
+                        medialib.addAndPlay( model.id )
+                    else
+                        view.forceActiveFocus()
+                }
+            }
+
         }
 
         FocusScope {
@@ -176,7 +178,7 @@ Widgets.NavigableFocusScope {
                     id: artistBanner
                     width: albumSubView.width
                     artist: (artistList.currentIndex >= 0)
-                            ? delegateModel.items.get(artistList.currentIndex).model
+                            ? artistModel.getDataAt(artistList.currentIndex)
                             : ({})
                     navigationParent: root
                     navigationLeftItem: artistList
diff --git a/modules/gui/qt/medialibrary/qml/MusicGenres.qml b/modules/gui/qt/medialibrary/qml/MusicGenres.qml
index d88534601e..92ac0e02a9 100644
--- a/modules/gui/qt/medialibrary/qml/MusicGenres.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicGenres.qml
@@ -27,7 +27,7 @@ import "qrc:///style/"
 
 Widgets.NavigableFocusScope {
     id: root
-    property alias model: delegateModelId.model
+    property alias model: genreModel
     property var sortModel: [
         { text: i18n.qtr("Alphabetic"), criteria: "title" }
     ]
@@ -55,8 +55,8 @@ Widgets.NavigableFocusScope {
         }
     }
 
-    function showAlbumView( parent, name ) {
-        history.push([ "mc", "music", "genres", "albums", { parentId: parent, genreName: name } ])
+    function showAlbumView( m ) {
+        history.push([ "mc", "music", "genres", "albums", { parentId: m.id, genreName: m.name } ])
     }
 
     function resetFocus() {
@@ -91,44 +91,7 @@ Widgets.NavigableFocusScope {
             ml: medialib
         }
 
-        delegate: Package {
-            Widgets.ListItem {
-                id: listDelegate
-                Package.name: "list"
-
-                width: root.width
-                height: VLCStyle.icon_normal + VLCStyle.margin_small
-                selected: delegateModelId.isSelected(index)
-                Connections {
-                   target: delegateModelId
-                   onSelectionChanged: listDelegate.selected = delegateModelId.isSelected(index)
-                }
-
-
-                cover: Image {
-                    id: cover_obj
-                    fillMode: Image.PreserveAspectFit
-                    source: model.cover || VLCStyle.noArtAlbum
-                    sourceSize: Qt.size(width, height)
-                }
-
-                line1: (model.name || "Unknown genre")+" - "+model.nb_tracks+" tracks"
-
-                onItemClicked: {
-                    delegateModelId.updateSelection( modifier, view.currentItem.currentIndex, index )
-                    view.currentItem.currentIndex = index
-                    this.forceActiveFocus()
-                }
-                onPlayClicked: {
-                    medialib.addAndPlay( model.id )
-                }
-                onItemDoubleClicked: {
-                    root.showAlbumView(model.id, model.name)
-                }
-                onAddToPlaylistClicked: {
-                    medialib.addToPlaylist( model.id );
-                }
-            }
+        delegate: Item {
         }
 
         onCountChanged: {
@@ -141,7 +104,7 @@ Widgets.NavigableFocusScope {
             if (delegateModelId.selectedGroup.count > 1) {
                 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)
+                showAlbumView( delegateModelId.selectedGroup.get(0).model )
             }
         }
     }
@@ -178,11 +141,6 @@ Widgets.NavigableFocusScope {
                 image: model.cover || VLCStyle.noArtAlbum
                 title: model.name || "Unknown genre"
                 subtitle: ""
-                selected: delegateModelId.isSelected( index )
-                Connections {
-                   target: delegateModelId
-                   onSelectionChanged: selected = delegateModelId.isSelected(index)
-                }
 
                 onItemClicked: {
                     delegateModelId.updateSelection( modifier , view.currentItem.currentIndex, index)
@@ -190,7 +148,7 @@ Widgets.NavigableFocusScope {
                     view.currentItem.forceActiveFocus()
                 }
 
-                onItemDoubleClicked: root.showAlbumView(model.id, model.name)
+                onItemDoubleClicked: root.showAlbumView(model)
             }
 
             focus: true
@@ -214,7 +172,7 @@ Widgets.NavigableFocusScope {
         Widgets.KeyNavigableListView {
             id: listView_id
 
-            model: delegateModelId.parts.list
+            model: genreModel
 
             header: headerComponent
 
@@ -226,6 +184,43 @@ Widgets.NavigableFocusScope {
             onActionAtIndex: delegateModelId.actionAtIndex(index)
 
             navigationParent: root
+
+            delegate: Widgets.ListItem {
+                id: listDelegate
+
+                width: root.width
+                height: VLCStyle.icon_normal + VLCStyle.margin_small
+                selected: delegateModelId.isSelected(index)
+                Connections {
+                   target: delegateModelId
+                   onSelectionChanged: listDelegate.selected = delegateModelId.isSelected(index)
+                }
+
+
+                cover: Image {
+                    id: cover_obj
+                    fillMode: Image.PreserveAspectFit
+                    source: model.cover || VLCStyle.noArtAlbum
+                    sourceSize: Qt.size(width, height)
+                }
+
+                line1: (model.name || "Unknown genre")+" - "+model.nb_tracks+" tracks"
+
+                onItemClicked: {
+                    delegateModelId.updateSelection( modifier, view.currentItem.currentIndex, index )
+                    view.currentItem.currentIndex = index
+                    this.forceActiveFocus()
+                }
+                onPlayClicked: {
+                    medialib.addAndPlay( model.id )
+                }
+                onItemDoubleClicked: {
+                    root.showAlbumView(model)
+                }
+                onAddToPlaylistClicked: {
+                    medialib.addToPlaylist( model.id );
+                }
+            }
         }
     }
 
diff --git a/modules/gui/qt/medialibrary/qml/VideoDisplay.qml b/modules/gui/qt/medialibrary/qml/VideoDisplay.qml
index 5dc91a0028..db77718c09 100644
--- a/modules/gui/qt/medialibrary/qml/VideoDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/VideoDisplay.qml
@@ -119,8 +119,7 @@ Widgets.NavigableFocusScope {
             id: videoModel
             ml: medialib
         }
-        delegate: Package{
-            Item { Package.name: "grid" }
+        delegate: Item{
         }
 
         onCountChanged: {
diff --git a/modules/gui/qt/medialibrary/qml/VideoListDisplay.qml b/modules/gui/qt/medialibrary/qml/VideoListDisplay.qml
index 8528b6f108..3ac24038a3 100644
--- a/modules/gui/qt/medialibrary/qml/VideoListDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/VideoListDisplay.qml
@@ -50,7 +50,7 @@ Widgets.KeyNavigableTableView {
         property var model: parent.colModel
         FocusScope{
             anchors.fill: parent
-            focus: isFocusOnContextButton && rowModel.index === currentIndex
+            focus: isFocusOnContextButton && colDel.rowModel.index === currentIndex
             onFocusChanged: focus && contextButtonLoader.forceActiveFocus()
 
             Loader{
diff --git a/modules/gui/qt/widgets/qml/KeyNavigableTableView.qml b/modules/gui/qt/widgets/qml/KeyNavigableTableView.qml
index 7c1a8fe03d..9c0bf7ecad 100644
--- a/modules/gui/qt/widgets/qml/KeyNavigableTableView.qml
+++ b/modules/gui/qt/widgets/qml/KeyNavigableTableView.qml
@@ -64,74 +64,7 @@ NavigableFocusScope {
 
         model: root.model
 
-        delegate: Package {
-            id: element
-            property var rowModel: model
-            property bool selected: delegateModel.isSelected(index)
-            Connections {
-               target: delegateModel
-               onSelectionChanged: element.selected = delegateModel.isSelected(index)
-            }
-
-            Rectangle {
-                Package.name: "list"
-                id: lineView
-
-                width: root.width
-                height: root.rowHeight
-                color: VLCStyle.colors.getBgColor(selected, hoverArea.containsMouse, lineView.activeFocus)
-
-                MouseArea {
-                    id: hoverArea
-                    anchors.fill: parent
-                    hoverEnabled: true
-                    Keys.onMenuPressed: root.contextMenuButtonClicked(contextButton,rowModel)
-                    acceptedButtons: Qt.RightButton | Qt.LeftButton
-
-                    onClicked: {
-                        delegateModel.updateSelection( mouse.modifiers , view.currentIndex, index)
-                        view.currentIndex = rowModel.index
-                        lineView.forceActiveFocus()
-
-                        if (mouse.button === Qt.RightButton){
-                            root.rightClick(lineView,rowModel)
-                        }
-                    }
-
-                    onDoubleClicked: {
-                        actionForSelection(delegateModel.selectedIndexes())
-                    }
-
-                        Row {
-                            anchors {
-                                topMargin: VLCStyle.margin_xxsmall
-                                bottomMargin: VLCStyle.margin_xxsmall
-                                leftMargin: VLCStyle.margin_xxxsmall
-                                rightMargin: VLCStyle.margin_xxxsmall
-                                fill: parent
-                            }
-                            Repeater {
-                                model: sortModel
-
-                                Item {
-                                    height: parent.height
-                                    width: modelData.width * view.width
-                                    Layout.alignment: Qt.AlignVCenter
-                                    Loader{
-                                        anchors.fill: parent
-                                        sourceComponent: colDelegate
-
-                                        property var rowModel: element.rowModel
-                                        property var colModel: modelData
-
-                                    }
-
-                                }
-                            }
-                        }
-
-                }
-            }
+        delegate: Item {
         }
     }
 
@@ -143,8 +76,7 @@ NavigableFocusScope {
 
         focus: true
 
-        model : delegateModel.parts.list
-        modelCount: delegateModel.items.count
+        model : root.model
 
         headerPositioning: ListView.OverlayHeader
 
@@ -216,6 +148,69 @@ NavigableFocusScope {
             }
         }
 
+        delegate:Rectangle {
+            id: lineView
+
+            width: view.width
+            height: root.rowHeight
+            color: VLCStyle.colors.getBgColor(selected, hoverArea.containsMouse, lineView.activeFocus)
+
+            property var rowModel: model
+            property bool selected: delegateModel.isSelected(index)
+            Connections {
+                target: delegateModel
+                onSelectionChanged: lineView.selected = delegateModel.isSelected(index)
+            }
+
+            MouseArea {
+                id: hoverArea
+                anchors.fill: parent
+                hoverEnabled: true
+                Keys.onMenuPressed: root.contextMenuButtonClicked(contextButton,rowModel)
+                acceptedButtons: Qt.RightButton | Qt.LeftButton
+
+                onClicked: {
+                    delegateModel.updateSelection( mouse.modifiers , view.currentIndex, index)
+                    view.currentIndex = rowModel.index
+                    lineView.forceActiveFocus()
+
+                    if (mouse.button === Qt.RightButton){
+                        root.rightClick(lineView,rowModel)
+                    }
+                }
+
+                onDoubleClicked: {
+                    actionForSelection(delegateModel.selectedIndexes())
+                }
+
+                Row {
+                    anchors {
+                        topMargin: VLCStyle.margin_xxsmall
+                        bottomMargin: VLCStyle.margin_xxsmall
+                        leftMargin: VLCStyle.margin_xxxsmall
+                        rightMargin: VLCStyle.margin_xxxsmall
+                        fill: parent
+                    }
+                    Repeater {
+                        model: sortModel
+
+                        Item {
+                            height: parent.height
+                            width: modelData.width * view.width
+                            Layout.alignment: Qt.AlignVCenter
+                            Loader{
+                                anchors.fill: parent
+                                sourceComponent: colDelegate
+
+                                property var rowModel: lineView.rowModel
+                                property var colModel: modelData
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
         onSelectAll: delegateModel.selectAll()
         onSelectionUpdated: delegateModel.updateSelection( keyModifiers, oldIndex, newIndex )
         onActionAtIndex: root.actionForSelection( delegateModel.selectedIndexes() )
diff --git a/modules/gui/qt/widgets/qml/ListItem.qml b/modules/gui/qt/widgets/qml/ListItem.qml
index b23749bd2c..70a768b010 100644
--- a/modules/gui/qt/widgets/qml/ListItem.qml
+++ b/modules/gui/qt/widgets/qml/ListItem.qml
@@ -45,8 +45,6 @@ NavigableFocusScope {
 
     property bool selected: false
 
-    Keys.onMenuPressed: root.contextMenuButtonClicked(cover_bg)
-
     Accessible.role: Accessible.ListItem
     Accessible.name: line1
 
-- 
2.25.1



More information about the vlc-devel mailing list