Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Operators.Views.GraphVisualization.Views/3.3/OperatorGraphView.cs

Last change on this file was 16140, checked in by abeham, 6 years ago

#2817: updated to trunk r15680

File size: 14.3 KB
RevLine 
[2801]1#region License Information
2/* HeuristicLab
[16140]3 * Copyright (C) 2002-2018 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.Drawing;
24using System.Linq;
25using System.Windows.Forms;
[4068]26using HeuristicLab.Core;
[13014]27using HeuristicLab.Core.Views;
[2801]28using HeuristicLab.MainForm;
[4068]29using HeuristicLab.MainForm.WindowsForms;
[8000]30using HeuristicLab.Netron.CustomTools;
[2801]31using Netron.Diagramming.Core;
32
[6036]33namespace HeuristicLab.Operators.Views.GraphVisualization.Views {
[2917]34  [View("OperatorGraph View (Chart)")]
[3361]35  [Content(typeof(OperatorGraph), true)]
[3514]36  public partial class OperatorGraphView : AsynchronousContentView {
[8000]37    private ITool lastActiveTool;
38
[2853]39    public OperatorGraphView() {
[2801]40      InitializeComponent();
[4011]41
[2893]42      this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
[2898]43      this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
[3176]44      this.graphVisualizationInfoView.Controller.OnMouseDown += new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
[8000]45      this.graphVisualizationInfoView.Controller.ParentControl.MouseDown += new MouseEventHandler(ParentControl_MouseDown);
46      this.graphVisualizationInfoView.Controller.ParentControl.MouseUp += new MouseEventHandler(ParentControl_MouseUp);
[2901]47      foreach (ITool tool in this.graphVisualizationInfoView.Controller.Tools) {
48        tool.OnToolActivate += new EventHandler<ToolEventArgs>(tool_OnToolActivate);
49        tool.OnToolDeactivate += new EventHandler<ToolEventArgs>(tool_OnToolDeactivate);
50      }
[2801]51    }
52
[2853]53    public new OperatorGraph Content {
54      get { return (OperatorGraph)base.Content; }
[2801]55      set { base.Content = value; }
56    }
57
[2853]58    protected override void OnContentChanged() {
[4011]59      if (Content != null) {
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      } else {
69        this.graphVisualizationInfoView.Content = null;
[2898]70      }
[2801]71    }
72
[3904]73    protected override void SetEnabledStateOfControls() {
74      base.SetEnabledStateOfControls();
[3355]75      if (Content == null) {
76        selectButton.Enabled = false;
77        relayoutButton.Enabled = false;
[3523]78        zoomToFitButton.Enabled = false;
[3355]79        zoomInButton.Enabled = false;
80        zoomOutButton.Enabled = false;
81        screenshotButton.Enabled = false;
82        detailsViewHost.Enabled = false;
83        connectButton.Enabled = false;
84      } else {
85        selectButton.Enabled = true;
86        relayoutButton.Enabled = true;
[3523]87        zoomToFitButton.Enabled = true;
[3355]88        zoomInButton.Enabled = true;
89        zoomOutButton.Enabled = true;
90        screenshotButton.Enabled = true;
91        detailsViewHost.Enabled = true;
[4435]92        connectButton.Enabled = !ReadOnly;
[3355]93      }
[3514]94    }
[3355]95
[3386]96    private OperatorGraphVisualizationInfo VisualizationInfo {
97      get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
[2853]98      set { this.Content.VisualizationInfo = value; }
99    }
[2819]100
[2898]101    private void Controller_SelectionChanged(object sender, EventArgs e) {
[3514]102      this.detailsViewHost.ViewType = null;
103      this.detailsViewHost.Content = null;
104
[2895]105      CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
106      if (selectedObjects.Count == 1) {
107        IShape shape = selectedObjects[0] as IShape;
108        if (shape != null) {
[2934]109          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]110          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
111          this.detailsViewHost.Content = op;
[2895]112        }
113      }
[3514]114
[2898]115      IConnector connector = this.graphVisualizationInfoView.Controller.Model.Selection.Connector;
116      if (connector != null) {
117        IShape shape = connector.Parent as IShape;
118        string connectorName = connector.Name;
119        if (shape == null) {
120          shape = connector.AttachedTo.Parent as IShape; //connection connector selected
121          connectorName = connector.AttachedTo.Name;
122        }
123        if (shape != null) {
[2934]124          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]125          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
126          if (connectorName != "Predecessor") {
127            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
[2949]128            this.detailsViewHost.ViewType = null;
[2898]129            this.detailsViewHost.Content = parameter;
130          }
131        }
132      }
[3514]133
[2895]134    }
135
[3176]136    private void Controller_OnMouseDown(object sender, MouseEventArgs e) {
137      if (e.Clicks >= 2) {
138        IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.Location);
139        if (shape != null) {
140          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
141          if (shapeInfo != null) {
142            IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
[13244]143            IOperatorGraphOperator graphOp = op as IOperatorGraphOperator;
[13014]144
145            Control c = this;
146            BreadcrumbViewHost vh;
147
148            do {
149              c = c.Parent;
150              vh = c as BreadcrumbViewHost;
151            } while ((vh == null || !vh.EnableBreadcrumbs) && c != null);
152
[13244]153            if (graphOp != null && vh != null) {
[13014]154              vh.AddBreadcrumbs(vh.Content);
[13244]155              vh.AddBreadcrumb(graphOp.Name, graphOp.OperatorGraph);
156              vh.Content = graphOp.OperatorGraph;
[13014]157              vh.ReadOnly = ReadOnly;
158              vh.Locked = Locked;
159            } else {
160              IContentView view = MainFormManager.MainForm.ShowContent(op);
161              if (view != null) {
162                view.ReadOnly = ReadOnly;
163                view.Locked = Locked;
164              }
[3423]165            }
[13014]166
[3176]167            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
168            if (eventArgs != null)
169              eventArgs.Handled = true;
170          }
171        }
172      }
173    }
174
[8000]175    void ParentControl_MouseDown(object sender, MouseEventArgs e) {
176      if (e.Button == MouseButtons.Middle) {
[8860]177        lastActiveTool = graphVisualizationInfoView.Controller.ActiveTool;
178        graphVisualizationInfoView.Controller.ActivateTool(CustomPanTool.ToolName);
[8000]179      }
180    }
181
182    void ParentControl_MouseUp(object sender, MouseEventArgs e) {
183      if (e.Button == MouseButtons.Middle) {
[8860]184        var activeTool = graphVisualizationInfoView.Controller.ActiveTool;
185        graphVisualizationInfoView.Controller.DeactivateTool(activeTool);
186        if (lastActiveTool != null && !(lastActiveTool is ZoomToolBase))
187          graphVisualizationInfoView.Controller.ActivateTool(lastActiveTool.Name);
[8000]188      }
189    }
190
[2893]191    #region context menu
192    private void Controller_OnShowContextMenu(object sender, EntityMenuEventArgs e) {
193      IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.MouseEventArgs.Location);
194      if (shape != null) {
195        IShapeInfo shapeInfo = shape.Tag as IShapeInfo;
196        this.shapeContextMenu.Tag = shapeInfo;
197        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(e.MouseEventArgs.Location);
198        this.shapeContextMenu.Show(this, Point.Round(worldPoint));
[2861]199      }
[2801]200    }
201
[2893]202    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
[2934]203      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]204      if (shapeInfo != null) {
205        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
206        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
[3514]207        this.initialToolStripMenuItem.Enabled = !ReadOnly && !Locked;
[2893]208        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
[5224]209        this.breakPointToolStripMenuItem.Enabled = !Locked;
[2853]210      }
[2801]211    }
212
[2893]213    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
[2934]214      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]215      if (shapeInfo != null) {
216        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
[3557]217        IContentView view = MainFormManager.MainForm.ShowContent(op);
218        if (view != null) {
219          view.ReadOnly = this.ReadOnly;
220          view.Locked = this.Locked;
221        }
[2853]222      }
[2801]223    }
[2819]224
[2893]225    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
[4011]226      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
227      if (this.VisualizationInfo.InitialShape == shapeInfo)
228        this.VisualizationInfo.InitialShape = null;
229      else
230        this.VisualizationInfo.InitialShape = shapeInfo;
[2861]231    }
[2819]232
[2893]233    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
[4011]234      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
235      if (shapeInfo != null) {
236        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
237        op.Breakpoint = !op.Breakpoint;
238      }
[2861]239    }
[2893]240    #endregion
[2819]241
[2853]242    #region drag and drop
[3556]243    private void OperatorGraphView_DragEnterOver(object sender, DragEventArgs e) {
[2853]244      e.Effect = DragDropEffects.None;
[5837]245      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOperator)) {
[3694]246        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[3556]247        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
[5744]248        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
249        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
250        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
[2853]251      }
252    }
253
254    private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
255      if (e.Effect != DragDropEffects.None) {
[5837]256        IOperator op = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IOperator;
[5744]257        if (e.Effect.HasFlag(DragDropEffects.Copy)) op = (IOperator)op.Clone();
[3386]258        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
[2893]259        Point mouse = new Point(MousePosition.X, MousePosition.Y);
[2934]260        Point screen = this.graphVisualizationInfoView.PointToScreen(new Point(0, 0));
261        Point control = new Point(mouse.X - screen.X, mouse.Y - screen.Y);
262        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.ViewToWorld(control);
[2893]263
[2934]264        if (worldPoint.X < 0)
265          worldPoint.X = 0;
266        if (worldPoint.Y < 0)
267          worldPoint.Y = 0;
268
[2893]269        shapeInfo.Location = Point.Round(worldPoint);
[2853]270        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
271      }
272    }
273    #endregion
[2899]274
[2901]275    private void tool_OnToolActivate(object sender, ToolEventArgs e) {
276      Button button = GetButtonForTool(e.Properties.Name);
277      if (button != null)
278        button.Enabled = false;
279    }
280
281    private void tool_OnToolDeactivate(object sender, ToolEventArgs e) {
282      Button button = GetButtonForTool(e.Properties.Name);
283      if (button != null)
284        button.Enabled = true;
285    }
286
287    private Button GetButtonForTool(string toolName) {
288      Button button = null;
289      switch (toolName) {
290        case ControllerBase.SelectionToolName:
291          button = this.selectButton;
292          break;
293        case ControllerBase.ConnectionToolName:
294          button = this.connectButton;
295          break;
296        case ControllerBase.ZoomAreaToolName:
[3523]297          button = this.zoomToFitButton;
[2901]298          break;
299      }
300      return button;
301    }
302
[2899]303    private void selectButton_Click(object sender, EventArgs e) {
304      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
[2901]305      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
[2899]306    }
307
308    private void connectButton_Click(object sender, EventArgs e) {
309      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
310    }
311
312    private void relayoutButton_Click(object sender, EventArgs e) {
313      this.graphVisualizationInfoView.RelayoutGraph();
314    }
315
316    private void zoomAreaButton_Click(object sender, EventArgs e) {
[2934]317      this.graphVisualizationInfoView.Controller.View.ZoomFit();
[2899]318    }
319
320    private void zoomInButton_Click(object sender, EventArgs e) {
321      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
322    }
323
324    private void zoomOutButton_Click(object sender, EventArgs e) {
325      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
326    }
[2934]327
328    private void screenshotButton_Click(object sender, EventArgs e) {
[3176]329      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
[2934]330      SaveFileDialog saveFileDialog = new SaveFileDialog();
331      saveFileDialog.Title = "Save Screenshot";
[3753]332      saveFileDialog.DefaultExt = "png";
333      saveFileDialog.Filter = "Portable Network Graphics|*.png|All Files|*.*";
[2934]334      saveFileDialog.FilterIndex = 1;
[3514]335      saveFileDialog.AddExtension = true;
[2934]336
337      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
338        bitmap.Save(saveFileDialog.FileName);
339      }
340    }
[2801]341  }
342}
Note: See TracBrowser for help on using the repository browser.