eric6/E5Gui/E5TextInputDialog.py

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

eric ide

mercurial