Free cookie consent management tool by TermsFeed Policy Generator

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

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

changed logic of showing new views (ticket #972)

File size: 13.6 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.MainForm.ShowContent(op);
160            if (view != null) {
161              view.ReadOnly = this.ReadOnly;
162              view.Locked = this.Locked;
163            }
164            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
165            if (eventArgs != null)
166              eventArgs.Handled = true;
167          }
168        }
169      }
170    }
171
172    #region context menu
173    private void Controller_OnShowContextMenu(object sender, EntityMenuEventArgs e) {
174      IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.MouseEventArgs.Location);
175      if (shape != null) {
176        IShapeInfo shapeInfo = shape.Tag as IShapeInfo;
177        this.shapeContextMenu.Tag = shapeInfo;
178        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(e.MouseEventArgs.Location);
179        this.shapeContextMenu.Show(this, Point.Round(worldPoint));
180      }
181    }
182
183    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
184      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
185      if (shapeInfo != null) {
186        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
187        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
188        this.initialToolStripMenuItem.Enabled = !ReadOnly && !Locked;
189        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
190        this.breakPointToolStripMenuItem.Enabled = !ReadOnly && !Locked;
191      }
192    }
193
194    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
195      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
196      if (shapeInfo != null) {
197        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
198        IContentView view = MainFormManager.MainForm.ShowContent(op);
199        if (view != null) {
200          view.ReadOnly = this.ReadOnly;
201          view.Locked = this.Locked;
202        }
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_DragEnterOver(object sender, DragEventArgs e) {
229      e.Effect = DragDropEffects.None;
230      Type type = e.Data.GetData("Type") as Type;
231      if (!ReadOnly && (type != null) && (typeof(IOperator).IsAssignableFrom(type))) {
232        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Link;  // CTRL key       
233        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
234        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
235        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
236        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
237      }
238    }
239
240    private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
241      if (e.Effect != DragDropEffects.None) {
242        IOperator op = e.Data.GetData("Value") as IOperator;
243        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) op = (IOperator)op.Clone();
244        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
245        Point mouse = new Point(MousePosition.X, MousePosition.Y);
246        Point screen = this.graphVisualizationInfoView.PointToScreen(new Point(0, 0));
247        Point control = new Point(mouse.X - screen.X, mouse.Y - screen.Y);
248        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.ViewToWorld(control);
249
250        if (worldPoint.X < 0)
251          worldPoint.X = 0;
252        if (worldPoint.Y < 0)
253          worldPoint.Y = 0;
254
255        shapeInfo.Location = Point.Round(worldPoint);
256        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
257      }
258    }
259    #endregion
260
261    private void tool_OnToolActivate(object sender, ToolEventArgs e) {
262      Button button = GetButtonForTool(e.Properties.Name);
263      if (button != null)
264        button.Enabled = false;
265    }
266
267    private void tool_OnToolDeactivate(object sender, ToolEventArgs e) {
268      Button button = GetButtonForTool(e.Properties.Name);
269      if (button != null)
270        button.Enabled = true;
271    }
272
273    private Button GetButtonForTool(string toolName) {
274      Button button = null;
275      switch (toolName) {
276        case ControllerBase.SelectionToolName:
277          button = this.selectButton;
278          break;
279        case ControllerBase.PanToolName:
280          button = this.panButton;
281          break;
282        case ControllerBase.ConnectionToolName:
283          button = this.connectButton;
284          break;
285        case ControllerBase.ZoomAreaToolName:
286          button = this.zoomToFitButton;
287          break;
288      }
289      return button;
290    }
291
292    private void selectButton_Click(object sender, EventArgs e) {
293      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
294      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
295    }
296
297    private void panButton_Click(object sender, EventArgs e) {
298      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.PanToolName);
299    }
300
301    private void connectButton_Click(object sender, EventArgs e) {
302      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
303    }
304
305    private void relayoutButton_Click(object sender, EventArgs e) {
306      this.graphVisualizationInfoView.RelayoutGraph();
307    }
308
309    private void zoomAreaButton_Click(object sender, EventArgs e) {
310      this.graphVisualizationInfoView.Controller.View.ZoomFit();
311    }
312
313    private void zoomInButton_Click(object sender, EventArgs e) {
314      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
315    }
316
317    private void zoomOutButton_Click(object sender, EventArgs e) {
318      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
319    }
320
321    private void screenshotButton_Click(object sender, EventArgs e) {
322      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
323      SaveFileDialog saveFileDialog = new SaveFileDialog();
324      saveFileDialog.Title = "Save Screenshot";
325      saveFileDialog.DefaultExt = "bmp";
326      saveFileDialog.Filter = "Bitmap|*.bmp|All Files|*.*";
327      saveFileDialog.FilterIndex = 1;
328      saveFileDialog.AddExtension = true;
329
330      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
331        bitmap.Save(saveFileDialog.FileName);
332      }
333    }
334  }
335}
Note: See TracBrowser for help on using the repository browser.