console_dash_widget.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 from rosgraph_msgs.msg import Log
00034 import rospkg
00035 import rospy
00036 from python_qt_binding.QtCore import QMutex, QMutexLocker, QSize, QTimer
00037 
00038 from rqt_console.console import Console
00039 from rqt_console.console_widget import ConsoleWidget
00040 from rqt_console.message_data_model import MessageDataModel
00041 from rqt_console.message_proxy_model import MessageProxyModel
00042 
00043 from .icon_tool_button import IconToolButton
00044 
00045 
00046 class ConsoleDashWidget(IconToolButton):
00047     """
00048     A widget which brings up the ROS console.
00049 
00050     :param context: The plugin context to create the monitor in.
00051     :type context: qt_gui.plugin_context.PluginContext
00052     """
00053     def __init__(self, context, icon_paths=None, minimal=True):
00054         ok_icon = ['bg-green.svg', 'ic-console.svg']
00055         warn_icon = ['bg-yellow.svg', 'ic-console.svg', 'ol-warn-badge.svg']
00056         err_icon = ['bg-red.svg', 'ic-console.svg', 'ol-err-badge.svg']
00057         stale_icon = ['bg-grey.svg', 'ic-console.svg', 'ol-stale-badge.svg']
00058 
00059         icons = [ok_icon, warn_icon, err_icon, stale_icon]
00060 
00061         super(ConsoleDashWidget, self).__init__('Console Widget', icons, icon_paths=icon_paths)
00062 
00063         self.minimal = minimal
00064         self.setFixedSize(self._icons[0].actualSize(QSize(50, 30)))
00065 
00066         self._datamodel = MessageDataModel()
00067         self._proxymodel = MessageProxyModel()
00068         self._proxymodel.setSourceModel(self._datamodel)
00069 
00070         self._console = None
00071         self._rospack = rospkg.RosPack()
00072         if self._console is None:
00073             self._console = ConsoleWidget(self._proxymodel, self._rospack, minimal=self.minimal)
00074             self._console.destroyed.connect(self._console_destroyed)
00075 
00076         self._message_queue = []
00077         self._mutex = QMutex()
00078         self._subscriber = rospy.Subscriber('/rosout_agg', Log, self._message_cb)
00079 
00080         self.context = context
00081         self.clicked.connect(self._show_console)
00082 
00083         self.update_state(0)
00084         self._timer = QTimer()
00085         self._timer.timeout.connect(self._insert_messages)
00086         self._timer.start(100)
00087 
00088         self._console_shown = False
00089         self.setToolTip("Rosout")
00090 
00091     def _show_console(self):
00092         if self._console is None:
00093             self._console = ConsoleWidget(self._proxymodel, self._rospack, minimal=self.minimal)
00094             self._console.destroyed.connect(self._console_destroyed)
00095         try:
00096             if self._console_shown:
00097                 self.context.remove_widget(self._console)
00098                 self._console_shown = not self._console_shown
00099             else:
00100                 self.context.add_widget(self._console)
00101                 self._console_shown = not self._console_shown
00102         except Exception:
00103             self._console_shown = not self._console_shown
00104             self._show_console()
00105 
00106     def _insert_messages(self):
00107         with QMutexLocker(self._mutex):
00108             msgs = self._message_queue
00109             self._message_queue = []
00110         if msgs:
00111             self._datamodel.insert_rows(msgs)
00112 
00113         # The console may not yet be initialized or may have been closed
00114         # So fail silently
00115         try:
00116             self.update_rosout()
00117         except:
00118             pass
00119 
00120     def _message_cb(self, log_msg):
00121         if not self._console._paused:
00122             msg = Console.convert_rosgraph_log_message(log_msg)
00123             with QMutexLocker(self._mutex):
00124                 self._message_queue.append(msg)
00125 
00126     def update_rosout(self):
00127         summary_dur = 30.0
00128         if (rospy.get_time() < 30.0):
00129             summary_dur = rospy.get_time() - 1.0
00130 
00131         if (summary_dur < 0):
00132             summary_dur = 0.0
00133 
00134         summary = self._console.get_message_summary(summary_dur)
00135 
00136         if (summary.fatal or summary.error):
00137             self.update_state(2)
00138         elif (summary.warn):
00139             self.update_state(1)
00140         else:
00141             self.update_state(0)
00142 
00143         tooltip = ""
00144         if (summary.fatal):
00145             tooltip += "\nFatal: %s" % (summary.fatal)
00146         if (summary.error):
00147             tooltip += "\nError: %s" % (summary.error)
00148         if (summary.warn):
00149             tooltip += "\nWarn: %s" % (summary.warn)
00150         if (summary.info):
00151             tooltip += "\nInfo: %s" % (summary.info)
00152         if (summary.debug):
00153             tooltip += "\nDebug: %s" % (summary.debug)
00154 
00155         if (len(tooltip) == 0):
00156             tooltip = "Rosout: no recent activity"
00157         else:
00158             tooltip = "Rosout: recent activity:" + tooltip
00159 
00160         if tooltip != self.toolTip():
00161             self.setToolTip(tooltip)
00162 
00163     def _console_destroyed(self):
00164         if self._console:
00165             self._console.cleanup_browsers_on_close()
00166         self._console = None
00167 
00168     def shutdown_widget(self):
00169         if self._console:
00170             self._console.cleanup_browsers_on_close()
00171         if self._subscriber:
00172             self._subscriber.unregister()
00173         self._timer.stop()
00174 
00175     def save_settings(self, plugin_settings, instance_settings):
00176         self._console.save_settings(plugin_settings, instance_settings)
00177 
00178     def restore_settings(self, plugin_settings, instance_settings):
00179         self._console.restore_settings(plugin_settings, instance_settings)


rqt_robot_dashboard
Author(s): Ze'ev Klapow
autogenerated on Sat Jun 8 2019 20:02:45