0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 11:41:49 +02:00

refactor: BECFigure, BECPlotBase changed back to pyqtgraph classes inheritance

This commit is contained in:
wyzula-jan
2024-02-14 18:27:59 +01:00
parent 9ef331c272
commit 7768e594b5
2 changed files with 23 additions and 37 deletions

View File

@ -24,44 +24,37 @@ class FigureConfig(ConnectionConfig):
) )
class BECFigure(BECConnector): class BECFigure(BECConnector, pg.GraphicsLayoutWidget):
def __init__( def __init__(
self, self,
parent: Optional[QWidget] = None, parent: Optional[QWidget] = None,
config: Optional[FigureConfig] = None, config: Optional[FigureConfig] = None,
client=None, client=None,
gui_id: Optional[str] = None, gui_id: Optional[str] = None,
graphics_layout_widget: Optional[pg.GraphicsLayoutWidget] = None,
): ):
if config is None: if config is None:
config = FigureConfig(widget_class=self.__class__.__name__) config = FigureConfig(widget_class=self.__class__.__name__)
else: else:
self.config = config self.config = config
super().__init__(client=client, config=config, gui_id=gui_id) super().__init__(client=client, config=config, gui_id=gui_id)
# pg.GraphicsLayoutWidget.__init__(self, parent) #in case of inheritance pg.GraphicsLayoutWidget.__init__(self, parent) # in case of inheritance
self.glw = (
graphics_layout_widget
if graphics_layout_widget
else pg.GraphicsLayoutWidget(parent=parent)
)
self.widgets = {} self.widgets = {}
# TODO just testing adding plot # TODO just testing adding plot
self.add_widget("widget_1", row=0, col=0, title="Plot 1") self.add_widget("widget_1", row=0, col=0, title="Plot 1")
self.widgets["widget_1"].plot( self.widgets["widget_1"].plot_data(
np.linspace(0, 10, 100), np.sin(np.linspace(0, 10, 100)), label="sin(x)" np.linspace(0, 10, 100), np.sin(np.linspace(0, 10, 100)), label="sin(x)"
) )
def show(self): # TODO check if useful for anything # def show(self): # TODO check if useful for anything
self.window = QMainWindow() # self.window = QMainWindow()
self.window.setCentralWidget(self.glw) # self.window.setCentralWidget(self)
self.window.show() # self.window.show()
#
def close(self): # TODO check if useful for anything # def close(self): # TODO check if useful for anything
if hasattr(self, "window"): # if hasattr(self, "window"):
self.window.close() # self.window.close()
def add_widget(self, widget_id: str = None, row: int = None, col: int = None, **kwargs): def add_widget(self, widget_id: str = None, row: int = None, col: int = None, **kwargs):
# Generate unique widget_id if not provided # Generate unique widget_id if not provided
@ -78,13 +71,12 @@ class BECFigure(BECConnector):
parent_figure_id=self.gui_id, widget_class="BECPlotBase", gui_id=widget_id, **kwargs parent_figure_id=self.gui_id, widget_class="BECPlotBase", gui_id=widget_id, **kwargs
) )
plot_item = pg.PlotItem() widget = BECPlotBase(config=widget_config)
widget = BECPlotBase(plot_item=plot_item, config=widget_config)
# Check if position is occupied # Check if position is occupied
if row is not None and col is not None: if row is not None and col is not None:
print("adding plot") print("adding plot")
if self.glw.getItem(row, col): if self.getItem(row, col):
print( print(
f"Position at row {row} and column {col} is already occupied." f"Position at row {row} and column {col} is already occupied."
) # TODO change to raise error ) # TODO change to raise error
@ -94,14 +86,14 @@ class BECFigure(BECConnector):
widget_config.column = col widget_config.column = col
# Add widget to the figure # Add widget to the figure
self.glw.addItem(widget.plt, row=row, col=col) self.addItem(widget, row=row, col=col)
else: else:
row, col = self._find_next_empty_position() row, col = self._find_next_empty_position()
widget_config.row = row widget_config.row = row
widget_config.column = col widget_config.column = col
# Add widget to the figure # Add widget to the figure
self.glw.addItem(widget.plt, row=row, col=col) self.addItem(widget, row=row, col=col)
# Saving config for future referencing # Saving config for future referencing
self.config.widgets[widget_id] = widget_config self.config.widgets[widget_id] = widget_config
@ -130,7 +122,7 @@ class BECFigure(BECConnector):
Returns: Returns:
BECPlotBase: the widget at the given coordinates BECPlotBase: the widget at the given coordinates
""" """
widget = self.glw.getItem(row, col) widget = self.getItem(row, col)
if widget is None: if widget is None:
raise KeyError(f"No widget at coordinates ({row}, {col})") raise KeyError(f"No widget at coordinates ({row}, {col})")
return widget return widget
@ -151,7 +143,7 @@ class BECFigure(BECConnector):
def _find_next_empty_position(self): def _find_next_empty_position(self):
"""Find the next empty position (new row) in the figure.""" """Find the next empty position (new row) in the figure."""
row, col = 0, 0 row, col = 0, 0
while self.glw.getItem(row, col): while self.getItem(row, col):
row += 1 row += 1
return row, col return row, col
@ -207,13 +199,8 @@ class DebugWindow(QWidget):
def _init_ui(self): def _init_ui(self):
# Plotting window # Plotting window
self.glw_1_layout = QVBoxLayout(self.glw) # Create a new QVBoxLayout self.glw_1_layout = QVBoxLayout(self.glw) # Create a new QVBoxLayout
graphics_layout_widget = ( self.figure = BECFigure(parent=self) # Create a new BECDeviceMonitor
pg.GraphicsLayoutWidget() self.glw_1_layout.addWidget(self.figure) # Add BECDeviceMonitor to the layout
) # create a new pg.GraphicsLayoutWidget instance
self.figure = BECFigure(
graphics_layout_widget=graphics_layout_widget, parent=self
) # Create a new BECDeviceMonitor
self.glw_1_layout.addWidget(self.figure.glw) # Add BECDeviceMonitor to the layout
self.console_layout = QVBoxLayout(self.widget_console) self.console_layout = QVBoxLayout(self.widget_console)
self.console = JupyterConsoleWidget() self.console = JupyterConsoleWidget()

View File

@ -33,7 +33,7 @@ class WidgetConfig(ConnectionConfig):
) )
class BECPlotBase(BECConnector): # , pg.PlotItem): class BECPlotBase(BECConnector, pg.PlotItem):
USER_ACCESS = [ USER_ACCESS = [
"set", "set",
"set_title", "set_title",
@ -49,7 +49,6 @@ class BECPlotBase(BECConnector): # , pg.PlotItem):
def __init__( def __init__(
self, self,
parent: Optional[QWidget] = None, # TODO decide if needed for this class parent: Optional[QWidget] = None, # TODO decide if needed for this class
plot_item: Optional[pg.PlotItem] = None,
config: Optional[WidgetConfig] = None, config: Optional[WidgetConfig] = None,
client=None, client=None,
gui_id: Optional[str] = None, gui_id: Optional[str] = None,
@ -57,7 +56,7 @@ class BECPlotBase(BECConnector): # , pg.PlotItem):
if config is None: if config is None:
config = WidgetConfig(widget_class=self.__class__.__name__) config = WidgetConfig(widget_class=self.__class__.__name__)
super().__init__(client=client, config=config, gui_id=gui_id) super().__init__(client=client, config=config, gui_id=gui_id)
self.plt = plot_item if plot_item else pg.PlotItem(parent=parent) pg.PlotItem.__init__(self, parent)
def set(self, **kwargs) -> None: def set(self, **kwargs) -> None:
""" """
@ -176,7 +175,7 @@ class BECPlotBase(BECConnector): # , pg.PlotItem):
self.config.axis.x_grid = x self.config.axis.x_grid = x
self.config.axis.y_grid = y self.config.axis.y_grid = y
def plot(self, data_x: list | np.ndarray, data_y: list | np.ndarray, label: str = None): def plot_data(self, data_x: list | np.ndarray, data_y: list | np.ndarray, label: str = None):
""" """
Plot custom data on the plot widget. These data are not saved in config. Plot custom data on the plot widget. These data are not saved in config.
Args: Args:
@ -184,4 +183,4 @@ class BECPlotBase(BECConnector): # , pg.PlotItem):
data_y(list|np.ndarray): y-axis data data_y(list|np.ndarray): y-axis data
label(str): label of the plot label(str): label of the plot
""" """
self.plt.plot(data_x, data_y, name=label) self.plot(data_x, data_y, name=label)