[feat:] Basic Submit-Implementation

This commit is contained in:
Elias Fierke
2024-12-24 12:07:44 +01:00
parent fe5c5e78a7
commit 95322ef9d0

View File

@@ -11,6 +11,9 @@ using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Threading;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
namespace PLG_Exam
{
public partial class MainWindow : Window
@@ -71,15 +74,15 @@ namespace PLG_Exam
case Avalonia.Input.Key.R:
// Strg + R: Abgeben (coming soon)
await OnSubmitClick(sender, e);
OnSubmitClick(sender, e);
break;
}
}
}
private async Task OnSubmitClick(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
private async void OnSubmitClick(object sender, RoutedEventArgs e)
{
await MessageBox.Show(this, "Abgeben-Funktion wird bald verfügbar sein!", "Abgeben", MessageBoxButton.Ok);
ExportToPdf();
}
@@ -510,6 +513,60 @@ namespace PLG_Exam
private async void ExportToPdf()
{
try
{
_ = GetCurrentExamDataAsJson();
// Speicher-Dialog
var saveDialog = new SaveFileDialog
{
DefaultExtension = "pdf",
Filters = { new FileDialogFilter { Name = "PDF files", Extensions = { "pdf" } } }
};
var filePath = await saveDialog.ShowAsync(this);
if (string.IsNullOrEmpty(filePath)) return;
// PDF-Dokument erstellen
using var document = new PdfDocument();
// Erste Seite: Name, Vorname, Datum
var firstPage = document.AddPage();
var gfx = XGraphics.FromPdfPage(firstPage);
var font = new XFont("Arial", 14, XFontStyleEx.Bold);
gfx.DrawString("PLG Exam", font, XBrushes.Black, new XRect(0, 0, firstPage.Width, 50), XStringFormats.TopCenter);
gfx.DrawString($"Name: {_currentExam.Name}", font, XBrushes.Black, new XRect(50, 100, firstPage.Width, firstPage.Height), XStringFormats.TopLeft);
gfx.DrawString($"Vorname: {_currentExam.Vorname}", font, XBrushes.Black, new XRect(50, 130, firstPage.Width, firstPage.Height), XStringFormats.TopLeft);
gfx.DrawString($"Datum: {_currentExam.Datum?.ToShortDateString() ?? "N/A"}", font, XBrushes.Black, new XRect(50, 160, firstPage.Width, firstPage.Height), XStringFormats.TopLeft);
// Weitere Seiten: Aufgaben
foreach (var tab in _currentExam.Tabs)
{
var page = document.AddPage();
var pageGfx = XGraphics.FromPdfPage(page);
pageGfx.DrawString($"Aufgabe {tab.Aufgabennummer}", font, XBrushes.Black, new XRect(50, 50, page.Width, page.Height), XStringFormats.TopLeft);
pageGfx.DrawString($"Überschrift: {tab.Überschrift}", font, XBrushes.Black, new XRect(50, 100, page.Width, page.Height), XStringFormats.TopLeft);
var descriptionFont = new XFont("Arial", 12, XFontStyleEx.Regular);
var descriptionRect = new XRect(50, 150, page.Width - 100, page.Height - 200);
pageGfx.DrawString(tab.Inhalt, descriptionFont, XBrushes.Black, descriptionRect, XStringFormats.TopLeft);
}
// PDF speichern
document.Save(filePath);
await MessageBox.Show(this, "PDF erfolgreich gespeichert!", "Erfolg", MessageBoxButton.Ok);
}
catch (Exception ex)
{
Console.WriteLine($"Fehler beim PDF-Export: {ex.Message}");
await MessageBox.Show(this, "Fehler beim PDF-Export.", "Fehler", MessageBoxButton.Ok);
}
}
}
}