cf1cdc86 by 柴进

处理mac打包后粘贴图片缩略图不可见的问题,处理历史history不可见问题

1 parent 68235b13
...@@ -511,6 +511,22 @@ class HistoryManager: ...@@ -511,6 +511,22 @@ class HistoryManager:
511 511
512 return timestamp 512 return timestamp
513 513
514 def _fix_history_path(self, stored_path: Path, timestamp: str) -> Path:
515 """修正历史记录中的路径,使其指向当前 base_path
516
517 存储迁移后,index 里的绝对路径可能指向旧位置(如 .app 内部),
518 而实际文件已在新的 base_path 下。用 timestamp + 文件名重建路径。
519 """
520 if stored_path.exists():
521 return stored_path
522
523 # 尝试在当前 base_path 下找到对应文件
524 corrected = self.base_path / timestamp / stored_path.name
525 if corrected.exists():
526 return corrected
527
528 return stored_path
529
514 def load_history_index(self) -> List[HistoryItem]: 530 def load_history_index(self) -> List[HistoryItem]:
515 """加载历史记录索引 531 """加载历史记录索引
516 532
...@@ -525,6 +541,28 @@ class HistoryManager: ...@@ -525,6 +541,28 @@ class HistoryManager:
525 data = json.load(f) 541 data = json.load(f)
526 542
527 history_items = [HistoryItem.from_dict(item) for item in data] 543 history_items = [HistoryItem.from_dict(item) for item in data]
544
545 # 修正可能过期的绝对路径(存储迁移后旧路径不再有效)
546 needs_save = False
547 for item in history_items:
548 fixed_gen = self._fix_history_path(item.generated_image_path, item.timestamp)
549 if fixed_gen != item.generated_image_path:
550 item.generated_image_path = fixed_gen
551 needs_save = True
552
553 fixed_refs = []
554 for ref_path in item.reference_image_paths:
555 fixed_ref = self._fix_history_path(ref_path, item.timestamp)
556 if fixed_ref != ref_path:
557 needs_save = True
558 fixed_refs.append(fixed_ref)
559 item.reference_image_paths = fixed_refs
560
561 # 路径修正后持久化,避免每次都修正
562 if needs_save:
563 self.logger.info("检测到历史记录路径变更,已自动修正")
564 self._save_history_index(history_items)
565
528 # 按时间戳倒序排列 566 # 按时间戳倒序排列
529 history_items.sort(key=lambda x: x.timestamp, reverse=True) 567 history_items.sort(key=lambda x: x.timestamp, reverse=True)
530 return history_items 568 return history_items
...@@ -2093,11 +2131,13 @@ class ImageGeneratorWindow(QMainWindow): ...@@ -2093,11 +2131,13 @@ class ImageGeneratorWindow(QMainWindow):
2093 """Update image preview thumbnails""" 2131 """Update image preview thumbnails"""
2094 self.logger.info(f"更新图片预览,共 {len(self.uploaded_images)} 张图片") 2132 self.logger.info(f"更新图片预览,共 {len(self.uploaded_images)} 张图片")
2095 try: 2133 try:
2096 # Clear existing previews 2134 # Clear existing previews - 立即删除而非 deleteLater,避免布局刷新时序问题
2097 while self.img_layout.count() > 1: # Keep the stretch 2135 while self.img_layout.count() > 1: # Keep the stretch
2098 item = self.img_layout.takeAt(0) 2136 item = self.img_layout.takeAt(0)
2099 if item.widget(): 2137 widget = item.widget()
2100 item.widget().deleteLater() 2138 if widget:
2139 widget.setParent(None)
2140 widget.deleteLater()
2101 2141
2102 # Add thumbnails 2142 # Add thumbnails
2103 for idx, file_path in enumerate(self.uploaded_images): 2143 for idx, file_path in enumerate(self.uploaded_images):
...@@ -2157,6 +2197,10 @@ class ImageGeneratorWindow(QMainWindow): ...@@ -2157,6 +2197,10 @@ class ImageGeneratorWindow(QMainWindow):
2157 except Exception as e: 2197 except Exception as e:
2158 self.logger.error(f"创建缩略图失败: {file_path}, 错误: {str(e)}", exc_info=True) 2198 self.logger.error(f"创建缩略图失败: {file_path}, 错误: {str(e)}", exc_info=True)
2159 2199
2200 # 强制刷新布局和滚动区域(macOS 打包环境下可能需要)
2201 self.img_container.adjustSize()
2202 self.img_scroll.update()
2203
2160 except Exception as e: 2204 except Exception as e:
2161 self.logger.error(f"更新图片预览失败: {str(e)}", exc_info=True) 2205 self.logger.error(f"更新图片预览失败: {str(e)}", exc_info=True)
2162 2206
......