Changeset 13135 for branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization/3.3/Primitives
- Timestamp:
- 11/11/15 13:53:10 (9 years ago)
- Location:
- branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization/3.3/Primitives
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization/3.3/Primitives/ConnectionLine.cs
r13077 r13135 59 59 protected virtual void RegisterPortRectangleEvents(PortRectangle portRectangle) { 60 60 portRectangle.RedrawRequired += PortRectangle_RedrawRequired; 61 portRectangle.NetworkItemChanged += PortRectangleNetworkItemChanged;62 if (portRectangle.NetworkItem != null) {63 var port = portRectangle.NetworkItem;64 port.NameChanged += Port_NameChanged;65 }66 61 } 67 62 68 63 protected virtual void DeregisterPortRectangleEvents(PortRectangle portRectangle) { 69 64 portRectangle.RedrawRequired -= PortRectangle_RedrawRequired; 70 portRectangle.NetworkItemChanged -= PortRectangleNetworkItemChanged;71 if (portRectangle.NetworkItem != null) {72 var port = portRectangle.NetworkItem;73 port.NameChanged -= Port_NameChanged;74 }75 65 } 76 66 … … 105 95 } 106 96 } 107 108 97 #endregion 109 98 … … 111 100 public event EventHandler StartPortRectangleChanged; 112 101 protected virtual void OnStartPortRectangleChanged() { 113 UpdateToolTip();114 102 var handler = StartPortRectangleChanged; 115 103 if (handler != null) handler(this, EventArgs.Empty); … … 118 106 public event EventHandler EndPortRectangleChanged; 119 107 protected virtual void OnEndPortRectangleChanged() { 120 UpdateToolTip();121 108 var handler = EndPortRectangleChanged; 122 109 if (handler != null) handler(this, EventArgs.Empty); … … 128 115 SetPosition(startPortRectangle.Point, endPortRectangle.Point); 129 116 } 130 131 private void PortRectangleNetworkItemChanged(object sender, EventArgs e) {132 UpdateToolTip();133 }134 135 private void Port_NameChanged(object sender, EventArgs e) {136 UpdateToolTip();137 }138 #endregion139 140 #region Helpers141 private void UpdateToolTip() {142 string startText = "null", endText = "null";143 144 if (startPortRectangle != null) {145 var startPort = startPortRectangle.NetworkItem;146 if (startPort != null) {147 startText = startPort.Name;148 var startNode = startPort.Parent;149 startText = (startNode != null ? startNode.Name : "null") + "." + startText;150 }151 }152 153 if (endPortRectangle != null) {154 var endPort = endPortRectangle.NetworkItem;155 if (endPort != null) {156 endText = endPort.Name;157 var endNode = endPort.Parent;158 endText = (endNode != null ? endNode.Name : "null") + "." + endText;159 }160 }161 162 ToolTipText = string.Format("[{0}] --> [{1}]", startText, endText);163 }164 117 #endregion 165 118 } -
branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization/3.3/Primitives/NodeRectangle.cs
r13077 r13135 23 23 using System.Collections.Generic; 24 24 using System.Drawing; 25 using System.Linq;25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core.Networks; 27 27 using HeuristicLab.Visualization; 28 28 29 29 namespace HeuristicLab.Networks.Views.NetworkVisualization { 30 public class NodeRectangle : Visualization.Rectangle, INetworkItemPrimitive<IVisualizableNode> {31 private readonly Dictionary<IPort, PortRectangle> port2PortRectangle = new Dictionary<IPort, PortRectangle>();32 33 protected IGroup portRectangles;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 34 35 35 protected bool IgnoreVisualPropertiesChanges { get; set; } 36 36 37 protected I VisualizableNode networkItem;38 public I VisualizableNode NetworkItem {37 protected INode networkItem; 38 public INode NetworkItem { 39 39 get { return networkItem; } 40 40 set { … … 43 43 networkItem = value; 44 44 if (networkItem != null) RegisterNetworkItemEvents(); 45 UpdateToolTip();46 45 LoadVisualProperties(); 47 46 OnNetworkItemChanged(); … … 49 48 } 50 49 51 public NodeRectangle(IChart chart, I VisualizableNode node)52 : base(chart, Point .Empty, Point.Empty) {50 public NodeRectangle(IChart chart, INode node) 51 : base(chart, PointD.Empty, PointD.Empty) { 53 52 NetworkItem = node; 54 53 55 54 portRectangles = new Group(chart); 56 55 foreach (var port in node.Ports) { 57 var port Rectangle = new PortRectangle(this, port);58 port2 PortRectangle.Add(port, portRectangle);59 portRectangles.Add(port Rectangle);56 var portPrimitive = PrimitiveAttribute.CreateDefaultPrimitive(port.GetType(), chart, port, node); // TODO: port.Parent != node 57 port2primitive.Add(port, portPrimitive); 58 portRectangles.Add(portPrimitive); 60 59 } 61 UpdatePortRectangles();62 60 portRectangles.RedrawRequired += (sender, args) => OnRedrawRequired(); 63 61 } 64 62 65 63 protected virtual void RegisterNetworkItemEvents() { 66 networkItem.NameChanged += NetworkItem NameChanged;64 networkItem.NameChanged += NetworkItem_NameChanged; 67 65 if (networkItem.VisualProperties != null) { 68 66 networkItem.VisualProperties.Changed += VisualProperties_Changed; … … 75 73 76 74 protected virtual void DeregisterNetworkItemEvents() { 77 networkItem.NameChanged -= NetworkItem NameChanged;75 networkItem.NameChanged -= NetworkItem_NameChanged; 78 76 if (networkItem.VisualProperties != null) { 79 77 networkItem.VisualProperties.Changed -= VisualProperties_Changed; … … 87 85 #region Overrides 88 86 public override void SetPosition(PointD lowerLeft, PointD upperRight) { 89 SuppressEvents = true; 90 try { 91 base.SetPosition(lowerLeft, upperRight); 92 SaveVisualProperties(); 93 UpdatePortRectangles(); 94 } finally { 95 SuppressEvents = false; 96 OnRedrawRequired(); 97 } 87 base.SetPosition(lowerLeft, upperRight); 88 SaveVisualProperties(); 98 89 } 99 90 … … 103 94 var p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y + Size.Height)); 104 95 105 using (var font = new Font(FontFamily.GenericSansSerif, (float)Math.Round(8.25 * Chart.WorldToPixelRatio.Width))) { 106 graphics.DrawString(ToolTipText, font, Brushes.Black, p.X + 5, p.Y + 5); 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 } 107 100 } 108 101 … … 111 104 #endregion 112 105 113 public PortRectangle GetPortRectangle(IPort name) {114 PortRectangle portRectangle;115 port2 PortRectangle.TryGetValue(name, out portRectangle);116 return port Rectangle;106 public IPrimitive GetPortPrimitive(IPort name) { 107 IPrimitive portPrimitive; 108 port2primitive.TryGetValue(name, out portPrimitive); 109 return portPrimitive; 117 110 } 118 111 … … 126 119 127 120 #region Event Handlers 128 private void NetworkItemNameChanged(object sender, EventArgs e) {129 UpdateToolTip();130 }131 132 121 private void VisualProperties_Changed(object sender, EventArgs e) { 133 122 if (IgnoreVisualPropertiesChanges) return; 123 134 124 LoadVisualProperties(); 135 125 } 136 126 127 private void NetworkItem_NameChanged(object sender, EventArgs e) { OnRedrawRequired(); } 128 137 129 private void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) { 138 130 foreach (var port in e.Items) { 139 var portRectangle = new PortRectangle(this, port);140 port2 PortRectangle.Add(port, portRectangle);131 var portRectangle = PrimitiveAttribute.CreateDefaultPrimitive(port.GetType(), Chart, port, networkItem); 132 port2primitive.Add(port, portRectangle); 141 133 portRectangles.Add(portRectangle); 142 134 } 143 135 144 SuppressEvents = true; 145 try { 146 UpdatePortRectangles(); 147 } finally { 148 SuppressEvents = false; 149 OnRedrawRequired(); 150 } 136 OnRedrawRequired(); 151 137 } 152 138 153 139 private void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) { 154 140 foreach (var port in e.Items) { 155 var portRectangle = port2 PortRectangle[port];156 port2 PortRectangle.Remove(port);141 var portRectangle = port2primitive[port]; 142 port2primitive.Remove(port); 157 143 portRectangles.Remove(portRectangle); 158 144 } 159 145 160 UpdatePortRectangles();161 146 OnRedrawRequired(); 162 147 } … … 164 149 165 150 #region Helpers 166 private void UpdateToolTip() { 167 ToolTipText = networkItem.Name; 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)); 168 158 } 169 159 170 160 private void LoadVisualProperties() { 171 var offset = Chart.UpperRight - Chart.LowerLeft; 172 var center = new PointD(Chart.LowerLeft.X + offset.DX * 0.5, Chart.LowerLeft.Y + offset.DY * 0.5); 173 var size = Chart.TransformPixelToWorld(new Size(200, 100)); 161 if (networkItem.VisualProperties == null) 162 networkItem.VisualProperties = CreateDefaultVisualProperties(); 174 163 175 bool vpAvailable = networkItem != null && networkItem.VisualProperties != null; 176 bool useDefaultLocation = !vpAvailable || double.IsNaN(networkItem.VisualProperties.X); 177 bool useDefaultSize = !vpAvailable || double.IsNaN(networkItem.VisualProperties.Width); 178 179 if (!useDefaultLocation) { 180 center.X = networkItem.VisualProperties.X; 181 center.Y = networkItem.VisualProperties.Y; 182 } 183 184 if (!useDefaultSize) { 185 size.Width = networkItem.VisualProperties.Width; 186 size.Height = networkItem.VisualProperties.Height; 187 } 188 189 var ll = new PointD(center.X - size.Width * 0.5, center.Y - size.Height * 0.5); 190 var ur = ll + new Offset(size.Width, size.Height); 191 192 SetPosition(ll, ur); 164 var vp = (INodeVisualProperties)networkItem.VisualProperties; 165 SetPosition(new PointD(vp.LowerLeft.X, vp.LowerLeft.Y), new PointD(vp.UpperRight.X, vp.UpperRight.Y)); 193 166 } 194 167 … … 196 169 if (networkItem == null) return; 197 170 198 var visualProperties = networkItem.VisualProperties; 199 var centerW = new PointD(LowerLeft.X + Size.Width * 0.5, UpperRight.Y - Size.Height * 0.5); 171 var vp = (INodeVisualProperties)networkItem.VisualProperties; 200 172 201 173 IgnoreVisualPropertiesChanges = true; 202 174 try { 203 visualProperties.X = centerW.X; 204 visualProperties.Y = centerW.Y; 205 visualProperties.Width = Size.Width; 206 visualProperties.Height = Size.Height; 175 vp.LowerLeft = new Point2D<double>(LowerLeft.X, LowerLeft.Y); 176 vp.UpperRight = new Point2D<double>(UpperRight.X, UpperRight.Y); 207 177 } finally { IgnoreVisualPropertiesChanges = false; } 208 209 OnRedrawRequired();210 }211 212 private void UpdatePortRectangles() {213 if (portRectangles == null) return;214 215 var rectangles = portRectangles.OfType<PortRectangle>().ToArray();216 double space = Size.Width / (rectangles.Length + 1);217 for (int i = 0; i < rectangles.Length; i++) {218 double dx = Math.Round(space * (i + 1));219 var point = LowerLeft + new Offset(dx, 0);220 rectangles[i].SetPosition(point);221 }222 178 } 223 179 #endregion -
branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization/3.3/Primitives/PortRectangle.cs
r13077 r13135 22 22 using System; 23 23 using System.Drawing; 24 using System.Drawing.Drawing2D; 24 25 using System.Linq; 26 using HeuristicLab.Common; 25 27 using HeuristicLab.Core.Networks; 26 28 using HeuristicLab.Visualization; 27 29 28 30 namespace HeuristicLab.Networks.Views.NetworkVisualization { 31 [Primitive(typeof(IPort), true)] 29 32 public class PortRectangle : FixedSizeRectangle, INetworkItemPrimitive<IPort> { 30 33 private const int size = 10; 34 35 protected readonly INodeVisualProperties nodeVisualProperties; 36 37 protected bool IgnoreVisualPropertiesChanges { get; set; } 31 38 32 39 public override Size Size { … … 45 52 networkItem = value; 46 53 if (networkItem != null) RegisterNetworkItemEvents(); 54 LoadVisualProperties(); 47 55 OnNetworkItemChanged(); 48 56 } 49 57 } 50 58 51 public PortRectangle(NodeRectangle nodeRectangle, IPort port) 52 : base(nodeRectangle.Chart, PointD.Empty, Size.Empty, Pens.Black, Brushes.Red) { 59 public PortRectangle(IChart chart, IPort port, INode node) 60 : base(chart, PointD.Empty, Size.Empty, Pens.Black, Brushes.Red) { 61 nodeVisualProperties = (INodeVisualProperties)node.VisualProperties; 62 nodeVisualProperties.Changed += NodeVisualProperties_Changed; 53 63 NetworkItem = port; 54 64 } 55 65 56 66 protected virtual void RegisterNetworkItemEvents() { 57 networkItem.NameChanged += NetworkItemNameChanged; 67 networkItem.NameChanged += NetworkItem_NameChanged; 68 if (networkItem.VisualProperties != null) { 69 networkItem.VisualProperties.Changed += VisualProperties_Changed; 70 } 58 71 var connectablePort = networkItem as IConnectablePort; 59 72 if (connectablePort != null) { … … 63 76 64 77 protected virtual void DeregisterNetworkItemEvents() { 65 networkItem.NameChanged -= NetworkItemNameChanged; 78 networkItem.NameChanged -= NetworkItem_NameChanged; 79 if (networkItem.VisualProperties != null) { 80 networkItem.VisualProperties.Changed -= VisualProperties_Changed; 81 } 66 82 var connectablePort = networkItem as IConnectablePort; 67 83 if (connectablePort != null) { … … 70 86 } 71 87 88 #region Overrides 89 public override void SetPosition(PointD point) { 90 base.SetPosition(point); 91 SaveVisualProperties(); 92 } 93 94 public override void Draw(Graphics graphics) { 95 base.Draw(graphics); 96 97 var p = Chart.TransformWorldToPixel(Point); 98 99 if (networkItem != null && networkItem.VisualProperties != null) { 100 using (var font = new Font(FontFamily.GenericSansSerif, (float)Math.Round(8.25 * Chart.WorldToPixelRatio.Width))) { 101 var textSize = graphics.MeasureString(networkItem.Name, font); 102 var vp = (IPortVisualProperties)networkItem.VisualProperties; 103 switch (vp.Orientation) { 104 case Orientation.North: p = new Point((int)(p.X - textSize.Width / 2), (int)(p.Y - Size.Height - textSize.Height)); break; 105 case Orientation.East: p = new Point(p.X + Size.Width, (int)(p.Y - textSize.Height / 2)); break; 106 case Orientation.South: p = new Point((int)(p.X - textSize.Width / 2), p.Y + Size.Height); break; 107 case Orientation.West: p = new Point((int)(p.X - Size.Width - textSize.Width), (int)(p.Y - textSize.Height / 2)); break; 108 } 109 110 graphics.DrawString(networkItem.Name, font, Brushes.Black, p); 111 } 112 } 113 } 114 115 public override void PostDraw(Graphics graphics) { 116 base.PostDraw(graphics); 117 118 if (Selected) { 119 var p = Chart.TransformWorldToPixel(Point); 120 using (var pen = new Pen(Color.LightGray, 3) { DashStyle = DashStyle.Dash }) 121 graphics.DrawRectangle(pen, p.X - Size.Width / 2, p.Y - Size.Height / 2, Size.Width, Size.Height); 122 } 123 } 124 125 #endregion 126 72 127 #region Events 128 73 129 public event EventHandler NetworkItemChanged; 130 74 131 protected virtual void OnNetworkItemChanged() { 75 UpdateToolTip();76 132 var handler = NetworkItemChanged; 77 133 if (handler != null) handler(this, EventArgs.Empty); 78 134 } 79 135 80 public event EventHandler NodeRectangleChanged;81 protected virtual void OnNodeRectangleChanged() {82 var handler = NodeRectangleChanged;83 if (handler != null) handler(this, EventArgs.Empty);84 }85 136 #endregion 86 137 87 138 #region Event Handlers 88 private void NetworkItemNameChanged(object sender, EventArgs e) { 89 UpdateToolTip(); 139 140 private void NodeVisualProperties_Changed(object sender, EventArgs e) { 141 if (IgnoreVisualPropertiesChanges) return; 142 143 LoadVisualProperties(); 144 } 145 146 private void VisualProperties_Changed(object sender, EventArgs e) { 147 if (IgnoreVisualPropertiesChanges) return; 148 149 LoadVisualProperties(); 150 } 151 152 private void NetworkItem_NameChanged(object sender, EventArgs e) { 153 OnRedrawRequired(); 90 154 } 91 155 92 156 private void Port_ConnectedPortChanged(object sender, EventArgs e) { 93 var connectablePort = (IConnectablePort)networkItem;94 95 157 var connectionLines = Chart.Group.GetAllPrimitives(Point).OfType<ConnectionLine>(); 96 158 var outgoingLines = connectionLines.Where(x => x.StartPortRectangle == this); 97 98 foreach (var l in outgoingLines) 99 Chart.Group.Remove(l); 100 159 foreach (var l in outgoingLines) Chart.Group.Remove(l); 160 161 var connectablePort = (IConnectablePort)networkItem; 101 162 var connectedPort = connectablePort.ConnectedPort as IConnectablePort; 102 163 if (connectedPort == null) return; 103 104 var connectedNode = connectedPort.Parent;105 if (connectedNode == null) return;106 164 107 165 var nodeRectangles = Chart.Group.Primitives.OfType<NodeRectangle>(); 108 166 PortRectangle endPortRectangle = null; 109 167 foreach (var nodeRectangle in nodeRectangles) { 110 endPortRectangle = nodeRectangle.GetPortRectangle(connectedPort);168 endPortRectangle = (PortRectangle)nodeRectangle.GetPortPrimitive(connectedPort); 111 169 if (endPortRectangle != null) break; 112 170 } … … 116 174 var connectionShape = new ConnectionLine(Chart, this, endPortRectangle); 117 175 Chart.Group.Add(connectionShape); 118 119 } 176 } 177 120 178 #endregion 121 179 122 180 #region Helpers 123 private void UpdateToolTip() { 124 ToolTipText = networkItem.Name; 125 } 181 182 private IPortVisualProperties CreateDefaultVisualProperties() { 183 return new PortVisualProperties(Orientation.South, 0.5); 184 } 185 186 private void LoadVisualProperties() { 187 if (networkItem.VisualProperties == null) 188 networkItem.VisualProperties = CreateDefaultVisualProperties(); 189 190 var vp = (IPortVisualProperties)networkItem.VisualProperties; 191 var p = PointD.Empty; 192 double nodeWidth = nodeVisualProperties.UpperRight.X - nodeVisualProperties.LowerLeft.X; 193 double nodeHeight = nodeVisualProperties.UpperRight.Y - nodeVisualProperties.LowerLeft.Y; 194 195 switch (vp.Orientation) { 196 case Orientation.North: 197 p.X = nodeVisualProperties.LowerLeft.X + vp.Offset * nodeWidth; 198 p.Y = nodeVisualProperties.UpperRight.Y; 199 break; 200 case Orientation.East: 201 p.X = nodeVisualProperties.UpperRight.X; 202 p.Y = nodeVisualProperties.UpperRight.Y - vp.Offset * nodeHeight; 203 break; 204 case Orientation.South: 205 p.X = nodeVisualProperties.UpperRight.X - vp.Offset * nodeWidth; 206 p.Y = nodeVisualProperties.LowerLeft.Y; 207 break; 208 case Orientation.West: 209 p.X = nodeVisualProperties.LowerLeft.X; 210 p.Y = nodeVisualProperties.LowerLeft.Y + vp.Offset * nodeHeight; 211 break; 212 } 213 214 SetPosition(p); 215 } 216 217 private void SaveVisualProperties() { 218 if (networkItem == null) return; 219 220 var vp = (IPortVisualProperties)networkItem.VisualProperties; 221 222 IgnoreVisualPropertiesChanges = true; 223 try { 224 var lowerLeft = nodeVisualProperties.LowerLeft; 225 var upperRight = nodeVisualProperties.UpperRight; 226 227 if (lowerLeft.X <= Point.X && Point.X <= upperRight.X) { 228 // check N/S 229 if (Point.Y.IsAlmost(upperRight.Y)) { 230 vp.Orientation = Orientation.North; 231 vp.Offset = (Point.X - lowerLeft.X) / (upperRight.X - lowerLeft.X); 232 } else if (Point.Y.IsAlmost(lowerLeft.Y)) { 233 vp.Orientation = Orientation.South; 234 vp.Offset = (upperRight.X - Point.X) / (upperRight.X - lowerLeft.X); 235 } 236 } 237 if (lowerLeft.Y <= Point.Y && Point.Y <= upperRight.Y) { 238 // check E/W 239 if (Point.X.IsAlmost(upperRight.X)) { 240 vp.Orientation = Orientation.East; 241 vp.Offset = (upperRight.Y - Point.Y) / (upperRight.Y - lowerLeft.Y); 242 } else if (Point.X.IsAlmost(lowerLeft.X)) { 243 vp.Orientation = Orientation.West; 244 vp.Offset = (Point.Y - lowerLeft.Y) / (upperRight.Y - lowerLeft.Y); 245 } 246 } 247 } finally { 248 IgnoreVisualPropertiesChanges = false; 249 } 250 } 251 126 252 #endregion 127 253 }
Note: See TracChangeset
for help on using the changeset viewer.