[vlc-commits] qml: factorize page loading

Pierre Lamot git at videolan.org
Mon Oct 5 18:10:37 CEST 2020


vlc | branch: master | Pierre Lamot <pierre at videolabs.io> | Thu Sep 24 10:34:49 2020 +0200| [fabf4c815a2e0b1a7cc1af977a9ed138312de27a] | committer: Pierre Lamot

qml: factorize page loading

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

 modules/gui/qt/Makefile.am                         |  1 +
 .../qt/medialibrary/qml/MusicArtistsDisplay.qml    | 35 ++---------
 modules/gui/qt/medialibrary/qml/MusicDisplay.qml   | 57 ++++--------------
 .../gui/qt/medialibrary/qml/MusicGenresDisplay.qml | 41 +++----------
 modules/gui/qt/network/qml/DiscoverDisplay.qml     | 59 +++++-------------
 modules/gui/qt/network/qml/ServicesHomeDisplay.qml | 38 ++----------
 modules/gui/qt/vlc.qrc                             |  1 +
 modules/gui/qt/widgets/qml/PageLoader.qml          | 70 ++++++++++++++++++++++
 8 files changed, 118 insertions(+), 184 deletions(-)

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 6ff173323e..3fae174717 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -719,6 +719,7 @@ libqt_plugin_la_QML = \
 	gui/qt/widgets/qml/SortControl.qml \
 	gui/qt/widgets/qml/SpinBoxExt.qml \
 	gui/qt/widgets/qml/StackViewExt.qml \
+	gui/qt/widgets/qml/PageLoader.qml \
 	gui/qt/widgets/qml/TabButtonExt.qml \
 	gui/qt/widgets/qml/TableColumns.qml \
 	gui/qt/widgets/qml/TextToolButton.qml \
diff --git a/modules/gui/qt/medialibrary/qml/MusicArtistsDisplay.qml b/modules/gui/qt/medialibrary/qml/MusicArtistsDisplay.qml
index a7ca1b6646..1e7161f4d6 100644
--- a/modules/gui/qt/medialibrary/qml/MusicArtistsDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicArtistsDisplay.qml
@@ -27,15 +27,13 @@ import "qrc:///widgets/" as Widgets
 import "qrc:///style/"
 
 
-Widgets.NavigableFocusScope {
+Widgets.PageLoader {
     id: root
 
-    //name and properties of the tab to be initially loaded
-    property string view: "all"
-    property var viewProperties: ({})
     property var model
 
-    readonly property var pageModel: [{
+    defaultPage: "all"
+    pageModel: [{
         name: "all",
         component: artistGridComponent
     }, {
@@ -43,24 +41,8 @@ Widgets.NavigableFocusScope {
         component: artistAlbumsComponent
     }]
 
-    Component.onCompleted: loadView()
-    onViewChanged: {
-        viewProperties = {}
-        loadView()
-    }
-    onViewPropertiesChanged: loadView()
-
-    function loadDefaultView() {
-        root.view = "all"
-        root.viewProperties= ({})
-    }
-
-    function loadView() {
-        var found = stackView.loadView(root.pageModel, view, viewProperties)
-        if (!found)
-            stackView.replace(root.pageModel[0].component)
-        stackView.currentItem.navigationParent = root
-        model = stackView.currentItem.model
+    onCurrentItemChanged: {
+        model = currentItem.model
     }
 
     function _updateArtistsAllHistory(currentIndex) {
@@ -261,11 +243,4 @@ Widgets.NavigableFocusScope {
             onCurrentAlbumIndexChanged: _updateArtistsAlbumsHistory(currentIndex, currentAlbumIndex)
         }
     }
-
-    Widgets.StackViewExt {
-        id: stackView
-
-        anchors.fill: parent
-        focus: true
-    }
 }
diff --git a/modules/gui/qt/medialibrary/qml/MusicDisplay.qml b/modules/gui/qt/medialibrary/qml/MusicDisplay.qml
index ca33a1fc46..5e693f9fc8 100644
--- a/modules/gui/qt/medialibrary/qml/MusicDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicDisplay.qml
@@ -27,44 +27,14 @@ import "qrc:///style/"
 
 import org.videolan.medialib 0.1
 
-Widgets.NavigableFocusScope {
+Widgets.PageLoader {
     id: root
 
-    //name and properties of the tab to be initially loaded
-    property string view: "artists"
-    property var viewProperties: ({})
-
     property var sortModel
     property var contentModel
 
-    onViewChanged: {
-        viewProperties = ({})
-        loadView()
-    }
-    onViewPropertiesChanged: loadView()
-    Component.onCompleted: loadView()
-
-    function loadView() {
-        var found = stackView.loadView(root.pageModel, view, viewProperties)
-        if (!found)
-            stackView.replace(root.pageModel[0].component)
-
-        stackView.currentItem.navigationParent = root
-        sortModel = stackView.currentItem.sortModel
-        contentModel = stackView.currentItem.model
-    }
-
-    //reset view
-    function loadDefaultView() {
-        root.view = "artists"
-        root.viewProperties= ({})
-    }
-
-    function loadIndex(index) {
-        history.push(["mc", "music", root.pageModel[index].name])
-    }
-
-    readonly property var pageModel: [{
+    defaultPage: "artists"
+    pageModel: [{
             displayText: i18n.qtr("Artists"),
             name: "artists",
             url: "qrc:///medialibrary/MusicArtistsDisplay.qml"
@@ -83,6 +53,15 @@ Widgets.NavigableFocusScope {
         }
     ]
 
+    onCurrentItemChanged: {
+        sortModel = currentItem.sortModel
+        contentModel = currentItem.model
+    }
+
+    function loadIndex(index) {
+        history.push(["mc", "music", root.pageModel[index].name])
+    }
+
     property var tabModel: ListModel {
         Component.onCompleted: {
             pageModel.forEach(function(e) {
@@ -93,16 +72,4 @@ Widgets.NavigableFocusScope {
             })
         }
     }
-
-    /* The data elements */
-    Widgets.StackViewExt  {
-        id: stackView
-        anchors.fill: parent
-        focus: true
-
-        onCurrentItemChanged: {
-            sortModel = stackView.currentItem.sortModel
-            contentModel = stackView.currentItem.model
-        }
-    }
 }
diff --git a/modules/gui/qt/medialibrary/qml/MusicGenresDisplay.qml b/modules/gui/qt/medialibrary/qml/MusicGenresDisplay.qml
index ced5c1acc9..16afffff07 100644
--- a/modules/gui/qt/medialibrary/qml/MusicGenresDisplay.qml
+++ b/modules/gui/qt/medialibrary/qml/MusicGenresDisplay.qml
@@ -25,17 +25,11 @@ import "qrc:///util/" as Util
 import "qrc:///widgets/" as Widgets
 import "qrc:///style/"
 
-Widgets.NavigableFocusScope {
+Widgets.PageLoader {
     id: root
 
-    //name and properties of the tab to be initially loaded
-    property string view: "all"
-    property var viewProperties: ({})
-
-    property var sortModel
-    property var model
-
-    readonly property var pageModel: [{
+    defaultPage: "all"
+    pageModel: [{
         name: "all",
         component: genresComponent
     }, {
@@ -43,26 +37,14 @@ Widgets.NavigableFocusScope {
         component: albumGenreComponent
     }]
 
-    Component.onCompleted: loadView()
-    onViewChanged: {
-        viewProperties = {}
-        loadView()
-    }
-    onViewPropertiesChanged: loadView()
+    property var sortModel
+    property var model
 
-    function loadDefaultView() {
-        root.view = "all"
-        root.viewProperties= ({})
+    onCurrentItemChanged: {
+        sortModel = currentItem.sortModel
+        model = currentItem.model
     }
 
-    function loadView() {
-        var found = stackView.loadView(root.pageModel, view, viewProperties)
-        if (!found)
-            stackView.replace(root.pageModel[0].component)
-        stackView.currentItem.navigationParent = root
-        sortModel = stackView.currentItem.sortModel
-        model = stackView.currentItem.model
-    }
 
     function _updateGenresAllHistory(currentIndex) {
         history.update(["mc", "music", "genres", "all", { "initialIndex": currentIndex }])
@@ -107,11 +89,4 @@ Widgets.NavigableFocusScope {
             onCurrentIndexChanged: _updateGenresAlbumsHistory(currentIndex, parentId, genreName)
         }
     }
-
-    Widgets.StackViewExt {
-        id: stackView
-
-        anchors.fill: parent
-        focus: true
-    }
 }
diff --git a/modules/gui/qt/network/qml/DiscoverDisplay.qml b/modules/gui/qt/network/qml/DiscoverDisplay.qml
index c03a3e3ec5..fc2275549a 100644
--- a/modules/gui/qt/network/qml/DiscoverDisplay.qml
+++ b/modules/gui/qt/network/qml/DiscoverDisplay.qml
@@ -27,44 +27,16 @@ import "qrc:///widgets/" as Widgets
 import "qrc:///util/" as Util
 import "qrc:///style/"
 
-Widgets.NavigableFocusScope {
+Widgets.PageLoader {
     id: root
 
-    //name and properties of the tab to be initially loaded
-    property string view: "services"
-    property var viewProperties: ({})
-
     property var sortModel
     property var contentModel
 
-    onViewChanged: {
-        viewProperties = ({})
-        loadView()
-    }
-    onViewPropertiesChanged: loadView()
-    Component.onCompleted: loadView()
-
-    function loadView() {
-        var found = stackView.loadView(root.pageModel, view, viewProperties)
-        if (!found)
-            stackView.replace(root.pageModel[0].component)
-
-        stackView.currentItem.navigationParent = root
-        sortModel = stackView.currentItem.sortModel
-        contentModel = stackView.currentItem.model
-    }
-
-    //reset view
-    function loadDefaultView() {
-        root.view = "services"
-        root.viewProperties= ({})
-    }
-
-    function loadIndex(index) {
-        history.push(["mc", "discover", root.pageModel[index].name])
-    }
+    //name and properties of the tab to be initially loaded
+    defaultPage: "services"
 
-    readonly property var pageModel: [{
+    pageModel: [{
             displayText: i18n.qtr("Services"),
             name: "services",
             url: "qrc:///network/ServicesHomeDisplay.qml"
@@ -75,6 +47,17 @@ Widgets.NavigableFocusScope {
         }
     ]
 
+    onCurrentItemChanged: {
+        sortModel = currentItem.sortModel
+        contentModel = currentItem.model
+    }
+
+
+    function loadIndex(index) {
+        history.push(["mc", "discover", root.pageModel[index].name])
+    }
+
+
     property var tabModel: ListModel {
         Component.onCompleted: {
             pageModel.forEach(function(e) {
@@ -85,16 +68,4 @@ Widgets.NavigableFocusScope {
             })
         }
     }
-
-    /* The data elements */
-    Widgets.StackViewExt  {
-        id: stackView
-        anchors.fill: parent
-        focus: true
-
-        onCurrentItemChanged: {
-            sortModel = stackView.currentItem.sortModel
-            contentModel = stackView.currentItem.model
-        }
-    }
 }
diff --git a/modules/gui/qt/network/qml/ServicesHomeDisplay.qml b/modules/gui/qt/network/qml/ServicesHomeDisplay.qml
index f8db63eb8e..d74ed06187 100644
--- a/modules/gui/qt/network/qml/ServicesHomeDisplay.qml
+++ b/modules/gui/qt/network/qml/ServicesHomeDisplay.qml
@@ -28,16 +28,14 @@ import "qrc:///widgets/" as Widgets
 import "qrc:///util/" as Util
 import "qrc:///style/"
 
-Widgets.NavigableFocusScope {
+Widgets.PageLoader {
     id: root
 
-    property string view: "all"
-    property var viewProperties: ({})
-
     property var sortModel
     property var model
 
-    readonly property var pageModel: [{
+    defaultPage: "all"
+    pageModel: [{
         name: "all",
         component: allSourcesComponent
     }, {
@@ -51,24 +49,9 @@ Widgets.NavigableFocusScope {
         component: sourceBrowseComponent
     }]
 
-    Component.onCompleted: loadView()
-    onViewChanged: {
-        viewProperties = {}
-        loadView()
-    }
-    onViewPropertiesChanged: loadView()
-
-    function loadDefaultView() {
-        root.view = "all"
-        root.viewProperties = ({})
-    }
-
-    function loadView() {
-        var found = stackView.loadView(root.pageModel, view, viewProperties)
-        if (!found)
-            stackView.replace(root.pageModel[0].component)
-        stackView.currentItem.navigationParent = root
-        model = stackView.currentItem.model
+    onCurrentItemChanged: {
+        sortModel = currentItem.sortModel
+        model = currentItem.model
     }
 
     Component {
@@ -390,13 +373,4 @@ Widgets.NavigableFocusScope {
             }
         }
     }
-
-
-
-    Widgets.StackViewExt {
-        id: stackView
-
-        anchors.fill: parent
-        focus: true
-    }
 }
diff --git a/modules/gui/qt/vlc.qrc b/modules/gui/qt/vlc.qrc
index ec13267124..8c495e9c55 100644
--- a/modules/gui/qt/vlc.qrc
+++ b/modules/gui/qt/vlc.qrc
@@ -225,6 +225,7 @@
         <file alias="HorizontalResizeHandle.qml">widgets/qml/HorizontalResizeHandle.qml</file>
         <file alias="PointingTooltip.qml">widgets/qml/PointingTooltip.qml</file>
         <file alias="FrostedGlassEffect.qml">widgets/qml/FrostedGlassEffect.qml</file>
+	<file alias="PageLoader.qml">widgets/qml/PageLoader.qml</file>
     </qresource>
     <qresource prefix="/util">
         <file alias="SelectableDelegateModel.qml">util/qml/SelectableDelegateModel.qml</file>
diff --git a/modules/gui/qt/widgets/qml/PageLoader.qml b/modules/gui/qt/widgets/qml/PageLoader.qml
new file mode 100644
index 0000000000..96cca84e98
--- /dev/null
+++ b/modules/gui/qt/widgets/qml/PageLoader.qml
@@ -0,0 +1,70 @@
+/*****************************************************************************
+ * Copyright (C) 2020 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+import QtQuick 2.0
+
+NavigableFocusScope {
+    id: root
+
+    property string view: defaultPage
+    property var viewProperties: ({})
+
+    property string defaultPage: ""
+
+    property var pageModel: []
+
+    signal pageChanged(string page)
+    signal currentItemChanged(var currentItem)
+
+    Component.onCompleted: loadView()
+    onViewChanged: {
+        viewProperties = {}
+        loadView()
+    }
+    onViewPropertiesChanged: loadView()
+
+    function loadDefaultView() {
+        root.view = defaultPage
+        root.viewProperties = ({})
+    }
+
+    function loadView() {
+        if (view === "") {
+            console.error("view is not defined")
+            return
+        }
+        if (pageModel === []) {
+            console.error("pageModel is not defined")
+            return
+        }
+        var found = stackView.loadView(root.pageModel, view, viewProperties)
+        if (!found)
+            stackView.replace(root.pageModel[0].component)
+
+        if (stackView.currentItem && stackView.currentItem.hasOwnProperty("navigationParent")) {
+            stackView.currentItem.navigationParent = root
+        }
+        root.currentItemChanged(stackView.currentItem)
+    }
+
+    StackViewExt {
+        id: stackView
+
+        anchors.fill: parent
+        focus: true
+    }
+}



More information about the vlc-commits mailing list