9
9
from ..forms .generated .ui_imagepackwidget import Ui_ImagePackWidget
10
10
from ..componentwidgets .imageviewerwidget import ImageViewerWidget
11
11
from ..componentwidgets .maskinputwidget import MaskInputWidget
12
+ from ..util .wait import WaitDialog
12
13
13
14
TR_gui_tool_imagepack = TranslationDomain ("gui_tool_imagepack" )
14
15
@@ -68,25 +69,45 @@ def getChildTools(cls, packid : str | None = None, category_kind : ImagePackDesc
68
69
current_pack : ImagePack
69
70
70
71
# 当前的组合、选区参数
71
- current_index : int | list [int ] | None = None
72
+ current_index : int | list [int ]
72
73
current_mask_params : list [typing .Any ] | None = None
73
74
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
+ )
74
87
75
88
def __init__ (self , parent : QWidget ):
76
89
super (ImagePackWidget , self ).__init__ (parent )
77
90
self .ui = Ui_ImagePackWidget ()
78
91
self .ui .setupUi (self )
79
92
self .viewer = ImageViewerWidget (self , context_menu_callback = self .populate_image_rightclick_menu )
80
93
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
82
97
self .current_mask_params = None
83
98
self .mask_param_widgets = []
99
+ self .background_radio_buttons = None
84
100
85
101
_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." ,
87
103
zh_cn = "该图片包没有可修改的选区。" ,
88
104
zh_hk = "該圖片包沒有可修改的選區。" ,
89
105
)
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
+ )
90
111
91
112
def setData (self , packid : str | None = None , category_kind : ImagePackDescriptor .ImagePackType | None = None ):
92
113
if category_kind is not None :
@@ -119,14 +140,41 @@ def setData(self, packid : str | None = None, category_kind : ImagePackDescripto
119
140
layout .addRow (nameLabel , inputWidget )
120
141
self .add_translatable_widget_child (inputWidget )
121
142
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 ))
123
144
else :
124
145
layout = QVBoxLayout ()
125
146
label = QLabel (self ._tr_no_mask .get ())
126
147
self .bind_text (label .setText , self ._tr_no_mask )
127
148
label .setWordWrap (True )
128
149
layout .addWidget (label )
129
150
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 )
130
178
# setData() 需要尽快返回,让组件先显示出来,后续再更新内容
131
179
QMetaObject .invokeMethod (self , "updateCurrentImage" , Qt .QueuedConnection )
132
180
@@ -136,7 +184,22 @@ def handleMaskParamUpdate(self, index : int):
136
184
raise RuntimeError ("current_mask_params is None" )
137
185
widget = self .mask_param_widgets [index ]
138
186
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 )
140
203
141
204
@Slot ()
142
205
def updateCurrentPack (self ):
@@ -151,8 +214,7 @@ def updateCurrentImage(self):
151
214
if isinstance (self .current_index , list ):
152
215
img = self .current_pack .get_composed_image_lower (self .current_index )
153
216
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 )
156
218
self .set_image (img )
157
219
158
220
def set_image (self , image : PIL .Image .Image | ImageWrapper ):
0 commit comments