Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/tutorius/actions.py
blob: 58a421661dd7c20dc64f6e45cf222028b106ed85 (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
# 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 dialog import TutoriusDialog


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

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

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


class DoOnceMixin(object):
    """Mixin class used to have an action be called only on the first do().
    
    To use this mixin, create a new class that derives from this and from
    the action you want executed only once.

    class ConcreteOnceAction(DoOnceMixin, ConcreteAction):
        pass

    This ConcreteActions's do() method will only be called on the first do()
    and the undo() will be callable after do() has been called

    Maybe it would be better off just using a wrapper class instead of this.

    But it was fun to play with.

    Did I mention the wrapper is 25 lines "south" of here? Besides, this mixin
    class fails at __init__ ..... mixins... right....narwhals!
    """
    def __init__(self ):
        super(DoOnceMixin, self).__init__()
        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
            super(DoOnceMixin, self).do()
            self._need_undo = True
            
    def undo(self):
        """
        Undo the action if it's been done
        """
        if self._need_undo:
            super(DoOnceMixin, self).undo()
            self._need_undo = False

class OnceWrapper(object):
    """Wraps a class to perform an action once only
    """
    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

class DialogMessage(Action):
    """Show a dialog!"""
    def __init__(self, message):
        super(DialogMessage, self).__init__()
        self._message = message
        self._dialog = None

    def do(self):
        """
            Show the dialog
        """
        self._dialog = TutoriusDialog(self._message)
        self._dialog.set_button_clicked_cb(self._dialog.close_self)
        self._dialog.set_modal(False)
        self._dialog.show()

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


class OnceDialogMessage(DoOnceMixin, DialogMessage):
    """Broken!"""
    pass