#!/usr/bin/env python
#
# $Id: AnimatedIcon.py,v 1.6 2001/11/21 11:37:19 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""Base class for icons to be used by the TreeNavigator.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: AnimatedIcon.py,v $',
'rcs_id' : '$Id: AnimatedIcon.py,v 1.6 2001/11/21 11:37:19 doughellmann Exp $',
'creator' : 'Doug Hellmann <doug@hellfly.net>',
'project' : 'PmwContribD',
'created' : 'Sun, 01-Apr-2001 14:10:50 EDT',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.6 $',
'date' : '$Date: 2001/11/21 11:37:19 $',
}
#
# Import system modules
#
import Tkinter
import Pmw
import sys, os, string
import Canvas
import math
#
# Import Local modules
#
from CanvasGroup import CanvasGroup
from geometrystring import geoStringToTuple
#
# Module
#
class AnimatedIcon:
"""Icon which can display itself on a canvas and respond to user interaction.
These icons are generally vector drawn, but a subclass could substitute
raster images.
"""
def __init__(self,
canvas,
name=None,
ulx=0, uly=0,
foreground='AntiqueWhite',
lightshadow='tan3',
darkshadow='tan1',
outline='black',
command=None,
allowMotion=0,
width=25,
height=20,
defaultSequence='<ButtonPress-1>',
balloonHelp=None,
balloonFcn=None,
):
"""Create an AnimatedIcon.
Arguments
'canvas' -- Canvas widget on which to draw.
'name' -- Icon name.
'ulx' -- Upper left X coordinate.
'uly' -- Upper left Y coordinate.
'foreground' -- Foreground color, default is 'AntiqueWhite'.
'lightshadow' -- Light shadow color (for top bevels), default is 'tan3'.
'darkshadow' -- Dark shadow color (for bottom bevels), default is 'tan1'.
'outline' -- Outline color, default is 'black'
'command' -- Callable object to invoke as callback.
'allowMotion' -- Boolean which controls whether the icon can
be dragged. When enabled, the user can click and drag the
icon around the canvas. Defaults to '0'.
'width' -- Width of icon.
'height' -- Height of icon.
'defaultSequence' -- Key sequence which invokes 'command'.
'balloonHelp' -- Balloon help string to be passed to 'balloonFcn'.
'balloonFcn' -- Callback to be called on '<Enter>' and
'<Leave>' events. The value of 'balloonHelp' is passed as
the only argument.
"""
# Store parameters
self.uniqueName = 'AnimatedFolder%s' % id(self)
self.defaultSequence = defaultSequence
self.balloonHelp=balloonHelp
self.balloonFcn=balloonFcn
#print 'param name is ', name
if name:
#print 'storing name'
self.name = name
else:
#print 'storing unique name'
self.name = self.uniqueName
self.canvas = canvas
self.ulx = ulx
self.uly = uly
self.foreground = foreground
self.lightshadow = lightshadow
self.darkshadow = darkshadow
self.outline = outline
self.command=command
self.width=width
self.height=height
self.canvas_objects = []
#
# Drag and drop attributes
#
self.dnd_animation = None
self.dnd_widget = None
# Create the group with the name created above
self.canvasGroup = CanvasGroup(self.canvas, tag=self.uniqueName)
self.canvasGroup.bind(self.defaultSequence, self.clickCB)
#self.canvasGroup.bind('<ButtonRelease-1>', self.releaseCB)
#self.canvasGroup.bind('<Motion>', self.motionCB)
if allowMotion:
self.canvasGroup.bind('<Button1-Motion>', self.motionCB)
if balloonFcn:
self.canvasGroup.bind('<Enter>', self.enterCB)
self.canvasGroup.bind('<Leave>', self.leaveCB)
# Create our icon image
self.createImage()
# Store the previous coordinates where
self.previous_coords = (ulx, uly)
return
def __str__(self):
return self.name
def set_geometry(self, width=None, height=None):
"Set the width and or height and redraw."
if width != None:
self.width = width
if height != None:
self.height = height
self.redraw()
return
def redraw(self):
pass
def bind(self, event_sequence, command):
"Bind a callback to an event sequence."
self.canvasGroup.bind(event_sequence, command)
return
def motionCB(self, event):
"Called when there is pointer motion over the icon."
#print 'event (%d,%d)' % (event.x_root, event.y_root)
#x,y = self.pointercoordstowidget(event.x_root, event.y_root, self.canvas)
x,y = (event.x_root, event.y_root)
#print 'corrected (%d, %d)' % (x,y)
#x,y = self.pointercoordstowidget(event.x, event.y, self.canvas)
#move_x = event.x_root - self.previous_coords[0]
#move_y = event.y_root - self.previous_coords[1]
move_x = x - self.previous_coords[0]
move_y = y - self.previous_coords[1]
self.move(move_x, move_y)
self.previous_coords = (x,y)
return
def move(self, x, y):
"Move the icon following the standard Tk move() definition."
self.canvasGroup.move(x, y)
return
def releaseCB(self, event):
"A mouse button was released."
self.button_down = None
return
def clickCB(self, event):
"A mouse button is clicked."
#print 'clickCB'
if self.command:
self.command(self, event)
return
def addtag(self, tag):
"Just like other Canvas object 'addtag' methods."
map(lambda x, t=tag: x.addtag(t), self.canvas_objects)
return
def enterCB(self, event=None):
"Called when mouse pointer enters the icon."
self.balloonFcn(self, self.balloonHelp)
return
def leaveCB(self, event=None):
"Called when mouse pointer leaves the icon."
self.balloonFcn(self, None)
return
def dupe(self, canvas=None, command=None, x=None, y=None):
"Make another icon just like this one."
if not canvas:
canvas = self.canvas
name=self.name
else:
name='Copy of %s' % self.name,
if x == None:
ulx=self.ulx
else:
ulx=x
if y == None:
uly=self.uly
else:
uly=y
copy = self.__class__(
canvas,
name,
ulx=ulx,
uly=uly,
foreground=self.foreground,
lightshadow=self.lightshadow,
darkshadow=self.darkshadow,
outline=self.outline,
allowMotion=1,
width=self.width,
height=self.height,
command=command,
)
return copy
def bbox(self):
"Return the bounding box of the icon."
return self.canvasGroup.bbox()
def delete(self):
"Remove the icon from the Canvas."
return self.canvasGroup.delete()
def pointercoordstowidget(self, x, y, widget):
"""Correct pointer coordinates
The correction is calculated based on the offset
over a specific widget and return the new (x,y).
"""
geom = geoStringToTuple(
#self.workarea.component('hull').winfo_geometry()
#widget.winfo_geometry()
widget.master.winfo_geometry(),
)
pointer_x = widget.canvasx(x) - geom[2]
pointer_y = widget.canvasy(y) - geom[3]
return (pointer_x, pointer_y)
#
# Drag and drop stuff
#
def moveItemToEvent(self, event):
"""DND related method.
The dnd event coordinates are relative to the root window,
not just the canvas. We have to convert them by translating
into the canvas drawing area. Then we can figure out the
change since the last movement, and move the item again.
"""
item = self.dnd_animation
return self.moveCanvasObjectToEvent(item, event)
def moveCanvasObjectToEvent(self, item, event):
"""DND related method.
"""
#print 'moveItemToEvent: moving %s to %d, %d' % (item.name,
# event.x, event.y)
bbox = item.bbox()
widget = self.dnd_widget
pointer_x, pointer_y = self.pointercoordstowidget(
event.x, event.y, widget)
dx = pointer_x - bbox[0][0]
dy = pointer_y - bbox[0][1]
item.move(dx, dy)
return
def dnd_end(self, target, event):
"Abandon drag and drop"
#print 'dnd_end: ', self, self.__class__.__name__, target
pass
def dnd_start(self, widget, event):
"Start dragging"
x,y = self.pointercoordstowidget(
event.x, event.y, widget)
#print '\ndnd_start: %s on %s(%s), %d, %d' % (
# self.name,
# widget.widgetName,
# widget.master.widgetName,
# x,
# y)
copy = self.dupe(canvas=widget, x=x, y=y)
self.dnd_animation = copy
self.dnd_widget = widget
return
def dnd_stop(self):
"Stop dragging"
#print 'dnd_stop: %s' % self.name
copy = self.dnd_animation
copy.delete()
self.dnd_animation = None
self.dnd_widget = None
return
def dnd_motion(self, event):
"While dragging."
#print 'dnd_motion: %s, %d, %d' % (self.name, event.x, event.y)
self.moveItemToEvent(event)
return
|