PCM_Report/splash_screen.py

145 lines
4.7 KiB
Python
Raw Normal View History

2025-12-11 14:32:31 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
启动加载界面
显示程序启动和模板加载进度防止用户误操作
"""
from PySide6.QtWidgets import QWidget, QVBoxLayout, QLabel, QProgressBar
from PySide6.QtCore import Qt, QTimer
from PySide6.QtGui import QPainter, QColor, QFont
class SplashScreen(QWidget):
"""半透明启动加载界面"""
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowFlags(
Qt.WindowStaysOnTopHint |
Qt.FramelessWindowHint |
Qt.Tool
)
self.setAttribute(Qt.WA_TranslucentBackground)
# 设置窗口大小
self.setFixedSize(500, 300)
# 主布局
layout = QVBoxLayout()
layout.setContentsMargins(40, 40, 40, 40)
layout.setSpacing(20)
# 标题
self.title_label = QLabel("Docx 报告生成器")
self.title_label.setAlignment(Qt.AlignCenter)
title_font = QFont()
title_font.setPointSize(20)
title_font.setBold(True)
self.title_label.setFont(title_font)
self.title_label.setStyleSheet("color: white;")
# 状态标签
self.status_label = QLabel("正在启动...")
self.status_label.setAlignment(Qt.AlignCenter)
status_font = QFont()
status_font.setPointSize(12)
self.status_label.setFont(status_font)
self.status_label.setStyleSheet("color: #E0E0E0;")
# 进度条
self.progress_bar = QProgressBar()
self.progress_bar.setMinimum(0)
self.progress_bar.setMaximum(100)
self.progress_bar.setValue(0)
self.progress_bar.setTextVisible(True)
self.progress_bar.setStyleSheet("""
QProgressBar {
border: 2px solid #555;
border-radius: 8px;
background-color: rgba(255, 255, 255, 0.1);
text-align: center;
color: white;
font-size: 11px;
height: 25px;
}
QProgressBar::chunk {
background-color: qlineargradient(
x1:0, y1:0, x2:1, y2:0,
stop:0 #4A90E2,
stop:1 #357ABD
);
border-radius: 6px;
}
""")
# 详细信息标签
self.detail_label = QLabel("")
self.detail_label.setAlignment(Qt.AlignCenter)
detail_font = QFont()
detail_font.setPointSize(9)
self.detail_label.setFont(detail_font)
self.detail_label.setStyleSheet("color: #B0B0B0;")
self.detail_label.setWordWrap(True)
# 添加到布局
layout.addStretch(1)
layout.addWidget(self.title_label)
layout.addSpacing(10)
layout.addWidget(self.status_label)
layout.addWidget(self.progress_bar)
layout.addWidget(self.detail_label)
layout.addStretch(1)
self.setLayout(layout)
# 居中显示
self._center_on_screen()
def _center_on_screen(self):
"""将窗口居中显示"""
from PySide6.QtWidgets import QApplication
screen = QApplication.primaryScreen().geometry()
x = (screen.width() - self.width()) // 2
y = (screen.height() - self.height()) // 2
self.move(x, y)
def paintEvent(self, event):
"""绘制半透明背景"""
painter = QPainter(self)
try:
painter.setRenderHint(QPainter.Antialiasing)
# 绘制圆角矩形背景
painter.setBrush(QColor(40, 40, 40, 230)) # 深灰色90%不透明
painter.setPen(QColor(80, 80, 80, 255))
painter.drawRoundedRect(0, 0, self.width(), self.height(), 15, 15)
finally:
painter.end() # 确保 painter 正确结束
def update_status(self, message: str, progress: int = None, detail: str = ""):
"""
更新加载状态
Args:
message: 主要状态信息
progress: 进度值 (0-100)None表示不更新
detail: 详细信息
"""
self.status_label.setText(message)
if progress is not None:
self.progress_bar.setValue(progress)
if detail:
self.detail_label.setText(detail)
# 强制刷新界面
QApplication.processEvents()
def finish(self):
"""完成加载,关闭启动界面"""
self.update_status("加载完成", 100)
QTimer.singleShot(300, self.close) # 延迟300ms后关闭
# 导入QApplication以便在update_status中使用
from PySide6.QtWidgets import QApplication