MOD: Window behavior while resizing // new search logic due to vector embedding in postgre

This commit is contained in:
Luis Sander 2025-04-10 10:41:37 +02:00
parent ba44f03264
commit 0041872afd
2 changed files with 182 additions and 235 deletions

View File

@ -1,204 +1,124 @@
<mah:MetroWindow xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
x:Class="PrototypWPFHAG.SearchWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PrototypWPFHAG"
Title="PDF-Verwaltung (Admin)" Height="600" Width="1000"
WindowTitleBrush="FireBrick"
Icon="pack://application:,,,/Images/databaseicon.png">
<Grid>
<!-- 左侧垂直布局 -->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="56.527"/>
<ColumnDefinition Width="143.473"/>
<!-- 左侧固定宽度 -->
<ColumnDefinition Width="*"/>
<!-- 右侧占满剩余空间 -->
</Grid.ColumnDefinitions>
x:Class="PrototypWPFHAG.SearchWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PrototypWPFHAG"
Title="PDF-Verwaltung (Admin)" Height="600" Width="1000"
WindowTitleBrush="FireBrick"
Icon="pack://application:,,,/Images/databaseicon.png"
ResizeMode="CanResizeWithGrip">
<!-- GesamtLayout: Zwei Spalten linke Seite (jetzt 600px) und rechte Seite (flexibel) -->
<Border BorderBrush="FireBrick" BorderThickness="3" Padding="0">
<Grid>
<Grid.ColumnDefinitions>
<!-- Breitere linke Seite: -->
<ColumnDefinition Width="500"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- 左侧区域 -->
<Border
BorderBrush="FireBrick"
BorderThickness="3" Padding="20"
Grid.ColumnSpan="3"
/>
<StackPanel
Orientation="Vertical"
Margin="10,10,772,10"
Grid.ColumnSpan="3">
<Grid
Margin="0,0,0,10"
Height="85">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="*"
/>
<ColumnDefinition
Width="Auto"
/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto"
/>
<RowDefinition
Height="Auto"
/>
</Grid.RowDefinitions>
<!-- Linke Seite -->
<Border Grid.Column="0" Margin="10,10,0,10" Padding="20">
<!-- Untergliederung: zwei Spalten (links: Upload/Steuerelemente, rechts: Suchergebnisse) -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<!-- Upload-/SuchSteuerelemente in mehreren Zeilen -->
<RowDefinition Height="Auto"/>
<!-- Drag & Drop -->
<RowDefinition Height="Auto"/>
<!-- Upload-Button -->
<RowDefinition Height="Auto"/>
<!-- RadioButtons für Suchmodus -->
<RowDefinition Height="Auto"/>
<!-- Suchtext und Suchen-Button -->
<RowDefinition Height="Auto"/>
<!-- Status & Progress -->
<RowDefinition Height="*"/>
<!-- eventuell Füllraum -->
<RowDefinition Height="Auto"/>
<!-- Zurück-Button -->
</Grid.RowDefinitions>
<Border
BorderBrush="Gray"
BorderThickness="1"
Margin="5,10,0,-85" Grid.Row="1">
<Canvas
x:Name="PdfDropCanvas"
Margin="5,0,0,-24"
Grid.RowSpan="2"
AllowDrop="True"
Background="Transparent"
DragEnter="PdfDropCanvas_DragEnter"
Drop="PdfDropCanvas_Drop">
<!-- Obere Zeile: Drag & Drop-Bereich -->
<Border Grid.ColumnSpan="2" Height="75" BorderBrush="Gray" BorderThickness="1">
<Grid Background="Transparent" AllowDrop="True"
DragEnter="PdfDropCanvas_DragEnter" Drop="PdfDropCanvas_Drop" Margin="0,0,9,0">
<!-- Hinweistext und ggf. Vorschau (zunächst unsichtbar) -->
<TextBlock x:Name="DropHintText" Text="PDF hier rein ziehen"
Foreground="Gray" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5"/>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5,30,0,0">
<Image x:Name="PdfIcon" Source="pack://application:,,,/Images/pdf-icon.png"
Width="25" Height="25" Visibility="Collapsed"/>
<TextBlock x:Name="PdfFileNameText" Margin="10,0,0,0" Visibility="Collapsed"/>
</StackPanel>
</Grid>
</Border>
<TextBlock
x:Name="DropHintText"
Text="PDF hier rein ziehen"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="Gray"
/>
<!-- Linke Spalte, 2. Zeile: PDF hochladen Button -->
<Button Grid.Row="1" Grid.Column="0" Content="PDF hochladen"
Width="235" Height="30" Background="LightGreen" Click="UploadButton_Click"
Margin="0,10,0,0"/>
<!-- Vorschau-Elemente (unsichtbar, bis eine Datei abgelegt wird) -->
<Image
x:Name="PdfIcon"
Source="pack://application:,,,/Images/pdf-icon.png"
Width="25" Height="25"
Margin="0,5,0,0"
Visibility="Collapsed"
/>
<TextBlock
x:Name="PdfFileNameText"
Margin="40,10,0,0"
Visibility="Collapsed"
/>
</Canvas>
</Border>
<!-- Linke Spalte, 3. Zeile: RadioButtons für Suchmodus -->
<StackPanel Grid.Row="2" Grid.Column="0" Orientation="Horizontal" Margin="0,10,0,0">
<RadioButton x:Name="SearchByIdRadio" Content="ID" IsChecked="True" Margin="0,0,10,0"/>
<RadioButton x:Name="SearchByTextRadio" Content="Text"/>
</StackPanel>
<!-- Linke Spalte, 4. Zeile: Suchfeld und Suchbutton -->
<StackPanel Grid.Row="3" Grid.Column="0" Orientation="Vertical" Margin="0,10,0,0">
<TextBox x:Name="SearchTextBox" Width="232" />
<Button Content="Suchen" x:Name="SearchButton" Click="SearchButton_Click"
Width="118" Margin="0,5,0,0"/>
</StackPanel>
</Grid>
<!-- Linke Spalte, 5. Zeile: Upload-Status und Fortschrittsanzeige -->
<StackPanel Grid.Row="4" Grid.Column="0" Orientation="Vertical" Margin="0,10,0,0">
<TextBlock x:Name="UploadStatusText" TextAlignment="Center" Height="27"/>
<ProgressBar x:Name="UploadProgressBar" Height="10" Minimum="0" Maximum="100"
Visibility="Collapsed" Margin="0,5,0,0"/>
</StackPanel>
<!-- PDFField -->
<Grid Margin="0,0,0,10">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="*"
/>
<ColumnDefinition
Width="Auto"
/>
</Grid.ColumnDefinitions>
<Button
Content="PDF hochladen"
Width="auto" Height="30"
Margin="5,0,0,0"
Background="LightGreen"
Click="UploadButton_Click"
/>
<!-- Rechte Spalte: Suchergebnisse ListBox -->
<ListBox Grid.Row="1" Grid.RowSpan="5" Grid.Column="1"
x:Name="SearchResultsListBox" DisplayMemberPath="DocumentName"
ItemsSource="{Binding}" SelectionMode="Extended"
SelectionChanged="SearchResultsListBox_SelectionChanged" Margin="10,10,0,0"/>
</Grid>
<!-- Linke Spalte, untere Zeile: Button "Zurück zur Anmeldung" -->
<Button Grid.Row="6" Grid.Column="0" Content="Zurück zur Anmeldung"
Click="BackToLogIn_Click" Height="30" Background="#ffd64f" Margin="0,10,0,0"/>
</Grid>
</Border>
<StackPanel Orientation="Horizontal" Margin="5,10">
<RadioButton x:Name="SearchByIdRadio" Content="ID" IsChecked="True"/>
<RadioButton x:Name="SearchByTextRadio" Content="Text" Margin="10,0"/>
</StackPanel>
<TextBox
x:Name="SearchTextBox"
Margin="5,0,0,0"
/>
<Button
Content="Suchen"
x:Name="SearchButton"
Click="SearchButton_Click"
Margin="5,10,0,10"
/>
<!-- ListField -->
<ListBox
x:Name="SearchResultsListBox"
DisplayMemberPath="Filename"
Margin="5,0,0,0"
ItemsSource="{Binding}"
SelectionMode="Extended"
SelectionChanged="SearchResultsListBox_SelectionChanged"
/>
<TextBlock
x:Name="UploadStatusText"
TextAlignment="Center" Margin="0,35,0,35" Height="27"
/>
<ProgressBar
x:Name="UploadProgressBar"
Height="10"
Minimum="0" Maximum="100"
Visibility="Collapsed"
Margin="0,20,0,0"
/>
<Button
Content="Zurück zur Anmeldung"
Click="BackToLogIn_Click"
Margin="5,10,0,0"
Height="30"
Background="#ffd64f"
/>
</StackPanel>
<!-- 右侧区域 -->
<Grid
Grid.Column="2"
Margin="60,10,10,10">
<Label
Content="PDF-Inhalt:"
HorizontalAlignment="Left"
Margin="0,2,0,8"
/>
<ScrollViewer
x:Name="ContentScrollViewer"
Margin="0,0,0,35"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">
<TextBox
x:Name="ContentTextBox"
Text="{Binding SelectedDocument.Content}"
IsReadOnly="True"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
Margin="5,25, 0, 35"
Height="Auto"
MinHeight="494"
/>
</ScrollViewer>
<!-- 删除按钮 -->
<Button
Content="Löschen"
Width="100"
Height="30"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Background="Firebrick"
Click="DeleteButton_Click"
/>
<!-- Rechte Seite -->
<Border Grid.Column="1" Margin="43,10,10,10" Padding="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<!-- Überschrift -->
<RowDefinition Height="*"/>
<!-- PDF-Inhalt -->
<RowDefinition Height="Auto"/>
<!-- Lösch-Button -->
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="PDF-Inhalt:" Margin="0,0,0,5"/>
<!-- Für den Inhalt wird ein TextBox genutzt, der bei Bedarf automatisch Scrollbars anzeigt -->
<TextBox Grid.Row="1" x:Name="ContentTextBox"
Text="{Binding SelectedDocument.Content}"
IsReadOnly="True" TextWrapping="Wrap" AcceptsReturn="True"
VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"/>
<Button Grid.Row="2" Content="Löschen" Width="100" Height="30"
Background="Firebrick" Click="DeleteButton_Click"
HorizontalAlignment="Center" Margin="0,10,0,0"/>
</Grid>
</Border>
</Grid>
</Grid>
</mah:MetroWindow>
</Border>
</mah:MetroWindow>

View File

@ -1,14 +1,17 @@
using MahApps.Metro.Controls;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace PrototypWPFHAG
@ -60,7 +63,7 @@ namespace PrototypWPFHAG
}
else
{
await SearchByTextAsync();
await SearchBySimilarityAsync();
}
}
catch (Exception ex)
@ -90,7 +93,6 @@ namespace PrototypWPFHAG
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(json);
// Extrahiere das Array "documents"
var documents = result.GetProperty("documents");
if (documents.GetArrayLength() == 0)
{
@ -98,22 +100,24 @@ namespace PrototypWPFHAG
return;
}
// Nimm das erste Element des Arrays
var document = documents[0];
// UI aktualisieren
// Korrigierte UI-Aktualisierung
await Dispatcher.InvokeAsync(() =>
{
SearchResultsListBox.ItemsSource = new List<SearchResult>
SearchResultsListBox.ItemsSource = new List<DocumentDetail> // Änderung von SearchResult zu DocumentDetail
{
new SearchResult
new DocumentDetail
{
Id = document.GetProperty("id").GetInt32(),
Filename = document.GetProperty("filename").GetString(),
DocumentName = document.GetProperty("document_name").GetString(), // Korrekter Property-Name
Content = document.GetProperty("content").GetString()
}
};
ContentTextBox.Text = document.GetProperty("content").GetString();
// Wichtig: DisplayMemberPath korrekt setzen
SearchResultsListBox.DisplayMemberPath = "DocumentName";
});
}
catch (Exception ex)
@ -121,6 +125,7 @@ namespace PrototypWPFHAG
MessageBox.Show($"Fehler: {ex.Message}");
}
}
private async Task SearchByTextAsync()
{
try
@ -137,7 +142,7 @@ namespace PrototypWPFHAG
MessageBox.Show($"Gefundene Dokumente: {documents?.Count}", "Debug");
SearchResultsListBox.ItemsSource = documents;
SearchResultsListBox.DisplayMemberPath = "Filename";
SearchResultsListBox.DisplayMemberPath = "name";
}
catch (Exception ex)
{
@ -145,7 +150,27 @@ namespace PrototypWPFHAG
}
}
private async Task SearchBySimilarityAsync()
{
try
{
var encodedQuery = Uri.EscapeDataString(SearchTextBox.Text);
var response = await _httpClient.GetAsync($"/documents/similarity?query={encodedQuery}");
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<ApiResponse>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
SearchResultsListBox.ItemsSource = result?.Documents;
SearchResultsListBox.DisplayMemberPath = "DocumentName";
}
catch (Exception ex)
{
MessageBox.Show($"Fehler: {ex.Message}");
}
}
private void PdfDropCanvas_DragEnter(object sender, DragEventArgs e)
{
@ -192,6 +217,9 @@ namespace PrototypWPFHAG
[JsonPropertyName("document")]
public DocumentDetail Document { get; set; }
[JsonPropertyName("documents")]
public List<DocumentDetail> Documents { get; set; }
}
public class DocumentDetail
@ -199,11 +227,33 @@ namespace PrototypWPFHAG
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("filename")]
public string Filename { get; set; }
[JsonPropertyName("document_name")]
public string DocumentName { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }
[JsonPropertyName("distance")]
public double Distance { get; set; }
// Für die TextBox-Bindung
private bool _isSelected;
public bool IsSelected
{
get => _isSelected;
set
{
_isSelected = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ApiError
{
@ -303,7 +353,7 @@ namespace PrototypWPFHAG
var deletedIds = new List<int>();
var errorIds = new List<int>();
foreach (var item in SearchResultsListBox.SelectedItems.Cast<SearchResult>().ToList())
foreach (var item in SearchResultsListBox.SelectedItems.Cast<DocumentDetail>().ToList())
{
try
{
@ -334,7 +384,7 @@ namespace PrototypWPFHAG
}
else
{
await SearchByTextAsync(); // Neu laden der Textsuche
await SearchBySimilarityAsync(); // Neu laden der Textsuche
}
// Feedback an Benutzer
@ -352,40 +402,17 @@ namespace PrototypWPFHAG
private async void SearchResultsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (SearchResultsListBox.SelectedItem is SearchResult selectedDocument)
if (SearchResultsListBox.SelectedItem is DocumentDetail selected)
{
try
{
var response = await _httpClient.GetAsync($"/documents/by-id/{selectedDocument.Id}");
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(json);
var response = await _httpClient.GetAsync($"/documents/{selected.Id}/markdown");
var content = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(content);
// Sicherstellen, dass das "documents" Array existiert
if (!result.TryGetProperty("documents", out var documents))
{
ContentTextBox.Text = "Ungültiges Antwortformat";
return;
}
// Sicherstellen, dass das Array mindestens ein Element hat
if (documents.GetArrayLength() == 0)
{
ContentTextBox.Text = "Dokument nicht gefunden";
return;
}
var document = documents[0];
// Sicherstellen, dass alle benötigten Felder existieren
if (!document.TryGetProperty("content", out var contentProp))
{
ContentTextBox.Text = "Dokumentinhalt fehlt";
return;
}
ContentTextBox.Text = contentProp.GetString();
Debug.WriteLine($"API-Antwort: {json}");
ContentTextBox.Text = result.GetProperty("document")
.GetProperty("content")
.GetString();
}
catch (Exception ex)
{