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