329 lines
12 KiB
Python
329 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
项目设置页面组件
|
|
|
|
这个页面允许用户配置项目相关的设置,包括:
|
|
- 默认项目创建位置
|
|
- 项目模板设置
|
|
- 项目命名规则
|
|
- 其他项目相关配置
|
|
"""
|
|
|
|
import os
|
|
from PyQt5.QtWidgets import *
|
|
from PyQt5.QtCore import *
|
|
from PyQt5.QtGui import *
|
|
|
|
|
|
class ProjectSettingsPage(QWidget):
|
|
"""
|
|
项目设置页面
|
|
|
|
提供项目相关的配置选项,主要包括默认项目位置的设置。
|
|
"""
|
|
|
|
# 信号定义
|
|
settings_changed = pyqtSignal() # 设置发生变化时发出
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.settings = QSettings("MetaCore", "ProjectManager") # 用于保存设置
|
|
self.init_ui()
|
|
self.load_settings()
|
|
|
|
def init_ui(self):
|
|
"""初始化用户界面"""
|
|
layout = QVBoxLayout(self)
|
|
layout.setContentsMargins(30, 30, 30, 30)
|
|
layout.setSpacing(20)
|
|
|
|
# 页面标题
|
|
self.create_page_header(layout)
|
|
|
|
# 默认项目位置设置
|
|
self.create_default_location_section(layout)
|
|
|
|
self.create_open_location_section(layout)
|
|
|
|
# 项目创建设置
|
|
# self.create_project_creation_section(layout)
|
|
|
|
# 按钮区域
|
|
self.create_button_section(layout)
|
|
|
|
# 添加弹性空间
|
|
layout.addStretch()
|
|
|
|
def create_page_header(self, layout):
|
|
"""创建页面标题"""
|
|
header_widget = QWidget()
|
|
header_layout = QVBoxLayout(header_widget)
|
|
header_layout.setContentsMargins(0, 0, 0, 0)
|
|
header_layout.setSpacing(8)
|
|
|
|
# 主标题
|
|
title_label = QLabel("项目设置")
|
|
title_label.setObjectName("pageTitle")
|
|
header_layout.addWidget(title_label)
|
|
|
|
# 副标题
|
|
subtitle_label = QLabel("配置项目创建和管理的相关设置")
|
|
subtitle_label.setObjectName("pageSubtitle")
|
|
header_layout.addWidget(subtitle_label)
|
|
|
|
layout.addWidget(header_widget)
|
|
|
|
def create_default_location_section(self, layout):
|
|
"""创建默认项目位置设置区域"""
|
|
# 分组框
|
|
group_box = QGroupBox("默认项目位置")
|
|
group_box.setObjectName("settingsGroup")
|
|
group_layout = QVBoxLayout(group_box)
|
|
group_layout.setSpacing(15)
|
|
|
|
# 说明文字
|
|
desc_label = QLabel("设置新项目的默认创建位置。")
|
|
desc_label.setObjectName("settingsDescription")
|
|
desc_label.setWordWrap(True)
|
|
group_layout.addWidget(desc_label)
|
|
|
|
# 当前路径显示和选择
|
|
path_widget = QWidget()
|
|
path_layout = QHBoxLayout(path_widget)
|
|
path_layout.setContentsMargins(0, 0, 0, 0)
|
|
path_layout.setSpacing(10)
|
|
|
|
# 路径输入框
|
|
self.path_input = QLineEdit()
|
|
self.path_input.setObjectName("pathInput")
|
|
self.path_input.setPlaceholderText("选择默认项目创建位置...")
|
|
self.path_input.setReadOnly(True) # 只读,通过按钮选择
|
|
path_layout.addWidget(self.path_input)
|
|
|
|
# 浏览按钮
|
|
self.browse_btn = QPushButton("浏览...")
|
|
self.browse_btn.setObjectName("browseBtn")
|
|
self.browse_btn.clicked.connect(self.browse_default_location)
|
|
path_layout.addWidget(self.browse_btn)
|
|
|
|
# 重置按钮
|
|
self.reset_btn = QPushButton("重置")
|
|
self.reset_btn.setObjectName("resetBtn")
|
|
self.reset_btn.clicked.connect(self.reset_default_location)
|
|
path_layout.addWidget(self.reset_btn)
|
|
|
|
group_layout.addWidget(path_widget)
|
|
|
|
layout.addWidget(group_box)
|
|
|
|
def create_open_location_section(self, layout):
|
|
"""创建打开位置设置区域"""
|
|
# 分组框
|
|
group_box = QGroupBox("打开位置")
|
|
group_box.setObjectName("settingsGroup")
|
|
group_layout = QVBoxLayout(group_box)
|
|
group_layout.setSpacing(15)
|
|
|
|
# 说明文字
|
|
desc_label = QLabel("设置项目文件的默认打开位置。")
|
|
desc_label.setObjectName("settingsDescription")
|
|
desc_label.setWordWrap(True)
|
|
group_layout.addWidget(desc_label)
|
|
|
|
# 当前路径显示和选择
|
|
path_widget = QWidget()
|
|
path_layout = QHBoxLayout(path_widget)
|
|
path_layout.setContentsMargins(0, 0, 0, 0)
|
|
path_layout.setSpacing(10)
|
|
|
|
# 路径输入框
|
|
self.open_path_input = QLineEdit()
|
|
self.open_path_input.setObjectName("pathInput")
|
|
self.open_path_input.setPlaceholderText("选择默认项目打开位置...")
|
|
self.open_path_input.setReadOnly(True) # 只读,通过按钮选择
|
|
path_layout.addWidget(self.open_path_input)
|
|
|
|
# 浏览按钮
|
|
self.open_browse_btn = QPushButton("浏览...")
|
|
self.open_browse_btn.setObjectName("browseBtn")
|
|
self.open_browse_btn.clicked.connect(self.browse_open_location)
|
|
path_layout.addWidget(self.open_browse_btn)
|
|
|
|
# 重置按钮
|
|
self.open_reset_btn = QPushButton("重置")
|
|
self.open_reset_btn.setObjectName("resetBtn")
|
|
self.open_reset_btn.clicked.connect(self.reset_open_location)
|
|
path_layout.addWidget(self.open_reset_btn)
|
|
|
|
group_layout.addWidget(path_widget)
|
|
|
|
layout.addWidget(group_box)
|
|
|
|
def browse_open_location(self):
|
|
"""浏览选择打开位置"""
|
|
current_path = self.open_path_input.text() or self.get_default_projects_path()
|
|
|
|
folder = QFileDialog.getExistingDirectory(
|
|
self,
|
|
"选择默认项目打开位置",
|
|
current_path
|
|
)
|
|
|
|
if folder:
|
|
self.open_path_input.setText(folder)
|
|
# 可以根据需要添加保存逻辑
|
|
self.save_settings_immediately("default_open_location", self.open_path_input.text())
|
|
|
|
def reset_open_location(self):
|
|
"""重置为默认打开位置"""
|
|
default_path = self.get_default_projects_path()
|
|
self.open_path_input.setText(default_path)
|
|
|
|
def create_project_creation_section(self, layout):
|
|
"""创建项目创建设置区域"""
|
|
# 分组框
|
|
group_box = QGroupBox("项目创建设置")
|
|
group_box.setObjectName("settingsGroup")
|
|
group_layout = QVBoxLayout(group_box)
|
|
group_layout.setSpacing(15)
|
|
|
|
# 自动打开项目文件夹选项
|
|
self.auto_open_checkbox = QCheckBox("创建项目后自动打开项目文件夹")
|
|
self.auto_open_checkbox.setObjectName("settingsCheckbox")
|
|
group_layout.addWidget(self.auto_open_checkbox)
|
|
|
|
# 创建README文件选项
|
|
self.create_readme_checkbox = QCheckBox("自动创建 README.md 文件")
|
|
self.create_readme_checkbox.setObjectName("settingsCheckbox")
|
|
self.create_readme_checkbox.setChecked(True) # 默认选中
|
|
group_layout.addWidget(self.create_readme_checkbox)
|
|
|
|
# 创建.gitignore文件选项
|
|
self.create_gitignore_checkbox = QCheckBox("自动创建 .gitignore 文件")
|
|
self.create_gitignore_checkbox.setObjectName("settingsCheckbox")
|
|
group_layout.addWidget(self.create_gitignore_checkbox)
|
|
|
|
layout.addWidget(group_box)
|
|
|
|
def create_button_section(self, layout):
|
|
"""创建按钮区域(已移除,路径选择后自动保存)"""
|
|
# 不再需要按钮区域,路径选择后自动保存
|
|
pass
|
|
|
|
def get_default_projects_path(self):
|
|
"""获取默认项目目录路径"""
|
|
# 获取当前文件所在目录的上级目录(项目根目录)
|
|
# current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
# return os.path.join(current_dir, "MetaCore_Projects")
|
|
current_dir = ''
|
|
return current_dir
|
|
|
|
def browse_default_location(self):
|
|
"""浏览选择默认位置"""
|
|
current_path = self.path_input.text() or self.get_default_projects_path()
|
|
|
|
folder = QFileDialog.getExistingDirectory(
|
|
self,
|
|
"选择默认项目创建位置",
|
|
current_path
|
|
)
|
|
|
|
if folder:
|
|
self.path_input.setText(folder)
|
|
# 路径选择后立即保存设置
|
|
self.save_settings_immediately("default_project_location", self.path_input.text())
|
|
|
|
def reset_default_location(self):
|
|
"""重置为默认位置"""
|
|
default_path = self.get_default_projects_path()
|
|
self.path_input.setText(default_path)
|
|
|
|
def load_settings(self):
|
|
"""加载保存的设置"""
|
|
# 加载默认项目位置
|
|
default_path = self.settings.value(
|
|
"default_project_location",
|
|
self.get_default_projects_path()
|
|
)
|
|
self.path_input.setText(default_path)
|
|
|
|
default_open_path = self.settings.value(
|
|
"default_open_location",
|
|
self.get_default_projects_path()
|
|
)
|
|
self.open_path_input.setText(default_open_path)
|
|
|
|
# 加载其他设置
|
|
# self.auto_open_checkbox.setChecked(
|
|
# self.settings.value("auto_open_folder", False, type=bool)
|
|
# )
|
|
# self.create_readme_checkbox.setChecked(
|
|
# self.settings.value("create_readme", True, type=bool)
|
|
# )
|
|
# self.create_gitignore_checkbox.setChecked(
|
|
# self.settings.value("create_gitignore", False, type=bool)
|
|
# )
|
|
|
|
def save_settings_immediately(self, key, value):
|
|
"""立即保存设置(路径选择后自动调用)"""
|
|
# 保存默认项目位置
|
|
self.settings.setValue(key, value)
|
|
|
|
# 确保设置被保存
|
|
self.settings.sync()
|
|
|
|
# 发出设置变化信号
|
|
self.settings_changed.emit()
|
|
|
|
# 显示简短的成功提示(可选)
|
|
# QMessageBox.information(self, "设置已保存", "默认项目位置已更新!")
|
|
|
|
# def apply_settings(self):
|
|
# """应用设置(保留方法以防其他地方调用)"""
|
|
# self.save_settings_immediately()
|
|
#
|
|
# # 显示成功消息
|
|
# QMessageBox.information(self, "设置已保存", "项目设置已成功保存!")
|
|
#
|
|
# def restore_defaults(self):
|
|
# """恢复默认设置(已移除按钮,保留方法以防其他地方调用)"""
|
|
# reply = QMessageBox.question(
|
|
# self,
|
|
# "恢复默认设置",
|
|
# "确定要恢复所有设置为默认值吗?",
|
|
# QMessageBox.Yes | QMessageBox.No,
|
|
# QMessageBox.No
|
|
# )
|
|
#
|
|
# if reply == QMessageBox.Yes:
|
|
# # 恢复默认值
|
|
# self.path_input.setText(self.get_default_projects_path())
|
|
#
|
|
# # 立即保存恢复的设置
|
|
# self.save_settings_immediately()
|
|
#
|
|
# QMessageBox.information(self, "恢复完成", "已恢复默认设置!")
|
|
|
|
def get_default_project_location(self):
|
|
"""获取默认项目位置"""
|
|
return self.settings.value(
|
|
"default_project_location",
|
|
self.get_default_projects_path()
|
|
)
|
|
|
|
def get_default_open_location(self):
|
|
"""获取默认项目位置"""
|
|
return self.settings.value(
|
|
"default_open_location",
|
|
self.get_default_projects_path()
|
|
)
|
|
|
|
# def get_project_creation_settings(self):
|
|
# """获取项目创建设置"""
|
|
# return {
|
|
# 'auto_open_folder': self.settings.value("auto_open_folder", False, type=bool),
|
|
# 'create_readme': self.settings.value("create_readme", True, type=bool),
|
|
# 'create_gitignore': self.settings.value("create_gitignore", False, type=bool),
|
|
# }
|