Free cookie consent management tool by TermsFeed Policy Generator

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

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

fixed context menu (ticket #867)

File size: 13.4 KB
RevLine 
[2801]1#region License Information
2/* HeuristicLab
[3742]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2801]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;
[2819]34using HeuristicLab.Parameters;
[2853]35using HeuristicLab.MainForm.WindowsForms;
[2861]36using HeuristicLab.Collections;
[2801]37
38namespace HeuristicLab.Operators.Views.GraphVisualization {
[2917]39  [View("OperatorGraph View (Chart)")]
[3361]40  [Content(typeof(OperatorGraph), true)]
[3514]41  public partial class OperatorGraphView : AsynchronousContentView {
[2853]42    public OperatorGraphView() {
[2801]43      InitializeComponent();
[3764]44 
[2893]45      this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
[2898]46      this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
[3176]47      this.graphVisualizationInfoView.Controller.OnMouseDown += new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
[2901]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      }
[2801]52    }
53
[2853]54    public new OperatorGraph Content {
55      get { return (OperatorGraph)base.Content; }
[2801]56      set { base.Content = value; }
57    }
58
[2853]59    protected override void OnContentChanged() {
[2898]60      bool createdVisualizationInfo = false;
61      if (this.VisualizationInfo == null) {
[3386]62        this.VisualizationInfo = new OperatorGraphVisualizationInfo(this.Content);
[2898]63        createdVisualizationInfo = true;
64      }
[2893]65      this.graphVisualizationInfoView.Content = this.VisualizationInfo;
[2898]66      if (createdVisualizationInfo)
[2899]67        this.graphVisualizationInfoView.RelayoutGraph();
[3355]68
[3362]69      this.SetEnabledStateOfControls();
[2801]70    }
71
[3355]72    protected override void OnReadOnlyChanged() {
73      base.OnReadOnlyChanged();
[3362]74      this.SetEnabledStateOfControls();
[3355]75    }
76
[3514]77    protected override void OnLockedChanged() {
78      base.OnLockedChanged();
79      this.SetEnabledStateOfControls();
80    }
81
[3362]82    private void SetEnabledStateOfControls() {
[3355]83      if (Content == null) {
84        selectButton.Enabled = false;
85        panButton.Enabled = false;
86        relayoutButton.Enabled = false;
[3523]87        zoomToFitButton.Enabled = false;
[3355]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;
[3523]97        zoomToFitButton.Enabled = true;
[3355]98        zoomInButton.Enabled = true;
99        zoomOutButton.Enabled = true;
100        screenshotButton.Enabled = true;
101        detailsViewHost.Enabled = true;
[3514]102        connectButton.Enabled = !ReadOnly && !Locked;
[3355]103      }
[3514]104    }
[3355]105
[3386]106    private OperatorGraphVisualizationInfo VisualizationInfo {
107      get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
[2853]108      set { this.Content.VisualizationInfo = value; }
109    }
[2819]110
[2898]111    private void Controller_SelectionChanged(object sender, EventArgs e) {
[3514]112      this.detailsViewHost.ViewType = null;
113      this.detailsViewHost.Content = null;
114
[2895]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) {
[2934]119          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]120          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
121          this.detailsViewHost.Content = op;
[2895]122        }
123      }
[3514]124
[2898]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) {
[2934]134          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]135          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
136          if (connectorName != "Predecessor") {
137            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
[2949]138            this.detailsViewHost.ViewType = null;
[2898]139            this.detailsViewHost.Content = parameter;
140          }
141        }
142      }
[3514]143
[2895]144    }
145
[3176]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);
[3557]153            IContentView view = MainFormManager.MainForm.ShowContent(op);
[3423]154            if (view != null) {
155              view.ReadOnly = this.ReadOnly;
156              view.Locked = this.Locked;
157            }
[3176]158            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
159            if (eventArgs != null)
160              eventArgs.Handled = true;
161          }
162        }
163      }
164    }
165
[2893]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));
[2861]174      }
[2801]175    }
176
[2893]177    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
[2934]178      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]179      if (shapeInfo != null) {
180        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
181        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
[3514]182        this.initialToolStripMenuItem.Enabled = !ReadOnly && !Locked;
[2893]183        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
[3765]184        this.breakPointToolStripMenuItem.Enabled = !Locked;
[2853]185      }
[2801]186    }
187
[2893]188    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
[2934]189      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]190      if (shapeInfo != null) {
191        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
[3557]192        IContentView view = MainFormManager.MainForm.ShowContent(op);
193        if (view != null) {
194          view.ReadOnly = this.ReadOnly;
195          view.Locked = this.Locked;
196        }
[2853]197      }
[2801]198    }
[2819]199
[2893]200    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
[3355]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;
[2861]206    }
[2819]207
[2893]208    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
[3355]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        }
[2861]214    }
[2893]215    #endregion
[2819]216
[2853]217    #region drag and drop
[3556]218    private void OperatorGraphView_DragEnterOver(object sender, DragEventArgs e) {
[2853]219      e.Effect = DragDropEffects.None;
220      Type type = e.Data.GetData("Type") as Type;
[3556]221      if (!ReadOnly && (type != null) && (typeof(IOperator).IsAssignableFrom(type))) {
[3694]222        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3556]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;
[2853]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;
[3556]233        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) op = (IOperator)op.Clone();
[3386]234        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
[2893]235        Point mouse = new Point(MousePosition.X, MousePosition.Y);
[2934]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);
[2893]239
[2934]240        if (worldPoint.X < 0)
241          worldPoint.X = 0;
242        if (worldPoint.Y < 0)
243          worldPoint.Y = 0;
244
[2893]245        shapeInfo.Location = Point.Round(worldPoint);
[2853]246        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
247      }
248    }
249    #endregion
[2899]250
[2901]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:
[3523]276          button = this.zoomToFitButton;
[2901]277          break;
278      }
279      return button;
280    }
281
[2899]282    private void selectButton_Click(object sender, EventArgs e) {
283      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
[2901]284      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
[2899]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) {
[2934]300      this.graphVisualizationInfoView.Controller.View.ZoomFit();
[2899]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    }
[2934]310
311    private void screenshotButton_Click(object sender, EventArgs e) {
[3176]312      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
[2934]313      SaveFileDialog saveFileDialog = new SaveFileDialog();
314      saveFileDialog.Title = "Save Screenshot";
[3753]315      saveFileDialog.DefaultExt = "png";
316      saveFileDialog.Filter = "Portable Network Graphics|*.png|All Files|*.*";
[2934]317      saveFileDialog.FilterIndex = 1;
[3514]318      saveFileDialog.AddExtension = true;
[2934]319
320      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
321        bitmap.Save(saveFileDialog.FileName);
322      }
323    }
[2801]324  }
325}
Note: See TracBrowser for help on using the repository browser.