/usr/lib/python2.7/dist-packages/ginga/canvas/CanvasMixin.py is in python-ginga 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# CanvasMixin.py -- enable canvas like capabilities.
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
from ginga.canvas.CompoundMixin import CompoundMixin
from ginga.util.six.moves import map, filter
__all__ = ['CanvasMixin']
class CanvasError(Exception):
pass
class CanvasMixin(object):
"""A CanvasMixin is combined with the CompoundMixin to make a
tag-addressible canvas-like interface. This mixin should precede the
CompoundMixin in the inheritance (and so, method resolution) order.
"""
def __init__(self):
assert isinstance(self, CompoundMixin), "Missing CompoundMixin class"
# holds the list of tags
self.tags = {}
self.count = 0
for name in ('modified', ):
self.enable_callback(name)
def update_canvas(self, whence=3):
self.make_callback('modified', whence)
def redraw(self, whence=3):
self.make_callback('modified', whence)
def subcanvas_updated_cb(self, canvas, whence):
"""
This is a notification that a subcanvas (a canvas contained in
our canvas) has been modified. We in turn signal that our canvas
has been modified.
"""
# avoid self-referential loops
if canvas != self:
#print("%s subcanvas %s was updated" % (self.name, canvas.name))
self.make_callback('modified', whence)
def add(self, obj, tag=None, tagpfx=None, belowThis=None, redraw=True):
self.count += 1
if tag:
# user supplied a tag
if tag in self.tags:
raise CanvasError("Tag already used: '%s'" % (tag))
else:
if tagpfx:
# user supplied a tag prefix
if tagpfx.startswith('@'):
raise CanvasError("Tag prefix may not begin with '@'")
tag = '%s%d' % (tagpfx, self.count)
else:
# make up our own tag
tag = '@%d' % (self.count)
obj.tag = tag
self.tags[tag] = obj
self.add_object(obj, belowThis=belowThis)
# propagate change notification on this canvas
if obj.has_callback('modified'):
obj.add_callback('modified', self.subcanvas_updated_cb)
if redraw:
self.update_canvas(whence=3)
return tag
def delete_objects_by_tag(self, tags, redraw=True):
for tag in tags:
try:
obj = self.tags[tag]
del self.tags[tag]
super(CanvasMixin, self).delete_object(obj)
except Exception as e:
continue
if redraw:
self.update_canvas(whence=3)
def delete_object_by_tag(self, tag, redraw=True):
self.delete_objects_by_tag([tag], redraw=redraw)
def get_object_by_tag(self, tag):
obj = self.tags[tag]
return obj
def lookup_object_tag(self, obj):
# TODO: we may need to have a reverse index eventually
for tag, ref in self.tags.items():
if ref == obj:
return tag
return None
def get_tags_by_tag_pfx(self, tagpfx):
res = []
keys = filter(lambda k: k.startswith(tagpfx), self.tags.keys())
return keys
def get_objects_by_tag_pfx(self, tagpfx):
return list(map(lambda k: self.tags[k], self.getTagsByTagpfx(tagpfx)))
def delete_all_objects(self, redraw=True):
self.tags = {}
CompoundMixin.delete_all_objects(self)
if redraw:
self.update_canvas(whence=3)
def delete_objects(self, objects, redraw=True):
for tag, obj in list(self.tags.items()):
if obj in objects:
self.delete_object_by_tag(tag, redraw=False)
if redraw:
self.update_canvas(whence=3)
def delete_object(self, obj, redraw=True):
self.delete_objects([obj], redraw=redraw)
def raise_object_by_tag(self, tag, aboveThis=None, redraw=True):
obj1 = self.get_object_by_tag(tag)
if aboveThis is None:
self.raise_object(obj1)
else:
obj2 = self.get_object_by_tag(aboveThis)
self.raise_object(obj1, obj2)
if redraw:
self.update_canvas(whence=3)
def lower_object_by_tag(self, tag, belowThis=None, redraw=True):
obj1 = self.get_object_by_tag(tag)
if belowThis is None:
self.lower_object(obj1)
else:
obj2 = self.get_object_by_tag(belowThis)
self.lower_object(obj1, obj2)
if redraw:
self.update_canvas(whence=3)
### NON-PEP8 EQUIVALENTS -- TO BE DEPRECATED ###
deleteObjectsByTag = delete_objects_by_tag
deleteObjectByTag = delete_object_by_tag
getObjectByTag = get_object_by_tag
getTagsByTagpfx = get_tags_by_tag_pfx
getObjectsByTagpfx = get_objects_by_tag_pfx
getObjectsByTagpfx = get_objects_by_tag_pfx
deleteAllObjects = delete_all_objects
deleteObjects = delete_objects
deleteObject = delete_object
raiseObjectByTag = raise_object_by_tag
lowerObjectByTag = lower_object_by_tag
#END
|