Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/las-gui.py
blob: 4d833faec331db7f24b8d66340930d2eb346d9ff (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
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
#!/usr/bin/env python


import pygtk
pygtk.require('2.0')
import gtk
from las import Listenspell

class LS_gui:

	# This is a callback function. The data arguments are ignored
	# in this example. More on callbacks below.
	def __init__(self, handle):
		self.__init__()

	def __init__(self):
		DBname = "dict.db"
		self.use_phoneme  = False
		self.las = Listenspell(DBname)
		self.load_activity_interface()
		self.play_game("start")
		gtk.main()


	def destroy(self, widget, data=None):
		#print "destroy signal occurred"
		self.game_exit()

	def load_activity_interface(self):
		self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.main_window.connect("destroy", self.destroy)

		self.main_window.set_border_width(10)

		self.main_window.set_title("Listen and Spell")

		self.Hcontainer   = gtk.HBox()
		self.Hcontainer.show()

		self.main_window.add(self.Hcontainer)

		self.vcontainer_left = gtk.VBox()
		self.vcontainer_left.set_border_width(10)
		self.vcontainer_left.show()

		self.vcontainer_right = gtk.VBox()
		self.vcontainer_right.set_border_width(10)
		self.vcontainer_right.show()


		self.Hcontainer.pack_start(self.vcontainer_left,True, True,0)
		self.Hcontainer.pack_end(self.vcontainer_right,False, False,0)

		#####################Left Pane widgets##########################

		#self.label_v_left_a = gtk.Label("Hello this is label 1")
		#self.label_v_left_b = gtk.Label("label 2")

		#self.label_v_left_a.show()
		#self.label_v_left_b.show()

		#self.main_output_view = gtk.TextView()
		#self.main_output_buffer = gtk.TextBuffer()
		#self.main_output_buffer.set_text("This is main output")
		#self.main_output_view.set_editable(False)
		#self.main_output_view.set_buffer(self.main_output_buffer)
		
		#self.display_main_output("This is main output")
		
		#self.main_output_view.show_all()
		
		self.text_input = gtk.Entry()
		#self.text_input.set_text("Preset input text")
		#self.text_input.
		self.text_input.show()
		self.text_input.connect("focus-in-event", self.text_input_focus, None)
		self.text_input.connect("activate", self.text_input_activate, None)

		self.console_text_view = gtk.TextView()
		
		
		#self.console_text_view.show()
		#self.h_adj = gtk.Adjustment(value=0, lower=0, upper=4, step_incr=1, page_incr=0, page_size=0)
		#self.console_text_sw = gtk.ScrolledWindow(vadjustment=self.h_adj)
		
		self.console_text_sw = gtk.ScrolledWindow()
		self.console_text_sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
		self.console_text_sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

		self.console_text_sw.add(self.console_text_view)


		self.text_submit_button = gtk.Button(label="Submit")
		self.text_submit_button.show()
		self.text_submit_button.connect("clicked", self.submit_button_clicked, None)

		self.console_text_buffer = gtk.TextBuffer()
		#self.console_text_buffer.set_text("This is hint text")
		self.console_text_view.set_editable(False)
		self.console_text_view.set_buffer(self.console_text_buffer)

		self.console_text_frame = gtk.Frame("Console:")
		self.console_text_frame.add(self.console_text_sw)
		self.console_text_frame.show_all()

		#self.vcontainer_left.pack_start(self.main_output_view, True, True )
		#self.vcontainer_left.pack_start(self.label_v_left_b, True, False )
		
		self.vcontainer_left.pack_start(self.console_text_frame, True, True )
		self.vcontainer_left.pack_start(self.text_input, False, True )
		self.vcontainer_left.pack_start(self.text_submit_button, False, False )

		################################################True################

		#################Right pane wigets##############################

		self.v_buttonbox = gtk.VButtonBox()

		self.repeat_word_button = gtk.Button(label="Repeat Word")
		self.repeat_word_button.connect("clicked", self.repeat_word_button_clicked, None)

		self.get_def_button = gtk.Button(label="Get Def")
		self.get_def_button.connect("clicked", self.get_def_button_clicked, None)

		self.get_usage_button = gtk.Button("Get Usage")
		self.get_usage_button.connect("clicked", self.get_usage_button_clicked, None)

		self.get_word_length_button = gtk.Button("Get Word Length")
		self.get_word_length_button.connect("clicked", self.get_word_length_button_clicked, None)

		self.v_buttonbox.add(self.repeat_word_button)
		self.v_buttonbox.add(self.get_def_button)
		self.v_buttonbox.add(self.get_usage_button)
		self.v_buttonbox.add(self.get_word_length_button)

		self.v_buttonbox.show_all()

		self.button_frame = gtk.Frame("Buttons")
		self.button_frame.add(self.v_buttonbox)
		self.button_frame.show()

		self.vcontainer_right.add(self.button_frame)

		self.stats_frame = gtk.Frame("Stats")

		self.stats_table = gtk.Table(4,2,True)

		self.score_label = gtk.Label("Score: ")
		self.score_value_label = gtk.Label("?")
		#self.score_value_label.

		self.skill_level_label = gtk.Label("Skill Level: ")
		self.skill_level_value_label = gtk.Label("?")

		self.words_played_label = gtk.Label("Words played: ")
		self.words_played_value_label = gtk.Label("?")

		self.words_correct_label = gtk.Label("Correct Words: ")
		self.words_correct_value_label = gtk.Label("?")

		self.stats_table.attach(self.score_label, 0, 1, 0, 1)
		self.stats_table.attach(self.score_value_label, 1, 2, 0, 1)

		self.stats_table.attach(self.skill_level_label, 0, 1, 1, 2)
		self.stats_table.attach(self.skill_level_value_label, 1, 2, 1, 2)

		self.stats_table.attach(self.words_played_label, 0, 1, 2, 3)
		self.stats_table.attach(self.words_played_value_label, 1, 2, 2, 3)

		self.stats_table.attach(self.words_correct_label, 0, 1, 3, 4)
		self.stats_table.attach(self.words_correct_value_label, 1, 2, 3, 4)

		self.vcontainer_right.add(self.stats_frame)
		self.stats_frame.add(self.stats_table)

		self.stats_frame.show_all()

		################################################################

		self.main_window.show()

		##################Call Backs####################################

	def submit_button_clicked(self, widget, data = None):
		answer = self.text_input.get_text()
		self.las.say_text("You typed")
		self.shout(answer)
		if answer == self.elem:
			self.las.say_text("Correct")
			self.las.ans_correct(self.wordid)
			
		else:
			self.las.ans_incorrect(self.wordid)
			self.las.say_text("Incorrect")
			self.display_console("Incorrect. The correct spelling is.. ")
			self.las.say_text("The correct spelling is..\n")
			self.shout(self.elem)
		self.update_all()
		self.play_game("next word")
		return False

	def text_input_focus(self, widget, event, data= None):
		#print "text_input_focus_in"
		return False

	def text_input_activate(self, widget, data=None):
		self.submit_button_clicked(widget, data)
		#print "text_input_activate"
		return False

	def repeat_word_button_clicked(self, widget, data = None):
		if self.elem != self.pronounelem and self.use_phoneme:
			self.las.say_text(self.pronounelem, is_phoneme = True)
		else:
			self.las.say_text(self.elem)

	def get_def_button_clicked(self, widget, data = None):
		def_list = self.las.get_word_info(self.wordid, "def")
		self.display_console("Definition: ")
		for (pos, definition, name) in def_list:
			self.display_console(pos + "(" + name + ") : " + definition)

	def get_usage_button_clicked(self, widget, data = None):
		if self.usage_used == 0:
			usage = self.las.get_word_info(self.wordid, "usage")
			self.total_num_usage = len(usage)
		if self.total_num_usage == 0:
			self.display_console("No usage in the database")
		elif self.total_num_usage == self.usage_used:
			self.display_console("No more usage in the database")
		else:
			(sample) = usage[self.usage_used]
			las.say_text(sample)
			self.usage_used = self.usage_used + 1

	def get_word_length_button_clicked(self, widget, data = None):
		self.display_console("Word Length: " + str(len(self.elem)))

	def ask_skill_level(self):
		self.skill_level_dialog = gtk.Dialog("Enter Skill Level", self.main_window, 0,(gtk.STOCK_OK, gtk.RESPONSE_OK, "Quit Game", gtk.RESPONSE_CANCEL))
		self.las.say_text("Skill Level")
		hbox = gtk.HBox(False, 8)
		hbox.set_border_width(8)
		self.skill_level_dialog.vbox.pack_start(hbox, False, False, 0)
		
		table = gtk.Table(1, 1)
		table.set_row_spacings(4)
		table.set_col_spacings(4)
		hbox.pack_start(table, True, True, 0)

		label = gtk.Label("Skill Level")
		#label.set_use_underline(True)
		table.attach(label, 0, 1, 0, 1)
		local_skill_level = gtk.Entry()
		local_skill_level.set_text(str(self.las.get_skill_level()))
		local_skill_level.connect("activate", self.local_skill_level_activate, None)
		table.attach(local_skill_level, 1, 2, 0, 1)
		label.set_mnemonic_widget(local_skill_level)

		self.skill_level_dialog.show_all()
		response = self.skill_level_dialog.run()

		if response == gtk.RESPONSE_OK:
			skill_level = int (local_skill_level.get_text())
			self.skill_level_dialog.destroy()
			self.las.set_skill_level(skill_level)
			self.update_all()
			
		elif response == gtk.RESPONSE_CANCEL:
			self.game_exit()
		elif response == gtk.RESPONSE_NONE:
			self.game_exit()
			
	def local_skill_level_focus(self, widget, event, data= None): # not in use
		#print "text_input_focus_in"
		return False

	def local_skill_level_activate(self, widget, data=None):
		self.skill_level_dialog.response(gtk.RESPONSE_OK)

	def shout(self,string):
		self.display_console("")
		for char in string:
			self.display_console(char, newline = False)
			self.las.say_text(char)
		self.las.say_text(string)
	
	
	################################################################

	#####################Update methods#############################
	def update_all(self):
		self.update_score()
		self.update_skill_level()
		self.update_words_played()
		self.update_words_correct()
		
	def update_score(self, score = None):
		if score ==None:
			score = self.las.get_points()
		self.score_value_label.set_text(str(score))

	def update_skill_level(self, skill_level = None):
		if skill_level ==None:
			skill_level = self.las.get_skill_level()
		self.skill_level_value_label.set_text(str(skill_level))

	def update_words_played(self, words_played = None):
		if words_played ==None:
			words_played = self.las.get_words_played()
		self.words_played_value_label.set_text(str(words_played))

	def update_words_correct(self, words_correct = None):
		if words_correct ==None:
			words_correct = self.las.get_words_correct()
		self.words_correct_value_label.set_text(str(words_correct))

	def play_word(self):
		self.usage_used = -1
		self.total_num_usage = -1
		
		self.text_input.set_text("") # Clear answer field
		
		# Initilazing variable for eacch word
		
		self.elem        = self.las.get_word_info(self.wordid, "word")   #get a word from the list
		self.pronounelem = self.las.get_word_info(self.wordid, "phnm")   #get a pronounciation from the list

		self.display_console("Spell...")       # say the explanation if the word is ambiguous.
		if self.elem != self.pronounelem and self.use_phoneme:    # determine whether to bother pronouncing a description
			self.las.say_text("Spell..." + self.pronounelem, is_phoneme = True)
		else:
			#print "Spell... "
			self.las.say_text("Spell..." + self.elem)
			
	def play_game(self,mode = "start"):
		if mode == "start":
			self.las.play_sound("begin")
			self.this_level_size = 7
			self.this_level_max_error = 3
			#self.display_main_output("Welcome")
			self.las.say_text("Welcome", wait = False)
			self.ask_skill_level()
			self.wordid_list = self.las.load_wordid(self.this_level_size + self.this_level_max_error)
			self.this_level_num_words = len(self.wordid_list)
			self.this_level_words_left = self.this_level_num_words
			self.word_index = 1
			self.wordid = self.wordid_list[self.word_index]
			self.play_word()
		elif mode == "next word":
			self.this_level_words_left -= 1
			if self.this_level_num_words == 0:
				if self.las.words_correct == self.this_level_size:
					self.display_console("You have crossed this level")
					self.las.say_text("congratulation")
					self.display_console("Please select new level to play the game")
				else:
					self.display_console("Sorry you are not able to cross this level")
					self.las.say_text("Game Over")
					self.display_console("Please select lower level to play the game")
				self.play_game("start")
			self.word_index += 1
			self.wordid = self.wordid_list[self.word_index]
			self.play_word()

	########################Display Methods#########################

	def display_console(self, data, newline = True ):
		if newline == True:
			data = "\n" + data
		self.console_text_buffer.insert_at_cursor(data)

	#def display_main_output(self, data, newline = True, clear_previous = True):
		#if clear_previous == True:
			#self.main_output_buffer.set_text(data)
		#elif newline == True:
			#text = "\n" + data
			#self.main_output_buffer.insert_at_cursor(data)
		#else:
			#self.main_output_buffer.insert_at_cursor(data)

	
	################################################################
	
	def game_exit(self):
		self.las.exit_game()
		gtk.main_quit()

	########################Game Logic##############################

	################################################################



	def main(self):
		# All PyGTK applications must have a gtk.main(). Control ends here
		# and waits for an event to occur (like a key press or mouse event).
		gtk.main()

# If the program is run directly or passed as an argument to the python
# interpreter then create a HelloWorld instance and show it
if __name__ == "__main__":
	gui = LS_gui()
	#gui.__init__()
	#gui.play_game("start")
	#gui.main()