Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3623 was 3566, checked in by mkommend, 15 years ago

removed ctors with contents in all views (ticket #972)

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