@@ -14,6 +14,7 @@ class EditorLogic(QObject):
1414 Handles the business logic for the editor view, including file list management.
1515 """
1616 file_list_changed = pyqtSignal (list )
17+ file_list_with_tree_changed = pyqtSignal (list , dict ) # (files, folder_map)
1718
1819 def __init__ (self , controller , parent = None ):
1920 super ().__init__ (parent )
@@ -77,16 +78,27 @@ def add_folder(self, folder_path: str):
7778 if not folder_path or not os .path .isdir (folder_path ):
7879 return
7980
80- image_extensions = {'.png' , '.jpg' , '.jpeg' , '.bmp' , '.webp' }
81- try :
82- files_in_folder = [
83- os .path .join (folder_path , f )
84- for f in os .listdir (folder_path )
85- if os .path .splitext (f )[1 ].lower () in image_extensions
86- ]
87- self .add_files (files_in_folder )
88- except OSError as e :
89- print (f"Error reading folder { folder_path } : { e } " )
81+ # 检查是否是第一次添加文件(列表为空)
82+ is_first_add = len (self .source_files ) == 0
83+
84+ # 添加文件夹路径到source_files,让FileListView创建树形结构
85+ if folder_path not in self .source_files :
86+ self .source_files .append (folder_path )
87+ self .file_list_changed .emit (self .source_files )
88+
89+ # 如果是第一次添加,自动加载第一个图片
90+ if is_first_add :
91+ # 获取文件夹中的第一个图片
92+ image_extensions = {'.png' , '.jpg' , '.jpeg' , '.bmp' , '.webp' }
93+ try :
94+ for root , dirs , files in os .walk (folder_path ):
95+ for f in sorted (files ):
96+ if os .path .splitext (f )[1 ].lower () in image_extensions :
97+ first_image = os .path .join (root , f )
98+ self .load_image_into_editor (first_image )
99+ return
100+ except OSError as e :
101+ print (f"Error reading folder { folder_path } : { e } " )
90102
91103 @pyqtSlot (list )
92104 def add_files_from_paths (self , paths : List [str ]):
@@ -192,41 +204,32 @@ def clear_list(self):
192204 def load_file_lists (self , source_files : List [str ], translated_files : List [str ], folder_map : dict = None ):
193205 """
194206 Receives the file lists from the coordinator to populate the editor.
195- folder_map: 文件到文件夹的映射,用于支持文件夹分组显示
207+ folder_map: 文件到文件夹的映射,用于支持文件夹分组显示(key是源文件路径)
196208 """
209+ print (f"[DEBUG editor_logic] load_file_lists called" )
210+ print (f"[DEBUG editor_logic] source_files: { source_files } " )
211+ print (f"[DEBUG editor_logic] translated_files: { translated_files } " )
212+ print (f"[DEBUG editor_logic] folder_map: { folder_map } " )
213+
197214 self .source_files = source_files
198215 self .translated_files = translated_files
199216 self .translation_map_cache .clear () # Clear cache when lists change
200217
201- # 如果提供了folder_map,将文件按文件夹分组
202- files_to_show = self .translated_files if self .translated_files else self .source_files
218+ # 选择要显示的文件列表
219+ # 如果有翻译后的文件,显示源文件列表(但实际加载翻译后的文件)
220+ # 这样文件夹结构和主页视图保持一致
221+ files_to_show = self .source_files
222+
223+ print (f"[DEBUG editor_logic] files_to_show: { files_to_show } " )
224+ print (f"[DEBUG editor_logic] Has folder_map: { bool (folder_map )} " )
203225
226+ # 如果有folder_map,使用树形结构显示
204227 if folder_map :
205- # 按文件夹分组
206- folder_groups = {}
207- single_files = []
208-
209- for file_path in files_to_show :
210- folder = folder_map .get (file_path )
211- if folder :
212- if folder not in folder_groups :
213- folder_groups [folder ] = []
214- folder_groups [folder ].append (file_path )
215- else :
216- single_files .append (file_path )
217-
218- # 构建文件列表:按文件夹分组,但只添加文件,不添加文件夹路径
219- # 这样可以保持文件夹分组的顺序,但不会让FileListView重新展开文件夹
220- grouped_list = []
221- for folder , files in sorted (folder_groups .items ()):
222- # 只添加文件,不添加文件夹路径
223- # 文件会按文件夹分组显示,但不会重新展开文件夹
224- grouped_list .extend (files )
225- grouped_list .extend (single_files ) # 添加单独的文件
226-
227- self .file_list_changed .emit (grouped_list )
228+ print (f"[DEBUG editor_logic] Emitting file_list_with_tree_changed signal" )
229+ self .file_list_with_tree_changed .emit (files_to_show , folder_map )
228230 else :
229- # 发射翻译后的文件列表,而不是源文件列表
231+ # 否则使用平铺列表
232+ print (f"[DEBUG editor_logic] Emitting file_list_changed signal" )
230233 self .file_list_changed .emit (files_to_show )
231234
232235 @pyqtSlot (str )
0 commit comments