优化矩形处理逻辑,添加辅助函数以设置矩形的x坐标和宽度,支持不同类型的矩形对象
This commit is contained in:
@ -911,7 +911,7 @@ class MainWindow_SA_label(QMainWindow):
|
|||||||
end_point = start_point + self.config["WindowSignalSecond"] * plot_freq
|
end_point = start_point + self.config["WindowSignalSecond"] * plot_freq
|
||||||
for channel, ax in self.channel_to_ax.items():
|
for channel, ax in self.channel_to_ax.items():
|
||||||
if self.channel_to_best_fit_checkbox[channel] is not None and (
|
if self.channel_to_best_fit_checkbox[channel] is not None and (
|
||||||
not self.channel_to_best_fit_checkbox[channel].isChecked()):
|
not self.channel_to_best_fit_checkbox[channel].isChecked()):
|
||||||
continue
|
continue
|
||||||
signal_max = self.data.channel[channel][start_point: end_point].max()
|
signal_max = self.data.channel[channel][start_point: end_point].max()
|
||||||
signal_min = self.data.channel[channel][start_point: end_point].min()
|
signal_min = self.data.channel[channel][start_point: end_point].min()
|
||||||
@ -1055,8 +1055,8 @@ class MainWindow_SA_label(QMainWindow):
|
|||||||
|
|
||||||
new_width = max(new_width, 1) # 最小宽度为1秒
|
new_width = max(new_width, 1) # 最小宽度为1秒
|
||||||
|
|
||||||
self.selected_event_rect.set_x(new_start)
|
self.__set_x_and_width__(self.selected_event_rect, new_start, new_width)
|
||||||
self.selected_event_rect.set_width(new_width)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if self.press is not None and self.press[1] == 'empty':
|
if self.press is not None and self.press[1] == 'empty':
|
||||||
@ -1255,8 +1255,7 @@ class MainWindow_SA_label(QMainWindow):
|
|||||||
event_right = self.selected_event_info["correct_End"] if self.selected_event_info["isLabeled"] != -1 else \
|
event_right = self.selected_event_info["correct_End"] if self.selected_event_info["isLabeled"] != -1 else \
|
||||||
self.selected_event_info["End"]
|
self.selected_event_info["End"]
|
||||||
if x_rect != event_left or width != (event_right - event_left):
|
if x_rect != event_left or width != (event_right - event_left):
|
||||||
self.selected_event_rect.set_x(event_left)
|
self.__set_x_and_width__(self.selected_event_rect, event_left, event_right - event_left)
|
||||||
self.selected_event_rect.set_width(event_right - event_left)
|
|
||||||
self.canvas.draw_idle()
|
self.canvas.draw_idle()
|
||||||
|
|
||||||
@overrides
|
@overrides
|
||||||
@ -2005,7 +2004,7 @@ class MainWindow_SA_label(QMainWindow):
|
|||||||
def __get_x_and_width__(self, event_rect):
|
def __get_x_and_width__(self, event_rect):
|
||||||
"""Get the width of a rectangle in data coordinates."""
|
"""Get the width of a rectangle in data coordinates."""
|
||||||
# matplotlib >=3.9 axvspan 返回的是一个Rectangle对象
|
# matplotlib >=3.9 axvspan 返回的是一个Rectangle对象
|
||||||
# matplorlib < 3.9 axvspan 返回的是一个PolyCollection对象
|
# matplorlib < 3.9 axvspan 返回的是一个Poly对象
|
||||||
if isinstance(event_rect, Rectangle):
|
if isinstance(event_rect, Rectangle):
|
||||||
x = event_rect.get_x()
|
x = event_rect.get_x()
|
||||||
width = event_rect.get_width()
|
width = event_rect.get_width()
|
||||||
@ -2017,6 +2016,23 @@ class MainWindow_SA_label(QMainWindow):
|
|||||||
else:
|
else:
|
||||||
raise TypeError("Unsupported event_rect type")
|
raise TypeError("Unsupported event_rect type")
|
||||||
return x, width
|
return x, width
|
||||||
|
|
||||||
|
def __set_x_and_width__(self, event_rect, x, width):
|
||||||
|
"""Set the x and width of a rectangle in data coordinates."""
|
||||||
|
if isinstance(event_rect, Rectangle):
|
||||||
|
event_rect.set_x(x)
|
||||||
|
event_rect.set_width(width)
|
||||||
|
elif isinstance(event_rect, Polygon):
|
||||||
|
verts = event_rect.get_xy()
|
||||||
|
verts[0, 0] = x
|
||||||
|
verts[1, 0] = x
|
||||||
|
verts[2, 0] = x + width
|
||||||
|
verts[3, 0] = x + width
|
||||||
|
event_rect.set_xy(verts)
|
||||||
|
else:
|
||||||
|
raise TypeError("Unsupported event_rect type")
|
||||||
|
|
||||||
|
|
||||||
class CustomNavigationToolbar(NavigationToolbar2QT):
|
class CustomNavigationToolbar(NavigationToolbar2QT):
|
||||||
def __init__(self, canvas, parent):
|
def __init__(self, canvas, parent):
|
||||||
super().__init__(canvas, parent)
|
super().__init__(canvas, parent)
|
||||||
|
|||||||
Reference in New Issue
Block a user