TemplateDesigner: Klausur-Deckblatt einrichten und Punktetabelle in der Vorschau
CI / build-and-test (push) Canceled after 0s

Platzhalterkatalog und Tabellen-Zeichner liegen jetzt in LehrerApp.Templating,
damit Designer und Hauptapp dieselbe Tabelle zeichnen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-07 10:47:12 +02:00
1 parent dd92128464
commit 98c5f9eba0
11 files changed
+400 -114

No files matched your search

@@ -1,82 +1,21 @@
using System.Globalization;
using LehrerApp.Core.Services;
using LehrerApp.Templating;
namespace LehrerApp.Desktop.Services;
/// <summary>Zeichnet die Punktetabelle des Klausur-Deckblatts (Aufgabe / Max. Punktzahl /
/// Punktzahl). Die Spalten haben eine feste Breite: passen nicht alle Aufgaben nebeneinander in die
/// Box der Vorlage, folgt darunter ein weiterer Tabellenblock, statt alles zu verkleinern. Gedacht
/// für eine <c>FLOWDRAWBOX … fit=content</c>, damit nachfolgende Elemente mitwandern.</summary>
/// <summary>Brücke von den Klausurdaten zur Punktetabelle des Deckblatts. Gezeichnet wird in
/// <see cref="ExamPointsTableDrawing"/> (Vorlagenbibliothek), damit der Vorlagen-Designer dieselbe
/// Tabelle in der Vorschau zeigt.</summary>
public static class ExamPointsTableDrawingBuilder
{
public const string PlaceholderName = "Exam.PointsTable";
public const string PlaceholderName = ExamPointsTableDrawing.PlaceholderName;
private static readonly CultureInfo German = CultureInfo.GetCultureInfo("de-DE");
// Referenzentwurf in Millimetern, abgemessen an der Schulvorlage.
private const float LabelWidth = 38f, ColumnWidth = 20.5f;
private const float HeaderHeight = 5.5f, MaximumHeight = 8.5f, PointsHeight = 11.5f;
private const float BlockGap = 4f, Padding = 1.5f, Stroke = 0.2f, HeavyStroke = 0.45f;
private const float FontSize = 10f;
/// <param name="contentWidth">Breite der deklarierten Box in der Maßeinheit der Vorlage.</param>
/// <param name="millimeterScale">1 mm in der Maßeinheit der Vorlage (siehe
/// <see cref="LetterPlaceholderBuilder.FindDeclaredDrawingBox(LoadedTemplate, string)"/>).</param>
public static DrawingValue Build(IReadOnlyList<ExamCoverTaskColumn> columns, float contentWidth = 170,
float millimeterScale = 1)
{
if (columns.Count == 0) return new DrawingValue([], 0);
var k = millimeterScale;
var perBlock = ColumnsPerBlock(contentWidth, millimeterScale);
// Halbe Strichstärke Abstand zum Rand, sonst schneidet die Box die äußeren Linien an.
var origin = HeavyStroke / 2 * k;
var blockHeight = (HeaderHeight + MaximumHeight + PointsHeight) * k;
var commands = new List<DrawingCommand>();
var y = origin;
for (var start = 0; start < columns.Count; start += perBlock)
{
if (start > 0) y += BlockGap * k;
var block = columns.Skip(start).Take(perBlock).ToList();
var maximumY = y + HeaderHeight * k;
var pointsY = maximumY + MaximumHeight * k;
float millimeterScale = 1) =>
ExamPointsTableDrawing.Build(
columns.Select(c => new ExamPointsTableColumn(c.Number, c.MaxPoints, c.IsBonus, c.AchievedPoints)).ToList(),
contentWidth, millimeterScale);
Cell(commands, origin, y, LabelWidth * k, HeaderHeight * k, "Aufgabe", DrawingTextAlignment.AlignRight, false, k);
Cell(commands, origin, maximumY, LabelWidth * k, MaximumHeight * k, "Max. Punktzahl", DrawingTextAlignment.AlignRight, false, k);
Cell(commands, origin, pointsY, LabelWidth * k, PointsHeight * k, "Punktzahl", DrawingTextAlignment.AlignRight, true, k);
for (var index = 0; index < block.Count; index++)
{
var column = block[index];
var x = origin + (LabelWidth + index * ColumnWidth) * k;
Cell(commands, x, y, ColumnWidth * k, HeaderHeight * k,
column.IsBonus ? "" : $"Nr. {column.Number}", DrawingTextAlignment.AlignCenter, false, k);
Cell(commands, x, maximumY, ColumnWidth * k, MaximumHeight * k,
(column.IsBonus ? "+" : "") + Format(column.MaxPoints), DrawingTextAlignment.AlignCenter, false, k);
Cell(commands, x, pointsY, ColumnWidth * k, PointsHeight * k,
column.AchievedPoints is { } achieved ? Format(achieved) : "", DrawingTextAlignment.AlignCenter, false, k);
}
commands.Add(new DrawLine(origin, pointsY, origin + (LabelWidth + block.Count * ColumnWidth) * k, pointsY,
StrokeWidth: HeavyStroke * k));
y += blockHeight;
}
return new DrawingValue(commands, y + origin);
}
/// <summary>Wie viele Aufgaben bei fester Spaltenbreite nebeneinander in die Box passen.</summary>
public static int ColumnsPerBlock(float contentWidth, float millimeterScale = 1) =>
Math.Max(1, (int)Math.Floor((contentWidth / millimeterScale - HeavyStroke - LabelWidth + 0.01f) / ColumnWidth));
private static void Cell(List<DrawingCommand> commands, float x, float y, float width, float height,
string text, DrawingTextAlignment alignment, bool bold, float k)
{
commands.Add(new DrawRectangle(x, y, width, height, StrokeWidth: Stroke * k));
if (text.Length == 0) return;
// DrawStringEx setzt die Grundlinie auf Y + Schriftgröße; so sitzt die Zeile optisch mittig.
var fontHeight = FontSize * UnitConverter.Points(1, "pt") / UnitConverter.Points(1, "mm") * k;
var textY = y + height / 2 - fontHeight * 0.64f;
commands.Add(new DrawStringEx(x + Padding * k, textY, y + height - textY, width - 2 * Padding * k, text,
alignment, FontSize, Bold: bold));
}
private static string Format(double value) => value.ToString("0.##", German);
ExamPointsTableDrawing.ColumnsPerBlock(contentWidth, millimeterScale);
}
@@ -51,34 +51,8 @@ public static class LetterPlaceholderBuilder
_ => false,
};
/// <summary>Tatsächliche DRAWBOX/FLOWDRAWBOX-Größe eines Platzhalters (in der Maßeinheit der
/// Vorlage) plus dem Umrechnungsfaktor "1mm in dieser Maßeinheit". DrawingValue-Builder wie
/// <see cref="StudentAttendanceCalendarDrawingBuilder"/> entwerfen ihr Raster in Millimetern;
/// ohne MillimeterScale bliebe das bei einer in "pt" (statt "mm") deklarierten Vorlage viel zu
/// klein (1 "mm-Einheit" würde als 1pt ≈ 0.35mm gerendert). IsFixed unterscheidet DRAWBOX (feste,
/// nicht umbrechende Box - Inhalt, der die Höhe sprengt, wird von QuestTemplateRenderer
/// stillschweigend am unteren Rand abgeschnitten statt umzubrechen) von FLOWDRAWBOX (fließt bei
/// Bedarf automatisch auf weitere Seiten, siehe DrawingElementRenderer.Slice) - nur bei
/// IsFixed=true darf/muss ein Builder seine Höhe an Height anpassen.</summary>
public readonly record struct DeclaredDrawingBox(float Width, float Height, float MillimeterScale, bool IsFixed);
/// <summary>Siehe <see cref="DrawingBoxLocator"/> — dort liegt die Suche, damit auch der
/// Vorlagen-Designer sie nutzen kann.</summary>
public static DeclaredDrawingBox? FindDeclaredDrawingBox(LoadedTemplate template, string placeholderName) =>
FindDeclaredDrawingBox(template.Layout, placeholderName) ??
(template.ContinuationLayout is not null ? FindDeclaredDrawingBox(template.ContinuationLayout, placeholderName) : null);
private static DeclaredDrawingBox? FindDeclaredDrawingBox(TemplateLayout layout, string placeholderName)
{
var box = layout.Elements.Concat(layout.PageTemplates.SelectMany(p => p.Elements))
.Concat(layout.ContentFlows.SelectMany(f => f.Elements))
.Select(e => e switch
{
DrawBoxElement draw when draw.Placeholder == placeholderName => ((float Width, float Height, bool IsFixed)?)(draw.Width, draw.Height, true),
FlowDrawBoxElement flow when flow.Placeholder == placeholderName => (flow.Width, flow.Height, false),
_ => null,
})
.FirstOrDefault(b => b is not null);
if (box is null) return null;
var millimeterScale = UnitConverter.Points(1, "mm") / UnitConverter.Points(1, layout.Unit);
return new DeclaredDrawingBox(box.Value.Width, box.Value.Height, millimeterScale, box.Value.IsFixed);
}
DrawingBoxLocator.Find(template, placeholderName);
}
@@ -157,9 +157,9 @@ public partial class CreateLetterDialogViewModel : ObservableObject
/// Platzhalter, damit der Kalender/die Fehltagesliste die real verfügbare Fläche ausfüllen statt
/// eine feste Breite zu raten (siehe LetterPlaceholderBuilder.FindDeclaredDrawingBox). Fällt auf
/// 170mm zurück, wenn keine Vorlage gewählt ist oder die Box nicht gefunden wird.</summary>
private LetterPlaceholderBuilder.DeclaredDrawingBox DeclaredDrawingBox(string placeholderName)
private DeclaredDrawingBox DeclaredDrawingBox(string placeholderName)
{
var fallback = new LetterPlaceholderBuilder.DeclaredDrawingBox(170, float.PositiveInfinity, 1, false);
var fallback = new DeclaredDrawingBox(170, float.PositiveInfinity, 1, false);
if (SelectedTemplate is null) return fallback;
try { return LetterPlaceholderBuilder.FindDeclaredDrawingBox(_templates.Load(SelectedTemplate.Model), placeholderName) ?? fallback; }
catch (Exception ex) when (ex is IOException or InvalidDataException or TemplateValidationException) { return fallback; }