Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3455 was 3455, checked in by swagner, 14 years ago

Removed manual propagation of ReadOnly property (#973)

File size: 12.9 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        connectButton.Enabled = !ReadOnly;
105      }
106    }
107
108    private OperatorGraphVisualizationInfo VisualizationInfo {
109      get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
110      set { this.Content.VisualizationInfo = value; }
111    }
112
113    private void Controller_SelectionChanged(object sender, EventArgs e) {
114      CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
115      this.detailsViewHost.ViewType = null;
116      if (selectedObjects.Count == 1) {
117        IShape shape = selectedObjects[0] as IShape;
118        if (shape != null) {
119          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
120          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
121          this.detailsViewHost.ViewType = null;
122          this.detailsViewHost.Content = op;
123          return;
124        }
125      }
126      IConnector connector = this.graphVisualizationInfoView.Controller.Model.Selection.Connector;
127      if (connector != null) {
128        IShape shape = connector.Parent as IShape;
129        string connectorName = connector.Name;
130        if (shape == null) {
131          shape = connector.AttachedTo.Parent as IShape; //connection connector selected
132          connectorName = connector.AttachedTo.Name;
133        }
134        if (shape != null) {
135          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
136          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
137          if (connectorName != "Predecessor") {
138            IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
139            this.detailsViewHost.ViewType = null;
140            this.detailsViewHost.Content = parameter;
141            return;
142          }
143        }
144      }
145      this.detailsViewHost.ViewType = null;
146      this.detailsViewHost.Content = null;
147    }
148
149    private void Controller_OnMouseDown(object sender, MouseEventArgs e) {
150      if (e.Clicks >= 2) {
151        IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.Location);
152        if (shape != null) {
153          IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
154          if (shapeInfo != null) {
155            IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
156            IContentView view = MainFormManager.CreateDefaultView(op);
157            if (view != null) {
158              view.ReadOnly = this.ReadOnly;
159              view.Locked = this.Locked;
160              view.Show();
161            }
162            HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
163            if (eventArgs != null)
164              eventArgs.Handled = true;
165          }
166        }
167      }
168    }
169
170    #region context menu
171    private void Controller_OnShowContextMenu(object sender, EntityMenuEventArgs e) {
172      IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.MouseEventArgs.Location);
173      if (shape != null) {
174        IShapeInfo shapeInfo = shape.Tag as IShapeInfo;
175        this.shapeContextMenu.Tag = shapeInfo;
176        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(e.MouseEventArgs.Location);
177        this.shapeContextMenu.Show(this, Point.Round(worldPoint));
178      }
179    }
180
181    private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
182      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
183      if (shapeInfo != null) {
184        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
185        this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
186        this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
187      }
188    }
189
190    private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
191      IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
192      if (shapeInfo != null) {
193        IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
194        IContentView view = MainFormManager.CreateDefaultView(op);
195        view.ReadOnly = this.ReadOnly;
196        view.Locked = this.Locked;
197        view.Show();
198      }
199    }
200
201    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
202      if (!ReadOnly) {
203        IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
204        if (this.VisualizationInfo.InitialShape == shapeInfo)
205          this.VisualizationInfo.InitialShape = null;
206        else
207          this.VisualizationInfo.InitialShape = shapeInfo;
208      }
209    }
210
211    private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
212      if (!ReadOnly) {
213        IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
214        if (shapeInfo != null) {
215          IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
216          op.Breakpoint = !op.Breakpoint;
217        }
218      }
219    }
220    #endregion
221
222    #region drag and drop
223    private void OperatorGraphView_DragEnter(object sender, DragEventArgs e) {
224      e.Effect = DragDropEffects.None;
225      if (ReadOnly)
226        return;
227      Type type = e.Data.GetData("Type") as Type;
228      if ((type != null) && (typeof(IOperator).IsAssignableFrom(type))) {
229        e.Effect = DragDropEffects.Copy;
230      }
231    }
232
233    private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
234      if (e.Effect != DragDropEffects.None) {
235        IOperator op = e.Data.GetData("Value") as IOperator;
236        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
237        Point mouse = new Point(MousePosition.X, MousePosition.Y);
238        Point screen = this.graphVisualizationInfoView.PointToScreen(new Point(0, 0));
239        Point control = new Point(mouse.X - screen.X, mouse.Y - screen.Y);
240        PointF worldPoint = this.graphVisualizationInfoView.Controller.View.ViewToWorld(control);
241
242        if (worldPoint.X < 0)
243          worldPoint.X = 0;
244        if (worldPoint.Y < 0)
245          worldPoint.Y = 0;
246
247        shapeInfo.Location = Point.Round(worldPoint);
248        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
249      }
250    }
251    #endregion
252
253    private void tool_OnToolActivate(object sender, ToolEventArgs e) {
254      Button button = GetButtonForTool(e.Properties.Name);
255      if (button != null)
256        button.Enabled = false;
257    }
258
259    private void tool_OnToolDeactivate(object sender, ToolEventArgs e) {
260      Button button = GetButtonForTool(e.Properties.Name);
261      if (button != null)
262        button.Enabled = true;
263    }
264
265    private Button GetButtonForTool(string toolName) {
266      Button button = null;
267      switch (toolName) {
268        case ControllerBase.SelectionToolName:
269          button = this.selectButton;
270          break;
271        case ControllerBase.PanToolName:
272          button = this.panButton;
273          break;
274        case ControllerBase.ConnectionToolName:
275          button = this.connectButton;
276          break;
277        case ControllerBase.ZoomAreaToolName:
278          button = this.zoomAreaButton;
279          break;
280      }
281      return button;
282    }
283
284    private void selectButton_Click(object sender, EventArgs e) {
285      ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
286      this.graphVisualizationInfoView.Controller.DeactivateAllTools();
287    }
288
289    private void panButton_Click(object sender, EventArgs e) {
290      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.PanToolName);
291    }
292
293    private void connectButton_Click(object sender, EventArgs e) {
294      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
295    }
296
297    private void relayoutButton_Click(object sender, EventArgs e) {
298      this.graphVisualizationInfoView.RelayoutGraph();
299    }
300
301    private void zoomAreaButton_Click(object sender, EventArgs e) {
302      this.graphVisualizationInfoView.Controller.View.ZoomFit();
303    }
304
305    private void zoomInButton_Click(object sender, EventArgs e) {
306      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
307    }
308
309    private void zoomOutButton_Click(object sender, EventArgs e) {
310      this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
311    }
312
313    private void screenshotButton_Click(object sender, EventArgs e) {
314      Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
315      SaveFileDialog saveFileDialog = new SaveFileDialog();
316      saveFileDialog.Title = "Save Screenshot";
317      saveFileDialog.DefaultExt = "bmp";
318      saveFileDialog.Filter = "Bitmap|*.bmp|All Files|*.*";
319      saveFileDialog.FilterIndex = 1;
320
321      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
322        bitmap.Save(saveFileDialog.FileName);
323      }
324    }
325  }
326}
Note: See TracBrowser for help on using the repository browser.