From 38619bb43a9258c02dbdd0d134db3c12f43fbd7a Mon Sep 17 00:00:00 2001 From: Christophe Gueret Date: Sun, 13 Jan 2013 00:11:15 +0000 Subject: Added keeping the last score --- diff --git a/activity_quiz/Activity.py b/activity_quiz/Activity.py index b86059f..f0ba268 100644 --- a/activity_quiz/Activity.py +++ b/activity_quiz/Activity.py @@ -11,9 +11,8 @@ from sugar3.activity.widgets import ActivityButton from sugar3.activity.widgets import TitleEntry from sugar3.activity.widgets import StopButton from sugar3.activity.widgets import DescriptionItem -from sugar3.graphics.toolbutton import ToolButton from Application import Main - +import simplejson class QuizActivity(activity.Activity): def __init__(self, handle): @@ -23,7 +22,7 @@ class QuizActivity(activity.Activity): activity.Activity.__init__(self, handle) # Create the main part of the application - main = Main() + self._application = Main() # toolbar with the new toolbar redesign toolbar_box = ToolbarBox() @@ -54,6 +53,31 @@ class QuizActivity(activity.Activity): toolbar_box.show() # Set the canvas - self.set_canvas(main.get_widget()) - main.get_widget().show() + self.set_canvas(self._application.get_widget()) + self.canvas.show() + + def write_file(self, file_path): + if not self.metadata['mime_type']: + self.metadata['mime_type'] = 'text/plain' + + data = {} + data['last_score'] = self._application.get_score() + + fd = open(file_path, 'w') + text = simplejson.dumps(data) + fd.write(text) + fd.close() + + def read_file(self, file_path): + if self.metadata['mime_type'] != 'text/plain': + return + + fd = open(file_path, 'r') + text = fd.read() + data = simplejson.loads(text) + fd.close() + if 'last_score' in data: + self._application.set_score(int(data['last_score'])) + else: + self._application.set_score(0) diff --git a/activity_quiz/Application.py b/activity_quiz/Application.py index e8d2b82..3172741 100644 --- a/activity_quiz/Application.py +++ b/activity_quiz/Application.py @@ -61,6 +61,13 @@ class Main(object): ''' return self._gui.get_object("main_box") + def get_score(self): + return self._points + + def set_score(self, value): + self._points = value + self._gui.get_object("score_zone").set_label(str(self._points)) + def make_new_question(self): ''' Prepare a new question: pick a set of cities and update the buttons @@ -79,8 +86,7 @@ class Main(object): ''' # Check the answer picked if number == self._good_answer: - self._points = self._points + 1 - self._gui.get_object("score_zone").set_label(str(self._points)) + self.set_score(self.get_score() + 1) # Create a new question self.make_new_question() diff --git a/activity_quiz/GUI.glade b/activity_quiz/GUI.glade index 5980607..1202abf 100644 --- a/activity_quiz/GUI.glade +++ b/activity_quiz/GUI.glade @@ -28,7 +28,7 @@ True False 0 - etched-out + out True @@ -108,7 +108,7 @@ True False 0 - etched-out + out True -- cgit v0.9.1