messages_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 os
00034 
00035 import rospkg
00036 import rosmsg
00037 import roslib
00038 
00039 from python_qt_binding import loadUi
00040 from python_qt_binding.QtCore import Qt
00041 from python_qt_binding.QtGui import QAction, QIcon, QMenu, QMessageBox, QTreeView, QWidget
00042 
00043 from .messages_tree_view import MessagesTreeView
00044 
00045 from rqt_console.text_browse_dialog import TextBrowseDialog
00046 
00047 
00048 class MessagesWidget(QWidget):
00049     def __init__(self, mode=rosmsg.MODE_MSG):
00050         super(MessagesWidget, self).__init__()
00051         ui_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'messages.ui')
00052         loadUi(ui_file, self, {'MessagesTreeView': MessagesTreeView})
00053         self.setObjectName('MessagesUi')
00054         self._mode = mode
00055 
00056         self.add_button.setIcon(QIcon.fromTheme('list-add'))
00057         self.add_button.clicked.connect(self._add_message)
00058         self._refresh_packages()
00059         self._refresh_messages(self.package_combo.itemText(0))
00060         self.package_combo.currentIndexChanged[str].connect(self._refresh_messages)
00061         self.messages_tree.mousePressEvent = self._handle_mouse_press
00062 
00063         self._browsers = []
00064 
00065     def _refresh_packages(self):
00066         rospack = rospkg.RosPack()
00067         packages = sorted([pkg_tuple[0] for pkg_tuple in rosmsg.iterate_packages(rospack, self._mode)])
00068         self._package_list = packages
00069         self.package_combo.clear()
00070         self.package_combo.addItems(self._package_list)
00071         self.package_combo.setCurrentIndex(0)
00072 
00073     def _refresh_messages(self, package=None):
00074         if package is None or len(package) == 0:
00075             return
00076         self._messages = []
00077         if self._mode == rosmsg.MODE_MSG:
00078             message_list = rosmsg.list_msgs(package)
00079         elif self._mode == rosmsg.MODE_SRV:
00080             message_list = rosmsg.list_srvs(package)
00081         for message in message_list:
00082             if self._mode == rosmsg.MODE_MSG:
00083                 message_class = roslib.message.get_message_class(message)
00084             elif self._mode == rosmsg.MODE_SRV:
00085                 message_class = roslib.message.get_service_class(message)
00086             if message_class is not None:
00087                 self._messages.append(message)
00088 
00089         self._messages = [x.split('/')[1] for x in self._messages]
00090 
00091         self.message_combo.clear()
00092         self.message_combo.addItems(self._messages)
00093 
00094     def _add_message(self):
00095         if self.message_combo.count() == 0:
00096             return
00097         message = self.package_combo.currentText() + '/' + self.message_combo.currentText()
00098         if self._mode == rosmsg.MODE_MSG:
00099             message_class = roslib.message.get_message_class(message)()
00100             self.messages_tree.model().add_message(message_class, self.tr('Message Root'), message, message)
00101         elif self._mode == rosmsg.MODE_SRV:
00102             message_class = roslib.message.get_service_class(message)()
00103             self.messages_tree.model().add_message(message_class._request_class, self.tr('Service Request'), message, message)
00104             self.messages_tree.model().add_message(message_class._response_class, self.tr('Service Response'), message, message)
00105         self.messages_tree._recursive_set_editable(self.messages_tree.model().invisibleRootItem(), False)
00106 
00107     def _handle_mouse_press(self, event, old_pressEvent=QTreeView.mousePressEvent):
00108         if event.buttons() & Qt.RightButton and event.modifiers() == Qt.NoModifier:
00109             self._rightclick_menu(event)
00110             event.accept()
00111         return old_pressEvent(self.messages_tree, event)
00112 
00113     def _rightclick_menu(self, event):
00114         menu = QMenu()
00115         text_action = QAction(self.tr('View Text'), menu)
00116         menu.addAction(text_action)
00117         raw_action = QAction(self.tr('View Raw'), menu)
00118         menu.addAction(raw_action)
00119 
00120         action = menu.exec_(event.globalPos())
00121 
00122         if action == raw_action or action == text_action:
00123             selected = self.messages_tree.selectedIndexes()
00124             selected_type = selected[1].data()
00125 
00126             if selected_type[-2:] == '[]':
00127                 selected_type = selected_type[:-2]
00128             browsetext = None
00129             try:
00130                 if self._mode == rosmsg.MODE_MSG:
00131                     browsetext = rosmsg.get_msg_text(selected_type, action == raw_action)
00132                 elif self._mode == rosmsg.MODE_SRV:
00133                     browsetext = rosmsg.get_srv_text(selected_type, action == raw_action)
00134                 else:
00135                     raise
00136             except rosmsg.ROSMsgException:
00137                 QMessageBox.warning(self, self.tr('Warning'), self.tr('The selected item component does not have text to view.'))
00138             if browsetext is not None:
00139                 self._browsers.append(TextBrowseDialog(browsetext))
00140                 self._browsers[-1].show()
00141         else:
00142             return
00143 
00144     def cleanup_browsers_on_close(self):
00145         for browser in self._browsers:
00146             browser.close()


rqt_msg
Author(s): Aaron Blasdel
autogenerated on Fri Jan 3 2014 11:54:52