This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
selectable.py
89 lines (66 loc) · 2.21 KB
/
selectable.py
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
import gtk
import view
import controller
import gobject
from receiver import receiver, handler
class Selection(object):
def __init__(self):
self.selected = set()
def addToSelection(self, obj):
self.setSelectionTo(self.selected | set([obj]))
def removeFromSelection(self, obj):
self.setSelectionTo(self.selected - set([obj]))
def replaceSelection(self, obj):
self.setSelectionTo(set([obj]))
def clearSelection(self):
self.setSelectionTo(set())
def setSelectionTo(self, objs):
old = self.selected
self.selected = objs
for obj in self.selected - old:
obj.select()
for obj in old - self.selected:
obj.deselect()
def delete(self):
objs = list(self.selected)
for obj in objs:
obj.delete()
def __iter__(self):
return iter(self.selected)
class Controller(controller.Controller):
def click(self, pos):
if self._last_event.get_state() & gtk.gdk.SHIFT_MASK:
self._view.addToSelection()
elif self._last_event.get_state() & gtk.gdk.CONTROL_MASK:
self._view.removeFromSelection()
else:
self._view.replaceSelection()
def set_pos(self, obj, pos):
controller.Controller.set_pos(self, obj, pos)
if self._view.selection:
deltas = ((obj, self.pos(obj) - self.pos(self._view)) for obj in
self._view.selection if obj is not self._view)
for obj, delta in deltas:
obj.set_pos(pos + delta)
class Selectable(view.View):
Controller = Controller
def __init__(self, selection):
view.View.__init__(self)
self.selection = selection
def replaceSelection(self):
if self.selection:
self.selection.replaceSelection(self)
def addToSelection(self):
if self.selection:
self.selection.addToSelection(self)
def removeFromSelection(self):
if self.selection:
self.selection.removeFromSelection(self)
def select(self):
pass
def deselect(self):
pass
def delete(self):
pass
def set_pos(self, (x, y)):
self.set_simple_transform(x, y, 1.0, 0)