HoYoCenter/interface/setting.py

65 lines
2.2 KiB
Python
Raw Normal View History

2024-09-09 10:50:37 +08:00
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
2024-09-08 19:53:00 +08:00
from qfluentwidgets import FluentIcon as FIF
2024-09-09 11:01:40 +08:00
from qfluentwidgets import CommandBar, Action, InfoBar, InfoBarPosition, SubtitleLabel
2024-09-08 20:57:25 +08:00
from .json_edit import CodeEditor
import app.env as env
import os
2024-09-08 15:48:25 +08:00
class SettingWidget(QWidget):
2024-09-09 11:01:40 +08:00
def __init__(self):
2024-09-08 15:48:25 +08:00
super().__init__()
2024-09-09 10:50:37 +08:00
self.base = QVBoxLayout(self)
# 创建水平布局来放置工具栏
bar_layout = QHBoxLayout()
2024-09-09 11:01:40 +08:00
title = SubtitleLabel("设置")
bar_layout.addWidget(title, alignment=Qt.AlignmentFlag.AlignLeft) # 设置工具栏右对齐
2024-09-09 10:50:37 +08:00
bar = CommandBar()
bar.addAction(Action(FIF.SAVE, "保存", triggered=self.save))
bar.addAction(Action(FIF.SYNC, "重新加载", triggered=self.reload))
bar_layout.addWidget(bar, alignment=Qt.AlignmentFlag.AlignRight) # 设置工具栏右对齐
# 将水平布局添加到垂直布局中
self.base.addLayout(bar_layout)
# 加载 JSON 配置
with open(os.path.join(env.dirs.user_config_path, "config.json"), "r") as config_file:
2024-09-09 10:50:37 +08:00
config_data = config_file.read()
self.c1 = CodeEditor(config_data)
# 将代码编辑器添加到垂直布局中
self.base.addWidget(self.c1)
2024-09-08 19:53:00 +08:00
self.setLayout(self.base) # 设置布局
2024-09-09 10:50:37 +08:00
def save(self):
# 保存配置到 JSON 文件
with open("config.json", "w") as config_file:
config_file.write(self.c1.toPlainText())
2024-09-09 11:01:40 +08:00
InfoBar.success(
title='成功',
content="配置文件保存成功",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=2000,
parent=self
)
2024-09-09 10:50:37 +08:00
def reload(self):
# 重新加载 JSON 配置
with open("config.json", "r") as config_file:
config_data = config_file.read()
2024-09-09 11:01:40 +08:00
self.c1.setPlainText(config_data)
InfoBar.success(
title='成功',
content="配置文件加载成功",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=2000,
parent=self
)