dashboard.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 from python_qt_binding.QtCore import QSize, Qt
34 from python_qt_binding.QtWidgets import QToolBar, QGroupBox, QHBoxLayout
35 from qt_gui.plugin import Plugin
36 
37 
39  """
40  Base class from which dashboards should inherit.
41 
42  :param context: the plugin context
43  :type context: qt_gui.plugin.Plugin
44  """
45  def __init__(self, context):
46  super(Dashboard, self).__init__(context)
47  self.context = context
48  self.setup(context)
49 
50  if not hasattr(self, 'name'):
51  self.name = 'Dashboard'
52  if not hasattr(self, 'max_icon_size'):
53  self.max_icon_size = QSize(50, 30)
54  self._main_widget = QToolBar()
55  self._main_widget.setIconSize(self.max_icon_size)
56  self._main_widget.setObjectName(self.name)
57  self._main_widget.setWindowTitle(self.name)
58  if context.serial_number() > 1:
59  self._main_widget.setWindowTitle(self._main_widget.windowTitle() + (' (%d)' % context.serial_number()))
60 
61  # Convert list of widgets into layout
62  self.add_widgets()
63 
64  # Display the dashboard
65  context.add_toolbar(self._main_widget)
66 
67  def setup(self, context):
68  """
69  Called during ``__init__`` Subclasses should do initialization here.
70 
71  NOTE when overriding this method you should provide a ``self.name`` to
72  avoid naming conflicts.
73 
74  :param context: The plugin context
75  :type context: qt_gui.plugin.Plugin
76  """
77  pass
78 
79  def shutdown_plugin(self):
80  """
81  Called when the toolbar is closed by Qt.
82  """
83  for widget in self._widgets:
84  if hasattr(widget, 'shutdown_widget'):
85  widget.shutdown_widget()
86  if hasattr(widget, 'close'):
87  widget.close()
88 
89  self.shutdown_dashboard()
90 
91  def shutdown_dashboard(self):
92  """
93  Called after shutdown plugin, subclasses should do cleanup here, not in shutdown_plugin
94  """
95  pass
96 
97  def get_widgets(self):
98  """
99  Most of the dashboard customization should be done here.
100  If this function is not overriden the dashboard will display nothing.
101 
102  :returns: List of lists containing dashboard widgets, or list of lists
103  containing a string followed by a list of dashboard widgets.
104  """
105  return []
106 
107  def add_widgets(self):
108  """
109  Add groups of widgets to _main_widget. Supports group labels.
110 
111  This method can be reimplemented in order to customize appearances.
112  """
113  widgets = self.get_widgets()
114  self._widgets = [] # stores widgets which may need to be shut down when done
115  for group in widgets:
116  # Check for group label
117  if isinstance(group[0], str):
118  grouplabel, v = group
119  box = QGroupBox(grouplabel)
120  box.setContentsMargins(0, 18, 0, 0) # LTRB
121  # Apply the center-label directive only for single-icon groups
122  if len(group[1]) == 1:
123  box.setAlignment(Qt.AlignHCenter)
124  else:
125  box = QGroupBox()
126  box.setContentsMargins(0, 0, 0, 0) # LTRB
127  v = group
128  # Add widgets to QGroupBox
129  layout = QHBoxLayout()
130  layout.setSpacing(0)
131  layout.setContentsMargins(0, 0, 0, 0) # LTRB
132  for i in v:
133  try:
134  try:
135  i.setIconSize(self.max_icon_size) # without this, icons are tiny
136  except AttributeError as e:
137  # triggers with battery which uses a QLabel instead of a QToolButton-based widget
138  pass
139  layout.addWidget(i)
140  self._widgets.append(i)
141  except:
142  raise Exception("All widgets must be a subclass of QWidget!")
143 
144  layout.activate()
145  box.setLayout(layout)
146  self._main_widget.addWidget(box)
147  self._main_widget.addSeparator()


rqt_robot_dashboard
Author(s): Ze'ev Klapow
autogenerated on Mon Feb 28 2022 23:38:55