timeline_menu.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2009, 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 python_qt_binding.QtWidgets import QVBoxLayout, QMenu, QWidget, QDockWidget
00034 
00035 
00036 class TopicPopupWidget(QWidget):
00037 
00038     def __init__(self, popup_name, timeline, viewer_type, topic):
00039         super(TopicPopupWidget, self).__init__()
00040         self.setObjectName(popup_name)
00041         self.setWindowTitle(popup_name)
00042 
00043         layout = QVBoxLayout()
00044         self.setLayout(layout)
00045 
00046         self._timeline = timeline
00047         self._viewer_type = viewer_type
00048         self._topic = topic
00049         self._viewer = None
00050         self._is_listening = False
00051 
00052     def hideEvent(self, event):
00053         if self._is_listening:
00054             self._timeline.remove_listener(self._topic, self._viewer)
00055             self._is_listening = False
00056         super(TopicPopupWidget, self).hideEvent(event)
00057 
00058     def showEvent(self, event):
00059         if not self._is_listening:
00060             self._timeline.add_listener(self._topic, self._viewer)
00061             self._is_listening = True
00062         super(TopicPopupWidget, self).showEvent(event)
00063 
00064     def show(self, context):
00065         """
00066         Make this topic popup visible, if necessary. This includes setting up
00067         the proper close button hacks
00068         """
00069         if not self.parent():
00070             context.add_widget(self)
00071             # make the dock widget closable, even if it normally isn't
00072             dock_features = self.parent().features()
00073             dock_features |= QDockWidget.DockWidgetClosable
00074             self.parent().setFeatures(dock_features)
00075 
00076             # remove old listener
00077             if self._viewer:
00078                 self._timeline.remove_listener(self._topic, self._viewer)
00079                 self._viewer = None
00080 
00081             # clean out the layout
00082             while self.layout().count() > 0:
00083                 item = self.layout().itemAt(0)
00084                 self.layout().removeItem(item)
00085 
00086             # create a new viewer
00087             self._viewer = self._viewer_type(self._timeline, self, self._topic)
00088             if not self._is_listening:
00089                 self._timeline.add_listener(self._topic, self._viewer)
00090                 self._is_listening = True
00091 
00092         super(TopicPopupWidget, self).show()
00093 
00094 
00095 class TimelinePopupMenu(QMenu):
00096 
00097     """
00098     Custom popup menu displayed on rightclick from timeline
00099     """
00100 
00101     def __init__(self, timeline, event, menu_topic):
00102         super(TimelinePopupMenu, self).__init__()
00103 
00104         self.parent = timeline
00105         self.timeline = timeline
00106 
00107         if menu_topic is not None:
00108             self.setTitle(menu_topic)
00109             self._menu_topic = menu_topic
00110         else:
00111             self._menu_topic = None
00112 
00113         self._reset_timeline = self.addAction('Reset Timeline')
00114 
00115         self._play_all = self.addAction('Play All Messages')
00116         self._play_all.setCheckable(True)
00117         self._play_all.setChecked(self.timeline.play_all)
00118 
00119         self.addSeparator()
00120 
00121         self._renderers = self.timeline._timeline_frame.get_renderers()
00122         self._thumbnail_actions = []
00123 
00124         # create thumbnail menu items
00125         if menu_topic is None:
00126             submenu = self.addMenu('Thumbnails...')
00127             self._thumbnail_show_action = submenu.addAction('Show All')
00128             self._thumbnail_hide_action = submenu.addAction('Hide All')
00129             submenu.addSeparator()
00130 
00131             for topic, renderer in self._renderers:
00132                 self._thumbnail_actions.append(submenu.addAction(topic))
00133                 self._thumbnail_actions[-1].setCheckable(True)
00134                 self._thumbnail_actions[-1].setChecked(
00135                     self.timeline._timeline_frame.is_renderer_active(topic))
00136         else:
00137             self._thumbnail_show_action = None
00138             self._thumbnail_hide_action = None
00139             for topic, renderer in self._renderers:
00140                 if menu_topic == topic:
00141                     self._thumbnail_actions.append(self.addAction("Thumbnail"))
00142                     self._thumbnail_actions[-1].setCheckable(True)
00143                     self._thumbnail_actions[-1].setChecked(
00144                         self.timeline._timeline_frame.is_renderer_active(topic))
00145 
00146         # create view menu items
00147         self._topic_actions = []
00148         self._type_actions = []
00149         if menu_topic is None:
00150             self._topics = self.timeline._timeline_frame.topics
00151             view_topics_menu = self.addMenu('View (by Topic)')
00152             for topic in self._topics:
00153                 datatype = self.timeline.get_datatype(topic)
00154 
00155                 # View... / topic
00156                 topic_menu = QMenu(topic, self)
00157                 viewer_types = self.timeline._timeline_frame.get_viewer_types(datatype)
00158 
00159                 # View... / topic / Viewer
00160                 for viewer_type in viewer_types:
00161                     tempaction = topic_menu.addAction(viewer_type.name)
00162                     tempaction.setData(viewer_type)
00163                     self._topic_actions.append(tempaction)
00164                 view_topics_menu.addMenu(topic_menu)
00165 
00166             view_type_menu = self.addMenu('View (by Type)')
00167             self._topics_by_type = self.timeline._timeline_frame._topics_by_datatype
00168             for datatype in self._topics_by_type:
00169                 # View... / datatype
00170                 datatype_menu = QMenu(datatype, self)
00171                 datatype_topics = self._topics_by_type[datatype]
00172                 viewer_types = self.timeline._timeline_frame.get_viewer_types(datatype)
00173                 for topic in [t for t in self._topics if t in datatype_topics]:   # use timeline ordering
00174                     topic_menu = QMenu(topic, datatype_menu)
00175                     # View... / datatype / topic / Viewer
00176                     for viewer_type in viewer_types:
00177                         tempaction = topic_menu.addAction(viewer_type.name)
00178                         tempaction.setData(viewer_type)
00179                         self._topic_actions.append(tempaction)
00180                     datatype_menu.addMenu(topic_menu)
00181                 view_type_menu.addMenu(datatype_menu)
00182         else:
00183             view_menu = self.addMenu("View")
00184             datatype = self.timeline.get_datatype(menu_topic)
00185 
00186             viewer_types = self.timeline._timeline_frame.get_viewer_types(datatype)
00187             for viewer_type in viewer_types:
00188                 tempaction = view_menu.addAction(viewer_type.name)
00189                 tempaction.setData(viewer_type)
00190                 self._topic_actions.append(tempaction)
00191 
00192         self.addSeparator()
00193 
00194         # create publish menu items
00195         self._publish_actions = []
00196         if menu_topic is None:
00197             submenu = self.addMenu('Publish...')
00198 
00199             self._publish_all = submenu.addAction('Publish All')
00200             self._publish_none = submenu.addAction('Publish None')
00201 
00202             submenu.addSeparator()
00203 
00204             for topic in self._topics:
00205                 self._publish_actions.append(submenu.addAction(topic))
00206                 self._publish_actions[-1].setCheckable(True)
00207                 self._publish_actions[-1].setChecked(self.timeline.is_publishing(topic))
00208         else:
00209             self._publish_actions.append(self.addAction("Publish"))
00210             self._publish_actions[-1].setCheckable(True)
00211             self._publish_actions[-1].setChecked(self.timeline.is_publishing(menu_topic))
00212             self._publish_all = None
00213             self._publish_none = None
00214 
00215         action = self.exec_(event.globalPos())
00216         if action is not None and action != 0:
00217             self.process(action)
00218 
00219     def process(self, action):
00220         """
00221         :param action: action to execute, ''QAction''
00222         :raises: when it doesn't recognice the action passed in, ''Exception''
00223         """
00224         if action == self._reset_timeline:
00225             self.timeline._timeline_frame.reset_timeline()
00226         elif action == self._play_all:
00227             self.timeline.toggle_play_all()
00228         elif action == self._publish_all:
00229             for topic in self.timeline._timeline_frame.topics:
00230                 if not self.timeline.start_publishing(topic):
00231                     break
00232         elif action == self._publish_none:
00233             for topic in self.timeline._timeline_frame.topics:
00234                 if not self.timeline.stop_publishing(topic):
00235                     break
00236         elif action == self._thumbnail_show_action:
00237             self.timeline._timeline_frame.set_renderers_active(True)
00238         elif action == self._thumbnail_hide_action:
00239             self.timeline._timeline_frame.set_renderers_active(False)
00240         elif action in self._thumbnail_actions:
00241             if self._menu_topic is None:
00242                 topic = action.text()
00243             else:
00244                 topic = self._menu_topic
00245 
00246             if self.timeline._timeline_frame.is_renderer_active(topic):
00247                 self.timeline._timeline_frame.set_renderer_active(topic, False)
00248             else:
00249                 self.timeline._timeline_frame.set_renderer_active(topic, True)
00250         elif action in self._topic_actions + self._type_actions:
00251             if self._menu_topic is None:
00252                 topic = action.parentWidget().title()
00253             else:
00254                 topic = self._menu_topic
00255 
00256             popup_name = topic + '__' + action.text()
00257             if popup_name not in self.timeline.popups:
00258                 frame = TopicPopupWidget(popup_name, self.timeline,
00259                                          action.data(), str(topic))
00260 
00261                 self.timeline.add_view(topic, frame)
00262                 self.timeline.popups[popup_name] = frame
00263 
00264             # make popup visible
00265             frame = self.timeline.popups[popup_name]
00266             frame.show(self.timeline.get_context())
00267 
00268         elif action in self._publish_actions:
00269             if self._menu_topic is None:
00270                 topic = action.text()
00271             else:
00272                 topic = self._menu_topic
00273 
00274             if self.timeline.is_publishing(topic):
00275                 self.timeline.stop_publishing(topic)
00276             else:
00277                 self.timeline.start_publishing(topic)
00278         else:
00279             raise Exception('Unknown action in TimelinePopupMenu.process')


rqt_bag
Author(s): Aaron Blasdel, Tim Field
autogenerated on Thu Jun 6 2019 18:52:48