-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_application.py
executable file
·442 lines (329 loc) · 15.5 KB
/
test_application.py
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/bin/env python
import sys
import random
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QStringList, pyqtSignature
from PyQt4.QtGui import QPixmap
from xml.dom import minidom, Node
sys.path.insert(0, "lib")
from gui_test_application import Ui_TestApplicationGui
ARQUIVO_DE_TESTES = "res/tests.xml"
IMAGES_DIR = "res/test_application_images"
class userAnswers ():
deutan = 0
protan = 0
colorblind = 0
mono = 0
normal = 0
t_deutan = 0
t_protan = 0
t_colorblind = 0
t_mono = 0
t_normal = 0
total_tests = 0
def print_(self):
print "Colorblind %s" % self.colorblind
print "Deutan %s" % self.deutan
print "Protan %s" % self.protan
print "Mono %s" % self.mono
print "Normal %s" % self.normal
print "Total Colorblind %s" % self.t_colorblind
print "Total Deutan %s" % self.t_deutan
print "Total Protan %s" % self.t_protan
print "Total Mono %s" % self.t_mono
print "Total Normal %s" % self.t_normal
print "Test total: %s" % self.total_tests
def askToSave(self):
fileName = QtGui.QFileDialog.getSaveFileName(None, "Salvar como...",".xml", "Arquivo XML (*.xml)")
if fileName != None:
fileHandle = open ( fileName, 'w' )
fileHandle.write ( '<?xml version="1.0" encoding="UTF-8"?>\n' )
fileHandle.write ( '<teste>\n' )
fileHandle.write ( ' <variaveis daltonismo="' + str(100*self.colorblind/self.t_colorblind) + '" protan="' + str(100*self.protan/self.t_protan) + '" deutan="' + str(100*self.deutan/self.t_deutan) + '" normalismo="' + str(100*self.normal/self.t_normal) + '"/>\n' )
fileHandle.write ( '</teste>\n\n' )
fileHandle.close()
def text(self):
return self.unify ("<font color='white' size='+2'>" +
"Resultados: <BR>" +
" Colorblind: " + str(100*self.colorblind/self.t_colorblind) + "% <BR>" +
" Deutan: " + str(100*self.deutan/self.t_deutan) + "% <BR>" +
" Protan: " + str(100*self.protan/self.t_protan) + "% <BR>" +
" Mono: " + str(100*self.mono/self.t_mono) + "% <BR>" +
" Normal: " + str(100*self.normal/self.t_normal) + "% <BR>" +
"</font>")
def unify (self, a):
return a
class type ():
answers = None
def __init__(self, ua):
self.answers = {}
self.ua = ua
def addComplexAnswer(self, kind, weight, response, main_weight):
a = {}
a['kind'] = kind
a['weight'] = weight
a['main_weight'] = main_weight
self.answers[response] = a
print '\t\t\t Expected answer: %s/%s (with #%s/colorblind: %s)' % (a['kind'], response, a['weight'], a['main_weight'])
def addAnswer(self, kind, response, weight):
a = {}
a['kind'] = kind
a['weight'] = weight
self.answers[response] = a
print '\t\t\t Expected answer: %s/%s (with #%s)' % (a['kind'], response, a['weight'])
def verifyAnswer(self, response):
d = 0
p = 0
c = 0
n = 0
m = 0
for (name, a) in self.answers.items():
#print name
if a['kind'] == 'deutan':
if d < int(a['weight']):
ua.t_deutan = int(ua.t_deutan) + int(a['weight']) - int(d)
d = int(a['weight'])
elif a['kind'] == 'protan':
if p < int(a['weight']):
ua.t_protan = ua.t_protan + int(a['weight']) - p
p = int(a['weight'])
elif a['kind'] == 'colorblind':
if c < int(a['weight']):
ua.t_colorblind = ua.t_colorblind + int(a['weight']) - c
c = int(a['weight'])
elif a['kind'] == 'normal':
if n < int(a['weight']):
ua.t_normal = ua.t_normal + int(a['weight']) - n
n = int(a['weight'])
elif a['kind'] == 'mono':
if m < int(a['weight']):
ua.t_mono = ua.t_mono + int(a['weight']) - m
m = int(a['weight'])
if self.answers.has_key(str(response)):
a = self.answers[str(response)]
if a['kind'] == 'deutan':
ua.deutan = ua.deutan + int(a['weight'])
elif a['kind'] == 'protan':
ua.protan = ua.protan + int(a['weight'])
elif a['kind'] == 'colorblind':
ua.colorblind = ua.colorblind + int(a['weight'])
elif a['kind'] == 'normal':
ua.normal = ua.normal + int(a['weight'])
elif a['kind'] == 'mono':
ua.mono = ua.mono + int(a['weight'])
print "Its a valid answer: %s" % self.answers[str(response)]
if a.has_key('main_weight'):
ua.colorblind = ua.colorblind + int(a['main_weight'])
else:
#ua.mono = ua.mono + 1
print "Sorry but, Answer not found or not an colorblind kind :(\nValid answers are: "
for (name, value) in self.answers.items():
print "Value: %s Data: %s" % (name, value)
class test ():
name = None
type = None
question = None
uri = None
preservePosition = False
def __init__(self, ua):
self.type = type(ua)
self.preservePosition = False
def setQuestion(self, question):
self.question = question
def getQuestion(self):
return self.question
def setImage(self, uri):
self.uri = uri
def getImage(self):
return self.uri
def verifyAnswer(self, answer):
return self.type.verifyAnswer(answer)
def setPreservePosition(self, bool):
self.preservePosition = bool
def getPreservePosition(self):
return self.preservePosition
class tests ():
def __init__(self, ua):
# Parser xml file
self.doc = minidom.parse(ARQUIVO_DE_TESTES)
# Init the tests array
self.tests = []
# Init the actual test
self.actual = 0
# User answer
self.ua = ua
# Tests Order
self.order = []
# Insert tests.
node = self.doc.documentElement
for child in node.childNodes:
self.insert(child)
self.select_order()
def select_order(self):
print "Teste: " + str(len(self.tests))
total = 0
pos = 0
for i in range(0, len(self.tests)):
self.order.insert(len(self.order), -1)
for i in self.tests:
print "Hein?"
if i.getPreservePosition() == True:
print "Han?"
self.order[pos] = i
pos = pos + 1
else:
total = total + 1
for i in self.tests:
if i.getPreservePosition() == False:
#rand_pos = 0
rand_pos = random.randrange(0, total, 2)
while self.order[rand_pos] != -1:
rand_pos = rand_pos + 1
if rand_pos >= len(self.tests):
rand_pos = 0
print "bonga! rand_pos: " + str(rand_pos)
self.order[rand_pos] = i
print "+++>>> " + str(self.order)
self.tests = self.order
def insert (self, node):
if node.nodeType == Node.ELEMENT_NODE:
print 'Element name: %s' % node.nodeName
t = test(self.ua)
self.tests.insert(len(self.tests), t)
# List attributes.
for (name, value) in node.attributes.items():
#print '\tAttr -- Name: %s Value: %s' % (name, unicode(value))
if name == 'image':
t.setImage(value)
if name == 'question':
t.setQuestion(value)
if name == 'preservePosition':
t.setPreservePosition(bool(value))
print "asdfasdfasdfas: " + str(t.getPreservePosition())
for child in node.childNodes:
if child.nodeType == Node.ELEMENT_NODE:
print '\tSub element name: %s' % child.nodeName
main_weight_ = None
main_response_ = None
main_kind_ = child.nodeName
for (name, value) in child.attributes.items():
if name == 'weigth':
main_weight_ = value
if name == 'answer':
main_response_ = value
print '\t\tAttr -- Name: %s Value: %s' % (name, value)
for kind in child.childNodes:
# protans/deutans saves weigth and correct values.
kind_ = None
weight_ = None
response_ = None
if kind.nodeType == Node.ELEMENT_NODE:
print '\t\tSub Sub element name: %s' % kind.nodeName
kind_ = kind.nodeName
for (name, value) in kind.attributes.items():
if name == 'weigth':
weight_ = value
if name == 'answer':
response_ = value
print '\t\t\tAttr -- Name: %s Value: %s' % (name, value)
t.type.addComplexAnswer(kind_, weight_, response_, main_weight_)
if main_weight_ != None and main_response_ != None and main_kind_ != None:
t.type.addAnswer(main_kind_, main_response_, main_weight_)
def getImage (self):
return self.testes[self.actual].getImage()
def getNext (self):
self.actual = self.actual + 1
return self.tests[self.actual]
def getActual (self):
return self.tests[self.actual]
def getPos (self):
return self.actual
def getLen (self):
return len(self.tests)
class ui (QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui_ = Ui_TestApplicationGui()
self.ui_.setupUi(self)
self.t = t
self.ua = ua
print "actual: " + str(t.getActual()) + " // " + str(t.order)
self.drawImage(t.getActual().getImage());
self.setPergunta(self.t.getActual().getQuestion())
self.connect(self.ui_.nextBnt, QtCore.SIGNAL('clicked()'),
self.next)
self.connect(self.ui_.quitBnt, QtCore.SIGNAL('clicked()'), self.fileExit)
# def __init__(self, t, ua, parent=None, name=None, fl=0):
#Ui_Projeto_Piloto.__init__(self, parent, name, fl)
#self.setupUi(self)
#self.t = t
#self.ua = ua
#print "actual: " + str(t.getActual()) + " // " + str(t.order)
#self.drawImage(t.getActual().getImage());
#self.setPergunta(self.t.getActual().getQuestion())
#self.connect(self.nextBnt, SIGNAL('clicked()'), self.next)
#self.connect(self.quitBnt, SIGNAL('clicked()'), self.fileExit)
def fileExit(self):
quit()
def drawImage (self, uri):
uri = IMAGES_DIR + "/" + str(uri)
image = QPixmap()
image.load(uri)
self.ui_.img.setPixmap(image)
print uri
def finaliza (self):
# self.ui_.img.setText(self.ua.text())
self.ui_.img.setText(""
+ "<font size=\"+4\">Obrigado!</font><BR><BR>"
+ self.ua.text()
+ "<BR><BR> Voce deseja salvar o teste?"
)
self.disconnect(self.ui_.nextBnt, QtCore.SIGNAL('clicked()'),
self.next)
self.connect(self.ui_.nextBnt, QtCore.SIGNAL('clicked()'),
self.ua.askToSave)
self.ui_.nextBnt.setText("Salvar teste")
#self.ui_.nextBnt.setDisabled(False)
print self.ua.print_()
def setPergunta (self, question):
self.ui_.textLabel1.setText(question)
def next (self):
self.ua.total_tests = self.ua.total_tests + 1
# Validade de answer
self.t.getActual().verifyAnswer(
self.ui_.lineEdit.text()
)
# Setup the next test if applicapable, if not
# just show the results
if self.ui_.nextBnt.text() == 'Fi&nalizar':
self.finaliza()
#self.ui_.nextBnt.setDisabled(True)
self.ui_.lineEdit.hide()
self.ui_.textLabel1.hide()
self.ui_.line1.hide()
return
self.t.getNext()
self.drawImage(self.t.getActual().getImage())
self.setPergunta(self.t.getActual().getQuestion())
# Debug and clean up the interface.
print " > Pos: " + str(self.t.getPos())
print " > Len: " + str(self.t.getLen()-1)
if self.t.getPos() == self.t.getLen()-1:
self.ui_.nextBnt.setText("Fi&nalizar")
print "Text: " + str(self.ui_.lineEdit.text())
self.ui_.lineEdit.setText("")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
# User answers
ua = userAnswers()
# Load xml into computer memory.
t = tests(ua)
# Show up interface.
f = ui()
f.t = t
t.ua = ua
# Start application.
#app.setMainWidget(f)
#app.exec_loop()
f.show()
sys.exit(app.exec_())