This commit implements the comprehensive UI code refactoring plan to improve code quality, reduce duplication, and enhance maintainability. Phase 1: High Priority Improvements - Create BaseDialog class to eliminate window centering code duplication across ProgressDialog, DeleteProgressWindow, LoginDialog, and UserSelectionDialog (~30 lines of duplicate code removed) - Unify log color configuration with LogTheme class in gui/log_config.py - Create permission check decorators (@require_admin, @require_permission, @require_user_type, @handle_errors) - Update all dialog classes to use unified patterns Phase 2: Architecture Improvements - Create input validation framework (ValidationResult, Validator, ValidatedWidget classes) - Implement StateManager with observer pattern for component state sharing - Create unified ErrorHandler for consistent error handling - Extract CheckboxTreeview into reusable component New Modules: - gui/widgets/base_dialog.py - Base dialog class with modal setup and centering - gui/utils/decorators.py - Permission and error handling decorators - gui/utils/error_handler.py - Unified error handling with user-friendly messages - gui/utils/state_manager.py - State management with observer pattern - gui/utils/validators.py - Input validation framework - gui/material_validation/checkbox_treeview.py - Reusable checkbox table Modified Files: - gui/log_config.py - Added LogTheme class for centralized styling - gui/widgets/log_text.py - Use LogTheme.COLORS - gui/widgets/progress_dialog.py - Inherit from BaseDialog - gui/widgets/delete_progress_window.py - Inherit from BaseDialog, use LogTheme - gui/widgets/__init__.py - Add new exports, optional imports - gui/login_dialog.py - Use unified centering pattern - gui/user_selection_dialog.py - Use unified centering pattern - gui/material_validation_tab.py - Import CheckboxTreeview from new module Benefits: - Reduced code duplication by ~200 lines - Improved maintainability through centralized configuration - Better abstraction with 5 new reusable base classes - Enhanced type safety with type hints - Future-proof theming support via LogTheme Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
129 lines
3.3 KiB
Python
129 lines
3.3 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
进度对话框组件
|
|
|
|
用于显示长时间运行操作的进度。
|
|
"""
|
|
|
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
from typing import Optional, Callable
|
|
from gui.widgets.base_dialog import BaseDialog
|
|
|
|
|
|
class ProgressDialog(BaseDialog):
|
|
"""进度对话框"""
|
|
|
|
def __init__(
|
|
self,
|
|
parent,
|
|
title: str = "处理中...",
|
|
message: str = "请稍候",
|
|
can_cancel: bool = True,
|
|
on_cancel: Optional[Callable] = None
|
|
):
|
|
"""
|
|
初始化进度对话框
|
|
|
|
Args:
|
|
parent: 父窗口
|
|
title: 对话框标题
|
|
message: 显示消息
|
|
can_cancel: 是否可以取消
|
|
on_cancel: 取消回调函数
|
|
"""
|
|
self.can_cancel = can_cancel
|
|
self.on_cancel = on_cancel
|
|
self.cancelled = False
|
|
self.message_label = None
|
|
self.progress = None
|
|
self.cancel_button = None
|
|
|
|
# 调用父类初始化(会自动设置为模态并居中)
|
|
super().__init__(parent, title)
|
|
|
|
# 设置固定大小
|
|
self.resizable(False, False)
|
|
|
|
# 创建内容
|
|
self._create_widgets(message)
|
|
|
|
# 设置固定大小并重新居中
|
|
self._set_fixed_size(400, 150)
|
|
|
|
def _set_fixed_size(self, width: int, height: int):
|
|
"""
|
|
设置固定大小并重新居中
|
|
|
|
Args:
|
|
width: 宽度
|
|
height: 高度
|
|
"""
|
|
self.update_idletasks()
|
|
self.geometry(f"{width}x{height}")
|
|
self._center_window()
|
|
|
|
def _create_widgets(self, message: str):
|
|
"""创建对话框组件"""
|
|
# 消息标签
|
|
self.message_label = ttk.Label(self, text=message, wraplength=380)
|
|
self.message_label.pack(pady=(20, 10), padx=20)
|
|
|
|
# 进度条
|
|
self.progress = ttk.Progressbar(
|
|
self,
|
|
mode='indeterminate',
|
|
length=360
|
|
)
|
|
self.progress.pack(pady=10, padx=20)
|
|
self.progress.start(10)
|
|
|
|
# 取消按钮
|
|
if self.can_cancel:
|
|
button_frame = ttk.Frame(self)
|
|
button_frame.pack(pady=10)
|
|
|
|
self.cancel_button = ttk.Button(
|
|
button_frame,
|
|
text="取消",
|
|
command=self._on_cancel
|
|
)
|
|
self.cancel_button.pack()
|
|
|
|
def _on_cancel(self):
|
|
"""处理取消操作"""
|
|
self.cancelled = True
|
|
if self.on_cancel:
|
|
self.on_cancel()
|
|
self.close()
|
|
|
|
def update_message(self, message: str):
|
|
"""更新显示消息"""
|
|
if self.message_label:
|
|
self.message_label.config(text=message)
|
|
self.update_idletasks()
|
|
|
|
def set_progress(self, value: int, maximum: int = 100):
|
|
"""
|
|
设置进度值
|
|
|
|
Args:
|
|
value: 当前进度值
|
|
maximum: 最大值
|
|
"""
|
|
if self.progress:
|
|
self.progress.config(mode='determinate', maximum=maximum)
|
|
self.progress['value'] = value
|
|
self.update_idletasks()
|
|
|
|
def close(self):
|
|
"""关闭对话框"""
|
|
if self.progress:
|
|
self.progress.stop()
|
|
super().close()
|
|
|
|
def is_cancelled(self) -> bool:
|
|
"""检查是否已取消"""
|
|
return self.cancelled
|