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