Skip to content

Commit ba5eea9

Browse files
committed
v1.8.7 - 优化翻译导入功能和文件夹收藏功能
## UI改进 - 文件夹选择对话框新增收藏功能 - 收藏的文件夹显示在快速访问面板的收藏夹分组中 - 点击星星图标即可收藏/取消收藏文件夹 ## 功能优化 - 支持紧凑JSON格式和模板格式的翻译文件解析 - 修复导入翻译时translation为空导致渲染无文字的问题 - 修复正则表达式无法匹配translated字段的问题 - load_text模式下translation为空时自动使用原文渲染 ## 配置管理 - 收藏文件夹统一通过config_service管理 - config_service保存配置时保留favorite_folders字段 - 模板文件config-example.json保持干净
1 parent a03c920 commit ba5eea9

7 files changed

Lines changed: 693 additions & 39 deletions

File tree

desktop_qt_ui/app_logic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ def add_folder(self):
497497
folders = select_folders(
498498
parent=None,
499499
start_dir=last_dir,
500-
multi_select=True
500+
multi_select=True,
501+
config_service=self.config_service
501502
)
502503

503504
if folders:

desktop_qt_ui/core/config_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class CliSettings(BaseModel):
125125
class AppSection(BaseModel):
126126
last_open_dir: str = '.'
127127
last_output_path: str = ""
128-
favorite_folders: List[str] = Field(default_factory=list)
128+
favorite_folders: Optional[List[str]] = None
129129

130130
class AppSettings(BaseModel):
131131
app: AppSection = Field(default_factory=AppSection)

desktop_qt_ui/services/config_service.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ def save_config_file(self, config_path: Optional[str] = None) -> bool:
188188
# 获取当前配置
189189
config_dict = self.current_config.dict()
190190

191+
# 读取现有配置,保留favorite_folders
192+
existing_favorites = None
193+
if os.path.exists(save_path):
194+
try:
195+
with open(save_path, 'r', encoding='utf-8') as f:
196+
existing_config = json.load(f)
197+
existing_favorites = existing_config.get('app', {}).get('favorite_folders')
198+
except:
199+
pass
200+
191201
# 只有保存到模板配置时才重置临时状态
192202
is_default_config = save_path == self.default_config_path
193203
if is_default_config:
@@ -196,9 +206,17 @@ def save_config_file(self, config_path: Optional[str] = None) -> bool:
196206
config_dict['app'] = {}
197207
config_dict['app']['last_open_dir'] = '.'
198208
config_dict['app']['last_output_path'] = ''
209+
# 模板配置不保存favorite_folders
210+
config_dict['app'].pop('favorite_folders', None)
199211

200212
if 'cli' in config_dict:
201213
config_dict['cli']['verbose'] = False
214+
else:
215+
# 用户配置保留favorite_folders
216+
if existing_favorites is not None:
217+
if 'app' not in config_dict:
218+
config_dict['app'] = {}
219+
config_dict['app']['favorite_folders'] = existing_favorites
202220

203221
try:
204222
os.makedirs(os.path.dirname(save_path), exist_ok=True)

desktop_qt_ui/widgets/folder_dialog.py

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,14 @@ def editorEvent(self, event, model, option, index):
255255
class FolderDialog(QDialog):
256256
"""现代化文件夹选择对话框"""
257257

258-
def __init__(self, parent=None, start_dir: str = "", multi_select: bool = True):
258+
def __init__(self, parent=None, start_dir: str = "", multi_select: bool = True, config_service=None):
259259
super().__init__(parent)
260260
self.multi_select = multi_select
261261
self.selected_folders: List[str] = []
262262
self.history: List[str] = [] # 导航历史
263263
self.history_index = -1 # 当前历史位置
264264
self.favorite_folders: List[str] = [] # 收藏的文件夹
265+
self.config_service = config_service
265266

266267
self.setWindowTitle("选择文件夹" + (" (可多选)" if multi_select else ""))
267268
self.setMinimumSize(1000, 650)
@@ -1162,48 +1163,71 @@ def _get_config_path(self) -> str:
11621163
config_path = os.path.join(project_root, "examples", "config.json")
11631164

11641165
return config_path
1166+
1167+
def _get_favorites_config_path(self) -> str:
1168+
"""获取收藏文件夹配置文件路径(用户目录)"""
1169+
# 使用用户目录存储收藏,避免污染模板文件
1170+
user_config_dir = Path.home() / ".manga-translator-ui"
1171+
user_config_dir.mkdir(exist_ok=True)
1172+
return str(user_config_dir / "favorites.json")
11651173

11661174
def _load_favorite_folders(self):
11671175
"""从配置文件加载收藏文件夹"""
11681176
try:
1169-
config_path = self._get_config_path()
1170-
if os.path.exists(config_path):
1171-
with open(config_path, 'r', encoding='utf-8') as f:
1172-
config = json.load(f)
1173-
self.favorite_folders = config.get('app', {}).get('favorite_folders', [])
1177+
if self.config_service:
1178+
# 使用config_service加载
1179+
config = self.config_service.get_config()
1180+
self.favorite_folders = config.app.favorite_folders or []
1181+
else:
1182+
# 降级方案:直接读取文件
1183+
config_path = self._get_config_path()
1184+
if os.path.exists(config_path):
1185+
with open(config_path, 'r', encoding='utf-8') as f:
1186+
config_dict = json.load(f)
1187+
self.favorite_folders = config_dict.get('app', {}).get('favorite_folders', [])
1188+
else:
1189+
self.favorite_folders = []
11741190
except Exception as e:
11751191
print(f"加载收藏文件夹失败: {e}")
11761192
self.favorite_folders = []
11771193

11781194
def _save_favorite_folders(self):
11791195
"""保存收藏文件夹到配置文件"""
11801196
try:
1181-
config_path = self._get_config_path()
1182-
1183-
# 读取现有配置
1184-
config = {}
1185-
if os.path.exists(config_path):
1186-
try:
1187-
with open(config_path, 'r', encoding='utf-8') as f:
1188-
config = json.load(f)
1189-
except:
1190-
config = {}
1191-
1192-
# 确保 app 键存在
1193-
if 'app' not in config:
1194-
config['app'] = {}
1195-
1196-
# 确保 app 是字典类型
1197-
if not isinstance(config['app'], dict):
1198-
config['app'] = {}
1199-
1200-
# 更新收藏文件夹
1201-
config['app']['favorite_folders'] = self.favorite_folders
1202-
1203-
# 保存配置
1204-
os.makedirs(os.path.dirname(config_path), exist_ok=True)
1205-
with open(config_path, 'w', encoding='utf-8') as f:
1206-
json.dump(config, f, indent=2, ensure_ascii=False)
1197+
if self.config_service:
1198+
# 使用config_service保存
1199+
config = self.config_service.get_config()
1200+
config.app.favorite_folders = self.favorite_folders
1201+
self.config_service.set_config(config)
1202+
self.config_service.save_config_file()
1203+
else:
1204+
# 降级方案:直接写入文件
1205+
config_path = self._get_config_path()
1206+
1207+
# 读取现有配置
1208+
config_dict = {}
1209+
if os.path.exists(config_path):
1210+
try:
1211+
with open(config_path, 'r', encoding='utf-8') as f:
1212+
config_dict = json.load(f)
1213+
except:
1214+
config_dict = {}
1215+
1216+
# 确保 app 键存在
1217+
if 'app' not in config_dict:
1218+
config_dict['app'] = {}
1219+
1220+
# 确保 app 是字典类型
1221+
if not isinstance(config_dict['app'], dict):
1222+
config_dict['app'] = {}
1223+
1224+
# 更新收藏文件夹
1225+
config_dict['app']['favorite_folders'] = self.favorite_folders
1226+
1227+
# 保存配置
1228+
os.makedirs(os.path.dirname(config_path), exist_ok=True)
1229+
with open(config_path, 'w', encoding='utf-8') as f:
1230+
json.dump(config_dict, f, indent=2, ensure_ascii=False)
12071231

12081232
except Exception as e:
12091233
print(f"保存收藏文件夹失败: {e}")
@@ -1250,19 +1274,20 @@ def _refresh_shortcuts_tree(self):
12501274
self.folder_tree.viewport().update()
12511275

12521276

1253-
def select_folders(parent=None, start_dir: str = "", multi_select: bool = True) -> Optional[List[str]]:
1277+
def select_folders(parent=None, start_dir: str = "", multi_select: bool = True, config_service=None) -> Optional[List[str]]:
12541278
"""
12551279
显示文件夹选择对话框
12561280
12571281
Args:
12581282
parent: 父窗口
12591283
start_dir: 起始目录
12601284
multi_select: 是否支持多选
1285+
config_service: 配置服务实例
12611286
12621287
Returns:
12631288
选中的文件夹路径列表,如果取消则返回 None
12641289
"""
1265-
dialog = FolderDialog(parent, start_dir, multi_select)
1290+
dialog = FolderDialog(parent, start_dir, multi_select, config_service)
12661291
if dialog.exec() == QDialog.DialogCode.Accepted:
12671292
return dialog.get_selected_folders()
12681293
return None

0 commit comments

Comments
 (0)