Go to the documentation of this file.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 os
00034
00035 from python_qt_binding.QtCore import Signal
00036 from python_qt_binding.QtGui 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])