mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 09:46:30 +00:00
63 lines
2.3 KiB
C#
63 lines
2.3 KiB
C#
using System.IO;
|
|
using System.Windows.Input;
|
|
using Azaion.Annotator.DTO;
|
|
using MediatR;
|
|
|
|
namespace Azaion.Annotator;
|
|
|
|
public class DatasetExplorerEventHandler(DatasetExplorer datasetExplorer,
|
|
Config config,
|
|
IGalleryManager galleryManager,
|
|
FormState formState) : INotificationHandler<KeyEvent>
|
|
{
|
|
private readonly Dictionary<Key, PlaybackControlEnum> _keysControlEnumDict = new()
|
|
{
|
|
{ Key.Enter, PlaybackControlEnum.SaveAnnotations },
|
|
{ Key.Delete, PlaybackControlEnum.RemoveSelectedAnns },
|
|
{ Key.X, PlaybackControlEnum.RemoveAllAnns }
|
|
};
|
|
|
|
public async Task Handle(KeyEvent keyEvent, CancellationToken cancellationToken)
|
|
{
|
|
if (formState.ActiveWindow != WindowsEnum.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.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:
|
|
var currentAnns = datasetExplorer.ExplorerEditor.CurrentAnns
|
|
.Select(x => new YoloLabel(x.Info, datasetExplorer.ExplorerEditor.RenderSize, datasetExplorer.ExplorerEditor.RenderSize))
|
|
.ToList();
|
|
|
|
await YoloLabel.WriteToFile(currentAnns, Path.Combine(config.LabelsDirectory, datasetExplorer.CurrentThumbnail!.LabelPath));
|
|
await galleryManager.CreateThumbnail(datasetExplorer.CurrentThumbnail.ImagePath);
|
|
|
|
datasetExplorer.Switcher.SelectedIndex = 0;
|
|
break;
|
|
case PlaybackControlEnum.RemoveSelectedAnns:
|
|
datasetExplorer.ExplorerEditor.RemoveSelectedAnns();
|
|
break;
|
|
case PlaybackControlEnum.RemoveAllAnns:
|
|
datasetExplorer.ExplorerEditor.RemoveAllAnns();
|
|
break;
|
|
}
|
|
}
|
|
}
|