E5Gui/E5ListSelectionDialog.py

changeset 6437
3dd851d1edf8
parent 6048
82ad8ec9548c
child 6645
ad476851d7e0
diff -r 7fa64ad908c3 -r 3dd851d1edf8 E5Gui/E5ListSelectionDialog.py
--- a/E5Gui/E5ListSelectionDialog.py	Fri Jul 20 20:24:29 2018 +0200
+++ b/E5Gui/E5ListSelectionDialog.py	Sat Jul 21 13:46:07 2018 +0200
@@ -9,8 +9,9 @@
 
 from __future__ import unicode_literals
 
-from PyQt5.QtCore import pyqtSlot
-from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractItemView
+from PyQt5.QtCore import pyqtSlot, Qt
+from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractItemView, \
+    QListWidgetItem
 
 from .Ui_E5ListSelectionDialog import Ui_E5ListSelectionDialog
 
@@ -21,7 +22,7 @@
     """
     def __init__(self, entries,
                  selectionMode=QAbstractItemView.ExtendedSelection,
-                 title="", message="", parent=None):
+                 title="", message="", checkBoxSelection=False, parent=None):
         """
         Constructor
         
@@ -33,19 +34,32 @@
         @type str
         @param message message to be show in the dialog
         @type str
+        @param checkBoxSelection flag indicating to select items via their
+            checkbox
+        @type bool
         @param parent reference to the parent widget
         @type QWidget
         """
         super(E5ListSelectionDialog, self).__init__(parent)
         self.setupUi(self)
         
-        self.selectionList.setSelectionMode(selectionMode)
         if title:
             self.setWindowTitle(title)
         if message:
             self.messageLabel.setText(message)
         
-        self.selectionList.addItems(entries)
+        self.__checkCount = 0
+        self.__isCheckBoxSelection = checkBoxSelection
+        if self.__isCheckBoxSelection:
+            self.selectionList.setSelectionMode(QAbstractItemView.NoSelection)
+            for entry in entries:
+                itm = QListWidgetItem(entry)
+                itm.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
+                itm.setCheckState(Qt.Unchecked)
+                self.selectionList.addItem(itm)
+        else:
+            self.selectionList.setSelectionMode(selectionMode)
+            self.selectionList.addItems(entries)
         
         self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False)
     
@@ -54,8 +68,24 @@
         """
         Private slot handling a change of the selection.
         """
-        self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(
-            len(self.selectionList.selectedItems()) > 0)
+        if not self.__isCheckBoxSelection:
+            self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(
+                len(self.selectionList.selectedItems()) > 0)
+    
+    def on_selectionList_itemChanged(self, itm):
+        """
+        Private slot handling a change of an item.
+        
+        @param itm reference to the changed item
+        @type QListWidgetItem
+        """
+        if self.__isCheckBoxSelection:
+            if itm.checkState() == Qt.Checked:
+                self.__checkCount += 1
+            else:
+                self.__checkCount -= 1
+            self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(
+                self.__checkCount > 0)
     
     def getSelection(self):
         """
@@ -65,6 +95,12 @@
         @rtype list of str
         """
         entries = []
-        for item in self.selectionList.selectedItems():
-            entries.append(item.text())
+        if self.__isCheckBoxSelection:
+            for row in range(self.selectionList.count()):
+                item = self.selectionList.item(row)
+                if item.checkState() == Qt.Checked:
+                    entries.append(item.text())
+        else:
+            for item in self.selectionList.selectedItems():
+                entries.append(item.text())
         return entries

eric ide

mercurial