icon_tool_button.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, 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 Willow Garage, Inc. 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 os
00034 
00035 from python_qt_binding.QtCore import Signal
00036 from python_qt_binding.QtWidgets import QToolButton
00037 import rospy
00038 
00039 from .util import IconHelper
00040 
00041 
00042 class IconToolButton(QToolButton):
00043     """
00044     This is the base class for all widgets.
00045     It provides state and icon switching support as well as convenience functions for creating icons.
00046 
00047     :raises IndexError: if ``icons`` is not a list of lists of strings
00048 
00049     :param name: name of the object
00050     :type name: str
00051     :param icons: A list of lists of strings to create icons for the states of this button.\
00052     If only one is supplied then ok, warn, error, stale icons will be created with overlays
00053 
00054     :type icons: list
00055     :param clicked_icons: A list of clicked state icons. len must equal icons
00056     :type clicked_icons: list
00057     :param suppress_overlays: if false and there is only one icon path supplied
00058     :type suppress_overlays: bool
00059     :param icon_paths: list of lists of package and subdirectory in the form\
00060     ['package name', 'subdirectory'] example ['rqt_pr2_dashboard', 'images/svg']
00061 
00062     :type icon_paths: list of lists of strings
00063     """
00064     state_changed = Signal(int)
00065 
00066     def __init__(self, name, icons, clicked_icons=None, suppress_overlays=False, icon_paths=None):
00067         super(IconToolButton, self).__init__()
00068 
00069         self.name = name
00070         self.setObjectName(self.name)
00071 
00072         self.state_changed.connect(self._update_state)
00073         self.pressed.connect(self._pressed)
00074         self.released.connect(self._released)
00075 
00076         import rospkg
00077         icon_paths = (icon_paths if icon_paths else []) + [['rqt_robot_dashboard', 'images']]
00078         paths = []
00079         rp = rospkg.RosPack()
00080         for path in icon_paths:
00081             paths.append(os.path.join(rp.get_path(path[0]), path[1]))
00082         self.icon_helper = IconHelper(paths, name)
00083         converted_icons = self.icon_helper.set_icon_lists(icons, clicked_icons, suppress_overlays)
00084         self._icons = converted_icons[0]
00085         self._clicked_icons = converted_icons[1]
00086 
00087         self.setStyleSheet('QToolButton {border: none;}')
00088 
00089         self.__state = 0
00090 
00091 
00092     def update_state(self, state):
00093         """
00094         Set the state of this button.
00095         This will also update the icon for the button based on the ``self._icons`` list
00096 
00097         :raises IndexError: If state is not a proper index to ``self._icons``
00098 
00099         :param state: The state to set.
00100         :type state: int
00101         """
00102         if 0 <= state and state < len(self._icons):
00103             self.__state = state
00104             self.state_changed.emit(self.__state)
00105         else:
00106             raise IndexError("%s update_state received invalid state: %s" % (self.name, state))
00107 
00108     @property
00109     def state(self):
00110         """
00111         Read-only accessor for the widgets current state.
00112         """
00113         return self.__state
00114 
00115     def _update_state(self, state):
00116         if self.isDown():
00117             self.setIcon(self._clicked_icons[self.__state])
00118         else:
00119             self.setIcon(self._icons[self.__state])
00120 
00121     def _pressed(self):
00122         self.setIcon(self._clicked_icons[self.__state])
00123 
00124     def _released(self):
00125         self.setIcon(self._icons[self.__state])


rqt_robot_dashboard
Author(s): Ze'ev Klapow
autogenerated on Wed May 3 2017 02:40:43