Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/11/15 13:53:10 (9 years ago)
Author:
jkarder
Message:

#2205: worked on optimization networks

  • refactored network visualization
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  
    5959    protected virtual void RegisterPortRectangleEvents(PortRectangle portRectangle) {
    6060      portRectangle.RedrawRequired += PortRectangle_RedrawRequired;
    61       portRectangle.NetworkItemChanged += PortRectangleNetworkItemChanged;
    62       if (portRectangle.NetworkItem != null) {
    63         var port = portRectangle.NetworkItem;
    64         port.NameChanged += Port_NameChanged;
    65       }
    6661    }
    6762
    6863    protected virtual void DeregisterPortRectangleEvents(PortRectangle portRectangle) {
    6964      portRectangle.RedrawRequired -= PortRectangle_RedrawRequired;
    70       portRectangle.NetworkItemChanged -= PortRectangleNetworkItemChanged;
    71       if (portRectangle.NetworkItem != null) {
    72         var port = portRectangle.NetworkItem;
    73         port.NameChanged -= Port_NameChanged;
    74       }
    7565    }
    7666
     
    10595      }
    10696    }
    107 
    10897    #endregion
    10998
     
    111100    public event EventHandler StartPortRectangleChanged;
    112101    protected virtual void OnStartPortRectangleChanged() {
    113       UpdateToolTip();
    114102      var handler = StartPortRectangleChanged;
    115103      if (handler != null) handler(this, EventArgs.Empty);
     
    118106    public event EventHandler EndPortRectangleChanged;
    119107    protected virtual void OnEndPortRectangleChanged() {
    120       UpdateToolTip();
    121108      var handler = EndPortRectangleChanged;
    122109      if (handler != null) handler(this, EventArgs.Empty);
     
    128115      SetPosition(startPortRectangle.Point, endPortRectangle.Point);
    129116    }
    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     #endregion
    139 
    140     #region Helpers
    141     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     }
    164117    #endregion
    165118  }
  • branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization/3.3/Primitives/NodeRectangle.cs

    r13077 r13135  
    2323using System.Collections.Generic;
    2424using System.Drawing;
    25 using System.Linq;
     25using HeuristicLab.Common;
    2626using HeuristicLab.Core.Networks;
    2727using HeuristicLab.Visualization;
    2828
    2929namespace 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;
    3434
    3535    protected bool IgnoreVisualPropertiesChanges { get; set; }
    3636
    37     protected IVisualizableNode networkItem;
    38     public IVisualizableNode NetworkItem {
     37    protected INode networkItem;
     38    public INode NetworkItem {
    3939      get { return networkItem; }
    4040      set {
     
    4343        networkItem = value;
    4444        if (networkItem != null) RegisterNetworkItemEvents();
    45         UpdateToolTip();
    4645        LoadVisualProperties();
    4746        OnNetworkItemChanged();
     
    4948    }
    5049
    51     public NodeRectangle(IChart chart, IVisualizableNode node)
    52       : base(chart, Point.Empty, Point.Empty) {
     50    public NodeRectangle(IChart chart, INode node)
     51      : base(chart, PointD.Empty, PointD.Empty) {
    5352      NetworkItem = node;
    5453
    5554      portRectangles = new Group(chart);
    5655      foreach (var port in node.Ports) {
    57         var portRectangle = new PortRectangle(this, port);
    58         port2PortRectangle.Add(port, portRectangle);
    59         portRectangles.Add(portRectangle);
     56        var portPrimitive = PrimitiveAttribute.CreateDefaultPrimitive(port.GetType(), chart, port, node); // TODO: port.Parent != node
     57        port2primitive.Add(port, portPrimitive);
     58        portRectangles.Add(portPrimitive);
    6059      }
    61       UpdatePortRectangles();
    6260      portRectangles.RedrawRequired += (sender, args) => OnRedrawRequired();
    6361    }
    6462
    6563    protected virtual void RegisterNetworkItemEvents() {
    66       networkItem.NameChanged += NetworkItemNameChanged;
     64      networkItem.NameChanged += NetworkItem_NameChanged;
    6765      if (networkItem.VisualProperties != null) {
    6866        networkItem.VisualProperties.Changed += VisualProperties_Changed;
     
    7573
    7674    protected virtual void DeregisterNetworkItemEvents() {
    77       networkItem.NameChanged -= NetworkItemNameChanged;
     75      networkItem.NameChanged -= NetworkItem_NameChanged;
    7876      if (networkItem.VisualProperties != null) {
    7977        networkItem.VisualProperties.Changed -= VisualProperties_Changed;
     
    8785    #region Overrides
    8886    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();
    9889    }
    9990
     
    10394      var p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y + Size.Height));
    10495
    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        }
    107100      }
    108101
     
    111104    #endregion
    112105
    113     public PortRectangle GetPortRectangle(IPort name) {
    114       PortRectangle portRectangle;
    115       port2PortRectangle.TryGetValue(name, out portRectangle);
    116       return portRectangle;
     106    public IPrimitive GetPortPrimitive(IPort name) {
     107      IPrimitive portPrimitive;
     108      port2primitive.TryGetValue(name, out portPrimitive);
     109      return portPrimitive;
    117110    }
    118111
     
    126119
    127120    #region Event Handlers
    128     private void NetworkItemNameChanged(object sender, EventArgs e) {
    129       UpdateToolTip();
    130     }
    131 
    132121    private void VisualProperties_Changed(object sender, EventArgs e) {
    133122      if (IgnoreVisualPropertiesChanges) return;
     123
    134124      LoadVisualProperties();
    135125    }
    136126
     127    private void NetworkItem_NameChanged(object sender, EventArgs e) { OnRedrawRequired(); }
     128
    137129    private void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    138130      foreach (var port in e.Items) {
    139         var portRectangle = new PortRectangle(this, port);
    140         port2PortRectangle.Add(port, portRectangle);
     131        var portRectangle = PrimitiveAttribute.CreateDefaultPrimitive(port.GetType(), Chart, port, networkItem);
     132        port2primitive.Add(port, portRectangle);
    141133        portRectangles.Add(portRectangle);
    142134      }
    143135
    144       SuppressEvents = true;
    145       try {
    146         UpdatePortRectangles();
    147       } finally {
    148         SuppressEvents = false;
    149         OnRedrawRequired();
    150       }
     136      OnRedrawRequired();
    151137    }
    152138
    153139    private void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    154140      foreach (var port in e.Items) {
    155         var portRectangle = port2PortRectangle[port];
    156         port2PortRectangle.Remove(port);
     141        var portRectangle = port2primitive[port];
     142        port2primitive.Remove(port);
    157143        portRectangles.Remove(portRectangle);
    158144      }
    159145
    160       UpdatePortRectangles();
    161146      OnRedrawRequired();
    162147    }
     
    164149
    165150    #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));
    168158    }
    169159
    170160    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();
    174163
    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));
    193166    }
    194167
     
    196169      if (networkItem == null) return;
    197170
    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;
    200172
    201173      IgnoreVisualPropertiesChanges = true;
    202174      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);
    207177      } 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       }
    222178    }
    223179    #endregion
  • branches/OptimizationNetworks/HeuristicLab.Networks.Views.NetworkVisualization/3.3/Primitives/PortRectangle.cs

    r13077 r13135  
    2222using System;
    2323using System.Drawing;
     24using System.Drawing.Drawing2D;
    2425using System.Linq;
     26using HeuristicLab.Common;
    2527using HeuristicLab.Core.Networks;
    2628using HeuristicLab.Visualization;
    2729
    2830namespace HeuristicLab.Networks.Views.NetworkVisualization {
     31  [Primitive(typeof(IPort), true)]
    2932  public class PortRectangle : FixedSizeRectangle, INetworkItemPrimitive<IPort> {
    3033    private const int size = 10;
     34
     35    protected readonly INodeVisualProperties nodeVisualProperties;
     36
     37    protected bool IgnoreVisualPropertiesChanges { get; set; }
    3138
    3239    public override Size Size {
     
    4552        networkItem = value;
    4653        if (networkItem != null) RegisterNetworkItemEvents();
     54        LoadVisualProperties();
    4755        OnNetworkItemChanged();
    4856      }
    4957    }
    5058
    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;
    5363      NetworkItem = port;
    5464    }
    5565
    5666    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      }
    5871      var connectablePort = networkItem as IConnectablePort;
    5972      if (connectablePort != null) {
     
    6376
    6477    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      }
    6682      var connectablePort = networkItem as IConnectablePort;
    6783      if (connectablePort != null) {
     
    7086    }
    7187
     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
    72127    #region Events
     128
    73129    public event EventHandler NetworkItemChanged;
     130
    74131    protected virtual void OnNetworkItemChanged() {
    75       UpdateToolTip();
    76132      var handler = NetworkItemChanged;
    77133      if (handler != null) handler(this, EventArgs.Empty);
    78134    }
    79135
    80     public event EventHandler NodeRectangleChanged;
    81     protected virtual void OnNodeRectangleChanged() {
    82       var handler = NodeRectangleChanged;
    83       if (handler != null) handler(this, EventArgs.Empty);
    84     }
    85136    #endregion
    86137
    87138    #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();
    90154    }
    91155
    92156    private void Port_ConnectedPortChanged(object sender, EventArgs e) {
    93       var connectablePort = (IConnectablePort)networkItem;
    94 
    95157      var connectionLines = Chart.Group.GetAllPrimitives(Point).OfType<ConnectionLine>();
    96158      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;
    101162      var connectedPort = connectablePort.ConnectedPort as IConnectablePort;
    102163      if (connectedPort == null) return;
    103 
    104       var connectedNode = connectedPort.Parent;
    105       if (connectedNode == null) return;
    106164
    107165      var nodeRectangles = Chart.Group.Primitives.OfType<NodeRectangle>();
    108166      PortRectangle endPortRectangle = null;
    109167      foreach (var nodeRectangle in nodeRectangles) {
    110         endPortRectangle = nodeRectangle.GetPortRectangle(connectedPort);
     168        endPortRectangle = (PortRectangle)nodeRectangle.GetPortPrimitive(connectedPort);
    111169        if (endPortRectangle != null) break;
    112170      }
     
    116174      var connectionShape = new ConnectionLine(Chart, this, endPortRectangle);
    117175      Chart.Group.Add(connectionShape);
    118 
    119     }
     176    }
     177
    120178    #endregion
    121179
    122180    #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
    126252    #endregion
    127253  }
Note: See TracChangeset for help on using the changeset viewer.