-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
145 lines (119 loc) · 4.1 KB
/
Copy pathcommand.py
File metadata and controls
145 lines (119 loc) · 4.1 KB
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
# Calendar, Graphical calendar applet with novel interface
#
# command.py
#
# Copyright (c) 2010, Brandon Lewis <brandon_lewis@berkeley.edu>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import gtk
class Command(object):
def do(self):
return False
def undo(self):
return False
class MouseCommand(Command):
abs = None
rel = None
shift = None
control = None
cursor = None
@classmethod
def create_for_point(cls, instance, abs):
if cls.can_do(instance, abs):
return cls(instance, abs)
@classmethod
def can_do(cls, instance, abs):
return False
def __init__(self, intance, abs):
raise NotImplemented
def update(self, abs, rel, shift=False, control=False):
self.abs = abs
self.rel = rel
self.shift = shift
self.control = control
self.do()
def flick(self, velocity_vector, finished_cb = None, shift=False,
control=False):
self.flick_velocity = velocity_vector
self.finished_cb = None
self.shift = shift
self.control = control
self.flick_start()
def flick_start(self):
pass
def flick_stop(self):
pass
class MenuCommand(Command):
label = ""
stockid = None
tooltip = None
def __init__(self, app):
self.app = app
self.configure()
def configure(self):
pass
@classmethod
def can_do(cls, app):
return True
@classmethod
def create_action_group(cls, app):
ret = gtk.ActionGroup("command_actions")
for sbcls in cls.__subclasses__():
action = gtk.Action(sbcls.__name__, sbcls.label, sbcls.tooltip,
sbcls.stockid)
action.connect("activate", app.do_command, sbcls)
action.set_sensitive(sbcls.can_do(app))
ret.add_action(action)
sbcls.action = action
return ret
@classmethod
def update_actions(cls, app):
for sbcls in cls.__subclasses__():
sbcls.action.set_sensitive(sbcls.can_do(app))
class UndoStack(object):
def __init__(self, undo_action=None, redo_action=None):
self.undo_stack = []
self.redo_stack = []
if not undo_action:
self.undo_action = gtk.Action("Undo", None, None, gtk.STOCK_UNDO)
else:
self.undo_action = undo_action
self.undo_action.set_sensitive(False)
self.undo_action.connect("activate", self.undo)
if not redo_action:
self.redo_action = gtk.Action("Redo", None, None, gtk.STOCK_REDO)
else:
self.redo_action = redo_action
self.redo_action.set_sensitive(False)
self.redo_action.connect("activate", self.redo)
def commit(self, action):
# an action will return True if it can be undone
self.undo_stack.append(action)
self.redo_stack = []
self.redo_action.set_sensitive(False)
self.undo_action.set_sensitive(True)
def undo(self, unused_action):
action = self.undo_stack.pop()
action.undo()
self.redo_stack.append(action)
self.undo_action.set_sensitive(len(self.undo_stack))
self.redo_action.set_sensitive(True)
def redo(self, unused_action):
action = self.redo_stack.pop()
action.do()
self.undo_stack.append(action)
self.undo_action.set_sensitive(True)
self.redo_action.set_sensitive(len(self.redo_stack))