|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2018 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.__label.setWordWrap(True) |
|
37 self.__layout.addWidget(self.__label) |
|
38 |
|
39 self.__lineEdit = E5ClearableLineEdit(self) |
|
40 self.__layout.addWidget(self.__lineEdit) |
|
41 |
|
42 self.__buttonBox = QDialogButtonBox( |
|
43 QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self) |
|
44 self.__layout.addWidget(self.__buttonBox) |
|
45 |
|
46 self.__buttonBox.accepted.connect(self.accept) |
|
47 self.__buttonBox.rejected.connect(self.reject) |
|
48 |
|
49 msh = self.minimumSizeHint() |
|
50 self.resize(max(self.width(), msh.width()), msh.height()) |
|
51 |
|
52 def setTextEchoMode(self, echoMode): |
|
53 """ |
|
54 Public method to set the echo mode of the line edit. |
|
55 |
|
56 @param echoMode echo mode of the line edit |
|
57 @type QLineEdit.EchoMode |
|
58 """ |
|
59 self.__lineEdit.setEchoMode(echoMode) |
|
60 |
|
61 def textEchoMode(self): |
|
62 """ |
|
63 Public method to get the current echo mode of the line edit. |
|
64 |
|
65 @return echo mode of the line edit |
|
66 @rtype QLineEdit.EchoMode |
|
67 """ |
|
68 return self.__lineEdit.echoMode() |
|
69 |
|
70 def setTextValue(self, text): |
|
71 """ |
|
72 Public method to set the text of the line edit. |
|
73 |
|
74 @param text text for the line edit |
|
75 @type str |
|
76 """ |
|
77 self.__lineEdit.setText(text) |
|
78 |
|
79 def textValue(self): |
|
80 """ |
|
81 Public method to get the text of the line edit. |
|
82 |
|
83 @return text of the line edit |
|
84 @rtype str |
|
85 """ |
|
86 return self.__lineEdit.text() |
|
87 |
|
88 def setLabelText(self, text): |
|
89 """ |
|
90 Public method to set the label text. |
|
91 |
|
92 @param text label text |
|
93 @type str |
|
94 """ |
|
95 self.__label.setText(text) |
|
96 |
|
97 def labelText(self): |
|
98 """ |
|
99 Public method to get the current label text. |
|
100 |
|
101 @return current label text |
|
102 @rtype str |
|
103 """ |
|
104 return self.label.text() |
|
105 |
|
106 |
|
107 def getText(parent, title, label, mode=QLineEdit.Normal, text=""): |
|
108 """ |
|
109 Function to get create a dialog to enter some text and return it. |
|
110 |
|
111 @param parent reference to the parent widget |
|
112 @type QWidget |
|
113 @param title title of the dialog |
|
114 @type str |
|
115 @param label label of the dialog |
|
116 @type str |
|
117 @param mode echo mode of the line edit |
|
118 @type QLineEdit.EchoMode |
|
119 @param text initial text of the line edit |
|
120 @type str |
|
121 @return tuple containing a flag indicating the dialog was accepted and the |
|
122 entered text |
|
123 @rtype tuple of (bool, str) |
|
124 """ |
|
125 dlg = E5TextInputDialog(parent) |
|
126 dlg.setWindowTitle(title) |
|
127 dlg.setLabelText(label) |
|
128 dlg.setTextEchoMode(mode) |
|
129 dlg.setTextValue(text) |
|
130 if dlg.exec_() == QDialog.Accepted: |
|
131 return True, dlg.textValue() |
|
132 else: |
|
133 return False, "" |