aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/Converters/SegmentToGradientStopsConverterMulti.cs
blob: 697a904072b07cccc8c5aaa773aea30a5cbf6224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
using Tango.BL.Entities;

namespace Tango.MachineStudio.Developer.Converters
{
    public class SegmentToGradientStopsConverterMulti : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                Segment segment = values[0] as Segment;

                if (segment != null)
                {
                    GradientStopCollection stops = new GradientStopCollection();

                    foreach (var stop in segment.BrushStops.OrderBy(x => x.StopIndex))
                    {
                        stops.Add(new GradientStop(stop.Color, stop.OffsetPercent / 100d));
                    }

                    return stops;
                }

                return null;
            }
            catch
            {
                return null;
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
ss="k">using Tango.MachineStudio.Common.Notifications; using Tango.SharedUI; namespace Tango.MachineStudio.HardwareDesigner.ViewModels { public class MainViewVM : ViewModel { private INotificationProvider _notification; private bool _isNew; private ObservablesEntitiesAdapter _adapter; public ObservablesEntitiesAdapter Adapter { get { return _adapter; } set { _adapter = value; RaisePropertyChangedAuto(); } } private HardwareVersion _selectedVersion; public HardwareVersion SelectedVersion { get { return _selectedVersion; } set { _selectedVersion = value; RaisePropertyChangedAuto(); OnSelectedVersionChanged(); } } private HardwareVersion _currentVersion; public HardwareVersion CurrentVersion { get { return _currentVersion; } set { _currentVersion = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private IObservableEntity _selectedHardwareObject; public IObservableEntity SelectedHardwareObject { get { return _selectedHardwareObject; } set { _selectedHardwareObject = null; RaisePropertyChangedAuto(); _selectedHardwareObject = value; RaisePropertyChangedAuto(); } } public RelayCommand SaveCommand { get; set; } public RelayCommand DeleteCommand { get; set; } public RelayCommand NewCommand { get; set; } public MainViewVM(INotificationProvider notification) { _notification = notification; Adapter = ObservablesEntitiesAdapter.Instance; SaveCommand = new RelayCommand(Save, () => CurrentVersion != null); NewCommand = new RelayCommand(New); DeleteCommand = new RelayCommand(Delete, () => !_isNew && CurrentVersion != null); } private void OnSelectedVersionChanged() { if (SelectedVersion != null) { _isNew = false; CurrentVersion = SelectedVersion.Clone(); } InvalidateRelayCommands(); } public void OnRemoveDancer(HardwareDancer dancer) { CurrentVersion.HardwareDancers.Remove(dancer); } public void OnRemoveMotor(HardwareMotor motor) { CurrentVersion.HardwareMotors.Remove(motor); } public void OnRemovePidControl(HardwarePidControl pidControl) { CurrentVersion.HardwarePidControls.Remove(pidControl); } public void OnRemoveWinder(HardwareWinder hardwareWinder) { CurrentVersion.HardwareWinders.Remove(hardwareWinder); } public void OnMotorDrop(HardwareMotorType motorType) { if (CheckCurrentVersionNull()) return; if (!CurrentVersion.HardwareMotors.ToList().Exists(x => x.HardwareMotorType == motorType)) { CurrentVersion.HardwareMotors.Add(new HardwareMotor() { HardwareMotorType = motorType }); } } public void OnDropDancer(HardwareDancerType dancerType) { if (CheckCurrentVersionNull()) return; if (!CurrentVersion.HardwareDancers.ToList().Exists(x => x.HardwareDancerType == dancerType)) { CurrentVersion.HardwareDancers.Add(new HardwareDancer() { HardwareDancerType = dancerType }); } } public void OnDropPidControl(HardwarePidControlType pidControlType) { if (CheckCurrentVersionNull()) return; if (!CurrentVersion.HardwarePidControls.ToList().Exists(x => x.HardwarePidControlType == pidControlType)) { CurrentVersion.HardwarePidControls.Add(new HardwarePidControl() { HardwarePidControlType = pidControlType }); } } public void OnDropWinder(HardwareWinderType hardwareWinderType) { if (CheckCurrentVersionNull()) return; if (!CurrentVersion.HardwareWinders.ToList().Exists(x => x.HardwareWinderType == hardwareWinderType)) { CurrentVersion.HardwareWinders.Add(new HardwareWinder() { HardwareWinderType = hardwareWinderType }); } } private bool CheckCurrentVersionNull() { if (CurrentVersion == null) { _notification.ShowInfo("Please select a hardware version before attempting to insert any components."); return true; } return false; } private void New() { String name = _notification.ShowTextInput("Enter hardware version name", "Name"); if (!String.IsNullOrWhiteSpace(name)) { SelectedVersion = null; CurrentVersion = new HardwareVersion(); CurrentVersion.Version = Adapter.HardwareVersions.Max(x => x.Version) + 1; CurrentVersion.Name = name; _isNew = true; InvalidateRelayCommands(); } } private async void Save() { if (CurrentVersion != null) { using (_notification.PushTaskItem("Saving hardware version...")) { HardwareVersion realVersion = null; if (_isNew) { realVersion = CurrentVersion; } else { realVersion = Adapter.HardwareVersions.SingleOrDefault(x => x.Guid == SelectedVersion.Guid); realVersion.Version = CurrentVersion.Version; realVersion.Name = CurrentVersion.Name; realVersion.HardwareDancers.ToList().ForEach(x => x.DefferedDelete(Adapter.Context)); realVersion.HardwareMotors.ToList().ForEach(x => x.DefferedDelete(Adapter.Context)); realVersion.HardwarePidControls.ToList().ForEach(x => x.DefferedDelete(Adapter.Context)); realVersion.HardwareDancers.Clear(); realVersion.HardwareMotors.Clear(); realVersion.HardwarePidControls.Clear(); foreach (var item in CurrentVersion.HardwareDancers.ToList().Select(x => x.Clone())) { item.HardwareVersionGuid = realVersion.Guid; realVersion.HardwareDancers.Add(item); } foreach (var item in CurrentVersion.HardwareMotors.ToList().Select(x => x.Clone())) { item.HardwareVersionGuid = realVersion.Guid; realVersion.HardwareMotors.Add(item); } foreach (var item in CurrentVersion.HardwarePidControls.ToList().Select(x => x.Clone())) { item.HardwareVersionGuid = realVersion.Guid; realVersion.HardwarePidControls.Add(item); } } if (_isNew) { Adapter.Context.HardwareVersions.Add(realVersion); } await realVersion.SaveAsync(Adapter.Context); SelectedVersion = Adapter.HardwareVersions.SingleOrDefault(x => x.Guid == realVersion.Guid); } } } private void Delete() { if (_notification.ShowQuestion("Are you sure you want to delete this hardware version?")) { using (_notification.PushTaskItem("Deleting hardware version...")) { SelectedVersion.DeleteAsync(Adapter.Context); SelectedVersion = null; CurrentVersion = null; } } } } }