页面4样式修改
This commit is contained in:
@ -1,148 +1,102 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
自定义组件 - 文件选择控件等公共组件
|
||||
自定义基础组件库
|
||||
|
||||
提供全站统一封装的文件选择器、目录选择器等。
|
||||
样式全权委托给全局 styles.py,杜绝在此处进行硬编码修饰。
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from PyQt5.QtWidgets import (
|
||||
QWidget, QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog,
|
||||
)
|
||||
from PyQt5.QtCore import Qt
|
||||
|
||||
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QLineEdit, QPushButton, QFileDialog
|
||||
from src.gui.styles import ModernStylesheet
|
||||
|
||||
class DirSelectWidget(QWidget):
|
||||
"""目录选择组件"""
|
||||
def __init__(self, label_text, parent=None):
|
||||
"""
|
||||
初始化目录选择组件
|
||||
|
||||
Args:
|
||||
label_text: 标签文本
|
||||
parent: 父控件
|
||||
"""
|
||||
def __init__(self, label_text: str, parent=None):
|
||||
super().__init__(parent)
|
||||
self.init_ui(label_text)
|
||||
|
||||
def init_ui(self, label_text):
|
||||
layout = QHBoxLayout()
|
||||
def init_ui(self, label_text: str):
|
||||
layout = QHBoxLayout(self)
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
layout.setSpacing(8)
|
||||
|
||||
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)
|
||||
|
||||
self.label.setMinimumWidth(100) # 保底宽度,防挤压
|
||||
layout.addWidget(self.label)
|
||||
|
||||
self.line_edit = QLineEdit()
|
||||
self.line_edit.setReadOnly(True)
|
||||
self.line_edit.setPlaceholderText("请选择目录...")
|
||||
layout.addWidget(self.line_edit, 1)
|
||||
|
||||
self.browse_btn = QPushButton("浏览...")
|
||||
self.browse_btn.setStyleSheet(ModernStylesheet.get_button_stylesheet('normal'))
|
||||
self.browse_btn.clicked.connect(self.browse_dir)
|
||||
layout.addWidget(self.browse_btn)
|
||||
|
||||
self.setLayout(layout)
|
||||
|
||||
def browse_dir(self):
|
||||
"""浏览目录 - 智能记忆上次选择位置"""
|
||||
current_text = self.line_edit.text().strip()
|
||||
initial_dir = ""
|
||||
current_path = self.get_path()
|
||||
start_dir = current_path if os.path.isdir(current_path) else ""
|
||||
directory = QFileDialog.getExistingDirectory(self, "选择目录", start_dir)
|
||||
if directory:
|
||||
self.set_path(directory)
|
||||
|
||||
# 最高优先级:输入框已有路径存在
|
||||
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
|
||||
def get_path(self) -> str:
|
||||
return self.line_edit.text().strip()
|
||||
|
||||
# 调用目录选择对话框
|
||||
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))
|
||||
def set_path(self, path: str):
|
||||
if path:
|
||||
self.line_edit.setText(os.path.normpath(path).replace('\\', '/'))
|
||||
|
||||
|
||||
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: 父控件
|
||||
"""
|
||||
def __init__(self, label_text: str, file_filter: str = "All Files (*.*)", mode: str = "open", parent=None):
|
||||
super().__init__(parent)
|
||||
self.file_filter = file_filter
|
||||
self.mode = mode # "open" 或 "save"
|
||||
self.mode = mode
|
||||
self.init_ui(label_text)
|
||||
|
||||
def init_ui(self, label_text):
|
||||
layout = QHBoxLayout()
|
||||
def init_ui(self, label_text: str):
|
||||
layout = QHBoxLayout(self)
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
layout.setSpacing(8)
|
||||
|
||||
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)
|
||||
|
||||
self.label.setMinimumWidth(100) # 保底宽度,防挤压
|
||||
layout.addWidget(self.label)
|
||||
|
||||
self.line_edit = QLineEdit()
|
||||
self.line_edit.setReadOnly(True)
|
||||
self.line_edit.setPlaceholderText("请选择文件..." if self.mode == "open" else "请指定保存位置...")
|
||||
layout.addWidget(self.line_edit, 1)
|
||||
|
||||
self.browse_btn = QPushButton("浏览...")
|
||||
self.browse_btn.setStyleSheet(ModernStylesheet.get_button_stylesheet('normal'))
|
||||
self.browse_btn.clicked.connect(self.browse_file)
|
||||
layout.addWidget(self.browse_btn)
|
||||
|
||||
self.setLayout(layout)
|
||||
|
||||
def browse_file(self):
|
||||
"""浏览文件 - 智能记忆上次选择位置"""
|
||||
current_text = self.line_edit.text().strip()
|
||||
initial_dir = ""
|
||||
current_path = self.get_path()
|
||||
start_dir = ""
|
||||
if current_path:
|
||||
start_dir = current_path if os.path.isdir(current_path) else os.path.dirname(current_path)
|
||||
|
||||
# 最高优先级:输入框已有路径存在
|
||||
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
|
||||
)
|
||||
if self.mode == "open":
|
||||
file_path, _ = QFileDialog.getOpenFileName(self, "选择文件", start_dir, self.file_filter)
|
||||
else:
|
||||
file_path, _ = QFileDialog.getOpenFileName(
|
||||
self, "选择文件", initial_dir, self.file_filter
|
||||
)
|
||||
file_path, _ = QFileDialog.getSaveFileName(self, "保存文件", start_dir, self.file_filter)
|
||||
|
||||
if file_path:
|
||||
self.line_edit.setText(file_path)
|
||||
self.set_path(file_path)
|
||||
|
||||
def get_path(self):
|
||||
"""获取路径"""
|
||||
return self.line_edit.text()
|
||||
def get_path(self) -> str:
|
||||
return self.line_edit.text().strip()
|
||||
|
||||
def set_path(self, path):
|
||||
"""设置路径"""
|
||||
self.line_edit.setText(str(path))
|
||||
def set_path(self, path: str):
|
||||
if path:
|
||||
self.line_edit.setText(os.path.normpath(path).replace('\\', '/'))
|
||||
|
||||
def set_read_only(self, read_only=True):
|
||||
"""设置文件选择框为只读,并禁用浏览按钮。"""
|
||||
def set_read_only(self, read_only: bool = True):
|
||||
self.line_edit.setReadOnly(read_only)
|
||||
self.browse_btn.setEnabled(not read_only)
|
||||
self.browse_btn.setEnabled(not read_only)
|
||||
self.label.setEnabled(not read_only)
|
||||
Reference in New Issue
Block a user