Skip to content

Commit e0a6fdd

Browse files
committed
SyncPlotWindow: fixed constructor, added doc
1 parent 2109ada commit e0a6fdd

File tree

2 files changed

+68
-16
lines changed

2 files changed

+68
-16
lines changed

plotpy/plot/plotwidget.py

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -806,28 +806,62 @@ def add_plot(
806806

807807

808808
class SyncPlotWindow(QW.QMainWindow):
809-
"""Window for showing plots, optionally synchronized"""
809+
"""Window for showing plots, optionally synchronized
810810
811-
def __init__(self, parent=None, title=None, options=None):
811+
Args:
812+
parent: parent widget
813+
toolbar: show/hide toolbar
814+
options: plot options
815+
panels: additionnal panels
816+
auto_tools: If True, the plot tools are automatically registered.
817+
If False, the user must register the tools manually.
818+
title: The window title
819+
icon: The window icon
820+
821+
Usage: first, create a window, then add plots to it, then call the
822+
:py:meth:`.SyncPlotWindow.finalize_configuration` method to add panels and
823+
eventually register tools.
824+
825+
Example::
826+
827+
from plotpy.plot import BasePlot, SyncPlotWindow
828+
win = SyncPlotWindow(title="My window")
829+
plot = BasePlot()
830+
win.add_plot(plot)
831+
win.finalize_configuration()
832+
win.show()
833+
"""
834+
835+
def __init__(
836+
self,
837+
parent: QWidget | None = None,
838+
toolbar: bool = True,
839+
options: PlotOptions | None = None,
840+
auto_tools: bool = True,
841+
title: str = "PlotPy",
842+
icon: str = "plotpy.svg",
843+
) -> None:
812844
super().__init__(parent)
813-
title = self.__doc__ if title is None else title
814-
set_widget_title_icon(self, title, "plotpy.svg")
845+
set_widget_title_icon(self, title, icon)
815846
self.manager = PlotManager(None)
816847
self.manager.set_main(self)
817848
self.subplotwidget = SubplotWidget(self.manager, parent=self, options=options)
818849
self.setCentralWidget(self.subplotwidget)
819-
toolbar = QW.QToolBar(_("Tools"), self)
820-
self.manager.add_toolbar(toolbar, "default")
821-
toolbar.setMovable(True)
822-
toolbar.setFloatable(True)
823-
self.addToolBar(toolbar)
824-
825-
def finalize_configuration(self):
850+
self.toolbar = QW.QToolBar(_("Tools"), self)
851+
self.toolbar.setVisible(toolbar)
852+
self.manager.add_toolbar(self.toolbar, "default")
853+
self.toolbar.setMovable(True)
854+
self.toolbar.setFloatable(True)
855+
self.addToolBar(self.toolbar)
856+
self.auto_tools = auto_tools
857+
858+
def finalize_configuration(self) -> None:
826859
"""Configure plot manager and register all tools"""
827860
self.subplotwidget.add_panels_to_manager()
828-
self.subplotwidget.register_tools()
861+
if self.auto_tools:
862+
self.subplotwidget.register_tools()
829863

830-
def rescale_plots(self):
864+
def rescale_plots(self) -> None:
831865
"""Rescale all plots"""
832866
QW.QApplication.instance().processEvents()
833867
for plot in self.subplotwidget.plots:
@@ -838,8 +872,23 @@ def showEvent(self, event): # pylint: disable=C0103
838872
super().showEvent(event)
839873
QC.QTimer.singleShot(0, self.rescale_plots)
840874

841-
def add_plot(self, row, col, plot, sync=False, plot_id=None):
842-
"""Add plot to window"""
875+
def add_plot(
876+
self,
877+
row: int,
878+
col: int,
879+
plot: BasePlot,
880+
sync: bool = False,
881+
plot_id: str | None = None,
882+
) -> None:
883+
"""Add plot to window
884+
885+
Args:
886+
row: The row index
887+
col: The column index
888+
plot: The plot to add
889+
sync: If True, the axes are synchronized
890+
plot_id: The plot id
891+
"""
843892
if plot_id is None:
844893
plot_id = str(len(self.subplotwidget.plots) + 1)
845894
self.subplotwidget.add_plot(plot, row, col, plot_id)

plotpy/tests/gui/test_syncplot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
def plot(plot_type, *itemlists):
2222
"""Plot items in SyncPlotDialog"""
23-
win = SyncPlotWindow(options=PlotOptions(type=plot_type))
23+
win = SyncPlotWindow(
24+
title="Window for showing plots, optionally synchronized",
25+
options=PlotOptions(type=plot_type),
26+
)
2427
row, col = 0, 0
2528
for items in itemlists:
2629
plot = BasePlot()

0 commit comments

Comments
 (0)