Free cookie consent management tool by TermsFeed Policy Generator

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

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

smaller changes in OperatorGraphVisualization (ticket #867)

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