mirror of
https://github.com/azaion/annotations.git
synced 2026-04-23 08:16:29 +00:00
a493606f64
fix publishing
127 lines
5.2 KiB
C#
127 lines
5.2 KiB
C#
using System.IO;
|
|
using System.Windows.Input;
|
|
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.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();
|
|
await annotationService.SaveAnnotation(a.OriginalMediaName, a.Time, a.ImageExtension, detections, SourceEnum.Manual, token: cancellationToken);
|
|
datasetExplorer.SwitchTab(toEditor: false);
|
|
break;
|
|
case PlaybackControlEnum.RemoveSelectedAnns:
|
|
datasetExplorer.ExplorerEditor.RemoveSelectedAnns();
|
|
break;
|
|
case PlaybackControlEnum.RemoveAllAnns:
|
|
datasetExplorer.ExplorerEditor.RemoveAllAnns();
|
|
break;
|
|
case PlaybackControlEnum.Close:
|
|
datasetExplorer.SwitchTab(toEditor: false);
|
|
break;
|
|
case PlaybackControlEnum.ValidateAnnotations:
|
|
var annotations = datasetExplorer.ThumbnailsView.SelectedItems.Cast<AnnotationThumbnail>()
|
|
.Select(x => x.Annotation)
|
|
.ToList();
|
|
foreach (var annotation in annotations)
|
|
await annotationService.ValidateAnnotation(annotation, cancellationToken);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public async Task Handle(AnnotationCreatedEvent notification, CancellationToken cancellationToken)
|
|
{
|
|
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);
|
|
}
|
|
await 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;
|
|
}
|
|
}
|