Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4047 was 4011, checked in by mkommend, 14 years ago
  • refactored ViewHost and various views to use fewer nested controls
  • added UnitTests for ContentViews to ensure proper using of the ContentAttribute
  • fixed some views which could not handle null as Content

(ticket #972)

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