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
def __init__(self, name='Battery', icons=None, charge_icons=None, icon_paths=None, suppress_overlays=False, stale_icon=None)


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