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._message_queue = []
00071         self._mutex = QMutex()
00072         self._subscriber = rospy.Subscriber('/rosout_agg', Log, self._message_cb)
00073 
00074         self._console = None
00075         self.context = context
00076         self.clicked.connect(self._show_console)
00077 
00078         self.update_state(0)
00079         self._timer = QTimer()
00080         self._timer.timeout.connect(self._insert_messages)
00081         self._timer.start(100)
00082 
00083         self._rospack = rospkg.RosPack()
00084         if self._console is None:
00085             self._console = ConsoleWidget(self._proxymodel, self._rospack, minimal=self.minimal)
00086             self._console.destroyed.connect(self._console_destroyed)
00087         self._console_shown = False
00088         self.setToolTip("Rosout")
00089 
00090     def _show_console(self):
00091         if self._console is None:
00092             self._console = ConsoleWidget(self._proxymodel, self._rospack, minimal=self.minimal)
00093             self._console.destroyed.connect(self._console_destroyed)
00094         try:
00095             if self._console_shown:
00096                 self.context.remove_widget(self._console)
00097                 self._console_shown = not self._console_shown
00098             else:
00099                 self.context.add_widget(self._console)
00100                 self._console_shown = not self._console_shown
00101         except Exception:
00102             self._console_shown = not self._console_shown
00103             self._show_console()
00104 
00105     def _insert_messages(self):
00106         with QMutexLocker(self._mutex):
00107             msgs = self._message_queue
00108             self._message_queue = []
00109         if msgs:
00110             self._datamodel.insert_rows(msgs)
00111 
00112         # The console may not yet be initialized or may have been closed
00113         # So fail silently
00114         try:
00115             self.update_rosout()
00116         except:
00117             pass
00118 
00119     def _message_cb(self, log_msg):
00120         if not self._console._paused:
00121             msg = Console.convert_rosgraph_log_message(log_msg)
00122             with QMutexLocker(self._mutex):
00123                 self._message_queue.append(msg)
00124 
00125     def update_rosout(self):
00126         summary_dur = 30.0
00127         if (rospy.get_time() < 30.0):
00128             summary_dur = rospy.get_time() - 1.0
00129 
00130         if (summary_dur < 0):
00131             summary_dur = 0.0
00132 
00133         summary = self._console.get_message_summary(summary_dur)
00134 
00135         if (summary.fatal or summary.error):
00136             self.update_state(2)
00137         elif (summary.warn):
00138             self.update_state(1)
00139         else:
00140             self.update_state(0)
00141 
00142         tooltip = ""
00143         if (summary.fatal):
00144             tooltip += "\nFatal: %s" % (summary.fatal)
00145         if (summary.error):
00146             tooltip += "\nError: %s" % (summary.error)
00147         if (summary.warn):
00148             tooltip += "\nWarn: %s" % (summary.warn)
00149         if (summary.info):
00150             tooltip += "\nInfo: %s" % (summary.info)
00151         if (summary.debug):
00152             tooltip += "\nDebug: %s" % (summary.debug)
00153 
00154         if (len(tooltip) == 0):
00155             tooltip = "Rosout: no recent activity"
00156         else:
00157             tooltip = "Rosout: recent activity:" + tooltip
00158 
00159         if tooltip != self.toolTip():
00160             self.setToolTip(tooltip)
00161 
00162     def _console_destroyed(self):
00163         if self._console:
00164             self._console.cleanup_browsers_on_close()
00165         self._console = None
00166 
00167     def shutdown_widget(self):
00168         if self._console:
00169             self._console.cleanup_browsers_on_close()
00170         if self._subscriber:
00171             self._subscriber.unregister()
00172         self._timer.stop()
00173 
00174     def save_settings(self, plugin_settings, instance_settings):
00175         self._console.save_settings(plugin_settings, instance_settings)
00176 
00177     def restore_settings(self, plugin_settings, instance_settings):
00178         self._console.restore_settings(plugin_settings, instance_settings)


rqt_robot_dashboard
Author(s): Ze'ev Klapow
autogenerated on Fri Aug 28 2015 12:50:54