MetaCore-startup/MetaCore/ui/icon_manager.py
陈横 fd3eb3e72a feat(ui): 优化项目导入和创建对话框界面
- 移除了创建项目对话框的最大尺寸限制,提升布局灵活性
- 调整了导入项目对话框中文件列表项的样式和结构
- 更新了图标管理器,新增 import_file_list 图标支持
- 修改了文件列表项组件,使用自定义图标并优化布局间距
- 调整了样式表中多个组件的尺寸和颜色,改善视觉效果
- 优化了文件列表项的显示逻辑,修复了可能的布局问题
- 更新了项目预览图片路径,确保正确显示项目封面
2025-10-23 18:56:28 +08:00

228 lines
6.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
图标管理器
统一管理应用程序中使用的所有图标
"""
import os
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import QSize
class IconManager:
"""图标管理器"""
# 图标目录路径
# ICONS_DIR = "Resources/Icons"
ICONS_DIR = os.path.join(os.path.dirname(__file__), "..", "Resources", "Icons")
# 图标文件映射
ICON_FILES = {
# 应用程序图标
'app': 'app_icon.png',
'logo': 'logo.png',
# 操作图标
'create': 'create.png',
'import': 'import.png',
'folder': 'folder.png',
'delete': 'delete.png',
'Refresh': 'Refresh.png',
# 视图图标
'grid_view': 'grid_view.png',
'list_view': 'list_view.png',
# 导航图标
'overview': 'overview.png',
'management': 'management.png',
'resource_category': 'category.png',
'resource_management': 'resource.png',
'project_settings': 'settings.png',
'system_settings': 'system.png',
# 树箭头
'down': 'solid_down_arrows.png',
'right': 'solid_right_arrows.png',
'search': 'search_icon.png',
'infomation': 'infomation_icon.png',
'infomation_hover': 'infomation_hover_icon.png',
'infomation_check': 'infomation_check_icon.png',
# 为获取项目图片
'project_empty_icon': 'project_empty_icon.png',
# 卡片右键菜单
'refresh_projectcard': 'refresh_projectcard_icon.png',
'open_projectcard': 'open_projectcard_icon.png',
'remove_projectcard': 'remove_projectcard_icon.png',
# 导入对话框
'import_file': 'import_file_icon.png',
'import_file_list': 'import_file_list_icon.png',
# 创建项目浏览图标
'file_icon': 'file_icon.png',
'empty_folder_icon': 'empty_folder_icon.png',
'close_bt_icon': 'close_bt_icon.png',
# 弹窗图标
'success_icon': 'success_icon.png',
'warning_icon': 'warning_icon.png',
'fail_icon': 'delete_fail_icon.png',
}
# 图标缓存
_icon_cache = {}
@classmethod
def get_icon(cls, icon_name: str, size: QSize = None) -> QIcon:
"""
获取图标
Args:
icon_name (str): 图标名称
size (QSize, optional): 图标尺寸
Returns:
QIcon: 图标对象,如果文件不存在则返回空图标
"""
# 检查缓存
cache_key = f"{icon_name}_{size.width() if size else 'default'}_{size.height() if size else 'default'}"
if cache_key in cls._icon_cache:
return cls._icon_cache[cache_key]
# 获取图标文件路径
icon_path = cls.get_icon_path(icon_name)
if not icon_path or not os.path.exists(icon_path):
# 如果图标文件不存在,返回空图标
icon = QIcon()
else:
# 创建图标
if size:
pixmap = QPixmap(icon_path).scaled(size, aspectRatioMode=1, transformMode=1)
icon = QIcon(pixmap)
else:
icon = QIcon(icon_path)
# 缓存图标
cls._icon_cache[cache_key] = icon
return icon
@classmethod
def get_icon_path(cls, icon_name: str) -> str:
"""
获取图标文件路径
Args:
icon_name (str): 图标名称
Returns:
str: 图标文件的完整路径,如果不存在则返回空字符串
"""
if icon_name not in cls.ICON_FILES:
return ""
icon_file = cls.ICON_FILES[icon_name]
icon_path = os.path.join(cls.ICONS_DIR, icon_file)
return icon_path
@classmethod
def get_pixmap(cls, icon_name: str, size: QSize = None) -> QPixmap:
"""
获取图标的 QPixmap 对象
Args:
icon_name (str): 图标名称
size (QSize, optional): 图标尺寸
Returns:
QPixmap: 图标的 QPixmap 对象
"""
icon_path = cls.get_icon_path(icon_name)
if not icon_path or not os.path.exists(icon_path):
return QPixmap()
pixmap = QPixmap(icon_path)
if size:
pixmap = pixmap.scaled(size, aspectRatioMode=1, transformMode=1)
return pixmap
@classmethod
def icon_exists(cls, icon_name: str) -> bool:
"""
检查图标是否存在
Args:
icon_name (str): 图标名称
Returns:
bool: 图标文件是否存在
"""
icon_path = cls.get_icon_path(icon_name)
print("icon_path", icon_path)
print("os.path.exists(icon_path)", os.path.exists(icon_path))
return icon_path and os.path.exists(icon_path)
@classmethod
def get_project_type_icon(cls, project_type: str) -> QIcon:
"""
根据项目类型获取对应图标
Args:
project_type (str): 项目类型
Returns:
QIcon: 项目类型对应的图标
"""
icon_name = f"project_{project_type}"
if icon_name in cls.ICON_FILES:
return cls.get_icon(icon_name)
else:
# 如果没有对应的项目类型图标,返回默认项目图标
return cls.get_icon('project_empty')
@classmethod
def clear_cache(cls):
"""清空图标缓存"""
cls._icon_cache.clear()
@classmethod
def ensure_icons_directory(cls):
"""确保图标目录存在"""
if not os.path.exists(cls.ICONS_DIR):
os.makedirs(cls.ICONS_DIR, exist_ok=True)
@classmethod
def get_available_icons(cls) -> list:
"""
获取所有可用的图标列表
Returns:
list: 可用图标名称列表
"""
available_icons = []
for icon_name in cls.ICON_FILES:
if cls.icon_exists(icon_name):
available_icons.append(icon_name)
return available_icons
@classmethod
def get_missing_icons(cls) -> list:
"""
获取缺失的图标列表
Returns:
list: 缺失图标名称列表
"""
missing_icons = []
for icon_name in cls.ICON_FILES:
if not cls.icon_exists(icon_name):
missing_icons.append(icon_name)
return missing_icons