Skip to content

Commit d51a961

Browse files
Add debounce timer for x-range changes and optimize curve resampling
1 parent e78b75f commit d51a961

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

robot_log_visualizer/plotter/pyqtgraph_viewer_canvas.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def update_vline_value(self, value: float) -> None:
145145
def closeEvent(self, event): # type: ignore[override]
146146
"""Ensure timers are stopped before Qt destroys the object."""
147147
self._timer.stop()
148+
self._range_change_timer.stop()
148149
super().closeEvent(event)
149150

150151
# -------------------------------------------------------------#
@@ -181,9 +182,36 @@ def _init_ui(self) -> None:
181182
self._timer.timeout.connect(self._update_vline)
182183
self._timer.start(self._period_ms)
183184

185+
# Timer for debouncing x-range changes (zoom-adaptive resampling)
186+
self._range_change_timer = QtCore.QTimer(self)
187+
self._range_change_timer.setSingleShot(True)
188+
self._range_change_timer.setInterval(50) # ms debounce
189+
self._range_change_timer.timeout.connect(self._refresh_curves_for_viewport)
190+
184191
def _connect_signals(self) -> None:
185192
"""Wire Qt / pyqtgraph signals to slots."""
186193
self._plot.scene().sigMouseClicked.connect(self._on_mouse_click)
194+
self._plot.sigXRangeChanged.connect(lambda *_: self._range_change_timer.start())
195+
196+
def _refresh_curves_for_viewport(self) -> None:
197+
"""Resample all curves to the resolution of the current visible x window."""
198+
if not self._curve_fulldata or self._signal_provider is None:
199+
return
200+
if self._signal_provider.provider_type == ProviderType.REALTIME:
201+
return
202+
203+
x_min, x_max = self._plot.viewRange()[0]
204+
205+
for key, (x_full, y_full) in self._curve_fulldata.items():
206+
if key not in self._curves:
207+
continue
208+
mask = (x_full >= x_min) & (x_full <= x_max)
209+
if not np.any(mask):
210+
continue
211+
x_vis = x_full[mask]
212+
y_vis = y_full[mask]
213+
x_ds, y_ds = self._downsample_peak(x_vis, y_vis)
214+
self._curves[key].setData(x_ds, y_ds)
187215

188216
def _add_missing_curves(
189217
self, paths: Sequence[Path], legends: Sequence[Legend]
@@ -216,7 +244,6 @@ def _add_missing_curves(
216244
pen=pen,
217245
name="/".join(legend[1:]),
218246
symbol=None,
219-
clipToView=True,
220247
)
221248
self._curve_fulldata[key] = (x, y)
222249

0 commit comments

Comments
 (0)