[13077] | 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 System.Linq;
|
---|
| 26 | using HeuristicLab.Core.Networks;
|
---|
| 27 | using HeuristicLab.Visualization;
|
---|
| 28 |
|
---|
| 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;
|
---|
| 34 |
|
---|
| 35 | protected bool IgnoreVisualPropertiesChanges { get; set; }
|
---|
| 36 |
|
---|
| 37 | protected IVisualizableNode networkItem;
|
---|
| 38 | public IVisualizableNode 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 | UpdateToolTip();
|
---|
| 46 | LoadVisualProperties();
|
---|
| 47 | OnNetworkItemChanged();
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public NodeRectangle(IChart chart, IVisualizableNode node)
|
---|
| 52 | : base(chart, Point.Empty, Point.Empty) {
|
---|
| 53 | NetworkItem = node;
|
---|
| 54 |
|
---|
| 55 | portRectangles = new Group(chart);
|
---|
| 56 | foreach (var port in node.Ports) {
|
---|
| 57 | var portRectangle = new PortRectangle(this, port);
|
---|
| 58 | port2PortRectangle.Add(port, portRectangle);
|
---|
| 59 | portRectangles.Add(portRectangle);
|
---|
| 60 | }
|
---|
| 61 | UpdatePortRectangles();
|
---|
| 62 | portRectangles.RedrawRequired += (sender, args) => OnRedrawRequired();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | protected virtual void RegisterNetworkItemEvents() {
|
---|
| 66 | networkItem.NameChanged += NetworkItemNameChanged;
|
---|
| 67 | if (networkItem.VisualProperties != null) {
|
---|
| 68 | networkItem.VisualProperties.Changed += VisualProperties_Changed;
|
---|
| 69 | }
|
---|
| 70 | if (networkItem.Ports != null) {
|
---|
| 71 | networkItem.Ports.ItemsAdded += Ports_ItemsAdded;
|
---|
| 72 | networkItem.Ports.ItemsRemoved += Ports_ItemsRemoved;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | protected virtual void DeregisterNetworkItemEvents() {
|
---|
| 77 | networkItem.NameChanged -= NetworkItemNameChanged;
|
---|
| 78 | if (networkItem.VisualProperties != null) {
|
---|
| 79 | networkItem.VisualProperties.Changed -= VisualProperties_Changed;
|
---|
| 80 | }
|
---|
| 81 | if (networkItem.Ports != null) {
|
---|
| 82 | networkItem.Ports.ItemsAdded -= Ports_ItemsAdded;
|
---|
| 83 | networkItem.Ports.ItemsRemoved -= Ports_ItemsRemoved;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | #region Overrides
|
---|
| 88 | 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 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public override void Draw(Graphics graphics) {
|
---|
| 101 | base.Draw(graphics);
|
---|
| 102 |
|
---|
| 103 | var p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y + Size.Height));
|
---|
| 104 |
|
---|
| 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);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | portRectangles.Draw(graphics);
|
---|
| 110 | }
|
---|
| 111 | #endregion
|
---|
| 112 |
|
---|
| 113 | public PortRectangle GetPortRectangle(IPort name) {
|
---|
| 114 | PortRectangle portRectangle;
|
---|
| 115 | port2PortRectangle.TryGetValue(name, out portRectangle);
|
---|
| 116 | return portRectangle;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | #region Events
|
---|
| 120 | public event EventHandler NetworkItemChanged;
|
---|
| 121 | protected virtual void OnNetworkItemChanged() {
|
---|
| 122 | var handler = NetworkItemChanged;
|
---|
| 123 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 124 | }
|
---|
| 125 | #endregion
|
---|
| 126 |
|
---|
| 127 | #region Event Handlers
|
---|
| 128 | private void NetworkItemNameChanged(object sender, EventArgs e) {
|
---|
| 129 | UpdateToolTip();
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | private void VisualProperties_Changed(object sender, EventArgs e) {
|
---|
| 133 | if (IgnoreVisualPropertiesChanges) return;
|
---|
| 134 | LoadVisualProperties();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | private void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 138 | foreach (var port in e.Items) {
|
---|
| 139 | var portRectangle = new PortRectangle(this, port);
|
---|
| 140 | port2PortRectangle.Add(port, portRectangle);
|
---|
| 141 | portRectangles.Add(portRectangle);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | SuppressEvents = true;
|
---|
| 145 | try {
|
---|
| 146 | UpdatePortRectangles();
|
---|
| 147 | } finally {
|
---|
| 148 | SuppressEvents = false;
|
---|
| 149 | OnRedrawRequired();
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | private void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
|
---|
| 154 | foreach (var port in e.Items) {
|
---|
| 155 | var portRectangle = port2PortRectangle[port];
|
---|
| 156 | port2PortRectangle.Remove(port);
|
---|
| 157 | portRectangles.Remove(portRectangle);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | UpdatePortRectangles();
|
---|
| 161 | OnRedrawRequired();
|
---|
| 162 | }
|
---|
| 163 | #endregion
|
---|
| 164 |
|
---|
| 165 | #region Helpers
|
---|
| 166 | private void UpdateToolTip() {
|
---|
| 167 | ToolTipText = networkItem.Name;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | 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));
|
---|
| 174 |
|
---|
| 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);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | private void SaveVisualProperties() {
|
---|
| 196 | if (networkItem == null) return;
|
---|
| 197 |
|
---|
| 198 | var visualProperties = networkItem.VisualProperties;
|
---|
| 199 | var centerW = new PointD(LowerLeft.X + Size.Width * 0.5, UpperRight.Y - Size.Height * 0.5);
|
---|
| 200 |
|
---|
| 201 | IgnoreVisualPropertiesChanges = true;
|
---|
| 202 | try {
|
---|
| 203 | visualProperties.X = centerW.X;
|
---|
| 204 | visualProperties.Y = centerW.Y;
|
---|
| 205 | visualProperties.Width = Size.Width;
|
---|
| 206 | visualProperties.Height = Size.Height;
|
---|
| 207 | } 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 | }
|
---|
| 223 | #endregion
|
---|
| 224 | }
|
---|
| 225 | }
|
---|