Skip to content

Commit 0b6714a

Browse files
HackLinjiuyuexushengj
authored andcommitted
finish refactor
1 parent 4d85c79 commit 0b6714a

5 files changed

Lines changed: 395 additions & 238 deletions

File tree

src/preppipe_gui_pyside6/componentwidgets/assetcardwidget.py

Lines changed: 137 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ class AssetCardStyleManager:
3030
'pressed': "QPushButton:pressed { background-color: rgba(245, 245, 245, 0.95); border-color: rgba(220, 220, 220, 0.95); border-radius: {radius}px; color: #000000; }",
3131
'focus': "QPushButton:focus { background-color: rgba(235, 235, 235, 0.95); border: 1px solid rgba(220, 220, 220, 0.95); border-radius: {radius}px; color: #000000; outline: none; }",
3232
'flat': "QPushButton:flat { border-radius: {radius}px; }"
33+
},
34+
'tags_button_notag': {
35+
'normal': "QPushButton { background-color: rgba(235, 235, 235, 0.95); border: 1px solid rgba(220, 220, 220, 0.95); border-radius: {radius}px; color: #999999; padding: 0px 10px; text-align: center; outline: none; height: {height}px; qproperty-flat: false; }",
36+
'hover': "QPushButton:hover { background-color: rgba(245, 245, 245, 0.95); border-color: rgba(230, 230, 230, 0.95); border-radius: {radius}px; color: #999999; }",
37+
'pressed': "QPushButton:pressed { background-color: rgba(245, 245, 245, 0.95); border-color: rgba(220, 220, 220, 0.95); border-radius: {radius}px; color: #999999; }",
38+
'focus': "QPushButton:focus { background-color: rgba(235, 235, 235, 0.95); border: 1px solid rgba(220, 220, 220, 0.95); border-radius: {radius}px; color: #999999; outline: none; }",
39+
'flat': "QPushButton:flat { border-radius: {radius}px; }"
40+
},
41+
'preset_tags_button': {
42+
'normal': "QPushButton { background-color: rgba(200, 200, 200, 0.95); border: 1px solid rgba(180, 180, 180, 0.95); border-radius: {radius}px; color: #333333; padding: 0px 10px; text-align: center; outline: none; height: {height}px; qproperty-flat: false; }",
43+
'hover': "QPushButton:hover { background-color: rgba(210, 210, 210, 0.95); border-color: rgba(190, 190, 190, 0.95); border-radius: {radius}px; color: #333333; }",
44+
'pressed': "QPushButton:pressed { background-color: rgba(210, 210, 210, 0.95); border-color: rgba(180, 180, 180, 0.95); border-radius: {radius}px; color: #333333; }",
45+
'focus': "QPushButton:focus { background-color: rgba(200, 200, 200, 0.95); border: 1px solid rgba(180, 180, 180, 0.95); border-radius: {radius}px; color: #333333; outline: none; }",
46+
'flat': "QPushButton:flat { border-radius: {radius}px; }"
3347
}
3448
},
3549
'dark': {
@@ -47,6 +61,20 @@ class AssetCardStyleManager:
4761
'pressed': "QPushButton:pressed { background-color: rgba(85, 85, 85, 0.95); border: none; border-radius: {radius}px; color: #DDDDDD; }",
4862
'focus': "QPushButton:focus { background-color: rgba(65, 65, 65, 0.95); border: none; border-radius: {radius}px; color: #CCCCCC; outline: none; }",
4963
'flat': "QPushButton:flat { border: none; border-radius: {radius}px; }"
64+
},
65+
'tags_button_notag': {
66+
'normal': "QPushButton { background-color: rgba(65, 65, 65, 0.95); border: none; border-radius: {radius}px; color: #666666; padding: 0px 10px; text-align: center; outline: none; height: {height}px; qproperty-flat: false; }",
67+
'hover': "QPushButton:hover { background-color: rgba(85, 85, 85, 0.95); border: none; border-radius: {radius}px; color: #666666; }",
68+
'pressed': "QPushButton:pressed { background-color: rgba(85, 85, 85, 0.95); border: none; border-radius: {radius}px; color: #666666; }",
69+
'focus': "QPushButton:focus { background-color: rgba(65, 65, 65, 0.95); border: none; border-radius: {radius}px; color: #666666; outline: none; }",
70+
'flat': "QPushButton:flat { border: none; border-radius: {radius}px; }"
71+
},
72+
'preset_tags_button': {
73+
'normal': "QPushButton { background-color: rgba(45, 45, 45, 0.95); border: none; border-radius: {radius}px; color: #AAAAAA; padding: 0px 10px; text-align: center; outline: none; height: {height}px; qproperty-flat: false; }",
74+
'hover': "QPushButton:hover { background-color: rgba(55, 55, 55, 0.95); border: none; border-radius: {radius}px; color: #BBBBBB; }",
75+
'pressed': "QPushButton:pressed { background-color: rgba(55, 55, 55, 0.95); border: none; border-radius: {radius}px; color: #BBBBBB; }",
76+
'focus': "QPushButton:focus { background-color: rgba(45, 45, 45, 0.95); border: none; border-radius: {radius}px; color: #AAAAAA; outline: none; }",
77+
'flat': "QPushButton:flat { border: none; border-radius: {radius}px; }"
5078
}
5179
}
5280
}
@@ -67,20 +95,24 @@ def get_style(style_name, palette=None):
6795
return AssetCardStyleManager._styles[theme].get(style_name, "")
6896

6997
@staticmethod
70-
def get_tags_button_style(height, palette=None):
98+
def get_tags_button_style(height, palette=None, is_preset=False, is_tag_empty=False):
7199
"""获取标签按钮样式,根据高度动态计算圆角"""
72100
theme = AssetCardStyleManager.detect_theme(palette)
73101
radius = height // 2
102+
if is_preset:
103+
style_key = 'preset_tags_button'
104+
else:
105+
style_key = 'tags_button_notag' if is_tag_empty else 'tags_button'
74106
styles = []
75-
for key, template in AssetCardStyleManager._styles[theme].get('tags_button', {}).items():
107+
for key, template in AssetCardStyleManager._styles[theme][style_key].items():
76108
style = template.replace('{radius}', str(radius))
77109
style = style.replace('{height}', str(height))
78110
styles.append(style)
79111
return "\n".join(styles)
80112

81113
@staticmethod
82-
def apply_tags_button_style(button, height, palette=None):
83-
style = AssetCardStyleManager.get_tags_button_style(height, palette)
114+
def apply_tags_button_style(button, height, palette=None, is_preset=False, is_tag_empty=False):
115+
style = AssetCardStyleManager.get_tags_button_style(height, palette, is_preset, is_tag_empty)
84116
button.setStyleSheet(style)
85117

86118
@staticmethod
@@ -123,10 +155,10 @@ class AssetCardWidget(QPushButton):
123155
右键菜单:可进行查看详情(等同左键)、快捷添加最近标签、编辑标签(等同点击标签按钮)
124156
"""
125157

126-
_tr_no_tags = TR_gui_assetcardwidget.tr("assetcardwidget_no_tags",
127-
en="No tags",
128-
zh_cn="无标签",
129-
zh_hk="无标签",
158+
_tr_no_preset_tags = TR_gui_assetcardwidget.tr("assetcardwidget_no_preset_tags",
159+
en="(No preset tags)",
160+
zh_cn="(无预设标签)",
161+
zh_hk="(无预设标签)",
130162
)
131163
_tr_show_detail = TR_gui_assetcardwidget.tr("assetcardwidget_show_detail",
132164
en="Show detail",
@@ -148,6 +180,11 @@ class AssetCardWidget(QPushButton):
148180
zh_cn="(无自定义标签)",
149181
zh_hk="(无自定义标签)",
150182
)
183+
_tr_square_bracket_template=TR_gui_assetcardwidget.tr("assetcardwidget_square_bracket",
184+
en="[{value}]",
185+
zh_cn="【{value}】",
186+
zh_hk="【{value}】",
187+
)
151188

152189
clicked = Signal(str, QMouseEvent)
153190
tags_button_clicked = Signal(str, QPushButton)
@@ -157,12 +194,14 @@ class AssetCardWidget(QPushButton):
157194
_is_hovered: bool
158195
_thumbnail_loaded: bool
159196
_name_full_text: str
160-
_tags_full_text: str
197+
_custom_tags_full_text: str
198+
_preset_tags_full_text: str
161199
name_font: QFont
162200
tags_font: QFont
163201
name_font_metrics: QFontMetrics
164202
tags_font_metrics: QFontMetrics
165-
tags_button: QPushButton
203+
custom_tags_button: QPushButton
204+
preset_tags_button: QPushButton
166205
layout: QVBoxLayout
167206
image_label: QLabel
168207
name_label: QLabel
@@ -175,7 +214,8 @@ def __init__(self, asset_id: str, width: int, height: int, name_font=None, tags_
175214
self._thumbnail_loaded = False
176215

177216
self._name_full_text = ''
178-
self._tags_full_text = ''
217+
self._custom_tags_full_text = ''
218+
self._preset_tags_full_text = ''
179219

180220
self.name_font = name_font or QFont()
181221
self.tags_font = tags_font or QFont()
@@ -184,7 +224,8 @@ def __init__(self, asset_id: str, width: int, height: int, name_font=None, tags_
184224
self.name_font_metrics = QFontMetrics(self.name_font)
185225
self.tags_font_metrics = QFontMetrics(self.tags_font)
186226

187-
self.tags_button = None
227+
self.custom_tags_button = None
228+
self.preset_tags_button = None
188229
self._initialize_ui(width, height)
189230

190231
def _calculate_elided_text(self, text, widget, margin):
@@ -201,7 +242,8 @@ def resizeEvent(self, event):
201242
def _update_elided_text(self):
202243
size = self.name_label.size()
203244
self.name_label.resizeEvent(QResizeEvent(size, size))
204-
self.tags_button.resizeEvent(None)
245+
self.custom_tags_button.resizeEvent(None)
246+
self.preset_tags_button.resizeEvent(None)
205247

206248
def _initialize_ui(self, width: int, height: int):
207249
self.setFixedSize(width, height)
@@ -276,31 +318,58 @@ def custom_resize_event(event):
276318
self.layout.addWidget(self.name_label)
277319

278320
def _create_tags_section(self):
279-
self.tags_button = QPushButton("", self)
280-
self.tags_button.setFont(self.tags_font)
281-
self.tags_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
282-
self.tags_button.setMouseTracking(True)
283-
284-
original_resize_event = self.tags_button.resizeEvent
285-
def custom_resize_event(event):
286-
fixed_width = self.tags_button.width()
287-
fixed_height = self.tags_button.height()
321+
self.custom_tags_button = QPushButton("", self)
322+
self.custom_tags_button.setFont(self.tags_font)
323+
self.custom_tags_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
324+
self.custom_tags_button.setMouseTracking(True)
325+
326+
original_custom_resize_event = self.custom_tags_button.resizeEvent
327+
def custom_tags_resize_event(event):
328+
fixed_width = self.custom_tags_button.width()
329+
fixed_height = self.custom_tags_button.height()
330+
331+
original_custom_resize_event(event)
332+
if self._custom_tags_full_text:
333+
self.custom_tags_button.setText(self._calculate_elided_text(self._custom_tags_full_text, self.custom_tags_button, 10))
334+
335+
self.custom_tags_button.setFixedWidth(fixed_width)
336+
self.custom_tags_button.setFixedHeight(fixed_height)
337+
self.custom_tags_button.resizeEvent = custom_tags_resize_event
338+
339+
self.preset_tags_button = QPushButton("", self)
340+
self.preset_tags_button.setFont(self.tags_font)
341+
self.preset_tags_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
342+
self.preset_tags_button.setMouseTracking(False)
343+
self.preset_tags_button.setEnabled(False)
344+
345+
original_preset_resize_event = self.preset_tags_button.resizeEvent
346+
def preset_tags_resize_event(event):
347+
fixed_width = self.preset_tags_button.width()
348+
fixed_height = self.preset_tags_button.height()
349+
350+
original_preset_resize_event(event)
351+
if self._preset_tags_full_text:
352+
self.preset_tags_button.setText(self._calculate_elided_text(self._preset_tags_full_text, self.preset_tags_button, 10))
353+
354+
self.preset_tags_button.setFixedWidth(fixed_width)
355+
self.preset_tags_button.setFixedHeight(fixed_height)
356+
self.preset_tags_button.resizeEvent = preset_tags_resize_event
288357

289-
original_resize_event(event)
290-
if self._tags_full_text:
291-
self.tags_button.setText(self._calculate_elided_text(self._tags_full_text, self.tags_button, 10))
358+
tag_button_height = int(self.tags_font_metrics.height() * 1.4)
359+
button_width = self.width() - self.layout.contentsMargins().left() - self.layout.contentsMargins().right()
292360

293-
self.tags_button.setFixedWidth(fixed_width)
294-
self.tags_button.setFixedHeight(fixed_height)
295-
self.tags_button.resizeEvent = custom_resize_event
361+
self.custom_tags_button.setFixedHeight(tag_button_height)
362+
self.custom_tags_button.setFixedWidth(button_width)
363+
AssetCardStyleManager.apply_tags_button_style(self.custom_tags_button, tag_button_height, is_preset=False, is_tag_empty=True)
364+
self.custom_tags_button.clicked.connect(self._on_tags_button_clicked)
296365

297-
tag_button_height = int(self.tags_font_metrics.height() * 1.4)
298-
self.tags_button.setFixedHeight(tag_button_height)
299-
self.tags_button.setFixedWidth(self.width() - self.layout.contentsMargins().left() - self.layout.contentsMargins().right())
300-
AssetCardStyleManager.apply_tags_button_style(self.tags_button, tag_button_height)
366+
self.preset_tags_button.setFixedHeight(tag_button_height)
367+
self.preset_tags_button.setFixedWidth(button_width)
368+
AssetCardStyleManager.apply_tags_button_style(self.preset_tags_button, tag_button_height, is_preset=True)
301369

302-
self.tags_button.clicked.connect(self._on_tags_button_clicked)
303-
self.layout.addWidget(self.tags_button)
370+
# 添加到布局
371+
self.layout.addWidget(self.custom_tags_button)
372+
self.layout.addWidget(self.preset_tags_button)
304373

305374
def enterEvent(self, event):
306375
super().enterEvent(event)
@@ -319,10 +388,10 @@ def mousePressEvent(self, event):
319388

320389
def _on_tags_button_clicked(self):
321390
self.set_selected(True)
322-
self.tags_button_clicked.emit(self.asset_id, self.tags_button)
323-
self.tags_button.setDown(False)
324-
self.tags_button.clearFocus()
325-
self.tags_button.update()
391+
self.tags_button_clicked.emit(self.asset_id, self.custom_tags_button)
392+
self.custom_tags_button.setDown(False)
393+
self.custom_tags_button.clearFocus()
394+
self.custom_tags_button.update()
326395

327396
def _get_asset_name(self):
328397
asset = AssetManager.get_instance().get_asset(self.asset_id)
@@ -335,20 +404,35 @@ def update_text(self):
335404
self.update_tags()
336405

337406
def update_name(self):
338-
self._name_full_text = self._get_asset_name()
407+
tag_manager = AssetTagManager.get_instance()
408+
asset_type = tag_manager.get_asset_type_tag(self.asset_id)
409+
type_translation = asset_type.translatable.get()
410+
prefix = self._tr_square_bracket_template.get()
411+
self._name_full_text = f"{prefix.format(value=type_translation)}{self._get_asset_name()}"
339412
self.name_label.setText(self._calculate_elided_text(self._name_full_text, self.name_label, 8))
340413

341414
def update_tags(self):
342-
asset_tags = AssetTagManager.get_instance().get_asset_tags_display(self.asset_id)
343-
self._tags_full_text = ", ".join(sorted(asset_tags)) if asset_tags else self._tr_no_tags.get()
415+
tag_manager = AssetTagManager.get_instance()
416+
all_asset_tags = tag_manager.get_asset_tags(self.asset_id)
417+
custom_tags = []
418+
preset_tags = []
419+
420+
for tag_semantic in all_asset_tags:
421+
display_text = tag_manager.get_tag_display_text(tag_semantic)
422+
if tag_manager.is_preset_tag(tag_semantic):
423+
if tag_manager.get_asset_tag_type_from_semantic(tag_semantic) is None:
424+
preset_tags.append(display_text)
425+
else:
426+
custom_tags.append(display_text)
344427

345-
current_width = self.tags_button.width()
346-
current_height = self.tags_button.height()
428+
self._custom_tags_full_text = ", ".join(custom_tags) if custom_tags else self._tr_no_custom_tags.get()
429+
self.custom_tags_button.resizeEvent(None)
347430

348-
self.tags_button.resizeEvent(None)
431+
self._preset_tags_full_text = ", ".join(preset_tags) if preset_tags else self._tr_no_preset_tags.get()
432+
self.preset_tags_button.resizeEvent(None)
349433

350-
self.tags_button.setFixedWidth(current_width)
351-
self.tags_button.setFixedHeight(current_height)
434+
is_custom_tag_empty = len(custom_tags) == 0
435+
AssetCardStyleManager.apply_tags_button_style(self.custom_tags_button, self.custom_tags_button.height(), is_preset=False, is_tag_empty=is_custom_tag_empty)
352436

353437
def set_selected(self, selected: bool):
354438
if self._is_selected != selected:
@@ -426,7 +510,7 @@ def _show_context_menu(self, position):
426510
add_tag_menu.setStyleSheet(menu_style)
427511

428512
tag_manager = AssetTagManager.get_instance()
429-
recent_tags_display = tag_manager.get_recent_tags_display()
513+
recent_tags_display = tag_manager.get_recent_tags_display(self.asset_id)
430514

431515
if recent_tags_display:
432516
for tag_display in recent_tags_display:
@@ -435,9 +519,9 @@ def _show_context_menu(self, position):
435519
tag_action.triggered.connect(lambda checked=False, tag=tag_semantic: self._add_tag_to_asset(tag))
436520
add_tag_menu.addAction(tag_action)
437521
else:
438-
no_tags_action = QAction(self._tr_no_custom_tags.get(), self)
439-
no_tags_action.setDisabled(True)
440-
add_tag_menu.addAction(no_tags_action)
522+
no_preset_tags_action = QAction(self._tr_no_custom_tags.get(), self)
523+
no_preset_tags_action.setDisabled(True)
524+
add_tag_menu.addAction(no_preset_tags_action)
441525

442526
menu.exec_(self.mapToGlobal(position))
443527

@@ -450,5 +534,7 @@ def update_style(self, palette=None):
450534
AssetCardStyleManager.apply_style(self, self._is_selected, self._is_hovered, palette=palette)
451535
AssetCardStyleManager.apply_label_style(self.name_label)
452536
AssetCardStyleManager.apply_image_label_style(self.image_label)
453-
AssetCardStyleManager.apply_tags_button_style(self.tags_button, self.tags_button.height())
537+
is_custom_tag_empty = self._custom_tags_full_text == self._tr_no_custom_tags.get()
538+
AssetCardStyleManager.apply_tags_button_style(self.custom_tags_button, self.custom_tags_button.height(), is_preset=False, is_tag_empty=is_custom_tag_empty)
539+
AssetCardStyleManager.apply_tags_button_style(self.preset_tags_button, self.preset_tags_button.height(), is_preset=True)
454540
self._update_elided_text()

src/preppipe_gui_pyside6/forms/assetbrowserwidget.ui

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,20 @@
5353
</property>
5454
<layout class="QVBoxLayout" name="categoriesLayout">
5555
<item>
56-
<widget class="QListWidget" name="categoriesListWidget"/>
56+
<widget class="QTreeWidget" name="categoriesTreeWidget">
57+
<property name="sizePolicy">
58+
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
59+
<horstretch>0</horstretch>
60+
<verstretch>0</verstretch>
61+
</sizepolicy>
62+
</property>
63+
<property name="headerHidden">
64+
<bool>true</bool>
65+
</property>
66+
<property name="frameStyle">
67+
<enum>QFrame::NoFrame</enum>
68+
</property>
69+
</widget>
5770
</item>
5871

5972
</layout>

0 commit comments

Comments
 (0)