battery_dash_widget.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 
34 import os
35 import rospkg
36 from python_qt_binding.QtCore import Signal, QSize
37 from python_qt_binding.QtWidgets import QLabel
38 from .util import IconHelper
39 
40 class BatteryDashWidget(QLabel):
41  """
42  A Widget which displays incremental battery state, including a status tip.
43  To use this widget simply call :func:`update_perc` and :func:`update_time`
44  to change the displayed charge percentage and time remaining, respectively.
45 
46  :param name: The name of this widget
47  :type name: str
48  """
49  state_changed = Signal(int)
50 
51  def __init__(self, name='Battery', icons=None, charge_icons=None,
52  icon_paths=None, suppress_overlays=False, stale_icon=None):
53  super(BatteryDashWidget, self).__init__()
54  if not icons:
55  icons = []
56  charge_icons = []
57  for x in range(6):
58  icons.append(['ic-battery-%s.svg' % (x * 20)])
59  charge_icons.append(['ic-battery-charge-%s.svg' % (x * 20)])
60  if not stale_icon:
61  stale_icon = ['ic-battery-0.svg', 'ol-stale-battery.svg']
62  icon_paths = (icon_paths if icon_paths else []) + [['rqt_robot_dashboard', 'images']]
63  paths = []
64  rp = rospkg.RosPack()
65  for path in icon_paths:
66  paths.append(os.path.join(rp.get_path(path[0]), path[1]))
67  self._icon_helper = IconHelper(paths, name)
68  # Add stale icon at end of icons so that it gets composited
69  icons.append(stale_icon)
70  charge_icons.append(stale_icon) # Need icons and charge_icons length to be same
71  converted_icons = self._icon_helper.set_icon_lists(icons, charge_icons, suppress_overlays)
72  self._icons = converted_icons[0]
73  self._charge_icons = converted_icons[1]
74  self._name = name
75  self._charging = False
76  self._stale = True
77  self.__state = 0
78  self.setMargin(5)
79  self.state_changed.connect(self._update_state)
80  self.update_perc(0)
81  self.update_time(0)
82 
83  def _update_state(self, state):
84  if self._stale:
85  self.setPixmap(self._icons[-1].pixmap(QSize(60, 100)))
86  elif self._charging:
87  self.setPixmap(self._charge_icons[state].pixmap(QSize(60, 100)))
88  else:
89  self.setPixmap(self._icons[state].pixmap(QSize(60, 100)))
90 
91  @property
92  def state(self):
93  """
94  Read-only accessor for the widgets current state.
95  """
96  return self.__state
97 
98  def set_charging(self, value):
99  self._charging = value
100 
101  def update_perc(self, val):
102  """
103  Update the displayed battery percentage.
104  The default implementation of this method displays in 20% increments
105 
106  :param val: The new value to be displayed.
107  :type val: int
108  """
109  self.update_state(round(val / 20.0))
110 
111  def update_state(self, state):
112  """
113  Set the state of this button.
114  This will also update the icon for the button based on the ``self._icons`` list
115 
116  :raises IndexError: If state is not a proper index to ``self._icons``
117 
118  :param state: The state to set.
119  :type state: int
120  """
121  if 0 <= state and state < len(self._icons):
122  self.__state = state
123  self.state_changed.emit(self.__state)
124  else:
125  raise IndexError("%s update_state received invalid state: %s" % (self._name, state))
126 
127  def update_time(self, value):
128  try:
129  fval = float(value)
130  self.setToolTip("%s: %.2f%% remaining" % (self._name, fval))
131  except ValueError:
132  self.setToolTip("%s: %s%% remaining" % (self._name, value))
133 
134  def set_stale(self):
135  """Set button to stale.
136 
137  Not used by base dashboard implementation.
138  """
139  self._charging = False
140  self._stale = True
141  self.setToolTip("%s: Stale" % self._name)
142  # This triggers self.update_state which in turn will trigger _update_state
143  self.update_perc(0)
144 
145  def unset_stale(self):
146  self._stale = False
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget._update_state
def _update_state(self, state)
Definition: battery_dash_widget.py:83
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.update_time
def update_time(self, value)
Definition: battery_dash_widget.py:127
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget
Definition: battery_dash_widget.py:40
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.update_state
def update_state(self, state)
Definition: battery_dash_widget.py:111
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget._icon_helper
_icon_helper
Definition: battery_dash_widget.py:66
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.set_charging
def set_charging(self, value)
Definition: battery_dash_widget.py:98
rqt_robot_dashboard.util.IconHelper
Definition: util.py:105
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget._stale
_stale
Definition: battery_dash_widget.py:75
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget._charge_icons
_charge_icons
Definition: battery_dash_widget.py:72
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.__state
__state
Definition: battery_dash_widget.py:76
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.update_perc
def update_perc(self, val)
Definition: battery_dash_widget.py:101
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget._name
_name
Definition: battery_dash_widget.py:73
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget._charging
_charging
Definition: battery_dash_widget.py:74
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.set_stale
def set_stale(self)
Definition: battery_dash_widget.py:134
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.__init__
def __init__(self, name='Battery', icons=None, charge_icons=None, icon_paths=None, suppress_overlays=False, stale_icon=None)
Definition: battery_dash_widget.py:51
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.state_changed
state_changed
Definition: battery_dash_widget.py:49
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.unset_stale
def unset_stale(self)
Definition: battery_dash_widget.py:145
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget._icons
_icons
Definition: battery_dash_widget.py:71
rqt_robot_dashboard.battery_dash_widget.BatteryDashWidget.state
def state(self)
Definition: battery_dash_widget.py:92


rqt_robot_dashboard
Author(s): Ze'ev Klapow
autogenerated on Wed Mar 2 2022 00:58:48