00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import roslib
00034 roslib.load_manifest('lwr_dashboard')
00035
00036 import wx
00037
00038 from os import path
00039
00040 class StatusControl(wx.Window):
00041 def __init__(self, parent, id, icons_path, base_name, toggleable):
00042 wx.Window.__init__(self, parent, id)
00043 self.SetSize(wx.Size(40, 40))
00044
00045 base_bitmap = wx.Bitmap(path.join(icons_path, "%s.png" % base_name), wx.BITMAP_TYPE_PNG);
00046
00047 self._ok = (base_bitmap.GetSubBitmap(wx.Rect( 0, 0, 40, 40)),
00048 base_bitmap.GetSubBitmap(wx.Rect( 0, 40, 40, 40)))
00049 self._warn = (base_bitmap.GetSubBitmap(wx.Rect( 40, 0, 40, 40)),
00050 base_bitmap.GetSubBitmap(wx.Rect( 40, 40, 40, 40)))
00051 self._error = (base_bitmap.GetSubBitmap(wx.Rect( 80, 0, 40, 40)),
00052 base_bitmap.GetSubBitmap(wx.Rect( 80, 40, 40, 40)))
00053 self._stale = (base_bitmap.GetSubBitmap(wx.Rect(120, 0, 40, 40)),
00054 base_bitmap.GetSubBitmap(wx.Rect(120, 40, 40, 40)))
00055
00056 self._color = None
00057 self.set_stale()
00058
00059 self.Bind(wx.EVT_PAINT, self.on_paint)
00060 self.Bind(wx.EVT_LEFT_UP, self.on_left_up)
00061 self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
00062 self.Bind(wx.EVT_LEAVE_WINDOW, self.on_leave_window)
00063 self.Bind(wx.EVT_ENTER_WINDOW, self.on_enter_window)
00064
00065 self._toggled = False
00066 self._left_down = False
00067
00068 def toggle(self, tog):
00069 if (self._toggled == tog):
00070 return False
00071
00072 self._toggled = tog
00073 self.Refresh()
00074
00075 return True
00076
00077 def on_left_down(self, evt):
00078 self.toggle(True)
00079 self._left_down = True
00080 self.Refresh()
00081
00082 def on_left_up(self, evt):
00083 self.toggle(False)
00084 self._left_down = False
00085 x = evt.GetX()
00086 y = evt.GetY()
00087 if (x >= 0 and y >= 0 and x < self.GetSize().GetWidth() and y < self.GetSize().GetHeight()):
00088 event = wx.CommandEvent(wx.EVT_BUTTON._getEvtType(), self.GetId())
00089 wx.PostEvent(self, event)
00090
00091 self.Refresh()
00092
00093 def on_leave_window(self, evt):
00094 self.toggle(False)
00095 self.Refresh()
00096
00097 def on_enter_window(self, evt):
00098 if (self._left_down):
00099 self.toggle(True)
00100
00101 self.Refresh()
00102
00103 def on_paint(self, evt):
00104 dc = wx.BufferedPaintDC(self)
00105 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
00106 dc.Clear()
00107
00108 size = self.GetSize();
00109
00110 bitmap = None
00111 if (self._toggled):
00112 bitmap = self._color[1]
00113 else:
00114 bitmap = self._color[0]
00115
00116 offset_x = (size.GetWidth() - bitmap.GetWidth()) / 2.0
00117 offset_y = (size.GetHeight() - bitmap.GetHeight()) / 2.0
00118
00119 dc.DrawBitmap(bitmap, offset_x, offset_y, True)
00120
00121 def set_ok(self):
00122 if (self._color == self._ok):
00123 return False
00124
00125 self._color = self._ok
00126 self.update()
00127
00128 return True
00129
00130 def set_warn(self):
00131 if (self._color == self._warn):
00132 return False
00133
00134 self._color = self._warn
00135 self.update()
00136
00137 return True
00138
00139 def set_error(self):
00140 if (self._color == self._error):
00141 return False
00142
00143 self._color = self._error
00144 self.update()
00145
00146 return True
00147
00148 def set_stale(self):
00149 if (self._color == self._stale):
00150 return False
00151
00152 self._color = self._stale
00153 self.update()
00154
00155 return True
00156
00157 def update(self):
00158 self.Refresh()