refactor: implement UI code optimization and refactoring
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>
This commit is contained in:
@@ -9,9 +9,10 @@
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
from typing import Optional, Callable
|
||||
from gui.widgets.base_dialog import BaseDialog
|
||||
|
||||
|
||||
class ProgressDialog:
|
||||
class ProgressDialog(BaseDialog):
|
||||
"""进度对话框"""
|
||||
|
||||
def __init__(
|
||||
@@ -32,42 +33,46 @@ class ProgressDialog:
|
||||
can_cancel: 是否可以取消
|
||||
on_cancel: 取消回调函数
|
||||
"""
|
||||
self.parent = parent
|
||||
self.can_cancel = can_cancel
|
||||
self.on_cancel = on_cancel
|
||||
self.cancelled = False
|
||||
self.message_label = None
|
||||
self.progress = None
|
||||
self.cancel_button = None
|
||||
|
||||
# 创建对话框
|
||||
self.dialog = tk.Toplevel(parent)
|
||||
self.dialog.title(title)
|
||||
self.dialog.resizable(False, False)
|
||||
self.dialog.transient(parent)
|
||||
self.dialog.grab_set()
|
||||
# 调用父类初始化(会自动设置为模态并居中)
|
||||
super().__init__(parent, title)
|
||||
|
||||
# 居中显示
|
||||
self._center()
|
||||
# 设置固定大小
|
||||
self.resizable(False, False)
|
||||
|
||||
# 创建内容
|
||||
self._create_widgets(message)
|
||||
|
||||
def _center(self):
|
||||
"""将对话框居中显示"""
|
||||
self.dialog.update_idletasks()
|
||||
width = 400
|
||||
height = 150
|
||||
x = (self.dialog.winfo_screenwidth() // 2) - (width // 2)
|
||||
y = (self.dialog.winfo_screenheight() // 2) - (height // 2)
|
||||
self.dialog.geometry(f"{width}x{height}+{x}+{y}")
|
||||
# 设置固定大小并重新居中
|
||||
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.dialog, text=message, wraplength=380)
|
||||
self.message_label = ttk.Label(self, text=message, wraplength=380)
|
||||
self.message_label.pack(pady=(20, 10), padx=20)
|
||||
|
||||
# 进度条
|
||||
self.progress = ttk.Progressbar(
|
||||
self.dialog,
|
||||
self,
|
||||
mode='indeterminate',
|
||||
length=360
|
||||
)
|
||||
@@ -76,7 +81,7 @@ class ProgressDialog:
|
||||
|
||||
# 取消按钮
|
||||
if self.can_cancel:
|
||||
button_frame = ttk.Frame(self.dialog)
|
||||
button_frame = ttk.Frame(self)
|
||||
button_frame.pack(pady=10)
|
||||
|
||||
self.cancel_button = ttk.Button(
|
||||
@@ -95,8 +100,9 @@ class ProgressDialog:
|
||||
|
||||
def update_message(self, message: str):
|
||||
"""更新显示消息"""
|
||||
self.message_label.config(text=message)
|
||||
self.dialog.update_idletasks()
|
||||
if self.message_label:
|
||||
self.message_label.config(text=message)
|
||||
self.update_idletasks()
|
||||
|
||||
def set_progress(self, value: int, maximum: int = 100):
|
||||
"""
|
||||
@@ -106,14 +112,16 @@ class ProgressDialog:
|
||||
value: 当前进度值
|
||||
maximum: 最大值
|
||||
"""
|
||||
self.progress.config(mode='determinate', maximum=maximum)
|
||||
self.progress['value'] = value
|
||||
self.dialog.update_idletasks()
|
||||
if self.progress:
|
||||
self.progress.config(mode='determinate', maximum=maximum)
|
||||
self.progress['value'] = value
|
||||
self.update_idletasks()
|
||||
|
||||
def close(self):
|
||||
"""关闭对话框"""
|
||||
self.progress.stop()
|
||||
self.dialog.destroy()
|
||||
if self.progress:
|
||||
self.progress.stop()
|
||||
super().close()
|
||||
|
||||
def is_cancelled(self) -> bool:
|
||||
"""检查是否已取消"""
|
||||
|
||||
Reference in New Issue
Block a user