mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
fix(crosshair): update markers if necessary
This commit is contained in:
@ -47,26 +47,19 @@ class Crosshair(QObject):
|
|||||||
self.plot_item.ctrl.logYCheck.checkStateChanged.connect(self.check_log)
|
self.plot_item.ctrl.logYCheck.checkStateChanged.connect(self.check_log)
|
||||||
|
|
||||||
# Initialize markers
|
# Initialize markers
|
||||||
self.marker_moved_1d = []
|
self.marker_moved_1d = {}
|
||||||
self.marker_clicked_1d = []
|
self.marker_clicked_1d = {}
|
||||||
self.marker_2d = None
|
self.marker_2d = None
|
||||||
self.update_markers()
|
self.update_markers()
|
||||||
|
|
||||||
def update_markers(self):
|
def update_markers(self):
|
||||||
"""Update the markers for the crosshair, creating new ones if necessary."""
|
"""Update the markers for the crosshair, creating new ones if necessary."""
|
||||||
|
|
||||||
# Clear existing markers
|
|
||||||
for marker in self.marker_moved_1d + self.marker_clicked_1d:
|
|
||||||
self.plot_item.removeItem(marker)
|
|
||||||
if self.marker_2d:
|
|
||||||
self.plot_item.removeItem(self.marker_2d)
|
|
||||||
|
|
||||||
# Create new markers
|
# Create new markers
|
||||||
self.marker_moved_1d = {}
|
|
||||||
self.marker_clicked_1d = {}
|
|
||||||
self.marker_2d = None
|
|
||||||
for item in self.plot_item.items:
|
for item in self.plot_item.items:
|
||||||
if isinstance(item, pg.PlotDataItem): # 1D plot
|
if isinstance(item, pg.PlotDataItem): # 1D plot
|
||||||
|
if item.name() in self.marker_moved_1d:
|
||||||
|
continue
|
||||||
pen = item.opts["pen"]
|
pen = item.opts["pen"]
|
||||||
color = pen.color() if hasattr(pen, "color") else pg.mkColor(pen)
|
color = pen.color() if hasattr(pen, "color") else pg.mkColor(pen)
|
||||||
marker_moved = pg.ScatterPlotItem(
|
marker_moved = pg.ScatterPlotItem(
|
||||||
@ -88,6 +81,8 @@ class Crosshair(QObject):
|
|||||||
self.plot_item.addItem(marker_clicked)
|
self.plot_item.addItem(marker_clicked)
|
||||||
|
|
||||||
elif isinstance(item, pg.ImageItem): # 2D plot
|
elif isinstance(item, pg.ImageItem): # 2D plot
|
||||||
|
if self.marker_2d is not None:
|
||||||
|
continue
|
||||||
self.marker_2d = pg.ROI(
|
self.marker_2d = pg.ROI(
|
||||||
[0, 0], size=[1, 1], pen=pg.mkPen("r", width=2), movable=False
|
[0, 0], size=[1, 1], pen=pg.mkPen("r", width=2), movable=False
|
||||||
)
|
)
|
||||||
@ -113,6 +108,8 @@ class Crosshair(QObject):
|
|||||||
if isinstance(item, pg.PlotDataItem): # 1D plot
|
if isinstance(item, pg.PlotDataItem): # 1D plot
|
||||||
name = item.name()
|
name = item.name()
|
||||||
plot_data = item._getDisplayDataset()
|
plot_data = item._getDisplayDataset()
|
||||||
|
if plot_data is None:
|
||||||
|
continue
|
||||||
x_data, y_data = plot_data.x, plot_data.y
|
x_data, y_data = plot_data.x, plot_data.y
|
||||||
if x_data is not None and y_data is not None:
|
if x_data is not None and y_data is not None:
|
||||||
if self.is_log_x:
|
if self.is_log_x:
|
||||||
@ -166,6 +163,7 @@ class Crosshair(QObject):
|
|||||||
event: The mouse moved event
|
event: The mouse moved event
|
||||||
"""
|
"""
|
||||||
pos = event[0]
|
pos = event[0]
|
||||||
|
self.update_markers()
|
||||||
if self.plot_item.vb.sceneBoundingRect().contains(pos):
|
if self.plot_item.vb.sceneBoundingRect().contains(pos):
|
||||||
mouse_point = self.plot_item.vb.mapSceneToView(pos)
|
mouse_point = self.plot_item.vb.mapSceneToView(pos)
|
||||||
self.v_line.setPos(mouse_point.x())
|
self.v_line.setPos(mouse_point.x())
|
||||||
@ -215,7 +213,7 @@ class Crosshair(QObject):
|
|||||||
# we only accept left mouse clicks
|
# we only accept left mouse clicks
|
||||||
if event.button() != Qt.MouseButton.LeftButton:
|
if event.button() != Qt.MouseButton.LeftButton:
|
||||||
return
|
return
|
||||||
|
self.update_markers()
|
||||||
if self.plot_item.vb.sceneBoundingRect().contains(event._scenePos):
|
if self.plot_item.vb.sceneBoundingRect().contains(event._scenePos):
|
||||||
mouse_point = self.plot_item.vb.mapSceneToView(event._scenePos)
|
mouse_point = self.plot_item.vb.mapSceneToView(event._scenePos)
|
||||||
x, y = mouse_point.x(), mouse_point.y()
|
x, y = mouse_point.x(), mouse_point.y()
|
||||||
@ -258,10 +256,8 @@ class Crosshair(QObject):
|
|||||||
"""Clears the markers from the plot."""
|
"""Clears the markers from the plot."""
|
||||||
for marker in self.marker_moved_1d.values():
|
for marker in self.marker_moved_1d.values():
|
||||||
marker.clear()
|
marker.clear()
|
||||||
# marker.deleteLater()
|
|
||||||
for marker in self.marker_clicked_1d.values():
|
for marker in self.marker_clicked_1d.values():
|
||||||
marker.clear()
|
marker.clear()
|
||||||
# marker.deleteLater()
|
|
||||||
|
|
||||||
def check_log(self):
|
def check_log(self):
|
||||||
"""Checks if the x or y axis is in log scale and updates the internal state accordingly."""
|
"""Checks if the x or y axis is in log scale and updates the internal state accordingly."""
|
||||||
|
@ -356,6 +356,7 @@ class BECPlotBase(BECConnector, pg.GraphicsLayout):
|
|||||||
"""Reset the plot widget."""
|
"""Reset the plot widget."""
|
||||||
if self.crosshair is not None:
|
if self.crosshair is not None:
|
||||||
self.crosshair.clear_markers()
|
self.crosshair.clear_markers()
|
||||||
|
self.crosshair.update_markers()
|
||||||
|
|
||||||
def export(self):
|
def export(self):
|
||||||
"""Show the Export Dialog of the plot widget."""
|
"""Show the Export Dialog of the plot widget."""
|
||||||
|
Reference in New Issue
Block a user