优化矩形处理逻辑,添加辅助函数以设置矩形的x坐标和宽度,支持不同类型的矩形对象

This commit is contained in:
marques
2025-09-04 15:45:12 +08:00
parent 2a0a62d201
commit 4abd6168cd

View File

@ -911,7 +911,7 @@ class MainWindow_SA_label(QMainWindow):
end_point = start_point + self.config["WindowSignalSecond"] * plot_freq
for channel, ax in self.channel_to_ax.items():
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
signal_max = self.data.channel[channel][start_point: end_point].max()
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秒
self.selected_event_rect.set_x(new_start)
self.selected_event_rect.set_width(new_width)
self.__set_x_and_width__(self.selected_event_rect, new_start, new_width)
else:
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 \
self.selected_event_info["End"]
if x_rect != event_left or width != (event_right - event_left):
self.selected_event_rect.set_x(event_left)
self.selected_event_rect.set_width(event_right - event_left)
self.__set_x_and_width__(self.selected_event_rect, event_left, event_right - event_left)
self.canvas.draw_idle()
@overrides
@ -2005,7 +2004,7 @@ class MainWindow_SA_label(QMainWindow):
def __get_x_and_width__(self, event_rect):
"""Get the width of a rectangle in data coordinates."""
# matplotlib >=3.9 axvspan 返回的是一个Rectangle对象
# matplorlib < 3.9 axvspan 返回的是一个PolyCollection对象
# matplorlib < 3.9 axvspan 返回的是一个Poly对象
if isinstance(event_rect, Rectangle):
x = event_rect.get_x()
width = event_rect.get_width()
@ -2017,6 +2016,23 @@ class MainWindow_SA_label(QMainWindow):
else:
raise TypeError("Unsupported event_rect type")
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):
def __init__(self, canvas, parent):
super().__init__(canvas, parent)