Free cookie consent management tool by TermsFeed Policy Generator

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

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

added setting of locked property after the creation of contentviews (ticket #982)

File size: 13.0 KB
RevLine 
[2801]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.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)]
[2853]41  public partial class OperatorGraphView : ContentView {
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 OperatorGraphView(OperatorGraph content)
[2801]56      : this() {
57      this.Content = content;
58    }
59
[2853]60    public new OperatorGraph Content {
61      get { return (OperatorGraph)base.Content; }
[2801]62      set { base.Content = value; }
63    }
64
[2853]65    protected override void OnContentChanged() {
[2898]66      bool createdVisualizationInfo = false;
67      if (this.VisualizationInfo == null) {
[3386]68        this.VisualizationInfo = new OperatorGraphVisualizationInfo(this.Content);
[2898]69        createdVisualizationInfo = true;
70      }
[2893]71      this.graphVisualizationInfoView.Content = this.VisualizationInfo;
[2898]72      if (createdVisualizationInfo)
[2899]73        this.graphVisualizationInfoView.RelayoutGraph();
[3355]74
[3362]75      this.SetEnabledStateOfControls();
[2801]76    }
77
[3355]78    protected override void OnReadOnlyChanged() {
79      base.OnReadOnlyChanged();
[3362]80      this.SetEnabledStateOfControls();
[3355]81    }
82
[3362]83    private void SetEnabledStateOfControls() {
[3355]84      if (Content == null) {
85        selectButton.Enabled = false;
86        panButton.Enabled = false;
87        relayoutButton.Enabled = false;
88        zoomAreaButton.Enabled = false;
89        zoomInButton.Enabled = false;
90        zoomOutButton.Enabled = false;
91        screenshotButton.Enabled = false;
92        detailsViewHost.Enabled = false;
93        graphVisualizationInfoView.ReadOnly = true;
94        connectButton.Enabled = false;
95      } else {
96        selectButton.Enabled = true;
97        panButton.Enabled = true;
98        relayoutButton.Enabled = true;
99        zoomAreaButton.Enabled = true;
100        zoomInButton.Enabled = true;
101        zoomOutButton.Enabled = true;
102        screenshotButton.Enabled = true;
103        detailsViewHost.Enabled = true;
104        detailsViewHost.ReadOnly = ReadOnly;
105        graphVisualizationInfoView.ReadOnly = ReadOnly;
106        connectButton.Enabled = !ReadOnly;
107      }
108    }
109
[3386]110    private OperatorGraphVisualizationInfo VisualizationInfo {
111      get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
[2853]112      set { this.Content.VisualizationInfo = value; }
113    }
[2819]114
[2898]115    private void Controller_SelectionChanged(object sender, EventArgs e) {
[2895]116      CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
[2898]117      this.detailsViewHost.ViewType = null;
[2895]118      if (selectedObjects.Count == 1) {
119        IShape shape = selectedObjects[0] as IShape;
120        if (shape != null) {
[2934]121          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]122          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
[2949]123          this.detailsViewHost.ViewType = null;
[2898]124          this.detailsViewHost.Content = op;
[2895]125          return;
126        }
127      }
[2898]128      IConnector connector = this.graphVisualizationInfoView.Controller.Model.Selection.Connector;
129      if (connector != null) {
130        IShape shape = connector.Parent as IShape;
131        string connectorName = connector.Name;
132        if (shape == null) {
133          shape = connector.AttachedTo.Parent as IShape; //connection connector selected
134          connectorName = connector.AttachedTo.Name;
135        }
136        if (shape != null) {
[2934]137          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
[2898]138          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
139          if (connectorName != "Predecessor") {
140            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
[2949]141            this.detailsViewHost.ViewType = null;
[2898]142            this.detailsViewHost.Content = parameter;
143            return;
144          }
145        }
146      }
147      this.detailsViewHost.ViewType = null;
148      this.detailsViewHost.Content = null;
[2895]149    }
150
[3176]151    private void Controller_OnMouseDown(object sender, MouseEventArgs e) {
152      if (e.Clicks >= 2) {
153        IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.Location);
154        if (shape != null) {
155          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
156          if (shapeInfo != null) {
157            IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
[3423]158            IContentView view = MainFormManager.CreateDefaultView(op);
159            if (view != null) {
160              view.ReadOnly = this.ReadOnly;
161              view.Locked = this.Locked;
162              view.Show();
163            }
[3176]164            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
165            if (eventArgs != null)
166              eventArgs.Handled = true;
167          }
168        }
169      }
170    }
171
[2893]172    #region context menu
173    private void Controller_OnShowContextMenu(object sender, EntityMenuEventArgs e) {
174      IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.MouseEventArgs.Location);
175      if (shape != null) {
176        IShapeInfo shapeInfo = shape.Tag as IShapeInfo;
177        this.shapeContextMenu.Tag = shapeInfo;
178        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(e.MouseEventArgs.Location);
179        this.shapeContextMenu.Show(this, Point.Round(worldPoint));
[2861]180      }
[2801]181    }
182
[2893]183    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
[2934]184      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]185      if (shapeInfo != null) {
186        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
187        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
188        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
[2853]189      }
[2801]190    }
191
[2893]192    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
[2934]193      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
[2893]194      if (shapeInfo != null) {
195        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
[3423]196        IContentView view = MainFormManager.CreateDefaultView(op);
197        view.ReadOnly = this.ReadOnly;
198        view.Locked = this.Locked;
199        view.Show();
[2853]200      }
[2801]201    }
[2819]202
[2893]203    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
[3355]204      if (!ReadOnly) {
205        IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
206        if (this.VisualizationInfo.InitialShape == shapeInfo)
207          this.VisualizationInfo.InitialShape = null;
208        else
209          this.VisualizationInfo.InitialShape = shapeInfo;
210      }
[2861]211    }
[2819]212
[2893]213    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
[3355]214      if (!ReadOnly) {
215        IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
216        if (shapeInfo != null) {
217          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
218          op.Breakpoint = !op.Breakpoint;
219        }
[2861]220      }
221    }
[2893]222    #endregion
[2819]223
[2853]224    #region drag and drop
225    private void OperatorGraphView_DragEnter(object sender, DragEventArgs e) {
226      e.Effect = DragDropEffects.None;
[3355]227      if (ReadOnly)
228        return;
[2853]229      Type type = e.Data.GetData("Type") as Type;
230      if ((type != null) && (typeof(IOperator).IsAssignableFrom(type))) {
231        e.Effect = DragDropEffects.Copy;
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;
[3386]238        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
[2893]239        Point mouse = new Point(MousePosition.X, MousePosition.Y);
[2934]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);
[2893]243
[2934]244        if (worldPoint.X < 0)
245          worldPoint.X = 0;
246        if (worldPoint.Y < 0)
247          worldPoint.Y = 0;
248
[2893]249        shapeInfo.Location = Point.Round(worldPoint);
[2853]250        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
251      }
252    }
253    #endregion
[2899]254
[2901]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.PanToolName:
274          button = this.panButton;
275          break;
276        case ControllerBase.ConnectionToolName:
277          button = this.connectButton;
278          break;
279        case ControllerBase.ZoomAreaToolName:
280          button = this.zoomAreaButton;
281          break;
282      }
283      return button;
284    }
285
[2899]286    private void selectButton_Click(object sender, EventArgs e) {
287      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
[2901]288      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
[2899]289    }
290
291    private void panButton_Click(object sender, EventArgs e) {
292      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.PanToolName);
293    }
294
295    private void connectButton_Click(object sender, EventArgs e) {
296      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
297    }
298
299    private void relayoutButton_Click(object sender, EventArgs e) {
300      this.graphVisualizationInfoView.RelayoutGraph();
301    }
302
303    private void zoomAreaButton_Click(object sender, EventArgs e) {
[2934]304      this.graphVisualizationInfoView.Controller.View.ZoomFit();
[2899]305    }
306
307    private void zoomInButton_Click(object sender, EventArgs e) {
308      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
309    }
310
311    private void zoomOutButton_Click(object sender, EventArgs e) {
312      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
313    }
[2934]314
315    private void screenshotButton_Click(object sender, EventArgs e) {
[3176]316      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
[2934]317      SaveFileDialog saveFileDialog = new SaveFileDialog();
318      saveFileDialog.Title = "Save Screenshot";
319      saveFileDialog.DefaultExt = "bmp";
320      saveFileDialog.Filter = "Bitmap|*.bmp|All Files|*.*";
321      saveFileDialog.FilterIndex = 1;
322
323      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
324        bitmap.Save(saveFileDialog.FileName);
325      }
326    }
[2801]327  }
328}
Note: See TracBrowser for help on using the repository browser.