aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Printing/Job.cs
blob: 9ffff33728966355e5ae0149a1014aa469783f9b (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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Integration.Observables
{
    /// <summary>
    /// Extends the standard observable Job class.
    /// </summary>
    /// <seealso cref="Tango.Integration.Observables.ObservableEntity{Tango.Integration.Observables.Job}" />
    public partial class Job
    {
        #region Events

        /// <summary>
        /// Occurs when the job total segments length has changed.
        /// </summary>
        public event EventHandler LengthChanged;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the total job segments length.
        /// </summary>
        [NotMapped]
        public double Length
        {
            get { return Segments.Sum(x => x.Length) + (EnableInterSegment ? (InterSegmentLength * (Segments.Count > 0 ? Segments.Count - 1 : Segments.Count)) : 0); }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the CollectionChanged event of the Segments collection.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
        private void Segments_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            foreach (var segment in Segments)
            {
                segment.PropertyChanged += Segment_PropertyChanged;
            }

            OnLengthChanged();
        }

        /// <summary>
        /// Handles the PropertyChanged event of all job segments.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        private void Segment_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Segment.Length))
            {
                OnLengthChanged();
            }
        }

        #endregion

        #region Virtual Methods

        /// <summary>
        /// Called when the <see cref="Length"/> property has been changed
        /// </summary>
        protected virtual void OnLengthChanged()
        {
            RaisePropertyChanged(nameof(Length));
            LengthChanged?.Invoke(this, new EventArgs());
        }

        #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 (propName == nameof(Segments))
            {
                if (Segments != null)
                {
                    Segments.CollectionChanged -= Segments_CollectionChanged;
                    Segments.CollectionChanged += Segments_CollectionChanged;

                    OnLengthChanged();
                }
            }

            if (propName == nameof(InterSegmentLength) || propName == nameof(EnableInterSegment))
            {
                OnLengthChanged();
            }
        }

        #endregion
    }
}