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 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
00072 dock_features = self.parent().features()
00073 dock_features |= QDockWidget.DockWidgetClosable
00074 self.parent().setFeatures(dock_features)
00075
00076
00077 if self._viewer:
00078 self._timeline.remove_listener(self._topic, self._viewer)
00079 self._viewer = None
00080
00081
00082 while self.layout().count() > 0:
00083 item = self.layout().itemAt(0)
00084 self.layout().removeItem(item)
00085
00086
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
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
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
00156 topic_menu = QMenu(topic, self)
00157 viewer_types = self.timeline._timeline_frame.get_viewer_types(datatype)
00158
00159
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
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]:
00174 topic_menu = QMenu(topic, datatype_menu)
00175
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
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
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')