1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core.Networks;
|
---|
27 | using HeuristicLab.Visualization;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Networks.Views.NetworkVisualization {
|
---|
30 | [Primitive(typeof(INode), true)]
|
---|
31 | public class NodeRectangle : Visualization.Rectangle, INetworkItemPrimitive<INode> {
|
---|
32 | protected readonly Dictionary<IPort, IPrimitive> port2primitive = new Dictionary<IPort, IPrimitive>();
|
---|
33 | protected readonly IGroup portRectangles;
|
---|
34 |
|
---|
35 | protected bool IgnoreVisualPropertiesChanges { get; set; }
|
---|
36 |
|
---|
37 | protected INode networkItem;
|
---|
38 | public INode NetworkItem {
|
---|
39 | get { return networkItem; }
|
---|
40 | set {
|
---|
41 | if (networkItem == value) return;
|
---|
42 | if (networkItem != null) DeregisterNetworkItemEvents();
|
---|
43 | networkItem = value;
|
---|
44 | if (networkItem != null) RegisterNetworkItemEvents();
|
---|
45 | LoadVisualProperties();
|
---|
46 | OnNetworkItemChanged();
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public NodeRectangle(IChart chart, INode node)
|
---|
51 | : base(chart, PointD.Empty, PointD.Empty) {
|
---|
52 | NetworkItem = node;
|
---|
53 |
|
---|
54 | portRectangles = new Group(chart);
|
---|
55 | foreach (var port in node.Ports) {
|
---|
56 | var portPrimitive = PrimitiveAttribute.CreateDefaultPrimitive(port.GetType(), chart, port, node); // TODO: port.Parent != node
|
---|
57 | port2primitive.Add(port, portPrimitive);
|
---|
58 | portRectangles.Add(portPrimitive);
|
---|
59 | }
|
---|
60 | portRectangles.RedrawRequired += (sender, args) => OnRedrawRequired();
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected virtual void RegisterNetworkItemEvents() {
|
---|
64 | networkItem.NameChanged += NetworkItem_NameChanged;
|
---|
65 | if (networkItem.VisualProperties != null) {
|
---|
66 | networkItem.VisualProperties.Changed += VisualProperties_Changed;
|
---|
67 | }
|
---|
68 | if (networkItem.Ports != null) {
|
---|
69 | networkItem.Ports.ItemsAdded += Ports_ItemsAdded;
|
---|
70 | networkItem.Ports.ItemsRemoved += Ports_ItemsRemoved;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected virtual void DeregisterNetworkItemEvents() {
|
---|
75 | networkItem.NameChanged -= NetworkItem_NameChanged;
|
---|
76 | if (networkItem.VisualProperties != null) {
|
---|
77 | networkItem.VisualProperties.Changed -= VisualProperties_Changed;
|
---|
78 | }
|
---|
79 | if (networkItem.Ports != null) {
|
---|
80 | networkItem.Ports.ItemsAdded -= Ports_ItemsAdded;
|
---|
81 | networkItem.Ports.ItemsRemoved -= Ports_ItemsRemoved;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | #region Overrides
|
---|
86 | public override void SetPosition(PointD lowerLeft, PointD upperRight) {
|
---|
87 | base.SetPosition(lowerLeft, upperRight);
|
---|
88 | SaveVisualProperties();
|
---|
89 | }
|
---|
90 |
|
---|
91 | public override void Draw(Graphics graphics) {
|
---|
92 | base.Draw(graphics);
|
---|
93 |
|
---|
94 | var p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y + Size.Height));
|
---|
95 |
|
---|
96 | if (networkItem != null) {
|
---|
97 | using (var font = new Font(FontFamily.GenericSansSerif, (float)Math.Round(8.25 * Chart.WorldToPixelRatio.Width))) {
|
---|
98 | graphics.DrawString(networkItem.Name, font, Brushes.Black, p.X + 5, p.Y + 5);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | portRectangles.Draw(graphics);
|
---|
103 | }
|
---|
104 | #endregion
|
---|
105 |
|
---|
106 | public IPrimitive GetPortPrimitive(IPort name) {
|
---|
107 | IPrimitive portPrimitive;
|
---|
108 | port2primitive.TryGetValue(name, out portPrimitive);
|
---|
109 | return portPrimitive;
|
---|
110 | }
|
---|
111 |
|
---|
112 | #region Events
|
---|
113 | public event EventHandler NetworkItemChanged;
|
---|
114 | protected virtual void OnNetworkItemChanged() {
|
---|
115 | var handler = NetworkItemChanged;
|
---|
116 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
117 | }
|
---|
118 | #endregion
|
---|
119 |
|
---|
120 | #region Event Handlers
|
---|
121 | private void VisualProperties_Changed(object sender, EventArgs e) {
|
---|
122 | if (IgnoreVisualPropertiesChanges) return;
|
---|
123 |
|
---|
124 | LoadVisualProperties();
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void NetworkItem_NameChanged(object sender, EventArgs e) { OnRedrawRequired(); }
|
---|
128 |
|
---|
129 | private void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
130 | foreach (var port in e.Items) {
|
---|
131 | var portRectangle = PrimitiveAttribute.CreateDefaultPrimitive(port.GetType(), Chart, port, networkItem);
|
---|
132 | port2primitive.Add(port, portRectangle);
|
---|
133 | portRectangles.Add(portRectangle);
|
---|
134 | }
|
---|
135 |
|
---|
136 | OnRedrawRequired();
|
---|
137 | }
|
---|
138 |
|
---|
139 | private void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
140 | foreach (var port in e.Items) {
|
---|
141 | var portRectangle = port2primitive[port];
|
---|
142 | port2primitive.Remove(port);
|
---|
143 | portRectangles.Remove(portRectangle);
|
---|
144 | }
|
---|
145 |
|
---|
146 | OnRedrawRequired();
|
---|
147 | }
|
---|
148 | #endregion
|
---|
149 |
|
---|
150 | #region Helpers
|
---|
151 | private INodeVisualProperties CreateDefaultVisualProperties() {
|
---|
152 | var offset = Chart.UpperRight - Chart.LowerLeft;
|
---|
153 | var center = new PointD(Chart.LowerLeft.X + offset.DX * 0.5, Chart.LowerLeft.Y + offset.DY * 0.5);
|
---|
154 | var size = Chart.TransformPixelToWorld(new Size(200, 100));
|
---|
155 | var lowerLeft = center - new Offset(size.Width * 0.5, size.Height * 0.5);
|
---|
156 | var upperRight = center + new Offset(size.Width * 0.5, size.Height * 0.5);
|
---|
157 | return new NodeVisualProperties(new Point2D<double>(lowerLeft.X, lowerLeft.Y), new Point2D<double>(upperRight.X, upperRight.Y));
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void LoadVisualProperties() {
|
---|
161 | if (networkItem.VisualProperties == null)
|
---|
162 | networkItem.VisualProperties = CreateDefaultVisualProperties();
|
---|
163 |
|
---|
164 | var vp = (INodeVisualProperties)networkItem.VisualProperties;
|
---|
165 | SetPosition(new PointD(vp.LowerLeft.X, vp.LowerLeft.Y), new PointD(vp.UpperRight.X, vp.UpperRight.Y));
|
---|
166 | }
|
---|
167 |
|
---|
168 | private void SaveVisualProperties() {
|
---|
169 | if (networkItem == null) return;
|
---|
170 |
|
---|
171 | var vp = (INodeVisualProperties)networkItem.VisualProperties;
|
---|
172 |
|
---|
173 | IgnoreVisualPropertiesChanges = true;
|
---|
174 | try {
|
---|
175 | vp.LowerLeft = new Point2D<double>(LowerLeft.X, LowerLeft.Y);
|
---|
176 | vp.UpperRight = new Point2D<double>(UpperRight.X, UpperRight.Y);
|
---|
177 | } finally { IgnoreVisualPropertiesChanges = false; }
|
---|
178 | }
|
---|
179 | #endregion
|
---|
180 | }
|
---|
181 | }
|
---|