Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Operators.Views.GraphVisualization.Views/3.3/OperatorGraphView.cs @ 16453

Last change on this file since 16453 was 16453, checked in by jkarder, 6 years ago

#2520: updated year of copyrights

File size: 14.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.Netron.CustomTools;
31using Netron.Diagramming.Core;
32
33namespace HeuristicLab.Operators.Views.GraphVisualization.Views {
34  [View("OperatorGraph View (Chart)")]
35  [Content(typeof(OperatorGraph), true)]
36  public partial class OperatorGraphView : AsynchronousContentView {
37    private ITool lastActiveTool;
38
39    public OperatorGraphView() {
40      InitializeComponent();
41
42      this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
43      this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
44      this.graphVisualizationInfoView.Controller.OnMouseDown += new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
45      this.graphVisualizationInfoView.Controller.ParentControl.MouseDown += new MouseEventHandler(ParentControl_MouseDown);
46      this.graphVisualizationInfoView.Controller.ParentControl.MouseUp += new MouseEventHandler(ParentControl_MouseUp);
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      }
51    }
52
53    public new OperatorGraph Content {
54      get { return (OperatorGraph)base.Content; }
55      set { base.Content = value; }
56    }
57
58    protected override void OnContentChanged() {
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;
70      }
71    }
72
73    protected override void SetEnabledStateOfControls() {
74      base.SetEnabledStateOfControls();
75      if (Content == null) {
76        selectButton.Enabled = false;
77        relayoutButton.Enabled = false;
78        zoomToFitButton.Enabled = false;
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;
87        zoomToFitButton.Enabled = true;
88        zoomInButton.Enabled = true;
89        zoomOutButton.Enabled = true;
90        screenshotButton.Enabled = true;
91        detailsViewHost.Enabled = true;
92        connectButton.Enabled = !ReadOnly;
93      }
94    }
95
96    private OperatorGraphVisualizationInfo VisualizationInfo {
97      get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
98      set { this.Content.VisualizationInfo = value; }
99    }
100
101    private void Controller_SelectionChanged(object sender, EventArgs e) {
102      this.detailsViewHost.ViewType = null;
103      this.detailsViewHost.Content = null;
104
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) {
109          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
110          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
111          this.detailsViewHost.Content = op;
112        }
113      }
114
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) {
124          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
125          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
126          if (connectorName != "Predecessor") {
127            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
128            this.detailsViewHost.ViewType = null;
129            this.detailsViewHost.Content = parameter;
130          }
131        }
132      }
133
134    }
135
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);
143            IOperatorGraphOperator graphOp = op as IOperatorGraphOperator;
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
153            if (graphOp != null && vh != null) {
154              vh.AddBreadcrumbs(vh.Content);
155              vh.AddBreadcrumb(graphOp.Name, graphOp.OperatorGraph);
156              vh.Content = graphOp.OperatorGraph;
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              }
165            }
166
167            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
168            if (eventArgs != null)
169              eventArgs.Handled = true;
170          }
171        }
172      }
173    }
174
175    void ParentControl_MouseDown(object sender, MouseEventArgs e) {
176      if (e.Button == MouseButtons.Middle) {
177        lastActiveTool = graphVisualizationInfoView.Controller.ActiveTool;
178        graphVisualizationInfoView.Controller.ActivateTool(CustomPanTool.ToolName);
179      }
180    }
181
182    void ParentControl_MouseUp(object sender, MouseEventArgs e) {
183      if (e.Button == MouseButtons.Middle) {
184        var activeTool = graphVisualizationInfoView.Controller.ActiveTool;
185        graphVisualizationInfoView.Controller.DeactivateTool(activeTool);
186        if (lastActiveTool != null && !(lastActiveTool is ZoomToolBase))
187          graphVisualizationInfoView.Controller.ActivateTool(lastActiveTool.Name);
188      }
189    }
190
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));
199      }
200    }
201
202    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
203      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
204      if (shapeInfo != null) {
205        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
206        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
207        this.initialToolStripMenuItem.Enabled = !ReadOnly && !Locked;
208        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
209        this.breakPointToolStripMenuItem.Enabled = !Locked;
210      }
211    }
212
213    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
214      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
215      if (shapeInfo != null) {
216        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
217        IContentView view = MainFormManager.MainForm.ShowContent(op);
218        if (view != null) {
219          view.ReadOnly = this.ReadOnly;
220          view.Locked = this.Locked;
221        }
222      }
223    }
224
225    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
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;
231    }
232
233    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
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      }
239    }
240    #endregion
241
242    #region drag and drop
243    private void OperatorGraphView_DragEnterOver(object sender, DragEventArgs e) {
244      e.Effect = DragDropEffects.None;
245      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOperator)) {
246        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
247        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
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;
251      }
252    }
253
254    private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
255      if (e.Effect != DragDropEffects.None) {
256        IOperator op = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IOperator;
257        if (e.Effect.HasFlag(DragDropEffects.Copy)) op = (IOperator)op.Clone();
258        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
259        Point mouse = new Point(MousePosition.X, MousePosition.Y);
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);
263
264        if (worldPoint.X < 0)
265          worldPoint.X = 0;
266        if (worldPoint.Y < 0)
267          worldPoint.Y = 0;
268
269        shapeInfo.Location = Point.Round(worldPoint);
270        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
271      }
272    }
273    #endregion
274
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:
297          button = this.zoomToFitButton;
298          break;
299      }
300      return button;
301    }
302
303    private void selectButton_Click(object sender, EventArgs e) {
304      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
305      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
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) {
317      this.graphVisualizationInfoView.Controller.View.ZoomFit();
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    }
327
328    private void screenshotButton_Click(object sender, EventArgs e) {
329      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
330      SaveFileDialog saveFileDialog = new SaveFileDialog();
331      saveFileDialog.Title = "Save Screenshot";
332      saveFileDialog.DefaultExt = "png";
333      saveFileDialog.Filter = "Portable Network Graphics|*.png|All Files|*.*";
334      saveFileDialog.FilterIndex = 1;
335      saveFileDialog.AddExtension = true;
336
337      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
338        bitmap.Save(saveFileDialog.FileName);
339      }
340    }
341  }
342}
Note: See TracBrowser for help on using the repository browser.