# Copyright (C) 2009, Tutorius.org # Copyright (C) 2009, Vincent Vinet # # This program 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 2 of the License, or # (at your option) any later version. # # This program 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 this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """ Core This module contains the core classes for tutorius """ import gtk import logging from sugar.tutorius.dialog import TutoriusDialog from sugar.tutorius.gtkutils import find_widget logger = logging.getLogger("tutorius") class Tutorial (object): """ Tutorial Class, used to run through the FSM. """ def __init__(self, name, fsm): """Create an unattached tutorial """ object.__init__(self) self.name = name self.state_machine = fsm self.state = None self.handlers = [] self.activity = None #Rest of initialisation happens when attached def attach(self, activity): """Attach to a running activity @param activity the activity to attach to """ #For now, absolutely detach if a previous one! if self.activity: self.detach() self.activity = activity self.set_state("INIT") def detach(self): """Detach from the current activity""" #Remove handlers for eventfilter in self.state_machine.get(self.state,{}).get("EventFilters",()): eventfilter.remove_handlers() #Undo actions for act in self.state_machine.get(self.state,{}).get("Actions",()): act.undo() #FIXME There should be some amount of resetting done here... self.activity = None def set_state(self, name): """Switch to a new state""" if not self.state_machine.has_key(name): return logger.debug("====NEW STATE: %s====" % name) #Remove handlers for eventfilter in self.state_machine.get(self.state,{}).get("EventFilters",()): eventfilter.remove_handlers() #Undo actions for act in self.state_machine.get(self.state,{}).get("Actions",()): act.undo() #Switch to new state self.state = name newstate = self.state_machine.get(name) #Register handlers for eventfilters for eventfilter in newstate["EventFilters"]: eventfilter.install_handlers(self._eventfilter_state_done, activity=self.activity) #Do actions for act in newstate.get("Actions",()): act.do() def _eventfilter_state_done(self, eventfilter): """Callback handler for eventfilter to notify when we must go to the next state.""" #XXX Tests should be run here normally #Swith to the next state pointed by the eventfilter self.set_state(eventfilter.get_next_state()) # def register_signal(self, handler, obj_fqdn, signal_name): # """Register a signal handler onto a specific widget # @param handler function to attach as a handler # @param obj_fqdn fqdn-style object name # @param signal_name signal name to connect to # # Side effects: # the object found and the handler id obtained by connect() are # appended in self.handlers # """ # obj = find_widget(self.activity, obj_fqdn) # self.handlers.append( \ # (obj, obj.connect(signal_name, handler, (signal_name, obj_fqdn) ))\ # ) class State: """This is a step in a tutorial. The state represents a collection of actions to undertake when entering the state, and a description of an event filter with associated actions to go to the next state.""" def __init__(self): """Initializes the content of the state, as in loading the actions that are required and building the correct tests.""" self.actions = [] self.tests = [] def setup(self): """Install the state itself. This is the best time to pop-up a dialog that has to remain for the duration of the state.""" for act in self.actions: act.do() def teardown(self): """Undo every action that was installed for this state. This means removing dialogs that were displayed, removing highlights, etc...""" for act in self.actions: act.undo() def verify(self): """Run the internal tests to see if one of them passes. If it does, then do the associated processing to go in the next state.""" for test in self.tests: if test.verify() == True: actions = test.get_actions() for act in actions: act.do() # Now that we execute the actions related to a test, we might # want to undo them right after --- should we use a callback or # a timer? class FiniteStateMachine(State): """This is a collection of states, with a start state and an end callback. It is used to simplify the development of the various tutorials by encapsulating a collection of states that represent a given learning process.""" def __init__(self, start_state, setup_actions): """The constructor for a FSM. Pass in the start state and the setup actions that need to be taken when the FSM itself start (which may be different from what is done in the first state of the machine).""" State.__init__(self) self.start_state = start_state self.actions = setup_actions self.current_state = self.start_state def setup(self): """ Set up the FSM """ for act in self.actions: act.do() def teardown(self): """ Revert any changes done by setup() """ for act in self.actions: act.undo() def verify(self): "Verify if the current state passes it's tests""" return self.current_state.verify()