ProjectDjangoTagsMenu/DjangoTagInputDialog.py

branch
eric7
changeset 63
85418cf03fdb
parent 60
85d3931419d3
child 67
807714618a59
equal deleted inserted replaced
62:dba433b4f3d6 63:85418cf03fdb
6 """ 6 """
7 Module implementing a dialog to enter data for the creation of a tag. 7 Module implementing a dialog to enter data for the creation of a tag.
8 """ 8 """
9 9
10 from PyQt6.QtCore import Qt 10 from PyQt6.QtCore import Qt
11 from PyQt6.QtWidgets import ( 11 from PyQt6.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QLineEdit
12 QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QLineEdit
13 )
14 12
15 13
16 class DjangoTagInputDialog(QDialog): 14 class DjangoTagInputDialog(QDialog):
17 """ 15 """
18 Class implementing a dialog to enter data for the creation of a tag. 16 Class implementing a dialog to enter data for the creation of a tag.
19 """ 17 """
18
20 def __init__(self, labels, defaults=None, parent=None): 19 def __init__(self, labels, defaults=None, parent=None):
21 """ 20 """
22 Constructor 21 Constructor
23 22
24 @param labels list of labels for the entry fields 23 @param labels list of labels for the entry fields
25 @type list of str 24 @type list of str
26 @param defaults list of default values for the entry fields 25 @param defaults list of default values for the entry fields
27 @type list of str 26 @type list of str
28 @param parent reference to the parent widget 27 @param parent reference to the parent widget
29 @type QWidget 28 @type QWidget
30 @exception RuntimeError raised to signal too many labels were given 29 @exception RuntimeError raised to signal too many labels were given
31 """ 30 """
32 super().__init__(parent) 31 super().__init__(parent)
33 32
34 if len(labels) == 0 or len(labels) > 5: 33 if len(labels) == 0 or len(labels) > 5:
35 raise RuntimeError( 34 raise RuntimeError("Illegal number of labels given (max. 5 allowed)")
36 "Illegal number of labels given (max. 5 allowed)") 35
37
38 self.__inputs = [] 36 self.__inputs = []
39 37
40 self.__topLayout = QVBoxLayout(self) 38 self.__topLayout = QVBoxLayout(self)
41 for index, label in enumerate(labels): 39 for index, label in enumerate(labels):
42 self.__topLayout.addWidget(QLabel(label, self)) 40 self.__topLayout.addWidget(QLabel(label, self))
43 entry = QLineEdit(self) 41 entry = QLineEdit(self)
44 entry.setClearButtonEnabled(True) 42 entry.setClearButtonEnabled(True)
45 if defaults and index < len(defaults): 43 if defaults and index < len(defaults):
46 entry.setText(defaults[index]) 44 entry.setText(defaults[index])
47 self.__inputs.append(entry) 45 self.__inputs.append(entry)
48 self.__topLayout.addWidget(entry) 46 self.__topLayout.addWidget(entry)
49 self.__buttonBox = QDialogButtonBox( 47 self.__buttonBox = QDialogButtonBox(
50 QDialogButtonBox.StandardButton.Ok | 48 QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel,
51 QDialogButtonBox.StandardButton.Cancel,
52 Qt.Orientation.Horizontal, 49 Qt.Orientation.Horizontal,
53 self 50 self,
54 ) 51 )
55 self.__topLayout.addWidget(self.__buttonBox) 52 self.__topLayout.addWidget(self.__buttonBox)
56 53
57 self.resize(400, self.minimumSizeHint().height()) 54 self.resize(400, self.minimumSizeHint().height())
58 self.setSizeGripEnabled(True) 55 self.setSizeGripEnabled(True)
59 56
60 self.__buttonBox.accepted.connect(self.accept) 57 self.__buttonBox.accepted.connect(self.accept)
61 self.__buttonBox.rejected.connect(self.reject) 58 self.__buttonBox.rejected.connect(self.reject)
62 59
63 self.__inputs[0].selectAll() 60 self.__inputs[0].selectAll()
64 self.__inputs[0].setFocus() 61 self.__inputs[0].setFocus()
65 62
66 def getData(self): 63 def getData(self):
67 """ 64 """
68 Public method to retrieve the entered data. 65 Public method to retrieve the entered data.
69 66
70 @return tuple containing the text of all entries 67 @return tuple containing the text of all entries
71 @rtype tuple of str 68 @rtype tuple of str
72 """ 69 """
73 data = [input.text().strip() for input in self.__inputs] 70 data = [input.text().strip() for input in self.__inputs]
74 return tuple(data) 71 return tuple(data)
75 72
76 @staticmethod 73 @staticmethod
77 def getText(parent, title, labels, defaults=None): 74 def getText(parent, title, labels, defaults=None):
78 """ 75 """
79 Public static method to create the dialog and return the entered data. 76 Public static method to create the dialog and return the entered data.
80 77
81 @param parent reference to the parent widget 78 @param parent reference to the parent widget
82 @type QWidget 79 @type QWidget
83 @param title title of the dialog 80 @param title title of the dialog
84 @type str 81 @type str
85 @param labels list of labels for the entry fields 82 @param labels list of labels for the entry fields
90 and a flag indicating the acceptance state 87 and a flag indicating the acceptance state
91 @rtype tuple of (tuple of str, bool) 88 @rtype tuple of (tuple of str, bool)
92 """ 89 """
93 if defaults is None: 90 if defaults is None:
94 defaults = [] 91 defaults = []
95 92
96 dlg = DjangoTagInputDialog(labels, defaults, parent) 93 dlg = DjangoTagInputDialog(labels, defaults, parent)
97 dlg.setWindowTitle(title) 94 dlg.setWindowTitle(title)
98 if dlg.exec() == QDialog.DialogCode.Accepted: 95 if dlg.exec() == QDialog.DialogCode.Accepted:
99 return dlg.getData(), True 96 return dlg.getData(), True
100 else: 97 else:

eric ide

mercurial