|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select from a list of strings. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot, Qt |
|
11 from PyQt6.QtWidgets import ( |
|
12 QDialog, QDialogButtonBox, QAbstractItemView, QListWidgetItem |
|
13 ) |
|
14 |
|
15 from .Ui_EricListSelectionDialog import Ui_EricListSelectionDialog |
|
16 |
|
17 |
|
18 class EricListSelectionDialog(QDialog, Ui_EricListSelectionDialog): |
|
19 """ |
|
20 Class implementing a dialog to select from a list of strings. |
|
21 """ |
|
22 def __init__(self, entries, |
|
23 selectionMode=QAbstractItemView.SelectionMode |
|
24 .ExtendedSelection, |
|
25 title="", message="", checkBoxSelection=False, |
|
26 doubleClickOk=False, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param entries list of entries to select from |
|
31 @type list of str |
|
32 @param selectionMode selection mode for the list |
|
33 @type QAbstractItemView.SelectionMode |
|
34 @param title title of the dialog |
|
35 @type str |
|
36 @param message message to be show in the dialog |
|
37 @type str |
|
38 @param checkBoxSelection flag indicating to select items via their |
|
39 checkbox |
|
40 @type bool |
|
41 @param doubleClickOk flag indicating to accept the dialog upon a |
|
42 double click of an item (single selection only) |
|
43 @type bool |
|
44 @param parent reference to the parent widget |
|
45 @type QWidget |
|
46 """ |
|
47 super().__init__(parent) |
|
48 self.setupUi(self) |
|
49 |
|
50 if title: |
|
51 self.setWindowTitle(title) |
|
52 if message: |
|
53 self.messageLabel.setText(message) |
|
54 |
|
55 self.__checkCount = 0 |
|
56 self.__isCheckBoxSelection = checkBoxSelection |
|
57 if self.__isCheckBoxSelection: |
|
58 self.selectionList.setSelectionMode( |
|
59 QAbstractItemView.SelectionMode.NoSelection) |
|
60 for entry in entries: |
|
61 itm = QListWidgetItem(entry) |
|
62 itm.setFlags(Qt.ItemFlag.ItemIsUserCheckable | |
|
63 Qt.ItemFlag.ItemIsEnabled) |
|
64 itm.setCheckState(Qt.CheckState.Unchecked) |
|
65 self.selectionList.addItem(itm) |
|
66 else: |
|
67 self.selectionList.setSelectionMode(selectionMode) |
|
68 self.selectionList.addItems(entries) |
|
69 |
|
70 self.buttonBox.button( |
|
71 QDialogButtonBox.StandardButton.Ok).setEnabled(False) |
|
72 |
|
73 @pyqtSlot() |
|
74 def on_selectionList_itemSelectionChanged(self): |
|
75 """ |
|
76 Private slot handling a change of the selection. |
|
77 """ |
|
78 if not self.__isCheckBoxSelection: |
|
79 self.buttonBox.button( |
|
80 QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
81 len(self.selectionList.selectedItems()) > 0) |
|
82 |
|
83 def on_selectionList_itemChanged(self, itm): |
|
84 """ |
|
85 Private slot handling a change of an item. |
|
86 |
|
87 @param itm reference to the changed item |
|
88 @type QListWidgetItem |
|
89 """ |
|
90 if self.__isCheckBoxSelection: |
|
91 if itm.checkState() == Qt.CheckState.Checked: |
|
92 self.__checkCount += 1 |
|
93 else: |
|
94 self.__checkCount -= 1 |
|
95 self.buttonBox.button( |
|
96 QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
97 self.__checkCount > 0) |
|
98 |
|
99 @pyqtSlot(QListWidgetItem) |
|
100 def on_selectionList_itemDoubleClicked(self, item): |
|
101 """ |
|
102 Private slot handling double clicking an item. |
|
103 |
|
104 @param item double clicked item |
|
105 @type QListWidgetItem |
|
106 """ |
|
107 if ( |
|
108 not self.__isCheckBoxSelection and |
|
109 self.selectionList.selectionMode() == |
|
110 QAbstractItemView.SelectionMode.SingleSelection |
|
111 ): |
|
112 self.accept() |
|
113 |
|
114 def getSelection(self): |
|
115 """ |
|
116 Public method to retrieve the selected items. |
|
117 |
|
118 @return selected entries |
|
119 @rtype list of str |
|
120 """ |
|
121 entries = [] |
|
122 if self.__isCheckBoxSelection: |
|
123 for row in range(self.selectionList.count()): |
|
124 item = self.selectionList.item(row) |
|
125 if item.checkState() == Qt.CheckState.Checked: |
|
126 entries.append(item.text()) |
|
127 else: |
|
128 for item in self.selectionList.selectedItems(): |
|
129 entries.append(item.text()) |
|
130 return entries |