From 50e86d29ec671997f599560202ff7eb84440393b Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Mon, 9 Sep 2019 10:11:20 +0300 Subject: Refactoring ColorLab module CCT association. --- .../Models/CctModel.cs | 16 ++ .../Tango.MachineStudio.ColorLab.csproj | 3 +- .../ViewModels/MainViewVM.cs | 188 +++++++++++++++++---- .../Views/MainView.xaml | 3 +- .../ViewModels/MainViewVM.cs | 2 + 5 files changed, 173 insertions(+), 39 deletions(-) create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Models/CctModel.cs (limited to 'Software/Visual_Studio/MachineStudio/Modules') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Models/CctModel.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Models/CctModel.cs new file mode 100644 index 000000000..bb5525ffb --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Models/CctModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.MachineStudio.ColorLab.Models +{ + public class CctModel + { + public String Guid { get; set; } + public String FileName { get; set; } + public bool IsNew { get; set; } + public byte[] Data { get; set; } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Tango.MachineStudio.ColorLab.csproj b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Tango.MachineStudio.ColorLab.csproj index 367657cdc..ef01ae5f9 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Tango.MachineStudio.ColorLab.csproj +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Tango.MachineStudio.ColorLab.csproj @@ -73,6 +73,7 @@ GlobalVersionInfo.cs + @@ -196,7 +197,7 @@ - + \ No newline at end of file diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/ViewModels/MainViewVM.cs index 71929b72b..a521127e1 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/ViewModels/MainViewVM.cs @@ -17,6 +17,7 @@ using Tango.BL.Entities; using Tango.BL.Enumerations; using Tango.ColorConversion; using Tango.Core.Commands; +using Tango.MachineStudio.ColorLab.Models; using Tango.MachineStudio.Common; using Tango.MachineStudio.Common.Controls; using Tango.MachineStudio.Common.Notifications; @@ -25,6 +26,7 @@ using Tango.PMR.ColorLab; using Tango.Settings; using Tango.SharedUI; using Tango.SharedUI.Controls; +using System.Data.Entity; namespace Tango.MachineStudio.ColorLab.ViewModels { @@ -32,7 +34,6 @@ namespace Tango.MachineStudio.ColorLab.ViewModels { private ObservablesContext _dbContext; private INotificationProvider _notification; - private bool _isNewCCT; private bool _prevent_inverse_conversion; #region Properties @@ -183,16 +184,40 @@ namespace Tango.MachineStudio.ColorLab.ViewModels set { _targetColor = value; RaisePropertyChangedAuto(); } } - private Cct _cct; - /// - /// Gets or sets the CCT. - /// - public Cct CCT + private ObservableCollection _ccts; + public ObservableCollection CCTS + { + get { return _ccts; } + set + { + _ccts = value; + RaisePropertyChangedAuto(); + } + } + + private CctModel _selectedCCT; + public CctModel SelectedCCT { - get { return _cct; } - set { _cct = value; RaisePropertyChangedAuto(); } + get { return _selectedCCT; } + set + { + _selectedCCT = value; + RaisePropertyChangedAuto(); + InvalidateRelayCommands(); + OnSelectedCCTChanged(); + } } + //private Cct _cct; + ///// + ///// Gets or sets the CCT. + ///// + //public Cct CCT + //{ + // get { return _cct; } + // set { _cct = value; RaisePropertyChangedAuto(); } + //} + private int _deltaChroma; /// /// Gets or sets the delta chroma. @@ -265,14 +290,14 @@ namespace Tango.MachineStudio.ColorLab.ViewModels { _notification = notification; - CCT = new Cct(); + // CCT = new Cct(); SourceColor = new RgbVM(); SourceColor.ColorChanged += SourceColor_ColorChanged; ImportForwardDataCommand = new RelayCommand(ImportForwardData, () => SelectedRML != null && IsFree); - ExportForwardDataCommand = new RelayCommand(ExportForwardData, () => SelectedRML != null && CCT != null && CCT.FileName != null && IsFree); + ExportForwardDataCommand = new RelayCommand(ExportForwardData, () => SelectedRML != null && SelectedCCT != null && IsFree); SaveCommand = new RelayCommand(Save, () => SelectedRML != null && IsFree); } @@ -299,6 +324,15 @@ namespace Tango.MachineStudio.ColorLab.ViewModels Machines = _dbContext.Machines.ToObservableCollection(); ColorSpaces = _dbContext.ColorSpaces.ToObservableCollection(); Rmls = _dbContext.Rmls.ToObservableCollection(); + + CCTS = _dbContext.Ccts.Select(x => new CctModel() + { + Guid = x.Guid, + FileName = x.FileName, + + }).ToObservableCollection(); + + CCTS.Where(x => String.IsNullOrWhiteSpace(x.FileName)).ToList().ForEach(x => x.FileName = x.Guid); }); var settings = SettingsManager.Default.GetOrCreate(); @@ -331,13 +365,13 @@ namespace Tango.MachineStudio.ColorLab.ViewModels { try { - if (LiquidsCalibrationData == null || CCT.Data == null) return; + if (LiquidsCalibrationData == null || SelectedCCT == null || SelectedCCT.Data == null) return; ConversionInput input = new ConversionInput(); input.ColorSpace = SourceColor.IsLab ? PMR.ColorLab.ColorSpace.Lab : PMR.ColorLab.ColorSpace.Rgb; input.DeltaChroma = DeltaChroma; input.DeltaL = DeltaL; - input.ForwardData = ByteString.CopyFrom(CCT.Data); + input.ForwardData = ByteString.CopyFrom(SelectedCCT.Data); input.InputCoordinates = new InputCoordinates(); input.InputCoordinates.Red = (int)SourceColor.Red; @@ -452,17 +486,57 @@ namespace Tango.MachineStudio.ColorLab.ViewModels String file = GetCCTFileOpen(); if (file != null) { - CCT.FileName = Path.GetFileName(file); - CCT.Data = File.ReadAllBytes(file); + CctModel cctModel = new CctModel(); + cctModel.Guid = Guid.NewGuid().ToString(); + cctModel.IsNew = true; + + cctModel.FileName = Path.GetFileName(file); + cctModel.Data = File.ReadAllBytes(file); + + CCTS.Insert(0, cctModel); + SelectedCCT = cctModel; } } private void ExportForwardData() { - String file = GetCCTFileSave(CCT.FileName); - if (file != null) + + if (SelectedCCT != null) { - File.WriteAllBytes(file, CCT.Data); + String file = GetCCTFileSave(SelectedCCT.FileName); + if (file != null) + { + using (_notification.PushTaskItem("Exporting CCT file...")) + { + try + { + IsFree = false; + + if (SelectedCCT.IsNew) + { + File.WriteAllBytes(file, SelectedCCT.Data); + } + else + { + var cct = _dbContext.Ccts.SingleOrDefault(x => x.Guid == SelectedCCT.Guid); + + if (cct != null) + { + File.WriteAllBytes(file, cct.Data); + } + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error exporting CCT file."); + _notification.ShowError($"An error occurred while trying to export the CCT file.\n{ex.Message}"); + } + finally + { + IsFree = true; + } + } + } } } @@ -497,6 +571,25 @@ namespace Tango.MachineStudio.ColorLab.ViewModels #endregion #region Liquid Volumes + private async void OnSelectedCCTChanged() + { + if (SelectedCCT != null && !SelectedCCT.IsNew) + { + using (_notification.PushTaskItem("Loading CCT data...")) + { + IsFree = false; + + var cct = await _dbContext.Ccts.SingleOrDefaultAsync(x => x.Guid == SelectedCCT.Guid); + + if (cct != null) + { + SelectedCCT.Data = cct.Data; + } + + IsFree = true; + } + } + } private void OnSelectedMachineChanged() { @@ -551,7 +644,7 @@ namespace Tango.MachineStudio.ColorLab.ViewModels input.ColorSpace = PMR.ColorLab.ColorSpace.Volume; input.DeltaChroma = DeltaChroma; input.DeltaL = DeltaL; - input.ForwardData = ByteString.CopyFrom(CCT.Data); + input.ForwardData = ByteString.CopyFrom(SelectedCCT.Data); input.InputCoordinates = new InputCoordinates(); input.ThreadL = SelectedRML.WhitePointL; @@ -636,12 +729,33 @@ namespace Tango.MachineStudio.ColorLab.ViewModels _dbContext.Adapter.GetConfiguration(x => x.Guid == SelectedMachine.ConfigurationGuid); } - _selectedRML = new RmlBuilder(_dbContext) + var builder = new RmlBuilder(_dbContext) .Set(_selectedRML.Guid) .WithActiveParametersGroup() .WithCAT(SelectedMachine.Guid) - .WithCCT() - .WithLiquidFactors().Build(); + .WithLiquidFactors(); + + if (IsHosted) + { + builder.WithCCT(); + } + + _selectedRML = builder.Build(); + + if (IsHosted) + { + if(SelectedRML.Cct != null){ + + SelectedCCT = new CctModel() + { + Data = SelectedRML.Cct.Data + }; + } + } + else + { + SelectedCCT = CCTS.SingleOrDefault(x => x.Guid == SelectedRML.CctGuid); + } LiquidVolumes = SelectedMachine.Configuration.GetSupportedIdsPacks(_selectedRML).Select(x => new LiquidVolumeVM() { @@ -685,16 +799,6 @@ namespace Tango.MachineStudio.ColorLab.ViewModels } } - _isNewCCT = false; - CCT = SelectedRML.Cct; - - if (CCT == null) - { - CCT = new Cct(); - SelectedRML.Cct = CCT; - _isNewCCT = true; - } - var settings = SettingsManager.Default.GetOrCreate(); settings.LastMachineSerialNumber = SelectedMachine.SerialNumber; settings.LastRmlGuid = SelectedRML.Guid; @@ -750,11 +854,6 @@ namespace Tango.MachineStudio.ColorLab.ViewModels public async Task SaveChanges() { - if (_isNewCCT) - { - _dbContext.Ccts.Add(CCT); - } - foreach (var calDataVM in LiquidsCalibrationData) { var cat = calDataVM.IdsPack.LiquidType.Cats.FirstOrDefault(x => x.Machine == SelectedMachine && x.Rml == SelectedRML); @@ -777,6 +876,23 @@ namespace Tango.MachineStudio.ColorLab.ViewModels cat.PutCalibrationData(calData); } + if (SelectedCCT != null && !IsHosted) + { + if (SelectedCCT.IsNew) + { + Cct cct = new Cct(); + cct.Guid = SelectedCCT.Guid; + cct.FileName = SelectedCCT.FileName; + cct.Data = SelectedCCT.Data; + SelectedRML.Cct = cct; + SelectedCCT.IsNew = false; + } + else + { + SelectedRML.CctGuid = SelectedCCT.Guid; + } + } + await _dbContext.SaveChangesAsync(); InvalidateLiquidFactorsCalibrationData(); diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Views/MainView.xaml b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Views/MainView.xaml index 49966bad2..37f5f70be 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Views/MainView.xaml +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/Views/MainView.xaml @@ -148,8 +148,7 @@ - - +