Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3742 was 3742, checked in by gkronber, 14 years ago

Fixed GPL license headers and deleted files which are not referenced by projects. #893

File size: 13.5 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();
[2893]44      Caption = "Operator Graph Visualization";
45
46      this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
[2898]47      this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
[3176]48      this.graphVisualizationInfoView.Controller.OnMouseDown += new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
[2901]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      }
[2801]53    }
54
[2853]55    public new OperatorGraph Content {
56      get { return (OperatorGraph)base.Content; }
[2801]57      set { base.Content = value; }
58    }
59
[2853]60    protected override void OnContentChanged() {
[2898]61      bool createdVisualizationInfo = false;
62      if (this.VisualizationInfo == null) {
[3386]63        this.VisualizationInfo = new OperatorGraphVisualizationInfo(this.Content);
[2898]64        createdVisualizationInfo = true;
65      }
[2893]66      this.graphVisualizationInfoView.Content = this.VisualizationInfo;
[2898]67      if (createdVisualizationInfo)
[2899]68        this.graphVisualizationInfoView.RelayoutGraph();
[3355]69
[3362]70      this.SetEnabledStateOfControls();
[2801]71    }
72
[3355]73    protected override void OnReadOnlyChanged() {
74      base.OnReadOnlyChanged();
[3362]75      this.SetEnabledStateOfControls();
[3355]76    }
77
[3514]78    protected override void OnLockedChanged() {
79      base.OnLockedChanged();
80      this.SetEnabledStateOfControls();
81    }
82
[3362]83    private void SetEnabledStateOfControls() {
[3355]84      if (Content == null) {
85        selectButton.Enabled = false;
86        panButton.Enabled = false;
87        relayoutButton.Enabled = false;
[3523]88        zoomToFitButton.Enabled = false;
[3355]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;
[3523]98        zoomToFitButton.Enabled = true;
[3355]99        zoomInButton.Enabled = true;
100        zoomOutButton.Enabled = true;
101        screenshotButton.Enabled = true;
102        detailsViewHost.Enabled = true;
[3514]103        connectButton.Enabled = !ReadOnly && !Locked;
[3355]104      }
[3514]105    }
[3355]106
[3386]107    private OperatorGraphVisualizationInfo VisualizationInfo {
108      get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
[2853]109      set { this.Content.VisualizationInfo = value; }
110    }
[2819]111
[2898]112    private void Controller_SelectionChanged(object sender, EventArgs e) {
[3514]113      this.detailsViewHost.ViewType = null;
114      this.detailsViewHost.Content = null;
115
[2895]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) {
[2934]120          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]121          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
122          this.detailsViewHost.Content = op;
[2895]123        }
124      }
[3514]125
[2898]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) {
[2934]135          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]136          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
137          if (connectorName != "Predecessor") {
138            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
[2949]139            this.detailsViewHost.ViewType = null;
[2898]140            this.detailsViewHost.Content = parameter;
141          }
142        }
143      }
[3514]144
[2895]145    }
146
[3176]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);
[3557]154            IContentView view = MainFormManager.MainForm.ShowContent(op);
[3423]155            if (view != null) {
156              view.ReadOnly = this.ReadOnly;
157              view.Locked = this.Locked;
158            }
[3176]159            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
160            if (eventArgs != null)
161              eventArgs.Handled = true;
162          }
163        }
164      }
165    }
166
[2893]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));
[2861]175      }
[2801]176    }
177
[2893]178    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
[2934]179      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]180      if (shapeInfo != null) {
181        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
182        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
[3514]183        this.initialToolStripMenuItem.Enabled = !ReadOnly && !Locked;
[2893]184        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
[3514]185        this.breakPointToolStripMenuItem.Enabled = !ReadOnly && !Locked;
[2853]186      }
[2801]187    }
188
[2893]189    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
[2934]190      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]191      if (shapeInfo != null) {
192        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
[3557]193        IContentView view = MainFormManager.MainForm.ShowContent(op);
194        if (view != null) {
195          view.ReadOnly = this.ReadOnly;
196          view.Locked = this.Locked;
197        }
[2853]198      }
[2801]199    }
[2819]200
[2893]201    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
[3355]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      }
[2861]209    }
[2819]210
[2893]211    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
[3355]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        }
[2861]218      }
219    }
[2893]220    #endregion
[2819]221
[2853]222    #region drag and drop
[3556]223    private void OperatorGraphView_DragEnterOver(object sender, DragEventArgs e) {
[2853]224      e.Effect = DragDropEffects.None;
225      Type type = e.Data.GetData("Type") as Type;
[3556]226      if (!ReadOnly && (type != null) && (typeof(IOperator).IsAssignableFrom(type))) {
[3694]227        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3556]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;
[2853]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;
[3556]238        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) op = (IOperator)op.Clone();
[3386]239        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
[2893]240        Point mouse = new Point(MousePosition.X, MousePosition.Y);
[2934]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);
[2893]244
[2934]245        if (worldPoint.X < 0)
246          worldPoint.X = 0;
247        if (worldPoint.Y < 0)
248          worldPoint.Y = 0;
249
[2893]250        shapeInfo.Location = Point.Round(worldPoint);
[2853]251        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
252      }
253    }
254    #endregion
[2899]255
[2901]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:
[3523]281          button = this.zoomToFitButton;
[2901]282          break;
283      }
284      return button;
285    }
286
[2899]287    private void selectButton_Click(object sender, EventArgs e) {
288      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
[2901]289      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
[2899]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) {
[2934]305      this.graphVisualizationInfoView.Controller.View.ZoomFit();
[2899]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    }
[2934]315
316    private void screenshotButton_Click(object sender, EventArgs e) {
[3176]317      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
[2934]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;
[3514]323      saveFileDialog.AddExtension = true;
[2934]324
325      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
326        bitmap.Save(saveFileDialog.FileName);
327      }
328    }
[2801]329  }
330}
Note: See TracBrowser for help on using the repository browser.