Skip to content

Commit 11c6565

Browse files
committed
Integrated DataLab's SyncPlotWindow
1 parent 123125c commit 11c6565

3 files changed

Lines changed: 55 additions & 61 deletions

File tree

plotpy/plot/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
PlotWidget,
1010
PlotWindow,
1111
SubplotWidget,
12+
SyncPlotWindow,
1213
set_widget_title_icon,
1314
)

plotpy/plot/plotwidget.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
if TYPE_CHECKING: # pragma: no cover
2323
from plotpy.panels.base import PanelWidget
24-
from plotpy.styles import GridParam
2524

2625

2726
def configure_plot_splitter(qsplit: QW.QSplitter, decreasing_size: bool = True) -> None:
@@ -161,8 +160,7 @@ def __set_image_layout(self) -> None:
161160

162161
# Avoid circular import
163162
# pylint: disable=import-outside-toplevel
164-
from plotpy.panels.csection.cswidget import (XCrossSection,
165-
YCrossSection)
163+
from plotpy.panels.csection.cswidget import XCrossSection, YCrossSection
166164

167165
self.ycsw = YCrossSection(
168166
self,
@@ -811,4 +809,54 @@ def add_plot(
811809
self.manager.add_plot(plot, plot_id)
812810

813811

814-
# TODO: Migrate DataLab's SyncPlotWindow right here
812+
class SyncPlotWindow(QW.QMainWindow):
813+
"""Window for showing plots, optionally synchronized"""
814+
815+
def __init__(self, parent=None, title=None, options=None):
816+
super().__init__(parent)
817+
title = self.__doc__ if title is None else title
818+
set_widget_title_icon(self, title, "plotpy.svg")
819+
self.manager = PlotManager(None)
820+
self.manager.set_main(self)
821+
self.subplotwidget = SubplotWidget(self.manager, parent=self, options=options)
822+
self.setCentralWidget(self.subplotwidget)
823+
toolbar = QW.QToolBar(_("Tools"), self)
824+
self.manager.add_toolbar(toolbar, "default")
825+
toolbar.setMovable(True)
826+
toolbar.setFloatable(True)
827+
self.addToolBar(toolbar)
828+
829+
def finalize_configuration(self):
830+
"""Configure plot manager and register all tools"""
831+
self.subplotwidget.add_panels_to_manager()
832+
self.subplotwidget.register_tools()
833+
834+
def rescale_plots(self):
835+
"""Rescale all plots"""
836+
QW.QApplication.instance().processEvents()
837+
for plot in self.subplotwidget.plots:
838+
plot.do_autoscale()
839+
840+
def showEvent(self, event): # pylint: disable=C0103
841+
"""Reimplement Qt method"""
842+
super().showEvent(event)
843+
QC.QTimer.singleShot(0, self.rescale_plots)
844+
845+
def add_plot(self, row, col, plot, sync=False, plot_id=None):
846+
"""Add plot to window"""
847+
if plot_id is None:
848+
plot_id = str(len(self.subplotwidget.plots) + 1)
849+
self.subplotwidget.add_plot(plot, row, col, plot_id)
850+
if sync and len(self.subplotwidget.plots) > 1:
851+
syncaxis = self.manager.synchronize_axis
852+
for i_plot in range(len(self.subplotwidget.plots) - 1):
853+
syncaxis(BasePlot.X_BOTTOM, [plot_id, f"{i_plot + 1}"])
854+
syncaxis(BasePlot.Y_LEFT, [plot_id, f"{i_plot + 1}"])
855+
856+
def get_plots(self) -> list[BasePlot]:
857+
"""Return the plots
858+
859+
Returns:
860+
list[BasePlot]: The plots
861+
"""
862+
return self.subplotwidget.get_plots()

plotpy/tests/gui/test_syncplot.py

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,15 @@
99

1010
import numpy as np
1111
from guidata.qthelpers import qt_app_context
12-
from qtpy import QtCore as QC
1312
from qtpy import QtGui as QG
14-
from qtpy import QtWidgets as QW
1513

1614
from plotpy.builder import make
1715
from plotpy.config import _
18-
from plotpy.plot import BasePlot, PlotOptions, SubplotWidget, set_widget_title_icon
19-
from plotpy.plot.manager import PlotManager
16+
from plotpy.plot import BasePlot, PlotOptions
17+
from plotpy.plot.plotwidget import SyncPlotWindow
2018
from plotpy.tests.data import gen_2d_gaussian
2119

2220

23-
class SyncPlotWindow(QW.QMainWindow):
24-
"""Window for showing plots, optionally synchronized"""
25-
26-
def __init__(self, parent=None, title=None, options=None):
27-
super().__init__(parent)
28-
title = self.__doc__ if title is None else title
29-
set_widget_title_icon(self, title, "plotpy.svg")
30-
self.manager = PlotManager(None)
31-
self.manager.set_main(self)
32-
self.subplotwidget = SubplotWidget(self.manager, parent=self, options=options)
33-
self.setCentralWidget(self.subplotwidget)
34-
toolbar = QW.QToolBar(_("Tools"), self)
35-
self.manager.add_toolbar(toolbar, "default")
36-
toolbar.setMovable(True)
37-
toolbar.setFloatable(True)
38-
self.addToolBar(toolbar)
39-
40-
def finalize_configuration(self):
41-
"""Configure plot manager and register all tools"""
42-
self.subplotwidget.add_panels_to_manager()
43-
self.subplotwidget.register_tools()
44-
45-
def rescale_plots(self):
46-
"""Rescale all plots"""
47-
QW.QApplication.instance().processEvents()
48-
for plot in self.subplotwidget.plots:
49-
plot.do_autoscale()
50-
51-
def showEvent(self, event): # pylint: disable=C0103
52-
"""Reimplement Qt method"""
53-
super().showEvent(event)
54-
QC.QTimer.singleShot(0, self.rescale_plots)
55-
56-
def add_plot(self, row, col, plot, sync=False, plot_id=None):
57-
"""Add plot to window"""
58-
if plot_id is None:
59-
plot_id = str(len(self.subplotwidget.plots) + 1)
60-
self.subplotwidget.add_plot(plot, row, col, plot_id)
61-
if sync and len(self.subplotwidget.plots) > 1:
62-
syncaxis = self.manager.synchronize_axis
63-
for i_plot in range(len(self.subplotwidget.plots) - 1):
64-
syncaxis(BasePlot.X_BOTTOM, [plot_id, f"{i_plot + 1}"])
65-
syncaxis(BasePlot.Y_LEFT, [plot_id, f"{i_plot + 1}"])
66-
67-
def get_plots(self) -> list[BasePlot]:
68-
"""Return the plots
69-
70-
Returns:
71-
list[BasePlot]: The plots
72-
"""
73-
return self.subplotwidget.get_plots()
74-
75-
7621
def plot(plot_type, *itemlists):
7722
"""Plot items in SyncPlotDialog"""
7823
win = SyncPlotWindow(options=PlotOptions(type=plot_type))

0 commit comments

Comments
 (0)