Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10355 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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