29 selectionMode=QAbstractItemView.SelectionMode.ExtendedSelection, |
29 selectionMode=QAbstractItemView.SelectionMode.ExtendedSelection, |
30 title="", |
30 title="", |
31 message="", |
31 message="", |
32 checkBoxSelection=False, |
32 checkBoxSelection=False, |
33 doubleClickOk=False, |
33 doubleClickOk=False, |
|
34 emptySelectionOk=False, |
|
35 showSelectAll=False, |
34 parent=None, |
36 parent=None, |
35 ): |
37 ): |
36 """ |
38 """ |
37 Constructor |
39 Constructor |
38 |
40 |
39 @param entries list of entries to select from |
41 @param entries list of entries to select from |
40 @type list of str |
42 @type list of str or list of tuple of (str, Any) |
41 @param selectionMode selection mode for the list |
43 @param selectionMode selection mode for the list |
42 @type QAbstractItemView.SelectionMode |
44 @type QAbstractItemView.SelectionMode |
43 @param title title of the dialog |
45 @param title title of the dialog |
44 @type str |
46 @type str |
45 @param message message to be show in the dialog |
47 @param message message to be show in the dialog |
61 if message: |
67 if message: |
62 self.messageLabel.setText(message) |
68 self.messageLabel.setText(message) |
63 |
69 |
64 self.__checkCount = 0 |
70 self.__checkCount = 0 |
65 self.__isCheckBoxSelection = checkBoxSelection |
71 self.__isCheckBoxSelection = checkBoxSelection |
66 if self.__isCheckBoxSelection: |
72 self.__doubleClickOk = doubleClickOk |
67 self.selectionList.setSelectionMode( |
73 self.__emptySelectionOk = emptySelectionOk |
68 QAbstractItemView.SelectionMode.NoSelection |
74 |
69 ) |
75 self.selectionList.setSelectionMode( |
70 for entry in entries: |
76 QAbstractItemView.SelectionMode.NoSelection |
|
77 if self.__isCheckBoxSelection |
|
78 else selectionMode |
|
79 ) |
|
80 |
|
81 for entry in entries: |
|
82 if isinstance(entry, tuple): |
|
83 itm = QListWidgetItem(entry[0]) |
|
84 itm.setData(Qt.ItemDataRole.UserRole, entry[1]) |
|
85 else: |
71 itm = QListWidgetItem(entry) |
86 itm = QListWidgetItem(entry) |
|
87 if self.__isCheckBoxSelection: |
72 itm.setFlags( |
88 itm.setFlags( |
73 Qt.ItemFlag.ItemIsUserCheckable | Qt.ItemFlag.ItemIsEnabled |
89 Qt.ItemFlag.ItemIsUserCheckable | Qt.ItemFlag.ItemIsEnabled |
74 ) |
90 ) |
75 itm.setCheckState(Qt.CheckState.Unchecked) |
91 itm.setCheckState(Qt.CheckState.Unchecked) |
76 self.selectionList.addItem(itm) |
92 self.selectionList.addItem(itm) |
77 else: |
93 |
78 self.selectionList.setSelectionMode(selectionMode) |
94 if showSelectAll: |
79 self.selectionList.addItems(entries) |
95 self.buttonBox.addButton( |
80 |
96 self.tr("Deselect All"), QDialogButtonBox.ButtonRole.ActionRole |
81 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(False) |
97 ).clicked.connect(lambda: self.__selectAll(False)) |
|
98 self.buttonBox.addButton( |
|
99 self.tr("Select All"), QDialogButtonBox.ButtonRole.ActionRole |
|
100 ).clicked.connect(lambda: self.__selectAll(True)) |
|
101 |
|
102 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
103 emptySelectionOk |
|
104 ) |
82 |
105 |
83 @pyqtSlot() |
106 @pyqtSlot() |
84 def on_selectionList_itemSelectionChanged(self): |
107 def on_selectionList_itemSelectionChanged(self): |
85 """ |
108 """ |
86 Private slot handling a change of the selection. |
109 Private slot handling a change of the selection. |
87 """ |
110 """ |
88 if not self.__isCheckBoxSelection: |
111 if not self.__isCheckBoxSelection: |
89 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
112 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
90 len(self.selectionList.selectedItems()) > 0 |
113 len(self.selectionList.selectedItems()) > 0 or self.__emptySelectionOk |
91 ) |
114 ) |
92 |
115 |
|
116 @pyqtSlot(QListWidgetItem) |
93 def on_selectionList_itemChanged(self, itm): |
117 def on_selectionList_itemChanged(self, itm): |
94 """ |
118 """ |
95 Private slot handling a change of an item. |
119 Private slot handling a change of an item. |
96 |
120 |
97 @param itm reference to the changed item |
121 @param itm reference to the changed item |
101 if itm.checkState() == Qt.CheckState.Checked: |
125 if itm.checkState() == Qt.CheckState.Checked: |
102 self.__checkCount += 1 |
126 self.__checkCount += 1 |
103 elif self.__checkCount > 0: |
127 elif self.__checkCount > 0: |
104 self.__checkCount -= 1 |
128 self.__checkCount -= 1 |
105 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
129 self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( |
106 self.__checkCount > 0 |
130 self.__checkCount > 0 or self.__emptySelectionOk |
107 ) |
131 ) |
108 |
132 |
109 @pyqtSlot(QListWidgetItem) |
133 @pyqtSlot(QListWidgetItem) |
110 def on_selectionList_itemDoubleClicked(self, item): |
134 def on_selectionList_itemDoubleClicked(self, item): |
111 """ |
135 """ |
116 """ |
140 """ |
117 if ( |
141 if ( |
118 not self.__isCheckBoxSelection |
142 not self.__isCheckBoxSelection |
119 and self.selectionList.selectionMode() |
143 and self.selectionList.selectionMode() |
120 == QAbstractItemView.SelectionMode.SingleSelection |
144 == QAbstractItemView.SelectionMode.SingleSelection |
|
145 and self.__doubleClickOk |
121 ): |
146 ): |
122 self.accept() |
147 self.accept() |
123 |
148 |
|
149 def __selectAll(self, state): |
|
150 """ |
|
151 Private method to select or deselect all entries. |
|
152 |
|
153 @param state flag indicating the desired selection state |
|
154 @type bool |
|
155 """ |
|
156 for row in range(self.selectionList.count()): |
|
157 item = self.selectionList.item(row) |
|
158 if self.__isCheckBoxSelection: |
|
159 if state: |
|
160 item.setCheckState(Qt.CheckState.Checked) |
|
161 else: |
|
162 item.setCheckState(Qt.CheckState.Unchecked) |
|
163 else: |
|
164 item.setSelected(state) |
|
165 |
|
166 def setSelection(self, selection): |
|
167 """ |
|
168 Public method to preselect a list of entries. |
|
169 |
|
170 @param selection list of selected entries |
|
171 @type list of str |
|
172 """ |
|
173 for name in selection: |
|
174 itemList = self.selectionList.findItems( |
|
175 name, Qt.MatchFlag.MatchCaseSensitive | Qt.MatchFlag.MatchStartsWith |
|
176 ) |
|
177 if itemList: |
|
178 if self.__isCheckBoxSelection: |
|
179 itemList[0].setCheckState(Qt.CheckState.Checked) |
|
180 else: |
|
181 itemList[0].setSelected(True) |
|
182 |
124 def getSelection(self): |
183 def getSelection(self): |
125 """ |
184 """ |
126 Public method to retrieve the selected items. |
185 Public method to retrieve the selected items. |
127 |
186 |
128 @return selected entries |
187 @return selected entries |
129 @rtype list of str |
188 @rtype list of str or list of tuple of (str, Any) |
130 """ |
189 """ |
131 entries = [] |
190 entries = [] |
132 if self.__isCheckBoxSelection: |
191 if self.__isCheckBoxSelection: |
133 for row in range(self.selectionList.count()): |
192 for row in range(self.selectionList.count()): |
134 item = self.selectionList.item(row) |
193 item = self.selectionList.item(row) |
135 if item.checkState() == Qt.CheckState.Checked: |
194 if item.checkState() == Qt.CheckState.Checked: |
136 entries.append(item.text()) |
195 data = item.data(Qt.ItemDataRole.UserRole) |
|
196 if data is None: |
|
197 entries.append(item.text()) |
|
198 else: |
|
199 entries.append((item.text(), data)) |
137 else: |
200 else: |
138 for item in self.selectionList.selectedItems(): |
201 for item in self.selectionList.selectedItems(): |
139 entries.append(item.text()) |
202 data = item.data(Qt.ItemDataRole.UserRole) |
|
203 if data is None: |
|
204 entries.append(item.text()) |
|
205 else: |
|
206 entries.append((item.text(), data)) |
140 return entries |
207 return entries |