Changeset 2853 for trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphView.cs
- Timestamp:
- 02/23/10 10:58:11 (15 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphView.cs
r2825 r2853 33 33 using Netron.Diagramming.Core; 34 34 using HeuristicLab.Parameters; 35 using HeuristicLab.MainForm.WindowsForms; 35 36 36 37 namespace HeuristicLab.Operators.Views.GraphVisualization { 37 [Content(typeof(IOperator), false)] 38 public partial class OperatorGraphVisualizationView : ItemView { 39 private Dictionary<IOperator, IShape> operatorShapeMapping; 40 private Dictionary<KeyValuePair<IOperator, IValueParameter<IOperator>>, IConnection> connectionMapping; 41 private HashSet<IValueParameter<IOperator>> parameters; 42 /// <summary> 43 /// Initializes a new instance of <see cref="OperatorGraphVisualizationView"/> with caption "Operator Graph". 44 /// </summary> 45 public OperatorGraphVisualizationView() { 38 [Content(typeof(OperatorGraph), false)] 39 public partial class OperatorGraphView : ContentView { 40 private BidirectionalLookup<IShapeInfo, IShape> shapeMapping; 41 private BidirectionalLookup<IConnectionInfo, IConnection> connectionMapping; 42 43 private bool causedUpdateOfShapeInfo; 44 public OperatorGraphView() { 46 45 InitializeComponent(); 46 this.causedUpdateOfShapeInfo = false; 47 47 Caption = "Operator Graph"; 48 this.operatorShapeMapping = new Dictionary<IOperator, IShape>(); 49 this.connectionMapping = new Dictionary<KeyValuePair<IOperator, IValueParameter<IOperator>>, IConnection>(); 50 this.parameters = new HashSet<IValueParameter<IOperator>>(); 51 52 this.graphVisualization.Controller.Model.OnEntityRemoved += new EventHandler<EntityEventArgs>(Model_OnEntityRemoved); 53 } 54 55 /// <summary> 56 /// Initializes a new instance of <see cref="OperatorGraphVisualizationView"/> 57 /// with the given <paramref name="operatorGraph"/>. 58 /// </summary> 59 /// <remarks>Calls <see cref="OperatorGraphView()"/>.</remarks> 60 /// <param name="operatorGraph">The operator graph to represent visually.</param> 61 public OperatorGraphVisualizationView(IOperator content) 48 this.shapeMapping = new BidirectionalLookup<IShapeInfo, IShape>(); 49 this.connectionMapping = new BidirectionalLookup<IConnectionInfo, IConnection>(); 50 } 51 52 public OperatorGraphView(OperatorGraph content) 62 53 : this() { 63 54 this.Content = content; 64 55 } 65 56 66 /// <summary> 67 /// Gets or sets the operator graph to represent visually. 68 /// </summary> 69 /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>. 70 /// No own data storage present.</remarks> 71 public new IOperator Content { 72 get { return (IOperator)base.Content; } 57 public new OperatorGraph Content { 58 get { return (OperatorGraph)base.Content; } 73 59 set { base.Content = value; } 74 60 } 75 61 76 private IModel GraphModel {77 get { return this.graphVisualization.Controller.Model; }78 }79 80 81 62 protected override void OnContentChanged() { 82 base.OnContentChanged(); 83 this.ClearGraph(); 84 85 this.CreateGraph(null, new OperatorParameter(string.Empty, this.Content)); 86 foreach (IShape shape in this.operatorShapeMapping.Values) 87 this.GraphModel.AddShape(shape); 88 89 foreach (IConnection connection in this.connectionMapping.Values) 90 this.GraphModel.AddConnection(connection); 91 92 if (this.Content == null) 93 this.graphVisualization.Controller.Model.LayoutRoot = null; 94 else 95 this.graphVisualization.Controller.Model.LayoutRoot = this.operatorShapeMapping[this.Content]; 96 this.RelayoutOperatorGraph(); 97 } 98 99 private void opParam_ValueChanged(object sender, EventArgs e) { 100 if (InvokeRequired) 101 Invoke(new EventHandler(opParam_ValueChanged), sender, e); 102 else { 103 this.OnContentChanged(); 63 if (this.VisualizationInfo == null) 64 this.VisualizationInfo = new OperatorGraphVisualizationInfo(this.Content); 65 this.UpdateVisualizationInfo(); 66 } 67 68 private OperatorGraphVisualizationInfo VisualizationInfo { 69 get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; } 70 set { this.Content.VisualizationInfo = value; } 71 } 72 73 private void UpdateVisualizationInfo() { 74 foreach (IShapeInfo shapeInfo in this.shapeMapping.FirstValues) 75 this.RemoveShapeInfo(shapeInfo); 76 this.shapeMapping.Clear(); 77 78 foreach (IShapeInfo shapeInfo in this.VisualizationInfo.ShapeInfos) 79 this.AddShapeInfo(shapeInfo); 80 } 81 82 protected override void RegisterContentEvents() { 83 base.RegisterContentEvents(); 84 this.VisualizationInfo.ObserveableShapeInfos.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsAdded); 85 this.VisualizationInfo.ObserveableShapeInfos.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsRemoved); 86 this.VisualizationInfo.ObserveableShapeInfos.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_CollectionReset); 87 } 88 89 protected override void DeregisterContentEvents() { 90 base.DeregisterContentEvents(); 91 this.VisualizationInfo.ObserveableShapeInfos.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsAdded); 92 this.VisualizationInfo.ObserveableShapeInfos.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsRemoved); 93 this.VisualizationInfo.ObserveableShapeInfos.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_CollectionReset); 94 } 95 96 97 private void ShapeInfos_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) { 98 foreach (IShapeInfo shapeInfo in e.OldItems) 99 this.RemoveShapeInfo(shapeInfo); 100 foreach (IShapeInfo shapeInfo in e.Items) 101 this.AddShapeInfo(shapeInfo); 102 this.graphVisualization.Invalidate(); 103 } 104 105 private void ShapeInfos_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) { 106 foreach (IShapeInfo shapeInfo in e.Items) 107 this.AddShapeInfo(shapeInfo); 108 this.graphVisualization.Invalidate(); 109 } 110 111 private void ShapeInfos_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) { 112 foreach (IShapeInfo shapeInfo in e.Items) 113 this.RemoveShapeInfo(shapeInfo); 114 this.graphVisualization.Invalidate(); 115 } 116 117 private void AddShapeInfo(IShapeInfo shapeInfo) { 118 IShape shape = shapeInfo.CreateShape(); 119 this.graphVisualization.Controller.Model.AddShape(shape); 120 this.shapeMapping.Add(shapeInfo, shape); 121 122 shape.OnEntityChange += new EventHandler<EntityEventArgs>(shape_OnEntityChange); 123 shapeInfo.Changed += new ChangedEventHandler(shapeInfo_Changed); 124 } 125 126 private void RemoveShapeInfo(IShapeInfo shapeInfo) { 127 IShape shape = this.shapeMapping.GetByFirst(shapeInfo); 128 shape.OnEntityChange -= new EventHandler<EntityEventArgs>(shape_OnEntityChange); 129 shapeInfo.Changed -= new ChangedEventHandler(shapeInfo_Changed); 130 131 this.shapeMapping.RemoveByFirst(shapeInfo); 132 if (this.graphVisualization.Controller.Model.Shapes.Contains(shape)) 133 this.graphVisualization.Controller.Model.RemoveShape(shape); 134 } 135 136 private void shapeInfo_Changed(object sender, ChangedEventArgs e) { 137 IShapeInfo shapeInfo = (IShapeInfo)sender; 138 this.UpdateShape(shapeInfo); 139 } 140 141 private void shape_OnEntityChange(object sender, EntityEventArgs e) { 142 this.causedUpdateOfShapeInfo = true; 143 IShape shape = e.Entity as IShape; 144 IShapeInfo shapeInfo = this.shapeMapping.GetBySecond(shape); 145 146 shapeInfo.Location = shape.Location; 147 this.graphVisualization.Invalidate(); 148 this.causedUpdateOfShapeInfo = false; 149 } 150 151 private void UpdateShape(IShapeInfo shapeInfo) { 152 if (!this.causedUpdateOfShapeInfo) { 153 IShape shape = this.shapeMapping.GetByFirst(shapeInfo); 154 shape.Location = shapeInfo.Location; 104 155 } 105 156 } 106 157 107 private void ClearGraph() { 108 this.GraphModel.Clear(); 109 foreach (IValueParameter<IOperator> opParam in this.parameters) 110 opParam.ValueChanged -= opParam_ValueChanged; 111 112 this.operatorShapeMapping.Clear(); 113 this.parameters.Clear(); 114 this.connectionMapping.Clear(); 115 } 116 117 118 119 private void CreateGraph(IOperator parent, IValueParameter<IOperator> op) { 120 if (op == null || op.Value == null) 121 return; 122 123 IShape shape; 124 if (!this.operatorShapeMapping.ContainsKey(op.Value)) { 125 shape = CreateOperatorShape(op.Value); 126 this.operatorShapeMapping[op.Value] = shape; 127 128 foreach (IParameter param in op.Value.Parameters) { 129 IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>; 130 if (opParam != null) { 131 HandleOperatorParameter(opParam); 132 this.CreateGraph(op.Value, opParam); 133 } 134 } 158 private void graphVisualization_OnEntityRemoved(object sender, EntityEventArgs e) { 159 IShape shape = (IShape)e.Entity; 160 if (this.shapeMapping.ContainsSecond(shape)) { 161 IShapeInfo shapeInfo = this.shapeMapping.GetBySecond(shape); 162 this.VisualizationInfo.RemoveShapeInfo(shapeInfo); 135 163 } 136 137 if (parent != null) 138 ConnectShapes(parent, op); 139 } 140 141 private void HandleOperatorParameter(IValueParameter<IOperator> opParam) { 142 if (opParam == null) 143 return; 144 opParam.ValueChanged += new EventHandler(opParam_ValueChanged); 145 parameters.Add(opParam); 146 } 147 148 private IConnection ConnectShapes(IOperator parent, IValueParameter<IOperator> opParam) { 149 IShape operatorShape = this.operatorShapeMapping[opParam.Value]; 150 IShape parentShape = this.operatorShapeMapping[parent]; 151 IConnector operatorConnector = parentShape.Connectors.Where(c => c.Name == "Bottom connector").First(); 152 IConnector parentConnector = operatorShape.Connectors.Where(c => c.Name == "Top connector").First(); 153 154 IConnection connection = new Connection(parentConnector.Point, operatorConnector.Point); 155 parentConnector.AttachConnector(connection.From); 156 operatorConnector.AttachConnector(connection.To); 157 158 this.connectionMapping[new KeyValuePair<IOperator, IValueParameter<IOperator>>(parent, opParam)] = connection; 159 return connection; 160 } 161 162 private IShape CreateOperatorShape(IOperator op) { 163 ClassShape shape = new ClassShape(); 164 shape.Name = op.Name; 165 shape.Text = op.Name; 166 shape.Title = op.Name; 167 shape.SubTitle = op.GetType().ToString(); 168 169 return shape; 170 } 171 172 private void Model_OnEntityRemoved(object sender, EntityEventArgs e) { 173 IShape shape = e.Entity as IShape; 174 if (shape != null) { 175 IOperator op = operatorShapeMapping.Where(os => os.Value == shape).First().Key; 176 if (op == this.Content) 177 this.Content = null; 178 else { 179 //clear all connections to the removed operator 180 IEnumerable<IValueParameter<IOperator>> parentOperator = this.connectionMapping.Where(cs => cs.Key.Value.Value == op).Select(x => x.Key.Value); 181 foreach(IValueParameter<IOperator> opParam in parentOperator.ToArray()) 182 opParam.Value = null; 183 184 //remove connections from graph view 185 IEnumerable<IConnection> connections = this.connectionMapping.Where(cs => cs.Key.Value.Value == op).Select(x => x.Value); 186 foreach (IConnection connection in connections) 187 this.GraphModel.Remove(connection); 188 189 this.graphVisualization.Invalidate(); 190 } 191 192 193 } 194 } 164 } 165 166 167 168 169 //protected override void OnContentChanged() { 170 // base.OnContentChanged(); 171 // this.ClearGraph(); 172 173 // this.CreateGraph(null, new OperatorParameter(string.Empty, this.Content)); 174 // foreach (IShape shape in this.operatorShapeMapping.Values) 175 // this.GraphModel.AddShape(shape); 176 177 // foreach (IConnection connection in this.connectionMapping.Values) 178 // this.GraphModel.AddConnection(connection); 179 180 // if (this.Content == null) 181 // this.graphVisualization.Controller.Model.LayoutRoot = null; 182 // else 183 // this.graphVisualization.Controller.Model.LayoutRoot = this.operatorShapeMapping[this.Content]; 184 // this.RelayoutOperatorGraph(); 185 //} 186 187 //private void opParam_ValueChanged(object sender, EventArgs e) { 188 // if (InvokeRequired) 189 // Invoke(new EventHandler(opParam_ValueChanged), sender, e); 190 // else { 191 // this.OnContentChanged(); 192 // } 193 //} 194 195 //private void ClearGraph() { 196 // this.GraphModel.Clear(); 197 // foreach (IValueParameter<IOperator> opParam in this.parameters) 198 // opParam.ValueChanged -= opParam_ValueChanged; 199 200 // this.operatorShapeMapping.Clear(); 201 // this.parameters.Clear(); 202 // this.connectionMapping.Clear(); 203 //} 204 205 206 207 //private void CreateGraph(IOperator parent, IValueParameter<IOperator> op) { 208 // if (op == null || op.Value == null) 209 // return; 210 211 // IShape shape; 212 // if (!this.operatorShapeMapping.ContainsKey(op.Value)) { 213 // //shape = GraphVisualizationInfo.CreateShape(op.Value); 214 // //this.operatorShapeMapping[op.Value] = shape; 215 216 // foreach (IParameter param in op.Value.Parameters) { 217 // IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>; 218 // if (opParam != null) { 219 // HandleOperatorParameter(opParam); 220 // this.CreateGraph(op.Value, opParam); 221 // } 222 // } 223 // } 224 225 // if (parent != null) { 226 // IShape from = this.operatorShapeMapping[parent]; 227 // IShape to = this.operatorShapeMapping[op.Value]; 228 // //IConnection connection = GraphVisualizationInfo.CreateConnection(from,to); 229 // //this.connectionMapping[new KeyValuePair<IOperator, IValueParameter<IOperator>>(parent, op)] = connection; 230 // } 231 //} 232 233 //private void DeleteGraph(IOperator parent, IValueParameter<IOperator> op) { 234 235 //} 236 237 //private void HandleOperatorParameter(IValueParameter<IOperator> opParam) { 238 // if (opParam == null) 239 // return; 240 // opParam.ValueChanged += new EventHandler(opParam_ValueChanged); 241 // parameters.Add(opParam); 242 //} 243 244 //private void Model_OnEntityRemoved(object sender, EntityEventArgs e) { 245 // IShape shape = e.Entity as IShape; 246 // if (shape != null) { 247 // IOperator op = operatorShapeMapping.Where(os => os.Value == shape).First().Key; 248 // if (op == this.Content) 249 // this.Content = null; 250 // else { 251 // //clear all connections to the removed operator 252 // IEnumerable<IValueParameter<IOperator>> parentOperator = this.connectionMapping.Where(cs => cs.Key.Value.Value == op).Select(x => x.Key.Value); 253 // foreach (IValueParameter<IOperator> opParam in parentOperator.ToArray()) 254 // opParam.Value = null; 255 256 // //remove connections from graph view 257 // IEnumerable<IConnection> connections = this.connectionMapping.Where(cs => cs.Key.Value.Value == op).Select(x => x.Value); 258 // foreach (IConnection connection in connections) 259 // this.GraphModel.Remove(connection); 260 261 // this.graphVisualization.Invalidate(); 262 // } 263 264 265 // } 266 //} 195 267 196 268 #region methods for toolbar items 197 269 198 270 internal void RelayoutOperatorGraph() { 199 if (this.operatorShapeMapping.Count > 0 && this.connectionMapping.Count > 0) { //otherwise the layout does not work200 this.graphVisualization.Invalidate();201 this.graphVisualization.Controller.RunActivity("Standard TreeLayout");202 }271 //if (this.operatorShapeMapping.Count > 0 && this.connectionMapping.Count > 0) { //otherwise the layout does not work 272 this.graphVisualization.Controller.RunActivity("Standard TreeLayout"); 273 this.graphVisualization.Invalidate(); 274 //} 203 275 } 204 276 … … 221 293 internal void ActivateSelectTool() { 222 294 this.graphVisualization.Controller.ActivateTool(ControllerBase.SelectionToolName); 295 } 296 297 #endregion 298 299 #region drag and drop 300 private void OperatorGraphView_DragEnter(object sender, DragEventArgs e) { 301 e.Effect = DragDropEffects.None; 302 Type type = e.Data.GetData("Type") as Type; 303 if ((type != null) && (typeof(IOperator).IsAssignableFrom(type))) { 304 e.Effect = DragDropEffects.Copy; 305 } 306 } 307 308 private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) { 309 if (e.Effect != DragDropEffects.None) { 310 IOperator op = e.Data.GetData("Value") as IOperator; 311 IShapeInfo shapeInfo = ShapeInfoFactory.CreateShapeInfo(op); 312 Point controlCoordinates = this.PointToClient(new Point(e.X, e.Y)); 313 PointF viewCoordinates = this.graphVisualization.Controller.View.DeviceToView(controlCoordinates); 314 shapeInfo.Location = new Point((int)viewCoordinates.X, (int)viewCoordinates.Y); 315 this.VisualizationInfo.AddShapeInfo(op, shapeInfo); 316 } 223 317 } 224 318
Note: See TracChangeset
for help on using the changeset viewer.