Free cookie consent management tool by TermsFeed Policy Generator

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

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

implemented open default view on double click (ticket #867)

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