icon_tool_button.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import os
34 
35 from python_qt_binding.QtCore import Signal
36 from python_qt_binding.QtWidgets import QToolButton
37 import rospy
38 
39 from .util import IconHelper
40 
41 
42 class IconToolButton(QToolButton):
43  """
44  This is the base class for all widgets.
45  It provides state and icon switching support as well as convenience functions for creating icons.
46 
47  :raises IndexError: if ``icons`` is not a list of lists of strings
48 
49  :param name: name of the object
50  :type name: str
51  :param icons: A list of lists of strings to create icons for the states of this button.\
52  If only one is supplied then ok, warn, error, stale icons will be created with overlays
53 
54  :type icons: list
55  :param clicked_icons: A list of clicked state icons. len must equal icons
56  :type clicked_icons: list
57  :param suppress_overlays: if false and there is only one icon path supplied
58  :type suppress_overlays: bool
59  :param icon_paths: list of lists of package and subdirectory in the form\
60  ['package name', 'subdirectory'] example ['rqt_pr2_dashboard', 'images/svg']
61 
62  :type icon_paths: list of lists of strings
63  """
64  state_changed = Signal(int)
65 
66  def __init__(self, name, icons, clicked_icons=None, suppress_overlays=False, icon_paths=None):
67  super(IconToolButton, self).__init__()
68 
69  self.name = name
70  self.setObjectName(self.name)
71 
72  self.state_changed.connect(self._update_state)
73  self.pressed.connect(self._pressed)
74  self.released.connect(self._released)
75 
76  import rospkg
77  icon_paths = (icon_paths if icon_paths else []) + [['rqt_robot_dashboard', 'images']]
78  paths = []
79  rp = rospkg.RosPack()
80  for path in icon_paths:
81  paths.append(os.path.join(rp.get_path(path[0]), path[1]))
82  self.icon_helper = IconHelper(paths, name)
83  converted_icons = self.icon_helper.set_icon_lists(icons, clicked_icons, suppress_overlays)
84  self._icons = converted_icons[0]
85  self._clicked_icons = converted_icons[1]
86 
87  self.setStyleSheet('QToolButton {border: none;}')
88 
89  self.__state = 0
90 
91 
92  def update_state(self, state):
93  """
94  Set the state of this button.
95  This will also update the icon for the button based on the ``self._icons`` list
96 
97  :raises IndexError: If state is not a proper index to ``self._icons``
98 
99  :param state: The state to set.
100  :type state: int
101  """
102  if 0 <= state and state < len(self._icons):
103  self.__state = state
104  self.state_changed.emit(self.__state)
105  else:
106  raise IndexError("%s update_state received invalid state: %s" % (self.name, state))
107 
108  @property
109  def state(self):
110  """
111  Read-only accessor for the widgets current state.
112  """
113  return self.__state
114 
115  def _update_state(self, state):
116  if self.isDown():
117  self.setIcon(self._clicked_icons[self.__state])
118  else:
119  self.setIcon(self._icons[self.__state])
120 
121  def _pressed(self):
122  self.setIcon(self._clicked_icons[self.__state])
123 
124  def _released(self):
125  self.setIcon(self._icons[self.__state])
def __init__(self, name, icons, clicked_icons=None, suppress_overlays=False, icon_paths=None)


rqt_robot_dashboard
Author(s): Ze'ev Klapow
autogenerated on Fri May 15 2020 03:39:04