|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2021 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, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param entries list of entries to select from |
|
30 @type list of str |
|
31 @param selectionMode selection mode for the list |
|
32 @type QAbstractItemView.SelectionMode |
|
33 @param title title of the dialog |
|
34 @type str |
|
35 @param message message to be show in the dialog |
|
36 @type str |
|
37 @param checkBoxSelection flag indicating to select items via their |
|
38 checkbox |
|
39 @type bool |
|
40 @param parent reference to the parent widget |
|
41 @type QWidget |
|
42 """ |
|
43 super().__init__(parent) |
|
44 self.setupUi(self) |
|
45 |
|
46 if title: |
|
47 self.setWindowTitle(title) |
|
48 if message: |
|
49 self.messageLabel.setText(message) |
|
50 |
|
51 self.__checkCount = 0 |
|
52 self.__isCheckBoxSelection = checkBoxSelection |
|
53 if self.__isCheckBoxSelection: |
|
54 self.selectionList.setSelectionMode( |
|
55 QAbstractItemView.SelectionMode.NoSelection) |
|
56 for entry in entries: |
|
57 itm = QListWidgetItem(entry) |
|
58 itm.setFlags(Qt.ItemFlag.ItemIsUserCheckable | |
|
59 Qt.ItemFlag.ItemIsEnabled) |
|
60 itm.setCheckState(Qt.CheckState.Unchecked) |
|
61 self.selectionList.addItem(itm) |
|
62 else: |
|
63 self.selectionList.setSelectionMode(selectionMode) |
|
64 self.selectionList.addItems(entries) |
|
65 |
|
66 self.buttonBox.button( |
|
67 QDialogButtonBox.StandardButton.Ok).setEnabled(False) |
|
68 |
|
69 @pyqtSlot() |
|
70 def on_selectionList_itemSelectionChanged(self): |
|
71 """ |
|
72 Private slot handling a change of the selection. |
|
73 """ |
|
74 if not self.__isCheckBoxSelection: |
|
75 self.buttonBox.button( |
|
76 QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
77 len(self.selectionList.selectedItems()) > 0) |
|
78 |
|
79 def on_selectionList_itemChanged(self, itm): |
|
80 """ |
|
81 Private slot handling a change of an item. |
|
82 |
|
83 @param itm reference to the changed item |
|
84 @type QListWidgetItem |
|
85 """ |
|
86 if self.__isCheckBoxSelection: |
|
87 if itm.checkState() == Qt.CheckState.Checked: |
|
88 self.__checkCount += 1 |
|
89 else: |
|
90 self.__checkCount -= 1 |
|
91 self.buttonBox.button( |
|
92 QDialogButtonBox.StandardButton.Ok).setEnabled( |
|
93 self.__checkCount > 0) |
|
94 |
|
95 def getSelection(self): |
|
96 """ |
|
97 Public method to retrieve the selected items. |
|
98 |
|
99 @return selected entries |
|
100 @rtype list of str |
|
101 """ |
|
102 entries = [] |
|
103 if self.__isCheckBoxSelection: |
|
104 for row in range(self.selectionList.count()): |
|
105 item = self.selectionList.item(row) |
|
106 if item.checkState() == Qt.CheckState.Checked: |
|
107 entries.append(item.text()) |
|
108 else: |
|
109 for item in self.selectionList.selectedItems(): |
|
110 entries.append(item.text()) |
|
111 return entries |