Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis.Views/3.3/GanttDataView.cs @ 14734

Last change on this file since 14734 was 14734, checked in by abeham, 7 years ago

#2744: Added content objects and corresponding view for visualizing Gantt data

File size: 5.7 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Drawing;
26using System.Linq;
27using HeuristicLab.Collections;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Analysis.Views {
32  [View("Gantt Data View")]
33  [Content(typeof(GanttData), IsDefaultView = true)]
34  public partial class GanttDataView : ItemView {
35
36    public new GanttData Content {
37      get { return (GanttData)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public GanttDataView() {
42      InitializeComponent();
43    }
44
45    #region De/Register Content Events
46    protected override void RegisterContentEvents() {
47      base.RegisterContentEvents();
48      Content.Rows.ItemsAdded += GanttRowsOnChanged;
49      Content.Rows.ItemsRemoved += GanttRowsOnRemoved;
50      Content.Rows.ItemsReplaced += GanttRowsOnChanged;
51      Content.Rows.CollectionReset += GanttRowsOnChanged;
52      foreach (var row in Content.Rows)
53        RegisterGanttRowEvents(row);
54    }
55
56    private void RegisterGanttRowEvents(GanttRow row) {
57      row.Items.ItemsAdded += GanttRowItemsOnChanged;
58      row.Items.ItemsReplaced += GanttRowItemsOnChanged;
59      row.Items.ItemsRemoved += GanttRowItemsOnRemoved;
60      row.Items.CollectionReset += GanttRowItemsOnChanged;
61      foreach (var item in row.Items)
62        RegisterGanttRowItemEvents(item);
63    }
64
65    private void RegisterGanttRowItemEvents(GanttItem item) {
66      item.PropertyChanged += GanttRowItemsItemOnChanged;
67    }
68
69    protected override void DeregisterContentEvents() {
70      base.DeregisterContentEvents();
71      Content.Rows.ItemsAdded -= GanttRowsOnChanged;
72      Content.Rows.ItemsRemoved -= GanttRowsOnRemoved;
73      Content.Rows.ItemsReplaced -= GanttRowsOnChanged;
74      Content.Rows.CollectionReset -= GanttRowsOnChanged;
75      foreach (var row in Content.Rows)
76        DeregisterGanttRowEvents(row);
77    }
78
79    private void DeregisterGanttRowEvents(GanttRow row) {
80      row.Items.ItemsAdded -= GanttRowItemsOnChanged;
81      row.Items.ItemsReplaced -= GanttRowItemsOnChanged;
82      row.Items.ItemsRemoved -= GanttRowItemsOnRemoved;
83      row.Items.CollectionReset -= GanttRowItemsOnChanged;
84      foreach (var item in row.Items)
85        DeregisterGanttRowItemEvents(item);
86    }
87
88    private void DeregisterGanttRowItemEvents(GanttItem item) {
89      item.PropertyChanged -= GanttRowItemsItemOnChanged;
90    }
91    #endregion
92
93    protected override void OnContentChanged() {
94      base.OnContentChanged();
95
96      UpdateVisualization();
97    }
98
99    protected override void SetEnabledStateOfControls() {
100      base.SetEnabledStateOfControls();
101      ganttChart.Enabled = !Locked && !ReadOnly && Content != null;
102    }
103
104    private void UpdateVisualization() {
105      ganttChart.Reset();
106
107      if (Content == null) return;
108
109      var colors = (KnownColor[])Enum.GetValues(typeof(KnownColor));
110
111      var categories = Content.Rows.SelectMany(x => x.Items.Select(y => y.Category ?? string.Empty)).Distinct();
112      foreach (var cat in categories) {
113        Color color;
114        if (!Content.CategoryColors.TryGetValue(cat, out color)) {
115          // if no color is defined, a random color is chosen for each category
116          var rand = new Random(cat.GetHashCode());
117          color = Color.FromKnownColor(colors[rand.Next(colors.Length)]);
118        }
119        ganttChart.AddCategory(cat, color);
120      }
121     
122      ganttChart.Hide();
123      foreach (var row in Content.Rows) {
124        foreach (var item in row.Items) {
125          ganttChart.AddData(row.Name, item.Category ?? string.Empty, item.StartDate, item.EndDate, item.ToolTip, item.ShowLabel);
126        }
127      }
128      ganttChart.Show();
129    }
130
131    #region Content Event Handlers
132    private void GanttRowsOnChanged(object sender, CollectionItemsChangedEventArgs<GanttRow> e) {
133      foreach (var row in e.Items)
134        RegisterGanttRowEvents(row);
135      foreach (var row in e.OldItems)
136        DeregisterGanttRowEvents(row);
137
138      UpdateVisualization();
139    }
140
141    private void GanttRowsOnRemoved(object sender, CollectionItemsChangedEventArgs<GanttRow> e) {
142      foreach (var row in e.Items)
143        DeregisterGanttRowEvents(row);
144
145      UpdateVisualization();
146    }
147    private void GanttRowItemsOnChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<GanttItem>> e) {
148      foreach (var item in e.Items)
149        RegisterGanttRowItemEvents(item.Value);
150      foreach (var item in e.OldItems)
151        DeregisterGanttRowItemEvents(item.Value);
152
153      UpdateVisualization();
154    }
155    private void GanttRowItemsOnRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<GanttItem>> e) {
156      foreach (var item in e.Items)
157        DeregisterGanttRowItemEvents(item.Value);
158
159      UpdateVisualization();
160    }
161    private void GanttRowItemsItemOnChanged(object sender, EventArgs e) {
162      UpdateVisualization();
163    }
164    #endregion
165  }
166}
Note: See TracBrowser for help on using the repository browser.