aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Printing/BrushStop.cs
blob: f5b10e669d4bea4b6be1c8d152c7ebd4bdf8ce63 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using ColorMine.ColorSpaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Tango.Core;
using Tango.Integration.Printing;

namespace Tango.Integration.Observables
{
    /// <summary>
    /// Extends the standard observable BrushStop class.
    /// </summary>
    /// <seealso cref="Tango.Integration.Observables.ObservableEntity{Tango.Integration.Observables.BrushStop}" />
    public partial class BrushStop
    {
        [NotMapped]
        private bool _ignorePropChanged;

        #region Properties

        private ObservableCollection<LiquidVolume> _liquidVolumes;
        /// <summary>
        /// Gets or sets the collection of this brush stop liquid volumes.
        /// </summary>
        [NotMapped]
        public ObservableCollection<LiquidVolume> LiquidVolumes
        {
            get { return _liquidVolumes; }
            set { _liquidVolumes = value; RaisePropertyChangedAuto(); }
        }

        private Color _color;
        /// <summary>
        /// Gets or sets the brush stop color.
        /// </summary>
        [NotMapped]
        public Color Color
        {
            get { return _color; }
            set
            {
                _color = value;
                _red = _color.R;
                _green = _color.G;
                _blue = _color.B;
                RaisePropertyChanged(nameof(Color));
            }
        }

        /// <summary>
        /// Gets the brush stop index within it's segment brush stops collection.
        /// </summary>
        [NotMapped]
        public int Index
        {
            get { return Segment.BrushStops.IndexOf(this); }
        }

        /// <summary>
        /// Gets a value indicating whether this brush stop is the first one within its segment brush stops.
        /// </summary>
        [NotMapped]
        public bool IsFirst
        {
            get { return Segment.BrushStops.IndexOf(this) == 0; }
        }

        /// <summary>
        /// Gets a value indicating whether this brush stop is the last one within its segment brush stops.
        /// </summary>
        [NotMapped]
        public bool IsLast
        {
            get { return Segment.BrushStops.IndexOf(this) == Segment.BrushStops.Count - 1; }
        }

        /// <summary>
        /// Gets a value indicating whether this brush stop is not the first nor last within its segment brush stops.
        /// </summary>
        [NotMapped]
        public bool IsMiddle
        {
            get { return !IsFirst && !IsLast; }
        }

        /// <summary>
        /// Gets this brush stop offset in meters.
        /// </summary>
        [NotMapped]
        public double OffsetMeters
        {
            get
            {
                if (Segment != null)
                {
                    return Segment.Length * (OffsetPercent / 100d);
                }
                else
                {
                    return 0;
                }
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Notifies about the offset percentage and offset meters changes.
        /// </summary>
        public void RaiseOffsetChanged()
        {
            RaisePropertyChanged(nameof(OffsetPercent));
            RaisePropertyChanged(nameof(OffsetMeters));
            RaisePropertyChanged(nameof(IsFirst));
            RaisePropertyChanged(nameof(IsLast));
        }

        /// <summary>
        /// Sets this brush stop liquid volumes.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="rml">The RML.</param>
        /// <param name="processParametersTable">The process parameters table.</param>
        public void SetLiquidVolumes(Configuration configuration, Rml rml, ProcessParametersTable processParametersTable)
        {
            LiquidVolumes = new ObservableCollection<LiquidVolume>();

            foreach (var idsPack in configuration.IdsPacks)
            {
                LiquidVolumes.Add(new LiquidVolume(configuration, idsPack, rml, processParametersTable, this));
            }
        }

        #endregion

        #region Override Methods

        /// <summary>
        /// Raises the property changed event.
        /// </summary>
        /// <param name="propName">Name of the property.</param>
        protected override void RaisePropertyChanged(string propName)
        {
            base.RaisePropertyChanged(propName);

            if (!_ignorePropChanged && propName != nameof(ColorSpace) && ColorSpace != null)
            {

                SynchronizeColorSpaces();

                _ignorePropChanged = true;

                foreach (var prop in typeof(BrushStop).GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    RaisePropertyChanged(prop.Name);
                }

                _ignorePropChanged = false;
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Synchronizes between the different brush stop color spaces.
        /// </summary>
        private void SynchronizeColorSpaces()
        {
            Rgb rgb = new Rgb(Red, Green, Blue);
            Cmyk cmyk = new Cmyk(Cyan, Magenta, Yellow, Black);
            Lab lab = new Lab(L, A, B);

            switch ((ColorSpaces)ColorSpace.Code)
            {
                case ColorSpaces.RGB:
                    cmyk = rgb.To<Cmyk>();
                    lab = rgb.To<Lab>();
                    break;
                case ColorSpaces.CMYK:
                    rgb = cmyk.To<Rgb>();
                    lab = cmyk.To<Lab>();
                    break;
                case ColorSpaces.LAB:
                    rgb = lab.To<Rgb>();
                    cmyk = lab.To<Cmyk>();
                    break;
            }

            _red = (int)rgb.R;
            _green = (int)rgb.G;
            _blue = (int)rgb.B;

            _cyan = cmyk.C * 100d;
            _magenta = cmyk.M * 100d;
            _yellow = cmyk.Y * 100d;
            _black = cmyk.K * 100d;

            _l = lab.L;
            _a = lab.A;
            _b = lab.B;

            _color = Color.FromRgb((byte)_red, (byte)_green, (byte)_blue);
        }

        #endregion
    }
}