Skip to content

Commit 86d8790

Browse files
author
Shengjie Xu
committed
[GUI] add composition selection for Background image packs in ImagePackWidget
1 parent 7658b9d commit 86d8790

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

src/preppipe_gui_pyside6/forms/imagepackwidget.ui

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</sizepolicy>
7474
</property>
7575
<property name="title">
76-
<string>图片选择</string>
76+
<string>差分选择</string>
7777
</property>
7878
</widget>
7979
</item>
@@ -86,7 +86,7 @@
8686
</sizepolicy>
8787
</property>
8888
<property name="title">
89-
<string>选区参数</string>
89+
<string>选区修改参数</string>
9090
</property>
9191
</widget>
9292
</item>

src/preppipe_gui_pyside6/toolwidgets/imagepack.py

+69-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ..forms.generated.ui_imagepackwidget import Ui_ImagePackWidget
1010
from ..componentwidgets.imageviewerwidget import ImageViewerWidget
1111
from ..componentwidgets.maskinputwidget import MaskInputWidget
12+
from ..util.wait import WaitDialog
1213

1314
TR_gui_tool_imagepack = TranslationDomain("gui_tool_imagepack")
1415

@@ -68,25 +69,45 @@ def getChildTools(cls, packid : str | None = None, category_kind : ImagePackDesc
6869
current_pack : ImagePack
6970

7071
# 当前的组合、选区参数
71-
current_index : int | list[int] | None = None
72+
current_index : int | list[int]
7273
current_mask_params : list[typing.Any] | None = None
7374
mask_param_widgets : list[MaskInputWidget]
75+
background_radio_buttons : list[QRadioButton] | None = None
76+
77+
_tr_composition_selection = TR_gui_tool_imagepack.tr("composition_selection",
78+
en="Composition Selection",
79+
zh_cn="差分选择",
80+
zh_hk="差分選擇",
81+
)
82+
_tr_mask_parameters = TR_gui_tool_imagepack.tr("mask_parameters",
83+
en="Customization Parameters",
84+
zh_cn="选区修改参数",
85+
zh_hk="選區修改參數",
86+
)
7487

7588
def __init__(self, parent: QWidget):
7689
super(ImagePackWidget, self).__init__(parent)
7790
self.ui = Ui_ImagePackWidget()
7891
self.ui.setupUi(self)
7992
self.viewer = ImageViewerWidget(self, context_menu_callback=self.populate_image_rightclick_menu)
8093
self.ui.viewerLayout.addWidget(self.viewer)
81-
self.current_index = None
94+
self.bind_text(self.ui.sourceGroupBox.setTitle, self._tr_composition_selection)
95+
self.bind_text(self.ui.forkParamGroupBox.setTitle, self._tr_mask_parameters)
96+
self.current_index = 0
8297
self.current_mask_params = None
8398
self.mask_param_widgets = []
99+
self.background_radio_buttons = None
84100

85101
_tr_no_mask = TR_gui_tool_imagepack.tr("no_mask",
86-
en="This image pack contains no customizable mask regions.",
102+
en="This image pack contains no customizable regions.",
87103
zh_cn="该图片包没有可修改的选区。",
88104
zh_hk="該圖片包沒有可修改的選區。",
89105
)
106+
_tr_no_composite = TR_gui_tool_imagepack.tr("no_composite",
107+
en="This image pack contains no compositions.",
108+
zh_cn="该图片包没有差分组合。",
109+
zh_hk="該圖片包沒有差分組合。",
110+
)
90111

91112
def setData(self, packid : str | None = None, category_kind : ImagePackDescriptor.ImagePackType | None = None):
92113
if category_kind is not None:
@@ -119,14 +140,41 @@ def setData(self, packid : str | None = None, category_kind : ImagePackDescripto
119140
layout.addRow(nameLabel, inputWidget)
120141
self.add_translatable_widget_child(inputWidget)
121142
self.mask_param_widgets.append(inputWidget)
122-
inputWidget.valueChanged.connect(functools.partial(self.handleMaskParamUpdate, index), Qt.QueuedConnection)
143+
inputWidget.valueChanged.connect(functools.partial(self.handleMaskParamUpdate, index))
123144
else:
124145
layout = QVBoxLayout()
125146
label = QLabel(self._tr_no_mask.get())
126147
self.bind_text(label.setText, self._tr_no_mask)
127148
label.setWordWrap(True)
128149
layout.addWidget(label)
129150
self.ui.forkParamGroupBox.setLayout(layout)
151+
if len(self.descriptor.composites_code) > 0:
152+
match self.descriptor.packtype:
153+
case ImagePackDescriptor.ImagePackType.BACKGROUND:
154+
layout = QVBoxLayout()
155+
self.background_radio_buttons = []
156+
for index, code in enumerate(self.descriptor.composites_code):
157+
radio_button = QRadioButton(code)
158+
if name := self.descriptor.composites_references.get(code, None):
159+
self.bind_text(radio_button.setText, name)
160+
self.background_radio_buttons.append(radio_button)
161+
if index == 0:
162+
radio_button.setChecked(True)
163+
radio_button.toggled.connect(self.handleBackgroundCompositionChange)
164+
layout.addWidget(radio_button)
165+
self.ui.sourceGroupBox.setLayout(layout)
166+
case ImagePackDescriptor.ImagePackType.CHARACTER:
167+
# TODO
168+
pass
169+
case _:
170+
raise NotImplementedError(f"Pack type {self.descriptor.packtype} is not supported")
171+
else:
172+
layout = QVBoxLayout()
173+
label = QLabel(self._tr_no_composite.get())
174+
self.bind_text(label.setText, self._tr_no_composite)
175+
label.setWordWrap(True)
176+
layout.addWidget(label)
177+
self.ui.sourceGroupBox.setLayout(layout)
130178
# setData() 需要尽快返回,让组件先显示出来,后续再更新内容
131179
QMetaObject.invokeMethod(self, "updateCurrentImage", Qt.QueuedConnection)
132180

@@ -136,7 +184,22 @@ def handleMaskParamUpdate(self, index : int):
136184
raise RuntimeError("current_mask_params is None")
137185
widget = self.mask_param_widgets[index]
138186
self.current_mask_params[index] = widget.getValue()
139-
self.updateCurrentPack()
187+
WaitDialog.long_running_operation_start()
188+
QMetaObject.invokeMethod(self, "updateCurrentPack", Qt.QueuedConnection)
189+
190+
@Slot()
191+
def handleBackgroundCompositionChange(self):
192+
if self.background_radio_buttons is None:
193+
raise RuntimeError("background_radio_buttons is None")
194+
new_index = 0
195+
for index, button in enumerate(self.background_radio_buttons):
196+
if button.isChecked():
197+
new_index = index
198+
break
199+
if self.current_index == new_index:
200+
return
201+
self.current_index = new_index
202+
QMetaObject.invokeMethod(self, "updateCurrentImage", Qt.QueuedConnection)
140203

141204
@Slot()
142205
def updateCurrentPack(self):
@@ -151,8 +214,7 @@ def updateCurrentImage(self):
151214
if isinstance(self.current_index, list):
152215
img = self.current_pack.get_composed_image_lower(self.current_index)
153216
else:
154-
index = self.current_index if self.current_index is not None else 0
155-
img = self.current_pack.get_composed_image(index)
217+
img = self.current_pack.get_composed_image(self.current_index)
156218
self.set_image(img)
157219

158220
def set_image(self, image: PIL.Image.Image | ImageWrapper):

src/preppipe_gui_pyside6/util/wait.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from PySide6.QtCore import *
2+
3+
class WaitDialog:
4+
# TODO
5+
@staticmethod
6+
def long_running_operation_start():
7+
QCoreApplication.processEvents()

0 commit comments

Comments
 (0)