Package pyjamas :: Package ui :: Module Tooltip
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.Tooltip

  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  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  4   
  5  from PopupPanel import PopupPanel 
  6  from pyjamas import Factory 
  7  from HTML import HTML 
  8  from RootPanel import RootPanel 
  9  from pyjamas.Timer import Timer 
 10   
 11  tooltip_hide_timer = None 
 12   
13 -class Tooltip(PopupPanel):
14 - def __init__(self, sender, offsetX, offsetY, contents, 15 show_delay, hide_delay, styleName, **kwargs):
16 """ contents may be a text string or it may be a widget 17 """ 18 PopupPanel.__init__(self, True, **kwargs) 19 self.show_delay = show_delay 20 self.hide_delay = hide_delay 21 22 if isinstance(contents, str): 23 contents = HTML(contents) 24 self.add(contents) 25 26 left = sender.getAbsoluteLeft() + offsetX 27 top = sender.getAbsoluteTop() + offsetY 28 29 self.setPopupPosition(left, top) 30 self.setStyleName(styleName) 31 32 if tooltip_hide_timer: 33 self.tooltip_show_timer = Timer(1, self) 34 else: 35 self.tooltip_show_timer = Timer(self.show_delay, self)
36
37 - def show(self):
38 global tooltip_hide_timer 39 40 # activate fast tooltips 41 tooltip_hide_timer = Timer(self.hide_delay, self) 42 PopupPanel.show(self)
43
44 - def hide(self, autoClosed=False):
45 self.tooltip_show_timer.cancel() 46 PopupPanel.hide(self, autoClosed)
47
48 - def onTimer(self, tooltip_id):
49 global tooltip_hide_timer 50 51 # deactivate fast tooltips on last timer 52 if tooltip_hide_timer and tooltip_id == tooltip_hide_timer.getID(): 53 tooltip_hide_timer = None 54 55 if tooltip_id == self.tooltip_show_timer.getID(): 56 self.show() 57 else: 58 self.hide()
59 60
61 -class TooltipListener:
62 DEFAULT_TOOLTIP_STYLE = "TooltipPopup" 63 DEFAULT_OFFSET_X = 10 64 DEFAULT_OFFSET_Y = 35 65
66 - def __init__(self, text, show_delay = 1000, hide_delay = 5000, styleName = ""):
67 if not styleName: 68 styleName = TooltipListener.DEFAULT_TOOLTIP_STYLE 69 70 self.tooltip = None 71 self.text = text 72 self.styleName = styleName 73 self.show_delay = show_delay 74 self.hide_delay = hide_delay 75 self.offsetX = TooltipListener.DEFAULT_OFFSET_X 76 self.offsetY = TooltipListener.DEFAULT_OFFSET_Y
77
78 - def onMouseEnter(self, sender):
79 if self.tooltip is not None: 80 self.tooltip.hide() 81 self.tooltip = Tooltip(sender, self.offsetX, self.offsetY, self.text, self.show_delay, self.hide_delay, self.styleName)
82
83 - def onMouseLeave(self, sender):
84 if self.tooltip is not None: 85 self.tooltip.hide()
86
87 - def onMouseMove(self, sender, x, y):
88 pass
89
90 - def onMouseDown(self, sender, x, y):
91 pass
92
93 - def onMouseUp(self, sender, x, y):
94 pass
95
96 - def getStyleName(self):
97 return self.styleName
98
99 - def setStyleName(self, styleName):
100 self.styleName = styleName
101
102 - def getOffsetX(self):
103 return self.offsetX
104
105 - def setOffsetX(self, offsetX):
106 self.offsetX = offsetX
107
108 - def getOffsetY(self):
109 return self.offsetY
110
111 - def setOffsetY(self, offsetY):
112 self.offsetY = offsetY
113 114 Factory.registerClass('pyjamas.ui.Tooltip', Tooltip) 115