-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExecutorFormulas.py
More file actions
executable file
·420 lines (323 loc) · 13.3 KB
/
ExecutorFormulas.py
File metadata and controls
executable file
·420 lines (323 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/python
# -*- coding: latin-1 -*-
'''
Created on 22 de jul de 2018
@author: ryan
'''
import sys
# Private Version of myFormula
from formulas import myFormula
# Comercial Version file funcoes.py
#from funcoes import myFormula
from PyQt4 import QtGui, uic
from PyQt4.QtGui import *
from types import FunctionType
class ExecutorFormulas(QtGui.QMainWindow):
def __init__(self):
super(ExecutorFormulas, self).__init__()
uic.loadUi('GUI_v01.ui', self)
self.show()
# Debug Components
self.btnDebug.clicked.connect(self.showDebug)
self.aMenuDebug.triggered.connect(self.isChecked)
self.btnDebug.setEnabled(0)
# Defining button events to callback
self.btnClear.clicked.connect(self.clear)
self.btnCalc.clicked.connect(self.calcular)
self.btnConvert.clicked.connect(self.converter)
# Set disabled fields before function to be chosen
self.btnConvert.setEnabled(0)
self.rb1.setEnabled(0)
self.rb2.setEnabled(0)
self.rb3.setEnabled(0)
# Create objFormulas from "import formulas" file formulas.py (Class myFormula)
self.objFormulas = myFormula()
#self.objFormulas = FormulasMatematicas()
self.comboBox.addItem("Selecione uma formula...")
self.myFunctions = self.methods(myFormula)
#self.myFunctions = self.methods(FormulasMatematicas)
# Loop to check all functions on myFunctions Class but __init__, *GetEnum, Convert and __methods
# and include it to the comboBox field
for func in self.myFunctions:
if func == "__init__" or "GetEnum" in func or "Convert" in func or "__" in func:
continue
self.comboBox.addItem(func)
#Defining selected field and changeIndex event to the callback
self.comboBox.setCurrentIndex(0)
self.comboBox.currentIndexChanged.connect(self.novaFormula)
#Defining TAB Order
self.setTabOrder(self.leVar1, self.leVar2)
self.setTabOrder(self.leVar2, self.leVar3)
self.setTabOrder(self.leVar3, self.leVar3)
self.setTabOrder(self.leVar3, self.comboBox)
self.setTabOrder(self.comboBox, self.rb1)
self.setTabOrder(self.rb1, self.rb2)
self.setTabOrder(self.rb2, self.rb3)
self.setTabOrder(self.rb3, self.btnClear)
self.setTabOrder(self.btnClear, self.btnCalc)
self.setTabOrder(self.btnCalc, self.btnConvert)
self.setTabOrder(self.btnConvert, self.leVar1)
self.leVar1.setFocus()
self.textEdit.setReadOnly(True)
#Defining controls and objects:
self.varis = []
self.varislen = 0
self.unity = []
self.activeUnity = 0
self.Enum = ""
self.result = ""
'''
Callback for Menu debug when it (check|uncheck)
'''
def isChecked(self):
if self.aMenuDebug.isChecked():
self.textEdit.setText("Habilitando debug")
self.btnDebug.setEnabled(1)
else:
self.textEdit.setText("Desabilitando debug")
self.btnDebug.setEnabled(0)
pass
'''
Function to collect the selected unity
'''
def unidades(self):
if self.rb1.isChecked():
self.activeUnity = self.rb1.text()
elif self.rb2.isChecked():
self.activeUnity = self.rb2.text()
else:
self.activeUnity = self.rb3.text()
'''
Function to collect all the variables in the line edit
return false if some mandatory variable is empty
'''
def variaveis(self):
#TODO: To analyse if all mandatory variables are
#int/float numbers and not strings (return false if any strings are found)
if(self.varislen == 3):
self.x = self.leVar1.text()
self.y = self.leVar2.text()
self.z = self.leVar3.text()
if (self.x == "" or self.y == "" or self.z == ""):
return False
if(self.varislen == 2):
self.x = self.leVar1.text()
self.y = self.leVar2.text()
if (self.x == "" or self.y == "" ):
return False
if(self.varislen == 1):
self.x = self.leVar1.text()
if (self.x == "" ):
return False
return True
'''
Callback for Calcular button when it got pressed
'''
def calcular(self):
if "Selecione uma formula" in self.comboBox.currentText():
self.showdialog("Por favor, selecione uma formula.", ["Fomula","Missing"])
return
# If variaveis() returns false, show up dialog error for unvalid variables
if(not self.variaveis()):
self.showdialog("Por favor, insira os dados solicitados.", self.varis)
return
# Check and set unit
self.unidades()
#Build up the string to be evaluated to call selected formula
func = "self.objFormulas.%s" % self.comboBox.currentText()
# If unity was defined we're going to return activeUnity to the Formula method
if len(self.unity) > 0:
aUnity = ", self.activeUnity"
else:
aUnity = ""
# Include variable arguments to the string to be evaluated
if self.varislen == 3:
func += "(self.x, self.y, self.z %s)" % aUnity
elif(self.varislen == 2):
func += "(self.x, self.y %s)" % aUnity
else:
func += "(self.x %s)" % aUnity
if self.aMenuDebug.isChecked():
print func
strshow, self.result = eval(func)
self.showResult(strshow + "\nResultado = %.2f" % self.result)
else:
# Executing the formula calculation under "try/except" error handling mechanism
# If some error happens during this block the "except" will handle the error
try:
# Execute the string as regular function call via eval()
strshow, self.result = eval(func)
# Print out the result from the executed formula
self.showResult(strshow + "\nResultado = %.2f" % self.result)
except:
self.showdialog("Erro Inesperado (Cags in run) durante execução da formula. Verifique a implementacao da formula.", sys.exc_info()[0])
'''
Callback for Convert button when it got pressed
'''
def converter(self):
if self.result == "":
self.textEdit.setText("Sem 'Result' na memoria.")
return
# Get actual Unit
unit = self.activeUnity
# Update unit selected
self.unidades()
# Get the new selected unit
unitToConvert = self.activeUnity
if unit == unitToConvert:
return
if "Converter" in dir(self.objFormulas):
# Call the Convert method to process de unit conversion
# Should have the case implemented
result = self.objFormulas.Converter(self.result, unit, unitToConvert)
self.textEdit.setText("Convertendo:\nResultado %.2f %s para %s\n%.2f %s" % \
(self.result,unit, unitToConvert, result, unitToConvert))
else:
self.showdialog("Funcao Converter nao disponivel. Verifique a implementacao da funcao de conversao.", ["self.objFormulas.Converter", "Missing"] )
result = 0
self.result = result
'''
Callback for new formula selected on the combobox
'''
def novaFormula(self):
# If select the neutral option on the combobox ("Selecione uma formula...")
if self.comboBox.currentText() == "Selecione uma formula...":
self.clear()
return
#Clean up all fields
self.clear()
if self.aMenuDebug.isChecked():
func = "self.objFormulas.%sGetEnum()" % self.comboBox.currentText()
self.varis, self.Enum, self.unity = eval(func)
self.varislen = len(self.varis)
self.lblEnum.setText(self.Enum)
self.setlbl()
self.setrb()
else:
# Executing the formula GetEnum under try/except error handling mechanism
# If some error happens during this block the except will handle the error
try:
func = "self.objFormulas.%sGetEnum()" % self.comboBox.currentText()
self.varis, self.Enum, self.unity = eval(func)
self.varislen = len(self.varis)
self.lblEnum.setText(self.Enum)
self.setlbl()
self.setrb()
except:
self.showdialog("Erro Inesperado (Cags in run) durante execução da GetNum.\
Verifique a implementação da função GetNum.", sys.exc_info()[0])
'''
Function to set all the variables names and labels.
This function will also disable/enable unused/used var fields
'''
def setlbl(self):
# If we have 3 variables for this formula
if (self.varislen == 3):
self.lblVar1.setText(self.varis[0])
self.lblVar2.setText(self.varis[1])
self.lblVar3.setText(self.varis[2])
self.leVar2.setEnabled(1)
self.leVar3.setEnabled(1)
# If we have 2 variables for this formula
elif(self.varislen == 2):
self.lblVar1.setText(self.varis[0])
self.lblVar2.setText(self.varis[1])
self.lblVar3.setText("")
self.leVar2.setEnabled(1)
self.leVar3.setDisabled(1)
# If we have 1 variables for this formula
else:
self.lblVar1.setText(self.varis[0])
self.lblVar2.setText("")
self.lblVar3.setText("")
self.leVar2.setDisabled(1)
self.leVar3.setDisabled(1)
'''
Function to set all the radio button unity names and labels.
This function will also disable/enable unused/used unity fields
'''
def setrb(self):
# If we have 3 units for this formula
if (len(self.unity) == 3):
self.rb1.setText(self.unity[0])
self.rb2.setText(self.unity[1])
self.rb3.setText(self.unity[2])
self.rb1.setEnabled(1)
self.rb2.setEnabled(1)
self.rb3.setEnabled(1)
self.btnConvert.setEnabled(1)
# If we have 2 units for this formula
if (len(self.unity) == 2):
self.rb1.setText(self.unity[0])
self.rb2.setText(self.unity[1])
self.rb3.setText("")
self.rb1.setEnabled(1)
self.rb2.setEnabled(1)
self.rb3.setEnabled(0)
self.btnConvert.setEnabled(1)
# If we have 1 unit for this formula
if(len(self.unity) == 1):
self.rb1.setText(self.unity[0])
self.rb2.setText("")
self.rb3.setText("")
self.rb1.setEnabled(1)
self.rb2.setEnabled(0)
self.rb3.setEnabled(0)
self.btnConvert.setEnabled(0)
# If we have no unit for this formula
if(len(self.unity) == 0):
self.rb1.setEnabled(0)
self.rb2.setEnabled(0)
self.rb3.setEnabled(0)
self.btnConvert.setEnabled(0)
self.rb1.setText("")
self.rb2.setText("")
self.rb3.setText("")
'''
Function to clear the variables fields
'''
def clear(self):
self.leVar1.setText("")
self.leVar2.setText("")
self.leVar3.setText("")
self.textEdit.setText("")
self.result = ""
'''
Function to print the output to the user at the textEdit
'''
def showResult(self, strshow):
self.textEdit.setText("%s" % strshow )
'''
Function to show the error when processing the formula
'''
def showdialog(self, message, details):
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText(message)
msg.setInformativeText("Detalhes")
msg.setWindowTitle("Erro de Dados")
msg.setDetailedText("Detalhes do erro:\n%s" % details)
msg.setStandardButtons(QMessageBox.Cancel)
msg.exec_()
'''
Function to show the debug status
'''
def showDebug(self):
#self.unidades()
debug = "Var1 = '%s' | Var2 = '%s' | Var3 = '%s'\nFunc = '%s'\nResult = %s | Unidade = %s\nVaris %s\nUnity %s" % \
(self.leVar1.text(),self.leVar2.text(), self.leVar3.text(),
self.comboBox.currentText(), self.result, self.activeUnity,
self.varis, self.unity)
self.textEdit.setText(debug)
#self.textEdit.setText(self.lineEdit.text())
#self.textEdit.setText(self.lineEdit.text())
'''
Function to check all the methods (functions) in a class
'''
def methods(self, cls):
return [x for x, y in cls.__dict__.items() if type(y) == FunctionType]
#Main function to start the program
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myApp = ExecutorFormulas()
sys.exit(app.exec_())