143 lines
5.0 KiB
Python
143 lines
5.0 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
|
||
"""
|
||
UI Utilities Module for Plugin
|
||
UI工具模块 - 提供UI相关的通用函数
|
||
"""
|
||
#========================================= 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
|
||
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
|
||
|
||
#============================================ 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):
|
||
"""
|
||
根据控件名称查找父容器控件
|
||
|
||
Args:
|
||
widget_name (str): 控件名称
|
||
|
||
Returns:
|
||
QWidget: 找到的父容器控件,如果未找到则返回None
|
||
"""
|
||
# 查找主窗口中的所有控件
|
||
main_window = None
|
||
for widget in QtWidgets.QApplication.topLevelWidgets():
|
||
if widget.objectName() == f"{TOOL_NAME}MainWindow" or widget.objectName().endswith("MainWindow"):
|
||
main_window = widget
|
||
break
|
||
|
||
if not main_window:
|
||
print(f"无法找到主窗口,无法获取父容器: {widget_name}")
|
||
return None
|
||
|
||
# 在主窗口中查找指定名称的控件
|
||
found_widget = main_window.findChild(QtWidgets.QWidget, widget_name)
|
||
if found_widget:
|
||
return found_widget
|
||
|
||
# 如果未找到精确匹配,尝试模糊匹配
|
||
for child in main_window.findChildren(QtWidgets.QWidget):
|
||
if widget_name.lower() in child.objectName().lower():
|
||
return child
|
||
|
||
print(f"无法找到控件: {widget_name}")
|
||
return None |