From 6d7141c5b887a1b1619c67a4a649d61cfdc0a471 Mon Sep 17 00:00:00 2001 From: OpenCode Bot Date: Sun, 24 May 2026 22:45:30 +0800 Subject: [PATCH] feat: add ui module with drop zone --- temp_file_trans_client/ui.py | 134 +++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 temp_file_trans_client/ui.py diff --git a/temp_file_trans_client/ui.py b/temp_file_trans_client/ui.py new file mode 100644 index 0000000..67f5688 --- /dev/null +++ b/temp_file_trans_client/ui.py @@ -0,0 +1,134 @@ +from PySide6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, + QLineEdit, QPushButton, QLabel, QComboBox, + QProgressBar, QTextEdit, QFrame) +from PySide6.QtCore import Qt, QRect +from PySide6.QtGui import QPixmap, QPainter, QPen, QColor, QBrush + +class DropZone(QFrame): + def __init__(self, parent=None): + super().__init__(parent) + self.setAcceptDrops(True) + self.file_selected = None + self.setMinimumSize(400, 200) + self.setStyleSheet(""" + QFrame { + border: 2px dashed #aaa; + border-radius: 8px; + background-color: #fafafa; + } + QFrame:hover { + border-color: #3498db; + background-color: #ecf0f1; + } + """) + + def dragEnterEvent(self, event): + if event.mimeData().hasUrls(): + event.acceptProposedAction() + self.setStyleSheet(""" + QFrame { + border: 2px dashed #3498db; + border-radius: 8px; + background-color: #d5e8f7; + } + """) + + def dragLeaveEvent(self, event): + self.setStyleSheet(""" + QFrame { + border: 2px dashed #aaa; + border-radius: 8px; + background-color: #fafafa; + } + """) + + def dropEvent(self, event): + if event.mimeData().hasUrls(): + urls = event.mimeData().urls() + if urls: + self.file_selected = urls[0].toLocalFile() + event.acceptProposedAction() + self.parent().on_file_selected(self.file_selected) + + def mousePressEvent(self, event): + from PySide6.QtWidgets import QFileDialog + filepath, _ = QFileDialog.getOpenFileName(self, "选择文件") + if filepath: + self.file_selected = filepath + self.parent().on_file_selected(filepath) + +class UiCreator: + @staticmethod + def create_main_ui(window): + window.setWindowTitle("临时文件传输客户端") + window.setMinimumSize(400, 350) + window.resize(500, 450) + + central_widget = QWidget() + window.setCentralWidget(central_widget) + layout = QVBoxLayout(central_widget) + layout.setContentsMargins(20, 20, 20, 20) + layout.setSpacing(15) + + server_layout = QHBoxLayout() + server_label = QLabel("服务器:") + window.server_input = QLineEdit("http://localhost:5000") + window.save_btn = QPushButton("保存") + window.save_btn.setFixedWidth(60) + server_layout.addWidget(server_label) + server_layout.addWidget(window.server_input) + server_layout.addWidget(window.save_btn) + layout.addLayout(server_layout) + + window.drop_zone = DropZone(window) + window.drop_label = QLabel("📁 拖放文件到此处\n或点击选择文件") + window.drop_label.setAlignment(Qt.AlignCenter) + drop_layout = QVBoxLayout(window.drop_zone) + drop_layout.addWidget(window.drop_label) + layout.addWidget(window.drop_zone) + + action_layout = QHBoxLayout() + expiry_label = QLabel("过期时间:") + window.expiry_combo = QComboBox() + window.expiry_combo.addItems(["1h", "24h", "7d"]) + window.expiry_combo.setCurrentText("24h") + window.upload_btn = QPushButton("上传文件") + window.upload_btn.setFixedWidth(100) + action_layout.addWidget(expiry_label) + action_layout.addWidget(window.expiry_combo) + action_layout.addStretch() + action_layout.addWidget(window.upload_btn) + layout.addLayout(action_layout) + + window.progress_bar = QProgressBar() + window.progress_bar.setVisible(False) + window.progress_bar.setTextVisible(True) + layout.addWidget(window.progress_bar) + + window.result_area = QTextEdit() + window.result_area.setVisible(False) + window.result_area.setReadOnly(True) + window.result_area.setMaximumHeight(80) + window.result_area.setStyleSheet(""" + QTextEdit { + background-color: #f0f0f0; + border: 1px solid #ddd; + border-radius: 4px; + padding: 8px; + } + """) + layout.addWidget(window.result_area) + + window.copy_btn = QPushButton("复制链接") + window.copy_btn.setVisible(False) + window.copy_btn.setFixedWidth(80) + copy_layout = QHBoxLayout() + copy_layout.addStretch() + copy_layout.addWidget(window.copy_btn) + layout.addLayout(copy_layout) + + window.status_label = QLabel("状态: 就绪") + window.status_label.setStyleSheet("color: #666; font-size: 12px;") + layout.addWidget(window.status_label) + + return central_widget \ No newline at end of file