livia-test/Livia/ViewModels/RoiSummaryControlViewModel.cs
2025-03-28 14:31:53 +08:00

139 lines
5.7 KiB
C#

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Text.Json;
using System.Windows.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Livia.Models;
using Livia.Models.Data;
using Livia.Utility;
using Livia.Utility.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Livia.ViewModels;
public class RoiSummaryControlViewModel : ObservableRecipient, ILiviaModuleViewModel
{
//roi id to tab index
public static Dictionary<string, int>? RoiTabIdToTabIndexDictionary { get; set; }
public bool Visible { get => _visible; private set => SetProperty(ref _visible, value); }
public bool IsTwoSided { get => _isTwoSided; set => SetProperty(ref _isTwoSided, value); }
protected RoiExpanderGroupControlViewModel? SelectedIsCRoiExpanderGroupViewModel => GetSelectedRoiExpanderGroupControlViewModel("IC");
protected RoiExpanderGroupControlViewModel? SelectedIsPRoiExpanderGroupViewModel => GetSelectedRoiExpanderGroupControlViewModel("IP");
public string LoadPath { get; set; } = string.Empty;
public string RoiTabId { get; set; } = string.Empty;
public ObservableCollection<RoiExpanderGroupControlViewModel> RoiExpanderGroupViewModelCollection { get; } = [];
private bool _visible;
private bool _isTwoSided;
private readonly ILogger _logger;
private readonly IRoiLegendManager _roiLegendManager;
public RoiSummaryControlViewModel(ILogger logger, IRoiLegendManager roiLegendManager)
{
_logger = logger;
_roiLegendManager = roiLegendManager;
WeakReferenceMessenger.Default.RegisterAll(this);
}
protected RoiExpanderGroupControlViewModel? GetSelectedRoiExpanderGroupControlViewModel(string groupId)
{
RoiExpanderGroupControlViewModel? result = RoiExpanderGroupViewModelCollection.FirstOrDefault(viewModel => viewModel.GroupId == groupId && viewModel.SelectedCount > 0);
if (result == null)
{
_logger.LogInformation("Selected {group} is {id}", groupId, result?.Id);
}
return result;
}
protected virtual void UpdateDiagnosis() { }
private void ListenToTotalVolumeChange(INotifyPropertyChanged viewModel)
{
viewModel.PropertyChanged += (_, args) =>
{
if (args.PropertyName != nameof(RoiExpanderGroupControlViewModel.SelectedVolume))
return;
UpdateDiagnosis();
};
}
public async Task LoadData(DataBlock dataBlock)
{
//clear old data
foreach (RoiExpanderGroupControlViewModel viewModel in RoiExpanderGroupViewModelCollection)
{
viewModel.ClearData();
}
RoiExpanderGroupViewModelCollection.Clear();
string file = Path.Combine(dataBlock.ResultPath, LoadPath);
if (!File.Exists(file))
{
// no file to show
_logger.LogInformation("Roi JSON not found: {file}", file);
Visible = false;
return;
}
RoiSummaryDataJson roiSummaryDataJson = JsonSerializer.Deserialize<RoiSummaryDataJson>(await File.ReadAllTextAsync(file), UtilityExtensions.JsonSerializerOptions);
Visible = roiSummaryDataJson.Visible ?? true;
//add
foreach (RoiGroup roiGroup in roiSummaryDataJson.Groups)
{
Color color = (Color)ColorConverter.ConvertFromString(roiGroup.Color);
if (RoiTabIdToTabIndexDictionary == null || !RoiTabIdToTabIndexDictionary.TryGetValue(RoiTabId, out int tabIndex))
continue;
_roiLegendManager.AddItem(tabIndex, roiGroup.Id, roiGroup.Name, roiGroup.FullName, color);
if (roiGroup.Regions.Count == 0)
continue;
RoiExpanderGroupControlViewModel viewModel = ActivatorUtilities.CreateInstance<RoiExpanderGroupControlViewModel>(ServiceProviderFactory.ServiceProvider, roiGroup.Name, roiGroup.Id, color, IsTwoSided);
viewModel.Regions = roiGroup.Regions;
viewModel.ZIndex = roiGroup.ZIndex ?? 0;
viewModel.RoiTabId = RoiTabId;
await viewModel.LoadData(dataBlock);
ListenToTotalVolumeChange(viewModel);
RoiExpanderGroupViewModelCollection.Add(viewModel);
}
UpdateDiagnosis();
}
public void Init()
{
}
public IEnumerable<ReportModuleItemViewModel> GenerateReportModuleList()
{
foreach (RoiExpanderGroupControlViewModel roiGroupViewModel in RoiExpanderGroupViewModelCollection)
{
if (!roiGroupViewModel.Visible)
continue;
foreach (RoiExpanderControlViewModel roiViewModel in roiGroupViewModel.RoiViewModelCollection.Items)
{
if (roiViewModel.SubGroups.Count == 0)
{
yield return new ReportModuleItemViewModel(roiViewModel.Name, roiViewModel.GenerateModulesRoiData(), roiViewModel.IsChecked);
}
else
{
foreach (RoiExpanderGroupControlViewModel roiExpanderGroupControlViewModel in roiViewModel.SubGroups)
{
if (!roiExpanderGroupControlViewModel.Visible)
continue;
foreach (RoiExpanderControlViewModel subRoiViewModel in roiExpanderGroupControlViewModel.RoiViewModelCollection.Items)
{
yield return new ReportModuleItemViewModel(subRoiViewModel.Name, subRoiViewModel.GenerateModulesRoiData(), subRoiViewModel.IsChecked);
}
}
}
}
}
}
}