1+ from PyQt5 .QtWidgets import (QDialog , QVBoxLayout , QHBoxLayout , QLabel ,
2+ QProgressBar , QPushButton )
3+ from PyQt5 .QtCore import Qt , QTimer
4+ from assets .utils .clut_button import ClutButton
5+ from PyQt5 .QtWidgets import QWidget
6+
7+ class ClutProgressDialog (QDialog ):
8+ def __init__ (self , parent = None , title = "进度" , can_background = True ):
9+ super ().__init__ (parent )
10+ self .setWindowFlags (Qt .Dialog | Qt .FramelessWindowHint )
11+ self .setAttribute (Qt .WA_TranslucentBackground )
12+ self .setFixedSize (400 , 200 )
13+
14+ # 主布局
15+ main_layout = QVBoxLayout (self )
16+ main_layout .setContentsMargins (0 , 0 , 0 , 0 )
17+
18+ # 创建背景小部件
19+ self .bg_widget = QWidget ()
20+ self .bg_widget .setObjectName ("bg_widget" )
21+ self .bg_widget .setStyleSheet ("""
22+ QWidget#bg_widget {
23+ background: #2d2d2d;
24+ border-radius: 8px;
25+ border: 1px solid rgba(255, 255, 255, 0.1);
26+ }
27+ """ )
28+
29+ # 内容布局
30+ content_layout = QVBoxLayout (self .bg_widget )
31+ content_layout .setContentsMargins (20 , 20 , 20 , 20 )
32+
33+ # 标题
34+ title_layout = QHBoxLayout ()
35+ title_label = QLabel (f"| { title } " )
36+ title_label .setStyleSheet ("""
37+ color: white;
38+ font-size: 16px;
39+ font-weight: bold;
40+ """ )
41+ title_layout .addWidget (title_label )
42+ title_layout .addStretch ()
43+
44+ # 进度信息
45+ self .status_label = QLabel ("准备中..." )
46+ self .status_label .setStyleSheet ("color: white; font-size: 14px;" )
47+
48+ # 速度和进度信息
49+ info_layout = QHBoxLayout ()
50+ self .speed_label = QLabel ("速度: 0 KB/s" )
51+ self .speed_label .setStyleSheet ("color: rgba(255,255,255,0.7); font-size: 12px;" )
52+ self .progress_label = QLabel ("0%" )
53+ self .progress_label .setStyleSheet ("color: rgba(255,255,255,0.7); font-size: 12px;" )
54+ info_layout .addWidget (self .speed_label )
55+ info_layout .addStretch ()
56+ info_layout .addWidget (self .progress_label )
57+
58+ # 进度条
59+ self .progress_bar = QProgressBar ()
60+ self .progress_bar .setStyleSheet ("""
61+ QProgressBar {
62+ background: rgba(255, 255, 255, 0.1);
63+ border: none;
64+ border-radius: 4px;
65+ height: 8px;
66+ text-align: center;
67+ }
68+ QProgressBar::chunk {
69+ background: #4A9EFF;
70+ border-radius: 4px;
71+ }
72+ """ )
73+ self .progress_bar .setTextVisible (False )
74+
75+ # 按钮
76+ button_layout = QHBoxLayout ()
77+ button_layout .addStretch ()
78+
79+ # 创建按钮但先不添加
80+ self .background_button = ClutButton ("后台运行" , primary = False )
81+ self .view_button = ClutButton ("查看" , primary = True )
82+ self .close_button = ClutButton ("关闭" , primary = False )
83+
84+ if can_background :
85+ button_layout .addWidget (self .background_button )
86+ # 默认隐藏其他按钮
87+ self .view_button .hide ()
88+ self .close_button .hide ()
89+ button_layout .addWidget (self .view_button )
90+ button_layout .addWidget (self .close_button )
91+
92+ # 添加所有元素
93+ content_layout .addLayout (title_layout )
94+ content_layout .addWidget (self .status_label )
95+ content_layout .addLayout (info_layout )
96+ content_layout .addWidget (self .progress_bar )
97+ content_layout .addStretch ()
98+ content_layout .addLayout (button_layout )
99+
100+ # 添加背景小部件到主布局
101+ main_layout .addWidget (self .bg_widget )
102+
103+ # 初始化速度计算
104+ self ._last_bytes = 0
105+ self ._speed_timer = QTimer ()
106+ self ._speed_timer .timeout .connect (self ._update_speed )
107+ self ._speed_timer .start (1000 ) # 每秒更新一次
108+
109+ def _update_speed (self ):
110+ """更新下载速度"""
111+ current_bytes = self .progress_bar .value ()
112+ speed = current_bytes - self ._last_bytes
113+ self ._last_bytes = current_bytes
114+
115+ if speed < 1024 :
116+ speed_text = f"{ speed } B/s"
117+ elif speed < 1024 * 1024 :
118+ speed_text = f"{ speed / 1024 :.1f} KB/s"
119+ else :
120+ speed_text = f"{ speed / 1024 / 1024 :.1f} MB/s"
121+
122+ self .speed_label .setText (f"速度: { speed_text } " )
123+
124+ def update_progress (self , value , total ):
125+ """更新进度"""
126+ percentage = int (value / total * 100 )
127+ self .progress_bar .setValue (value )
128+ self .progress_bar .setMaximum (total )
129+ self .progress_label .setText (f"{ percentage } %" )
130+
131+ def set_status (self , text ):
132+ """更新状态文本"""
133+ self .status_label .setText (text )
134+
135+ def on_clone_complete (self ):
136+ """克隆完成时更新UI"""
137+ self .status_label .setText ("克隆完成" )
138+ self .progress_bar .setStyleSheet ("""
139+ QProgressBar {
140+ background: rgba(255, 255, 255, 0.1);
141+ border: none;
142+ border-radius: 4px;
143+ height: 8px;
144+ text-align: center;
145+ }
146+ QProgressBar::chunk {
147+ background: #4CAF50;
148+ border-radius: 4px;
149+ }
150+ """ )
151+ # 切换按钮
152+ self .background_button .hide ()
153+ self .view_button .show ()
154+ self .close_button .show ()
0 commit comments