publisher_widget.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (c) 2011, Dorian Scholz, TU Darmstadt
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 the TU Darmstadt 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 HOLDER 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 __future__ import division
00034 import os
00035 
00036 from python_qt_binding import loadUi
00037 from python_qt_binding.QtCore import Signal, Slot
00038 from python_qt_binding.QtGui import QIcon
00039 from python_qt_binding.QtWidgets import QWidget
00040 
00041 import roslib
00042 import rosmsg
00043 import rospkg
00044 import rospy
00045 
00046 from qt_gui_py_common.worker_thread import WorkerThread
00047 from rqt_py_common.extended_combo_box import ExtendedComboBox
00048 from .publisher_tree_widget import PublisherTreeWidget
00049 
00050 
00051 # main class inherits from the ui window class
00052 class PublisherWidget(QWidget):
00053     add_publisher = Signal(str, str, float, bool)
00054     change_publisher = Signal(int, str, str, str, object)
00055     publish_once = Signal(int)
00056     remove_publisher = Signal(int)
00057     clean_up_publishers = Signal()
00058 
00059     def __init__(self, parent=None):
00060         super(PublisherWidget, self).__init__(parent)
00061         self._topic_dict = {}
00062         self._update_thread = WorkerThread(self._update_thread_run, self._update_finished)
00063 
00064         self._rospack = rospkg.RosPack()
00065         ui_file = os.path.join(self._rospack.get_path('rqt_publisher'), 'resource', 'Publisher.ui')
00066         loadUi(ui_file, self,
00067                {'ExtendedComboBox': ExtendedComboBox, 'PublisherTreeWidget': PublisherTreeWidget})
00068         self.refresh_button.setIcon(QIcon.fromTheme('view-refresh'))
00069         self.refresh_button.clicked.connect(self.refresh_combo_boxes)
00070         self.add_publisher_button.setIcon(QIcon.fromTheme('list-add'))
00071         self.remove_publisher_button.setIcon(QIcon.fromTheme('list-remove'))
00072         self.clear_button.setIcon(QIcon.fromTheme('edit-clear'))
00073 
00074         self.refresh_combo_boxes()
00075 
00076         self.publisher_tree_widget.model().item_value_changed.connect(self.change_publisher)
00077         self.publisher_tree_widget.remove_publisher.connect(self.remove_publisher)
00078         self.publisher_tree_widget.publish_once.connect(self.publish_once)
00079         self.remove_publisher_button.clicked.connect(
00080             self.publisher_tree_widget.remove_selected_publishers)
00081         self.clear_button.clicked.connect(self.clean_up_publishers)
00082 
00083     def shutdown_plugin(self):
00084         self._update_thread.kill()
00085 
00086     @Slot()
00087     def refresh_combo_boxes(self):
00088         self._update_thread.kill()
00089         self.type_combo_box.setEnabled(False)
00090         self.topic_combo_box.setEnabled(False)
00091         self.type_combo_box.setEditText('updating...')
00092         self.topic_combo_box.setEditText('updating...')
00093         self._update_thread.start()
00094 
00095     # this runs in a non-gui thread, so don't access widgets here directly
00096     def _update_thread_run(self):
00097         # update type_combo_box
00098         message_type_names = []
00099         try:
00100             # this only works on fuerte and up
00101             packages = sorted(
00102                 [pkg_tuple[0] for pkg_tuple in
00103                     rosmsg.iterate_packages(self._rospack, rosmsg.MODE_MSG)])
00104         except:
00105             # this works up to electric
00106             packages = sorted(rosmsg.list_packages())
00107         for package in packages:
00108             for base_type_str in rosmsg.list_msgs(package, rospack=self._rospack):
00109                 message_class = roslib.message.get_message_class(base_type_str)
00110                 if message_class is not None:
00111                     message_type_names.append(base_type_str)
00112 
00113         self.type_combo_box.setItems.emit(sorted(message_type_names))
00114 
00115         # update topic_combo_box
00116         _, _, topic_types = rospy.get_master().getTopicTypes()
00117         self._topic_dict = dict(topic_types)
00118         self.topic_combo_box.setItems.emit(sorted(self._topic_dict.keys()))
00119 
00120     @Slot()
00121     def _update_finished(self):
00122         self.type_combo_box.setEnabled(True)
00123         self.topic_combo_box.setEnabled(True)
00124 
00125     @Slot(str)
00126     def on_topic_combo_box_currentIndexChanged(self, topic_name):
00127         if topic_name in self._topic_dict:
00128             self.type_combo_box.setEditText(self._topic_dict[topic_name])
00129 
00130     @Slot()
00131     def on_add_publisher_button_clicked(self):
00132         topic_name = str(self.topic_combo_box.currentText())
00133         type_name = str(self.type_combo_box.currentText())
00134         rate = float(self.frequency_combo_box.currentText())
00135         enabled = False
00136         self.add_publisher.emit(topic_name, type_name, rate, enabled)


rqt_publisher
Author(s): Dorian Scholz
autogenerated on Thu Jun 6 2019 17:40:36