refactor: Export-Schreibpfad aus dem Controller lösen

Etappe 4 der Controller-Zerlegung. `ExportService`
(src/whiteboard/io/export_service.gd) schreibt SVG und PDF, legt den
Exportordner an, bildet Pfade und bereinigt Dateinamen.

- `export_svg_to_path`/`export_pdf_to_path` delegieren nur noch; sie bleiben
  als Einstiegspunkte, weil Tests sie direkt aufrufen.
- Der Saisonvergleich nutzt denselben Pfadbau statt eigener Zeilen.
- Test: Pfadbildung, Namensbereinigung und ein tatsächlich geschriebenes PDF.

Bewusst im Controller: Export- und Abschluss-Dialog (Szene), PNG-Export
(SubViewport) und "im Dateimanager öffnen".

Controller: 8.566 -> 8.529 Zeilen. Kein Verhalten geändert, Suite grün.
Nächste Etappe laut Notiz: SeasonPanel.
This commit is contained in:
torsten 2026-11-13 13:28:03 +01:00
parent a3d35076dc
commit 7ee5e36af4
6 changed files with 117 additions and 59 deletions

View file

@ -95,7 +95,7 @@ const TOOL_ELEMENT_TYPES := [
PathSegment.ElementType.SPIN,
]
const ROUTINES_DIR := "user://routines"
const EXPORTS_DIR := "user://exports"
const EXPORTS_DIR := ExportService.EXPORTS_DIR
const EXPORT_SHEET_SCENE := preload("res://scenes/export/export_sheet.tscn")
const PRESENTATION_3D_SCENE_PATH := "res://scenes/presentation3d/presentation_3d.tscn"
const PLAYBACK_PLAY_ICON := preload("res://assets/icons/playback/play.svg")
@ -1000,20 +1000,16 @@ func _on_upgrade_pdf_pressed() -> void:
if pdf.is_empty():
_show_message(I18n.t("Export fehlgeschlagen."))
return
_ensure_exports_dir()
var stem := _normalize_file_stem(_routine.title)
if stem.is_empty():
stem = "kuer"
ExportService.ensure_dir()
var season := _upgrade_target_season.replace("/", "-")
var path := EXPORTS_DIR.path_join(
"%s_saisonvergleich_%s.pdf" % [stem, season]
var path := ExportService.build_path(
ExportService.normalize_stem(_routine.title),
"_saisonvergleich_" + season,
"pdf"
)
var file := FileAccess.open(path, FileAccess.WRITE)
if file == null:
if not ExportService.write_bytes(path, pdf):
_show_message(I18n.t("Export fehlgeschlagen."))
return
file.store_buffer(pdf)
file.close()
_last_export_path = ProjectSettings.globalize_path(path)
upgrade_dialog.hide()
_show_export_done()
@ -5810,7 +5806,7 @@ func _on_save_as_confirmed() -> void:
_show_message("Bitte einen Namen für die Kür eingeben.")
return
var stem := _normalize_file_stem(name)
var stem := ExportService.normalize_stem(name)
var path := ROUTINES_DIR.path_join(stem + ROUTINE_EXTENSION)
var apply := func() -> void:
_routine.title = name
@ -7327,7 +7323,7 @@ func _on_export_confirmed() -> void:
_show_message("Bitte einen Dateinamen eingeben.")
return
var stem := _normalize_file_stem(name)
var stem := ExportService.normalize_stem(name)
var scope_section_id := ""
var scope_suffix := ""
if export_scope_option.selected == 1:
@ -7335,7 +7331,7 @@ func _on_export_confirmed() -> void:
_show_message("Kein aktiver Abschnitt vorhanden.")
return
scope_section_id = _active_section.section_id
scope_suffix = "_" + _normalize_file_stem(_active_section.title)
scope_suffix = "_" + ExportService.normalize_stem(_active_section.title)
var format := export_format_option.get_item_id(
export_format_option.selected
@ -7360,7 +7356,7 @@ func _perform_export(
format: int,
scope_section_id: String
) -> void:
_ensure_exports_dir()
ExportService.ensure_dir()
var success := false
match format:
@ -7432,33 +7428,11 @@ func _on_export_copy_path_pressed() -> void:
func export_svg_to_path(path: String, section_id: String = "") -> bool:
if _routine == null:
return false
var svg := RoutineSvgExporter.build(_routine, section_id)
if svg.is_empty():
return false
var file := FileAccess.open(path, FileAccess.WRITE)
if file == null:
return false
file.store_string(svg)
file.close()
return true
return ExportService.write_svg(_routine, path, section_id)
func export_pdf_to_path(path: String, section_id: String = "") -> bool:
if _routine == null:
return false
var pdf := RoutinePdfExporter.build(_routine, section_id)
if pdf.is_empty():
return false
var file := FileAccess.open(path, FileAccess.WRITE)
if file == null:
return false
file.store_buffer(pdf)
file.close()
return true
return ExportService.write_pdf(_routine, path, section_id)
func export_png_to_path(path: String, section_id: String = "") -> bool:
@ -7488,13 +7462,10 @@ func export_png_to_path(path: String, section_id: String = "") -> bool:
return image.save_png(path) == OK
func _ensure_exports_dir() -> void:
if not DirAccess.dir_exists_absolute(EXPORTS_DIR):
DirAccess.make_dir_recursive_absolute(EXPORTS_DIR)
func _open_exports_folder() -> void:
_ensure_exports_dir()
ExportService.ensure_dir()
var folder := ProjectSettings.globalize_path(EXPORTS_DIR)
var error := OS.shell_show_in_file_manager(folder)
if error != OK:
@ -7693,7 +7664,7 @@ func _on_rename_routine_confirmed() -> void:
if new_title.is_empty():
_show_message("Bitte einen Namen für die Kür eingeben.")
return
var new_stem := _normalize_file_stem(new_title)
var new_stem := ExportService.normalize_stem(new_title)
var old_stem := old_file.get_basename()
if new_stem != old_stem:
for extension: String in [
@ -7946,14 +7917,6 @@ func _on_confirm_discard_canceled() -> void:
_pending_action = Callable()
func _normalize_file_stem(name: String) -> String:
var stem := name.strip_edges().to_lower()
for character: String in ["/", "\\", ":", "*", "?", "\"", "<", ">", "|"]:
stem = stem.replace(character, "")
stem = stem.replace(" ", "_")
if stem.is_empty():
stem = "kuer"
return stem
func _show_message(text: String) -> void:

View file

@ -0,0 +1,71 @@
class_name ExportService
extends RefCounted
## Writes exported documents and builds their paths.
##
## The dialogs ("Export", "Export abgeschlossen") stay in the controller; this
## service only knows the export folder, the file naming and how to serialise
## SVG and PDF. PNG needs a viewport and therefore stays in the controller.
const EXPORTS_DIR := "user://exports"
static func ensure_dir() -> void:
if not DirAccess.dir_exists_absolute(EXPORTS_DIR):
DirAccess.make_dir_recursive_absolute(EXPORTS_DIR)
## Lower-case, space-free file stem. Falls back to "kuer" for empty titles.
static func normalize_stem(name: String) -> String:
var stem := name.strip_edges().to_lower()
for character: String in ["/", "\\", ":", "*", "?", "\"", "<", ">", "|"]:
stem = stem.replace(character, "")
stem = stem.replace(" ", "_")
if stem.is_empty():
stem = "kuer"
return stem
## Full path inside the export folder, e.g. "user://exports/kuer_gold.pdf".
static func build_path(stem: String, suffix: String, extension: String) -> String:
return EXPORTS_DIR.path_join(stem + suffix + "." + extension)
static func write_bytes(path: String, bytes: PackedByteArray) -> bool:
if bytes.is_empty():
return false
var file := FileAccess.open(path, FileAccess.WRITE)
if file == null:
return false
file.store_buffer(bytes)
file.close()
return true
static func write_svg(
routine: Routine,
path: String,
section_id: String = ""
) -> bool:
if routine == null:
return false
var svg := RoutineSvgExporter.build(routine, section_id)
if svg.is_empty():
return false
var file := FileAccess.open(path, FileAccess.WRITE)
if file == null:
return false
file.store_string(svg)
file.close()
return true
static func write_pdf(
routine: Routine,
path: String,
section_id: String = ""
) -> bool:
if routine == null:
return false
var pdf := RoutinePdfExporter.build(routine, section_id)
return write_bytes(path, pdf)

View file

@ -0,0 +1 @@
uid://cl02cld4fbckd

View file

@ -106,6 +106,23 @@ func _run() -> void:
"the second section excludes the spin of section one"
)
# ExportService: Pfadbildung, Namensbereinigung und Schreiben.
_check(
ExportService.build_path("kuer", "_gold", "pdf").ends_with("kuer_gold.pdf"),
"the export path carries stem, suffix and extension"
)
_check(
ExportService.normalize_stem("Kür 2026/27: Gold?").begins_with("kür_2026"),
"the file stem drops forbidden characters"
)
var service_path := ExportService.build_path("service_test", "", "pdf")
_check(ExportService.write_pdf(routine, service_path), "the service writes a PDF")
_check(
FileAccess.file_exists(service_path),
"the written PDF exists"
)
DirAccess.remove_absolute(service_path)
var pdf := RoutinePdfExporter.build(routine)
var pdf_text := pdf.get_string_from_ascii()
_check(pdf.size() > 1000, "PDF export produces data")