using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.PPC.Common;
using Tango.PPC.Jobs.Messages;
using System.Data.Entity;
using Tango.Core.Commands;
using System.Windows;
using Tango.Touch.Controls;
using System.Windows.Media;
using Tango.DragAndDrop;
using System.ComponentModel;
using System.Windows.Data;
using Tango.PPC.Jobs.Dialogs;
using Tango.PPC.Jobs.Views;
using System.Runtime.InteropServices;
using System.Threading;
using Tango.SharedUI.Helpers;
using Tango.PPC.Common.Navigation;
using Tango.PPC.Jobs.NavigationObjects;
using Tango.PPC.Jobs.ViewContracts;
using System.Collections.ObjectModel;
using Tango.PPC.Common.Models;
using Tango.Logging;
using Tango.PPC.Common.Messages;
using Tango.BL.Builders;
using Tango.PPC.Jobs.AppButtons;
using Tango.Core.Threading;
using System.Diagnostics;
using System.Runtime.ExceptionServices;
using Tango.Explorer;
using Tango.PPC.Storage;
using System.IO;
using Tango.ColorConversion;
using Tango.Integration.Operation;
namespace Tango.PPC.Jobs.ViewModels
{
///
/// Represents the selected job view model.
///
///
public class JobViewVM : PPCViewModel, INavigationObjectReceiver
{
private ObservablesContext _db;
private bool _can_navigate_back;
private Thread _check_gamut_thread;
private Job _job_to_load;
private JobNavigationIntent _job_to_load_intent;
private static Dictionary> _jobs_fine_tune_items;
private StartPrintingButton _start_printing_btn;
private ActionTimer _volumeConversionTimer;
private IColorConverter _converter;
private string _current_job_string;
#region Properties
private Job _job;
///
/// Gets or sets the selected job.
///
public Job Job
{
get { return _job; }
set { _job = value; RaisePropertyChangedAuto(); }
}
private ICollectionView _segmentsCollectionView;
///
/// Gets or sets the job segments collection view.
///
public ICollectionView SegmentsCollectionView
{
get { return _segmentsCollectionView; }
set
{
_segmentsCollectionView = value;
RaisePropertyChangedAuto();
}
}
private List _colorSpaces;
///
/// Gets or sets the available color spaces.
///
public List ColorSpaces
{
get { return _colorSpaces; }
set { _colorSpaces = value; RaisePropertyChangedAuto(); }
}
private List _rmls;
///
/// Gets or sets the available RMLS.
///
public List Rmls
{
get { return _rmls; }
set { _rmls = value; RaisePropertyChangedAuto(); }
}
private List _spoolTypes;
///
/// Gets or sets the available spool types.
///
public List SpoolTypes
{
get { return _spoolTypes; }
set { _spoolTypes = value; RaisePropertyChangedAuto(); }
}
private List _customers;
///
/// Gets or sets the available customers.
///
public List Customers
{
get { return _customers; }
set { _customers = value; RaisePropertyChangedAuto(); }
}
private String _customersFilter;
///
/// Gets or sets the customers filter.
///
public String CustomersFilter
{
get { return _customersFilter; }
set { _customersFilter = value; RaisePropertyChangedAuto(); }
}
///
/// Gets or sets the customers automatic complete provider.
///
public AutoCompleteProvider CustomersAutoCompleteProvider { get; set; }
private ObservableCollection _fineTuneItems;
///
/// Gets or sets the fine tune items.
///
public ObservableCollection FineTuneItems
{
get { return _fineTuneItems; }
set { _fineTuneItems = value; RaisePropertyChangedAuto(); }
}
private ObservableCollection _approvalFineTuneItems;
///
/// Gets or sets the fine tune items.
///
public ObservableCollection ApprovalFineTuneItems
{
get { return _approvalFineTuneItems; }
set { _approvalFineTuneItems = value; RaisePropertyChangedAuto(); }
}
private bool _isFineTuneExpanded;
///
/// Gets or sets a value indicating whether the fine tuning region is expanded.
///
public bool IsFineTuneExpanded
{
get { return _isFineTuneExpanded; }
set
{
_isFineTuneExpanded = value;
RaisePropertyChangedAuto();
if (_isFineTuneExpanded)
{
SyncFineTuneItemsToBrushStops();
}
}
}
private bool _isJobDetailsExpanded;
///
/// Gets or sets a value indicating whether the job details area is expanded.
///
public bool IsJobDetailsExpanded
{
get { return _isJobDetailsExpanded; }
set { _isJobDetailsExpanded = value; RaisePropertyChangedAuto(); }
}
private List _catalogItems;
///
/// Gets or sets the twine catalog items.
///
public List CatalogItems
{
get { return _catalogItems; }
set { _catalogItems = value; RaisePropertyChangedAuto(); }
}
private ColorCatalogsItem _selectedCatalogItem;
///
/// Gets or sets the selected catalog item.
///
public ColorCatalogsItem SelectedCatalogItem
{
get { return _selectedCatalogItem; }
set { _selectedCatalogItem = value; RaisePropertyChangedAuto(); }
}
///
/// Gets or sets the twine catalog automatic complete provider.
///
public IAutoCompleteProvider CatalogAutoCompleteProvider { get; set; }
private ColorCatalog _selectedCatalog;
///
/// Gets or sets the selected catalog.
///
public ColorCatalog SelectedCatalog
{
get { return _selectedCatalog; }
set { _selectedCatalog = value; RaisePropertyChangedAuto(); }
}
#endregion
#region Commands
///
/// Gets or sets the add solid segment command.
///
public RelayCommand AddSolidSegmentCommand { get; set; }
///
/// Gets or sets the add gradient segment command.
///
public RelayCommand AddGradientSegmentCommand { get; set; }
///
/// Gets or sets the add brush stop command.
///
public RelayCommand AddBrushStopCommand { get; set; }
///
/// Gets or sets the segment dropped command.
///
public RelayCommand SegmentDroppedCommand { get; set; }
///
/// Gets or sets the remove segment command.
///
public RelayCommand RemoveSegmentCommand { get; set; }
///
/// Gets or sets the remove brush stop command.
///
public RelayCommand RemoveBrushStopCommand { get; set; }
///
/// Gets or sets the remove job command.
///
public RelayCommand RemoveJobCommand { get; set; }
///
/// Gets or sets the save job command.
///
public RelayCommand SaveJobCommand { get; set; }
///
/// Gets or sets the replace brush stop command.
///
public RelayCommand ReplaceBrushStopCommand { get; set; }
///
/// Gets or sets the twine catalog field tap command.
///
public RelayCommand OpenCatalogCommand { get; set; }
///
/// Gets or sets the increase decrease samples to dye command.
///
public RelayCommand IncreaseDecreaseSamplesToDyeCommand { get; set; }
///
/// Gets or sets the start sample dye command.
///
public RelayCommand StartSampleDyeCommand { get; set; }
///
/// Gets or sets the dye command.
///
public RelayCommand DyeCommand { get; set; }
///
/// Gets or sets the approve sample command.
///
public RelayCommand ApproveSampleCommand { get; set; }
///
/// Gets or sets the repeat sample dye command.
///
public RelayCommand RepeatSampleDyeCommand { get; set; }
///
/// Gets or sets another sample command.
///
public RelayCommand AnotherSampleCommand { get; set; }
///
/// Gets or sets the invoke fine tuning palette command.
///
public RelayCommand InvokeFineTuningPaletteCommand { get; set; }
///
/// Gets or sets the reset fine tuning command.
///
public RelayCommand ResetFineTuningCommand { get; set; }
///
/// Gets or sets the start fine tuning command.
///
public RelayCommand StartFineTuningCommand { get; set; }
///
/// Gets or sets the approve fine tuning command.
///
public RelayCommand ApproveFineTuningCommand { get; set; }
///
/// Gets or sets the repeat fine tuning command.
///
public RelayCommand RepeatFineTuningCommand { get; set; }
///
/// Gets or sets the export embroidery command.
///
public RelayCommand ExportEmbroideryCommand { get; set; }
#endregion
#region Constructors
///
/// Initializes the class.
///
static JobViewVM()
{
_jobs_fine_tune_items = new Dictionary>();
}
///
/// Initializes a new instance of the class.
///
public JobViewVM()
{
_converter = new DefaultColorConverter();
_volumeConversionTimer = new ActionTimer(TimeSpan.FromMilliseconds(50));
RegisterForMessage(HandleJobSelectedMessage);
FineTuneItems = new ObservableCollection();
ApprovalFineTuneItems = new ObservableCollection();
CatalogItems = new List();
CustomersAutoCompleteProvider = new AutoCompleteProvider((customer, filter) =>
{
return customer.Name.ToLower().StartsWith(filter != null ? filter.ToLower() : String.Empty);
});
CatalogAutoCompleteProvider = new AutoCompleteProvider((item, filter) =>
{
return !String.IsNullOrWhiteSpace(filter) && item.Name.ToLower().StartsWith(filter.ToLower());
});
//Initialize Commands
AddSolidSegmentCommand = new RelayCommand(() => AddSolidSegment());
AddBrushStopCommand = new RelayCommand(AddBrushStop);
AddGradientSegmentCommand = new RelayCommand(() => AddGradientSegment());
SegmentDroppedCommand = new RelayCommand((e) =>
{
DragAndDropSegment(
(e.Draggable as FrameworkElement).DataContext as Segment,
(e.Droppable as FrameworkElement).DataContext as Segment);
});
RemoveSegmentCommand = new RelayCommand(RemoveSegment);
RemoveBrushStopCommand = new RelayCommand(RemoveBrushStop);
RemoveJobCommand = new RelayCommand(RemoveJob);
SaveJobCommand = new RelayCommand(() => SaveJob());
ReplaceBrushStopCommand = new RelayCommand(InvokeColorAdjustmentForBrushStop);
IncreaseDecreaseSamplesToDyeCommand = new RelayCommand((x) =>
{
if (x == "+")
{
Job.SampleUnitsOrMeters++;
}
else
{
Job.SampleUnitsOrMeters--;
}
});
_check_gamut_thread = new Thread(CheckGamutThreadMethod);
_check_gamut_thread.IsBackground = true;
StartSampleDyeCommand = new RelayCommand(StartSampleDye, CanStartJob);
DyeCommand = new RelayCommand(StartJob, CanStartJob);
ApproveSampleCommand = new RelayCommand(ApproveSampleDye);
RepeatSampleDyeCommand = new RelayCommand(RepeatSampleDye);
AnotherSampleCommand = new RelayCommand(DyeAnotherSample);
InvokeFineTuningPaletteCommand = new RelayCommand(InvokeFineTuningPalette);
ResetFineTuningCommand = new RelayCommand(ResetFineTuning);
StartFineTuningCommand = new RelayCommand(StartFineTuning, () => FineTuneItems.Any(x => x.IsSelected) && CanStartJob());
RepeatFineTuningCommand = new RelayCommand(RepeatFineTuning);
ApproveFineTuningCommand = new RelayCommand(ApproveFineTuning);
OpenCatalogCommand = new RelayCommand(OpenCatalog);
ExportEmbroideryCommand = new RelayCommand(ExportEmbroidery);
}
#endregion
#region Job Management
///
/// Loads the job.
///
private async void LoadJob()
{
try
{
if (!(_job_to_load == null || (_job_to_load != null && Job != null && _job_to_load.Guid == Job.Guid)))
{
View.ScrollToTop();
LogManager.Log($"Loading selected job '{_job_to_load.Name}'...");
//NotificationProvider.SetGlobalBusyMessage("Loading job details...");
IsFree = false;
_can_navigate_back = false;
if (_db != null)
{
Rmls.ForEach(x => x.Cct = null);
Rmls = null;
_db.Dispose();
GC.Collect();
}
_db = ObservablesContext.CreateDefault();
Job = await new JobBuilder(_db).Set(_job_to_load.Guid)
.WithConfiguration()
.WithRML()
.WithUser()
.WithSegments()
.WithBrushStops()
.BuildAsync();
Job.RmlChanged -= OnRmlChanged;
Job.RmlChanged += OnRmlChanged;
Job.NameChanged -= Job_NameChanged;
Job.NameChanged += Job_NameChanged;
Job.ValidateOnPropertyChanged = true;
LogManager.Log("Loading RMLS...");
Rmls = (await new RmlsCollectionBuilder(_db).SetAll().WithActiveParametersGroup().WithCAT(Job.MachineGuid).WithCCT().WithLiquidFactors().BuildAsync()).ToList();
LogManager.Log("Loading Color Spaces...");
ColorSpaces = await _db.ColorSpaces.ToListAsync();
LogManager.Log("Loading Spool Types...");
SpoolTypes = await _db.SpoolTypes.ToListAsync();
LogManager.Log("Loading Customers...");
Customers = await _db.Customers.Where(x => x.OrganizationGuid == MachineProvider.Machine.OrganizationGuid).ToListAsync();
if (Job.ColorSpace.Space == BL.Enumerations.ColorSpaces.Catalog)
{
SelectedCatalog = await new ColorCatalogBuilder(_db).Set(Job.ColorCatalogGuid).WithGroups().WithItems().BuildAsync();
if (SelectedCatalog != null)
{
CatalogItems = SelectedCatalog.ColorCatalogsGroups.SelectMany(x => x.ColorCatalogsItems).OrderBy(x => x.ItemIndex).ToList();
}
else
{
await NotificationProvider.ShowError("The selected color catalog for this job could not be found.\nCannot load job.");
Job = null;
_can_navigate_back = true;
await NavigationManager.NavigateBack();
return;
}
}
foreach (var segment in Job.Segments)
{
SetSegmentLiquidVolumesIfVolume(segment);
}
if (!_check_gamut_thread.IsAlive)
{
_check_gamut_thread.Start();
}
SegmentsCollectionView = CollectionViewSource.GetDefaultView(Job.Segments);
SegmentsCollectionView.SortDescriptions.Add(new SortDescription(nameof(Segment.SegmentIndex), ListSortDirection.Ascending));
ResetFineTuning();
_job_to_load = null;
_current_job_string = Job.ToJobFileWhenLoaded().ToString();
}
if (!_jobs_fine_tune_items.ContainsKey(Job.Guid) && Job.JobFineTuningStatus == BL.Enumerations.FineTuningStatuses.PendingApproval)
{
Job.JobFineTuningStatus = BL.Enumerations.FineTuningStatuses.Unspecified;
}
if (_job_to_load_intent == JobNavigationIntent.NewJob)
{
IsJobDetailsExpanded = true;
}
LogManager.Log($"Job editing state = '{Job.JobEditingState}'.");
if (Job.JobEditingState == BL.Enumerations.EditingStates.SampleDye && Job.JobSampleDyeStatus == BL.Enumerations.SampleDyeStatuses.PendingApproval)
{
LogManager.Log("Directing view to display sample dye region.");
View.DisplaySampleDye();
}
else if (Job.JobEditingState == BL.Enumerations.EditingStates.FineTuning && Job.JobFineTuningStatus == BL.Enumerations.FineTuningStatuses.PendingApproval)
{
LogManager.Log("Directing view to display fine tuning region.");
View.DisplayFineTuning();
}
ValidateBrushStops();
DyeCommand.RaiseCanExecuteChanged();
StartSampleDyeCommand.RaiseCanExecuteChanged();
StartFineTuningCommand.RaiseCanExecuteChanged();
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error loading job '{_job_to_load.Name}'");
await NotificationProvider.ShowError("An error occurred while trying to load the selected job.");
_can_navigate_back = true;
await NavigationManager.NavigateBack();
}
finally
{
InvokeUI(() =>
{
IsFree = true;
//NotificationProvider.ReleaseGlobalBusyMessage();
});
}
}
private void Job_NameChanged(object sender, string e)
{
DyeCommand.RaiseCanExecuteChanged();
}
///
/// Saves the job.
///
private async void SaveJob(bool displayNotification = true)
{
try
{
if (Job.Validate(_db))
{
LogManager.Log("Saving job...");
if (!String.IsNullOrWhiteSpace(CustomersFilter))
{
if (!Customers.Exists(x => x.Name == CustomersFilter))
{
var newCustomer = new Customer()
{
OrganizationGuid = MachineProvider.Machine.OrganizationGuid,
Name = CustomersFilter,
};
_db.Customers.Add(newCustomer);
Job.Customer = newCustomer;
}
}
Job.LastUpdated = DateTime.UtcNow;
Job.JobStatus = BL.Enumerations.JobStatuses.Draft;
await _db.SaveChangesAsync();
_current_job_string = Job.ToJobFileWhenLoaded().ToString();
if (displayNotification)
{
await NotificationProvider.ShowInfo(String.Format("Job '{0}' saved successfully.", Job.Name));
}
RaiseMessage(new JobSavedMessage() { Job = Job });
}
else
{
await NotificationProvider.ShowError($"Error saving job. {Job.ValidationErrors.FirstOrDefault()}");
}
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error saving job '{Job.Name}'.");
await NotificationProvider.ShowError("An error occurred while trying to save the job.");
}
}
///
/// Removes the job.
///
private async void RemoveJob()
{
try
{
LogManager.Log("Removing job...");
if (await NotificationProvider.ShowQuestion("Are you sure you want to delete the this job?"))
{
await Job.DeleteCascadeAsync(_db);
RaiseMessage(new JobRemovedMessage() { Job = Job });
_can_navigate_back = true;
await NavigationManager.NavigateBack();
}
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error removing job '{Job.Name}'.");
await NotificationProvider.ShowError("An error occurred while trying to remove the job.");
}
}
///
/// Starts the job.
///
private async void StartJob()
{
try
{
LogManager.Log("Start job command pressed. Starting job and navigating to job progress view...");
await PrintingManager.Print(Job, _db);
await NavigationManager.NavigateTo(nameof(JobProgressView));
}
catch (InsufficientLiquidQuantityException)
{
//Ignore..
}
catch (Exception ex)
{
LogManager.Log(ex, "Could not start the current job.");
await NotificationProvider.ShowError($"{ex.Message}.");
}
}
///
/// Determines whether this instance [can start job].
///
private bool CanStartJob()
{
return
Job != null && Job.Validate(_db) &&
!Job.Segments.SelectMany(x => x.BrushStops).ToList().Exists(x => x.IsOutOfGamut);
}
#endregion
#region RML Changed
protected virtual void OnRmlChanged(object sender, Rml rml)
{
//There is no need for that.
////Replace all brush stops twine color catalogs.
//foreach (var stop in Job.Segments.SelectMany(x => x.BrushStops).Where(x => x.ColorCatalog != null && x.BrushColorSpace == BL.Enumerations.ColorSpaces.Twine).ToList())
//{
// var replacement = CatalogItems.SingleOrDefault(x => x.Name == stop.ColorCatalog.Name);
// stop.ColorCatalog = replacement;
//}
////Replace all brush stops coats color catalogs.
//foreach (var stop in Job.Segments.SelectMany(x => x.BrushStops).Where(x => x.ColorCatalog != null && x.BrushColorSpace == BL.Enumerations.ColorSpaces.Coats).ToList())
//{
// var replacement = CoatsCatalogItems.SingleOrDefault(x => x.Name == stop.ColorCatalog.Name);
// stop.ColorCatalog = replacement;
//}
ResetFineTuning();
}
#endregion
#region Segments Management
///
/// Adds a new solid segment.
///
private Segment AddSolidSegment()
{
try
{
LogManager.Log("Adding new solid segment...");
var s = Job.AddSolidSegment(MachineProvider.Machine.DefaultSegmentLength > 0 ? MachineProvider.Machine.DefaultSegmentLength : 10);
SetSegmentLiquidVolumesIfVolume(s);
return s;
}
catch (Exception ex)
{
LogManager.Log(ex, "Could not add a new solid segment.");
NotificationProvider.ShowError("An error occurred while trying to add a new segment.");
return null;
}
}
///
/// Adds a new gradient segment.
///
private Segment AddGradientSegment()
{
try
{
LogManager.Log("Adding new gradient segment...");
var s = Job.AddGradientSegment(MachineProvider.Machine.DefaultSegmentLength > 0 ? MachineProvider.Machine.DefaultSegmentLength : 10);
SetSegmentLiquidVolumesIfVolume(s);
return s;
}
catch (Exception ex)
{
LogManager.Log(ex, "Could not add a new gradient segment.");
NotificationProvider.ShowError("An error occurred while trying to add a new segment.");
return null;
}
}
///
/// Called when a segment has been dragged and dropped into another segment.
///
/// The dragged job.
/// The dropped job.
private void DragAndDropSegment(Segment draggedSegment, Segment droppedSegment)
{
LogManager.Log($"Segment Drag & Drop '{draggedSegment.SegmentIndex}' => '{droppedSegment.SegmentIndex}'.");
if (draggedSegment.SegmentIndex > droppedSegment.SegmentIndex)
{
draggedSegment.SegmentIndex = droppedSegment.SegmentIndex - 1;
}
else
{
draggedSegment.SegmentIndex = droppedSegment.SegmentIndex + 1;
}
int index = 1;
foreach (var segment in Job.Segments.OrderBy(x => x.SegmentIndex))
{
segment.SegmentIndex = index++;
}
SegmentsCollectionView.Refresh();
}
///
/// Removes the segment.
///
/// The segment.
private async void RemoveSegment(Segment segment)
{
if (Job.Segments.Count > 1)
{
try
{
if (await NotificationProvider.ShowQuestion("Are you sure you want to remove the selected segment?"))
{
LogManager.Log($"Removing job segment {segment.SegmentIndex}");
segment.BrushStops.ToList().ForEach(x =>
{
_db.BrushStops.Remove(x);
});
_db.Segments.Remove(segment);
ArrangeSegmentsIndices();
DyeCommand.RaiseCanExecuteChanged();
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Could not remove segment.");
await NotificationProvider.ShowError("An error occurred while trying to add a new segment.");
}
}
else
{
await NotificationProvider.ShowInfo("A job must contain at least one color segment.");
}
}
///
/// Sets the segment liquid volumes.
///
/// The segment.
private void SetSegmentLiquidVolumesIfVolume(Segment segment)
{
if (Job.ColorSpace.Code == BL.Enumerations.ColorSpaces.Volume.ToInt32())
{
foreach (var stop in segment.BrushStops)
{
stop.SetLiquidVolumes(Job.Machine.Configuration, Job.Rml, Job.Rml.GetActiveProcessGroup().ProcessParametersTables.FirstOrDefault());
var lub = stop.LiquidVolumes.FirstOrDefault(x => x.IdsPack.LiquidType.Code == (int)BL.Enumerations.LiquidTypes.Lubricant);
if (lub != null)
{
lub.Volume = 100;
}
}
}
}
private void ArrangeSegmentsIndices()
{
for (int i = 0; i < Job.Segments.Count; i++)
{
Job.Segments[i].SegmentIndex = i + 1;
}
}
#endregion
#region Brush Stops Management
///
/// Adds a new brush stop to the specified segment.
///
/// The segment.
private void AddBrushStop(Segment segment)
{
LogManager.Log($"Adding new brush stop to segment {segment.SegmentIndex}.");
segment.AddBrushStop();
SetSegmentLiquidVolumesIfVolume(segment);
}
///
/// Removes the brush stop.
///
/// The brush stop.
private void RemoveBrushStop(BrushStop brushStop)
{
if (brushStop.Segment.BrushStops.Count > 2)
{
LogManager.Log($"removing brush stop {brushStop.StopIndex} from segment {brushStop.Segment.SegmentIndex}.");
var segment = brushStop.Segment;
_db.BrushStops.Remove(brushStop);
ArrangeBrushStopsIndices(segment);
}
else
{
NotificationProvider.ShowInfo("Gradient segments must contain at least two colors.");
}
}
///
/// Invokes the color adjustment for the specified brush stop.
///
/// The brush stop.
private async void InvokeColorAdjustmentForBrushStop(BrushStop brushStop)
{
try
{
LogManager.Log($"Invoking triplet color adjustment dialog for brush stop {brushStop.StopIndex} at segment {brushStop.Segment.SegmentIndex}.");
LogManager.Log("Retrieving color conversion suggestions for brush stop...");
var conversionOutput = _converter.Convert(brushStop, true);
BasicColorCorrectionViewVM vm = null;
vm = await NotificationProvider.ShowDialog(new BasicColorCorrectionViewVM()
{
InvalidBrushStop = brushStop,
Suggestions = conversionOutput.CreateTrippletSuggestions(),
});
if (vm.Result == BasicColorCorrectionViewVM.ColorCorrectionDialogResult.MoreOptions)
{
LogManager.Log("Invoking hive color conversion dialog...");
vm = await NotificationProvider.ShowDialog(new AdvancedColorCorrectionViewVM()
{
InvalidBrushStop = brushStop,
Suggestions = conversionOutput.CreateHiveSuggestions(),
});
}
if (vm.Result == BasicColorCorrectionViewVM.ColorCorrectionDialogResult.Confirmed)
{
LogManager.Log($"Color suggestion selected: {vm.SelectedSuggestion.Color.ToString()}.");
if (brushStop.BrushColorSpace == BL.Enumerations.ColorSpaces.RGB)
{
brushStop.Red = vm.SelectedSuggestion.Coordinates.Red;
brushStop.Green = vm.SelectedSuggestion.Coordinates.Green;
brushStop.Blue = vm.SelectedSuggestion.Coordinates.Blue;
}
else if (brushStop.BrushColorSpace == BL.Enumerations.ColorSpaces.LAB)
{
brushStop.L = vm.SelectedSuggestion.Coordinates.L;
brushStop.A = vm.SelectedSuggestion.Coordinates.A;
brushStop.B = vm.SelectedSuggestion.Coordinates.B;
}
brushStop.Corrected = true;
brushStop.IsOutOfGamut = false;
brushStop.OutOfGamutChecked = true;
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error while invoking color adjustment dialog.");
await NotificationProvider.ShowError("An error occurred while trying to convert the selected color.");
}
}
///
/// Called when the brush stop field value has been changed (This called from the view!).
///
/// The brush stop.
[HandleProcessCorruptedStateExceptions]
public void OnBrushStopFieldValueChanged(BrushStop stop)
{
if (stop != null)
{
stop.Corrected = false;
stop.OutOfGamutChecked = false;
if (stop.BrushColorSpace == BL.Enumerations.ColorSpaces.Volume)
{
_volumeConversionTimer.ResetReplace(() =>
{
try
{
var output = _converter.Convert(stop, false);
stop.Red = output.SingleCoordinates.Red;
stop.Green = output.SingleCoordinates.Green;
stop.Blue = output.SingleCoordinates.Blue;
stop.Corrected = true;
stop.IsOutOfGamut = false;
}
catch (Exception ex)
{
LogManager.Log(ex, "An error occurred while trying to get volume => RGB from conversion engine.");
}
});
}
}
}
///
/// Opens the twine catalog for the specified brush stop.
///
/// The stop.
private async void OpenCatalog(BrushStop stop)
{
var catalogItem = await NavigationManager.NavigateForResult(new TwineCatalogNavigationObject()
{
SelectedItem = stop.ColorCatalogsItem,
Catalog = SelectedCatalog
}, true);
if (catalogItem != null)
{
stop.ColorCatalogsItem = catalogItem;
}
}
private void ArrangeBrushStopsIndices(Segment segment)
{
for (int i = 0; i < segment.BrushStops.Count; i++)
{
segment.BrushStops[i].StopIndex = i + 1;
}
}
private bool ValidateBrushStops()
{
return Job.Segments.SelectMany(x => x.BrushStops).ToList().All(x => x.Validate(_db));
}
#endregion
#region Job Selection Message
///
/// Handles the job selected message.
///
/// The message.
private void HandleJobSelectedMessage(JobSelectedMessage message)
{
_job_to_load = message.Job;
}
#endregion
#region Sample Dye
///
/// Starts a sample dye.
///
private async void StartSampleDye()
{
try
{
LogManager.Log("Sample dye command pressed...");
await PrintingManager.PrintSample(Job, _db);
await NavigationManager.NavigateTo(nameof(JobProgressView));
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error executing sample dye for job {Job.Name}.");
await NotificationProvider.ShowError(ex.Message);
}
}
///
/// Sets the job status back to not approved.
///
private void RepeatSampleDye()
{
LogManager.Log("Repeat sample dye command pressed...");
Job.JobEditingState = BL.Enumerations.EditingStates.Default;
Job.JobSampleDyeStatus = BL.Enumerations.SampleDyeStatuses.Unspecified;
}
///
/// Approves the sample dye.
///
private void ApproveSampleDye()
{
LogManager.Log("Approve sample dye command pressed...");
Job.JobEditingState = BL.Enumerations.EditingStates.Default;
Job.JobSampleDyeStatus = BL.Enumerations.SampleDyeStatuses.Approved;
Job.SampleDyeApproveDate = DateTime.UtcNow;
SaveJob(false);
}
///
/// Dyes another sample.
///
private void DyeAnotherSample()
{
LogManager.Log("Dye another sample dye command pressed...");
Job.JobEditingState = BL.Enumerations.EditingStates.Default;
Job.JobSampleDyeStatus = BL.Enumerations.SampleDyeStatuses.Unspecified;
}
#endregion
#region Fine Tuning
///
/// Synchronizes the fine tune items to brush stops.
///
private void SyncFineTuneItemsToBrushStops()
{
try
{
if (Job != null)
{
if (_jobs_fine_tune_items.ContainsKey(Job.Guid))
{
FineTuneItems = _jobs_fine_tune_items[Job.Guid].ToObservableCollection();
}
else
{
FineTuneItems.Clear();
foreach (var stop in Job.Segments.SelectMany(x => x.BrushStops).Where(x => x.ColorSpace.Space == BL.Enumerations.ColorSpaces.RGB || x.ColorSpace.Space == BL.Enumerations.ColorSpaces.LAB).DistinctBy(x => x.Color))
{
FineTuneItem item = new FineTuneItem(_converter.Convert(stop, true));
item.BrushStops = Job.Segments.SelectMany(x => x.BrushStops).Where(x => x.Color == stop.Color).ToList();
item.SelectedSuggestion = item.Suggestions[item.Suggestions.Count / 2];
item.SelectedChanged += () => StartFineTuningCommand.RaiseCanExecuteChanged();
FineTuneItems.Add(item);
}
_jobs_fine_tune_items[Job.Guid] = FineTuneItems.ToList();
}
ApprovalFineTuneItems = FineTuneItems.Where(x => x.IsSelected).ToObservableCollection();
StartFineTuningCommand.RaiseCanExecuteChanged();
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error while trying to synchronize fine tuning items with brush stops.");
}
}
///
/// Invokes the fine tuning palette dialog.
///
/// The fine tune item.
private async void InvokeFineTuningPalette(FineTuneItem fineTuneItem)
{
LogManager.Log("Invoke fine tuning palette command pressed...");
try
{
FineTuningPaletteViewVM vm = new FineTuningPaletteViewVM(fineTuneItem, Job);
await NotificationProvider.ShowDialog(vm);
if (vm.DialogResult)
{
fineTuneItem.Suggestions = vm.Suggestions;
fineTuneItem.SelectedSuggestion = vm.SelectedSuggestion;
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error invoking the fine tunning palette");
await NotificationProvider.ShowError("An error occurred while trying to display the fine tunning palette.");
}
}
///
/// Resets the fine tuning.
///
private void ResetFineTuning()
{
if (Job != null && _jobs_fine_tune_items.ContainsKey(Job.Guid))
{
_jobs_fine_tune_items.Remove(Job.Guid);
}
SyncFineTuneItemsToBrushStops();
}
///
/// Starts the fine tuning.
///
private async void StartFineTuning()
{
try
{
LogManager.Log("Start fine tunning job command pressed...");
_jobs_fine_tune_items[Job.Guid] = FineTuneItems.ToList();
await PrintingManager.PrintFineTuning(Job, _db, FineTuneItems);
await NavigationManager.NavigateTo(nameof(JobProgressView));
}
catch (Exception ex)
{
LogManager.Log(ex, "Error executing fine tuning job.");
await NotificationProvider.ShowError(ex.Message);
}
}
///
/// Approves the fine tuning.
///
private void ApproveFineTuning()
{
LogManager.Log("Approve fine tuning command pressed.");
Job.JobEditingState = BL.Enumerations.EditingStates.Default;
Job.JobFineTuningStatus = BL.Enumerations.FineTuningStatuses.Approved;
foreach (var item in ApprovalFineTuneItems)
{
foreach (var stop in item.BrushStops)
{
stop.Color = item.SelectedSuggestion.Color;
}
}
Job.FineTuningApproveDate = DateTime.UtcNow;
SaveJob(false);
if (_jobs_fine_tune_items.ContainsKey(Job.Guid))
{
_jobs_fine_tune_items.Remove(Job.Guid);
}
SyncFineTuneItemsToBrushStops();
}
///
/// Repeats the fine tuning.
///
private void RepeatFineTuning()
{
LogManager.Log("Repeat fine tuning command pressed.");
Job.JobEditingState = BL.Enumerations.EditingStates.Default;
Job.JobFineTuningStatus = BL.Enumerations.FineTuningStatuses.Unspecified;
}
#endregion
#region Out Of Gamut Check Thread
///
/// Iterates over all brush stops and checks for out of gamut.
///
[HandleProcessCorruptedStateExceptions]
private void CheckGamutThreadMethod()
{
while (true)
{
Thread.Sleep(500);
if (Job != null && Job.Rml.Cct != null && IsVisible && (Job.ColorSpace != null && (Job.ColorSpace.Code == BL.Enumerations.ColorSpaces.RGB.ToInt32() || Job.ColorSpace.Code == BL.Enumerations.ColorSpaces.LAB.ToInt32())))
{
var brushStops = Job.Segments.SelectMany(x => x.BrushStops).Where(x => !x.Corrected && !x.OutOfGamutChecked).ToList();
foreach (var stop in brushStops)
{
try
{
stop.IsOutOfGamut = _converter.IsOutOfGamut(stop);
stop.OutOfGamutChecked = true;
}
catch (AccessViolationException)
{
LogManager.Log($"Out of gamut check failed for brush stop {stop.StopIndex} at segment {stop.Segment.SegmentIndex}.", LogCategory.Warning);
continue;
}
catch
{
LogManager.Log($"Out of gamut check failed for brush stop {stop.StopIndex} at segment {stop.Segment.SegmentIndex}.", LogCategory.Warning);
}
}
if (brushStops.Count > 0)
{
InvokeUI(() =>
{
DyeCommand.RaiseCanExecuteChanged();
StartSampleDyeCommand.RaiseCanExecuteChanged();
StartFineTuningCommand.RaiseCanExecuteChanged();
});
}
}
}
}
#endregion
#region Export Embroidery
private async void ExportEmbroidery()
{
try
{
if (!StorageProvider.IsConnected)
{
await NotificationProvider.ShowError("No storage device connected.");
return;
}
var result = await NavigationManager.
NavigateForResult(
new Storage.Models.StorageNavigationRequest()
{
Intent = Storage.Models.StorageNavigationIntent.SaveFile,
DefaultFileName = Job.Name + Path.GetExtension(Job.EmbroideryFileName),
Filter = Path.GetExtension(Job.EmbroideryFileName),
Title = "Export Embroidery File",
});
if (result != null)
{
File.WriteAllBytes(Path.HasExtension(result.Path) ? result.Path : result.Path + Path.GetExtension(Job.EmbroideryFileName), Job.EmbroideryFileData);
await NotificationProvider.ShowSuccess("Embroidery file exported successfully.");
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error exporting embroidery file.");
await NotificationProvider.ShowError("An error occurred while trying to save the selected embroidery file.");
}
}
#endregion
#region IPPC ViewModel Overrides
///
/// Called when the application has been started.
///
public override void OnApplicationStarted()
{
base.OnApplicationStarted();
MachineProvider.MachineOperator.PrintingEnded += MachineOperator_PrintingEnded;
}
private void MachineOperator_PrintingEnded(object sender, Integration.Operation.PrintingEventArgs e)
{
if (IsVisible)
{
_start_printing_btn.Push();
}
}
///
/// Called when the navigation system has navigated to this VM view.
///
public override void OnNavigatedTo()
{
if (!MachineProvider.MachineOperator.IsPrinting)
{
_start_printing_btn.Push();
}
base.OnNavigatedTo();
LoadJob();
}
///
/// Called when the navigation system has navigated from this VM view.
///
public override void OnNavigatedFrom()
{
_start_printing_btn.Pop();
base.OnNavigatedFrom();
_job_to_load_intent = JobNavigationIntent.Default;
}
///
/// Called before the navigation system navigates back from this object.
/// Return false to abort the navigation.
///
///
public async override Task OnNavigateBackRequest()
{
bool result = true;
if (!_can_navigate_back)
{
bool jobChainged = false;
if (Job != null)
{
string job_string = Job.ToJobFileWhenLoaded().ToString();
jobChainged = job_string != _current_job_string;
}
if (jobChainged)
{
if (await NotificationProvider.ShowQuestion("Are you sure you want to exit this job without saving changes?"))
{
Job = null;
SegmentsCollectionView = null;
}
else
{
result = false;
}
}
else
{
Job = null;
SegmentsCollectionView = null;
}
}
return result;
}
public override void OnApplicationReady()
{
base.OnApplicationReady();
_start_printing_btn = new StartPrintingButton(DyeCommand, MachineProvider.MachineOperator);
}
#endregion
#region INavigationObjectReceiver
public void OnNavigatedToWithObject(JobNavigationObject e)
{
_job_to_load_intent = e.Intent;
_job_to_load = e.Job;
}
#endregion
}
}