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 | this.SuspendRendering();
|
---|
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 | Chart.OneLayerDown(visualArc); // 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 | ResumeRendering();
|
---|
170 | DrawInProgress = false;
|
---|
171 | }
|
---|
172 |
|
---|
173 | public event MouseEventHandler GenealogyGraphNodeClicked;
|
---|
174 | protected void OnGenealogyGraphNodeClicked(object sender, MouseEventArgs e) {
|
---|
175 | var clicked = GenealogyGraphNodeClicked;
|
---|
176 | if (clicked != null) clicked(sender, e);
|
---|
177 | }
|
---|
178 |
|
---|
179 | public event MouseEventHandler GenealogyGraphNodeDoubleClicked;
|
---|
180 | protected void OnGenealogyGraphNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
181 | var doubleClicked = GenealogyGraphNodeDoubleClicked;
|
---|
182 | if (doubleClicked != null)
|
---|
183 | doubleClicked(sender, e);
|
---|
184 | }
|
---|
185 | #region event handlers
|
---|
186 |
|
---|
187 | protected override void PictureBoxOnMouseMove(object sender, MouseEventArgs e) {
|
---|
188 | if (!DrawInProgress) {
|
---|
189 | switch (e.Button) {
|
---|
190 | case MouseButtons.Left:
|
---|
191 | Mode = ChartMode.Select;
|
---|
192 | Cursor = Cursors.Default;
|
---|
193 | break;
|
---|
194 | case MouseButtons.Middle:
|
---|
195 | Mode = ChartMode.Move;
|
---|
196 | Cursor = Cursors.Hand;
|
---|
197 | break;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | base.PictureBoxOnMouseMove(sender, e);
|
---|
201 | }
|
---|
202 |
|
---|
203 | protected override void PictureBoxOnMouseUp(object sender, MouseEventArgs e) {
|
---|
204 | Cursor = Cursors.Default;
|
---|
205 | if (Mode == ChartMode.Move) {
|
---|
206 | Mode = ChartMode.Select;
|
---|
207 | return;
|
---|
208 | }
|
---|
209 | if (Mode != ChartMode.Select) {
|
---|
210 | base.PictureBoxOnMouseUp(sender, e);
|
---|
211 | return;
|
---|
212 | }
|
---|
213 | var primitive = Chart.GetAllPrimitives(e.Location).FirstOrDefault(p => p is VisualGenealogyGraphNode);
|
---|
214 | if (primitive == null) {
|
---|
215 | SelectedVisualNode = null;
|
---|
216 | return;
|
---|
217 | }
|
---|
218 | if (SelectedVisualNode == primitive) return;
|
---|
219 | SelectedVisualNode = primitive as VisualGenealogyGraphNode;
|
---|
220 | if (SelectedVisualNode == null) return;
|
---|
221 |
|
---|
222 | UpdateSelectedVisualNode(); // redraw ancestries, mark node etc.
|
---|
223 |
|
---|
224 | base.PictureBoxOnMouseUp(sender, e);
|
---|
225 | }
|
---|
226 |
|
---|
227 | private void UpdateSelectedVisualNode() {
|
---|
228 | if (!LockGenealogy) {
|
---|
229 | // new node has been selected, clean up
|
---|
230 | SuspendRendering();
|
---|
231 | if (ModifierKeys != Keys.Shift) {
|
---|
232 | // clear colors
|
---|
233 | ClearPrimitives();
|
---|
234 | }
|
---|
235 | // use a rectangle to highlight the currently selected genealogy graph node
|
---|
236 | var graphNode = SelectedVisualNode.Data;
|
---|
237 | var visualNode = GetMappedNode(graphNode);
|
---|
238 |
|
---|
239 | DrawLineage(visualNode, n => SimpleLineages ? n.IncomingArcs.Take(1) : n.IncomingArcs, a => a.Source);
|
---|
240 | ((SolidBrush)visualNode.Brush).Color = Color.Transparent;
|
---|
241 | DrawLineage(visualNode, n => n.OutgoingArcs, a => a.Target);
|
---|
242 | }
|
---|
243 |
|
---|
244 | MarkSelectedNode();
|
---|
245 |
|
---|
246 | // update
|
---|
247 | ResumeRendering();
|
---|
248 |
|
---|
249 | if (SelectedVisualNode != null)
|
---|
250 | OnGenealogyGraphNodeClicked(SelectedVisualNode, null); // emit clicked event
|
---|
251 | }
|
---|
252 | #endregion
|
---|
253 |
|
---|
254 | #region drawing routines
|
---|
255 | private static void DrawLineage(VisualGenealogyGraphNode node, Func<VisualGenealogyGraphNode, IEnumerable<VisualGenealogyGraphArc>> arcSelector, Func<VisualGenealogyGraphArc, VisualGenealogyGraphNode> nodeSelector) {
|
---|
256 | var brush = (SolidBrush)node.Brush;
|
---|
257 | if (brush.Color != Color.Transparent) return; // this lineage was already drawn (avoid redrawing common ancestors)
|
---|
258 | brush.Color = node.Data.GetColor();
|
---|
259 | var arcs = arcSelector(node);
|
---|
260 | var pen = new Pen(Color.Transparent);
|
---|
261 | foreach (var arc in arcs) {
|
---|
262 | var source = arc.Source.Data;
|
---|
263 | var target = arc.Target.Data;
|
---|
264 | var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
|
---|
265 | var end = new Point((int)arc.End.X, (int)arc.End.Y);
|
---|
266 | arc.Pen = pen;
|
---|
267 | arc.Pen.Brush = new LinearGradientBrush(start, end, source.GetColor(), target.GetColor());
|
---|
268 | DrawLineage(nodeSelector(arc), arcSelector, nodeSelector);
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | void MarkSelectedNode() {
|
---|
273 | var center = SelectedVisualNode.Center;
|
---|
274 | var size = SelectedVisualNode.Size;
|
---|
275 | double x1 = center.X - size.Width / 2;
|
---|
276 | double x2 = x1 + size.Width;
|
---|
277 | double y1 = center.Y - size.Height / 2;
|
---|
278 | double y2 = y1 + size.Height;
|
---|
279 | if (TargetRectangle == null) {
|
---|
280 | var lowerLeft = new PointD(x1, y1);
|
---|
281 | var upperRight = new PointD(x2, y2);
|
---|
282 | TargetRectangle = new Visualization.Rectangle(Chart, lowerLeft, upperRight, new Pen(Color.Black), null);
|
---|
283 | Chart.Group.Add(TargetRectangle);
|
---|
284 | } else {
|
---|
285 | var lowerLeft = new PointD(x1, y1);
|
---|
286 | var upperRight = new PointD(x2, y2);
|
---|
287 | TargetRectangle.SetPosition(lowerLeft, upperRight);
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | private static VisualGenealogyGraphArc AddArc(IChart chart, VisualGenealogyGraphNode source, VisualGenealogyGraphNode target, Pen pen, Brush brush = null) {
|
---|
292 | var arc = new VisualGenealogyGraphArc(chart, source, target, pen) { Brush = brush };
|
---|
293 | arc.UpdatePosition();
|
---|
294 | source.OutgoingArcs.Add(arc);
|
---|
295 | target.IncomingArcs.Add(arc);
|
---|
296 | chart.Group.Add(arc);
|
---|
297 | return arc;
|
---|
298 | }
|
---|
299 |
|
---|
300 | public void ClearArcs() {
|
---|
301 | foreach (var primitive in Chart.Group.Primitives.OfType<VisualGenealogyGraphArc>()) {
|
---|
302 | primitive.Pen = Pens.Transparent;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | public void ClearNodes() {
|
---|
307 | foreach (var primitive in Chart.Group.Primitives.OfType<VisualGenealogyGraphNode>()) {
|
---|
308 | primitive.Brush = new SolidBrush(Color.Transparent);
|
---|
309 | primitive.Pen = new Pen(Color.LightGray);
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | public virtual void ClearPrimitives() {
|
---|
314 | foreach (var primitive in Chart.Group.Primitives) {
|
---|
315 | if (primitive is VisualGenealogyGraphArc) {
|
---|
316 | primitive.Pen = Pens.Transparent;
|
---|
317 | } else if (primitive is VisualGenealogyGraphNode) {
|
---|
318 | primitive.Brush = new SolidBrush(Color.Transparent);
|
---|
319 | primitive.Pen = new Pen(Color.DarkGray);
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | public void HighlightNodes(IEnumerable<IGenealogyGraphNode> nodes, bool clearPrimitives = true) {
|
---|
325 | if (clearPrimitives)
|
---|
326 | ClearPrimitives();
|
---|
327 |
|
---|
328 | foreach (var node in nodes) {
|
---|
329 | var graphNode = GetMappedNode(node);
|
---|
330 | graphNode.Brush = new SolidBrush(node.GetColor());
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | public void HighlightNodes(IEnumerable<IGenealogyGraphNode> nodes, Color color, bool clearPrimitives = true) {
|
---|
335 | if (clearPrimitives)
|
---|
336 | ClearPrimitives();
|
---|
337 |
|
---|
338 | foreach (var node in nodes.Select(GetMappedNode)) {
|
---|
339 | node.Brush = new SolidBrush(color);
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | public void HighlightAll() {
|
---|
344 | foreach (var visualNode in nodeMap.Values) {
|
---|
345 | visualNode.Brush = new SolidBrush(visualNode.Data.GetColor());
|
---|
346 | }
|
---|
347 | foreach (var arc in arcMap.Values) {
|
---|
348 | var source = arc.Source.Data;
|
---|
349 | var target = arc.Target.Data;
|
---|
350 | var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
|
---|
351 | var end = new Point((int)arc.End.X, (int)arc.End.Y);
|
---|
352 | arc.Pen.Brush = new LinearGradientBrush(start, end, source.GetColor(), target.GetColor());
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | public void HighlightArc(IGenealogyGraphNode source, IGenealogyGraphNode target) {
|
---|
357 | var arc = GetMappedArc(source, target) ??
|
---|
358 | AddArc(Chart, GetMappedNode(source), GetMappedNode(target), new Pen(Color.Transparent));
|
---|
359 | var start = new Point((int)arc.Start.X, (int)arc.Start.Y);
|
---|
360 | var end = new Point((int)arc.End.X, (int)arc.End.Y);
|
---|
361 | arc.Pen = new Pen(Color.Transparent);
|
---|
362 | arc.Pen.Brush = new LinearGradientBrush(start, end, source.GetColor(), target.GetColor());
|
---|
363 | }
|
---|
364 | #endregion
|
---|
365 | }
|
---|
366 |
|
---|
367 | internal static class Util {
|
---|
368 | public static Color GetColor(this IGenealogyGraphNode node) {
|
---|
369 | if (double.IsNaN(node.Quality))
|
---|
370 | return ColorGradient.Colors[0];
|
---|
371 | var colorIndex = (int)Math.Round(node.Quality * ColorGradient.Colors.Count);
|
---|
372 | if (colorIndex >= ColorGradient.Colors.Count) return ColorGradient.Colors.Last();
|
---|
373 | return ColorGradient.Colors[colorIndex];
|
---|
374 | }
|
---|
375 | }
|
---|
376 | } |
---|