brisonus_app_eq/ERNCTuningTool/app.py
2025-02-18 22:05:52 +08:00

205 lines
7.2 KiB
Python

import yaml
from PySide6.QtGui import QPixmap
from PySide6.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
from PySide6.QtCore import Qt, Slot
from communication.com_thread import ComThread
from communication.param_manager import ParamManager
from components.tabs.tab_comconfig.widget_tab_comconfig import WidgetTabComConfig
from components.tabs.tab_params_viewer.widget_tab_params_viewer import WidgetParamViewer
from components.tabs.tab_tf.widget_tab_tf import WidgetTabTf
from components.tabs.tab_enc.widget_tab_enc import WidgetTabEnc
from ui_form import Ui_MainWindow
from components.tabs.tab_ernc.widget_tab_ernc import WidgetTabErnc
from components.tabs.tab_rnc.widget_tab_rnc import WidgetTabRnc
# from components.common.widget_check_plot.widget_plot import WidgetPlot
from components.common.widget_progress_bar.widget_progress_bar import WidgetProgressBar
from components.common.widget_check_plot.widget_check_plot import WidgetCheckPlot
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
# 设置窗口标题
self.setWindowTitle('Brisonus EnRNC Tuning V0.1.1')
self.resize(1200, 800)
self.data = {
"connection_status": False,
"port_name": ""
}
self.tab_0 = WidgetTabTf()
self.tab_1 = WidgetTabErnc()
self.tab_2 = WidgetTabRnc()
self.tab_3 = WidgetTabEnc()
self.tab_4 = WidgetCheckPlot()
self.tab_5 = WidgetParamViewer()
self.tab_6 = WidgetTabComConfig()
self.connection_status_icon = [
QPixmap('./assets/img/disconnect.png'),
QPixmap('./assets/img/connected.png')
]
# 设置tab页
self.tabWidget.addTab(self.tab_0, 'TF')
self.tabWidget.addTab(self.tab_1, 'ERNC')
self.tabWidget.addTab(self.tab_2, 'RNC')
self.tabWidget.addTab(self.tab_3, 'ENC')
self.tabWidget.addTab(self.tab_4, 'PLOT')
self.tabWidget.addTab(self.tab_5, 'VIEWER')
self.tabWidget.addTab(self.tab_6, 'COM')
# 提示框
self.tip = WidgetProgressBar(self)
self.tip.move( int(self.width()/2 - self.tip.width()/2),
int(self.height()/2 - self.tip.height()/2))
self.tip.hide()
self.param_mgr = ParamManager()
self.com_thread = ComThread()
self.com_thread.worker.signal_task_progress.connect(self.on_task_progress)
self.com_thread.worker.signal_task_done.connect(self.on_task_done)
# 通信异常处理
self.com_thread.worker.signal_communication_failed.connect(
self.on_communication_failed
)
self.tab_6.signal_port_set.connect(self.on_signal_port_set)
# 设置开屏处于COM配置页面
self.tabWidget.setCurrentIndex(6)
self.update_ui()
def on_communication_failed(self):
QMessageBox.warning(self, "Error", f"通信异常!")
def update_ui(self):
self.label_connection_status.setScaledContents(True)
self.label_connection_status.setPixmap(
self.connection_status_icon[1 if self.data['connection_status'] else 0].scaled(
self.label_connection_status.size(),
Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
)
self.label_port_name.setText(self.data['port_name'])
def on_task_progress(self, progress_data):
self.tip.progressBar.setValue(progress_data['progress'])
self.tip.label_val1.setText(str(progress_data['processed']))
self.tip.label_val2.setText(str(progress_data['total']))
self.tip.show()
QApplication.processEvents()
# 端口设置操作
def on_signal_port_set(self, info):
self.data['port_name'] = info['port']['name']
if info['status']:
if self.com_thread.worker.set_com_interface(info['port']):
self.data['connection_status'] = True
else:
self.data['connection_status'] = False
else:
if self.com_thread.worker.disconnect_interface():
self.data['connection_status'] = False
else:
self.data['connection_status'] = True
self.update_ui()
@Slot()
def on_pushButton_s2m_clicked(self):
"""
从设备上读取所有参数
:return:
"""
self.param_mgr.gen_read_all_params()
self.com_thread.run_task()
@Slot()
def on_pushButton_m2s_clicked(self):
"""
将上位机端的参数全部同步到设备上
:return:
"""
self.param_mgr.gen_write_all_params()
self.com_thread.run_task()
@Slot()
def on_pushButton_import_clicked(self):
# 打开文件选择对话框
file_path, _ = QFileDialog.getOpenFileName(
self,
"Open File", # 对话框标题
"", # 默认目录
"Parameter Files (*.yaml);" # 文件过滤器
)
if file_path: # 如果用户选择了文件
QMessageBox.information(self, "Selected File", f"File Path:\n{file_path}")
# 从文件中读取参数
try:
with open(file_path, 'r') as file:
params_text = file.read()
params_data = yaml.safe_load(params_text)
self.param_mgr.import_params_data(params_data)
self.label_filepath.setText(file_path)
# 刷新数据
self.com_thread.worker.signal_task_done_2.emit()
#
print("signal_task_done_2.emit")
except Exception as e:
QMessageBox.warning(self, "Error", f"{e}")
else:
QMessageBox.warning(self, "No File Selected", "No file was selected!")
@Slot()
def on_pushButton_export_clicked(self):
"""
把数据库中的数据以“可阅读”形式保存到本地
:return:
"""
file_path, _ = QFileDialog.getSaveFileName(
self,
"Save File", # 对话框标题
"", # 默认目录
"Parameter Files (*.yaml);" # 文件过滤器
)
if file_path: # 如果用户选择了保存路径
try:
# 示例:写入文件内容
_params_data = self.param_mgr.dump_all_params()
with open(file_path, "w") as file:
yaml.dump(_params_data, file)
QMessageBox.information(self, "Success", f"File saved successfully to:\n{file_path}")
except Exception as e:
QMessageBox.critical(self, "Error", f"Failed to save file:\n{str(e)}")
# 打开保存文件对话框
def on_task_done(self):
self.tip.hide()
QApplication.processEvents()
# 打印参数值
def closeEvent(self, event):
# 退出时清理线程
self.com_thread.cleanup()
event.accept()
def close(self):
self.com_thread.quit()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
rnc = MainWindow()
rnc.show()
sys.exit(app.exec())