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 pyjamas.ui.PopupPanel import PopupPanel 
  6  from pyjamas import Factory 
  7  from pyjamas.ui.HTML import HTML 
  8  from pyjamas.ui.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, basestring): 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, timer):
49 global tooltip_hide_timer 50 51 # deactivate fast tooltips on last timer 52 if timer is tooltip_hide_timer: 53 tooltip_hide_timer = None 54 55 if timer is self.tooltip_show_timer: 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 = self.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 = self.DEFAULT_OFFSET_X 76 self.offsetY = self.DEFAULT_OFFSET_Y
77
78 - def onMouseEnter(self, sender):
79 self.hide() 80 self.tooltip = Tooltip(sender, self.offsetX, self.offsetY, self.text, self.show_delay, self.hide_delay, self.styleName)
81
82 - def onMouseLeave(self, sender):
83 self.hide()
84
85 - def onMouseMove(self, sender, x, y):
86 pass
87
88 - def onMouseDown(self, sender, x, y):
89 pass
90
91 - def onMouseUp(self, sender, x, y):
92 pass
93
94 - def getStyleName(self):
95 return self.styleName
96
97 - def setStyleName(self, styleName):
98 self.styleName = styleName
99
100 - def getOffsetX(self):
101 return self.offsetX
102
103 - def setOffsetX(self, offsetX):
104 self.offsetX = offsetX
105
106 - def getOffsetY(self):
107 return self.offsetY
108
109 - def setOffsetY(self, offsetY):
110 self.offsetY = offsetY
111
112 - def hide(self):
113 if self.tooltip is not None: 114 self.tooltip.hide()
115 116 Factory.registerClass('pyjamas.ui.Tooltip', 'Tooltip', Tooltip) 117