Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/tutorius/tutorial.py
blob: fc1d8ae56c6a547940530552ee1d12214704dee5 (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
# Copyright (C) 2009, Tutorius.org
# Copyright (C) 2009, Vincent Vinet <vince.vinet@gmail.com>
#
# 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


import gtk
import logging

from sugar.tutorius.dialog import TutoriusDialog


logger = logging.getLogger("tutorius")

class Event:
    def __init__(self, object_name, event_name ):
        self.object_name = object_name
        self.event_name = event_name

    def test(self, sig, name):
        if self.object_name == name and self.event_name == sig:
            return True
        return False


class Tutorial (object):
    EVENTS = [
        "focus",
        "button-press-event",
        "enter-notify-event",
        "leave-notify-event",
        "key-press-event",
        "text-selected",
        "clicked",
    ]

    IGNORED_WIDGETS = [
        "GtkVBox",
        "GtkHBox",
        "GtkAlignment",
        "GtkNotebook",
        "GtkButton",
        "GtkToolItem",
        "GtkToolbar",
    ]

    def __init__(self, name, fsm):
        object.__init__(self)
        self.name = name
        self.state_machine = fsm

        self.handlers = []
        self.activity = None
        #self.setState("INIT")
        #self.state="INIT"
        #self.register_signals(self.activity, self.handleEvent, max_depth=10)

    def attach(self, activity):
        #For now, absolutely detach if a previous one!
        if self.activity:
            self.detach()
        self.activity = activity
        self.state="INIT"
        self.register_signals(self.activity,self.handleEvent, max_depth=10)

    def detach(self):
        self.disconnectHandlers()
        self.activity = None
        
    def handleEvent(self, *args):
        sig, objname = args[-1]
        logger.debug("EVENT %s ON %s" % (sig, objname) )
        for transition, next in self.state_machine[self.state]["Events"]:
            if transition.test(sig,objname):
                logger.debug("====NEW STATE: %s====" % next)
                self.state = next
                dlg = TutoriusDialog(self.state_machine[self.state]["Message"])
                dlg.setButtonClickedCallback(dlg.closeSelf)
                dlg.run()

#    @staticmethod
#    def logEvent(obj, *args):
#        logger.debug("%s" % str(args[-1]))

    def disconnectHandlers(self):
        for t, id in self.handlers:
            t.disconnect_handler(id)

#    def setState(self,name):
#        self.disconnectHandlers()
#        self.state = name
#        newstate = ABIWORD_MEF.get(name,())
#        for event, n in newstate:
#            target = self.activity
#            try:
#                for obj in event.object_name.split("."):
#                    target = getattr(target,obj)
#                id = target.connect(self.handler,(event.object_name, event.event_name))
#                self.handlers.append(target, id)
#                id = target.connect(Tutorial.logEvent,"EVENT %s ON %s" % (event.object_name, event.event_name))
#                self.handlers.append(target, id)
#            except Exception, e:
#                logger.debug(str(e))
            
    def register_signals(self,object,handler,prefix=None,max_depth=None):
        """
            Recursive function to register event handlers on an object
            and it's children. The event handler is called with an extra
            argument which is a two-tuple containing the signal name and
            the FQDN-style name of the object that triggered the event.

            This function registers all of the events listed in 
            Tutorial.EVENTS and omits widgets with a name matching
            Tutorial.IGNORED_WIDGETS from the name hierarchy.

            Example arg tuple added:
                ("focus", "Activity.Toolbox.Bold")
            Side effects:
                -Handlers connected on the various objects
                -Handler ID's stored in self.handlers

            @param object the object to recurse on
            @param handler the handler function to connect
            @param prefix name prepended to the object name to form a chain
            @param max_depth maximum recursion depth, None for infinity
        """
        #Gtk Containers have a get_children() function
        if hasattr(object,"get_children") and \
            hasattr(object.get_children,"__call__"):
            for child in object.get_children():
                if max_depth is None or max_depth > 0:
                    #Recurse with a prefix on all children
                    pre = ".".join( \
                        [p for p in (prefix, object.get_name()) \
                        if not (p is None or p in Tutorial.IGNORED_WIDGETS)] \
                    )
                    self.register_signals(child,handler,pre,max_depth-1)
        name = ".".join( \
            [p for p in (prefix, object.get_name()) \
            if not (p is None or p in Tutorial.IGNORED_WIDGETS)] \
        )
        #register events on the object if a widget XXX necessary to check this?
        if isinstance(object,gtk.Widget):
            for sig in Tutorial.EVENTS:
                try:
                    self.handlers.append( (object,object.connect(sig,handler,(sig, name) )) )
                except TypeError:
                    continue
        

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)."""
        self.start_state = start_state
        self.actions = setup_actions
        
        self.tests = []
        
        self.current_state = self.start_state
    
    def setup(self):
        for act in self.actions:
            act.do()
    
    def teardown(self):
        for act in self.actions:
            act.undo
    
    def verify(self):
        return self.current_state.verify()