From 5003e43a9ad367bb01807dd907623ed51149a72f Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Tue, 10 Oct 2023 13:43:20 +0300 Subject: MyColors -added export & import buttons. Related Work Items: #8814 --- .../Dialogs/ColorSelectionView.xaml | 37 +++++---- .../Dialogs/ColorSelectionViewVM.cs | 93 +++++++++++++++++++++- .../Tango.PPC.JobsV2/Models/FavoriteColor.cs | 17 ++++ .../Tango.PPC.JobsV2/MyColors/MyColorsEngine.cs | 41 ++++++++++ .../Tango.PPC.JobsV2/Views/JobEurekaView.xaml | 2 +- 5 files changed, 170 insertions(+), 20 deletions(-) (limited to 'Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2') diff --git a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Dialogs/ColorSelectionView.xaml b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Dialogs/ColorSelectionView.xaml index 3ec03eff7..c60fed143 100644 --- a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Dialogs/ColorSelectionView.xaml +++ b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Dialogs/ColorSelectionView.xaml @@ -691,25 +691,25 @@ - + - - - + + + - - Add Group + + Add Group - - + Add @@ -720,25 +720,30 @@ My Colors - - + + - - - + + + - + - + - + + + + + diff --git a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Dialogs/ColorSelectionViewVM.cs b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Dialogs/ColorSelectionViewVM.cs index 37f5c3f7b..cc8ddc84e 100644 --- a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Dialogs/ColorSelectionViewVM.cs +++ b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Dialogs/ColorSelectionViewVM.cs @@ -1,9 +1,11 @@ using ColorMine.ColorSpaces; +using Microsoft.Win32; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -457,7 +459,25 @@ namespace Tango.PPC.Jobs.Dialogs set { _editColorButtonName = value; RaisePropertyChangedAuto();} } - + + private PPCSettings _settings; + /// + /// Gets the main PPC settings. + /// + public PPCSettings Settings + { + get + { + if (_settings == null) + { + _settings = SettingsManager.Default.GetOrCreate(); + } + + return _settings; + } + private set { _settings = value; } + } + #endregion #region commands @@ -483,6 +503,8 @@ namespace Tango.PPC.Jobs.Dialogs public RelayCommand DeleteColorCommand { get; set; } public RelayCommand VectorFineTuningCommand { get; set; } + public RelayCommand UploadFileCommand { get; set; } + public RelayCommand DownloadFileCommand { get; set; } #endregion @@ -534,6 +556,9 @@ namespace Tango.PPC.Jobs.Dialogs EditColorsLibraryCommand = new RelayCommand(EditColorsLibrary); EditColorsLibrariesCommand = new RelayCommand(EditColorsLibraries); DeleteColorCommand = new RelayCommand(DeleteColor); + UploadFileCommand = new RelayCommand(UploadFile); + DownloadFileCommand = new RelayCommand(DownloadFile); + MyColorsMode = false; EditColorsGroupMode = false; SaveMyColorMode = false; @@ -637,9 +662,7 @@ namespace Tango.PPC.Jobs.Dialogs CheckIsSelectedColorInLibrary(); SelectedBrushStop.ConvertColor();//test OOG } - - private void OnSelectedtabChanged() { switch (SelectedColorTab) @@ -1148,6 +1171,70 @@ namespace Tango.PPC.Jobs.Dialogs } } + private async void UploadFile() + { + if (Settings.StorageRootPath == null || false == Directory.Exists(Settings.StorageRootPath)) + { + Settings.StorageRootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Twine", "Storage"); + Directory.CreateDirectory(Settings.StorageRootPath); + } + SaveFileDialog dlg = new SaveFileDialog(); + dlg.InitialDirectory = Path.GetFullPath(Settings.StorageRootPath); + dlg.RestoreDirectory = true; + dlg.Filter = "Tango Update My Colors|*.colr"; + dlg.FileName = "MyColors"; + dlg.DefaultExt = ".colr"; + if (dlg.ShowDialog().Value) + { + try + { + IsFree = false; + MyColorsEngine.Default.ToFile(dlg.FileName); + await NotificationProvider.ShowSuccess("My colors file saved successfully."); + LogManager.Log($"My colors file saved successfully to file {dlg.FileName}."); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error saving My colors to file."); + await NotificationProvider.ShowError($"Error creating My colors file:\n{ex.Message}"); + } + finally + { + IsFree = true; + } + } + } + + private void DownloadFile() + { + if (Settings.StorageRootPath == null || false == Directory.Exists(Settings.StorageRootPath)) + { + Settings.StorageRootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Twine", "Storage"); + Directory.CreateDirectory(Settings.StorageRootPath); + } + if (Directory.Exists(Settings.StorageRootPath)) + { + Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog + { + InitialDirectory = Settings.StorageRootPath, + Title = "Browse My Colors Files", + + CheckFileExists = true, + CheckPathExists = true, + + DefaultExt = "colr", + Filter = "job files (*.colr)|*.colr", + ReadOnlyChecked = true, + ShowReadOnly = true + }; + if (openFileDialog.ShowDialog().Value) + { + MyColorsEngine.Default.FromFile(openFileDialog.FileName); + Libraries = MyColorsEngine.Default.GetAll().ToObservableCollection(); + } + } + } + private void LoadLibraryDictionary() { _colorSpaceToFavoriteColorDictionary = new Dictionary>(); diff --git a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Models/FavoriteColor.cs b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Models/FavoriteColor.cs index 00c8293bd..1d7b7c7c2 100644 --- a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Models/FavoriteColor.cs +++ b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Models/FavoriteColor.cs @@ -10,6 +10,7 @@ using System.Windows; using ColorMine.ColorSpaces; using Tango.Core; using LiteDB; +using Newtonsoft.Json; namespace Tango.PPC.Jobs.Models { @@ -86,14 +87,18 @@ namespace Tango.PPC.Jobs.Models } [BsonIgnore] + [JsonIgnore] public SolidColorBrush Brush { get { return new SolidColorBrush(Color); } } [BsonIgnore] + [JsonIgnore] private bool _isSelected; + [BsonIgnore] + [JsonIgnore] public bool IsSelected { get { return _isSelected; } @@ -117,5 +122,17 @@ namespace Tango.PPC.Jobs.Models ColorSpace = ColorSpaces.Volume; IsSelected = false; } + + public bool Equals(FavoriteColor color) + { + if(color == null) + return false; + + return (ColorSpace == color.ColorSpace + && Cyan == color.Cyan && Magenta == color.Magenta && Yellow == color.Yellow && Black ==color.Black + && Red == color.Red && Green == color.Green && Blue == color.Blue + && L == color.L && A == color.A && B == color.B + && Hue == color.Hue && Saturation == color.Saturation && Brightness == color.Brightness); + } } } diff --git a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/MyColors/MyColorsEngine.cs b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/MyColors/MyColorsEngine.cs index 27dabc2e7..2963d290b 100644 --- a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/MyColors/MyColorsEngine.cs +++ b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/MyColors/MyColorsEngine.cs @@ -6,11 +6,15 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.PPC.Jobs.Models; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace Tango.PPC.Jobs.MyColors { public class MyColorsEngine : IDisposable { + private JsonSerializerSettings _settings; + private LiteDatabase _db; private ILiteCollection _collection; @@ -35,6 +39,14 @@ namespace Tango.PPC.Jobs.MyColors Directory.CreateDirectory(dbFolder); _db = new LiteDatabase($"Filename={Path.Combine(dbFolder, "color_library.db")}"); _collection = _db.GetCollection("Libraries"); + + _settings = new JsonSerializerSettings() + { + TypeNameHandling = TypeNameHandling.Auto, + Formatting = Formatting.Indented, + ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore, + PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects + }; } public void UpdateLibrary(ColorLibrary library) @@ -72,5 +84,34 @@ namespace Tango.PPC.Jobs.MyColors { _collection.Delete(library.ID); } + + public void ToFile( string flePath) + { + string jsonCollection = JsonConvert.SerializeObject(GetAll(), _settings); + File.WriteAllText(flePath, jsonCollection); + } + + public void FromFile(String fileName) + { + string json = File.ReadAllText(fileName); + List colorCollection = JsonConvert.DeserializeObject>(json, _settings); + foreach(var colorLib in colorCollection) + { + var currentLib = _collection.FindOne(x => x.Name == colorLib.Name); + if (currentLib != null) + { + var newlist = colorLib.ColorList.Where(s => !currentLib.ColorList.Any(p => p.Equals(s))); + foreach( var x in newlist) + currentLib.ColorList.Add(x); + + UpdateLibrary(currentLib); + } + else + { + _collection.Insert(colorLib); + } + + } + } } } diff --git a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Views/JobEurekaView.xaml b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Views/JobEurekaView.xaml index d348b2cc7..a10524e71 100644 --- a/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Views/JobEurekaView.xaml +++ b/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Views/JobEurekaView.xaml @@ -962,7 +962,7 @@ - + Advanced Options -- cgit v1.3.1