$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 import roslib; roslib.load_manifest('generic_dashboard') 00034 00035 import wx 00036 00037 from os import path 00038 from generic_control import GenericControl 00039 00040 class StatusControl(GenericControl): 00041 def __init__(self, parent, icons_path, base_name, toggleable, states, default_state): 00042 GenericControl.__init__(self, parent) 00043 self.SetSize(wx.Size(32, 32)) 00044 00045 self._states = states 00046 self._bitmaps = {} 00047 for state, filekey in states.iteritems(): 00048 bitmap = wx.Bitmap(path.join(icons_path, "%s-%s.png"%(base_name, filekey)), wx.BITMAP_TYPE_PNG) 00049 self._bitmaps[(state, False)] = bitmap 00050 if toggleable: 00051 bitmap = wx.Bitmap(path.join(icons_path, "%s-%s-toggled.png"%(base_name, filekey)), wx.BITMAP_TYPE_PNG) 00052 self._bitmaps[(state, True)] = bitmap 00053 00054 self._color = default_state 00055 00056 self.Bind(wx.EVT_PAINT, self.on_paint) 00057 self.Bind(wx.EVT_LEFT_UP, self.on_left_up) 00058 self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down) 00059 self.Bind(wx.EVT_LEAVE_WINDOW, self.on_leave_window) 00060 self.Bind(wx.EVT_ENTER_WINDOW, self.on_enter_window) 00061 00062 self._toggled = False 00063 self._left_down = False 00064 00065 def toggle(self, tog): 00066 if (self._toggled == tog): 00067 return False 00068 00069 self._toggled = tog 00070 self.Refresh() 00071 00072 return True 00073 00074 def on_left_down(self, evt): 00075 self.toggle(True) 00076 self._left_down = True 00077 self.Refresh() 00078 00079 def on_left_up(self, evt): 00080 self.toggle(False) 00081 self._left_down = False 00082 x = evt.GetX() 00083 y = evt.GetY() 00084 if (x >= 0 and y >= 0 and x < self.GetSize().GetWidth() and y < self.GetSize().GetHeight()): 00085 event = wx.CommandEvent(wx.EVT_BUTTON._getEvtType(), self.GetId()) 00086 wx.PostEvent(self, event) 00087 00088 self.Refresh() 00089 00090 def on_leave_window(self, evt): 00091 self.toggle(False) 00092 self.Refresh() 00093 00094 def on_enter_window(self, evt): 00095 if (self._left_down): 00096 self.toggle(True) 00097 00098 self.Refresh() 00099 00100 def on_paint(self, evt): 00101 dc = wx.BufferedPaintDC(self) 00102 dc.SetBackground(wx.Brush(self.GetBackgroundColour())) 00103 dc.Clear() 00104 00105 if self._color is None: 00106 return 00107 00108 size = self.GetSize() 00109 00110 bitmap = self._bitmaps[ (self._color, self._toggled) ] 00111 00112 dc.DrawBitmap(bitmap, (size.GetWidth() - bitmap.GetWidth()) / 2.0, (size.GetHeight() - bitmap.GetHeight()) / 2.0, True) 00113 00114 def set_status(self, status): 00115 if self._color == status: 00116 return False 00117 00118 self._color = status 00119 self.update() 00120 00121 return True 00122 00123 def update(self): 00124 self.Refresh()