Free cookie consent management tool by TermsFeed Policy Generator

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

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

completly refactored the graph visualization (ticket #867)

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