Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/tutorius/actions.py
blob: 2d630be8db0f8e2c599fc8c49c3800dfbec9b5c2 (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
# Copyright (C) 2009, Tutorius.org
#
# 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
"""
This module defines Actions that can be done and undone on a state
"""
from gettext import gettext as _

from sugar.tutorius import gtkutils
from dialog import TutoriusDialog
import overlayer
from sugar.tutorius.editor import WidgetIdentifier
from sugar.tutorius.services import ObjectStore
from sugar.tutorius.properties import *

class Action(object):
    """Base class for Actions"""
    def __init__(self):
        object.__init__(self)
        self.properties = None

    def do(self, **kwargs):
        """
        Perform the action
        """
        raise NotImplementedError("Not implemented")

    def undo(self):
        """
        Revert anything the action has changed
        """
        pass #Should raise NotImplemented?

    def get_properties(self):
        """
        Fills self.property with a dict of TutoriusProperty and return the list 
        of property names. get_properties has to be called before accessing 
        self.property
        """
        if self.properties is None:
            self.properties = {}
            for i in dir(self):
                if isinstance(getattr(self,i), TutoriusProperty):
                    self.properties[i] = getattr(self,i)
        return self.properties.keys()

class OnceWrapper(object):
    """
    Wraps a class to perform an action once only

    This ConcreteActions's do() method will only be called on the first do()
    and the undo() will be callable after do() has been called
    """
    def __init__(self, action):
        self._action = action
        self._called = False
        self._need_undo = False

    def do(self):
        """
        Do the action only on the first time
        """
        if not self._called:
            self._called = True
            self._action.do()
            self._need_undo = True

    def undo(self):
        """
        Undo the action if it's been done
        """
        if self._need_undo:
            self._action.undo()
            self._need_undo = False
        
    def get_properties(self):
        return self._action.get_properties()

class DialogMessage(Action):
    """
    Shows a dialog with a given text, at the given position on the screen.

    @param message A string to display to the user
    @param pos A list of the form [x, y]
    """
    def __init__(self, message, pos=None):
        super(DialogMessage, self).__init__()
        self._dialog = None
        
        self.message = TStringProperty(message)
        self.position = TArrayProperty(pos or [0, 0], 2, 2)

    def do(self):
        """
            Show the dialog
        """
        self._dialog = TutoriusDialog(self.message.value)
        self._dialog.set_button_clicked_cb(self._dialog.close_self)
        self._dialog.set_modal(False)
        self._dialog.move(self.position.value[0], self.position.value[1])
        self._dialog.show()

    def undo(self):
        """
            Destroy the dialog
        """
        if self._dialog:
            self._dialog.destroy()
            self._dialog = None


class BubbleMessage(Action):
    """
    Shows a dialog with a given text, at the given position on the screen.

    @param message A string to display to the user
    @param pos A list of the form [x, y]
    @param speaker treeish representation of the speaking widget
    @param tailpos The position of the tail of the bubble; useful to point to
    specific elements of the interface
    """
    def __init__(self, message, pos=[0,0], speaker=None, tailpos=None):
        Action.__init__(self)
        self.message = TStringProperty(message)
        # Create the position as an array of fixed-size 2
        self.position = TArrayProperty(pos, 2, 2)
        # Do the same for the tail position
        self.tail_pos = TArrayProperty(tailpos, 2, 2)
        
        self.overlay = None
        self._bubble = None
        self._speaker = None
    
    def do(self):
        """
            Show the dialog
        """
        # get or inject overlayer
        self.overlay = ObjectStore().activity._overlayer
        # FIXME: subwindows, are left to overlap this. This behaviour is
        # undesirable. subwindows (i.e. child of top level windows) should be
        # handled either by rendering over them, or by finding different way to
        # draw the overlay.

        if not self._bubble:
            x, y = self._position
            # TODO: tails are relative to tailpos. They should be relative to
            # the speaking widget. Same of the bubble position.
            self._bubble = overlayer.TextBubble(text=self._message,
                tailpos=self._tailpos)
            self._bubble.show()
            self.overlay.put(self._bubble, x, y)
            self.overlay.queue_draw()

    def undo(self):
        """
            Destroy the dialog
        """
        if self._bubble:
            self._bubble.destroy()
            self._bubble = None
        
class WidgetIdentifyAction(Action):
    def __init__(self):
        self.activity = None
        self._dialog = None

    def do(self):
        os = ObjectStore()
        if os.activity:
            self.activity = os.activity

            self._dialog = WidgetIdentifier(self.activity)
            self._dialog.show()


    def undo(self):
        if self._dialog:
            self._dialog.destroy()

class ChainAction(Action):
    """Utility class to allow executing actions in a specific order"""
    def __init__(self, *actions):
        """ChainAction(action1, ... ) builds a chain of actions"""
        self._actions = actions

    def do(self,**kwargs):
        """do() each action in the chain"""
        for act in self._actions:
            act.do(**kwargs)

    def undo(self):
        """undo() each action in the chain, starting with the last"""
        for act in reversed(self._actions):
            act.undo()

class DisableWidgetAction(Action):
    def __init__(self, target):
        """Constructor
        @param target target treeish
        """
        Action.__init__(self)
        self._target = target
        self._widget = None

    def do(self):
        """Action do"""
        os = ObjectStore()
        if os.activity:
            self._widget = gtkutils.find_widget(os.activity, self._target)
            if self._widget:
                self._widget.set_sensitive(False)
                       
    def undo(self):    
        """Action undo"""
        if self._widget:
            self._widget.set_sensitive(True)
                       
                       
class TypeTextAction(Action):
    """                
    Simulate a user typing text in a widget
    Work on any widget that implements a insert_text method
                       
    @param widget The treehish representation of the widget
    @param text the text that is typed
    """                
    def __init__(self, widget, text):
        Action.__init__(self)
                       
        self._widget = widget
        self._text = text
                       
    def do(self, **kwargs):
        """            
        Type the text  
        """            
        widget = gtkutils.find_widget(ObjectStore().activity, self._widget)
        if hasattr(widget, "insert_text"):
            widget.insert_text(self._text, -1)
                       
    def undo(self):    
        """            
        no undo        
        """            
        pass           
                       
class ClickAction(Action):
    """                
    Action that simulate a click on a widget    
    Work on any widget that implements a clicked() method
                       
    @param widget The threehish representation of the widget
    """                
    def __init__(self, widget):
        Action.__init__(self)
        self._widget = widget
                       
    def do(self):      
        """            
        click the widget
        """
        widget = gtkutils.find_widget(ObjectStore().activity, self._widget)
        if hasattr(widget, "clicked"):
            widget.clicked()
        
    def undo(self):
        """
        No undo
        """
        pass