2024-12-17 20:40:04 +03:00
|
|
|
|
import pyqtgraph as pg
|
|
|
|
|
|
from pyqtgraph.parametertree import Parameter, ParameterTree
|
|
|
|
|
|
from typing import Union
|
|
|
|
|
|
from PyQt5 import QtWidgets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ReportSettings(QtWidgets.QWidget):
|
|
|
|
|
|
|
|
|
|
|
|
def build(self, reg_items: dict, curve_items: dict) -> None:
|
2024-12-18 12:58:37 +03:00
|
|
|
|
"""Создает ParameterTree для элементов всех графиков выбранной вкладки"""
|
2024-12-18 12:44:43 +03:00
|
|
|
|
self._clear()
|
2024-12-17 20:40:04 +03:00
|
|
|
|
param_tree = ParameterTree()
|
2024-12-18 12:44:43 +03:00
|
|
|
|
layout = self.layout()
|
2024-12-17 20:40:04 +03:00
|
|
|
|
layout.addWidget(param_tree)
|
|
|
|
|
|
|
|
|
|
|
|
body= [
|
|
|
|
|
|
self._generate_reg_params(reg_items),
|
|
|
|
|
|
self._generate_curve_params(curve_items)
|
|
|
|
|
|
]
|
|
|
|
|
|
# Добавляем параметры в дерево
|
|
|
|
|
|
params = Parameter.create(name='params', type='group', children=body)
|
2024-12-18 12:58:37 +03:00
|
|
|
|
params.sigTreeStateChanged.connect(
|
|
|
|
|
|
lambda: self._update_settings(reg_items, curve_items, params)
|
|
|
|
|
|
)
|
2024-12-17 20:40:04 +03:00
|
|
|
|
param_tree.setParameters(params, showTop=False)
|
|
|
|
|
|
self.show()
|
|
|
|
|
|
|
2024-12-18 12:58:37 +03:00
|
|
|
|
def _clear(self) -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Приводит виджет в готовое к работе состояние.
|
|
|
|
|
|
Удаляет все содержимое, если имеется
|
|
|
|
|
|
"""
|
2024-12-18 12:44:43 +03:00
|
|
|
|
main = self.layout()
|
|
|
|
|
|
if self.layout() is not None:
|
|
|
|
|
|
while main.count():
|
|
|
|
|
|
child = main.takeAt(0)
|
|
|
|
|
|
if child.widget() is not None:
|
|
|
|
|
|
child.widget().deleteLater()
|
|
|
|
|
|
else:
|
|
|
|
|
|
self.setLayout(QtWidgets.QVBoxLayout())
|
|
|
|
|
|
|
2024-12-18 12:58:37 +03:00
|
|
|
|
def _generate_reg_params(self,
|
|
|
|
|
|
reg_items: dict) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
"""Созадет реальные и идеальные секторы"""
|
2024-12-17 20:40:04 +03:00
|
|
|
|
|
|
|
|
|
|
res = {'name': 'Sectors', 'type': 'group', 'children': [
|
|
|
|
|
|
{'name': 'Real sectors', 'type': 'group', 'children': self._create_samples(reg_items["real"])},
|
|
|
|
|
|
{'name': 'Ideal sectors', 'type': 'group', 'children': self._create_samples(reg_items["ideal"])},
|
|
|
|
|
|
]}
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
2024-12-18 12:58:37 +03:00
|
|
|
|
def _generate_curve_params(self,
|
|
|
|
|
|
curve_items: dict) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
"""Создает реальные и идеальные линии графиков"""
|
2024-12-17 20:40:04 +03:00
|
|
|
|
|
|
|
|
|
|
res = {'name': 'Plots', 'type': 'group', 'children': [
|
|
|
|
|
|
{'name': 'Real plots', 'type': 'group', 'children': self._create_samples(curve_items["real"])},
|
|
|
|
|
|
{'name': 'Ideal plots', 'type': 'group', 'children': self._create_ideal_curves(curve_items["ideal"])},
|
|
|
|
|
|
]}
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
2024-12-18 12:58:37 +03:00
|
|
|
|
def _create_ideal_curves(self,
|
|
|
|
|
|
curve: dict) -> list[dict]:
|
|
|
|
|
|
|
|
|
|
|
|
"""Создает секторы с этапами циклограммы"""
|
|
|
|
|
|
|
2024-12-17 20:40:04 +03:00
|
|
|
|
res = []
|
|
|
|
|
|
for key, item in curve.items():
|
|
|
|
|
|
param = {'name': key, 'type': 'group', 'children': self._create_samples(item)}
|
|
|
|
|
|
res.append(param)
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
2024-12-18 12:58:37 +03:00
|
|
|
|
def _create_samples(self,
|
|
|
|
|
|
sector: dict) -> list[dict]:
|
|
|
|
|
|
|
|
|
|
|
|
"""Создает список представленных элементов с их параметрами"""
|
|
|
|
|
|
|
2024-12-17 20:40:04 +03:00
|
|
|
|
res = []
|
|
|
|
|
|
for key, item in sector.items():
|
|
|
|
|
|
sample = item[0] if type(item) == list else item
|
|
|
|
|
|
param = {'name': key, 'type': 'group', 'children': self._create_settings(sample)}
|
|
|
|
|
|
res.append(param)
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
2024-12-18 12:58:37 +03:00
|
|
|
|
def _create_settings(self,
|
|
|
|
|
|
item: Union[pg.LinearRegionItem, pg.PlotDataItem]) -> list[dict]:
|
|
|
|
|
|
|
|
|
|
|
|
"""Получает настройки для элемента"""
|
|
|
|
|
|
|
2024-12-17 20:40:04 +03:00
|
|
|
|
if type(item) == pg.LinearRegionItem:
|
|
|
|
|
|
pen = item.lines[0].pen
|
|
|
|
|
|
brush = item.brush
|
|
|
|
|
|
fill_color = brush.color().getRgb()
|
|
|
|
|
|
else:
|
|
|
|
|
|
pen = pg.mkPen(item.opts.get("pen"))
|
|
|
|
|
|
fill_color = None
|
|
|
|
|
|
line_color = pen.color().getRgb()
|
|
|
|
|
|
line_thickness = pen.width()
|
|
|
|
|
|
visibility = item.isVisible()
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
{'name': 'Line color', 'type': 'color', 'value': line_color},
|
|
|
|
|
|
{'name': 'Line thickness', 'type': 'int', 'value': line_thickness, 'limits': (1, 10)},
|
|
|
|
|
|
{'name': 'Visibility', 'type': 'bool', 'value': visibility},
|
|
|
|
|
|
{'name': 'Fill color', 'type': 'color', 'value': fill_color},
|
|
|
|
|
|
]
|
|
|
|
|
|
|
2024-12-18 12:58:37 +03:00
|
|
|
|
def _update_settings(self,
|
|
|
|
|
|
reg_items: dict,
|
|
|
|
|
|
curve_items: dict,
|
|
|
|
|
|
params: Parameter) -> None:
|
|
|
|
|
|
|
|
|
|
|
|
"""Задает параметры элементов в соответствии с paramTree"""
|
|
|
|
|
|
|
2024-12-18 12:44:43 +03:00
|
|
|
|
real_sectors = params.child("Sectors").child("Real sectors")
|
|
|
|
|
|
ideal_sectors = params.child("Sectors").child("Ideal sectors")
|
|
|
|
|
|
|
|
|
|
|
|
real_plots = params.child("Plots").child("Real plots")
|
|
|
|
|
|
ideal_plots = params.child("Plots").child("Ideal plots")
|
|
|
|
|
|
|
|
|
|
|
|
self._set_sector_settings(reg_items["real"], real_sectors)
|
|
|
|
|
|
self._set_sector_settings(reg_items["ideal"], ideal_sectors)
|
|
|
|
|
|
|
|
|
|
|
|
self._set_plot_settings(curve_items["real"], real_plots)
|
|
|
|
|
|
for key, item_dict in curve_items["ideal"].items():
|
|
|
|
|
|
self._set_plot_settings(item_dict, ideal_plots.child(key))
|
|
|
|
|
|
|
2024-12-18 12:58:37 +03:00
|
|
|
|
def _set_sector_settings(self,
|
|
|
|
|
|
sectors: dict,
|
|
|
|
|
|
settings: Parameter) -> None:
|
|
|
|
|
|
|
|
|
|
|
|
"""Задает параметры секторов в соответствии с настройками"""
|
|
|
|
|
|
|
2024-12-18 12:44:43 +03:00
|
|
|
|
for key, item in sectors.items():
|
|
|
|
|
|
sample = settings.child(key)
|
|
|
|
|
|
line_color = sample.child("Line color").value()
|
|
|
|
|
|
line_width = sample.child("Line thickness").value()
|
|
|
|
|
|
visibility = sample.child("Visibility").value()
|
|
|
|
|
|
fill_color = sample.child("Fill color").value()
|
|
|
|
|
|
|
|
|
|
|
|
pen = pg.mkPen(color=line_color, width=line_width)
|
|
|
|
|
|
brush=pg.mkBrush(fill_color)
|
|
|
|
|
|
for reg in item:
|
|
|
|
|
|
reg.setVisible(visibility)
|
|
|
|
|
|
reg.lines[0].setPen(pen)
|
|
|
|
|
|
reg.lines[1].setPen(pen)
|
|
|
|
|
|
reg.setBrush(brush)
|
2024-12-17 20:40:04 +03:00
|
|
|
|
|
2024-12-18 12:58:37 +03:00
|
|
|
|
def _set_plot_settings(self,
|
|
|
|
|
|
curves:dict,
|
|
|
|
|
|
settings: Parameter) -> None:
|
|
|
|
|
|
|
|
|
|
|
|
"""Задает параметры кривых в соответствии с настройками"""
|
|
|
|
|
|
|
2024-12-18 12:44:43 +03:00
|
|
|
|
for key, item in curves.items():
|
|
|
|
|
|
sample = settings.child(key)
|
|
|
|
|
|
line_color = sample.child("Line color").value()
|
|
|
|
|
|
line_width = sample.child("Line thickness").value()
|
|
|
|
|
|
visibility = sample.child("Visibility").value()
|
|
|
|
|
|
pen = pg.mkPen(color=line_color, width=line_width)
|
|
|
|
|
|
if type(item) == list:
|
|
|
|
|
|
for curve in item:
|
|
|
|
|
|
curve.setVisible(visibility)
|
|
|
|
|
|
curve.setPen(pen)
|
|
|
|
|
|
else:
|
|
|
|
|
|
item.setVisible(visibility)
|
|
|
|
|
|
item.setPen(pen)
|