1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Drawing.Drawing2D;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Visualization;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.EvolutionTracking.Views {
|
---|
32 | public partial class GenealogyGraphChart : ChartControl {
|
---|
33 | private IGenealogyGraph genealogyGraph;
|
---|
34 |
|
---|
35 | private const double XIncrement = 30;
|
---|
36 | private const double YIncrement = 30;
|
---|
37 | private const double Diameter = 20;
|
---|
38 |
|
---|
39 | private readonly Brush defaultBrush;
|
---|
40 | private readonly Pen defaultPen;
|
---|
41 |
|
---|
42 | private Dictionary<IGenealogyGraphNode, VisualGenealogyGraphNode> nodeMap;
|
---|
43 | private Dictionary<Tuple<VisualGenealogyGraphNode, VisualGenealogyGraphNode>, VisualGenealogyGraphArc> arcMap;
|
---|
44 |
|
---|
45 | #region chart modes
|
---|
46 | public bool SimpleLineages { get; set; }
|
---|
47 | public bool LockGenealogy { get; set; }
|
---|
48 | public bool TraceFragments { get; set; }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | private bool DrawInProgress { get; set; } // do not try to update the chart while the drawing is not finished
|
---|
52 |
|
---|
53 | public IGenealogyGraph GenealogyGraph {
|
---|
54 | get { return genealogyGraph; }
|
---|
55 | set {
|
---|
56 | if (value == null) return;
|
---|
57 | genealogyGraph = value;
|
---|
58 | Clear();
|
---|
59 | DrawGraph(XIncrement, YIncrement, Diameter);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public IGenealogyGraphNode SelectedGraphNode {
|
---|
64 | get {
|
---|
65 | return SelectedVisualNode == null ? null : SelectedVisualNode.Data;
|
---|
66 | }
|
---|
67 | set {
|
---|
68 | if (value == null || value == SelectedGraphNode)
|
---|
69 | return;
|
---|
70 | SelectedVisualNode = GetMappedNode(value);
|
---|
71 | UpdateSelectedVisualNode();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void Clear() {
|
---|
76 | nodeMap = new Dictionary<IGenealogyGraphNode, VisualGenealogyGraphNode>();
|
---|
77 | arcMap = new Dictionary<Tuple<VisualGenealogyGraphNode, VisualGenealogyGraphNode>, VisualGenealogyGraphArc>();
|
---|
78 |
|
---|
79 | Chart.Group.Clear();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public bool UpdateEnabled {
|
---|
83 | get { return Chart.UpdateEnabled; }
|
---|
84 | set { Chart.UpdateEnabled = value; }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void EnforceUpdate() {
|
---|
88 | Chart.EnforceUpdate();
|
---|
89 | }
|
---|
90 |
|
---|
91 | private Visualization.Rectangle TargetRectangle { get; set; }
|
---|
92 | protected VisualGenealogyGraphNode SelectedVisualNode { get; set; }
|
---|
93 |
|
---|
94 | public VisualGenealogyGraphNode GetMappedNode(IGenealogyGraphNode node) {
|
---|
95 | VisualGenealogyGraphNode v;
|
---|
96 | nodeMap.TryGetValue(node, out v);
|
---|
97 | return v;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public VisualGenealogyGraphArc GetMappedArc(IGenealogyGraphNode source, IGenealogyGraphNode target) {
|
---|
101 | VisualGenealogyGraphNode visualSource, visualTarget;
|
---|
102 | nodeMap.TryGetValue(source, out visualSource);
|
---|
103 | nodeMap.TryGetValue(target, out visualTarget);
|
---|
104 |
|
---|
105 | if (visualSource == null || visualTarget == null) return null;
|
---|
106 |
|
---|
107 | VisualGenealogyGraphArc arc;
|
---|
108 | arcMap.TryGetValue(new Tuple<VisualGenealogyGraphNode, VisualGenealogyGraphNode>(visualSource, visualTarget), out arc);
|
---|
109 | return arc;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public GenealogyGraphChart() {
|
---|
113 | InitializeComponent();
|
---|
114 |
|
---|
115 | defaultBrush = new SolidBrush(Color.Transparent);
|
---|
116 | defaultPen = new Pen(Color.DarkGray);
|
---|
117 | }
|
---|
118 |
|
---|
119 | protected virtual void DrawGraph(double xIncrement, double yIncrement, double diameter) {
|
---|
120 | Chart.UpdateEnabled = false;
|
---|
121 | DrawInProgress = true;
|
---|
122 |
|
---|
123 | var ranks = GenealogyGraph.Ranks.Select(t => new { Rank = t.Key, Nodes = t.Value }).OrderBy(p => p.Rank).ToList();
|
---|
124 | double x = 0;
|
---|
125 | double y = PreferredSize.Height + yIncrement + 2 * diameter;
|
---|
126 |
|
---|
127 | foreach (var rank in ranks) {
|
---|
128 | var nodes = rank.Nodes.ToList();
|
---|
129 | nodes.Sort((a, b) => b.CompareTo(a)); // sort descending by quality
|
---|
130 | var nl = Environment.NewLine;
|
---|
131 |
|
---|
132 | foreach (var node in nodes) {
|
---|
133 | var brush = new SolidBrush(node.GetColor());
|
---|
134 | var visualNode = new VisualGenealogyGraphNode(Chart, x, y, x + diameter, y + diameter, defaultPen, brush) {
|
---|
135 | Data = node,
|
---|
136 | ToolTipText = "Rank: " + node.Rank + nl +
|
---|
137 | "Quality: " + String.Format("{0:0.0000}", node.Quality) + nl +
|
---|
138 | "IsElite: " + node.IsElite + nl +
|
---|
139 | "In/Out Degree: " + node.InDegree + " " + node.OutDegree
|
---|
140 | };
|
---|
141 | Chart.Group.Add(visualNode);
|
---|
142 | nodeMap.Add(node, visualNode);
|
---|
143 |
|
---|
144 | x += xIncrement;
|
---|
145 | }
|
---|
146 | y -= yIncrement;
|
---|
147 | x = 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | // add arcs
|
---|
151 | foreach (var node in GenealogyGraph.Vertices) {
|
---|
152 | if (!node.InArcs.Any()) continue;
|
---|
153 | var visualNode = GetMappedNode(node);
|
---|
154 |
|
---|
155 | if (visualNode == null) continue;
|
---|
156 | foreach (var arc in node.InArcs) {
|
---|
157 | var parent = arc.Source;
|
---|
158 | var visualParent = GetMappedNode(parent);
|
---|
159 | if (visualParent == null) continue;
|
---|
160 | var pen = Pens.Transparent;
|
---|
161 | var visualArc = AddArc(Chart, visualParent, visualNode, pen);
|
---|
162 | visualArc.OneLayerDown(); // send it behind the visual nodes
|
---|
163 | if (!arcMap.ContainsKey(Tuple.Create(visualParent, visualNode))) {
|
---|
164 | arcMap.Add(Tuple.Create(visualParent, visualNode), visualArc);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | Chart.UpdateEnabled = true;
|
---|
170 | Chart.EnforceUpdate();
|
---|
171 |
|
---|
172 | DrawInProgress = false;
|
---|
173 | }
|
---|
174 |
|
---|
175 | public event MouseEventHandler GenealogyGraphNodeClicked;
|
---|
176 | protected void OnGenealogyGraphNodeClicked(object sender, MouseEventArgs e) {
|
---|
177 | var clicked = GenealogyGraphNodeClicked;
|
---|
178 | if (clicked != null) clicked(sender, e);
|
---|
179 | }
|
---|
180 |
|
---|
181 | public event MouseEventHandler GenealogyGraphNodeDoubleClicked;
|
---|
182 | protected void OnGenealogyGraphNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
183 | var doubleClicked = GenealogyGraphNodeDoubleClicked;
|
---|
184 | if (doubleClicked != null)
|
---|
185 | doubleClicked(sender, e);
|
---|
186 | }
|
---|
187 | #region event handlers
|
---|
188 |
|
---|
189 | protected override void pictureBox_MouseMove(object sender, MouseEventArgs e) {
|
---|
190 | if (!DrawInProgress) {
|
---|
191 | switch (e.Button) {
|
---|
192 | case MouseButtons.Left:
|
---|
193 | Chart.Mode = ChartMode.Select;
|
---|
194 | Cursor = Cursors.Default;
|
---|
195 | break;
|
---|
196 | case MouseButtons.Middle:
|
---|
197 | Chart.Mode = ChartMode.Move;
|
---|
198 | Cursor = Cursors.Hand;
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | base.pictureBox_MouseMove(sender, e);
|
---|
203 | }
|
---|
204 |
|
---|
205 | protected override void pictureBox_MouseUp(object sender, MouseEventArgs e) {
|
---|
206 | Cursor = Cursors.Default;
|
---|
207 | if (Chart.Mode == ChartMode.Move) {
|
---|
208 | Chart.Mode = ChartMode.Select;
|
---|
209 | return;
|
---|
210 | }
|
---|
211 | if (Chart.Mode != ChartMode.Select) {
|
---|
212 | base.pictureBox_MouseUp(sender, e);
|
---|
213 | return;
|
---|
214 | }
|
---|
215 | var primitive = Chart.GetAllPrimitives(e.Location).FirstOrDefault(p => p is VisualGenealogyGraphNode);
|
---|
216 | if (primitive == null) {
|
---|
217 | SelectedVisualNode = null;
|
---|
218 | return;
|
---|
219 | }
|
---|
220 | if (SelectedVisualNode == primitive) return;
|
---|
221 | SelectedVisualNode = primitive as VisualGenealogyGraphNode;
|
---|
222 | if (SelectedVisualNode == null) return;
|
---|
223 |
|
---|
224 | UpdateSelectedVisualNode(); // redraw ancestries, mark node etc.
|
---|
225 |
|
---|
226 | base.pictureBox_MouseUp(sender, e);
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void UpdateSelectedVisualNode() {
|
---|
230 | if (!LockGenealogy) {
|
---|
231 | // new node has been selected, clean up
|
---|
232 | Chart.UpdateEnabled = false;
|
---|
233 | if (ModifierKeys != Keys.Shift) {
|
---|
234 | // clear colors
|
---|
235 | ClearPrimitives();
|
---|
236 | }
|
---|
237 | // use a rectangle to highlight the currently selected genealogy graph node
|
---|
238 | var graphNode = SelectedVisualNode.Data;
|
---|
239 | var visualNode = GetMappedNode(graphNode);
|
---|
240 |
|
---|
241 | DrawLineage(visualNode, n => SimpleLineages ? n.IncomingArcs.Take(1) : n.IncomingArcs, a => a.Source);
|
---|
242 | ((SolidBrush)visualNode.Brush).Color = Color.Transparent;
|
---|
243 | DrawLineage(visualNode, n => n.OutgoingArcs, a => a.Target);
|
---|
244 | }
|
---|
245 |
|
---|
246 | MarkSelectedNode();
|
---|
247 |
|
---|
248 | // update
|
---|
249 | UpdateEnabled = true;
|
---|
250 | EnforceUpdate();
|
---|
251 |
|
---|
252 | if (SelectedVisualNode != null)
|
---|
253 | OnGenealogyGraphNodeClicked(SelectedVisualNode, null); // emit clicked event
|
---|
254 | }
|
---|
255 | #endregion
|
---|
256 |
|
---|
257 | #region drawing routines
|
---|
258 | private static void DrawLineage(VisualGenealogyGraphNode node, Func<VisualGenealogyGraphNode, IEnumerable<VisualGenealogyGraphArc>> arcSelector, Func<VisualGenealogyGraphArc, VisualGenealogyGraphNode> nodeSelector) {
|
---|
259 | var brush = (SolidBrush)node.Brush;
|
---|
260 | if (brush.Color != Color.Transparent) return; // this lineage was already drawn (avoid redrawing common ancestors)
|
---|
261 | brush.Color = node.Data.GetColor();
|
---|
262 | var arcs = arcSelector(node);
|
---|
263 | var pen = new Pen(Color.Transparent);
|
---|
264 | foreach (var arc in arcs) {
|
---|
265 | var source = arc.Source.Data;
|
---|
266 | var target = arc.Target.Data;
|
---|
267 | var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
|
---|
268 | var end = new Point((int)arc.End.X, (int)arc.End.Y);
|
---|
269 | arc.Pen = pen;
|
---|
270 | arc.Pen.Brush = new LinearGradientBrush(start, end, source.GetColor(), target.GetColor());
|
---|
271 | DrawLineage(nodeSelector(arc), arcSelector, nodeSelector);
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | void MarkSelectedNode() {
|
---|
276 | var center = SelectedVisualNode.Center;
|
---|
277 | var size = SelectedVisualNode.Size;
|
---|
278 | double x1 = center.X - size.Width / 2;
|
---|
279 | double x2 = x1 + size.Width;
|
---|
280 | double y1 = center.Y - size.Height / 2;
|
---|
281 | double y2 = y1 + size.Height;
|
---|
282 | if (TargetRectangle == null) {
|
---|
283 | TargetRectangle = new Visualization.Rectangle(Chart, x1, y1, x2, y2, new Pen(Color.Black), null);
|
---|
284 | Chart.Group.Add(TargetRectangle);
|
---|
285 | } else {
|
---|
286 | TargetRectangle.SetPosition(x1, y1, x2, y2);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | private static VisualGenealogyGraphArc AddArc(IChart chart, VisualGenealogyGraphNode source, VisualGenealogyGraphNode target, Pen pen, Brush brush = null) {
|
---|
291 | var arc = new VisualGenealogyGraphArc(chart, source, target, pen) { Brush = brush };
|
---|
292 | arc.UpdatePosition();
|
---|
293 | source.OutgoingArcs.Add(arc);
|
---|
294 | target.IncomingArcs.Add(arc);
|
---|
295 | chart.Group.Add(arc);
|
---|
296 | return arc;
|
---|
297 | }
|
---|
298 |
|
---|
299 | public void ClearArcs() {
|
---|
300 | foreach (var primitive in Chart.Group.Primitives.OfType<VisualGenealogyGraphArc>()) {
|
---|
301 | primitive.Pen = Pens.Transparent;
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | public void ClearNodes() {
|
---|
306 | foreach (var primitive in Chart.Group.Primitives.OfType<VisualGenealogyGraphNode>()) {
|
---|
307 | primitive.Brush = new SolidBrush(Color.Transparent);
|
---|
308 | primitive.Pen = new Pen(Color.LightGray);
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | public virtual void ClearPrimitives() {
|
---|
313 | foreach (var primitive in Chart.Group.Primitives) {
|
---|
314 | if (primitive is VisualGenealogyGraphArc) {
|
---|
315 | primitive.Pen = Pens.Transparent;
|
---|
316 | } else if (primitive is VisualGenealogyGraphNode) {
|
---|
317 | primitive.Brush = new SolidBrush(Color.Transparent);
|
---|
318 | primitive.Pen = new Pen(Color.DarkGray);
|
---|
319 | }
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | public void HighlightNodes(IEnumerable<IGenealogyGraphNode> nodes, bool clearPrimitives = true) {
|
---|
324 | if (clearPrimitives)
|
---|
325 | ClearPrimitives();
|
---|
326 |
|
---|
327 | foreach (var node in nodes) {
|
---|
328 | var graphNode = GetMappedNode(node);
|
---|
329 | graphNode.Brush = new SolidBrush(node.GetColor());
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | public void HighlightNodes(IEnumerable<IGenealogyGraphNode> nodes, Color color, bool clearPrimitives = true) {
|
---|
334 | if (clearPrimitives)
|
---|
335 | ClearPrimitives();
|
---|
336 |
|
---|
337 | foreach (var node in nodes.Select(GetMappedNode)) {
|
---|
338 | node.Brush = new SolidBrush(color);
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | public void HighlightAll() {
|
---|
343 | foreach (var visualNode in nodeMap.Values) {
|
---|
344 | visualNode.Brush = new SolidBrush(visualNode.Data.GetColor());
|
---|
345 | }
|
---|
346 | foreach (var arc in arcMap.Values) {
|
---|
347 | var source = arc.Source.Data;
|
---|
348 | var target = arc.Target.Data;
|
---|
349 | var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
|
---|
350 | var end = new Point((int)arc.End.X, (int)arc.End.Y);
|
---|
351 | arc.Pen.Brush = new LinearGradientBrush(start, end, source.GetColor(), target.GetColor());
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | public void HighlightArc(IGenealogyGraphNode source, IGenealogyGraphNode target) {
|
---|
356 | var arc = GetMappedArc(source, target) ??
|
---|
357 | AddArc(Chart, GetMappedNode(source), GetMappedNode(target), new Pen(Color.Transparent));
|
---|
358 | var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
|
---|
359 | var end = new Point((int)arc.End.X, (int)arc.End.Y);
|
---|
360 | arc.Pen = new Pen(Color.Transparent);
|
---|
361 | arc.Pen.Brush = new LinearGradientBrush(start, end, source.GetColor(), target.GetColor());
|
---|
362 | }
|
---|
363 | #endregion
|
---|
364 | }
|
---|
365 |
|
---|
366 | internal static class Util {
|
---|
367 | public static Color GetColor(this IGenealogyGraphNode node) {
|
---|
368 | if (double.IsNaN(node.Quality))
|
---|
369 | return ColorGradient.Colors[0];
|
---|
370 | var colorIndex = (int)Math.Round(node.Quality * ColorGradient.Colors.Count);
|
---|
371 | if (colorIndex >= ColorGradient.Colors.Count) return ColorGradient.Colors.Last();
|
---|
372 | return ColorGradient.Colors[colorIndex];
|
---|
373 | }
|
---|
374 | }
|
---|
375 | } |
---|