Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphView.cs @ 2853

Last change on this file since 2853 was 2853, checked in by mkommend, 15 years ago

added first version of mapping for the graph visualization (ticket #867)

File size: 12.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
31using HeuristicLab.Core;
32using HeuristicLab.Core.Views;
33using Netron.Diagramming.Core;
34using HeuristicLab.Parameters;
35using HeuristicLab.MainForm.WindowsForms;
36
37namespace HeuristicLab.Operators.Views.GraphVisualization {
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() {
45      InitializeComponent();
46      this.causedUpdateOfShapeInfo = false;
47      Caption = "Operator Graph";
48      this.shapeMapping = new BidirectionalLookup<IShapeInfo, IShape>();
49      this.connectionMapping = new BidirectionalLookup<IConnectionInfo, IConnection>();
50    }
51
52    public OperatorGraphView(OperatorGraph content)
53      : this() {
54      this.Content = content;
55    }
56
57    public new OperatorGraph Content {
58      get { return (OperatorGraph)base.Content; }
59      set { base.Content = value; }
60    }
61
62    protected override void 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;
155      }
156    }
157
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);
163      }
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    //}
267
268    #region methods for toolbar items
269
270    internal void RelayoutOperatorGraph() {
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      //}
275    }
276
277    internal void ActivateZoomAreaTool() {
278      this.graphVisualization.Controller.ActivateTool(ControllerBase.ZoomAreaToolName);
279    }
280
281    internal void ActivateZoomInTool() {
282      this.graphVisualization.Controller.ActivateTool(ControllerBase.ZoomInToolName);
283    }
284
285    internal void ActivateZoomOutTool() {
286      this.graphVisualization.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
287    }
288
289    internal void ActivatePanTool() {
290      this.graphVisualization.Controller.ActivateTool(ControllerBase.PanToolName);
291    }
292
293    internal void ActivateSelectTool() {
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      }
317    }
318
319    #endregion
320  }
321}
Note: See TracBrowser for help on using the repository browser.