Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
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         
00108         
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)