revert 第一次提交
This commit is contained in:
taiyi 2025-09-03 11:55:31 +08:00
parent f868be562f
commit fc83bb0bfc
4 changed files with 17 additions and 81 deletions

View File

@ -177,8 +177,7 @@ class CanvasWidget(tk.Canvas):
# 绘制背景图(如果有) # 绘制背景图(如果有)
if self.project.template.bg_image: if self.project.template.bg_image:
# 使用更稳定的缓存键,包含图像的标识和尺寸 bg_key = id(self.project.template.bg_image)
bg_key = (id(self.project.template.bg_image), template_w, template_h, self.zoom)
if bg_key not in self.image_cache or self.image_cache[bg_key][0] != self.zoom: if bg_key not in self.image_cache or self.image_cache[bg_key][0] != self.zoom:
# 缩放背景图并缓存 # 缩放背景图并缓存
scaled_bg = self.project.template.bg_image.resize( scaled_bg = self.project.template.bg_image.resize(

View File

@ -264,35 +264,19 @@ class MainWindow(tk.Tk):
"""设置模板""" """设置模板"""
dialog = TemplateDialog(self, self.project.template) dialog = TemplateDialog(self, self.project.template)
if dialog.result: if dialog.result:
if dialog.result["type"] == "preset_loaded": if dialog.result["type"] == "preset":
# 直接使用已加载的模板设置不需要重新创建Template对象
# 模板已经在对话框中加载完成,只需确保画布刷新
pass
elif dialog.result["type"] == "preset":
# 确保传递所有参数并创建模板
self.project.template = Template( self.project.template = Template(
ratio=tuple(dialog.result["ratio"]), dialog.result["ratio"],
width_px=dialog.result["width"], dialog.result["width"]
height_px=dialog.result.get("height")
) )
# 设置背景颜色
self.project.template.bg_color = dialog.result["bg_color"]
# 设置背景图
if "bg_image" in dialog.result:
self.project.template.bg_image = dialog.result["bg_image"]
else: else:
self.project.template = Template() self.project.template = Template()
self.project.template.set_custom_size( self.project.template.set_custom_size(
dialog.result["width"], dialog.result["width"],
dialog.result["height"] dialog.result["height"]
) )
# 设置背景颜色
self.project.template.bg_color = dialog.result["bg_color"]
# 设置背景图
if "bg_image" in dialog.result:
self.project.template.bg_image = dialog.result["bg_image"]
# 确保更新画布显示 self.project.template.bg_color = dialog.result["bg_color"]
self.canvas.draw_preview() self.canvas.draw_preview()
def batch_export(self): def batch_export(self):

View File

@ -119,14 +119,10 @@ class ForegroundImage:
class Template: class Template:
"""模板对象,存储尺寸和背景信息""" """模板对象,存储尺寸和背景信息"""
def __init__(self, ratio=(16, 9), width_px=1200, height_px=None): def __init__(self, ratio=(16, 9), width_px=1200):
self.ratio = ratio self.ratio = ratio
self.width_px = width_px self.width_px = width_px
# 如果提供了高度,使用提供的高度,否则根据比例计算 self.height_px = int(width_px * ratio[1] / ratio[0])
if height_px is not None:
self.height_px = height_px
else:
self.height_px = int(width_px * ratio[1] / ratio[0])
self.bg_image = None self.bg_image = None
self.bg_color = "#FFFFFF" # 默认白色背景 self.bg_color = "#FFFFFF" # 默认白色背景
@ -163,7 +159,8 @@ class Template:
@classmethod @classmethod
def from_dict(cls, data): def from_dict(cls, data):
"""从字典创建对象""" """从字典创建对象"""
template = cls(data["ratio"], data["width_px"], data["height_px"]) template = cls(data["ratio"], data["width_px"])
template.height_px = data["height_px"]
template.bg_color = data.get("bg_color", "#FFFFFF") template.bg_color = data.get("bg_color", "#FFFFFF")
return template return template

View File

@ -110,8 +110,6 @@ class TemplateDialog(tk.Toplevel):
messagebox.showwarning("警告", "请先选择一个预设") messagebox.showwarning("警告", "请先选择一个预设")
return return
# 设置标志以避免在刷新界面时更改尺寸
self._loading_preset = True
if self.preset_manager.load_preset_to_template(selected_preset, self.current_template): if self.preset_manager.load_preset_to_template(selected_preset, self.current_template):
# 更新界面显示 # 更新界面显示
self.refresh_from_template() self.refresh_from_template()
@ -120,8 +118,6 @@ class TemplateDialog(tk.Toplevel):
messagebox.showinfo("成功", f"已加载预设: {selected_preset}") messagebox.showinfo("成功", f"已加载预设: {selected_preset}")
else: else:
messagebox.showerror("错误", "加载预设失败") messagebox.showerror("错误", "加载预设失败")
# 清除标志
self._loading_preset = False
def save_current_as_preset(self): def save_current_as_preset(self):
"""将当前设置保存为预设""" """将当前设置保存为预设"""
@ -181,13 +177,10 @@ class TemplateDialog(tk.Toplevel):
self.show_custom_frame() self.show_custom_frame()
else: else:
self.hide_custom_frame() self.hide_custom_frame()
# 仅在非预设加载情况下设置默认尺寸 # 设置默认尺寸
# 当从预设加载时,保持预设的尺寸不变 w, h = map(int, self.ratio_var.get().split(":"))
if not (hasattr(self, '_loading_preset') and self._loading_preset): self.width_var.set(1920 if w > h else 1080)
# 设置默认尺寸 self.height_var.set(int(self.width_var.get() * h / w))
w, h = map(int, self.ratio_var.get().split(":"))
self.width_var.set(1920 if w > h else 1080)
self.height_var.set(int(self.width_var.get() * h / w))
def show_custom_frame(self): def show_custom_frame(self):
"""显示自定义尺寸框""" """显示自定义尺寸框"""
@ -208,8 +201,6 @@ class TemplateDialog(tk.Toplevel):
if file_path: if file_path:
if self.current_template.load_background(file_path): if self.current_template.load_background(file_path):
self.bg_image_var.set("已设置") self.bg_image_var.set("已设置")
# 立即刷新画布预览
self.parent.get_canvas().draw_preview()
def clear_bg_image(self): def clear_bg_image(self):
"""清除背景图""" """清除背景图"""
@ -221,21 +212,7 @@ class TemplateDialog(tk.Toplevel):
# 如果选择了预设,则应用预设 # 如果选择了预设,则应用预设
selected_preset = self.preset_var.get() selected_preset = self.preset_var.get()
if selected_preset: if selected_preset:
if self.preset_manager.load_preset_to_template(selected_preset, self.current_template): if not self.preset_manager.load_preset_to_template(selected_preset, self.current_template):
# 刷新主窗口画布
self.parent.get_canvas().draw_preview()
# 设置返回结果,包含预设的所有信息
self.result = {
"type": "preset_loaded", # 修改类型以区分预设选择和预设加载
"ratio": self.current_template.ratio,
"width": self.current_template.width_px,
"height": self.current_template.height_px,
"bg_color": self.current_template.bg_color,
"bg_image": self.current_template.bg_image
}
self.destroy()
return
else:
messagebox.showerror("错误", "加载预设失败") messagebox.showerror("错误", "加载预设失败")
return return
@ -250,18 +227,15 @@ class TemplateDialog(tk.Toplevel):
"type": "custom", "type": "custom",
"width": width, "width": width,
"height": height, "height": height,
"bg_color": self.bg_color_var.get(), "bg_color": self.bg_color_var.get()
"bg_image": self.current_template.bg_image
} }
else: else:
w, h = map(int, self.ratio_var.get().split(":")) w, h = map(int, self.ratio_var.get().split(":"))
self.result = { self.result = {
"type": "preset", "type": "preset",
"ratio": tuple([w, h]), "ratio": (w, h),
"width": self.width_var.get(), "width": self.width_var.get(),
"height": self.height_var.get(), "bg_color": self.bg_color_var.get()
"bg_color": self.bg_color_var.get(),
"bg_image": self.current_template.bg_image
} }
self.destroy() self.destroy()
except Exception as e: except Exception as e:
@ -276,23 +250,5 @@ class TemplateDialog(tk.Toplevel):