src/eric7/EricWidgets/EricTextInputDialog.py

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

eric ide

mercurial