mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 22:16:30 +00:00
babcbc0fc7
clean warnings
154 lines
6.6 KiB
C#
154 lines
6.6 KiB
C#
using System.IO;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Threading;
|
|
using Azaion.Common.Database;
|
|
using Azaion.Common.DTO;
|
|
using Azaion.Common.DTO.Queue;
|
|
using Azaion.Common.Events;
|
|
using Azaion.Common.Services;
|
|
using MediatR;
|
|
|
|
namespace Azaion.Dataset;
|
|
|
|
public class DatasetExplorerEventHandler(
|
|
DatasetExplorer datasetExplorer,
|
|
AnnotationService annotationService) :
|
|
INotificationHandler<KeyEvent>,
|
|
INotificationHandler<DatasetExplorerControlEvent>,
|
|
INotificationHandler<AnnotationCreatedEvent>,
|
|
INotificationHandler<AnnotationsDeletedEvent>
|
|
{
|
|
private readonly Dictionary<Key, PlaybackControlEnum> _keysControlEnumDict = new()
|
|
{
|
|
{ Key.Enter, PlaybackControlEnum.SaveAnnotations },
|
|
{ Key.Delete, PlaybackControlEnum.RemoveSelectedAnns },
|
|
{ Key.X, PlaybackControlEnum.RemoveAllAnns },
|
|
{ Key.Escape, PlaybackControlEnum.Close },
|
|
{ Key.Down, PlaybackControlEnum.Next },
|
|
{ Key.Up, PlaybackControlEnum.Previous },
|
|
{ Key.V, PlaybackControlEnum.ValidateAnnotations},
|
|
};
|
|
|
|
public async Task Handle(DatasetExplorerControlEvent notification, CancellationToken cancellationToken)
|
|
{
|
|
await HandleControl(notification.PlaybackControl, cancellationToken);
|
|
}
|
|
|
|
public async Task Handle(KeyEvent keyEvent, CancellationToken cancellationToken)
|
|
{
|
|
if (keyEvent.WindowEnum != WindowEnum.DatasetExplorer)
|
|
return;
|
|
|
|
var key = keyEvent.Args.Key;
|
|
var keyNumber = (int?)null;
|
|
|
|
if ((int)key >= (int)Key.D1 && (int)key <= (int)Key.D9) keyNumber = key - Key.D1;
|
|
if ((int)key >= (int)Key.NumPad1 && (int)key <= (int)Key.NumPad9) keyNumber = key - Key.NumPad1;
|
|
|
|
if (keyNumber.HasValue)
|
|
datasetExplorer.LvClasses.SelectNum(keyNumber.Value);
|
|
else
|
|
{
|
|
if (datasetExplorer.Switcher.SelectedIndex == 1 && _keysControlEnumDict.TryGetValue(key, out var value))
|
|
await HandleControl(value, cancellationToken);
|
|
}
|
|
}
|
|
|
|
private async Task HandleControl(PlaybackControlEnum controlEnum, CancellationToken cancellationToken = default)
|
|
{
|
|
switch (controlEnum)
|
|
{
|
|
case PlaybackControlEnum.SaveAnnotations:
|
|
if (datasetExplorer.ThumbnailLoading)
|
|
return;
|
|
|
|
var a = datasetExplorer.CurrentAnnotation!.Annotation;
|
|
|
|
var detections = datasetExplorer.ExplorerEditor.CurrentDetections
|
|
.Select(x => new Detection(a.Name, x.GetLabel(datasetExplorer.ExplorerEditor.RenderSize)))
|
|
.ToList();
|
|
var index = datasetExplorer.ThumbnailsView.SelectedIndex;
|
|
await annotationService.SaveAnnotation(a.OriginalMediaName, a.Time, detections, token: cancellationToken);
|
|
await datasetExplorer.EditAnnotation(index + 1);
|
|
break;
|
|
case PlaybackControlEnum.RemoveSelectedAnns:
|
|
if (datasetExplorer.ExplorerEditor.CurrentDetections.Any(x => x.IsSelected))
|
|
datasetExplorer.ExplorerEditor.RemoveSelectedAnns();
|
|
else
|
|
{
|
|
await datasetExplorer.DeleteAnnotations();
|
|
await datasetExplorer.EditAnnotation(datasetExplorer.ThumbnailsView.SelectedIndex);
|
|
}
|
|
break;
|
|
case PlaybackControlEnum.RemoveAllAnns:
|
|
datasetExplorer.ExplorerEditor.RemoveAllAnns();
|
|
break;
|
|
case PlaybackControlEnum.Next:
|
|
await datasetExplorer.EditAnnotation(datasetExplorer.ThumbnailsView.SelectedIndex + 1);
|
|
break;
|
|
case PlaybackControlEnum.Previous:
|
|
await datasetExplorer.EditAnnotation(datasetExplorer.ThumbnailsView.SelectedIndex - 1);
|
|
break;
|
|
case PlaybackControlEnum.Close:
|
|
datasetExplorer.SwitchTab(toEditor: false);
|
|
break;
|
|
case PlaybackControlEnum.ValidateAnnotations:
|
|
var annotations = datasetExplorer.ThumbnailsView.SelectedItems.Cast<AnnotationThumbnail>()
|
|
.Select(x => x.Annotation)
|
|
.ToList();
|
|
await annotationService.ValidateAnnotations(annotations, cancellationToken);
|
|
foreach (var ann in datasetExplorer.SelectedAnnotations.Where(x => annotations.Contains(x.Annotation)))
|
|
{
|
|
ann.Annotation.AnnotationStatus = AnnotationStatus.Validated;
|
|
if (datasetExplorer.SelectedAnnotationDict.TryGetValue(ann.Annotation.Name, out var value))
|
|
value.Annotation.AnnotationStatus = AnnotationStatus.Validated;
|
|
ann.UpdateUI();
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
public Task Handle(AnnotationCreatedEvent notification, CancellationToken cancellationToken)
|
|
{
|
|
datasetExplorer.Dispatcher.Invoke(() =>
|
|
{
|
|
var annotation = notification.Annotation;
|
|
var selectedClass = datasetExplorer.LvClasses.CurrentClassNumber;
|
|
|
|
//TODO: For editing existing need to handle updates
|
|
datasetExplorer.AddAnnotationToDict(annotation);
|
|
if (annotation.Classes.Contains(selectedClass) || selectedClass == -1)
|
|
{
|
|
var annThumb = new AnnotationThumbnail(annotation);
|
|
if (datasetExplorer.SelectedAnnotationDict.ContainsKey(annThumb.Annotation.Name))
|
|
{
|
|
datasetExplorer.SelectedAnnotationDict.Remove(annThumb.Annotation.Name);
|
|
var ann = datasetExplorer.SelectedAnnotations.FirstOrDefault(x => x.Annotation.Name == annThumb.Annotation.Name);
|
|
if (ann != null)
|
|
datasetExplorer.SelectedAnnotations.Remove(ann);
|
|
}
|
|
|
|
datasetExplorer.SelectedAnnotations.Insert(0, annThumb);
|
|
datasetExplorer.SelectedAnnotationDict.Add(annThumb.Annotation.Name, annThumb);
|
|
}
|
|
});
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public async Task Handle(AnnotationsDeletedEvent notification, CancellationToken cancellationToken)
|
|
{
|
|
var names = notification.Annotations.Select(x => x.Name).ToList();
|
|
var annThumbs = datasetExplorer.SelectedAnnotationDict
|
|
.Where(x => names.Contains(x.Key))
|
|
.Select(x => x.Value)
|
|
.ToList();
|
|
foreach (var annThumb in annThumbs)
|
|
{
|
|
datasetExplorer.SelectedAnnotations.Remove(annThumb);
|
|
datasetExplorer.SelectedAnnotationDict.Remove(annThumb.Annotation.Name);
|
|
}
|
|
await Task.CompletedTask;
|
|
}
|
|
}
|