mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 15:06:29 +00:00
rework to Azaion.Suite
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using System.IO;
|
||||
using System.Windows.Input;
|
||||
using Azaion.Common.DTO;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Azaion.Dataset;
|
||||
|
||||
public class DatasetExplorerEventHandler(DatasetExplorer datasetExplorer, IGalleryManager galleryManager, IOptions<DirectoriesConfig> directoriesConfig)
|
||||
:
|
||||
INotificationHandler<KeyEvent>,
|
||||
INotificationHandler<ImageCreatedEvent>
|
||||
{
|
||||
private readonly Dictionary<Key, PlaybackControlEnum> _keysControlEnumDict = new()
|
||||
{
|
||||
{ Key.Enter, PlaybackControlEnum.SaveAnnotations },
|
||||
{ Key.Delete, PlaybackControlEnum.RemoveSelectedAnns },
|
||||
{ Key.X, PlaybackControlEnum.RemoveAllAnns },
|
||||
{ Key.Escape, PlaybackControlEnum.Close }
|
||||
};
|
||||
|
||||
public async Task Handle(KeyEvent keyEvent, CancellationToken cancellationToken)
|
||||
{
|
||||
if (keyEvent.WindowEnum != WindowEnum.Annotator)
|
||||
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.SelectedIndex = keyNumber.Value;
|
||||
else
|
||||
{
|
||||
if (datasetExplorer.Switcher.SelectedIndex == 1 && _keysControlEnumDict.TryGetValue(key, out var value))
|
||||
await HandleControl(value);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleControl(PlaybackControlEnum controlEnum)
|
||||
{
|
||||
switch (controlEnum)
|
||||
{
|
||||
case PlaybackControlEnum.SaveAnnotations:
|
||||
if (datasetExplorer.ThumbnailLoading)
|
||||
return;
|
||||
|
||||
var currentAnns = datasetExplorer.ExplorerEditor.CurrentAnns
|
||||
.Select(x => new YoloLabel(x.Info, datasetExplorer.ExplorerEditor.RenderSize, datasetExplorer.ExplorerEditor.RenderSize))
|
||||
.ToList();
|
||||
|
||||
await YoloLabel.WriteToFile(currentAnns, Path.Combine(directoriesConfig.Value.LabelsDirectory, datasetExplorer.CurrentThumbnail!.LabelPath));
|
||||
await galleryManager.CreateThumbnail(datasetExplorer.CurrentThumbnail.ImagePath);
|
||||
await galleryManager.SaveLabelsCache();
|
||||
datasetExplorer.CurrentThumbnail.UpdateImage();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public Task Handle(ImageCreatedEvent notification, CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user