mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-03-04 16:02:51 +01:00
fix: prel. support for config_plotter with widgets instead of items
This commit is contained in:
@@ -2,11 +2,12 @@ from typing import List
|
||||
|
||||
import numpy as np
|
||||
import pyqtgraph as pg
|
||||
from PyQt5.QtWidgets import QApplication, QGridLayout, QSizePolicy, QWidget
|
||||
from pyqtgraph import mkPen
|
||||
from pyqtgraph.Qt import QtCore, QtWidgets
|
||||
from pyqtgraph.Qt import QtCore
|
||||
|
||||
|
||||
class ConfigPlotter(pg.GraphicsWidget):
|
||||
class ConfigPlotter(QWidget):
|
||||
"""
|
||||
ConfigPlotter is a widget that can be used to plot data from multiple channels
|
||||
in a grid layout. The layout is specified by a list of dicts, where each dict
|
||||
@@ -28,7 +29,7 @@ class ConfigPlotter(pg.GraphicsWidget):
|
||||
"""
|
||||
|
||||
def __init__(self, configs: List[dict], parent=None):
|
||||
super().__init__(parent)
|
||||
super(ConfigPlotter, self).__init__()
|
||||
self.configs = configs
|
||||
self.plots = {}
|
||||
self._init_ui()
|
||||
@@ -37,63 +38,73 @@ class ConfigPlotter(pg.GraphicsWidget):
|
||||
def _init_ui(self):
|
||||
pg.setConfigOption("background", "w")
|
||||
pg.setConfigOption("foreground", "k")
|
||||
self.layout = QGridLayout()
|
||||
self.setLayout(self.layout)
|
||||
self.pen = mkPen(color=(56, 76, 107), width=4, style=QtCore.Qt.SolidLine)
|
||||
|
||||
self.view = pg.GraphicsView()
|
||||
self.view.setAntialiasing(True)
|
||||
self.view.show()
|
||||
|
||||
self.layout = pg.GraphicsLayout()
|
||||
self.view.setCentralWidget(self.layout)
|
||||
self.show()
|
||||
|
||||
def _init_plots(self):
|
||||
for config in self.configs:
|
||||
channels = config["config"]["channels"]
|
||||
for channel in channels:
|
||||
item = pg.PlotItem()
|
||||
self.layout.addItem(
|
||||
item,
|
||||
row=config["y"],
|
||||
col=config["x"],
|
||||
rowspan=config["rows"],
|
||||
colspan=config["cols"],
|
||||
)
|
||||
|
||||
# call the corresponding init function, e.g. init_plotitem
|
||||
init_func = getattr(self, f"init_{config['config']['item']}")
|
||||
init_func(channel, config["config"], item)
|
||||
init_func(channel, config)
|
||||
|
||||
# self.init_ImageItem(channel, config["config"], item)
|
||||
|
||||
def init_PlotItem(self, channel: str, config: dict, item: pg.GraphicsItem):
|
||||
def init_PlotItem(self, channel: str, config: dict):
|
||||
"""
|
||||
Initialize a PlotItem
|
||||
|
||||
Args:
|
||||
channel(str): channel to plot
|
||||
config(dict): config dict for the channel
|
||||
item(pg.GraphicsItem): PlotItem to plot the data
|
||||
"""
|
||||
# pylint: disable=invalid-name
|
||||
plot_data = item.plot(np.random.rand(100), pen=self.pen)
|
||||
item.setLabel("left", channel)
|
||||
self.plots[channel] = {"item": item, "plot_data": plot_data}
|
||||
plot_widget = pg.PlotWidget()
|
||||
plot_widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
|
||||
self.layout.addWidget(plot_widget, config["y"], config["x"], config["rows"], config["cols"])
|
||||
plot_data = plot_widget.plot(np.random.rand(100), pen=self.pen)
|
||||
# item.setLabel("left", channel)
|
||||
# self.plots[channel] = {"item": item, "plot_data": plot_data}
|
||||
|
||||
def init_ImageItem(self, channel: str, config: dict, item: pg.GraphicsItem):
|
||||
def init_ImageItem(self, channel: str, config: dict):
|
||||
"""
|
||||
Initialize an ImageItem
|
||||
|
||||
Args:
|
||||
channel(str): channel to plot
|
||||
config(dict): config dict for the channel
|
||||
item(pg.GraphicsItem): ImageItem to plot the data
|
||||
"""
|
||||
# pylint: disable=invalid-name
|
||||
item = pg.PlotItem()
|
||||
self.layout.addItem(
|
||||
item,
|
||||
row=config["y"],
|
||||
col=config["x"],
|
||||
rowspan=config["rows"],
|
||||
colspan=config["cols"],
|
||||
)
|
||||
img = pg.ImageItem()
|
||||
item.addItem(img)
|
||||
img.setImage(np.random.rand(100, 100))
|
||||
self.plots[channel] = {"item": item, "plot_data": img}
|
||||
|
||||
def init_ImageView(self, channel: str, config: dict):
|
||||
"""
|
||||
Initialize an ImageView
|
||||
|
||||
Args:
|
||||
channel(str): channel to plot
|
||||
config(dict): config dict for the channel
|
||||
"""
|
||||
# pylint: disable=invalid-name
|
||||
img = pg.ImageView()
|
||||
img.setImage(np.random.rand(100, 100))
|
||||
self.layout.addWidget(img, config["y"], config["x"], config["rows"], config["cols"])
|
||||
self.plots[channel] = {"item": img, "plot_data": img}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
@@ -118,10 +129,10 @@ if __name__ == "__main__":
|
||||
"rows": 2,
|
||||
"y": 0,
|
||||
"x": 1,
|
||||
"config": {"channels": ["c"], "label_xy": ["", "c"], "item": "ImageItem"},
|
||||
"config": {"channels": ["c"], "label_xy": ["", "c"], "item": "ImageView"},
|
||||
},
|
||||
]
|
||||
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
app = QApplication(sys.argv)
|
||||
win = ConfigPlotter(CONFIG)
|
||||
pg.exec()
|
||||
|
||||
Reference in New Issue
Block a user