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 import rospy
00034 from python_qt_binding.QtCore import QMutex, QMutexLocker, QSize, QTimer
00035 
00036 from rqt_console.console_subscriber import ConsoleSubscriber
00037 from rqt_console.console_widget import ConsoleWidget
00038 from rqt_console.message_data_model import MessageDataModel
00039 from rqt_console.message_proxy_model import MessageProxyModel
00040 
00041 from .icon_tool_button import IconToolButton
00042 
00043 
00044 class ConsoleDashWidget(IconToolButton):
00045     """
00046     A widget which brings up the ROS console.
00047 
00048     :param context: The plugin context to create the monitor in.
00049     :type context: qt_gui.plugin_context.PluginContext
00050     """
00051     def __init__(self, context, icon_paths=None, minimal=True):
00052         ok_icon = ['bg-green.svg', 'ic-console.svg']
00053         warn_icon = ['bg-yellow.svg', 'ic-console.svg', 'ol-warn-badge.svg']
00054         err_icon = ['bg-red.svg', 'ic-console.svg', 'ol-err-badge.svg']
00055         stale_icon = ['bg-grey.svg', 'ic-console.svg', 'ol-stale-badge.svg']
00056 
00057         icons = [ok_icon, warn_icon, err_icon, stale_icon]
00058 
00059         super(ConsoleDashWidget, self).__init__('Console Widget', icons, icon_paths=icon_paths)
00060 
00061         self.minimal = minimal
00062         self.setFixedSize(self._icons[0].actualSize(QSize(50, 30)))
00063 
00064         self._datamodel = MessageDataModel()
00065         self._proxymodel = MessageProxyModel()
00066         self._proxymodel.setSourceModel(self._datamodel)
00067 
00068         self._mutex = QMutex()
00069         self._subscriber = ConsoleSubscriber(self._message_cb)
00070 
00071         self._console = None
00072         self.context = context
00073         self.clicked.connect(self._show_console)
00074 
00075         self.update_state(0)
00076         self._timer = QTimer()
00077         self._timer.timeout.connect(self._insert_messages)
00078         self._timer.start(100)
00079 
00080         if self._console is None:
00081             self._console = ConsoleWidget(self._proxymodel, self.minimal)
00082             self._console.destroyed.connect(self._console_destroyed)
00083         self._console_shown = False
00084         self.setToolTip("Rosout")
00085 
00086     def _show_console(self):
00087         if self._console is None:
00088             self._console = ConsoleWidget(self._proxymodel, self.minimal)
00089             self._console.destroyed.connect(self._console_destroyed)
00090         try:
00091             if self._console_shown:
00092                 self.context.remove_widget(self._console)
00093                 self._console_shown = not self._console_shown
00094             else:
00095                 self.context.add_widget(self._console)
00096                 self._console_shown = not self._console_shown
00097         except Exception:
00098             self._console_shown = not self._console_shown
00099             self._show_console()
00100 
00101     def _insert_messages(self):
00102         with QMutexLocker(self._mutex):
00103             msgs = self._datamodel._insert_message_queue
00104             self._datamodel._insert_message_queue = []
00105         self._datamodel.insert_rows(msgs)
00106 
00107         # The console may not yet be initialized or may have been closed
00108         # So fail silently
00109         try:
00110             self.update_rosout()
00111             self._console.update_status()
00112         except:
00113             pass
00114 
00115     def _message_cb(self, msg):
00116         if not self._datamodel._paused:
00117             with QMutexLocker(self._mutex):
00118                 self._datamodel._insert_message_queue.append(msg)
00119 
00120     def update_rosout(self):
00121         summary_dur = 30.0
00122         if (rospy.get_time() < 30.0):
00123             summary_dur = rospy.get_time() - 1.0
00124 
00125         if (summary_dur < 0):
00126             summary_dur = 0.0
00127 
00128         summary = self._console.get_message_summary(summary_dur)
00129 
00130         if (summary.fatal or summary.error):
00131             self.update_state(2)
00132         elif (summary.warn):
00133             self.update_state(1)
00134         else:
00135             self.update_state(0)
00136 
00137         tooltip = ""
00138         if (summary.fatal):
00139             tooltip += "\nFatal: %s" % (summary.fatal)
00140         if (summary.error):
00141             tooltip += "\nError: %s" % (summary.error)
00142         if (summary.warn):
00143             tooltip += "\nWarn: %s" % (summary.warn)
00144         if (summary.info):
00145             tooltip += "\nInfo: %s" % (summary.info)
00146         if (summary.debug):
00147             tooltip += "\nDebug: %s" % (summary.debug)
00148 
00149         if (len(tooltip) == 0):
00150             tooltip = "Rosout: no recent activity"
00151         else:
00152             tooltip = "Rosout: recent activity:" + tooltip
00153 
00154         if tooltip != self.toolTip():
00155             self.setToolTip(tooltip)
00156 
00157     def _console_destroyed(self):
00158         if self._console:
00159             self._console.cleanup_browsers_on_close()
00160         self._console = None
00161 
00162     def shutdown_widget(self):
00163         if self._console:
00164             self._console.cleanup_browsers_on_close()
00165         if self._subscriber:
00166             self._subscriber.unsubscribe_topic()
00167         self._timer.stop()
00168 
00169     def save_settings(self, plugin_settings, instance_settings):
00170         self._console.save_settings(plugin_settings, instance_settings)
00171 
00172     def restore_settings(self, plugin_settings, instance_settings):
00173         self._console.restore_settings(plugin_settings, instance_settings)


rqt_robot_dashboard
Author(s): Ze'ev Klapow
autogenerated on Fri Jan 3 2014 11:56:25