/usr/lib/python3/dist-packages/ginga/tkw/ImageViewTk.py is in python3-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 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | #
# ImageViewTk.py -- a backend for Ginga using a Tk canvas widget
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
import numpy
import PIL.Image as PILimage
import PIL.ImageTk as PILimageTk
from ginga import Mixins, Bindings, colors
from ginga.canvas.mixins import DrawingMixin, CanvasMixin, CompoundMixin
from ginga.util.toolbox import ModeIndicator
from ginga.tkw import TkHelp
try:
# See if we have aggdraw module--best choice
from ginga.aggw.ImageViewAgg import ImageViewAgg as ImageView, \
ImageViewAggError as ImageViewError
except ImportError:
try:
# No, hmm..ok, see if we have opencv module...
from ginga.cvw.ImageViewCv import ImageViewCv as ImageView, \
ImageViewCvError as ImageViewError
except ImportError:
try:
# No dice. How about the PIL module?
from ginga.pilw.ImageViewPil import ImageViewPil as ImageView, \
ImageViewPilError as ImageViewError
except ImportError:
# Fall back to mock--there will be no graphic overlays
from ginga.mockw.ImageViewMock import ImageViewMock as ImageView, \
ImageViewMockError as ImageViewError
class ImageViewTkError(ImageViewError):
pass
class ImageViewTk(ImageView):
def __init__(self, logger=None, rgbmap=None, settings=None):
ImageView.__init__(self, logger=logger,
rgbmap=rgbmap,
settings=settings)
self.tkcanvas = None
self.tkphoto = None
self._defer_task = None
self.msgtask = None
def set_widget(self, canvas):
"""Call this method with the Tkinter canvas that will be used
for the display.
"""
self.tkcanvas = canvas
canvas.bind("<Configure>", self._resize_cb)
width = canvas.winfo_width()
height = canvas.winfo_height()
# see reschedule_redraw() method
self._defer_task = TkHelp.Timer(0.0,
lambda timer: self.delayed_redraw(),
tkcanvas=canvas)
self.msgtask = TkHelp.Timer(0.0,
lambda timer: self.onscreen_message(None),
tkcanvas=canvas)
self.configure_window(width, height)
def get_widget(self):
return self.tkcanvas
def update_image(self):
if self.tkcanvas is None:
return
cr = self.tkcanvas
# remove all old items from the canvas
items = cr.find_all()
for item in items:
cr.delete(item)
wd, ht = self.get_window_size()
# Get surface as a numpy array
arr8 = self.get_image_as_array()
# make a Tk photo image and stick it to the canvas
image = PILimage.fromarray(arr8)
photo = PILimageTk.PhotoImage(image)
# hang on to a reference otherwise it gets gc'd
self.tkphoto = photo
cr.create_image(0, 0, anchor='nw', image=photo)
# is this necessary?
cr.config(scrollregion=cr.bbox('all'))
def reschedule_redraw(self, time_sec):
self._defer_task.cancel()
self._defer_task.start(time_sec)
def configure_window(self, width, height):
self.configure_surface(width, height)
def _resize_cb(self, event):
self.configure_window(event.width, event.height)
def set_cursor(self, cursor):
if self.tkcanvas is None:
return
self.tkcanvas.config(cursor=cursor)
def onscreen_message(self, text, delay=None, redraw=True):
if self.tkcanvas is None:
return
self.msgtask.cancel()
self.set_onscreen_message(text, redraw=redraw)
if delay is not None:
self.msgtask.start(delay)
class ImageViewEvent(ImageViewTk):
def __init__(self, logger=None, rgbmap=None, settings=None):
ImageViewTk.__init__(self, logger=logger, rgbmap=rgbmap,
settings=settings)
# Does widget accept focus when mouse enters window
self.enter_focus = self.t_.get('enter_focus', True)
self._button = 0
# @$%&^(_)*&^ tk!!
self._keytbl = {
'shift_l': 'shift_l',
'shift_r': 'shift_r',
'control_l': 'control_l',
'control_r': 'control_r',
'alt_l': 'alt_l',
'alt_r': 'alt_r',
'super_l': 'super_l',
'super_r': 'super_r',
'meta_right': 'meta_right',
'asciitilde': '~',
'grave': 'backquote',
'exclam': '!',
'at': '@',
'numbersign': '#',
'percent': '%',
'asciicircum': '^',
'ampersand': '&',
'asterisk': '*',
'dollar': '$',
'parenleft': '(',
'parenright': ')',
'underscore': '_',
'minus': '-',
'plus': '+',
'equal': '=',
'braceleft': '{',
'braceright': '}',
'bracketleft': '[',
'bracketright': ']',
'bar': '|',
'colon': ':',
'semicolon': ';',
'quotedbl': 'doublequote',
'apostrophe': 'singlequote',
'backslash': 'backslash',
'less': '<',
'greater': '>',
'comma': ',',
'period': '.',
'question': '?',
'slash': '/',
'space': 'space',
'escape': 'escape',
'return': 'return',
'tab': 'tab',
'f1': 'f1',
'f2': 'f2',
'f3': 'f3',
'f4': 'f4',
'f5': 'f5',
'f6': 'f6',
'f7': 'f7',
'f8': 'f8',
'f9': 'f9',
'f10': 'f10',
'f11': 'f11',
'f12': 'f12',
}
# Define cursors for pick and pan
#hand = openHandCursor()
hand = 'fleur'
self.define_cursor('pan', hand)
cross = 'cross'
self.define_cursor('pick', cross)
for name in ('motion', 'button-press', 'button-release',
'key-press', 'key-release', 'drag-drop',
'scroll', 'map', 'focus', 'enter', 'leave',
):
self.enable_callback(name)
def set_widget(self, canvas):
super(ImageViewEvent, self).set_widget(canvas)
canvas.bind("<Enter>", self.enter_notify_event)
canvas.bind("<Leave>", self.leave_notify_event)
canvas.bind("<FocusIn>", lambda evt: self.focus_event(evt, True))
canvas.bind("<FocusOut>", lambda evt: self.focus_event(evt, False))
canvas.bind("<KeyPress>", self.key_press_event)
canvas.bind("<KeyRelease>", self.key_release_event)
#canvas.bind("<Map>", self.map_event)
# scroll events in tk are overloaded into the button press events
canvas.bind("<ButtonPress>", self.button_press_event)
canvas.bind("<ButtonRelease>", self.button_release_event)
canvas.bind("<Motion>", self.motion_notify_event)
# TODO: Set up widget as a drag and drop destination
return self.make_callback('map')
def transkey(self, keyname):
self.logger.debug("key name in tk '%s'" % (keyname))
try:
return self._keytbl[keyname.lower()]
except KeyError:
return keyname
def get_keyTable(self):
return self._keytbl
def set_enter_focus(self, tf):
self.enter_focus = tf
def focus_event(self, event, hasFocus):
return self.make_callback('focus', hasFocus)
def enter_notify_event(self, event):
if self.enter_focus:
self.tkcanvas.focus_set()
return self.make_callback('enter')
def leave_notify_event(self, event):
self.logger.debug("leaving widget...")
return self.make_callback('leave')
def key_press_event(self, event):
# without this we do not get key release events if the focus
# changes to another window
self.tkcanvas.grab_set_global()
keyname = event.keysym
keyname = self.transkey(keyname)
self.logger.debug("key press event, key=%s" % (keyname))
return self.make_ui_callback('key-press', keyname)
def key_release_event(self, event):
self.tkcanvas.grab_release()
keyname = event.keysym
keyname = self.transkey(keyname)
self.logger.debug("key release event, key=%s" % (keyname))
return self.make_ui_callback('key-release', keyname)
def button_press_event(self, event):
x = event.x; y = event.y
self.last_win_x, self.last_win_y = x, y
button = 0
if event.num != 0:
# some kind of wierd convention for scrolling, shoehorned into
# Tk, I guess
if event.num in (4, 5):
direction = 0.0 # up
if event.num == 5:
# down
direction = 180.0
# 15 deg is standard 1-click turn for a wheel mouse
numDegrees = 15.0
self.logger.debug("scroll deg=%f direction=%f" % (
numDegrees, direction))
data_x, data_y = self.get_data_xy(x, y)
self.last_data_x, self.last_data_y = data_x, data_y
return self.make_ui_callback('scroll', direction, numDegrees,
data_x, data_y)
button |= 0x1 << (event.num - 1)
self._button = button
self.logger.debug("button event at %dx%d, button=%x" % (x, y, button))
data_x, data_y = self.get_data_xy(x, y)
self.last_data_x, self.last_data_y = data_x, data_y
return self.make_ui_callback('button-press', button, data_x, data_y)
def button_release_event(self, event):
# event.button, event.x, event.y
x = event.x; y = event.y
self.last_win_x, self.last_win_y = x, y
button = 0
if event.num != 0:
if event.num in (4, 5):
return False
button |= 0x1 << (event.num - 1)
self._button = 0
self.logger.debug("button release at %dx%d button=%x" % (x, y, button))
data_x, data_y = self.get_data_xy(x, y)
self.last_data_x, self.last_data_y = data_x, data_y
return self.make_ui_callback('button-release', button, data_x, data_y)
def motion_notify_event(self, event):
#button = 0
button = self._button
x, y = event.x, event.y
self.last_win_x, self.last_win_y = x, y
# num = event.num
# if num == 1:
# button |= 0x1
# elif num == 2:
# button |= 0x2
# elif num == 3:
# button |= 0x4
self.logger.debug("motion event at %dx%d, button=%x" % (x, y, button))
data_x, data_y = self.get_data_xy(x, y)
self.last_data_x, self.last_data_y = data_x, data_y
return self.make_ui_callback('motion', button, data_x, data_y)
## def drop_event(self, widget, context, x, y, selection, targetType,
## time):
## if targetType != self.TARGET_TYPE_TEXT:
## return False
## paths = selection.data.split('\n')
## self.logger.debug("dropped filename(s): %s" % (str(paths)))
## return self.make_ui_callback('drag-drop', paths)
class ImageViewZoom(Mixins.UIMixin, ImageViewEvent):
# class variables for binding map and bindings can be set
bindmapClass = Bindings.BindingMapper
bindingsClass = Bindings.ImageViewBindings
@classmethod
def set_bindingsClass(cls, klass):
cls.bindingsClass = klass
@classmethod
def set_bindmapClass(cls, klass):
cls.bindmapClass = klass
def __init__(self, logger=None, rgbmap=None, settings=None,
bindmap=None, bindings=None):
ImageViewEvent.__init__(self, logger=logger, rgbmap=rgbmap,
settings=settings)
Mixins.UIMixin.__init__(self)
self.ui_setActive(True)
if bindmap is None:
bindmap = ImageViewZoom.bindmapClass(self.logger)
self.bindmap = bindmap
bindmap.register_for_events(self)
if bindings is None:
bindings = ImageViewZoom.bindingsClass(self.logger)
self.set_bindings(bindings)
def get_bindmap(self):
return self.bindmap
def get_bindings(self):
return self.bindings
def set_bindings(self, bindings):
self.bindings = bindings
bindings.set_bindings(self)
class CanvasView(ImageViewZoom):
def __init__(self, logger=None, settings=None, rgbmap=None,
bindmap=None, bindings=None):
ImageViewZoom.__init__(self, logger=logger, settings=settings,
rgbmap=rgbmap,
bindmap=bindmap, bindings=bindings)
# Needed for UIMixin to propagate events correctly
self.objects = [self.private_canvas]
def set_canvas(self, canvas, private_canvas=None):
super(CanvasView, self).set_canvas(canvas,
private_canvas=private_canvas)
self.objects[0] = self.private_canvas
class ImageViewCanvasError(ImageViewTkError):
pass
class ImageViewCanvas(ImageViewZoom,
DrawingMixin, CanvasMixin, CompoundMixin):
def __init__(self, logger=None, rgbmap=None, settings=None,
bindmap=None, bindings=None):
ImageViewZoom.__init__(self, logger=logger,
rgbmap=rgbmap,
settings=settings,
bindmap=bindmap,
bindings=bindings)
CompoundMixin.__init__(self)
CanvasMixin.__init__(self)
DrawingMixin.__init__(self)
# we are both a viewer and a canvas
self.set_canvas(self, private_canvas=self)
self._mi = ModeIndicator(self)
#END
|