1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2018 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter some text. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtWidgets import ( |
|
11 QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QLineEdit |
|
12 ) |
|
13 |
|
14 |
|
15 class EricTextInputDialog(QDialog): |
|
16 """ |
|
17 Class implementing a dialog to enter some text. |
|
18 """ |
|
19 def __init__(self, parent=None): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param parent reference to the parent widget |
|
24 @type QWidget |
|
25 """ |
|
26 super().__init__(parent) |
|
27 |
|
28 self.setMaximumWidth(600) |
|
29 |
|
30 self.__layout = QVBoxLayout(self) |
|
31 |
|
32 self.__label = QLabel(self) |
|
33 self.__layout.addWidget(self.__label) |
|
34 |
|
35 self.__lineEdit = QLineEdit(self) |
|
36 self.__lineEdit.setClearButtonEnabled(True) |
|
37 self.__layout.addWidget(self.__lineEdit) |
|
38 |
|
39 self.__buttonBox = QDialogButtonBox( |
|
40 QDialogButtonBox.StandardButton.Ok | |
|
41 QDialogButtonBox.StandardButton.Cancel, self) |
|
42 self.__layout.addWidget(self.__buttonBox) |
|
43 |
|
44 self.__buttonBox.accepted.connect(self.accept) |
|
45 self.__buttonBox.rejected.connect(self.reject) |
|
46 |
|
47 msh = self.minimumSizeHint() |
|
48 self.resize(max(self.width(), msh.width()), msh.height()) |
|
49 |
|
50 def setTextEchoMode(self, echoMode): |
|
51 """ |
|
52 Public method to set the echo mode of the line edit. |
|
53 |
|
54 @param echoMode echo mode of the line edit |
|
55 @type QLineEdit.EchoMode |
|
56 """ |
|
57 self.__lineEdit.setEchoMode(echoMode) |
|
58 |
|
59 def textEchoMode(self): |
|
60 """ |
|
61 Public method to get the current echo mode of the line edit. |
|
62 |
|
63 @return echo mode of the line edit |
|
64 @rtype QLineEdit.EchoMode |
|
65 """ |
|
66 return self.__lineEdit.echoMode() |
|
67 |
|
68 def setTextValue(self, text): |
|
69 """ |
|
70 Public method to set the text of the line edit. |
|
71 |
|
72 @param text text for the line edit |
|
73 @type str |
|
74 """ |
|
75 self.__lineEdit.setText(text) |
|
76 |
|
77 def textValue(self): |
|
78 """ |
|
79 Public method to get the text of the line edit. |
|
80 |
|
81 @return text of the line edit |
|
82 @rtype str |
|
83 """ |
|
84 return self.__lineEdit.text() |
|
85 |
|
86 def setLabelText(self, text): |
|
87 """ |
|
88 Public method to set the label text. |
|
89 |
|
90 @param text label text |
|
91 @type str |
|
92 """ |
|
93 self.__label.setText(text) |
|
94 |
|
95 msh = self.minimumSizeHint() |
|
96 labelSizeHint = self.__label.sizeHint() |
|
97 self.resize(max(self.width(), msh.width(), labelSizeHint.width()), |
|
98 msh.height()) |
|
99 |
|
100 def labelText(self): |
|
101 """ |
|
102 Public method to get the current label text. |
|
103 |
|
104 @return current label text |
|
105 @rtype str |
|
106 """ |
|
107 return self.label.text() |
|
108 |
|
109 |
|
110 def getText(parent, title, label, mode=QLineEdit.EchoMode.Normal, text="", |
|
111 minimumWidth=300): |
|
112 """ |
|
113 Function to get create a dialog to enter some text and return it. |
|
114 |
|
115 @param parent reference to the parent widget |
|
116 @type QWidget |
|
117 @param title title of the dialog |
|
118 @type str |
|
119 @param label label of the dialog |
|
120 @type str |
|
121 @param mode echo mode of the line edit |
|
122 @type QLineEdit.EchoMode |
|
123 @param text initial text of the line edit |
|
124 @type str |
|
125 @param minimumWidth minimum width of the dialog |
|
126 @type int |
|
127 @return tuple containing a flag indicating the dialog was accepted and the |
|
128 entered text |
|
129 @rtype tuple of (bool, str) |
|
130 """ |
|
131 dlg = EricTextInputDialog(parent) |
|
132 dlg.setWindowTitle(title) |
|
133 dlg.setLabelText(label) |
|
134 dlg.setTextEchoMode(mode) |
|
135 dlg.setTextValue(text) |
|
136 dlg.setMinimumWidth(minimumWidth) |
|
137 |
|
138 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
139 return True, dlg.textValue() |
|
140 else: |
|
141 return False, "" |
|