7 Module implementing a dialog to select from a list of strings. |
7 Module implementing a dialog to select from a list of strings. |
8 """ |
8 """ |
9 |
9 |
10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 from PyQt5.QtCore import pyqtSlot |
12 from PyQt5.QtCore import pyqtSlot, Qt |
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractItemView |
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractItemView, \ |
|
14 QListWidgetItem |
14 |
15 |
15 from .Ui_E5ListSelectionDialog import Ui_E5ListSelectionDialog |
16 from .Ui_E5ListSelectionDialog import Ui_E5ListSelectionDialog |
16 |
17 |
17 |
18 |
18 class E5ListSelectionDialog(QDialog, Ui_E5ListSelectionDialog): |
19 class E5ListSelectionDialog(QDialog, Ui_E5ListSelectionDialog): |
19 """ |
20 """ |
20 Class implementing a dialog to select from a list of strings. |
21 Class implementing a dialog to select from a list of strings. |
21 """ |
22 """ |
22 def __init__(self, entries, |
23 def __init__(self, entries, |
23 selectionMode=QAbstractItemView.ExtendedSelection, |
24 selectionMode=QAbstractItemView.ExtendedSelection, |
24 title="", message="", parent=None): |
25 title="", message="", checkBoxSelection=False, parent=None): |
25 """ |
26 """ |
26 Constructor |
27 Constructor |
27 |
28 |
28 @param entries list of entries to select from |
29 @param entries list of entries to select from |
29 @type list of str |
30 @type list of str |
31 @type QAbstractItemView.SelectionMode |
32 @type QAbstractItemView.SelectionMode |
32 @param title tirle of the dialog |
33 @param title tirle of the dialog |
33 @type str |
34 @type str |
34 @param message message to be show in the dialog |
35 @param message message to be show in the dialog |
35 @type str |
36 @type str |
|
37 @param checkBoxSelection flag indicating to select items via their |
|
38 checkbox |
|
39 @type bool |
36 @param parent reference to the parent widget |
40 @param parent reference to the parent widget |
37 @type QWidget |
41 @type QWidget |
38 """ |
42 """ |
39 super(E5ListSelectionDialog, self).__init__(parent) |
43 super(E5ListSelectionDialog, self).__init__(parent) |
40 self.setupUi(self) |
44 self.setupUi(self) |
41 |
45 |
42 self.selectionList.setSelectionMode(selectionMode) |
|
43 if title: |
46 if title: |
44 self.setWindowTitle(title) |
47 self.setWindowTitle(title) |
45 if message: |
48 if message: |
46 self.messageLabel.setText(message) |
49 self.messageLabel.setText(message) |
47 |
50 |
48 self.selectionList.addItems(entries) |
51 self.__checkCount = 0 |
|
52 self.__isCheckBoxSelection = checkBoxSelection |
|
53 if self.__isCheckBoxSelection: |
|
54 self.selectionList.setSelectionMode(QAbstractItemView.NoSelection) |
|
55 for entry in entries: |
|
56 itm = QListWidgetItem(entry) |
|
57 itm.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) |
|
58 itm.setCheckState(Qt.Unchecked) |
|
59 self.selectionList.addItem(itm) |
|
60 else: |
|
61 self.selectionList.setSelectionMode(selectionMode) |
|
62 self.selectionList.addItems(entries) |
49 |
63 |
50 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
64 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(False) |
51 |
65 |
52 @pyqtSlot() |
66 @pyqtSlot() |
53 def on_selectionList_itemSelectionChanged(self): |
67 def on_selectionList_itemSelectionChanged(self): |
54 """ |
68 """ |
55 Private slot handling a change of the selection. |
69 Private slot handling a change of the selection. |
56 """ |
70 """ |
57 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( |
71 if not self.__isCheckBoxSelection: |
58 len(self.selectionList.selectedItems()) > 0) |
72 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( |
|
73 len(self.selectionList.selectedItems()) > 0) |
|
74 |
|
75 def on_selectionList_itemChanged(self, itm): |
|
76 """ |
|
77 Private slot handling a change of an item. |
|
78 |
|
79 @param itm reference to the changed item |
|
80 @type QListWidgetItem |
|
81 """ |
|
82 if self.__isCheckBoxSelection: |
|
83 if itm.checkState() == Qt.Checked: |
|
84 self.__checkCount += 1 |
|
85 else: |
|
86 self.__checkCount -= 1 |
|
87 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled( |
|
88 self.__checkCount > 0) |
59 |
89 |
60 def getSelection(self): |
90 def getSelection(self): |
61 """ |
91 """ |
62 Public method to retrieve the selected items. |
92 Public method to retrieve the selected items. |
63 |
93 |
64 @return selected entries |
94 @return selected entries |
65 @rtype list of str |
95 @rtype list of str |
66 """ |
96 """ |
67 entries = [] |
97 entries = [] |
68 for item in self.selectionList.selectedItems(): |
98 if self.__isCheckBoxSelection: |
69 entries.append(item.text()) |
99 for row in range(self.selectionList.count()): |
|
100 item = self.selectionList.item(row) |
|
101 if item.checkState() == Qt.Checked: |
|
102 entries.append(item.text()) |
|
103 else: |
|
104 for item in self.selectionList.selectedItems(): |
|
105 entries.append(item.text()) |
70 return entries |
106 return entries |