eric6/E5Gui/E5ComboSelectionDialog.py

changeset 8038
73ec029d4107
child 8143
2c730d5fd177
equal deleted inserted replaced
8037:cf0e5b8cd23a 8038:73ec029d4107
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 PyQt5.QtCore import pyqtSlot
11 from PyQt5.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(E5ComboSelectionDialog, self).__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(QDialogButtonBox.Ok).setEnabled(bool(txt))
57
58 def getSelection(self):
59 """
60 Public method to retrieve the selected item and its data.
61
62 @return tuple containing the selected entry and its associated data
63 @rtype tuple of (str, any)
64 """
65 return (
66 self.selectionComboBox.currentText(),
67 self.selectionComboBox.currentData()
68 )

eric ide

mercurial