diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels')
4 files changed, 548 insertions, 13 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationTabVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationTabVM.cs index 0a27281ca..a2a119e29 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationTabVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorCalibrationTabVM.cs @@ -314,18 +314,6 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels var points = liquidData.RmlExtensionColorCalibrationsTestsLiquidDataPoints.OrderBy(y => y.Ink).ToList(); LoadDataPlots(points, GetPlot(liquidType), GetCalibrationDataVM(liquidType)); - //if (points.Count > 0) - //{ - // List<ColorLinearizationModel.LinearizationDataItem> items = new List<ColorLinearizationModel.LinearizationDataItem>(); - // points.ForEach(x => items.Add(new ColorLinearizationModel.LinearizationDataItem() { InkPercentage = x.Ink, L = x.L, A = x.A, B = x.B })); - // items.OrderBy(y => y.InkPercentage).ToList(); - // var index = 1; - // List<CalibrationDataPointVM> calibrationPoints = new List<CalibrationDataPointVM>(); - // points.ForEach(x => calibrationPoints.Add(new CalibrationDataPointVM() { Index = index++, X = x.Ink, Y = x.CalculatedPoint })); - - - // CreatePlots(ConvertLiquidTypeToPMR(liquidType), GetPlot(liquidType), items, GetCalibrationDataVM(liquidType)); - //} } } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeTabVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeTabVM.cs new file mode 100644 index 000000000..df0ba521c --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeTabVM.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.SharedUI; +using Tango.MachineStudio.ThreadExtensions.Models; +using Tango.BL.Enumerations; +using Tango.Core.Commands; +using Microsoft.Win32; +using Tango.MachineStudio.Common.Notifications; +using Tango.BL.ActionLogs; +using Tango.BL.Entities; +using Tango.Core; + +namespace Tango.MachineStudio.ThreadExtensions.ViewModels +{ + public class ColorShadeTabVM : ViewModel + { + private INotificationProvider _notification; + private IActionLogManager _actionLogManager; + + #region Properties + + private string _name; + /// <summary> + /// Gets or sets the name of the thread. Using in print + /// </summary> + public string Name + { + get { return _name; } + set + { + _name = value; + if (RmlExtensionColorShadesTest != null) + RmlExtensionColorShadesTest.Name = _name; + RaisePropertyChangedAuto(); + } + } + + private int _tabIndex; + + public int TabIndex + { + get { return _tabIndex; } + set + { + _tabIndex = value; + RaisePropertyChangedAuto(); + } + } + + + private bool _isSelected; + /// <summary> + /// Gets or sets a value indicating whether this instance is selected. + /// </summary> + public bool IsSelected + { + get { return _isSelected; } + set { _isSelected = value; RaisePropertyChangedAuto(); } + } + + private RmlExtensionColorShadesTest _rmlExtensionColorShadesTest; + + public RmlExtensionColorShadesTest RmlExtensionColorShadesTest + { + get { return _rmlExtensionColorShadesTest; } + set + { + _rmlExtensionColorShadesTest = value; + RaisePropertyChangedAuto(); + } + } + + /// <summary> + /// Gets or sets last points before save. Used in Save to remove points before save new. + /// </summary> + /// <value> + /// The removed points. + /// </value> + public List<RmlExtensionColorShadesTestsData> RemovedDataList { get; set; } + + #endregion + + RelayCommand ImportColorsCommand { get; set; } + + public ColorShadeTabVM(INotificationProvider notification, IActionLogManager actionLogManager) + { + _notification = notification; + _actionLogManager = actionLogManager; + ImportColorsCommand = new RelayCommand(ImportColors); + } + + private void ImportColors(object obj) + { + List<ColorShadesModel.ColorShadesDataItem> items; + + OpenFileDialog dlg = new OpenFileDialog(); + dlg.Title = "Select data file"; + dlg.Filter = "Excel |*.xlsx"; + items = null; + if (dlg.ShowDialogCenter() && dlg.FileName.IsNotNullOrEmpty()) + { + ColorShadesModel model = new ColorShadesModel(); + string errors = ""; + model.GetDataFromFile(dlg.FileName, out items, ref errors); + if (false == String.IsNullOrEmpty(errors) || items == null || items.Count == 0) + { + _notification.ShowError("An error occurred while trying to import data form the selected excel file. Please check the file format if valid and is available to read."); + + } + else + { + //Settings data to object + } + + } + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeViewVM.cs new file mode 100644 index 000000000..b1dac5ce9 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/ColorShadeViewVM.cs @@ -0,0 +1,406 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.ActionLogs; +using Tango.BL.Builders; +using Tango.BL.Entities; +using Tango.Core; +using Tango.MachineStudio.Common.Notifications; +using Tango.SharedUI; +using Tango.AutoComplete.Editors; +using Tango.MachineStudio.ThreadExtensions.Models; +using Tango.Core.Commands; +using Microsoft.Win32; +using Tango.Logging; +using Tango.MachineStudio.ThreadExtensions.ViewModels; +using System.Collections.ObjectModel; +using Tango.BL.Calibration; + +namespace Tango.MachineStudio.ThreadExtensions.ViewModels +{ + public class ColorShadeViewVM : ViewModel + { + private INotificationProvider _notification; + private IActionLogManager _actionLogManager; + + private ObservablesContext _active_context; + public event EventHandler SaveColorShadesEvent; + + #region Properties + + + private RmlExtensionColorShade _colorShade; + + public RmlExtensionColorShade RmlExtensionColorShade + { + get { return _colorShade; } + set { _colorShade = value; + RaisePropertyChangedAuto(); + } + } + + private ObservableCollection<ColorShadeTabVM> _colorShadeTabs; + + public ObservableCollection<ColorShadeTabVM> ColorShadeTabs + { + get { return _colorShadeTabs; } + set { _colorShadeTabs = value; } + } + + private ColorShadeTabVM _selectedTab; + + public ColorShadeTabVM SelectedTab + { + get { return _selectedTab; } + set + { + _selectedTab = value; + RaisePropertyChangedAuto(); + + foreach (var tab in _colorShadeTabs.Where(x => x != _selectedTab)) + { + tab.IsSelected = false; + } + + if (_selectedTab != null) + { + _selectedTab.IsSelected = true; + } + } + } + + private string _RMLExtentionGUID; + + public string RMLExtentionGUID + { + get { return _RMLExtentionGUID; } + set + { + _RMLExtentionGUID = value; + } + } + + private string _RMLGUID; + + public string RMLGUID + { + get { return _RMLGUID; } + set + { + _RMLGUID = value; + } + } + + protected string _selectedMachineGuid; + /// <summary> + /// Gets or sets the selected machine. + /// </summary> + public String SelectedMachineGUID + { + get { return _selectedMachineGuid; } + set + { + if (value != null && _selectedMachineGuid != value) + { + _selectedMachineGuid = value; + SelectedMachineChanged(); + RaisePropertyChangedAuto(); + InvalidateRelayCommands(); + } + } + } + + + private bool _isViewLoaded; + /// <summary> + /// Gets or sets a value indicating whether this instance is view loaded. Used to update charts. + /// </summary> + public bool IsViewLoaded + { + get { return _isViewLoaded; } + set + { + if (_isViewLoaded != value) + { + _isViewLoaded = value; + } + } + } + + #endregion + + #region commands + public RelayCommand CreateColorDataImportExcelTemplateCommand { get; set; } + + public RelayCommand SaveCommand { get; set; } + + public RelayCommand AddTabCommand { get; set; } + + public RelayCommand<ColorShadeTabVM> RemoveTabCommand { get; set; } + + public RelayCommand RenameTabCommand { get; set; } + + #endregion + + public ColorShadeViewVM(INotificationProvider notification, IActionLogManager actionLogManager) + { + _notification = notification; + _actionLogManager = actionLogManager; + ColorShadeTabs = new ObservableCollection<ColorShadeTabVM>(); + + SaveCommand = new RelayCommand(Save, () => IsFree); + + AddTabCommand = new RelayCommand(() => AddNewTab()); + RemoveTabCommand = new RelayCommand<ColorShadeTabVM>(RemoveTab); + RenameTabCommand = new RelayCommand(RenameTab); + + CreateColorDataImportExcelTemplateCommand = new RelayCommand(CreateColorDataImportExcelTemplate); + } + + #region Loading + public async void LoadColorShades() + { + if (SelectedMachineGUID == null) + { + _notification.ShowWarning(LogManager.Log($"Please, select machine.", LogCategory.Warning)); + IsFree = false; + return; + } + if (RMLGUID == null) + { + IsFree = false; + return; + } + try + { + IsFree = false; + if (_active_context != null) + { + _active_context.Dispose(); + } + + _active_context = ObservablesContext.CreateDefault(); + + ColorShadeTabs.Clear(); + LogManager.Log("Loading color shade view ..."); + using (_notification.PushTaskItem("Loading Color Shade View ...")) + { + var testResults = await new RMLExtensionColorShadeBuilder(_active_context).SetAll().ForRMLExtension(RMLExtentionGUID).ForMachine(SelectedMachineGUID).WithTests().BuildAsync(); + RmlExtensionColorShade = testResults.OrderBy(x => x.ID).ToList().FirstOrDefault(); + if (RmlExtensionColorShade == null) + { + RmlExtensionColorShade = new RmlExtensionColorShade() { RmlsExtensionsGuid = RMLExtentionGUID, MachineGuid = SelectedMachineGUID }; + _active_context.RmlExtensionColorShades.Add(RmlExtensionColorShade); + _active_context.SaveChanges(); + } + + foreach (var test in RmlExtensionColorShade.RmlExtensionColorShadesTests) + { + ColorShadeTabs.Add(new ColorShadeTabVM(_notification, _actionLogManager) { RmlExtensionColorShadesTest = test, Name = test.Name }); + if (ColorShadeTabs.Count == 1) + SelectedTab = ColorShadeTabs[0]; + } + if (ColorShadeTabs.Count == 0) + { + SelectedTab = CreateNewColorShadeTabVM("Untitled", 1); + ColorShadeTabs.Add(SelectedTab); + _active_context.SaveChanges(); + } + if (IsViewLoaded) + { + // ColorShadeTabs.ToList().ForEach(x => x.InitData()); + } + } + } + catch (Exception ex) + { + LogManager.Log(ex, $"Error loading Color Calibration tests.\n{ex.FlattenMessage()}"); + } + finally + { + IsFree = true; + } + } + + private ColorShadeTabVM CreateNewColorShadeTabVM(string name, int index) + { + ColorShadeTabVM newtab = new ColorShadeTabVM(_notification, _actionLogManager) { Name = name, TabIndex = index }; + + newtab.RmlExtensionColorShadesTest = new RmlExtensionColorShadesTest() + { + RmlExtensionColorShadesGuid = RMLExtentionGUID, + Name = name + }; + if (RmlExtensionColorShade != null) + { + RmlExtensionColorShade.RmlExtensionColorShadesTests.Add(newtab.RmlExtensionColorShadesTest); + } + + return newtab; + } + #endregion + + #region Methods + + private void OnRMLExtensionGUIDChanged() + { + } + + private void SelectedMachineChanged() + { + SelectedTab = null; + LoadColorShades(); + } + + private void CreateColorDataImportExcelTemplate() + { + SaveFileDialog dlg = new SaveFileDialog(); + try + { + dlg.Title = $"Create excel template file"; + dlg.Filter = "Excel Files|*.xlsx"; + dlg.DefaultExt = ".xlsx"; + dlg.FileName = "Color Shades File Template"; + if (dlg.ShowDialog().Value) + { + CalibrationHelper.CreateColorShadesInputExcelTemplate(dlg.FileName); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error generating excel color shades template file " + dlg.FileName); + _notification.ShowError("An error occurred while trying to generate the color shades file."); + } + } + + #endregion + + #region Tabs modification + + /// <summary> + /// Removes the specified tab. + /// </summary> + /// <param name="tab">The tab.</param> + private void RemoveTab(ColorShadeTabVM tab) + { + if (ColorShadeTabs.Count == 1) + return; + if (_notification.ShowQuestion("Are you sure you want to delete the selected tab?")) + { + _active_context.RmlExtensionColorShadesTestsData.RemoveRange(tab.RmlExtensionColorShadesTest.RmlExtensionColorShadesTestsData.ToList()); + + //_active_context.RmlExtensionColorCalibrationsTestsLiquidData.RemoveRange(tab.RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData); + tab.RmlExtensionColorShadesTest.RmlExtensionColorShadesTestsData = null; + + RmlExtensionColorShade.RmlExtensionColorShadesTests.Remove(tab.RmlExtensionColorShadesTest); + _active_context.RmlExtensionColorShadesTests.Remove(tab.RmlExtensionColorShadesTest); + + ColorShadeTabs.Remove(tab); + SelectedTab = ColorShadeTabs.LastOrDefault(); + _active_context.SaveChanges(); + } + } + + /// <summary> + /// Adds a new tab. + /// </summary> + private bool AddNewTab(String name = null) + { + if (ColorShadeTabs.Count > 7) + { + //_notification.ShowError("Cannot exceed the maximum number of 8 tabs. You can remove a tab, or create a new project."); + return false; + } + + if (name == null) + { + name = _notification.ShowTextInput("Enter tab name", "Tab Name", "Test"); + } + + if (!String.IsNullOrWhiteSpace(name)) + { + var tab = CreateNewColorShadeTabVM(name, (ColorShadeTabs.Count + 1)); + _active_context.SaveChanges(); + ColorShadeTabs.Add(tab); + SelectedTab = tab; + return true; + } + else + { + return false; + } + } + + /// <summary> + /// Renames the tab. + /// </summary> + /// <param name="tab">The tab.</param> + private void RenameTab() + { + if (SelectedTab != null) + { + var name = _notification.ShowTextInput("Enter tab name", "Tab Name", SelectedTab.Name); + + if (!String.IsNullOrWhiteSpace(name)) + { + SelectedTab.Name = name; + } + } + } + + #endregion + + #region Save + + public async void Save() + { + if (SelectedMachineGUID == null) + { + _notification.ShowWarning(LogManager.Log($"Could not save Color Shades. Please, select machine before save.", LogCategory.Warning)); + return; + } + + try + { + IsFree = false; + DateTime lastUpdated = DateTime.UtcNow; + RmlExtensionColorShade.LastUpdated = lastUpdated; + + foreach (var tab in ColorShadeTabs) + { + tab.RmlExtensionColorShadesTest.LastUpdated = lastUpdated; + //var removedPoints = tab.RemovedPoints; + //foreach (var liquidData in tab.RmlExtensionColorCalibrationsTest.RmlExtensionColorCalibrationsTestsLiquidData) + //{ + // var liquidtype = _liquids.Where(l => l.Guid == liquidData.LiquidTypeGuid).FirstOrDefault(); + // if (liquidtype != null && removedPoints.ContainsKey(liquidtype.Type)) + // { + // _active_context.RmlExtensionColorCalibrationsTestsLiquidDataPoints.RemoveRange(removedPoints[liquidtype.Type]); + // } + // liquidData.LastUpdated = lastUpdated; + //} + //tab.RemovedPoints.Clear(); + } + await _active_context.SaveChangesAsync(); + + // LoadColorParameters(); + } + catch (Exception ex) + { + LogManager.Log(ex, "Could not update color shades."); + _notification.ShowError($"An error occurred while trying to save color shades.\n{ex.Message}"); + } + finally + { + IsFree = true; + EventHandler handler = SaveColorShadesEvent; + handler?.Invoke(this, new EventArgs()); + } + } + + #endregion + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs index 1cc6e1dcc..47965deb4 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ThreadExtensions/ViewModels/MainViewVM.cs @@ -264,6 +264,14 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels set { _colorCalibrationViewVM = value; RaisePropertyChangedAuto(); } } + private ColorShadeViewVM _solorShadeViewVM; + public ColorShadeViewVM ColorShadeViewVM + { + get { return _solorShadeViewVM; } + set { _solorShadeViewVM = value; RaisePropertyChangedAuto(); } + } + + protected MachineModel _selectedMachine; /// <summary> /// Gets or sets the selected machine. @@ -1051,7 +1059,12 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels ColorCalibrationViewVM.ActiveRML = ActiveRML; ColorCalibrationViewVM.RMLGUID = ActiveRML.Guid; ColorCalibrationViewVM.Machine = SelectedMachine; - + + ColorShadeViewVM = new ColorShadeViewVM(_notification, _actionLogManager); + ColorShadeViewVM.RMLExtentionGUID = guid; + ColorShadeViewVM.RMLGUID = ActiveRML.Guid; + ColorShadeViewVM.SelectedMachineGUID = SelectedMachine != null ? SelectedMachine.Guid : null; ; + if (ActiveRMLExtension.RMLStatus == RMLExtensionStatus.New) { ColorParametersVewVM.SaveColorParameters -= UpdateStatus; @@ -1060,6 +1073,8 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels TestResultsViewVM.SaveTestResults += UpdateStatus; ColorCalibrationViewVM.SaveColorCalibration -= UpdateStatus; ColorCalibrationViewVM.SaveColorCalibration += UpdateStatus; + ColorShadeViewVM.SaveColorShadesEvent -= UpdateStatus; + ColorShadeViewVM.SaveColorShadesEvent += UpdateStatus; } View.NavigateTo(RMLExtensionNavigationView.RMLExtentionView); @@ -1123,6 +1138,7 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels ColorParametersVewVM.SaveColorParameters -= UpdateStatus; TestResultsViewVM.SaveTestResults -= UpdateStatus; ColorCalibrationViewVM.SaveColorCalibration -= UpdateStatus; + ColorShadeViewVM.SaveColorShadesEvent -= UpdateStatus; ActiveRMLExtension.RMLStatus = RMLExtensionStatus.InProgress; ActiveRMLExtension.LastUpdated = DateTime.UtcNow; @@ -1153,6 +1169,10 @@ namespace Tango.MachineStudio.ThreadExtensions.ViewModels { ColorCalibrationViewVM.Machine = SelectedMachine; } + if(ColorShadeViewVM != null) + { + ColorShadeViewVM.SelectedMachineGUID = SelectedMachine.Guid; + } } #endregion |
