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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 | using HeuristicLab.Core;
|
---|
32 | using HeuristicLab.Core.Views;
|
---|
33 | using Netron.Diagramming.Core;
|
---|
34 | using HeuristicLab.Parameters;
|
---|
35 | using HeuristicLab.MainForm.WindowsForms;
|
---|
36 | using HeuristicLab.Collections;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Operators.Views.GraphVisualization {
|
---|
39 | [View("Operator Graph Visualization")]
|
---|
40 | [Content(typeof(OperatorGraph), false)]
|
---|
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 | foreach (ITool tool in this.graphVisualizationInfoView.Controller.Tools) {
|
---|
49 | tool.OnToolActivate += new EventHandler<ToolEventArgs>(tool_OnToolActivate);
|
---|
50 | tool.OnToolDeactivate += new EventHandler<ToolEventArgs>(tool_OnToolDeactivate);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public OperatorGraphView(OperatorGraph content)
|
---|
55 | : this() {
|
---|
56 | this.Content = content;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public new OperatorGraph Content {
|
---|
60 | get { return (OperatorGraph)base.Content; }
|
---|
61 | set { base.Content = value; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected override void OnContentChanged() {
|
---|
65 | bool createdVisualizationInfo = false;
|
---|
66 | if (this.VisualizationInfo == null) {
|
---|
67 | this.VisualizationInfo = new GraphVisualizationInfo(this.Content);
|
---|
68 | createdVisualizationInfo = true;
|
---|
69 | }
|
---|
70 | this.graphVisualizationInfoView.Content = this.VisualizationInfo;
|
---|
71 | if (createdVisualizationInfo)
|
---|
72 | this.graphVisualizationInfoView.RelayoutGraph();
|
---|
73 | }
|
---|
74 |
|
---|
75 | private GraphVisualizationInfo VisualizationInfo {
|
---|
76 | get { return Content.VisualizationInfo as GraphVisualizationInfo; }
|
---|
77 | set { this.Content.VisualizationInfo = value; }
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | #region connector tooltips
|
---|
82 |
|
---|
83 | #endregion
|
---|
84 |
|
---|
85 | private void Controller_SelectionChanged(object sender, EventArgs e) {
|
---|
86 | CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
|
---|
87 | this.detailsViewHost.ViewType = null;
|
---|
88 | if (selectedObjects.Count == 1) {
|
---|
89 | IShape shape = selectedObjects[0] as IShape;
|
---|
90 | if (shape != null) {
|
---|
91 | IShapeInfo shapeInfo = shape.Tag as ShapeInfo;
|
---|
92 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
93 | this.detailsViewHost.Content = op;
|
---|
94 | return;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | IConnector connector = this.graphVisualizationInfoView.Controller.Model.Selection.Connector;
|
---|
98 | if (connector != null) {
|
---|
99 | IShape shape = connector.Parent as IShape;
|
---|
100 | string connectorName = connector.Name;
|
---|
101 | if (shape == null) {
|
---|
102 | shape = connector.AttachedTo.Parent as IShape; //connection connector selected
|
---|
103 | connectorName = connector.AttachedTo.Name;
|
---|
104 | }
|
---|
105 | if (shape != null) {
|
---|
106 | IShapeInfo shapeInfo = shape.Tag as ShapeInfo;
|
---|
107 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
108 | if (connectorName != "Predecessor") {
|
---|
109 | IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
|
---|
110 | this.detailsViewHost.Content = parameter;
|
---|
111 | return;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | this.detailsViewHost.ViewType = null;
|
---|
116 | this.detailsViewHost.Content = null;
|
---|
117 | }
|
---|
118 |
|
---|
119 | #region context menu
|
---|
120 | private void Controller_OnShowContextMenu(object sender, EntityMenuEventArgs e) {
|
---|
121 | IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.MouseEventArgs.Location);
|
---|
122 | if (shape != null) {
|
---|
123 | IShapeInfo shapeInfo = shape.Tag as IShapeInfo;
|
---|
124 | this.shapeContextMenu.Tag = shapeInfo;
|
---|
125 | PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(e.MouseEventArgs.Location);
|
---|
126 | this.shapeContextMenu.Show(this, Point.Round(worldPoint));
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
|
---|
131 | IShapeInfo shapeInfo = this.shapeContextMenu.Tag as ShapeInfo;
|
---|
132 | if (shapeInfo != null) {
|
---|
133 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
134 | this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
|
---|
135 | this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
140 | IShapeInfo shapeInfo = this.shapeContextMenu.Tag as ShapeInfo;
|
---|
141 | if (shapeInfo != null) {
|
---|
142 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
143 | MainFormManager.CreateDefaultView(op).Show();
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
148 | IShapeInfo shapeInfo = this.shapeContextMenu.Tag as ShapeInfo;
|
---|
149 | if (this.VisualizationInfo.InitialShape == shapeInfo)
|
---|
150 | this.VisualizationInfo.InitialShape = null;
|
---|
151 | else
|
---|
152 | this.VisualizationInfo.InitialShape = shapeInfo;
|
---|
153 | }
|
---|
154 |
|
---|
155 | private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
156 | IShapeInfo shapeInfo = this.shapeContextMenu.Tag as ShapeInfo;
|
---|
157 | if (shapeInfo != null) {
|
---|
158 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
159 | op.Breakpoint = !op.Breakpoint;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | #endregion
|
---|
163 |
|
---|
164 | #region drag and drop
|
---|
165 | private void OperatorGraphView_DragEnter(object sender, DragEventArgs e) {
|
---|
166 | e.Effect = DragDropEffects.None;
|
---|
167 | Type type = e.Data.GetData("Type") as Type;
|
---|
168 | if ((type != null) && (typeof(IOperator).IsAssignableFrom(type))) {
|
---|
169 | e.Effect = DragDropEffects.Copy;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
|
---|
174 | if (e.Effect != DragDropEffects.None) {
|
---|
175 | IOperator op = e.Data.GetData("Value") as IOperator;
|
---|
176 | IShapeInfo shapeInfo = Factory.CreateShapeInfo(op);
|
---|
177 | Point mouse = new Point(MousePosition.X, MousePosition.Y);
|
---|
178 | Point p = new Point(e.X, e.Y);
|
---|
179 | Point screen = this.PointToScreen(new Point(0, 0));
|
---|
180 | PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(new Point(mouse.X - screen.X, mouse.Y - screen.Y));
|
---|
181 |
|
---|
182 | shapeInfo.Location = Point.Round(worldPoint);
|
---|
183 | this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | #endregion
|
---|
188 |
|
---|
189 | private void tool_OnToolActivate(object sender, ToolEventArgs e) {
|
---|
190 | Button button = GetButtonForTool(e.Properties.Name);
|
---|
191 | if (button != null)
|
---|
192 | button.Enabled = false;
|
---|
193 | }
|
---|
194 |
|
---|
195 | private void tool_OnToolDeactivate(object sender, ToolEventArgs e) {
|
---|
196 | Button button = GetButtonForTool(e.Properties.Name);
|
---|
197 | if (button != null)
|
---|
198 | button.Enabled = true;
|
---|
199 | }
|
---|
200 |
|
---|
201 | private Button GetButtonForTool(string toolName) {
|
---|
202 | Button button = null;
|
---|
203 | switch (toolName) {
|
---|
204 | case ControllerBase.SelectionToolName:
|
---|
205 | button = this.selectButton;
|
---|
206 | break;
|
---|
207 | case ControllerBase.PanToolName:
|
---|
208 | button = this.panButton;
|
---|
209 | break;
|
---|
210 | case ControllerBase.ConnectionToolName:
|
---|
211 | button = this.connectButton;
|
---|
212 | break;
|
---|
213 | case ControllerBase.ZoomAreaToolName:
|
---|
214 | button = this.zoomAreaButton;
|
---|
215 | break;
|
---|
216 | }
|
---|
217 | return button;
|
---|
218 | }
|
---|
219 |
|
---|
220 | private void selectButton_Click(object sender, EventArgs e) {
|
---|
221 | ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
|
---|
222 | tool.IsSuspended = false;
|
---|
223 | this.graphVisualizationInfoView.Controller.DeactivateAllTools();
|
---|
224 | }
|
---|
225 |
|
---|
226 | private void panButton_Click(object sender, EventArgs e) {
|
---|
227 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.PanToolName);
|
---|
228 | }
|
---|
229 |
|
---|
230 | private void connectButton_Click(object sender, EventArgs e) {
|
---|
231 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
|
---|
232 | }
|
---|
233 |
|
---|
234 | private void relayoutButton_Click(object sender, EventArgs e) {
|
---|
235 | this.graphVisualizationInfoView.RelayoutGraph();
|
---|
236 | }
|
---|
237 |
|
---|
238 | private void zoomAreaButton_Click(object sender, EventArgs e) {
|
---|
239 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomAreaToolName);
|
---|
240 | }
|
---|
241 |
|
---|
242 | private void zoomInButton_Click(object sender, EventArgs e) {
|
---|
243 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
|
---|
244 | }
|
---|
245 |
|
---|
246 | private void zoomOutButton_Click(object sender, EventArgs e) {
|
---|
247 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|