Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • implemented reviewers' comments on version r2917.
File size: 10.7 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;
36using HeuristicLab.Collections;
37
38namespace HeuristicLab.Operators.Views.GraphVisualization {
39  [View("OperatorGraph View (Chart)")]
40  [Content(typeof(OperatorGraph), false)]
41  public partial class OperatorGraphView : ContentView {
42    public OperatorGraphView() {
43      InitializeComponent();
44      Caption = "Operator Graph Visualization";
45
46      this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
47      this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
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      }
52    }
53
54    public OperatorGraphView(OperatorGraph content)
55      : this() {
56      this.Content = content;
57    }
58
59    public new OperatorGraph Content {
60      get { return (OperatorGraph)base.Content; }
61      set { base.Content = value; }
62    }
63
64    protected override void OnContentChanged() {
65      bool createdVisualizationInfo = false;
66      if (this.VisualizationInfo == null) {
67        this.VisualizationInfo = new GraphVisualizationInfo(this.Content);
68        createdVisualizationInfo = true;
69      }
70      this.graphVisualizationInfoView.Content = this.VisualizationInfo;
71      if (createdVisualizationInfo)
72        this.graphVisualizationInfoView.RelayoutGraph();
73    }
74
75    private GraphVisualizationInfo VisualizationInfo {
76      get { return Content.VisualizationInfo as GraphVisualizationInfo; }
77      set { this.Content.VisualizationInfo = value; }
78    }
79
80    private void Controller_SelectionChanged(object sender, EventArgs e) {
81      CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
82      this.detailsViewHost.ViewType = null;
83      if (selectedObjects.Count == 1) {
84        IShape shape = selectedObjects[0] as IShape;
85        if (shape != null) {
86          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
87          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
88          this.detailsViewHost.ViewType = null;
89          this.detailsViewHost.Content = op;
90          return;
91        }
92      }
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) {
102          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
103          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
104          if (connectorName != "Predecessor") {
105            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
106            this.detailsViewHost.ViewType = null;
107            this.detailsViewHost.Content = parameter;
108            return;
109          }
110        }
111      }
112      this.detailsViewHost.ViewType = null;
113      this.detailsViewHost.Content = null;
114    }
115
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));
124      }
125    }
126
127    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
128      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
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;
133      }
134    }
135
136    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
137      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
138      if (shapeInfo != null) {
139        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
140        MainFormManager.CreateDefaultView(op).Show();
141      }
142    }
143
144    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
145      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
146      if (this.VisualizationInfo.InitialShape == shapeInfo)
147        this.VisualizationInfo.InitialShape = null;
148      else
149        this.VisualizationInfo.InitialShape = shapeInfo;
150    }
151
152    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
153      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
154      if (shapeInfo != null) {
155        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
156        op.Breakpoint = !op.Breakpoint;
157      }
158    }
159    #endregion
160
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;
173        IOperatorShapeInfo shapeInfo = Factory.CreateOperatorShapeInfo(op);
174        Point mouse = new Point(MousePosition.X, MousePosition.Y);
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);
178
179        if (worldPoint.X < 0)
180          worldPoint.X = 0;
181        if (worldPoint.Y < 0)
182          worldPoint.Y = 0;
183
184        shapeInfo.Location = Point.Round(worldPoint);
185        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
186      }
187    }
188    #endregion
189
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
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;
224      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
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) {
240      this.graphVisualizationInfoView.Controller.View.ZoomFit();
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    }
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
264  }
265}
Note: See TracBrowser for help on using the repository browser.