|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2003 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the input dialog wizard dialog. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import pyqtSlot |
|
15 from PyQt5.QtGui import QDoubleValidator |
|
16 from PyQt5.QtWidgets import QLineEdit, QDialog, QInputDialog, QDialogButtonBox |
|
17 |
|
18 from .Ui_InputDialogWizardDialog import Ui_InputDialogWizardDialog |
|
19 |
|
20 |
|
21 class InputDialogWizardDialog(QDialog, Ui_InputDialogWizardDialog): |
|
22 """ |
|
23 Class implementing the input dialog wizard dialog. |
|
24 |
|
25 It displays a dialog for entering the parameters |
|
26 for the QInputDialog code generator. |
|
27 """ |
|
28 def __init__(self, parent=None): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param parent parent widget (QWidget) |
|
33 """ |
|
34 super(InputDialogWizardDialog, self).__init__(parent) |
|
35 self.setupUi(self) |
|
36 |
|
37 # set the validators for the double line edots |
|
38 self.eDoubleDefault.setValidator( |
|
39 QDoubleValidator(-2147483647, 2147483647, 99, self.eDoubleDefault)) |
|
40 self.eDoubleFrom.setValidator( |
|
41 QDoubleValidator(-2147483647, 2147483647, 99, self.eDoubleFrom)) |
|
42 self.eDoubleTo.setValidator( |
|
43 QDoubleValidator(-2147483647, 2147483647, 99, self.eDoubleTo)) |
|
44 |
|
45 self.bTest = self.buttonBox.addButton( |
|
46 self.tr("Test"), QDialogButtonBox.ActionRole) |
|
47 |
|
48 msh = self.minimumSizeHint() |
|
49 self.resize(max(self.width(), msh.width()), msh.height()) |
|
50 |
|
51 @pyqtSlot(bool) |
|
52 def on_rItem_toggled(self, checked): |
|
53 """ |
|
54 Private slot to perform actions dependant on the item type selection. |
|
55 |
|
56 @param checked flag indicating the checked state (boolean) |
|
57 """ |
|
58 self.bTest.setEnabled(not checked) |
|
59 |
|
60 def on_buttonBox_clicked(self, button): |
|
61 """ |
|
62 Private slot called by a button of the button box clicked. |
|
63 |
|
64 @param button button that was clicked (QAbstractButton) |
|
65 """ |
|
66 if button == self.bTest: |
|
67 self.on_bTest_clicked() |
|
68 |
|
69 @pyqtSlot() |
|
70 def on_bTest_clicked(self): |
|
71 """ |
|
72 Private method to test the selected options. |
|
73 """ |
|
74 if self.rText.isChecked(): |
|
75 if self.rEchoNormal.isChecked(): |
|
76 echomode = QLineEdit.Normal |
|
77 elif self.rEchoNoEcho.isChecked(): |
|
78 echomode = QLineEdit.NoEcho |
|
79 else: |
|
80 echomode = QLineEdit.Password |
|
81 QInputDialog.getText( |
|
82 None, |
|
83 self.eCaption.text(), |
|
84 self.eLabel.text(), |
|
85 echomode, |
|
86 self.eTextDefault.text()) |
|
87 elif self.rInteger.isChecked(): |
|
88 QInputDialog.getInt( |
|
89 None, |
|
90 self.eCaption.text(), |
|
91 self.eLabel.text(), |
|
92 self.sIntDefault.value(), |
|
93 self.sIntFrom.value(), |
|
94 self.sIntTo.value(), |
|
95 self.sIntStep.value()) |
|
96 elif self.rDouble.isChecked(): |
|
97 try: |
|
98 doubleDefault = float(self.eDoubleDefault.text()) |
|
99 except ValueError: |
|
100 doubleDefault = 0 |
|
101 try: |
|
102 doubleFrom = float(self.eDoubleFrom.text()) |
|
103 except ValueError: |
|
104 doubleFrom = -2147483647 |
|
105 try: |
|
106 doubleTo = float(self.eDoubleTo.text()) |
|
107 except ValueError: |
|
108 doubleTo = 2147483647 |
|
109 QInputDialog.getDouble( |
|
110 None, |
|
111 self.eCaption.text(), |
|
112 self.eLabel.text(), |
|
113 doubleDefault, |
|
114 doubleFrom, |
|
115 doubleTo, |
|
116 self.sDoubleDecimals.value()) |
|
117 |
|
118 def getCode(self, indLevel, indString): |
|
119 """ |
|
120 Public method to get the source code for Qt4/Qt5. |
|
121 |
|
122 @param indLevel indentation level (int) |
|
123 @param indString string used for indentation (space or tab) (string) |
|
124 @return generated code (string) |
|
125 """ |
|
126 # calculate our indentation level and the indentation string |
|
127 il = indLevel + 1 |
|
128 istring = il * indString |
|
129 estring = os.linesep + indLevel * indString |
|
130 |
|
131 # now generate the code |
|
132 if self.parentSelf.isChecked(): |
|
133 parent = "self" |
|
134 elif self.parentNone.isChecked(): |
|
135 parent = "None" |
|
136 elif self.parentOther.isChecked(): |
|
137 parent = self.parentEdit.text() |
|
138 if parent == "": |
|
139 parent = "None" |
|
140 |
|
141 resvar = self.eResultVar.text() |
|
142 if not resvar: |
|
143 resvar = "result" |
|
144 code = '{0}, ok = QInputDialog.'.format(resvar) |
|
145 if self.rText.isChecked(): |
|
146 code += 'getText({0}{1}'.format(os.linesep, istring) |
|
147 code += '{0},{1}{2}'.format(parent, os.linesep, istring) |
|
148 code += 'self.tr("{0}"),{1}{2}'.format( |
|
149 self.eCaption.text(), os.linesep, istring) |
|
150 code += 'self.tr("{0}"),{1}{2}'.format( |
|
151 self.eLabel.text(), os.linesep, istring) |
|
152 if self.rEchoNormal.isChecked(): |
|
153 code += 'QLineEdit.Normal' |
|
154 elif self.rEchoNoEcho.isChecked(): |
|
155 code += 'QLineEdit.NoEcho' |
|
156 else: |
|
157 code += 'QLineEdit.Password' |
|
158 if self.eTextDefault.text(): |
|
159 code += ',{0}{1}self.tr("{2}")'.format( |
|
160 os.linesep, istring, self.eTextDefault.text()) |
|
161 code += '){0}'.format(estring) |
|
162 elif self.rInteger.isChecked(): |
|
163 code += 'getInt({0}{1}'.format(os.linesep, istring) |
|
164 code += '{0},{1}{2}'.format(parent, os.linesep, istring) |
|
165 code += 'self.tr("{0}"),{1}{2}'.format( |
|
166 self.eCaption.text(), os.linesep, istring) |
|
167 code += 'self.tr("{0}"),{1}{2}'.format( |
|
168 self.eLabel.text(), os.linesep, istring) |
|
169 code += '{0:d}, {1:d}, {2:d}, {3:d}){4}'.format( |
|
170 self.sIntDefault.value(), self.sIntFrom.value(), |
|
171 self.sIntTo.value(), self.sIntStep.value(), estring) |
|
172 elif self.rDouble.isChecked(): |
|
173 try: |
|
174 doubleDefault = float(self.eDoubleDefault.text()) |
|
175 except ValueError: |
|
176 doubleDefault = 0 |
|
177 try: |
|
178 doubleFrom = float(self.eDoubleFrom.text()) |
|
179 except ValueError: |
|
180 doubleFrom = -2147483647 |
|
181 try: |
|
182 doubleTo = float(self.eDoubleTo.text()) |
|
183 except ValueError: |
|
184 doubleTo = 2147483647 |
|
185 code += 'getDouble({0}{1}'.format(os.linesep, istring) |
|
186 code += '{0},{1}{2}'.format(parent, os.linesep, istring) |
|
187 code += 'self.tr("{0}"),{1}{2}'.format( |
|
188 self.eCaption.text(), os.linesep, istring) |
|
189 code += 'self.tr("{0}"),{1}{2}'.format( |
|
190 self.eLabel.text(), os.linesep, istring) |
|
191 code += '{0}, {1}, {2}, {3:d}){4}'.format( |
|
192 doubleDefault, doubleFrom, doubleTo, |
|
193 self.sDoubleDecimals.value(), estring) |
|
194 elif self.rItem.isChecked(): |
|
195 code += 'getItem({0}{1}'.format(os.linesep, istring) |
|
196 code += '{0},{1}{2}'.format(parent, os.linesep, istring) |
|
197 code += 'self.tr("{0}"),{1}{2}'.format( |
|
198 self.eCaption.text(), os.linesep, istring) |
|
199 code += 'self.tr("{0}"),{1}{2}'.format( |
|
200 self.eLabel.text(), os.linesep, istring) |
|
201 code += '{0},{1}{2}'.format( |
|
202 self.eVariable.text(), os.linesep, istring) |
|
203 code += '{0:d}, {1}){2}'.format( |
|
204 self.sCurrentItem.value(), self.cEditable.isChecked(), estring) |
|
205 |
|
206 return code |