291 lines
8.6 KiB
Python
291 lines
8.6 KiB
Python
"""
|
|
删除确认对话框
|
|
"""
|
|
from PyQt5.QtWidgets import (
|
|
QDialog, QVBoxLayout, QHBoxLayout, QLabel,
|
|
QPushButton, QTextEdit, QCheckBox, QMessageBox
|
|
)
|
|
from PyQt5.QtCore import Qt
|
|
from PyQt5.QtGui import QFont, QColor
|
|
|
|
from utils import format_size
|
|
|
|
|
|
class DeleteConfirmDialog(QDialog):
|
|
"""删除确认对话框"""
|
|
|
|
def __init__(
|
|
self,
|
|
paths: list,
|
|
total_size: int,
|
|
is_dir: bool = False,
|
|
parent=None
|
|
):
|
|
"""
|
|
初始化对话框
|
|
|
|
参数:
|
|
paths: 要删除的路径列表
|
|
total_size: 总大小(字节)
|
|
is_dir: 是否为目录
|
|
parent: 父窗口
|
|
"""
|
|
super().__init__(parent)
|
|
self.paths = paths
|
|
self.total_size = total_size
|
|
self.is_dir = is_dir
|
|
self.confirmed = False
|
|
self.init_ui()
|
|
|
|
def init_ui(self):
|
|
"""初始化界面"""
|
|
self.setWindowTitle("确认删除")
|
|
self.setMinimumSize(500, 350)
|
|
self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
|
|
|
|
# 主布局
|
|
layout = QVBoxLayout(self)
|
|
layout.setSpacing(15)
|
|
layout.setContentsMargins(20, 20, 20, 20)
|
|
|
|
# 警告图标和标题
|
|
header_layout = QHBoxLayout()
|
|
|
|
icon_label = QLabel("⚠️")
|
|
font = QFont()
|
|
font.setPointSize(36)
|
|
icon_label.setFont(font)
|
|
header_layout.addWidget(icon_label)
|
|
|
|
title_layout = QVBoxLayout()
|
|
|
|
title_label = QLabel("确认删除")
|
|
title_font = QFont()
|
|
title_font.setPointSize(14)
|
|
title_font.setBold(True)
|
|
title_label.setFont(title_font)
|
|
title_layout.addWidget(title_label)
|
|
|
|
size_label = QLabel(f"将释放 {format_size(self.total_size)} 空间")
|
|
size_label.setStyleSheet("color: green;")
|
|
title_layout.addWidget(size_label)
|
|
|
|
header_layout.addLayout(title_layout)
|
|
header_layout.addStretch()
|
|
|
|
layout.addLayout(header_layout)
|
|
|
|
# 分隔线
|
|
separator = QLabel()
|
|
separator.setFrameStyle(QLabel.HLine | QLabel.Sunken)
|
|
layout.addWidget(separator)
|
|
|
|
# 删除内容列表
|
|
content_label = QLabel("将删除以下内容:")
|
|
layout.addWidget(content_label)
|
|
|
|
self.content_text = QTextEdit()
|
|
self.content_text.setReadOnly(True)
|
|
self.content_text.setMaximumHeight(150)
|
|
|
|
# 显示删除列表
|
|
content = ""
|
|
for path in self.paths[:50]: # 最多显示 50 条
|
|
content += f"• {path}\n"
|
|
|
|
if len(self.paths) > 50:
|
|
content += f"\n... 还有 {len(self.paths) - 50} 个项目"
|
|
|
|
self.content_text.setPlainText(content)
|
|
layout.addWidget(self.content_text)
|
|
|
|
# 警告文本
|
|
if self.is_dir:
|
|
warning_label = QLabel(
|
|
"⚠️ 目录将被递归删除,包含其中的所有文件和子目录!"
|
|
)
|
|
warning_label.setStyleSheet("color: red; font-weight: bold;")
|
|
warning_label.setWordWrap(True)
|
|
layout.addWidget(warning_label)
|
|
|
|
# 确认复选框
|
|
self.confirm_checkbox = QCheckBox("我已确认要删除以上内容")
|
|
self.confirm_checkbox.stateChanged.connect(self.on_checkbox_changed)
|
|
layout.addWidget(self.confirm_checkbox)
|
|
|
|
# 按钮
|
|
button_layout = QHBoxLayout()
|
|
button_layout.addStretch()
|
|
|
|
self.cancel_button = QPushButton("取消")
|
|
self.cancel_button.clicked.connect(self.reject)
|
|
button_layout.addWidget(self.cancel_button)
|
|
|
|
self.delete_button = QPushButton("删除")
|
|
self.delete_button.setEnabled(False)
|
|
self.delete_button.setStyleSheet(
|
|
"QPushButton:enabled { background-color: #e74c3c; color: white; }"
|
|
)
|
|
self.delete_button.clicked.connect(self.on_delete_clicked)
|
|
button_layout.addWidget(self.delete_button)
|
|
|
|
layout.addLayout(button_layout)
|
|
|
|
def on_checkbox_changed(self, state):
|
|
"""复选框状态改变"""
|
|
self.delete_button.setEnabled(state == Qt.Checked)
|
|
|
|
def on_delete_clicked(self):
|
|
"""删除按钮点击"""
|
|
if not self.confirm_checkbox.isChecked():
|
|
return
|
|
|
|
# 最终确认
|
|
reply = QMessageBox.critical(
|
|
self,
|
|
"最终确认",
|
|
f"确定要删除 {len(self.paths)} 个项目吗?\n"
|
|
f"此操作不可撤销!",
|
|
QMessageBox.Yes | QMessageBox.No,
|
|
QMessageBox.No
|
|
)
|
|
|
|
if reply == QMessageBox.Yes:
|
|
self.confirmed = True
|
|
self.accept()
|
|
|
|
def is_confirmed(self) -> bool:
|
|
"""是否确认删除"""
|
|
return self.confirmed
|
|
|
|
|
|
class LogDeleteConfirmDialog(QDialog):
|
|
"""系统日志删除确认对话框"""
|
|
|
|
def __init__(
|
|
self,
|
|
log_files: list,
|
|
total_size: int,
|
|
parent=None
|
|
):
|
|
"""
|
|
初始化对话框
|
|
|
|
参数:
|
|
log_files: 日志文件列表
|
|
total_size: 总大小(字节)
|
|
parent: 父窗口
|
|
"""
|
|
super().__init__(parent)
|
|
self.log_files = log_files
|
|
self.total_size = total_size
|
|
self.confirmed = False
|
|
self.init_ui()
|
|
|
|
def init_ui(self):
|
|
"""初始化界面"""
|
|
self.setWindowTitle("清理系统日志")
|
|
self.setMinimumSize(500, 400)
|
|
self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
|
|
|
|
# 主布局
|
|
layout = QVBoxLayout(self)
|
|
layout.setSpacing(15)
|
|
layout.setContentsMargins(20, 20, 20, 20)
|
|
|
|
# 标题
|
|
header_layout = QHBoxLayout()
|
|
|
|
icon_label = QLabel("📋")
|
|
font = QFont()
|
|
font.setPointSize(36)
|
|
icon_label.setFont(font)
|
|
header_layout.addWidget(icon_label)
|
|
|
|
title_layout = QVBoxLayout()
|
|
|
|
title_label = QLabel("清理系统日志")
|
|
title_font = QFont()
|
|
title_font.setPointSize(14)
|
|
title_font.setBold(True)
|
|
title_label.setFont(title_font)
|
|
title_layout.addWidget(title_label)
|
|
|
|
info_label = QLabel(
|
|
f"检测到 /var/log 下有 {len(self.log_files)} 个日志文件,\n"
|
|
f"共占用 {format_size(self.total_size)} 空间。"
|
|
)
|
|
title_layout.addWidget(info_label)
|
|
|
|
header_layout.addLayout(title_layout)
|
|
header_layout.addStretch()
|
|
|
|
layout.addLayout(header_layout)
|
|
|
|
# 分隔线
|
|
separator = QLabel()
|
|
separator.setFrameStyle(QLabel.HLine | QLabel.Sunken)
|
|
layout.addWidget(separator)
|
|
|
|
# 日志文件列表
|
|
content_label = QLabel("将删除以下日志文件:")
|
|
layout.addWidget(content_label)
|
|
|
|
self.content_text = QTextEdit()
|
|
self.content_text.setReadOnly(True)
|
|
|
|
# 显示日志文件列表
|
|
content = ""
|
|
for log_file in self.log_files:
|
|
name = log_file.name if hasattr(log_file, 'name') else str(log_file)
|
|
size = format_size(log_file.size) if hasattr(log_file, 'size') else ""
|
|
content += f"• {name} ({size})\n"
|
|
|
|
self.content_text.setPlainText(content)
|
|
layout.addWidget(self.content_text)
|
|
|
|
# 说明文本
|
|
note_label = QLabel(
|
|
"💡 这些是 /var/log 目录下的日志文件(不包含子目录中的日志)。\n"
|
|
"删除后系统会自动创建新的日志文件。"
|
|
)
|
|
note_label.setWordWrap(True)
|
|
note_label.setStyleSheet("color: gray;")
|
|
layout.addWidget(note_label)
|
|
|
|
# 按钮
|
|
button_layout = QHBoxLayout()
|
|
button_layout.addStretch()
|
|
|
|
self.cancel_button = QPushButton("取消")
|
|
self.cancel_button.clicked.connect(self.reject)
|
|
button_layout.addWidget(self.cancel_button)
|
|
|
|
self.delete_button = QPushButton("清理日志")
|
|
self.delete_button.setStyleSheet(
|
|
"QPushButton { background-color: #3498db; color: white; }"
|
|
)
|
|
self.delete_button.clicked.connect(self.on_delete_clicked)
|
|
button_layout.addWidget(self.delete_button)
|
|
|
|
layout.addLayout(button_layout)
|
|
|
|
def on_delete_clicked(self):
|
|
"""删除按钮点击"""
|
|
reply = QMessageBox.question(
|
|
self,
|
|
"确认清理",
|
|
f"确定要清理这 {len(self.log_files)} 个日志文件吗?\n"
|
|
f"将释放 {format_size(self.total_size)} 空间。",
|
|
QMessageBox.Yes | QMessageBox.No,
|
|
QMessageBox.Yes
|
|
)
|
|
|
|
if reply == QMessageBox.Yes:
|
|
self.confirmed = True
|
|
self.accept()
|
|
|
|
def is_confirmed(self) -> bool:
|
|
"""是否确认删除"""
|
|
return self.confirmed
|