Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3001 was 2949, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments on version r2917.
File size: 10.7 KB
RevLine 
[2801]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;
[2819]34using HeuristicLab.Parameters;
[2853]35using HeuristicLab.MainForm.WindowsForms;
[2861]36using HeuristicLab.Collections;
[2801]37
38namespace HeuristicLab.Operators.Views.GraphVisualization {
[2917]39  [View("OperatorGraph View (Chart)")]
[2853]40  [Content(typeof(OperatorGraph), false)]
41  public partial class OperatorGraphView : ContentView {
42    public OperatorGraphView() {
[2801]43      InitializeComponent();
[2893]44      Caption = "Operator Graph Visualization";
45
46      this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
[2898]47      this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
[2901]48      foreach (ITool tool in this.graphVisualizationInfoView.Controller.Tools) {
49        tool.OnToolActivate += new EventHandler<ToolEventArgs>(tool_OnToolActivate);
50        tool.OnToolDeactivate += new EventHandler<ToolEventArgs>(tool_OnToolDeactivate);
51      }
[2801]52    }
53
[2853]54    public OperatorGraphView(OperatorGraph content)
[2801]55      : this() {
56      this.Content = content;
57    }
58
[2853]59    public new OperatorGraph Content {
60      get { return (OperatorGraph)base.Content; }
[2801]61      set { base.Content = value; }
62    }
63
[2853]64    protected override void OnContentChanged() {
[2898]65      bool createdVisualizationInfo = false;
66      if (this.VisualizationInfo == null) {
[2893]67        this.VisualizationInfo = new GraphVisualizationInfo(this.Content);
[2898]68        createdVisualizationInfo = true;
69      }
[2893]70      this.graphVisualizationInfoView.Content = this.VisualizationInfo;
[2898]71      if (createdVisualizationInfo)
[2899]72        this.graphVisualizationInfoView.RelayoutGraph();
[2801]73    }
74
[2893]75    private GraphVisualizationInfo VisualizationInfo {
76      get { return Content.VisualizationInfo as GraphVisualizationInfo; }
[2853]77      set { this.Content.VisualizationInfo = value; }
78    }
[2819]79
[2898]80    private void Controller_SelectionChanged(object sender, EventArgs e) {
[2895]81      CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
[2898]82      this.detailsViewHost.ViewType = null;
[2895]83      if (selectedObjects.Count == 1) {
84        IShape shape = selectedObjects[0] as IShape;
85        if (shape != null) {
[2934]86          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]87          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
[2949]88          this.detailsViewHost.ViewType = null;
[2898]89          this.detailsViewHost.Content = op;
[2895]90          return;
91        }
92      }
[2898]93      IConnector connector = this.graphVisualizationInfoView.Controller.Model.Selection.Connector;
94      if (connector != null) {
95        IShape shape = connector.Parent as IShape;
96        string connectorName = connector.Name;
97        if (shape == null) {
98          shape = connector.AttachedTo.Parent as IShape; //connection connector selected
99          connectorName = connector.AttachedTo.Name;
100        }
101        if (shape != null) {
[2934]102          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]103          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
104          if (connectorName != "Predecessor") {
105            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
[2949]106            this.detailsViewHost.ViewType = null;
[2898]107            this.detailsViewHost.Content = parameter;
108            return;
109          }
110        }
111      }
112      this.detailsViewHost.ViewType = null;
113      this.detailsViewHost.Content = null;
[2895]114    }
115
[2893]116    #region context menu
117    private void Controller_OnShowContextMenu(object sender, EntityMenuEventArgs e) {
118      IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.MouseEventArgs.Location);
119      if (shape != null) {
120        IShapeInfo shapeInfo = shape.Tag as IShapeInfo;
121        this.shapeContextMenu.Tag = shapeInfo;
122        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(e.MouseEventArgs.Location);
123        this.shapeContextMenu.Show(this, Point.Round(worldPoint));
[2861]124      }
[2801]125    }
126
[2893]127    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
[2934]128      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]129      if (shapeInfo != null) {
130        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
131        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
132        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
[2853]133      }
[2801]134    }
135
[2893]136    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
[2934]137      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]138      if (shapeInfo != null) {
139        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
140        MainFormManager.CreateDefaultView(op).Show();
[2853]141      }
[2801]142    }
[2819]143
[2893]144    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
[2934]145      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]146      if (this.VisualizationInfo.InitialShape == shapeInfo)
147        this.VisualizationInfo.InitialShape = null;
148      else
149        this.VisualizationInfo.InitialShape = shapeInfo;
[2861]150    }
[2819]151
[2893]152    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
[2934]153      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]154      if (shapeInfo != null) {
155        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
156        op.Breakpoint = !op.Breakpoint;
[2861]157      }
158    }
[2893]159    #endregion
[2819]160
[2853]161    #region drag and drop
162    private void OperatorGraphView_DragEnter(object sender, DragEventArgs e) {
163      e.Effect = DragDropEffects.None;
164      Type type = e.Data.GetData("Type") as Type;
165      if ((type != null) && (typeof(IOperator).IsAssignableFrom(type))) {
166        e.Effect = DragDropEffects.Copy;
167      }
168    }
169
170    private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
171      if (e.Effect != DragDropEffects.None) {
172        IOperator op = e.Data.GetData("Value") as IOperator;
[2934]173        IOperatorShapeInfo shapeInfo = Factory.CreateOperatorShapeInfo(op);
[2893]174        Point mouse = new Point(MousePosition.X, MousePosition.Y);
[2934]175        Point screen = this.graphVisualizationInfoView.PointToScreen(new Point(0, 0));
176        Point control = new Point(mouse.X - screen.X, mouse.Y - screen.Y);
177        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.ViewToWorld(control);
[2893]178
[2934]179        if (worldPoint.X < 0)
180          worldPoint.X = 0;
181        if (worldPoint.Y < 0)
182          worldPoint.Y = 0;
183
[2893]184        shapeInfo.Location = Point.Round(worldPoint);
[2853]185        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
186      }
187    }
188    #endregion
[2899]189
[2901]190    private void tool_OnToolActivate(object sender, ToolEventArgs e) {
191      Button button = GetButtonForTool(e.Properties.Name);
192      if (button != null)
193        button.Enabled = false;
194    }
195
196    private void tool_OnToolDeactivate(object sender, ToolEventArgs e) {
197      Button button = GetButtonForTool(e.Properties.Name);
198      if (button != null)
199        button.Enabled = true;
200    }
201
202    private Button GetButtonForTool(string toolName) {
203      Button button = null;
204      switch (toolName) {
205        case ControllerBase.SelectionToolName:
206          button = this.selectButton;
207          break;
208        case ControllerBase.PanToolName:
209          button = this.panButton;
210          break;
211        case ControllerBase.ConnectionToolName:
212          button = this.connectButton;
213          break;
214        case ControllerBase.ZoomAreaToolName:
215          button = this.zoomAreaButton;
216          break;
217      }
218      return button;
219    }
220
[2899]221    private void selectButton_Click(object sender, EventArgs e) {
222      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
223      tool.IsSuspended = false;
[2901]224      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
[2899]225    }
226
227    private void panButton_Click(object sender, EventArgs e) {
228      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.PanToolName);
229    }
230
231    private void connectButton_Click(object sender, EventArgs e) {
232      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
233    }
234
235    private void relayoutButton_Click(object sender, EventArgs e) {
236      this.graphVisualizationInfoView.RelayoutGraph();
237    }
238
239    private void zoomAreaButton_Click(object sender, EventArgs e) {
[2934]240      this.graphVisualizationInfoView.Controller.View.ZoomFit();
[2899]241    }
242
243    private void zoomInButton_Click(object sender, EventArgs e) {
244      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
245    }
246
247    private void zoomOutButton_Click(object sender, EventArgs e) {
248      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
249    }
[2934]250
251    private void screenshotButton_Click(object sender, EventArgs e) {
252      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables),this.graphVisualizationInfoView.Controller.View.Graphics);
253      SaveFileDialog saveFileDialog = new SaveFileDialog();
254      saveFileDialog.Title = "Save Screenshot";
255      saveFileDialog.DefaultExt = "bmp";
256      saveFileDialog.Filter = "Bitmap|*.bmp|All Files|*.*";
257      saveFileDialog.FilterIndex = 1;
258
259      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
260        bitmap.Save(saveFileDialog.FileName);
261      }
262    }
263
[2801]264  }
265}
Note: See TracBrowser for help on using the repository browser.