2025-01-20 14:38:41 +03:00
|
|
|
|
import traceback
|
|
|
|
|
|
import sys
|
|
|
|
|
|
from typing import Union
|
|
|
|
|
|
|
2024-12-17 20:40:04 +03:00
|
|
|
|
import pyqtgraph as pg
|
|
|
|
|
|
from pyqtgraph.parametertree import Parameter, ParameterTree
|
|
|
|
|
|
from PyQt5 import QtWidgets
|
2024-12-24 16:25:50 +03:00
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
2024-12-17 20:40:04 +03:00
|
|
|
|
|
|
|
|
|
|
class ReportSettings(QtWidgets.QWidget):
|
2024-12-23 18:51:17 +03:00
|
|
|
|
def __init__(self, parent = None):
|
|
|
|
|
|
super().__init__(parent)
|
2024-12-17 20:40:04 +03:00
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
def build(self, reg_items: dict, curve_items: dict, qt_items: dict) -> None:
|
2024-12-18 12:58:37 +03:00
|
|
|
|
"""Создает ParameterTree для элементов всех графиков выбранной вкладки"""
|
2024-12-24 16:25:50 +03:00
|
|
|
|
try:
|
|
|
|
|
|
self._clear()
|
|
|
|
|
|
param_tree = ParameterTree()
|
|
|
|
|
|
layout = self.layout()
|
|
|
|
|
|
layout.addWidget(param_tree)
|
2024-12-23 18:51:17 +03:00
|
|
|
|
body= [
|
2025-02-11 16:37:39 +03:00
|
|
|
|
ReportSettings._generate_reg_params(reg_items),
|
|
|
|
|
|
ReportSettings._generate_curve_params(curve_items),
|
|
|
|
|
|
ReportSettings._generate_qt_params(qt_items)
|
2024-12-24 16:25:50 +03:00
|
|
|
|
]
|
|
|
|
|
|
# Добавляем параметры в дерево
|
|
|
|
|
|
params = Parameter.create(name='params', type='group', children=body)
|
|
|
|
|
|
params.sigTreeStateChanged.connect(
|
2025-02-11 16:37:39 +03:00
|
|
|
|
lambda: ReportSettings._update_settings(reg_items, curve_items, qt_items, params)
|
2024-12-24 16:25:50 +03:00
|
|
|
|
)
|
|
|
|
|
|
param_tree.setParameters(params, showTop=False)
|
|
|
|
|
|
except:
|
|
|
|
|
|
tb = sys.exc_info()[2]
|
|
|
|
|
|
tbinfo = traceback.format_tb(tb)[0]
|
|
|
|
|
|
pymsg = "Traceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
|
|
|
|
|
|
logger.error(pymsg)
|
2024-12-17 20:40:04 +03:00
|
|
|
|
|
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())
|
|
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _generate_qt_params(qt_items: dict) -> dict:
|
2024-12-24 18:45:07 +03:00
|
|
|
|
"""Создает qt элементы"""
|
|
|
|
|
|
res = {'name': 'Qt elements', 'type': 'group', 'children':[
|
2025-02-11 16:37:39 +03:00
|
|
|
|
{'name': key, 'type': 'group', 'children': ReportSettings._create_qt_samples(item)} for key, item in qt_items.items()
|
2024-12-24 18:45:07 +03:00
|
|
|
|
]}
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _create_qt_samples(item: Union[QtWidgets.QWidget, QtWidgets.QLabel]) -> dict:
|
2024-12-24 18:45:07 +03:00
|
|
|
|
visibility = item.isVisible()
|
|
|
|
|
|
return [
|
|
|
|
|
|
{'name': 'Visibility', 'type': 'bool', 'value': visibility}
|
|
|
|
|
|
]
|
|
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _generate_reg_params(reg_items: dict) -> dict:
|
2024-12-18 12:58:37 +03:00
|
|
|
|
"""Созадет реальные и идеальные секторы"""
|
2024-12-17 20:40:04 +03:00
|
|
|
|
res = {'name': 'Sectors', 'type': 'group', 'children': [
|
2025-02-11 16:37:39 +03:00
|
|
|
|
{'name': 'Real sectors', 'type': 'group', 'children': ReportSettings._create_samples(reg_items["real"])},
|
|
|
|
|
|
{'name': 'Ideal sectors', 'type': 'group', 'children': ReportSettings._create_samples(reg_items["ideal"])},
|
2024-12-17 20:40:04 +03:00
|
|
|
|
]}
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _generate_curve_params(curve_items: dict) -> dict:
|
2024-12-18 12:58:37 +03:00
|
|
|
|
"""Создает реальные и идеальные линии графиков"""
|
2024-12-17 20:40:04 +03:00
|
|
|
|
res = {'name': 'Plots', 'type': 'group', 'children': [
|
2025-02-11 16:37:39 +03:00
|
|
|
|
{'name': 'Real plots', 'type': 'group', 'children': ReportSettings._create_samples(curve_items["real"])},
|
|
|
|
|
|
{'name': 'Ideal plots', 'type': 'group', 'children': ReportSettings._create_samples(curve_items["ideal"])},
|
2024-12-17 20:40:04 +03:00
|
|
|
|
]}
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _create_samples(sector: dict) -> list[dict]:
|
2024-12-18 12:58:37 +03:00
|
|
|
|
"""Создает список представленных элементов с их параметрами"""
|
2024-12-17 20:40:04 +03:00
|
|
|
|
res = []
|
|
|
|
|
|
for key, item in sector.items():
|
|
|
|
|
|
sample = item[0] if type(item) == list else item
|
2025-02-11 16:37:39 +03:00
|
|
|
|
param = {'name': key, 'type': 'group', 'children': ReportSettings._create_settings(sample)}
|
2024-12-17 20:40:04 +03:00
|
|
|
|
res.append(param)
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _create_settings(item: Union[pg.LinearRegionItem, pg.PlotDataItem]) -> list[dict]:
|
2024-12-18 12:58:37 +03:00
|
|
|
|
"""Получает настройки для элемента"""
|
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},
|
|
|
|
|
|
]
|
|
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _update_settings(reg_items: dict,
|
2024-12-24 18:45:07 +03:00
|
|
|
|
curve_items: dict,
|
|
|
|
|
|
qt_items: dict,
|
2024-12-18 12:58:37 +03:00
|
|
|
|
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")
|
2024-12-24 18:45:07 +03:00
|
|
|
|
|
|
|
|
|
|
qt_settings = params.child("Qt elements")
|
2024-12-18 12:44:43 +03:00
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
ReportSettings._set_sector_settings(reg_items["real"], real_sectors)
|
|
|
|
|
|
ReportSettings._set_sector_settings(reg_items["ideal"], ideal_sectors)
|
2024-12-18 12:44:43 +03:00
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
ReportSettings._set_plot_settings(curve_items["real"], real_plots)
|
|
|
|
|
|
ReportSettings._set_plot_settings(curve_items["ideal"], ideal_plots)
|
|
|
|
|
|
ReportSettings._set_qt_settings(qt_items, qt_settings)
|
2024-12-24 18:45:07 +03:00
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _set_sector_settings(sectors: dict,
|
2024-12-18 12:58:37 +03:00
|
|
|
|
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
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _set_plot_settings(curves: dict,
|
2024-12-18 12:58:37 +03:00
|
|
|
|
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)
|
2024-12-24 18:45:07 +03:00
|
|
|
|
item.setPen(pen)
|
|
|
|
|
|
|
2025-02-11 16:37:39 +03:00
|
|
|
|
@staticmethod
|
|
|
|
|
|
def _set_qt_settings(qt_items: dict,
|
2024-12-24 18:45:07 +03:00
|
|
|
|
settings: Parameter) -> None:
|
|
|
|
|
|
"""Задает параметры Qt элементов в соответствии с настройками"""
|
|
|
|
|
|
for key, item in qt_items.items():
|
|
|
|
|
|
sample = settings.child(key)
|
|
|
|
|
|
visibility = sample.child("Visibility").value()
|
|
|
|
|
|
item.setVisible(visibility)
|