aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Technician/Editors/ProcessParametersElementEditor.xaml.cs
blob: 5f3cda4940c3cbf495dbf90880d3412fc68b341b (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.Editors;
using Tango.BL.Entities;
using Tango.MachineStudio.Technician.TechItems;
using Tango.Core;
using static Tango.MachineStudio.Technician.TechItems.ProcessParametersItem;
using Tango.SharedUI.Editors;

namespace Tango.MachineStudio.Technician.Editors
{
    [ContentProperty("InnerContent")]
    public partial class ProcessParametersElementEditor : ElementEditor
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ProcessParametersElementEditor"/> class.
        /// </summary>
        public ProcessParametersElementEditor()
            : base()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProcessParametersElementEditor"/> class.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        public ProcessParametersElementEditor(ProcessParametersItem processParametersItem)
            : this()
        {
            ProcessParametersItem = processParametersItem;
            DataContext = ProcessParametersItem;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ProcessParametersElementEditor"/> class.
        /// </summary>
        /// <param name="frameworkElement">The framework element.</param>
        /// <param name="bounds">The bounds.</param>
        public ProcessParametersElementEditor(ProcessParametersItem processParametersItem, Rect bounds)
            : this(processParametersItem)
        {
            Left = bounds.Left;
            Top = bounds.Top;
            Width = bounds.Width;
            Height = bounds.Height;
        }

        private ProcessParametersItem _processParametersItem;

        public ProcessParametersItem ProcessParametersItem
        {
            get { return _processParametersItem; }
            set { _processParametersItem = value; RaisePropertyChanged(nameof(ProcessParametersItem)); }
        }


        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns></returns>
        public override IElementEditor Clone()
        {
            try
            {
                var clonedItem = ProcessParametersItem.Clone() as ProcessParametersItem;
                ProcessParametersElementEditor cloned = new ProcessParametersElementEditor(clonedItem);
                cloned.Top = Top;
                cloned.Left = Left;
                cloned.Width = Width;
                cloned.Height = Height;
                cloned.Angle = Angle;
                return cloned;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Could not clone this editor. You may have to create a custom editor and implement a custom Clone method.", ex);
            }
        }

        /// <summary>
        /// Gets the hosted element.
        /// </summary>
        [ParameterIgnore]
        public override Object HostedElement
        {
            get { return ProcessParametersItem; }
        }

        private void OnProcessParametersDropped(object sender, DragAndDrop.DropEventArgs e)
        {
            ParameterItem draggedItem = e.Draggable.DataContext as ParameterItem;
            ParameterItem droppedItem = e.Droppable.DataContext as ParameterItem;

            if (draggedItem != null && droppedItem != null && draggedItem.ParameterizedObject == droppedItem.ParameterizedObject)
            {
                var parameters = draggedItem.ParameterizedObject.Parameters;

                var draggedSettingItem = ProcessParametersItem.ParametersIndices.FirstOrDefault(x => x.Name == draggedItem.Name);
                var droppedSettingItem = ProcessParametersItem.ParametersIndices.FirstOrDefault(x => x.Name == droppedItem.Name);

                if (draggedSettingItem != null && droppedSettingItem != null)
                {
                    if (draggedSettingItem.Index > droppedSettingItem.Index)
                    {
                        draggedSettingItem.Index = droppedSettingItem.Index - 1;
                    }
                    else
                    {
                        draggedSettingItem.Index = droppedSettingItem.Index + 1;
                    }

                    int index = 1;

                    foreach (var settingItem in ProcessParametersItem.ParametersIndices.OrderBy(x => x.Index))
                    {
                        settingItem.Index = index++;
                    }
                }
            }

            var editor = e.Draggable.FindAncestor<ParameterizedEditor>();

            if (editor != null)
            {
                editor.ParameterizedObject = null;
                editor.ParameterizedObject = draggedItem.ParameterizedObject;
            }
        }

        private void ParameterizedEditor_GeneratingItems(object sender, SharedUI.Editors.ParameterizedEditor.GeneratingItemsEventArgs e)
        {
            List<ParameterItem> items = e.Result.ToList();

            if (ProcessParametersItem.ParametersIndices.Count > 0)
            {
                items.Clear();

                foreach (var item in ProcessParametersItem.ParametersIndices.OrderBy(x => x.Index))
                {
                    var p = e.Source.SingleOrDefault(x => x.Name == item.Name);

                    if (p != null)
                    {
                        items.Add(p);
                    }
                }
            }
            else
            {
                ProcessParametersTable p = new ProcessParametersTable();
                var pp = p.CreateParametersCollection(Core.ParameterItemMode.Binding);

                foreach (var item in pp)
                {
                    ProcessParametersItem.ParametersIndices.Add(new ParameterIndex()
                    {
                        Index = pp.IndexOf(item),
                        Name = item.Name
                    });
                }
            }

            e.Result = items;
        }
    }
}