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


rqt_bag
Author(s): Aaron Blasdel, Tim Field
autogenerated on Mon Oct 6 2014 07:15:30