[vlc-devel] [RFC 62/82] qml: add helper class to handle selection in DelegateModel

Pierre Lamot pierre at videolabs.io
Fri Feb 1 14:02:06 CET 2019


---
 modules/gui/qt/Makefile.am                    |  1 +
 .../qt/qml/utils/SelectableDelegateModel.qml  | 89 +++++++++++++++++++
 modules/gui/qt/vlc.qrc                        |  1 +
 3 files changed, 91 insertions(+)
 create mode 100644 modules/gui/qt/qml/utils/SelectableDelegateModel.qml

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index 7669d61b95..b4aae741a5 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -523,6 +523,7 @@ libqt_plugin_la_RES = \
 	gui/qt/pixmaps/search_clear.svg \
 	gui/qt/pixmaps/lock.svg \
 	gui/qt/qml/utils/NavigableFocusScope.qml \
+	gui/qt/qml/utils/SelectableDelegateModel.qml \
 	gui/qt/qml/style/qmldir \
 	gui/qt/qml/style/VLCIcons.qml \
 	gui/qt/qml/style/VLCStyle.qml \
diff --git a/modules/gui/qt/qml/utils/SelectableDelegateModel.qml b/modules/gui/qt/qml/utils/SelectableDelegateModel.qml
new file mode 100644
index 0000000000..5910f99278
--- /dev/null
+++ b/modules/gui/qt/qml/utils/SelectableDelegateModel.qml
@@ -0,0 +1,89 @@
+/*****************************************************************************
+ * Copyright (C) 2019 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.11
+import QtQml.Models 2.2
+
+DelegateModel {
+    id: delegateModel
+
+    property int shiftIndex: -1
+    property alias selectedGroup: selectedGroup
+
+    groups: [
+        DelegateModelGroup { id: selectedGroup; name: "selected"; includeByDefault: false }
+    ]
+
+    function _addRange(from, to) {
+        for (var i = from; i <= to; i++) {
+            delegateModel.items.get(i).inSelected = true
+        }
+    }
+    function _delRange(from, to) {
+        for (var i = from; i <= to; i++) {
+            delegateModel.items.get(i).inSelected = false
+        }
+    }
+
+    function selectAll() {
+        delegateModel.items.addGroups(0, delegateModel.items.count, ["selected"])
+    }
+
+    function updateSelection( keymodifiers, oldIndex, newIndex ) {
+        if ((keymodifiers & Qt.ShiftModifier)) {
+            if ( shiftIndex === oldIndex) {
+                if ( newIndex > shiftIndex )
+                    _addRange(shiftIndex, newIndex)
+                else
+                    _addRange(newIndex, shiftIndex)
+            } else if (shiftIndex <= newIndex && newIndex < oldIndex) {
+                _delRange(newIndex + 1, oldIndex )
+            } else if ( shiftIndex < oldIndex && oldIndex < newIndex ) {
+                _addRange(oldIndex, newIndex)
+            } else if ( newIndex < shiftIndex && shiftIndex < oldIndex ) {
+                _delRange(shiftIndex, oldIndex)
+                _addRange(newIndex, shiftIndex)
+            } else if ( newIndex < oldIndex && oldIndex < shiftIndex  ) {
+                _addRange(newIndex, oldIndex)
+            } else if ( oldIndex <= shiftIndex && shiftIndex < newIndex ) {
+                _delRange(oldIndex, shiftIndex)
+                _addRange(shiftIndex, newIndex)
+            } else if ( oldIndex < newIndex && newIndex <= shiftIndex  ) {
+                _delRange(oldIndex, newIndex - 1)
+            }
+        } else {
+
+            var e = delegateModel.items.get(newIndex)
+            if (e.inSelected) {
+                if ((keymodifiers & Qt.ControlModifier) == Qt.ControlModifier) {
+                    e.inSelected = false
+                } else {
+                    selectedGroup.remove(0,selectedGroup.count) //clear
+                }
+            } else {
+                if ((keymodifiers & Qt.ControlModifier) == Qt.ControlModifier) {
+                    e.inSelected = true
+                } else {
+                    if (selectedGroup.count > 0)
+                        selectedGroup.remove(0,selectedGroup.count) //clear
+                    e.inSelected = true
+                }
+            }
+            shiftIndex = newIndex
+        }
+    }
+}
diff --git a/modules/gui/qt/vlc.qrc b/modules/gui/qt/vlc.qrc
index 4ab1340161..c01c544c53 100644
--- a/modules/gui/qt/vlc.qrc
+++ b/modules/gui/qt/vlc.qrc
@@ -159,6 +159,7 @@
         <file alias="addon_yellow.svg">pixmaps/addons/addon_yellow.svg</file>
     </qresource>
     <qresource prefix="/utils">
+        <file alias="SelectableDelegateModel.qml">qml/utils/SelectableDelegateModel.qml</file>
         <file alias="NavigableFocusScope.qml">qml/utils/NavigableFocusScope.qml</file>
     </qresource>
     <qresource prefix="/style">
-- 
2.19.1



More information about the vlc-devel mailing list