1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to select one entry from a list of strings. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import pyqtSlot |
|
11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from .Ui_E5ComboSelectionDialog import Ui_E5ComboSelectionDialog |
|
14 |
|
15 |
|
16 class E5ComboSelectionDialog(QDialog, Ui_E5ComboSelectionDialog): |
|
17 """ |
|
18 Class implementing a dialog to select one entry from a list of strings. |
|
19 """ |
|
20 def __init__(self, entries, title="", message="", parent=None): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param entries list of entries to select from |
|
25 @type list of str or list of tuples of (str, any) |
|
26 @param title title of the dialog (defaults to "") |
|
27 @type str (optional) |
|
28 @param message message to be show in the dialog (defaults to "") |
|
29 @type str (optional) |
|
30 @param parent reference to the parent widget (defaults to None) |
|
31 @type QWidget (optional) |
|
32 """ |
|
33 super().__init__(parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 for entry in entries: |
|
37 if isinstance(entry, tuple): |
|
38 self.selectionComboBox.addItem(*entry) |
|
39 else: |
|
40 self.selectionComboBox.addItem(entry) |
|
41 |
|
42 self.on_selectionComboBox_currentTextChanged( |
|
43 self.selectionComboBox.itemText(0)) |
|
44 |
|
45 msh = self.minimumSizeHint() |
|
46 self.resize(max(self.width(), msh.width()), msh.height()) |
|
47 |
|
48 @pyqtSlot(str) |
|
49 def on_selectionComboBox_currentTextChanged(self, txt): |
|
50 """ |
|
51 Private slot to react upon changes of the selected entry. |
|
52 |
|
53 @param txt text of the selected entry |
|
54 @type str |
|
55 """ |
|
56 self.buttonBox.button( |
|
57 QDialogButtonBox.StandardButton.Ok).setEnabled(bool(txt)) |
|
58 |
|
59 def getSelection(self): |
|
60 """ |
|
61 Public method to retrieve the selected item and its data. |
|
62 |
|
63 @return tuple containing the selected entry and its associated data |
|
64 @rtype tuple of (str, any) |
|
65 """ |
|
66 return ( |
|
67 self.selectionComboBox.currentText(), |
|
68 self.selectionComboBox.currentData() |
|
69 ) |
|