Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/HelloDoctor.py
blob: 49a176e28b0b7e2111093db1d30dba26946cff20 (plain)
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
import sys
import os
import re
from time import gmtime, strftime 
import pygtk
import gtk
import clips

import gst
import tempfile

GST_PIPE = ['v4l2src', 'ffmpegcolorspace', 'pngenc']

from sugar.activity import activity
from sugar.graphics import style

from gettext import gettext as _

class HelloDoctor(activity.Activity):    
    def __init__(self, handle):
	"Entry point"
	activity.Activity.__init__(self, handle)
	self._name = handle

	toolbox = activity.ActivityToolbox(self)
        activity_toolbar = toolbox.get_activity_toolbar()
        activity_toolbar.keep.props.visible = False
        activity_toolbar.share.props.visible = False
        self.set_toolbox(toolbox)

        toolbox.show()

	self.set_title(_("HelloDoctor"))
#	self.window.set_border_width(5)
#	self.window.set_size_request(640, 480)
	self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
	self.settings = gtk.Settings()
	self.settings.set_string_property('gtk-font-name', 'courier bold 10', '')
	self.settings.set_long_property("gtk-button-images", False, "")
	self._main_view = gtk.EventBox()
	self._main_view.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("khaki"))
	self.set_canvas(self._main_view)
	self.homescreen()
        
    def homescreen(self, widget=None, oldcontainer=None):	
	homebox=gtk.VBox()
	table= gtk.Table(20, 12, True)
	colorbutton = gtk.ColorButton(gtk.gdk.color_parse("khaki"))
	colorbutton.connect('color-set', self.set_background_color)
	color = colorbutton.get_color()
	title_label=gtk.Label()
	labeltext_title=(_("Hello \nDoctor!"))
	title_label.set_markup("<big><big><big><big><big><big><big>%s</big></big></big></big></big></big></big>" % (labeltext_title))
	title_button=gtk.Button()
        title_button.connect("clicked", self.symptomChose, homebox, '')
	title_button_img=gtk.Image()
	title_button_img.set_from_file("images/interface/start_button.svg")
	title_button.set_image(title_button_img)
        title_image=gtk.Image()
        title_image.set_from_file("images/interface/doctor.svg")

	table.attach(colorbutton, 1, 3, 15, 18)
	table.attach(title_label, 0, 4, 4, 9)
	table.attach(title_image, 4, 12, 0, 15)
	table.attach(title_button, 7, 10, 14, 19)

        homebox.pack_start(table)
	
	if oldcontainer != None:
		self._main_view.remove(oldcontainer)
		oldcontainer.destroy()
	self._main_view.add(homebox)
	self._main_view.show()
	self.symptomMain()
	self.show_all()

    def set_background_color(self, colorbutton):
	color = colorbutton.get_color()
	self._main_view.modify_bg(gtk.STATE_NORMAL, color)

    def symptomMain(self):
	global curr_counter_number, counter_max, list
	list = []
	rulesfile = open("rules.clp", "r") 
	for line in rulesfile:
		matched_expr = re.search(re.escape("MAIN::symptom")+"(.*?)"+re.escape(" "),line)
		if matched_expr==None:
			continue
		toprin = matched_expr.group(1)
		list.append([toprin, ''])
		finiline = line.find('))')
		if finiline != -1:
			break

	curr_counter_number=0
	counter_max=len(list) -1

    def symptomChose(self, widget, oldcontainer, value):
	global curr_counter_number, counter_max, list
	if (value == "plus"):
		curr_counter_number=curr_counter_number+1
	if (value == "minus"):
		curr_counter_number=curr_counter_number-1

	symptom = list[curr_counter_number][0]
	fraction = 0.05 + (curr_counter_number/(counter_max+1.0))

	symptombox=gtk.VBox()
	symptom_table= gtk.Table(20, 14, True)

	progress=gtk.ProgressBar()
	progress.set_fraction(fraction)
	progress.set_text("%s/%s %s" % ((curr_counter_number+1),(counter_max+1),symptom))
	symptom_image=gtk.Image()
	symptom_image.set_from_file("images/symptoms/%s.svg" % (symptom))
	
	button_yes=gtk.ToggleButton()
	button_no=gtk.ToggleButton()
	button_yes.connect("toggled", self.toggle_callback, "yes", button_no)
	button_no.connect("toggled", self.toggle_callback, "no", button_yes)
	if (list[curr_counter_number][1] == 'yes'):
		button_yes.set_active(True)
	elif (list[curr_counter_number][1] == 'no'):
		button_no.set_active(True)

	but_yes_img=gtk.Image()
	but_yes_img.set_from_file("images/interface/oktick.svg")
	button_yes.set_image(but_yes_img)
	but_no_img=gtk.Image()
	but_no_img.set_from_file("images/interface/oktick.svg")
	button_no.set_image(but_no_img)

	symptom_table.attach(button_yes, 3, 5, 17, 20)
	symptom_table.attach(button_no, 9, 11, 17, 20)

	button_next=gtk.Button()
	but_next_img=gtk.Image()
	but_next_img.set_from_file("images/interface/next_but.svg")
	button_next.set_image(but_next_img)
	nextcon=button_next.connect("clicked", self.symptomChose, symptombox, "plus")

	if (curr_counter_number != 0):
		
		button_prev=gtk.Button()
		but_prev_img=gtk.Image()
		but_prev_img.set_from_file("images/interface/prev_but.svg")
		button_prev.connect("clicked", self.symptomChose, symptombox, "minus")
		button_prev.set_image(but_prev_img)
		symptom_table.attach(button_prev, 0, 2, 17, 20)
	if (curr_counter_number == counter_max):
		button_next.disconnect(nextcon)
		button_next.connect("clicked", self.finishdialog, symptombox)

	symptom_table.attach(progress, 1, 13, 0, 1)
	symptom_table.attach(symptom_image, 1, 13, 1, 17)
	symptom_table.attach(button_next, 12, 14, 17, 20)
	
	symptombox.add(symptom_table)

	print "values in symptomChose: curr: %s  max: %s" % (curr_counter_number, counter_max)
		
	self._main_view.remove(oldcontainer)
	oldcontainer.destroy()
	self._main_view.add(symptombox)
	self.show_all()

    def toggle_callback(self, widget, data, secondone):
	global list
	if (widget.get_active()):
		list[curr_counter_number][1]=data
		secondone.set_active(False)

    def finishdialog(self, widget, oldcontainer):
	dialog = gtk.Dialog("Finish?", None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT, gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT))
#	dialog.set_modal(True)
#	self._main_view.set_transient_for(dialog)
	dialogimage= gtk.Image()
	dialogimage.set_from_file("images/interface/to_solution.svg")
	dialog.vbox.pack_start(dialogimage, True, True, 0)

 	button1 = gtk.Button()
	dialog.action_area.pack_start(button1, True, True, 0)
	
 	button2 = gtk.Button()
	dialog.action_area.pack_start(button2, True, True, 0)

	dialogimage.show()

	dialogresult = dialog.run()

	if (dialogresult == gtk.RESPONSE_ACCEPT):
		self._main_view.remove(oldcontainer)
		oldcontainer.destroy()
		self.solution()
		dialog.destroy()

	elif (dialogresult == gtk.RESPONSE_REJECT):
		dialog.destroy()

    def solution(self):
	solutionbox= gtk.EventBox()
	solution_hands= gtk.HBox()
	solution_table= gtk.Table(20, 14, True)
	solutionbox.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
	
	patientphoto = gtk.Image()
	photo = Camera()
	photo.Snap()
	photopath = photo.snap_path
	patientpixbuf = gtk.gdk.pixbuf_new_from_file_at_size(photopath, 500, 475)
	patientphoto.set_from_pixbuf(patientpixbuf)
#	patientphoto.set_from_file("%s" % photopath)
#	patientpixbuf = gtk.gdk.pixbuf_new_from_file_at_size(photopath, 300, 500)
#	patientpixbuf.pixbuf

	logfile = open('logfile.txt', 'a')
	currtime = strftime("%H:%M:%S, %d %b %Y", gmtime())
	towrite = str('---------%s---------\n' % currtime)
	logfile.write(towrite)
	logfile.close()

	clips.Clear()
	clips.Load("rules.clp")
	clips.Reset()
	beginingstate= clips.FactList()
	total_diseases= 0
	for bs in beginingstate:
		if bs.Relation == 'disease':
			total_diseases += 1
			print total_diseases
	for i in list:
		sym = i[0]
		occ = i[1]
		if occ == '':
			occ = 'no'
		clips.Assert("(symptom %s %s)" % (sym,occ))
	clips.Run()
	result= clips.FactList()
	numof_res = 0
	for f in result:
		if f.Relation == 'disease':
			numof_res += 1	
			dis = f.CleanPPForm()
			number = re.search(re.escape("(disease ")+"(.*?)"+re.escape(" "), dis)
			justonedis = number.group(1)
			total = map(int, justonedis)
			print total

	solution_image = gtk.Image()
	if numof_res == total_diseases:
#		solution_image.set_from_file("%s" % photopath)
		solution_image.set_from_file("images/interface/healthy.svg")
	else:
		solution_image.set_from_file("images/interface/to_hospital.svg")
		num_label2 = gtk.Label()		
		if numof_res == 0:
			sollab_text = '000'
		elif numof_res == 1:
			sollab_text = justonedis
		else:
			sollab_text = 'UNK'
		solution_table.attach(num_label2, 4, 7, 14, 16)
		num_label2.set_markup("<big><big><big><big><big><big><big>%s</big></big></big></big></big></big></big>" % (sollab_text));
	solution_table.attach(patientphoto, 0, 7, 1, 10)
	solution_table.attach(solution_image, 2, 14, 1, 19)

	reload_button = gtk.Button()
	reload_button_img = gtk.Image()
#	reload_button_img.set_from_stock(gtk.STOCK_REFRESH, gtk.ICON_SIZE_BUTTON)
	reload_button_img.set_from_file("images/interface/reload.svg")
	reload_button.set_image(reload_button_img)
	reload_button.connect("clicked", self.homescreen, solution_table)
	solution_table.attach(reload_button, 12, 14, 17, 20)

	self._main_view.add(solution_table)
	self.show_all()

class Camera(object):

    """A class repre camera"""

    def __init__(self):
	snap_file, self.snap_path = tempfile.mkstemp()
	pipe = GST_PIPE + ['filesink location=%s' % self.snap_path]
	self.pipe = gst.parse_launch('!'.join(pipe))
	self.bus = self.pipe.get_bus()

    def Snap(self):
	"""Take a snapshop"""
	self.pipe.set_state(gst.STATE_PLAYING)
	self.bus.poll(gst.MESSAGE_EOS, -1)
	self.pipe.set_state(gst.STATE_NULL)


# if __name__ == "__main__":
#    
#	HelloDoctor().main()
#	gtk.main()