Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 13.0 KB
RevLine 
[2801]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2801]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;
[4068]26using HeuristicLab.Core;
[2801]27using HeuristicLab.MainForm;
[4068]28using HeuristicLab.MainForm.WindowsForms;
[2801]29using Netron.Diagramming.Core;
30
[6036]31namespace HeuristicLab.Operators.Views.GraphVisualization.Views {
[2917]32  [View("OperatorGraph View (Chart)")]
[3361]33  [Content(typeof(OperatorGraph), true)]
[3514]34  public partial class OperatorGraphView : AsynchronousContentView {
[2853]35    public OperatorGraphView() {
[2801]36      InitializeComponent();
[4011]37
[2893]38      this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
[2898]39      this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
[3176]40      this.graphVisualizationInfoView.Controller.OnMouseDown += new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
[2901]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      }
[2801]45    }
46
[2853]47    public new OperatorGraph Content {
48      get { return (OperatorGraph)base.Content; }
[2801]49      set { base.Content = value; }
50    }
51
[2853]52    protected override void OnContentChanged() {
[4011]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;
[2898]64      }
[2801]65    }
66
[3904]67    protected override void SetEnabledStateOfControls() {
68      base.SetEnabledStateOfControls();
[3355]69      if (Content == null) {
70        selectButton.Enabled = false;
71        panButton.Enabled = false;
72        relayoutButton.Enabled = false;
[3523]73        zoomToFitButton.Enabled = false;
[3355]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;
[3523]83        zoomToFitButton.Enabled = true;
[3355]84        zoomInButton.Enabled = true;
85        zoomOutButton.Enabled = true;
86        screenshotButton.Enabled = true;
87        detailsViewHost.Enabled = true;
[4435]88        connectButton.Enabled = !ReadOnly;
[3355]89      }
[3514]90    }
[3355]91
[3386]92    private OperatorGraphVisualizationInfo VisualizationInfo {
93      get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
[2853]94      set { this.Content.VisualizationInfo = value; }
95    }
[2819]96
[2898]97    private void Controller_SelectionChanged(object sender, EventArgs e) {
[3514]98      this.detailsViewHost.ViewType = null;
99      this.detailsViewHost.Content = null;
100
[2895]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) {
[2934]105          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]106          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
107          this.detailsViewHost.Content = op;
[2895]108        }
109      }
[3514]110
[2898]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) {
[2934]120          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]121          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
122          if (connectorName != "Predecessor") {
123            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
[2949]124            this.detailsViewHost.ViewType = null;
[2898]125            this.detailsViewHost.Content = parameter;
126          }
127        }
128      }
[3514]129
[2895]130    }
131
[3176]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);
[3557]139            IContentView view = MainFormManager.MainForm.ShowContent(op);
[3423]140            if (view != null) {
141              view.ReadOnly = this.ReadOnly;
142              view.Locked = this.Locked;
143            }
[3176]144            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
145            if (eventArgs != null)
146              eventArgs.Handled = true;
147          }
148        }
149      }
150    }
151
[2893]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));
[2861]160      }
[2801]161    }
162
[2893]163    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
[2934]164      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]165      if (shapeInfo != null) {
166        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
167        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
[3514]168        this.initialToolStripMenuItem.Enabled = !ReadOnly && !Locked;
[2893]169        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
[5224]170        this.breakPointToolStripMenuItem.Enabled = !Locked;
[2853]171      }
[2801]172    }
173
[2893]174    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
[2934]175      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]176      if (shapeInfo != null) {
177        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
[3557]178        IContentView view = MainFormManager.MainForm.ShowContent(op);
179        if (view != null) {
180          view.ReadOnly = this.ReadOnly;
181          view.Locked = this.Locked;
182        }
[2853]183      }
[2801]184    }
[2819]185
[2893]186    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
[4011]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;
[2861]192    }
[2819]193
[2893]194    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
[4011]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      }
[2861]200    }
[2893]201    #endregion
[2819]202
[2853]203    #region drag and drop
[3556]204    private void OperatorGraphView_DragEnterOver(object sender, DragEventArgs e) {
[2853]205      e.Effect = DragDropEffects.None;
[5837]206      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOperator)) {
[3694]207        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3556]208        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]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;
[2853]212      }
213    }
214
215    private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
216      if (e.Effect != DragDropEffects.None) {
[5837]217        IOperator op = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IOperator;
[5744]218        if (e.Effect.HasFlag(DragDropEffects.Copy)) op = (IOperator)op.Clone();
[3386]219        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
[2893]220        Point mouse = new Point(MousePosition.X, MousePosition.Y);
[2934]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);
[2893]224
[2934]225        if (worldPoint.X < 0)
226          worldPoint.X = 0;
227        if (worldPoint.Y < 0)
228          worldPoint.Y = 0;
229
[2893]230        shapeInfo.Location = Point.Round(worldPoint);
[2853]231        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
232      }
233    }
234    #endregion
[2899]235
[2901]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:
[3523]261          button = this.zoomToFitButton;
[2901]262          break;
263      }
264      return button;
265    }
266
[2899]267    private void selectButton_Click(object sender, EventArgs e) {
268      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
[2901]269      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
[2899]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) {
[2934]285      this.graphVisualizationInfoView.Controller.View.ZoomFit();
[2899]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    }
[2934]295
296    private void screenshotButton_Click(object sender, EventArgs e) {
[3176]297      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
[2934]298      SaveFileDialog saveFileDialog = new SaveFileDialog();
299      saveFileDialog.Title = "Save Screenshot";
[3753]300      saveFileDialog.DefaultExt = "png";
301      saveFileDialog.Filter = "Portable Network Graphics|*.png|All Files|*.*";
[2934]302      saveFileDialog.FilterIndex = 1;
[3514]303      saveFileDialog.AddExtension = true;
[2934]304
305      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
306        bitmap.Save(saveFileDialog.FileName);
307      }
308    }
[2801]309  }
310}
Note: See TracBrowser for help on using the repository browser.