#!/usr/bin/env python
#
# $Id: AnimatedFileIcon.py,v 1.3 2001/11/03 11:05:22 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.
#
"""A file icon which can be used by the TreeNavigator.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: AnimatedFileIcon.py,v $',
'rcs_id' : '$Id: AnimatedFileIcon.py,v 1.3 2001/11/03 11:05:22 doughellmann Exp $',
'creator' : 'Doug Hellmann <doug@hellfly.net>',
'project' : 'PmwContribD',
'created' : 'Sun, 01-Apr-2001 13:33:16 EDT',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.3 $',
'date' : '$Date: 2001/11/03 11:05:22 $',
}
#
# Import system modules
#
import Tkinter
import Pmw
import sys, os, string
import Canvas
import math
#
# Import Local modules
#
import AnimatedIcon, colormath
#
# Module
#
class AnimatedFileIcon(AnimatedIcon.AnimatedIcon):
def createImage(self):
"Draws the icon representation."
self.mainOutline = None
self.width_to_height_ratio = 8.5/11.0
self.corner_tab_porportion = 4
self.state = 'closed'
#
# The page shadow
#
self.shadowOutline = Canvas.Polygon(self.canvas,
self.shadowClosedCoords(),
outline='black',
fill='black',
)
self.shadowOutline.addtag(self.uniqueName)
self.canvas_objects.append(self.shadowOutline)
#
# The page outline
#
self.mainOutline = Canvas.Polygon(self.canvas,
self.outlineClosedCoords(),
outline=self.outline,
fill=self.foreground,
)
self.mainOutline.addtag(self.uniqueName)
self.canvas_objects.append(self.mainOutline)
#
# The folded over tab at the corner
#
colors = colormath.computeColorTriplet(self.canvas, self.foreground)
self.cornerTab = Canvas.Polygon(self.canvas,
self.cornerTabCoords(),
outline='black',
#outline=self.foreground,
fill=colors[1],
)
self.cornerTab.addtag(self.uniqueName)
self.canvas_objects.append(self.cornerTab)
# Store the previous coordinates where
self.previous_coords = (self.ulx, self.uly)
return
def getUL(self):
"Returns the upper left corner of the icon."
if self.mainOutline:
bbox = self.mainOutline.bbox()
ulx=bbox[0][0] + 2
uly=bbox[0][1] + 2
else:
ulx=self.ulx
uly=self.uly
return (ulx, uly)
def cornerTabCoords(self):
"Return (top, bottom_right, bottom_left) for the corner tab triangle."
ulx, uly = self.getUL()
width = self.width
height = self.height
corner_offset = width / self.corner_tab_porportion
top = (ulx+width-corner_offset, uly)
bottom_right = (ulx+width, uly+corner_offset)
bottom_left = (ulx+width-corner_offset, uly+corner_offset)
return (
top,
bottom_right,
bottom_left,
)
def outlineClosedCoords(self):
"Returns the coordinates when the icon is closed."
ulx, uly = self.getUL()
width = self.width
height = self.height
corner_offset = width / self.corner_tab_porportion
top_left = (ulx,uly)
top_right = (ulx+width-corner_offset,uly)
top_right2 = (ulx+width, uly+corner_offset)
bottom_right = (ulx+width, uly+height-1)
bottom_left = (ulx, uly+height-1)
return (
top_left,
top_right,
top_right2,
bottom_right,
bottom_left,
)
def shadowClosedCoords(self):
"Returns the shadow coordinates when in the closed state."
ulx, uly = self.getUL()
ulx = ulx + 1
uly = uly + 1
width = self.width
height = self.height
corner_offset = width / self.corner_tab_porportion
top_left = (ulx,uly)
top_right = (ulx+width-corner_offset,uly)
top_right2 = (ulx+width, uly+corner_offset)
bottom_right = (ulx+width, uly+height)
bottom_left = (ulx, uly+height)
return (
top_left,
top_right,
top_right2,
bottom_right,
bottom_left,
)
def outlineOpenCoords(self):
"Return outline coordinates when in open state."
return self.outlineClosedCoords()
def shadowOpenCoords(self):
"Return shadow coordinates when in open state."
return self.shadowClosedCoords()
def drawClosed(self):
"Draw the icon in closed state."
colors = colormath.computeColorTriplet(self.canvas, self.foreground)
self.shadowOutline.coords( self.shadowClosedCoords() )
self.mainOutline.coords( self.outlineClosedCoords() )
self.cornerTab.coords( self.cornerTabCoords() )
self.cornerTab['fill'] = colors[1]
return
def drawOpen(self):
"Draw the icon in open state."
self.drawClosed()
return
def redraw(self):
"Draw the icon in the current state."
if self.state == 'closed':
self.drawClosed()
elif self.state == 'open':
self.drawOpen()
else:
self.drawClosed()
return
def set_geometry(self, width=None, height=None):
"""Set the width and height of the icon.
The aspect ratio defined for the icon is respected.
Arguments
'width' -- New width value.
'height' -- New height value
"""
if height != None:
width = height * self.width_to_height_ratio
AnimatedIcon.AnimatedIcon.set_geometry(self,
width=width,
height=height)
return
def set_state(self, new_state):
"Set the current state of the widget and then redraw."
self.state = new_state
self.redraw()
return
def clickCB(self, event):
"Called when someone clicks on the icon."
self.previous_coords = (event.x_root, event.y_root)
if self.state == 'closed':
self.set_state('open')
else:
self.set_state('closed')
AnimatedIcon.AnimatedIcon.clickCB(self, event)
return
if __name__ == '__main__':
import GuiAppD
class DrawTest(GuiAppD.GuiAppD):
appname = 'Test animating file'
def createInterface(self):
self.scrolledCanvas = self.createcomponent('canvas', (), None,
Pmw.ScrolledCanvas,
(self.interior(),),
canvas_background='white'
)
self.scrolledCanvas.pack(
side=Tkinter.TOP,
expand=Tkinter.YES,
fill=Tkinter.BOTH
)
self.canvas = self.scrolledCanvas.component('canvas')
self.button_down = None
self.contents = []
ulx = 150
uly = 100
item1 = AnimatedFileIcon(self.canvas, name='item1', ulx=ulx, uly=uly,
command=self.clicked)
item1.bind('<Enter>', self.enterCB)
item2 = AnimatedFileIcon(self.canvas, ulx=ulx*2, uly=uly+2,
command=self.clicked,
allowMotion=1,
)
def clicked(self, icon, item):
#print 'clicked on %s!' % item
pass
def enterCB(self, event):
#print 'entered item1'
pass
DrawTest().run()
|