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('elektron_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( 40, 0, 40, 40)),
00048 base_bitmap.GetSubBitmap(wx.Rect( 40, 40, 40, 40)))
00049 self._warn = (base_bitmap.GetSubBitmap(wx.Rect( 0, 0, 40, 40)),
00050 base_bitmap.GetSubBitmap(wx.Rect( 0, 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 dc.DrawBitmap(bitmap, (size.GetWidth() - bitmap.GetWidth()) / 2.0, (size.GetHeight() - bitmap.GetHeight()) / 2.0, True)
00117
00118 def set_ok(self):
00119 if (self._color == self._ok):
00120 return False
00121
00122 self._color = self._ok
00123 self.update()
00124
00125 return True
00126
00127 def set_warn(self):
00128 if (self._color == self._warn):
00129 return False
00130
00131 self._color = self._warn
00132 self.update()
00133
00134 return True
00135
00136 def set_error(self):
00137 if (self._color == self._error):
00138 return False
00139
00140 self._color = self._error
00141 self.update()
00142
00143 return True
00144
00145 def set_stale(self):
00146 if (self._color == self._stale):
00147 return False
00148
00149 self._color = self._stale
00150 self.update()
00151
00152 return True
00153
00154 def update(self):
00155 self.Refresh()