197 lines
7.6 KiB
Python
197 lines
7.6 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
Toolbar UI Module for Plugin
|
||
工具栏UI模块 - 负责显示工具栏界面和基础操作
|
||
基本功能:
|
||
- 加载预设
|
||
- 保存预设
|
||
- 导入DNA
|
||
- 导出DNA
|
||
- 创建RL4节点(用于切换DNA编辑的状态)
|
||
- 删除RL4节点(用于切换DNA编辑的状态)
|
||
"""
|
||
|
||
#===================================== IMPORT MODULES =====================================
|
||
from Qt import QtWidgets, QtCore, QtGui
|
||
from Qt.QtCompat import wrapInstance
|
||
from maya import OpenMayaUI as omui
|
||
import sys
|
||
import os
|
||
|
||
#===================================== IMPORT FUNCTIONS ===================================
|
||
from scripts.utils import utils_toolbar as utils_toolbar
|
||
from scripts.ui import ui_utils
|
||
import config
|
||
|
||
# 导入图标路径
|
||
ICONS_PATH = config.ICONS_PATH
|
||
|
||
#========================================== WIDGETS ==========================================
|
||
# 全局变量存储UI控件
|
||
toolbar_buttons = {}
|
||
status_label = None
|
||
|
||
# 按钮图标和提示文本映射
|
||
button_configs = {
|
||
"open_file": {"icon": "open.png", "tooltip": "打开文件"},
|
||
"save_file": {"icon": "save.png", "tooltip": "保存文件"},
|
||
"save_as": {"icon": "save_new.png", "tooltip": "另存为"},
|
||
"load_preset": {"icon": "import_preset.png", "tooltip": "加载预设"},
|
||
"save_preset": {"icon": "export_preset.png", "tooltip": "导出预设"},
|
||
"import_dna": {"icon": "import_dna.png", "tooltip": "导入DNA"},
|
||
"export_dna": {"icon": "export_dna.png", "tooltip": "导出DNA"},
|
||
"create_rl4": {"icon": "create_rl4_node.png", "tooltip": "创建RL4节点"},
|
||
"delete_rl4": {"icon": "delete_rl4_node.png", "tooltip": "删除RL4节点"}
|
||
}
|
||
|
||
def widgets():
|
||
"""
|
||
创建工具栏UI控件
|
||
"""
|
||
global toolbar_buttons, status_label
|
||
|
||
# 创建按钮的辅助函数
|
||
def create_icon_button(button_id, size=24):
|
||
config = button_configs[button_id]
|
||
button = QtWidgets.QPushButton()
|
||
button.setToolTip(config["tooltip"]) # 设置提示文本
|
||
button.setFixedSize(size, size) # 设置固定大小
|
||
# 设置样式类名
|
||
button.setObjectName("ToolbarIconButton")
|
||
|
||
# 设置图标
|
||
icon_path = os.path.abspath(os.path.join(ICONS_PATH, config["icon"]))
|
||
if os.path.exists(icon_path):
|
||
icon = QtGui.QIcon(icon_path)
|
||
if not icon.isNull():
|
||
button.setIcon(icon)
|
||
button.setIconSize(QtCore.QSize(size-4, size-4)) # 图标尺寸稍小于按钮
|
||
print(f"成功设置按钮图标 {button_id}: {icon_path}")
|
||
else:
|
||
print(f"图标加载失败 {button_id}: {icon_path}")
|
||
else:
|
||
print(f"警告: 图标文件未找到 {button_id}: {icon_path}")
|
||
|
||
return button
|
||
|
||
# 创建所有按钮
|
||
# 文件操作按钮
|
||
toolbar_buttons["open_file"] = create_icon_button("open_file")
|
||
toolbar_buttons["save_file"] = create_icon_button("save_file")
|
||
toolbar_buttons["save_as"] = create_icon_button("save_as")
|
||
|
||
# 预设操作按钮
|
||
toolbar_buttons["load_preset"] = create_icon_button("load_preset")
|
||
toolbar_buttons["save_preset"] = create_icon_button("save_preset")
|
||
|
||
# DNA操作按钮
|
||
toolbar_buttons["import_dna"] = create_icon_button("import_dna")
|
||
toolbar_buttons["export_dna"] = create_icon_button("export_dna")
|
||
|
||
# RL4节点操作按钮
|
||
toolbar_buttons["create_rl4"] = create_icon_button("create_rl4")
|
||
toolbar_buttons["delete_rl4"] = create_icon_button("delete_rl4")
|
||
|
||
# 移除DNA下拉菜单
|
||
|
||
# 状态标签
|
||
status_label = QtWidgets.QLabel("就绪")
|
||
status_label.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
|
||
status_label.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
|
||
status_label.setObjectName("StatusLabel")
|
||
# status_label.setStyleSheet("""
|
||
# QLabel {
|
||
# color: #CCCCCC;
|
||
# padding-right: 5px;
|
||
# font-size: 9pt;
|
||
# }
|
||
# """)
|
||
|
||
#========================================== LAYOUTS ==========================================
|
||
def layouts(parent_frame=None):
|
||
"""
|
||
创建工具栏UI布局
|
||
|
||
Args:
|
||
parent_frame: 父容器控件,由Main.py传入
|
||
"""
|
||
# 获取父容器(在Main.py中创建的toolbar_frame)
|
||
if not parent_frame:
|
||
parent_frame = ui_utils.get_parent_widget("toolbar_frame")
|
||
if not parent_frame:
|
||
print("无法获取父容器,布局创建失败")
|
||
return
|
||
|
||
# 创建主布局
|
||
main_layout = parent_frame.layout()
|
||
if not main_layout:
|
||
print("父容器没有布局,布局创建失败")
|
||
return
|
||
|
||
# 创建工具栏布局
|
||
toolbar_layout = QtWidgets.QHBoxLayout()
|
||
toolbar_layout.setContentsMargins(2, 2, 2, 2)
|
||
toolbar_layout.setSpacing(2) # 减小按钮之间的间距
|
||
toolbar_layout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint) # 设置布局约束为默认,允许自适应
|
||
|
||
# 创建分隔符样式
|
||
def create_separator():
|
||
separator = QtWidgets.QFrame()
|
||
separator.setFrameShape(QtWidgets.QFrame.VLine)
|
||
separator.setFrameShadow(QtWidgets.QFrame.Sunken)
|
||
separator.setObjectName("ToolbarSeparator")
|
||
return separator
|
||
|
||
# 添加文件操作按钮
|
||
toolbar_layout.addWidget(toolbar_buttons["open_file"])
|
||
toolbar_layout.addWidget(toolbar_buttons["save_file"])
|
||
toolbar_layout.addWidget(toolbar_buttons["save_as"])
|
||
toolbar_layout.addWidget(create_separator()) # 使用自定义分隔符
|
||
|
||
# 添加预设操作按钮
|
||
toolbar_layout.addWidget(toolbar_buttons["load_preset"])
|
||
toolbar_layout.addWidget(toolbar_buttons["save_preset"])
|
||
toolbar_layout.addWidget(create_separator()) # 使用自定义分隔符
|
||
|
||
# 添加DNA操作按钮
|
||
toolbar_layout.addWidget(toolbar_buttons["import_dna"])
|
||
toolbar_layout.addWidget(toolbar_buttons["export_dna"])
|
||
toolbar_layout.addWidget(create_separator()) # 使用自定义分隔符
|
||
|
||
# 添加RL4节点操作按钮
|
||
toolbar_layout.addWidget(toolbar_buttons["create_rl4"])
|
||
toolbar_layout.addWidget(toolbar_buttons["delete_rl4"])
|
||
toolbar_layout.addStretch()
|
||
toolbar_layout.addWidget(status_label)
|
||
|
||
# 添加到主布局
|
||
main_layout.addLayout(toolbar_layout)
|
||
|
||
#========================================== CONNECTIONS ==========================================
|
||
def connections():
|
||
"""
|
||
连接工具栏UI信号和槽
|
||
"""
|
||
# 文件操作按钮连接
|
||
toolbar_buttons["open_file"].clicked.connect(lambda: print("打开文件功能待实现"))
|
||
toolbar_buttons["save_file"].clicked.connect(lambda: print("保存文件功能待实现"))
|
||
toolbar_buttons["save_as"].clicked.connect(lambda: print("另存为功能待实现"))
|
||
|
||
# 预设操作按钮连接
|
||
toolbar_buttons["load_preset"].clicked.connect(lambda: print("加载预设功能待实现"))
|
||
toolbar_buttons["save_preset"].clicked.connect(lambda: print("导出预设功能待实现"))
|
||
|
||
# DNA操作按钮连接
|
||
toolbar_buttons["import_dna"].clicked.connect(lambda: print("导入DNA功能待实现"))
|
||
toolbar_buttons["export_dna"].clicked.connect(lambda: print("导出DNA功能待实现"))
|
||
|
||
# RL4节点操作按钮连接
|
||
toolbar_buttons["create_rl4"].clicked.connect(lambda: print("创建RL4节点功能待实现"))
|
||
toolbar_buttons["delete_rl4"].clicked.connect(lambda: print("删除RL4节点功能待实现"))
|
||
|
||
#===================================== PLACEHOLDER FUNCTION ===================================
|
||
def toolbar_temp_function():
|
||
return utils_toolbar.toolbar_temp_utils_function()
|