体验升级:路径记忆、可视化深度扫描、文件名汉化
This commit is contained in:
@ -12,6 +12,67 @@ from PyQt5.QtWidgets import (
|
||||
from PyQt5.QtCore import Qt
|
||||
|
||||
|
||||
class DirSelectWidget(QWidget):
|
||||
"""目录选择组件"""
|
||||
def __init__(self, label_text, parent=None):
|
||||
"""
|
||||
初始化目录选择组件
|
||||
|
||||
Args:
|
||||
label_text: 标签文本
|
||||
parent: 父控件
|
||||
"""
|
||||
super().__init__(parent)
|
||||
self.init_ui(label_text)
|
||||
|
||||
def init_ui(self, label_text):
|
||||
layout = QHBoxLayout()
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
|
||||
self.label = QLabel(label_text)
|
||||
self.label.setMinimumWidth(120)
|
||||
self.line_edit = QLineEdit()
|
||||
self.line_edit.setPlaceholderText("请选择目录...")
|
||||
self.browse_btn = QPushButton("浏览...")
|
||||
self.browse_btn.setMaximumWidth(80)
|
||||
self.browse_btn.clicked.connect(self.browse_dir)
|
||||
|
||||
layout.addWidget(self.label)
|
||||
layout.addWidget(self.line_edit, 1)
|
||||
layout.addWidget(self.browse_btn)
|
||||
|
||||
self.setLayout(layout)
|
||||
|
||||
def browse_dir(self):
|
||||
"""浏览目录 - 智能记忆上次选择位置"""
|
||||
current_text = self.line_edit.text().strip()
|
||||
initial_dir = ""
|
||||
|
||||
# 最高优先级:输入框已有路径存在
|
||||
if current_text:
|
||||
if os.path.isdir(current_text):
|
||||
initial_dir = current_text
|
||||
else:
|
||||
dir_path = os.path.dirname(current_text)
|
||||
if dir_path and os.path.exists(dir_path):
|
||||
initial_dir = dir_path
|
||||
|
||||
# 调用目录选择对话框
|
||||
dir_path = QFileDialog.getExistingDirectory(
|
||||
self, "选择目录", initial_dir
|
||||
)
|
||||
if dir_path:
|
||||
self.line_edit.setText(dir_path)
|
||||
|
||||
def get_path(self):
|
||||
"""获取路径"""
|
||||
return self.line_edit.text()
|
||||
|
||||
def set_path(self, path):
|
||||
"""设置路径"""
|
||||
self.line_edit.setText(str(path))
|
||||
|
||||
|
||||
class FileSelectWidget(QWidget):
|
||||
"""文件选择组件"""
|
||||
def __init__(self, label_text, file_filter="All Files (*.*)", mode="open", parent=None):
|
||||
@ -49,14 +110,18 @@ class FileSelectWidget(QWidget):
|
||||
self.setLayout(layout)
|
||||
|
||||
def browse_file(self):
|
||||
"""浏览文件"""
|
||||
"""浏览文件 - 智能记忆上次选择位置"""
|
||||
current_text = self.line_edit.text().strip()
|
||||
initial_dir = ""
|
||||
|
||||
# 最高优先级:输入框已有路径存在
|
||||
if current_text:
|
||||
dir_path = os.path.dirname(current_text)
|
||||
if dir_path and os.path.exists(dir_path):
|
||||
initial_dir = dir_path
|
||||
if os.path.isdir(current_text):
|
||||
initial_dir = current_text
|
||||
else:
|
||||
dir_path = os.path.dirname(current_text)
|
||||
if dir_path and os.path.exists(dir_path):
|
||||
initial_dir = dir_path
|
||||
|
||||
if self.mode == "save":
|
||||
file_path, _ = QFileDialog.getSaveFileName(
|
||||
|
||||
Reference in New Issue
Block a user