# Copyright 2008 by Peter Moxhay and Wade Brainerd. # This file is part of Math. # # Math is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Math is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Math. If not, see . """Math - Software for developing math concepts.""" # Import standard Python modules. import logging, os, math, time, copy, json, random from gettext import gettext as _ # Import PyGTK. import gobject, pygtk, gtk, pango, cairo # Import Sugar UI modules. import sugar.activity.activity from sugar.graphics import toggletoolbutton from sugar.graphics import * # Import Math modules. from vector import Vector import testlesson, compare3lesson # Change to bundle directory. bundle_path = sugar.activity.activity.get_bundle_path() os.chdir(bundle_path) # Initialize logging. log = logging.getLogger('Math') log.setLevel(logging.DEBUG) logging.basicConfig() class MathActivity(sugar.activity.activity.Activity): """The main activity class which creates the user interface, manages loading and saving state, and controls the flow of the activity.""" def __init__(self, handle): sugar.activity.activity.Activity.__init__(self, handle) self.set_title(_("Math")) # The data dictionary is saved to and loaded from the Journal. All data about the state of the activity # which should be remembered goes in here. self.data = {} # Create screens. self.areascreen = compare3lesson.Compare3Lesson(self) # Initialize the screen stack. self.screens = [] self.screenbox = gtk.VBox() # Start with the area screen. self.push_screen(self.areascreen) # Initialize toolbar menus. self.build_toolbox() # This has to happen last, because it calls the read_file method when restoring from the Journal. # The set_canvas call sets the widget that covers the main activity window. self.set_canvas(self.screenbox) # Make everything visible. self.show_all() # Hide the sharing button from the activity toolbar since we don't support sharing (yet). activity_toolbar = self.tbox.get_activity_toolbar() activity_toolbar.share.props.visible = False def build_toolbox(self): self.tbox = sugar.activity.activity.ActivityToolbox(self) self.tbox.show_all() self.set_toolbox(self.tbox) # -------------------------------------------------------------------------------------------- # Screen stack # # The activity stores the active set of screens as a stack. To open a new screen, push the # screen onto the stack, and when the screen should be closed, pop it from the stack. # # For example, if the user starts the activity, chooses a lesson, and then finishes the # lesson and views a certificate award screen, the stack might look like: # # MainScreen -> LessonScreen -> CertificateScreen def push_screen(self, screen): """Pushes a new screen onto the screen stack. The new screen will become visible and the previous top screen will be hidden until the new screen is popped.""" if len(self.screens): self.screenbox.remove(self.screens[-1]) self.screenbox.pack_start(screen, True, True) self.screens.append(screen) def pop_screen(self): """Pops the top screen off the screen stack. The current top screen will be destroyed, and the next screen on the stack will become visible.""" self.screenbox.remove(self.screens[-1]) self.screens.pop() if len(self.screens): self.screenbox.pack_start(self.screens[-1]) # -------------------------------------------------------------------------------------------- # Journal interaction def read_file(self, file_path): """Called when the user resumes the activity from the Journal.""" if self.metadata['mime_type'] != 'text/plain': return fd = open(file_path, 'r') try: text = fd.read() self.data = json.read(text) finally: fd.close() def write_file(self, file_path): """Called when the user saves the activity to the Journal.""" if not self.metadata['mime_type']: self.metadata['mime_type'] = 'text/plain' fd = open(file_path, 'w') try: text = json.write(self.data) fd.write(text) finally: fd.close() # vim: et sw=4