widgets.py
Go to the documentation of this file.
1 import os
2 import re
3 import qt_gui.plugin
4 import python_qt_binding
5 
6 from .constants import _MICROSTRAIN_ROS_VERISON
7 from .constants import _PACKAGE_RESOURCE_DIR
8 from .constants import _NODE_NAME_ENV_KEY, _DEFAULT_NODE_NAME
9 from .services import DeviceReportMonitor
10 
11 class MicrostrainWidget(python_qt_binding.QtWidgets.QWidget):
12 
13  def __init__(self, node, name):
14  # Initialize the parent class
15  super(MicrostrainWidget, self).__init__()
16 
17  # Load the UI spec
18  python_qt_binding.loadUi(os.path.join(_PACKAGE_RESOURCE_DIR, '%s.ui' % name), self)
19 
20  # Set some other metadata
21  self.setObjectName(name)
22  self.setWindowTitle(name)
23 
24  # Save a copy of the node
25  self._node = node
26 
27  # Since this is loaded as an RQT plugin, we can't get config from a launch file. Instead, read an environment variable to determine what the default node name should be
28  self._node_name = self.sanitize_node_name(os.getenv(_NODE_NAME_ENV_KEY, _DEFAULT_NODE_NAME))
29 
30  # Set up the service status monitors
32 
33  # Configure with the default node name
34  self._configure()
35 
36  # Start the UI update loop
37  self._run_timer = python_qt_binding.QtCore.QTimer()
38  self._run_timer.setSingleShot(False)
39  self._run_timer.timeout.connect(self.run)
40  self._run_timer.start(250)
41 
42  def shutdown_timer(self):
43  self._run_timer.stop()
44  del self._run_timer
45 
46  def _configure(self):
47  # Expected to be implemented in the implementing class
48  pass
49 
50  def run(self):
51  # Expected to be implemented in the implementing class
52  pass
53 
54  def reconfigure(self, node_name):
55  # Update the device monitor, and our node name
56  self._node_name = node_name
57  self._device_report_monitor = DeviceReportMonitor(self._node, self._node_name)
58 
59  # Call the configure function again
60  self._configure()
61 
62  @staticmethod
63  def sanitize_node_name(node_name):
64  node_name = re.sub(r'\/+', '/', node_name) # Remove duplicate slashes
65  if not node_name.startswith('/'): # Start with a slash
66  node_name = '/' + node_name
67  if node_name.endswith('/'): # Do not end with a slash
68  node_name = node_name[:-1]
69  return node_name
70 
71  @staticmethod
72  def hide_show_widgets(widgets, show):
73  for widget in widgets:
74  if show and widget.isHidden():
75  widget.show()
76  elif not show and not widget.isHidden():
77  widget.hide()
78 
79 
81 
82  def __init__(self, context, name, widget_type):
83  # Initialize the parent class
84  super(MicrostrainPlugin, self).__init__(context)
85 
86  # Set some other metadata
87  self.setObjectName(name)
88 
89  # Save a copy of the node
90  if _MICROSTRAIN_ROS_VERISON == 2:
91  node = context.node
92  else:
93  node = None
94 
95  # Initialize the widget
96  self._widget = widget_type(node)
97 
98  # Show _widget.windowTitle on left-top of each plugin (when
99  # it's set in _widget). This is useful when you open multiple
100  # plugins at once. Also if you open multiple instances of your
101  # plugin at once, these lines add number to make it easy to
102  # tell from pane to pane.
103  if context.serial_number() > 1:
104  self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
105 
106  # Add widget to the user interface
107  context.add_widget(self._widget)
108 
109  def shutdown_plugin(self):
110  # Stop the update loop
111  self._widget.shutdown_timer()
112 
113  # Call the parent function
114  return super(MicrostrainPlugin, self).shutdown_plugin()
microstrain_inertial_rqt.utils.widgets.MicrostrainPlugin._widget
_widget
Definition: widgets.py:96
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget._device_report_monitor
_device_report_monitor
Definition: widgets.py:31
microstrain_inertial_rqt.utils.services.DeviceReportMonitor
Definition: services.py:9
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget._run_timer
_run_timer
Definition: widgets.py:37
qt_gui::plugin::Plugin
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget._node_name
_node_name
Definition: widgets.py:28
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget._node
_node
Definition: widgets.py:25
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget._configure
def _configure(self)
Definition: widgets.py:46
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget.run
def run(self)
Definition: widgets.py:50
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget.hide_show_widgets
def hide_show_widgets(widgets, show)
Definition: widgets.py:72
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget.sanitize_node_name
def sanitize_node_name(node_name)
Definition: widgets.py:63
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget.shutdown_timer
def shutdown_timer(self)
Definition: widgets.py:42
microstrain_inertial_rqt.utils.widgets.MicrostrainPlugin.__init__
def __init__(self, context, name, widget_type)
Definition: widgets.py:82
qt_gui::plugin
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget
Definition: widgets.py:11
microstrain_inertial_rqt.utils.widgets.MicrostrainPlugin.shutdown_plugin
def shutdown_plugin(self)
Definition: widgets.py:109
microstrain_inertial_rqt.utils.widgets.MicrostrainPlugin
Definition: widgets.py:80
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget.reconfigure
def reconfigure(self, node_name)
Definition: widgets.py:54
microstrain_inertial_rqt.utils.widgets.MicrostrainWidget.__init__
def __init__(self, node, name)
Definition: widgets.py:13


microstrain_inertial_rqt
Author(s): Parker Hannifin Corp
autogenerated on Fri Apr 18 2025 02:52:41