def mouseMoveEvent(self, mouse_event): if self._last_pan_point is not None: delta_scene = self.mapToScene(mouse_event.pos()) - self.mapToScene(self._last_pan_point) if not delta_scene.isNull(): self.centerOn(self._last_scene_center - delta_scene) self._last_scene_center -= delta_scene self._last_pan_point = mouse_event.pos() QGraphicsView.mouseMoveEvent(self, mouse_event)
def graphics_view_on_key_press(self, event): key = event.key() if key in (Qt.Key_Left, Qt.Key_Right, Qt.Key_Up, Qt.Key_Down, Qt.Key_PageUp, Qt.Key_PageDown): # This causes the graphics view to ignore these keys so they can be caught by the bag_widget keyPressEvent event.ignore() else: # Maintains functionality for all other keys QGraphicsView implements QGraphicsView.keyPressEvent(self.graphics_view, event)
def mouseMoveEvent(self, mouse_event): if self._last_pan_point is not None: delta_scene = self.mapToScene(mouse_event.pos()) - self.mapToScene( self._last_pan_point) if not delta_scene.isNull(): self.centerOn(self._last_scene_center - delta_scene) self._last_scene_center -= delta_scene self._last_pan_point = mouse_event.pos() QGraphicsView.mouseMoveEvent(self, mouse_event)
def wheelEvent(self, wheel_event): if wheel_event.modifiers() == Qt.NoModifier: delta = wheel_event.delta() delta = max(min(delta, 480), -480) mouse_before_scale_in_scene = self.mapToScene(wheel_event.pos()) scale_factor = 1 + (0.2 * (delta / 120.0)) scaling = QTransform(scale_factor, 0, 0, scale_factor, 0, 0) self.setTransform(self.transform() * scaling) mouse_after_scale_in_scene = self.mapToScene(wheel_event.pos()) center_in_scene = self.mapToScene(self.frameRect().center()) self.centerOn(center_in_scene + mouse_before_scale_in_scene - mouse_after_scale_in_scene) wheel_event.accept() else: QGraphicsView.wheelEvent(self, wheel_event)
def __init__(self, timeline, parent): super(ImageView, self).__init__(timeline, parent) self._image = None self._image_topic = None self._image_stamp = None self.quality = Image.NEAREST # quality hint for scaling # TODO put the image_topic and image_stamp on the picture or display them in some fashion self._overlay_font_size = 14.0 self._overlay_indent = (4, 4) self._overlay_color = (0.2, 0.2, 1.0) self._image_view = QGraphicsView(parent) self._image_view.resizeEvent = self._resizeEvent self._scene = QGraphicsScene() self._image_view.setScene(self._scene) parent.layout().addWidget(self._image_view)
def wheelEvent(self, wheel_event): if wheel_event.modifiers() == Qt.NoModifier: num_degrees = wheel_event.delta() / 8.0 num_steps = num_degrees / 15.0 mouse_before_scale_in_scene = self.mapToScene(wheel_event.pos()) scale_factor = 1.2 * num_steps if num_steps < 0: scale_factor = -1.0 / scale_factor scaling = QTransform(scale_factor, 0, 0, scale_factor, 0, 0) self.setTransform(self.transform() * scaling) mouse_after_scale_in_scene = self.mapToScene(wheel_event.pos()) center_in_scene = self.mapToScene(self.frameRect().center()) self.centerOn(center_in_scene + mouse_before_scale_in_scene - mouse_after_scale_in_scene) wheel_event.accept() else: QGraphicsView.wheelEvent(self, wheel_event)
def __init__(self, timeline, parent, topic): super(ImageView, self).__init__(timeline, parent, topic) self._image = None self._image_topic = None self._image_stamp = None self.quality = Image.NEAREST # quality hint for scaling # TODO put the image_topic and image_stamp on the picture or display them in some fashion self._overlay_font_size = 14.0 self._overlay_indent = (4, 4) self._overlay_color = (0.2, 0.2, 1.0) self._image_view = QGraphicsView(parent) self._image_view.resizeEvent = self._resizeEvent self._scene = QGraphicsScene() self._image_view.setScene(self._scene) parent.layout().addWidget(self._image_view)
class ImageView(TopicMessageView): """ Popup image viewer """ name = 'Image' def __init__(self, timeline, parent): super(ImageView, self).__init__(timeline, parent) self._image = None self._image_topic = None self._image_stamp = None self.quality = Image.NEAREST # quality hint for scaling # TODO put the image_topic and image_stamp on the picture or display them in some fashion self._overlay_font_size = 14.0 self._overlay_indent = (4, 4) self._overlay_color = (0.2, 0.2, 1.0) self._image_view = QGraphicsView(parent) self._image_view.resizeEvent = self._resizeEvent self._scene = QGraphicsScene() self._image_view.setScene(self._scene) parent.layout().addWidget(self._image_view) # MessageView implementation def _resizeEvent(self, event): # TODO make this smarter. currently there will be no scrollbar even if the timeline extends beyond the viewable area self._scene.setSceneRect(0, 0, self._image_view.size().width() - 2, self._image_view.size().height() - 2) self.put_image_into_scene() def message_viewed(self, bag, msg_details): """ refreshes the image """ TopicMessageView.message_viewed(self, bag, msg_details) topic, msg, t = msg_details[:3] if not msg: self.set_image(None, topic, stamp) else: self.set_image(msg, topic, msg.header.stamp) def message_cleared(self): TopicMessageView.message_cleared(self) self.set_image(None, None, None) # End MessageView implementation def put_image_into_scene(self): if self._image: resized_image = self._image.resize( (self._image_view.size().width() - 2, self._image_view.size().height() - 2), self.quality) QtImage = ImageQt.ImageQt(resized_image) pixmap = QPixmap.fromImage(QtImage) self._scene.clear() self._scene.addPixmap(pixmap) def set_image(self, image_msg, image_topic, image_stamp): self._image_msg = image_msg if image_msg: self._image = image_helper.imgmsg_to_pil(image_msg) else: self._image = None self._image_topic = image_topic self._image_stamp = image_stamp self.put_image_into_scene()
def mousePressEvent(self, mouse_event): self._last_pan_point = mouse_event.pos() self._last_scene_center = self._map_to_scene_f(QRectF(self.frameRect()).center()) self.setCursor(Qt.ClosedHandCursor) QGraphicsView.mousePressEvent(self, mouse_event)
class ImageView(TopicMessageView): """ Popup image viewer """ name = 'Image' def __init__(self, timeline, parent, topic): super(ImageView, self).__init__(timeline, parent, topic) self._image = None self._image_topic = None self._image_stamp = None self.quality = Image.NEAREST # quality hint for scaling # TODO put the image_topic and image_stamp on the picture or display them in some fashion self._overlay_font_size = 14.0 self._overlay_indent = (4, 4) self._overlay_color = (0.2, 0.2, 1.0) self._image_view = QGraphicsView(parent) self._image_view.resizeEvent = self._resizeEvent self._scene = QGraphicsScene() self._image_view.setScene(self._scene) parent.layout().addWidget(self._image_view) # MessageView implementation def _resizeEvent(self, event): # TODO make this smarter. currently there will be no scrollbar even if the timeline extends beyond the viewable area self._scene.setSceneRect(0, 0, self._image_view.size().width() - 2, self._image_view.size().height() - 2) self.put_image_into_scene() def message_viewed(self, bag, msg_details): """ refreshes the image """ TopicMessageView.message_viewed(self, bag, msg_details) topic, msg, t = msg_details[:3] if not msg: self.set_image(None, topic, 'no message') else: self.set_image(msg, topic, msg.header.stamp) def message_cleared(self): TopicMessageView.message_cleared(self) self.set_image(None, None, None) # End MessageView implementation def put_image_into_scene(self): if self._image: resized_image = self._image.resize((self._image_view.size().width() - 2, self._image_view.size().height() - 2), self.quality) QtImage = ImageQt(resized_image) pixmap = QPixmap.fromImage(QtImage) self._scene.clear() self._scene.addPixmap(pixmap) def set_image(self, image_msg, image_topic, image_stamp): self._image_msg = image_msg if image_msg: self._image = image_helper.imgmsg_to_pil(image_msg) else: self._image = None self._image_topic = image_topic self._image_stamp = image_stamp self.put_image_into_scene()
def mousePressEvent(self, mouse_event): self._last_pan_point = mouse_event.pos() self._last_scene_center = self._map_to_scene_f( QRectF(self.frameRect()).center()) self.setCursor(Qt.ClosedHandCursor) QGraphicsView.mousePressEvent(self, mouse_event)