Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Operators.Views.GraphVisualization.Views/3.3/OperatorGraphView.cs @ 7481

Last change on this file since 7481 was 6036, checked in by mkommend, 14 years ago

#1486: Decoupled operator graph visualization content classes from view classes.

File size: 13.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Views {
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 = !Locked;
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      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOperator)) {
207        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
208        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
209        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
210        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
211        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
212      }
213    }
214
215    private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
216      if (e.Effect != DragDropEffects.None) {
217        IOperator op = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IOperator;
218        if (e.Effect.HasFlag(DragDropEffects.Copy)) op = (IOperator)op.Clone();
219        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
220        Point mouse = new Point(MousePosition.X, MousePosition.Y);
221        Point screen = this.graphVisualizationInfoView.PointToScreen(new Point(0, 0));
222        Point control = new Point(mouse.X - screen.X, mouse.Y - screen.Y);
223        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.ViewToWorld(control);
224
225        if (worldPoint.X < 0)
226          worldPoint.X = 0;
227        if (worldPoint.Y < 0)
228          worldPoint.Y = 0;
229
230        shapeInfo.Location = Point.Round(worldPoint);
231        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
232      }
233    }
234    #endregion
235
236    private void tool_OnToolActivate(object sender, ToolEventArgs e) {
237      Button button = GetButtonForTool(e.Properties.Name);
238      if (button != null)
239        button.Enabled = false;
240    }
241
242    private void tool_OnToolDeactivate(object sender, ToolEventArgs e) {
243      Button button = GetButtonForTool(e.Properties.Name);
244      if (button != null)
245        button.Enabled = true;
246    }
247
248    private Button GetButtonForTool(string toolName) {
249      Button button = null;
250      switch (toolName) {
251        case ControllerBase.SelectionToolName:
252          button = this.selectButton;
253          break;
254        case ControllerBase.PanToolName:
255          button = this.panButton;
256          break;
257        case ControllerBase.ConnectionToolName:
258          button = this.connectButton;
259          break;
260        case ControllerBase.ZoomAreaToolName:
261          button = this.zoomToFitButton;
262          break;
263      }
264      return button;
265    }
266
267    private void selectButton_Click(object sender, EventArgs e) {
268      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
269      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
270    }
271
272    private void panButton_Click(object sender, EventArgs e) {
273      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.PanToolName);
274    }
275
276    private void connectButton_Click(object sender, EventArgs e) {
277      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
278    }
279
280    private void relayoutButton_Click(object sender, EventArgs e) {
281      this.graphVisualizationInfoView.RelayoutGraph();
282    }
283
284    private void zoomAreaButton_Click(object sender, EventArgs e) {
285      this.graphVisualizationInfoView.Controller.View.ZoomFit();
286    }
287
288    private void zoomInButton_Click(object sender, EventArgs e) {
289      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
290    }
291
292    private void zoomOutButton_Click(object sender, EventArgs e) {
293      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
294    }
295
296    private void screenshotButton_Click(object sender, EventArgs e) {
297      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
298      SaveFileDialog saveFileDialog = new SaveFileDialog();
299      saveFileDialog.Title = "Save Screenshot";
300      saveFileDialog.DefaultExt = "png";
301      saveFileDialog.Filter = "Portable Network Graphics|*.png|All Files|*.*";
302      saveFileDialog.FilterIndex = 1;
303      saveFileDialog.AddExtension = true;
304
305      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
306        bitmap.Save(saveFileDialog.FileName);
307      }
308    }
309  }
310}
Note: See TracBrowser for help on using the repository browser.