$search
00001 # Software License Agreement (BSD License) 00002 # 00003 # Copyright (c) 2008, Willow Garage, Inc. 00004 # All rights reserved. 00005 # 00006 # Redistribution and use in source and binary forms, with or without 00007 # modification, are permitted provided that the following conditions 00008 # are met: 00009 # 00010 # * Redistributions of source code must retain the above copyright 00011 # notice, this list of conditions and the following disclaimer. 00012 # * Redistributions in binary form must reproduce the above 00013 # copyright notice, this list of conditions and the following 00014 # disclaimer in the documentation and/or other materials provided 00015 # with the distribution. 00016 # * Neither the name of the Willow Garage nor the names of its 00017 # contributors may be used to endorse or promote products derived 00018 # from this software without specific prior written permission. 00019 # 00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 # POSSIBILITY OF SUCH DAMAGE. 00032 00033 # 00034 # Copied from pr2_dashboard. 00035 # 00036 00037 import roslib 00038 roslib.load_manifest('nao_dashboard') 00039 00040 import wx 00041 00042 from os import path 00043 00044 class StatusControl(wx.Window): 00045 def __init__(self, parent, id, icons_path, base_name, toggleable): 00046 wx.Window.__init__(self, parent, id) 00047 self.SetSize(wx.Size(32, 32)) 00048 00049 if (toggleable): 00050 self._ok = (wx.Bitmap(path.join(icons_path, "%s-green-untoggled.png"%(base_name)), wx.BITMAP_TYPE_PNG), 00051 wx.Bitmap(path.join(icons_path, "%s-green-toggled.png"%(base_name)), wx.BITMAP_TYPE_PNG)) 00052 self._warn = (wx.Bitmap(path.join(icons_path, "%s-yellow-untoggled.png"%(base_name)), wx.BITMAP_TYPE_PNG), 00053 wx.Bitmap(path.join(icons_path, "%s-yellow-toggled.png"%(base_name)), wx.BITMAP_TYPE_PNG)) 00054 self._error = (wx.Bitmap(path.join(icons_path, "%s-red-untoggled.png"%(base_name)), wx.BITMAP_TYPE_PNG), 00055 wx.Bitmap(path.join(icons_path, "%s-red-toggled.png"%(base_name)), wx.BITMAP_TYPE_PNG)) 00056 self._stale = (wx.Bitmap(path.join(icons_path, "%s-grey-untoggled.png"%(base_name)), wx.BITMAP_TYPE_PNG), 00057 wx.Bitmap(path.join(icons_path, "%s-grey-toggled.png"%(base_name)), wx.BITMAP_TYPE_PNG)) 00058 else: 00059 ok = wx.Bitmap(path.join(icons_path, "%s-green.png"%(base_name)), wx.BITMAP_TYPE_PNG) 00060 warn = wx.Bitmap(path.join(icons_path, "%s-yellow.png"%(base_name)), wx.BITMAP_TYPE_PNG) 00061 error = wx.Bitmap(path.join(icons_path, "%s-red.png"%(base_name)), wx.BITMAP_TYPE_PNG) 00062 stale = wx.Bitmap(path.join(icons_path, "%s-grey.png"%(base_name)), wx.BITMAP_TYPE_PNG) 00063 self._ok = (ok, ok) 00064 self._warn = (warn, warn) 00065 self._error = (error, error) 00066 self._stale = (stale, stale) 00067 00068 self._color = None 00069 self.set_stale() 00070 00071 self.Bind(wx.EVT_PAINT, self.on_paint) 00072 self.Bind(wx.EVT_LEFT_UP, self.on_left_up) 00073 self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down) 00074 self.Bind(wx.EVT_LEAVE_WINDOW, self.on_leave_window) 00075 self.Bind(wx.EVT_ENTER_WINDOW, self.on_enter_window) 00076 00077 self._toggled = False 00078 self._left_down = False 00079 00080 def toggle(self, tog): 00081 if (self._toggled == tog): 00082 return False 00083 00084 self._toggled = tog 00085 self.Refresh() 00086 00087 return True 00088 00089 def on_left_down(self, evt): 00090 self.toggle(True) 00091 self._left_down = True 00092 self.Refresh() 00093 00094 def on_left_up(self, evt): 00095 self.toggle(False) 00096 self._left_down = False 00097 x = evt.GetX() 00098 y = evt.GetY() 00099 if (x >= 0 and y >= 0 and x < self.GetSize().GetWidth() and y < self.GetSize().GetHeight()): 00100 event = wx.CommandEvent(wx.EVT_BUTTON._getEvtType(), self.GetId()) 00101 wx.PostEvent(self, event) 00102 00103 self.Refresh() 00104 00105 def on_leave_window(self, evt): 00106 self.toggle(False) 00107 self.Refresh() 00108 00109 def on_enter_window(self, evt): 00110 if (self._left_down): 00111 self.toggle(True) 00112 00113 self.Refresh() 00114 00115 def on_paint(self, evt): 00116 dc = wx.BufferedPaintDC(self) 00117 dc.SetBackground(wx.Brush(self.GetBackgroundColour())) 00118 dc.Clear() 00119 00120 size = self.GetSize(); 00121 00122 bitmap = None 00123 if (self._toggled): 00124 bitmap = self._color[1] 00125 else: 00126 bitmap = self._color[0] 00127 00128 dc.DrawBitmap(bitmap, (size.GetWidth() - bitmap.GetWidth()) / 2.0, (size.GetHeight() - bitmap.GetHeight()) / 2.0, True) 00129 00130 def set_ok(self): 00131 if (self._color == self._ok): 00132 return False 00133 00134 self._color = self._ok 00135 self.update() 00136 00137 return True 00138 00139 def set_warn(self): 00140 if (self._color == self._warn): 00141 return False 00142 00143 self._color = self._warn 00144 self.update() 00145 00146 return True 00147 00148 def set_error(self): 00149 if (self._color == self._error): 00150 return False 00151 00152 self._color = self._error 00153 self.update() 00154 00155 return True 00156 00157 def set_stale(self): 00158 if (self._color == self._stale): 00159 return False 00160 00161 self._color = self._stale 00162 self.update() 00163 00164 return True 00165 00166 def update(self): 00167 self.Refresh()