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('pr2_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(32, 32))
00044
00045 if (toggleable):
00046 self._ok = (wx.Bitmap(path.join(icons_path, "%s-green-untoggled.png"%(base_name)), wx.BITMAP_TYPE_PNG),
00047 wx.Bitmap(path.join(icons_path, "%s-green-toggled.png"%(base_name)), wx.BITMAP_TYPE_PNG))
00048 self._warn = (wx.Bitmap(path.join(icons_path, "%s-yellow-untoggled.png"%(base_name)), wx.BITMAP_TYPE_PNG),
00049 wx.Bitmap(path.join(icons_path, "%s-yellow-toggled.png"%(base_name)), wx.BITMAP_TYPE_PNG))
00050 self._error = (wx.Bitmap(path.join(icons_path, "%s-red-untoggled.png"%(base_name)), wx.BITMAP_TYPE_PNG),
00051 wx.Bitmap(path.join(icons_path, "%s-red-toggled.png"%(base_name)), wx.BITMAP_TYPE_PNG))
00052 self._stale = (wx.Bitmap(path.join(icons_path, "%s-grey-untoggled.png"%(base_name)), wx.BITMAP_TYPE_PNG),
00053 wx.Bitmap(path.join(icons_path, "%s-grey-toggled.png"%(base_name)), wx.BITMAP_TYPE_PNG))
00054 else:
00055 ok = wx.Bitmap(path.join(icons_path, "%s-green.png"%(base_name)), wx.BITMAP_TYPE_PNG)
00056 warn = wx.Bitmap(path.join(icons_path, "%s-yellow.png"%(base_name)), wx.BITMAP_TYPE_PNG)
00057 error = wx.Bitmap(path.join(icons_path, "%s-red.png"%(base_name)), wx.BITMAP_TYPE_PNG)
00058 stale = wx.Bitmap(path.join(icons_path, "%s-grey.png"%(base_name)), wx.BITMAP_TYPE_PNG)
00059 self._ok = (ok, ok)
00060 self._warn = (warn, warn)
00061 self._error = (error, error)
00062 self._stale = (stale, stale)
00063
00064 self._color = None
00065 self.set_stale()
00066
00067 self.Bind(wx.EVT_PAINT, self.on_paint)
00068 self.Bind(wx.EVT_LEFT_UP, self.on_left_up)
00069 self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
00070 self.Bind(wx.EVT_LEAVE_WINDOW, self.on_leave_window)
00071 self.Bind(wx.EVT_ENTER_WINDOW, self.on_enter_window)
00072
00073 self._toggled = False
00074 self._left_down = False
00075
00076 def toggle(self, tog):
00077 if (self._toggled == tog):
00078 return False
00079
00080 self._toggled = tog
00081 self.Refresh()
00082
00083 return True
00084
00085 def on_left_down(self, evt):
00086 self.toggle(True)
00087 self._left_down = True
00088 self.Refresh()
00089
00090 def on_left_up(self, evt):
00091 self.toggle(False)
00092 self._left_down = False
00093 x = evt.GetX()
00094 y = evt.GetY()
00095 if (x >= 0 and y >= 0 and x < self.GetSize().GetWidth() and y < self.GetSize().GetHeight()):
00096 event = wx.CommandEvent(wx.EVT_BUTTON._getEvtType(), self.GetId())
00097 wx.PostEvent(self, event)
00098
00099 self.Refresh()
00100
00101 def on_leave_window(self, evt):
00102 self.toggle(False)
00103 self.Refresh()
00104
00105 def on_enter_window(self, evt):
00106 if (self._left_down):
00107 self.toggle(True)
00108
00109 self.Refresh()
00110
00111 def on_paint(self, evt):
00112 dc = wx.BufferedPaintDC(self)
00113 dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
00114 dc.Clear()
00115
00116 size = self.GetSize();
00117
00118 bitmap = None
00119 if (self._toggled):
00120 bitmap = self._color[1]
00121 else:
00122 bitmap = self._color[0]
00123
00124 dc.DrawBitmap(bitmap, (size.GetWidth() - bitmap.GetWidth()) / 2.0, (size.GetHeight() - bitmap.GetHeight()) / 2.0, True)
00125
00126 def set_ok(self):
00127 if (self._color == self._ok):
00128 return False
00129
00130 self._color = self._ok
00131 self.update()
00132
00133 return True
00134
00135 def set_warn(self):
00136 if (self._color == self._warn):
00137 return False
00138
00139 self._color = self._warn
00140 self.update()
00141
00142 return True
00143
00144 def set_error(self):
00145 if (self._color == self._error):
00146 return False
00147
00148 self._color = self._error
00149 self.update()
00150
00151 return True
00152
00153 def set_stale(self):
00154 if (self._color == self._stale):
00155 return False
00156
00157 self._color = self._stale
00158 self.update()
00159
00160 return True
00161
00162 def update(self):
00163 self.Refresh()