PageRenderTime 253ms CodeModel.GetById 100ms app.highlight 9ms RepoModel.GetById 142ms app.codeStats 0ms

/addons/Tooltip.py

http://pyjamas.googlecode.com/
Python | 107 lines | 86 code | 17 blank | 4 comment | 4 complexity | e374404e499415fba1c392d5181ed2b3 MD5 | raw file
  1# Tooltip component for Pyjamas
  2# Ported by Willie Gollino from Tooltip component for GWT - Originally by Alexei Sokolov http://gwt.components.googlepages.com/
  3
  4from ui import PopupPanel, HTML, RootPanel
  5from Timer import Timer
  6
  7tooltip_hide_timer = None
  8
  9class Tooltip(PopupPanel):
 10    def __init__(self, sender, offsetX, offsetY, text, show_delay, hide_delay, styleName):
 11        global tooltip_hide_timer
 12        
 13        PopupPanel.__init__(self, True)
 14        self.show_delay = show_delay
 15        self.hide_delay = hide_delay
 16        
 17        contents = HTML(text)
 18        self.add(contents)
 19
 20        left = sender.getAbsoluteLeft() + offsetX
 21        top = sender.getAbsoluteTop() + offsetY
 22
 23        self.setPopupPosition(left, top)
 24        self.setStyleName(styleName)
 25
 26        if tooltip_hide_timer:
 27            self.tooltip_show_timer = Timer(1, self)
 28        else:
 29            self.tooltip_show_timer = Timer(self.show_delay, self)
 30        
 31    def show(self):
 32        global tooltip_hide_timer
 33        
 34        # activate fast tooltips
 35        tooltip_hide_timer=Timer(self.hide_delay, self)
 36        PopupPanel.show(self)
 37
 38    def hide(self):
 39        self.tooltip_show_timer.cancel()
 40        PopupPanel.hide(self)
 41
 42    def onTimer(self, id):
 43        global tooltip_hide_timer
 44
 45        # deactivate fast tooltips on last timer
 46        if tooltip_hide_timer and id == tooltip_hide_timer.getID():
 47            tooltip_hide_timer = None
 48
 49        if id == self.tooltip_show_timer.getID():
 50            self.show()
 51        else:
 52            self.hide()
 53
 54
 55class TooltipListener:
 56    DEFAULT_TOOLTIP_STYLE = "TooltipPopup"
 57    DEFAULT_OFFSET_X = 10
 58    DEFAULT_OFFSET_Y = 35
 59
 60    def __init__(self, text, show_delay = 1000, hide_delay = 5000, styleName = ""):
 61        if not styleName:
 62            styleName = TooltipListener.DEFAULT_TOOLTIP_STYLE
 63        
 64        self.tooltip = None
 65        self.text = text
 66        self.styleName = styleName
 67        self.show_delay = show_delay
 68        self.hide_delay = hide_delay
 69        self.offsetX = TooltipListener.DEFAULT_OFFSET_X
 70        self.offsetY = TooltipListener.DEFAULT_OFFSET_Y
 71
 72    def onMouseEnter(self, sender):
 73        if self.tooltip != None:
 74            self.tooltip.hide()
 75        self.tooltip = Tooltip(sender, self.offsetX, self.offsetY, self.text, self.show_delay, self.hide_delay, self.styleName)
 76
 77    def onMouseLeave(self, sender):
 78        if self.tooltip != None:
 79            self.tooltip.hide()
 80
 81    def onMouseMove(self, sender, x, y):
 82        pass
 83        
 84    def onMouseDown(self, sender, x, y):
 85        pass
 86    
 87    def onMouseUp(self, sender, x, y):
 88        pass
 89
 90    def getStyleName(self):
 91        return self.styleName
 92
 93    def setStyleName(self, styleName):
 94        self.styleName = styleName
 95
 96    def getOffsetX(self):
 97        return self.offsetX
 98
 99    def setOffsetX(self, offsetX):
100        self.offsetX = offsetX
101
102    def getOffsetY(self):
103        return self.offsetY
104
105    def setOffsetY(self, offsetY):
106        self.offsetY = offsetY
107