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, 12 years ago

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

File size: 13.0 KB
Line 
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;
34using HeuristicLab.Parameters;
35using HeuristicLab.MainForm.WindowsForms;
36using HeuristicLab.Collections;
37
38namespace HeuristicLab.Operators.Views.GraphVisualization {
39  [View("OperatorGraph View (Chart)")]
40  [Content(typeof(OperatorGraph), true)]
41  public partial class OperatorGraphView : ContentView {
42    public OperatorGraphView() {
43      InitializeComponent();
44      Caption = "Operator Graph Visualization";
45
46      this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
47      this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
48      this.graphVisualizationInfoView.Controller.OnMouseDown += new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
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      }
53    }
54
55    public OperatorGraphView(OperatorGraph content)
56      : this() {
57      this.Content = content;
58    }
59
60    public new OperatorGraph Content {
61      get { return (OperatorGraph)base.Content; }
62      set { base.Content = value; }
63    }
64
65    protected override void OnContentChanged() {
66      bool createdVisualizationInfo = false;
67      if (this.VisualizationInfo == null) {
68        this.VisualizationInfo = new OperatorGraphVisualizationInfo(this.Content);
69        createdVisualizationInfo = true;
70      }
71      this.graphVisualizationInfoView.Content = this.VisualizationInfo;
72      if (createdVisualizationInfo)
73        this.graphVisualizationInfoView.RelayoutGraph();
74
75      this.SetEnabledStateOfControls();
76    }
77
78    protected override void OnReadOnlyChanged() {
79      base.OnReadOnlyChanged();
80      this.SetEnabledStateOfControls();
81    }
82
83    private void SetEnabledStateOfControls() {
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
110    private OperatorGraphVisualizationInfo VisualizationInfo {
111      get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
112      set { this.Content.VisualizationInfo = value; }
113    }
114
115    private void Controller_SelectionChanged(object sender, EventArgs e) {
116      CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
117      this.detailsViewHost.ViewType = null;
118      if (selectedObjects.Count == 1) {
119        IShape shape = selectedObjects[0] as IShape;
120        if (shape != null) {
121          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
122          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
123          this.detailsViewHost.ViewType = null;
124          this.detailsViewHost.Content = op;
125          return;
126        }
127      }
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) {
137          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
138          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
139          if (connectorName != "Predecessor") {
140            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
141            this.detailsViewHost.ViewType = null;
142            this.detailsViewHost.Content = parameter;
143            return;
144          }
145        }
146      }
147      this.detailsViewHost.ViewType = null;
148      this.detailsViewHost.Content = null;
149    }
150
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);
158            IContentView view = MainFormManager.CreateDefaultView(op);
159            if (view != null) {
160              view.ReadOnly = this.ReadOnly;
161              view.Locked = this.Locked;
162              view.Show();
163            }
164            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
165            if (eventArgs != null)
166              eventArgs.Handled = true;
167          }
168        }
169      }
170    }
171
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));
180      }
181    }
182
183    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
184      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
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;
189      }
190    }
191
192    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
193      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
194      if (shapeInfo != null) {
195        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
196        IContentView view = MainFormManager.CreateDefaultView(op);
197        view.ReadOnly = this.ReadOnly;
198        view.Locked = this.Locked;
199        view.Show();
200      }
201    }
202
203    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
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      }
211    }
212
213    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
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        }
220      }
221    }
222    #endregion
223
224    #region drag and drop
225    private void OperatorGraphView_DragEnter(object sender, DragEventArgs e) {
226      e.Effect = DragDropEffects.None;
227      if (ReadOnly)
228        return;
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;
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.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
286    private void selectButton_Click(object sender, EventArgs e) {
287      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
288      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
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) {
304      this.graphVisualizationInfoView.Controller.View.ZoomFit();
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    }
314
315    private void screenshotButton_Click(object sender, EventArgs e) {
316      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
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    }
327  }
328}
Note: See TracBrowser for help on using the repository browser.