Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization/OperatorGraphView.cs @ 13042

Last change on this file since 13042 was 4448, checked in by mkommend, 14 years ago

Enabled breakpoints in the OperatorGraphView although it is locked (ticket #1155).

File size: 13.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using Netron.Diagramming.Core;
30
31namespace HeuristicLab.Operators.Views.GraphVisualization {
32  [View("OperatorGraph View (Chart)")]
33  [Content(typeof(OperatorGraph), true)]
34  public partial class OperatorGraphView : AsynchronousContentView {
35    public OperatorGraphView() {
36      InitializeComponent();
37
38      this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
39      this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
40      this.graphVisualizationInfoView.Controller.OnMouseDown += new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
41      foreach (ITool tool in this.graphVisualizationInfoView.Controller.Tools) {
42        tool.OnToolActivate += new EventHandler<ToolEventArgs>(tool_OnToolActivate);
43        tool.OnToolDeactivate += new EventHandler<ToolEventArgs>(tool_OnToolDeactivate);
44      }
45    }
46
47    public new OperatorGraph Content {
48      get { return (OperatorGraph)base.Content; }
49      set { base.Content = value; }
50    }
51
52    protected override void OnContentChanged() {
53      if (Content != null) {
54        bool createdVisualizationInfo = false;
55        if (this.VisualizationInfo == null) {
56          this.VisualizationInfo = new OperatorGraphVisualizationInfo(this.Content);
57          createdVisualizationInfo = true;
58        }
59        this.graphVisualizationInfoView.Content = this.VisualizationInfo;
60        if (createdVisualizationInfo)
61          this.graphVisualizationInfoView.RelayoutGraph();
62      } else {
63        this.graphVisualizationInfoView.Content = null;
64      }
65    }
66
67    protected override void SetEnabledStateOfControls() {
68      base.SetEnabledStateOfControls();
69      if (Content == null) {
70        selectButton.Enabled = false;
71        panButton.Enabled = false;
72        relayoutButton.Enabled = false;
73        zoomToFitButton.Enabled = false;
74        zoomInButton.Enabled = false;
75        zoomOutButton.Enabled = false;
76        screenshotButton.Enabled = false;
77        detailsViewHost.Enabled = false;
78        connectButton.Enabled = false;
79      } else {
80        selectButton.Enabled = true;
81        panButton.Enabled = true;
82        relayoutButton.Enabled = true;
83        zoomToFitButton.Enabled = true;
84        zoomInButton.Enabled = true;
85        zoomOutButton.Enabled = true;
86        screenshotButton.Enabled = true;
87        detailsViewHost.Enabled = true;
88        connectButton.Enabled = !ReadOnly;
89      }
90    }
91
92    private OperatorGraphVisualizationInfo VisualizationInfo {
93      get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
94      set { this.Content.VisualizationInfo = value; }
95    }
96
97    private void Controller_SelectionChanged(object sender, EventArgs e) {
98      this.detailsViewHost.ViewType = null;
99      this.detailsViewHost.Content = null;
100
101      CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
102      if (selectedObjects.Count == 1) {
103        IShape shape = selectedObjects[0] as IShape;
104        if (shape != null) {
105          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
106          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
107          this.detailsViewHost.Content = op;
108        }
109      }
110
111      IConnector connector = this.graphVisualizationInfoView.Controller.Model.Selection.Connector;
112      if (connector != null) {
113        IShape shape = connector.Parent as IShape;
114        string connectorName = connector.Name;
115        if (shape == null) {
116          shape = connector.AttachedTo.Parent as IShape; //connection connector selected
117          connectorName = connector.AttachedTo.Name;
118        }
119        if (shape != null) {
120          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
121          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
122          if (connectorName != "Predecessor") {
123            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
124            this.detailsViewHost.ViewType = null;
125            this.detailsViewHost.Content = parameter;
126          }
127        }
128      }
129
130    }
131
132    private void Controller_OnMouseDown(object sender, MouseEventArgs e) {
133      if (e.Clicks >= 2) {
134        IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.Location);
135        if (shape != null) {
136          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
137          if (shapeInfo != null) {
138            IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
139            IContentView view = MainFormManager.MainForm.ShowContent(op);
140            if (view != null) {
141              view.ReadOnly = this.ReadOnly;
142              view.Locked = this.Locked;
143            }
144            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
145            if (eventArgs != null)
146              eventArgs.Handled = true;
147          }
148        }
149      }
150    }
151
152    #region context menu
153    private void Controller_OnShowContextMenu(object sender, EntityMenuEventArgs e) {
154      IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.MouseEventArgs.Location);
155      if (shape != null) {
156        IShapeInfo shapeInfo = shape.Tag as IShapeInfo;
157        this.shapeContextMenu.Tag = shapeInfo;
158        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(e.MouseEventArgs.Location);
159        this.shapeContextMenu.Show(this, Point.Round(worldPoint));
160      }
161    }
162
163    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
164      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
165      if (shapeInfo != null) {
166        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
167        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
168        this.initialToolStripMenuItem.Enabled = !ReadOnly && !Locked;
169        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
170        this.breakPointToolStripMenuItem.Enabled = !ReadOnly;
171      }
172    }
173
174    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
175      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
176      if (shapeInfo != null) {
177        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
178        IContentView view = MainFormManager.MainForm.ShowContent(op);
179        if (view != null) {
180          view.ReadOnly = this.ReadOnly;
181          view.Locked = this.Locked;
182        }
183      }
184    }
185
186    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
187      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
188      if (this.VisualizationInfo.InitialShape == shapeInfo)
189        this.VisualizationInfo.InitialShape = null;
190      else
191        this.VisualizationInfo.InitialShape = shapeInfo;
192    }
193
194    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
195      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
196      if (shapeInfo != null) {
197        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
198        op.Breakpoint = !op.Breakpoint;
199      }
200    }
201    #endregion
202
203    #region drag and drop
204    private void OperatorGraphView_DragEnterOver(object sender, DragEventArgs e) {
205      e.Effect = DragDropEffects.None;
206      Type type = e.Data.GetData("Type") as Type;
207      if (!ReadOnly && (type != null) && (typeof(IOperator).IsAssignableFrom(type))) {
208        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
209        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
210        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
211        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
212        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
213      }
214    }
215
216    private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
217      if (e.Effect != DragDropEffects.None) {
218        IOperator op = e.Data.GetData("Value") as IOperator;
219        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) op = (IOperator)op.Clone();
220        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
221        Point mouse = new Point(MousePosition.X, MousePosition.Y);
222        Point screen = this.graphVisualizationInfoView.PointToScreen(new Point(0, 0));
223        Point control = new Point(mouse.X - screen.X, mouse.Y - screen.Y);
224        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.ViewToWorld(control);
225
226        if (worldPoint.X < 0)
227          worldPoint.X = 0;
228        if (worldPoint.Y < 0)
229          worldPoint.Y = 0;
230
231        shapeInfo.Location = Point.Round(worldPoint);
232        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
233      }
234    }
235    #endregion
236
237    private void tool_OnToolActivate(object sender, ToolEventArgs e) {
238      Button button = GetButtonForTool(e.Properties.Name);
239      if (button != null)
240        button.Enabled = false;
241    }
242
243    private void tool_OnToolDeactivate(object sender, ToolEventArgs e) {
244      Button button = GetButtonForTool(e.Properties.Name);
245      if (button != null)
246        button.Enabled = true;
247    }
248
249    private Button GetButtonForTool(string toolName) {
250      Button button = null;
251      switch (toolName) {
252        case ControllerBase.SelectionToolName:
253          button = this.selectButton;
254          break;
255        case ControllerBase.PanToolName:
256          button = this.panButton;
257          break;
258        case ControllerBase.ConnectionToolName:
259          button = this.connectButton;
260          break;
261        case ControllerBase.ZoomAreaToolName:
262          button = this.zoomToFitButton;
263          break;
264      }
265      return button;
266    }
267
268    private void selectButton_Click(object sender, EventArgs e) {
269      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
270      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
271    }
272
273    private void panButton_Click(object sender, EventArgs e) {
274      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.PanToolName);
275    }
276
277    private void connectButton_Click(object sender, EventArgs e) {
278      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
279    }
280
281    private void relayoutButton_Click(object sender, EventArgs e) {
282      this.graphVisualizationInfoView.RelayoutGraph();
283    }
284
285    private void zoomAreaButton_Click(object sender, EventArgs e) {
286      this.graphVisualizationInfoView.Controller.View.ZoomFit();
287    }
288
289    private void zoomInButton_Click(object sender, EventArgs e) {
290      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
291    }
292
293    private void zoomOutButton_Click(object sender, EventArgs e) {
294      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
295    }
296
297    private void screenshotButton_Click(object sender, EventArgs e) {
298      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
299      SaveFileDialog saveFileDialog = new SaveFileDialog();
300      saveFileDialog.Title = "Save Screenshot";
301      saveFileDialog.DefaultExt = "png";
302      saveFileDialog.Filter = "Portable Network Graphics|*.png|All Files|*.*";
303      saveFileDialog.FilterIndex = 1;
304      saveFileDialog.AddExtension = true;
305
306      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
307        bitmap.Save(saveFileDialog.FileName);
308      }
309    }
310  }
311}
Note: See TracBrowser for help on using the repository browser.