143 lines
4.3 KiB
Python
143 lines
4.3 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
自定义组件 - 文件选择控件等公共组件
|
|
"""
|
|
|
|
import os
|
|
|
|
from PyQt5.QtWidgets import (
|
|
QWidget, QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog,
|
|
)
|
|
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):
|
|
"""
|
|
初始化文件选择组件
|
|
|
|
Args:
|
|
label_text: 标签文本
|
|
file_filter: 文件过滤器
|
|
mode: 选择模式 - "open"(打开文件) 或 "save"(保存文件)
|
|
parent: 父控件
|
|
"""
|
|
super().__init__(parent)
|
|
self.file_filter = file_filter
|
|
self.mode = mode # "open" 或 "save"
|
|
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()
|
|
placeholder = "请选择保存路径..." if self.mode == "save" else "请选择文件..."
|
|
self.line_edit.setPlaceholderText(placeholder)
|
|
self.browse_btn = QPushButton("浏览...")
|
|
self.browse_btn.setMaximumWidth(80)
|
|
self.browse_btn.clicked.connect(self.browse_file)
|
|
|
|
layout.addWidget(self.label)
|
|
layout.addWidget(self.line_edit, 1)
|
|
layout.addWidget(self.browse_btn)
|
|
|
|
self.setLayout(layout)
|
|
|
|
def browse_file(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
|
|
|
|
if self.mode == "save":
|
|
file_path, _ = QFileDialog.getSaveFileName(
|
|
self, "保存文件", initial_dir, self.file_filter
|
|
)
|
|
else:
|
|
file_path, _ = QFileDialog.getOpenFileName(
|
|
self, "选择文件", initial_dir, self.file_filter
|
|
)
|
|
if file_path:
|
|
self.line_edit.setText(file_path)
|
|
|
|
def get_path(self):
|
|
"""获取路径"""
|
|
return self.line_edit.text()
|
|
|
|
def set_path(self, path):
|
|
"""设置路径"""
|
|
self.line_edit.setText(str(path)) |