def plot_data(self, data): if self.scene_manager is None: self.scene_manager = SceneManager(self) self.scene_manager.plot_data = data self.scene_manager.init_scene() self.setScene(self.scene_manager.scene) self.scene_manager.show_full_scene()
def plot_data(self, data): self.horizontalScrollBar().blockSignals(True) self.scene_creator = SceneManager(self) self.scene_creator.plot_data = data self.scene_creator.init_scene() self.setScene(self.scene_creator.scene) self.scene_creator.show_full_scene() self.update()
def plot_data(self, data): if self.scene_manager is None: self.scene_manager = SceneManager(self) self.scene_manager.minimum = self.scene_y_min self.scene_manager.maximum = self.scene_y_max self.scene_manager.plot_data = data self.scene_manager.init_scene() self.setScene(self.scene_manager.scene) self.scene_manager.show_full_scene()
def __init__(self): dir_name = os.path.dirname(os.readlink(__file__)) if os.path.islink( __file__) else os.path.dirname(__file__) self.dialog_ui = uic.loadUi( os.path.realpath(os.path.join( dir_name, "insert_sine_dialog.ui"))) # type: QDialog self.dialog_ui.setModal(True) self.complex_wave = None self.__amplitude = 0.5 self.__frequency = 10 self.__phase = 0 self.__sample_rate = 1e6 self.__num_samples = int(1e6) self.original_data = None self.draw_data = None self.position = 0 self.dialog_ui.doubleSpinBoxAmplitude.setValue(self.__amplitude) self.dialog_ui.doubleSpinBoxFrequency.setValue(self.__frequency) self.dialog_ui.doubleSpinBoxPhase.setValue(self.__phase) self.dialog_ui.doubleSpinBoxSampleRate.setValue(self.__sample_rate) self.dialog_ui.doubleSpinBoxNSamples.setValue(self.__num_samples) self.dialog_ui.lineEditTime.setValidator( QRegExpValidator( QRegExp("[0-9]+([nmµ]*|([\.,][0-9]{1,3}[nmµ]*))?$"))) scene_manager = SceneManager(self.dialog_ui.graphicsViewSineWave) self.dialog_ui.graphicsViewSineWave.scene_manager = scene_manager self.insert_indicator = scene_manager.scene.addRect( 0, -2, 0, 4, QPen(QColor(Qt.transparent), Qt.FlatCap), QBrush(self.INSERT_INDICATOR_COLOR)) self.insert_indicator.stackBefore(scene_manager.scene.selection_area) self.set_time() super().__init__(name="InsertSine")
def dialog_ui(self) -> QDialog: if self.__dialog_ui is None: dir_name = os.path.dirname(os.readlink(__file__)) if os.path.islink(__file__) else os.path.dirname(__file__) self.__dialog_ui = uic.loadUi(os.path.realpath(os.path.join(dir_name, "insert_sine_dialog.ui"))) self.__dialog_ui.setAttribute(Qt.WA_DeleteOnClose) self.__dialog_ui.setModal(True) self.__dialog_ui.doubleSpinBoxAmplitude.setValue(self.__amplitude) self.__dialog_ui.doubleSpinBoxFrequency.setValue(self.__frequency) self.__dialog_ui.doubleSpinBoxPhase.setValue(self.__phase) self.__dialog_ui.doubleSpinBoxSampleRate.setValue(self.__sample_rate) self.__dialog_ui.doubleSpinBoxNSamples.setValue(self.__num_samples) self.__dialog_ui.lineEditTime.setValidator( QRegExpValidator(QRegExp("[0-9]+([nmµ]*|([\.,][0-9]{1,3}[nmµ]*))?$"))) scene_manager = SceneManager(self.dialog_ui.graphicsViewSineWave) self.__dialog_ui.graphicsViewSineWave.scene_manager = scene_manager self.insert_indicator = scene_manager.scene.addRect(0, -2, 0, 4, QPen(QColor(Qt.transparent), Qt.FlatCap), QBrush(self.INSERT_INDICATOR_COLOR)) self.insert_indicator.stackBefore(scene_manager.scene.selection_area) self.set_time() return self.__dialog_ui
class ZoomableGraphicView(SelectableGraphicView): zoomed = pyqtSignal(float) def __init__(self, parent=None): super().__init__(parent) self.margin = 0.25 self.min_width = 100 self.max_width = "auto" self.redraw_timer = QTimer() self.redraw_timer.setSingleShot(True) self.redraw_timer.timeout.connect(self.redraw_view) self.zoomed.connect(self.on_signal_zoomed) self.horizontalScrollBar().valueChanged.connect( self.on_signal_scrolled) @property def y_center(self): if not hasattr(self, "scene_type") or self.scene_type == 0: # Normal scene return 0 else: return -self.signal.qad_center @property def scene_type(self): return 0 # gets overwritten in Epic Graphic View def zoom(self, factor, suppress_signal=False, event: QWheelEvent = None): if factor > 1 and self.view_rect().width() / factor < 300: factor = self.view_rect().width() / 300 old_pos = self.mapToScene(event.pos()) if event is not None else None if self.view_rect().width() / factor > self.sceneRect().width(): self.show_full_scene() factor = 1 self.scale(factor, 1) if not suppress_signal: self.zoomed.emit(factor) if event: move = self.mapToScene(event.pos()) - old_pos self.translate(move.x(), 0) else: self.centerOn(self.view_rect().x() + self.view_rect().width() / 2, self.y_center) def wheelEvent(self, event: QWheelEvent): zoom_factor = 1.001**event.angleDelta().y() self.zoom(zoom_factor, event=event) def mousePressEvent(self, event: QMouseEvent): super().mousePressEvent(event) if self.ctrl_mode and event.buttons() == Qt.LeftButton: self.zoom(1.1) elif self.ctrl_mode and event.buttons() == Qt.RightButton: self.zoom(0.9) def resizeEvent(self, event): if self.sceneRect().width() == 0: return if self.view_rect().width() > self.sceneRect().width(): x_factor = self.width() / self.sceneRect().width() self.scale(x_factor / self.transform().m11(), 1) self.auto_fit_view() def auto_fit_view(self): h_tar = self.sceneRect().height() h_view = self.view_rect().height() if abs(h_tar) > 0: self.scale(1, h_view / h_tar) self.centerOn(self.view_rect().x() + self.view_rect().width() / 2, self.y_center) def show_full_scene(self, reinitialize=False): y_factor = self.transform().m22() self.resetTransform() x_factor = self.width() / self.sceneRect().width() if self.sceneRect( ).width() else 1 self.scale(x_factor, y_factor) self.centerOn(0, self.y_center) self.redraw_view(reinitialize) def setScene(self, scene: QGraphicsScene): super().setScene(scene) self.margin = 0.25 * self.scene().height() def plot_data(self, data): if self.scene_manager is None: self.scene_manager = SceneManager(self) self.scene_manager.plot_data = data self.scene_manager.init_scene() self.setScene(self.scene_manager.scene) self.scene_manager.show_full_scene() def redraw_view(self, reinitialize=False): if self.scene_manager is not None: self.scene_manager.scene_type = self.scene_type if reinitialize: self.scene_manager.init_scene() vr = self.view_rect() start, end = vr.x(), vr.x() + vr.width() self.scene_manager.show_scene_section( start, end, *self._get_sub_path_ranges_and_colors(start, end)) def _get_sub_path_ranges_and_colors(self, start: float, end: float): # Overwritten in Epic Graphic View return None, None @pyqtSlot() def on_signal_zoomed(self): self.redraw_timer.start(30) @pyqtSlot() def on_signal_scrolled(self): self.redraw_timer.start(0)
class ZoomableGraphicView(SelectableGraphicView): zoomed = pyqtSignal(float) def __init__(self, parent=None): super().__init__(parent) self.zoom_in_action = QAction(self.tr("Zoom in"), self) self.zoom_in_action.setShortcut(QKeySequence.ZoomIn) self.zoom_in_action.triggered.connect(self.on_zoom_in_action_triggered) self.zoom_in_action.setShortcutContext(Qt.WidgetWithChildrenShortcut) self.zoom_in_action.setIcon(QIcon.fromTheme("zoom-in")) self.addAction(self.zoom_in_action) self.zoom_out_action = QAction(self.tr("Zoom out"), self) self.zoom_out_action.setShortcut(QKeySequence.ZoomOut) self.zoom_out_action.triggered.connect( self.on_zoom_out_action_triggered) self.zoom_out_action.setShortcutContext(Qt.WidgetWithChildrenShortcut) self.zoom_out_action.setIcon(QIcon.fromTheme("zoom-out")) self.addAction(self.zoom_out_action) self.margin = 0.25 self.min_width = 100 self.max_width = "auto" self.redraw_timer = QTimer() self.redraw_timer.setSingleShot(True) self.redraw_timer.timeout.connect(self.redraw_view) self.zoomed.connect(self.on_signal_zoomed) @property def y_center(self): if not hasattr(self, "scene_type") or self.scene_type == 0: # Normal scene return 0 else: return -self.signal.qad_center @property def scene_type(self): return 0 # gets overwritten in Epic Graphic View def scrollContentsBy(self, dx: int, dy: int): try: super().scrollContentsBy(dx, dy) self.redraw_timer.start(0) except RuntimeError as e: logger.warning("Graphic View already closed: " + str(e)) def zoom(self, factor, zoom_to_mouse_cursor=True, cursor_pos=None): if factor > 1 and self.view_rect().width() / factor < 300: factor = self.view_rect().width() / 300 if zoom_to_mouse_cursor: pos = self.mapFromGlobal( QCursor.pos()) if cursor_pos is None else cursor_pos else: pos = None old_pos = self.mapToScene(pos) if pos is not None else None if self.view_rect().width() / factor > self.sceneRect().width(): self.show_full_scene() factor = 1 self.scale(factor, 1) self.zoomed.emit(factor) if pos is not None: move = self.mapToScene(pos) - old_pos self.translate(move.x(), 0) def wheelEvent(self, event: QWheelEvent): zoom_factor = 1.001**event.angleDelta().y() self.zoom(zoom_factor, cursor_pos=event.pos()) def resizeEvent(self, event): if self.sceneRect().width() == 0: return if self.view_rect().width() > self.sceneRect().width(): x_factor = self.width() / self.sceneRect().width() self.scale(x_factor / self.transform().m11(), 1) self.auto_fit_view() def auto_fit_view(self): h_tar = self.sceneRect().height() h_view = self.view_rect().height() if abs(h_tar) > 0: self.scale(1, h_view / h_tar) self.centerOn(self.view_rect().x() + self.view_rect().width() / 2, self.y_center) def show_full_scene(self, reinitialize=False): y_factor = self.transform().m22() self.resetTransform() x_factor = self.width() / self.sceneRect().width() if self.sceneRect( ).width() else 1 self.scale(x_factor, y_factor) self.centerOn(0, self.y_center) self.redraw_view(reinitialize) def zoom_to_selection(self, start: int, end: int): if start == end: return x_factor = self.view_rect().width() / (end - start) self.zoom(x_factor, zoom_to_mouse_cursor=False) self.centerOn(start + (end - start) / 2, self.y_center) def setScene(self, scene: QGraphicsScene): super().setScene(scene) if self.scene() is not None: self.margin = 0.25 * self.scene().height() def plot_data(self, data): if self.scene_manager is None: self.scene_manager = SceneManager(self) self.scene_manager.plot_data = data self.scene_manager.init_scene() self.setScene(self.scene_manager.scene) self.scene_manager.show_full_scene() def redraw_view(self, reinitialize=False): if self.scene_manager is not None: self.scene_manager.scene_type = self.scene_type if reinitialize: self.scene_manager.init_scene() vr = self.view_rect() start, end = vr.x(), vr.x() + vr.width() self.scene_manager.show_scene_section( start, end, *self._get_sub_path_ranges_and_colors(start, end)) def _get_sub_path_ranges_and_colors(self, start: float, end: float): # Overwritten in Epic Graphic View return None, None def eliminate(self): self.redraw_timer.stop() super().eliminate() @pyqtSlot() def on_signal_zoomed(self): self.redraw_timer.start(30) @pyqtSlot() def on_zoom_in_action_triggered(self): self.zoom(1.1) @pyqtSlot() def on_zoom_out_action_triggered(self): self.zoom(0.9)
class ZoomableGraphicView(SelectableGraphicView): zoomed = pyqtSignal(float) def __init__(self, parent=None): super().__init__(parent) self.margin = 0.25 self.min_width = 100 self.max_width = "auto" self.zoomed.connect(self.handle_signal_zoomed_or_scrolled) self.horizontalScrollBar().valueChanged.connect( self.handle_signal_zoomed_or_scrolled) def wheelEvent(self, event: QWheelEvent): delta = event.angleDelta().y() zoom_factor = 1.001**delta p0scene = self.mapToScene(event.pos()) w = self.view_rect().width() zooming_in = zoom_factor > 1 if zooming_in and w / zoom_factor < self.min_width: return max_width = self.max_width if self.max_width == "auto": max_width = self.sceneRect().width() if not zooming_in and w / zoom_factor >= max_width and max_width: zoom_factor = w / max_width self.scale(zoom_factor, 1) p1mouse = self.mapFromScene(p0scene) move = p1mouse - event.pos() self.horizontalScrollBar().setValue(move.x() + self.horizontalScrollBar().value()) self.zoomed.emit(zoom_factor) def update(self, *__args): super().update(*__args) vr = self.view_rect() self.fitInView(vr.x(), self.sceneRect().y() - self.margin, vr.width(), self.sceneRect().height() + self.margin) self.horizontalScrollBar().blockSignals(False) def draw_full(self): rect = self.sceneRect() self.fitInView(rect.x(), rect.y() - self.margin, rect.width(), rect.height() + self.margin) def setScene(self, scene: QGraphicsScene): super().setScene(scene) self.margin = 0.25 * self.scene().height() def plot_data(self, data): self.horizontalScrollBar().blockSignals(True) self.scene_creator = SceneManager(self) self.scene_creator.plot_data = data self.scene_creator.init_scene() self.setScene(self.scene_creator.scene) self.scene_creator.show_full_scene() self.update() def handle_signal_zoomed_or_scrolled(self): if self.scene_creator is not None: x1 = self.view_rect().x() x2 = x1 + self.view_rect().width() self.scene_creator.show_scene_section(x1, x2)
class ZoomableGraphicView(SelectableGraphicView): # argument is x zoom factor # if argument is -1, then show_full_scene was triggered during zoom zoomed = pyqtSignal(float) def __init__(self, parent=None): super().__init__(parent) self.zoom_in_action = QAction(self.tr("Zoom in"), self) self.zoom_in_action.setShortcut(QKeySequence.ZoomIn) self.zoom_in_action.triggered.connect(self.on_zoom_in_action_triggered) self.zoom_in_action.setShortcutContext(Qt.WidgetWithChildrenShortcut) self.zoom_in_action.setIcon(QIcon.fromTheme("zoom-in")) self.addAction(self.zoom_in_action) self.zoom_out_action = QAction(self.tr("Zoom out"), self) self.zoom_out_action.setShortcut(QKeySequence.ZoomOut) self.zoom_out_action.triggered.connect(self.on_zoom_out_action_triggered) self.zoom_out_action.setShortcutContext(Qt.WidgetWithChildrenShortcut) self.zoom_out_action.setIcon(QIcon.fromTheme("zoom-out")) self.addAction(self.zoom_out_action) self.redraw_timer = QTimer() self.redraw_timer.setSingleShot(True) self.redraw_timer.timeout.connect(self.redraw_view) self.zoomed.connect(self.on_signal_zoomed) self.scene_y_min = float("nan") # NaN = AutoDetect self.scene_y_max = float("nan") # NaN = AutoDetect self.scene_x_zoom_stretch = 1 @property def y_center(self): if not hasattr(self, "scene_type") or self.scene_type == 0: # Normal scene return 0 else: return -self.signal.qad_center @property def scene_type(self): return 0 # gets overwritten in Epic Graphic View def scrollContentsBy(self, dx: int, dy: int): try: super().scrollContentsBy(dx, dy) self.redraw_timer.start(0) except RuntimeError as e: logger.warning("Graphic View already closed: " + str(e)) def zoom(self, factor, zoom_to_mouse_cursor=True, cursor_pos=None): if factor > 1 and self.view_rect().width() / factor < 300: factor = self.view_rect().width() / 300 if zoom_to_mouse_cursor: pos = self.mapFromGlobal(QCursor.pos()) if cursor_pos is None else cursor_pos else: pos = None old_pos = self.mapToScene(pos) if pos is not None else None show_full = False if self.view_rect().width() / factor > self.sceneRect().width(): self.show_full_scene() factor = 1 show_full = True self.scale(factor, 1) if show_full: self.zoomed.emit(-1) else: self.zoomed.emit(factor) if pos is not None: move = self.mapToScene(pos) - old_pos self.translate(move.x(), 0) def wheelEvent(self, event: QWheelEvent): zoom_factor = 1.001 ** event.angleDelta().y() self.zoom(zoom_factor, cursor_pos=event.pos()) def resizeEvent(self, event): if self.sceneRect().width() == 0: return self.auto_fit_view() def auto_fit_view(self): h_tar = self.sceneRect().height() h_view = self.view_rect().height() if abs(h_tar) > 0: self.scale(1, h_view / h_tar) self.centerOn(self.view_rect().x() + self.view_rect().width() / 2, self.y_center) def show_full_scene(self, reinitialize=False): y_factor = self.transform().m22() self.resetTransform() # Use full self.width() here to enable show_full_scene when view_rect not yet set e.g. in Record Signal Dialog x_factor = self.width() / (self.sceneRect().width() * self.scene_x_zoom_stretch) if self.sceneRect().width() else 1 self.scale(x_factor, y_factor) self.centerOn(0, self.y_center) self.redraw_view(reinitialize) def zoom_to_selection(self, start: int, end: int): if start == end: return x_factor = self.view_rect().width() / (end - start) self.zoom(x_factor, zoom_to_mouse_cursor=False) self.centerOn(start + (end - start) / 2, self.y_center) def plot_data(self, data): if self.scene_manager is None: self.scene_manager = SceneManager(self) self.scene_manager.minimum = self.scene_y_min self.scene_manager.maximum = self.scene_y_max self.scene_manager.plot_data = data self.scene_manager.init_scene() self.setScene(self.scene_manager.scene) self.scene_manager.show_full_scene() def redraw_view(self, reinitialize=False): if self.scene_manager is not None: self.scene_manager.scene_type = self.scene_type if reinitialize: self.scene_manager.init_scene() vr = self.view_rect() start, end = vr.x(), vr.x() + vr.width() self.scene_manager.show_scene_section(start, end, *self._get_sub_path_ranges_and_colors(start, end)) def _get_sub_path_ranges_and_colors(self, start: float, end: float): # Overwritten in Epic Graphic View return None, None def eliminate(self): self.redraw_timer.stop() super().eliminate() @pyqtSlot() def on_signal_zoomed(self): self.redraw_timer.start(30) @pyqtSlot() def on_zoom_in_action_triggered(self): self.zoom(1.1) @pyqtSlot() def on_zoom_out_action_triggered(self): self.zoom(0.9)