-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathchat_image_upload.py
More file actions
213 lines (189 loc) · 6.63 KB
/
chat_image_upload.py
File metadata and controls
213 lines (189 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from PySide6.QtWidgets import QPushButton, QFileDialog, QWidget, QLabel, QHBoxLayout
from PySide6.QtCore import Qt, Signal
from PySide6.QtGui import QPixmap, QPainter, QPainterPath
class ImageThumbnail(QWidget):
delete_clicked = Signal(str)
def __init__(self, image_path, parent=None):
super().__init__(parent)
self.image_path = image_path
self.setup_ui()
def setup_ui(self):
self.setFixedSize(90, 70)
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
container = QWidget(self)
container.setFixedSize(90, 70)
container.setStyleSheet(
"background-color: #f1f3f4; border-radius: 6px; border: 1px solid #dadce0;"
)
self.image_label = QLabel(container)
self.image_label.setGeometry(4, 4, 82, 62)
self.image_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
pixmap = QPixmap(self.image_path)
if not pixmap.isNull():
scaled = pixmap.scaled(
82,
62,
Qt.AspectRatioMode.KeepAspectRatioByExpanding,
Qt.TransformationMode.SmoothTransformation,
)
self.image_label.setPixmap(self.create_rounded_pixmap(scaled, 6))
self.delete_btn = QPushButton("×", container)
self.delete_btn.setGeometry(66, 4, 20, 20)
self.delete_btn.setStyleSheet(
"""
QPushButton {
background-color: rgba(0, 0, 0, 0.7);
color: white;
border: none;
border-radius: 10px;
font-size: 12px;
font-weight: bold;
padding: 0px;
}
QPushButton:hover {
background-color: rgba(220, 38, 38, 0.9);
}
"""
)
self.delete_btn.clicked.connect(lambda: self.delete_clicked.emit(self.image_path))
layout.addWidget(container)
def create_rounded_pixmap(self, pixmap, radius):
rounded = QPixmap(pixmap.size())
rounded.fill(Qt.GlobalColor.transparent)
painter = QPainter(rounded)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
path = QPainterPath()
path.addRoundedRect(0, 0, pixmap.width(), pixmap.height(), radius, radius)
painter.setClipPath(path)
painter.drawPixmap(0, 0, pixmap)
painter.end()
return rounded
class ChatImageUploadManager:
def __init__(self, workbench):
self.workbench = workbench
self.images = []
self.button = None
self.preview_container = None
self.preview_layout = None
def create_button(self):
btn = QPushButton("🖼")
btn.setFixedSize(26, 26)
btn.setCursor(Qt.CursorShape.PointingHandCursor)
btn.setToolTip("上传图片")
btn.setStyleSheet(
"""
QPushButton {
background-color: transparent;
color: #5f6368;
border: 1px solid #e0e0e0;
border-radius: 4px;
font-size: 14px;
}
QPushButton:hover {
background-color: #f1f3f4;
color: #1a73e8;
border: 1px solid #d2e3fc;
}
QPushButton:pressed {
background-color: #e8f0fe;
}
"""
)
btn.clicked.connect(self.on_clicked)
self.button = btn
return btn
def bind_preview_area(self, container, layout):
self.preview_container = container
self.preview_layout = layout
def on_clicked(self):
files, _ = QFileDialog.getOpenFileNames(
self.workbench,
"选择图片",
"",
"图片文件 (*.png *.jpg *.jpeg *.bmp *.webp *.gif)",
)
if not files:
return
for path in files:
if path not in self.images:
self.images.append(path)
self.update_button_state()
self.update_preview()
def get_images(self):
return list(self.images)
def clear_images(self):
self.images.clear()
self.update_button_state()
self.update_preview()
def remove_image(self, image_path):
if image_path in self.images:
self.images.remove(image_path)
self.update_button_state()
self.update_preview()
def update_button_state(self):
if not self.button:
return
if self.images:
count = len(self.images)
self.button.setToolTip(f"已选择 {count} 张图片")
self.button.setStyleSheet(
"""
QPushButton {
background-color: #e8f0fe;
color: #1a73e8;
border: 1px solid #d2e3fc;
border-radius: 4px;
font-size: 14px;
}
QPushButton:hover {
background-color: #d2e3fc;
color: #174ea6;
border: 1px solid #aecbfa;
}
QPushButton:pressed {
background-color: #c4d2ff;
}
"""
)
else:
self.button.setText("🖼")
self.button.setToolTip("上传图片")
self.button.setStyleSheet(
"""
QPushButton {
background-color: transparent;
color: #5f6368;
border: 1px solid #e0e0e0;
border-radius: 4px;
font-size: 14px;
}
QPushButton:hover {
background-color: #f1f3f4;
color: #1a73e8;
border: 1px solid #d2e3fc;
}
QPushButton:pressed {
background-color: #e8f0fe;
}
"""
)
def update_preview(self):
if not self.preview_layout:
return
while self.preview_layout.count():
item = self.preview_layout.takeAt(0)
w = item.widget()
if w:
w.deleteLater()
if not self.images:
if self.preview_container:
self.preview_container.setVisible(False)
return
for path in self.images:
thumb = ImageThumbnail(path, self.preview_container)
thumb.delete_clicked.connect(self.remove_image)
self.preview_layout.addWidget(thumb)
if self.preview_container:
self.preview_container.setVisible(True)