using System.Collections.Concurrent; using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; using Livia.Models; using Livia.Models.Data; using Livia.Utility.DependencyInjection; using Microsoft.Extensions.DependencyInjection; namespace Livia.ViewModels; public class ImageRotationViewerGroupControlViewModel(IDataBlockLoader dataBlockLoader) : ObservableObject, ILiviaModuleViewModel { public ObservableCollection ImageCollection { get; } = []; public int ImageCollectionRows { get => _imageCollectionRows; private set => SetProperty(ref _imageCollectionRows, value); } private int _imageCollectionRows; public async Task LoadData(DataBlock dataBlock) { foreach (ImageRotationViewerControlViewModel viewModel in ImageCollection) { viewModel.ClearData(); } ImageCollection.Clear(); AdditionalInfoJson additionalInfo = dataBlockLoader.GetSharedData("additionalInfo"); foreach (AxialOverviewImageInfo axialOverviewImageInfo in additionalInfo.AxialOverviewImageInfoList) { string displayName = axialOverviewImageInfo.ImgId; //read color bar ColorBarControlViewModel colorBarControlViewModel = ActivatorUtilities.CreateInstance(ServiceProviderFactory.ServiceProvider); int findIndex = additionalInfo.MosaicImageInfoList.FindIndex(item => item.ImgId == axialOverviewImageInfo.ImgId); if (findIndex >= 0) { MosaicImageInfo info = additionalInfo.MosaicImageInfoList[findIndex]; if (info.Range != null) { colorBarControlViewModel.ColorBarRangeMin = info.Range[0]; colorBarControlViewModel.ColorBarRangeMax = info.Range[1]; colorBarControlViewModel.ColorBarUnit = info.Unit; colorBarControlViewModel.ImagePath = info.IsGray ? ColorBarControlViewModel.BlackWhiteColorBarImage : ColorBarControlViewModel.PerfusionColorBarImage; } else { colorBarControlViewModel.Visible = false; } } else { colorBarControlViewModel.Visible = false; } ImageRotationViewerControlViewModel viewModel = ActivatorUtilities.CreateInstance(ServiceProviderFactory.ServiceProvider); viewModel.DisplayName = displayName; viewModel.StructLoadPath = axialOverviewImageInfo.FilePath; viewModel.StructDataType = ImageRotationViewerDataType.Dicom3D; viewModel.ColorBarControlViewModel = colorBarControlViewModel; viewModel.ImageIndexSyncKey = "structure"; viewModel.ShowHelperTip = additionalInfo.MosaicRoiOverlayInfoList.Exists(n => n.ImgId.Equals(axialOverviewImageInfo.ImgId)); viewModel.Init(); await viewModel.LoadData(dataBlock); Dictionary> roiDictionary = dataBlockLoader.GetSharedData>>("roiCollection"); //add roi to dictionary. if (RoiSummaryControlViewModel.RoiTabIdToTabIndexDictionary != null) { foreach (RoiOverlayInfo overlayInfo in additionalInfo.AxialRoiOverlayInfoList.Where(overlayInfo => overlayInfo.ImgId == axialOverviewImageInfo.ImgId)) { if (!roiDictionary.TryGetValue(overlayInfo.RoiTabId, out ConcurrentBag? bag)) continue; if (!RoiSummaryControlViewModel.RoiTabIdToTabIndexDictionary.TryGetValue(overlayInfo.RoiTabId, out int tabIndex)) continue; viewModel.AddRoiListToDictionary(tabIndex, bag); } } ImageCollection.Add(viewModel); } //decide how many rows we need according to image count ImageCollectionRows = ImageCollection.Count > 2 ? 2 : 1; } public void Init() { } }