Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost.Views/3.3/BioBoostSummaryView.cs @ 13073

Last change on this file since 13073 was 13072, checked in by gkronber, 9 years ago

#2499: added code from HeuristicLab.BioBoost.Views (from private repository) nothing much has been changed

File size: 6.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Globalization;
7using System.Linq;
8using System.Text;
9using System.Threading.Tasks;
10using System.Windows.Forms;
11using HeuristicLab.BioBoost.Representation;
12using HeuristicLab.Core.Views;
13using HeuristicLab.Data;
14using HeuristicLab.MainForm;
15
16namespace HeuristicLab.BioBoost.Views {
17
18  [Content(typeof(RegionDetailData), IsDefaultView = false)]
19  public partial class BioBoostSummaryView : ItemView {
20
21    public new RegionDetailData Content {
22      get { return (RegionDetailData)base.Content; }
23      set { base.Content = value; }
24    }
25
26    public BioBoostSummaryView() {
27      InitializeComponent();
28    }
29
30    protected override void DeregisterContentEvents() {
31      base.DeregisterContentEvents();
32    }
33
34    protected override void RegisterContentEvents() {
35      base.RegisterContentEvents();
36    }
37
38    protected override void SetEnabledStateOfControls() {
39      base.SetEnabledStateOfControls();
40    }
41
42    protected override void OnContentChanged() {
43      base.OnContentChanged();
44      if (Content == null) {
45        Clear();
46      } else {
47        Clear();
48        Populate();
49      }
50    }
51
52    protected void Clear() {
53      treeView.Nodes.Clear();
54      gridView.Rows.Clear();
55      regionLabel.Text = "no region selected";
56      valueLabel.Text = "no value selected";
57    }
58
59    protected void Populate() {
60      foreach (var targets in Content.Solution.TransportTargets) {
61        var node = treeView.Nodes.Add(targets.Key);
62        node.Tag = node.Text;
63        for (int i = 0; i < targets.Value.Length; i++) {
64          if (targets.Value[i] != null && targets.Value[i].Count > 0) {
65            var innerNode = node.Nodes.Add(Content.Solution.LocationNames[i]);
66            innerNode.Tag = innerNode.Text;
67            foreach (var suppliers in targets.Value[i]) {
68              var supplierNode = innerNode.Nodes.Add(Content.Solution.LocationNames[suppliers.Value]);
69              supplierNode.Tag = supplierNode.Text;
70            }
71          }
72        }
73      }
74      valueLabel.Text = "no value selected";
75      regionLabel.Text = Content.RegionName ?? "no region selected";
76    }
77
78    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
79      gridView.Rows.Clear();
80      if (treeView.SelectedNode != null) {
81        var i = GetLocationIndex((string)treeView.SelectedNode.Tag);
82        if (i >= 0) {
83          PopulateGridView(i);
84        }
85      }
86    }
87
88    private int GetLocationIndex(string regionName) {
89      return Content.Solution.LocationNames.IndexOfFirst(x => x == regionName);
90    }
91
92    private double? GetValue(string regionName, string valueName) {
93      if (valueName == null) return null;
94      var i = GetLocationIndex(regionName);
95      if (i < 0) return 0;
96      DoubleArray doubles;
97      IntArray ints;
98      if (Content.Solution.DoubleValues.TryGetValue(valueName, out doubles)) {
99        return doubles[i];
100      } else if (Content.Solution.IntValues.TryGetValue(valueName, out ints)) {
101        return ints[i];
102      }
103      return null;
104    }
105
106    private void PopulateGridView(int index) {
107      regionLabel.Text = Content.Solution.LocationNames[index];
108      foreach (var kvp in Content.Solution.DoubleValues.Where(kvp => kvp.Value[index] != 0)) {
109        gridView.Rows.Add(kvp.Key, kvp.Value[index]);
110      }
111      foreach (var kvp in Content.Solution.IntValues.Where(kvp => kvp.Value[index] != 0)) {
112        gridView.Rows.Add(kvp.Key, kvp.Value[index]);
113      }
114      foreach (var kvp in Content.Solution.StringValues.Where(kvp => !string.IsNullOrWhiteSpace(kvp.Value[index]))) {
115        gridView.Rows.Add(kvp.Key, kvp.Value[index]);
116      }
117      foreach (var kvp in from kvp
118                            in Content.Solution.TransportTargets
119                          where kvp.Value[index] != null
120                          let s = string.Join(", ", kvp.Value[index])
121                          where !string.IsNullOrWhiteSpace(s) select kvp) {
122        gridView.Rows.Add(kvp.Key, string.Join(", ", kvp.Value[index]));
123      }
124      gridView.Sort(NameColumn, ListSortDirection.Ascending);
125    }
126
127    private void gridView_CurrentCellChanged(object sender, EventArgs e) {
128      string valueName = null;
129      if (gridView.SelectedRows.Count > 0) {
130        valueName = (string) gridView.SelectedRows[0].Cells[0].Value;
131      }
132      foreach (TreeNode rootNodes in treeView.Nodes) {
133        UpdateAggregatedValues(rootNodes, valueName);
134      }
135    }
136
137    private static string F(double value, int significantDigits = 6) {
138      int msd = (int)Math.Ceiling(Math.Log10(value));
139      int decimals = 6 - msd;
140      if (decimals <= 0) {
141        return value.ToString("### ### ### ### ### ### ### ### ### ### ### ### ###", CultureInfo.InvariantCulture);
142      } else if (decimals > significantDigits) {
143        return value.ToString("g2");
144      } else {
145        StringBuilder sb = new StringBuilder();
146        for (int i = 0; i < msd; i+=3) {
147          if (sb.Length > 0) sb.Append(" ");
148          sb.Append("###");
149        }
150        sb.Append(".");
151        for (int i = 0; i < decimals; i += 3) {
152          if (sb[sb.Length-1] != '.') sb.Append(" ");
153          sb.Append("###");
154        }
155        return value.ToString(sb.ToString(), CultureInfo.InvariantCulture);
156      }
157    }
158
159    private double UpdateAggregatedValues(TreeNode node, string valueName) {
160      valueLabel.Text = valueName;
161      if (node.Nodes.Count == 0) {
162        var value = GetValue((string) node.Tag, valueName);
163        if (value.HasValue) {
164          node.Text = string.Format("{0} ({1})", node.Tag, F(value.Value));
165          return value.Value;
166        }
167        return 0;
168      }
169      double sum = 0;
170      int count = 0;
171      foreach (TreeNode subnode in node.Nodes) {
172        var subvalue = UpdateAggregatedValues(subnode, valueName);
173        sum += subvalue;
174        count++;
175      }
176      if (sum != 0) {
177        node.Text = string.Format("{0}   ({1} total,   {2} avg,    {3} count)", node.Tag, F(sum), F(sum/count), F(count));
178      } else {
179        var value = GetValue((string) node.Tag, valueName);
180        if (value.HasValue) {
181          node.Text = string.Format("{0} ({1})", node.Tag, F(value.Value));
182          return value.Value;
183        }
184        node.Text = (string) node.Tag;
185      }
186      return sum;
187    }
188
189  }
190}
Note: See TracBrowser for help on using the repository browser.