This commit is contained in:
2025-05-05 18:15:02 +08:00
parent 01c0ec760f
commit 46d93efb3c
11 changed files with 1728 additions and 1943 deletions

View File

@@ -5,17 +5,108 @@
UI Utilities Module for Plugin
UI工具模块 - 提供UI相关的通用函数
"""
#===================================== IMPORT MODULES =====================================
#========================================= IMPORT =========================================
from Qt import QtWidgets, QtCore, QtGui
from Qt.QtCompat import wrapInstance
from maya import OpenMayaUI as omui
import maya.cmds as cmds
import maya.mel as mel
import maya.utils as utils
import webbrowser
import subprocess
import importlib
import traceback
import locale
import sys
import os
#========================================== CONFIG ========================================
import config
#===================================== UI UTILITY FUNCTIONS ===================================
TOOL_NAME = config.TOOL_NAME
TOOL_VERSION = config.TOOL_VERSION
TOOL_AUTHOR = config.TOOL_AUTHOR
TOOL_YEAR = config.TOOL_YEAR
TOOL_MOD_FILENAME = config.TOOL_MOD_FILENAME
TOOL_LANG = config.TOOL_LANG
TOOL_WSCL_NAME = config.TOOL_WSCL_NAME
TOOL_HELP_URL = config.TOOL_HELP_URL
TOOL_PATH = config.TOOL_PATH
SCRIPTS_PATH = config.SCRIPTS_PATH
TOOL_MAIN_SCRIPT = config.TOOL_MAIN_SCRIPT
UI_PATH = config.UI_PATH
STYLE_FILE = config.STYLE_FILE
ICONS_PATH = config.ICONS_PATH
TOOL_ICON = config.TOOL_ICON
ASSETS_PATH = config.ASSETS_PATH
DNA_FILE_PATH = config.DNA_FILE_PATH
DNA_IMG_PATH = config.DNA_IMG_PATH
TOOL_COMMAND_ICON = config.TOOL_COMMAND_ICON
#========================================= LOCATION =======================================
from scripts.ui import localization
LANG = localization.LANG
TOOL_NAME = config.TOOL_NAME
#============================================ INIT ==========================================
# 定义窗口大小变化事件处理类
# 用于监听窗口大小变化并调整分割器宽度
class SplitterResizeHandler(QtCore.QObject):
def __init__(self, parent_tab, main_splitter, is_horizontal=True):
super(SplitterResizeHandler, self).__init__(parent_tab)
self.parent_tab = parent_tab
self.main_splitter = main_splitter
self.is_horizontal = is_horizontal # 是否为水平分割器
# 安装事件过滤器到父标签页
self.parent_tab.installEventFilter(self)
def eventFilter(self, obj, event):
# 监听大小变化事件
if obj == self.parent_tab and event.type() == QtCore.QEvent.Resize:
# 当窗口大小变化时,调整分割器宽度
self.adjustSplitterSizes()
return super(SplitterResizeHandler, self).eventFilter(obj, event)
def adjustSplitterSizes(self):
# 确保分割器存在且父标签页可见
if self.main_splitter and self.parent_tab.isVisible():
if self.is_horizontal:
# 获取父标签页当前宽度
width = self.parent_tab.width()
# 设置分割器左右宽度均等
self.main_splitter.setSizes([width // 2, width // 2])
print(f"分割器 - 窗口大小变化,调整水平分割器宽度为: {width // 2}, {width // 2}")
else:
# 获取父标签页当前高度
height = self.parent_tab.height()
# 设置分割器上下比例为3:1
self.main_splitter.setSizes([height * 3 // 4, height // 4])
print(f"分割器 - 窗口大小变化,调整垂直分割器高度为: {height * 3 // 4}, {height // 4}")
# 使用类来管理UI控件避免全局变量
class BaseUI:
"""
基础UI类 - 管理所有UI控件
使用字典存储控件、按钮、布局和分割器引用,避免全局变量
"""
def __init__(self):
# UI控件字典
self.controls = {}
# 按钮字典
self.buttons = {}
# 布局字典
self.layouts = {}
# 分割器字典
self.splitters = {}
# 分割器大小处理器字典
self.resize_handlers = {}
def get_maya_main_window():
"""
获取Maya主窗口
Returns:
QWidget: Maya主窗口控件
"""
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(int(main_window_ptr), QtWidgets.QWidget)
def get_parent_widget(widget_name):
"""
@@ -49,14 +140,4 @@ def get_parent_widget(widget_name):
return child
print(f"无法找到控件: {widget_name}")
return None
def get_maya_main_window():
"""
获取Maya主窗口
Returns:
QWidget: Maya主窗口控件
"""
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(int(main_window_ptr), QtWidgets.QWidget)
return None