Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization/OperatorGraphView.cs @ 6368

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

fixed context menu (ticket #867)

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