Skip to content

Commit

Permalink
Plots: Save scrollbars positions and restore them after loading a plot
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed May 13, 2024
1 parent e7f0935 commit 5a1c3f4
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions spyder/plugins/plots/widgets/figurebrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ def __init__(self, parent=None, background_color=None):
# An internal flag that tracks when the figure is being panned.
self._ispanning = False

# To save scrollbar values in the current thumbnail
self.verticalScrollBar().valueChanged.connect(
self._set_vscrollbar_value
)
self.horizontalScrollBar().valueChanged.connect(
self._set_hscrollbar_value
)

@property
def auto_fit_plotting(self):
"""
Expand Down Expand Up @@ -468,6 +476,20 @@ def load_figure(self, fig, fmt):
if self.current_thumbnail.scalefactor is None:
self.current_thumbnail.scalefactor = self.scalefactor

# Restore scrollbar values
QTimer.singleShot(
20,
lambda: self.verticalScrollBar().setValue(
self.current_thumbnail.vscrollbar_value
),
)
QTimer.singleShot(
20,
lambda: self.horizontalScrollBar().setValue(
self.current_thumbnail.hscrollbar_value
),
)

def eventFilter(self, widget, event):
"""A filter to control the zooming and panning of the figure canvas."""

Expand Down Expand Up @@ -635,6 +657,16 @@ def _adjust_scrollbar(self, f):
vb = self.verticalScrollBar()
vb.setValue(int(f * vb.value() + ((f - 1) * vb.pageStep()/2)))

def _set_vscrollbar_value(self, value):
"""Save vertical scrollbar value in current thumbnail."""
if self.current_thumbnail is not None:
self.current_thumbnail.vscrollbar_value = value

def _set_hscrollbar_value(self, value):
"""Save horizontal scrollbar value in current thumbnail."""
if self.current_thumbnail is not None:
self.current_thumbnail.hscrollbar_value = value


class ThumbnailScrollBar(QFrame):
"""
Expand Down Expand Up @@ -1166,6 +1198,8 @@ def __init__(self, parent=None, background_color=None, auto_fit=True):

self.auto_fit = auto_fit
self.scalefactor = None
self.vscrollbar_value = 0
self.hscrollbar_value = 0

self.canvas = FigureCanvas(
parent=self,
Expand Down

0 comments on commit 5a1c3f4

Please sign in to comment.