From 7ee5e36af415ab2b05001d5efd7f744ce079f01e Mon Sep 17 00:00:00 2001 From: torsten Date: Fri, 13 Nov 2026 13:28:03 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20Export-Schreibpfad=20aus=20dem=20Co?= =?UTF-8?q?ntroller=20l=C3=B6sen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- docs/refactor-whiteboard-controller.md | 16 +++-- docs/roadmap.md | 4 +- .../whiteboard/ctrl/whiteboard_controller.gd | 67 ++++------------- .../src/whiteboard/io/export_service.gd | 71 +++++++++++++++++++ .../src/whiteboard/io/export_service.gd.uid | 1 + rinkplanner/tests/run_export_tests.gd | 17 +++++ 6 files changed, 117 insertions(+), 59 deletions(-) create mode 100644 rinkplanner/src/whiteboard/io/export_service.gd create mode 100644 rinkplanner/src/whiteboard/io/export_service.gd.uid diff --git a/docs/refactor-whiteboard-controller.md b/docs/refactor-whiteboard-controller.md index 944e26a..6a5e909 100644 --- a/docs/refactor-whiteboard-controller.md +++ b/docs/refactor-whiteboard-controller.md @@ -4,7 +4,7 @@ Stand: 2026-11-13 Ziel: Der Controller war mit **9.092 Zeilen und 465 Funktionen** der größte Baustein im Projekt; die Roadmap nennt < 1.500 Zeilen pro Datei. Nach den -Etappen 1–3 sind es 8.566 Zeilen. Diese Notiz +Etappen 1–4 sind es 8.529 Zeilen. Diese Notiz hält die Reihenfolge fest, damit der Umbau in Etappen mit grüner Suite passiert und nicht als Big Bang. @@ -28,7 +28,7 @@ passiert und nicht als Big Bang. | `HelpContent` | Hilfetext für den ?-Dialog | 137 | keine | 1 ✅ | | `SeasonComparisonText` | Zeilen für den Saisonvergleichs-Dialog | 175 | nur Daten | 2 ✅ | | `ValidationReportView` | Zeilen für den Prüfdialog | 300 | nur Daten | 3 ✅ | -| `ExportService` | PDF/SVG/PNG schreiben, Abschlussdialog-Daten | ~250 | Dateisystem | 4 | +| `ExportService` | SVG/PDF schreiben, Pfade und Dateinamen | 90 | Dateisystem | 4 ✅ | | `SeasonPanel` | Saison-/Programm-/Kategorieauswahl und Regelnachführung | ~250 | Statusleiste | 5 | | `SyncPanel` | Dropbox-Sync, Konflikte, Küren verwalten | ~900 | Dialoge, Threads | 6 | | `HubPanel` | Kür-Hub (Login, Upload, Downloads) | ~650 | Dialoge, Threads | 7 | @@ -64,10 +64,16 @@ Details). Der Controller baut nur noch den Report und setzt das Label. Fertig: Dialog unverändert, Whiteboard-Test grün, eigener Test für den Text. -### 4. `ExportService` +### 4. `ExportService` ✅ -Schreiben der Dateien, Pfadbildung und der Abschluss-Dialog bekommen einen -eigenen Baustein; der Controller reicht Kür, Format und Abschnitt durch. +`src/whiteboard/io/export_service.gd` schreibt SVG und PDF, legt den +Exportordner an, bildet Pfade und bereinigt Dateinamen. Der Controller +delegiert nur noch; `export_svg_to_path`/`export_pdf_to_path` bleiben als +schmale Einstiegspunkte, weil Tests sie direkt aufrufen. + +Bewusst im Controller geblieben: der Export-Dialog und der +Abschluss-Dialog (Szene), der PNG-Export (braucht ein SubViewport) und das +Öffnen im Dateimanager. ### 5. `SeasonPanel` diff --git a/docs/roadmap.md b/docs/roadmap.md index b8c1454..dc576bc 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -270,8 +270,8 @@ Schritte. - [ ] `WhiteboardController` in Services/Panels zerlegen: Datei-I/O, Sync, Playback, Validierung, Element-Inspector, Section-Liste. Ziel: < 1.500 Zeilen pro Datei, keine 100 `%`-Lookups in einem Skript. In Etappen nach - `docs/refactor-whiteboard-controller.md`; Stand 2026-11-13: 8.566 Zeilen, - `HelpContent`, `SeasonComparisonText` und `ValidationReportView` ausgelagert. + `docs/refactor-whiteboard-controller.md`; Stand 2026-11-13: 8.529 Zeilen; `HelpContent`, `SeasonComparisonText`, + `ValidationReportView` und `ExportService` ausgelagert. - [ ] Gemeinsamer Test-Harness (oder gdUnit4), Timeout pro Testlauf, Coverage; CI auf dem Gitea-/Forgejo-Remote mit `tools/test.sh` und `tools/check_export.sh`. diff --git a/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd b/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd index e4a747c..a67bb82 100644 --- a/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd +++ b/rinkplanner/src/whiteboard/ctrl/whiteboard_controller.gd @@ -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: diff --git a/rinkplanner/src/whiteboard/io/export_service.gd b/rinkplanner/src/whiteboard/io/export_service.gd new file mode 100644 index 0000000..5c7855f --- /dev/null +++ b/rinkplanner/src/whiteboard/io/export_service.gd @@ -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) diff --git a/rinkplanner/src/whiteboard/io/export_service.gd.uid b/rinkplanner/src/whiteboard/io/export_service.gd.uid new file mode 100644 index 0000000..e865120 --- /dev/null +++ b/rinkplanner/src/whiteboard/io/export_service.gd.uid @@ -0,0 +1 @@ +uid://cl02cld4fbckd diff --git a/rinkplanner/tests/run_export_tests.gd b/rinkplanner/tests/run_export_tests.gd index 8dfe7bd..dda00a9 100644 --- a/rinkplanner/tests/run_export_tests.gd +++ b/rinkplanner/tests/run_export_tests.gd @@ -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")