[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 |
|
---|
| 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;
|
---|
[2819] | 34 | using HeuristicLab.Parameters;
|
---|
[2853] | 35 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[2861] | 36 | using HeuristicLab.Collections;
|
---|
[2801] | 37 |
|
---|
| 38 | namespace HeuristicLab.Operators.Views.GraphVisualization {
|
---|
[2917] | 39 | [View("OperatorGraph View (Chart)")]
|
---|
[2853] | 40 | [Content(typeof(OperatorGraph), false)]
|
---|
| 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) {
|
---|
[2893] | 68 | this.VisualizationInfo = new GraphVisualizationInfo(this.Content);
|
---|
[2898] | 69 | createdVisualizationInfo = true;
|
---|
| 70 | }
|
---|
[2893] | 71 | this.graphVisualizationInfoView.Content = this.VisualizationInfo;
|
---|
[2898] | 72 | if (createdVisualizationInfo)
|
---|
[2899] | 73 | this.graphVisualizationInfoView.RelayoutGraph();
|
---|
[2801] | 74 | }
|
---|
| 75 |
|
---|
[2893] | 76 | private GraphVisualizationInfo VisualizationInfo {
|
---|
| 77 | get { return Content.VisualizationInfo as GraphVisualizationInfo; }
|
---|
[2853] | 78 | set { this.Content.VisualizationInfo = value; }
|
---|
| 79 | }
|
---|
[2819] | 80 |
|
---|
[2898] | 81 | private void Controller_SelectionChanged(object sender, EventArgs e) {
|
---|
[2895] | 82 | CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
|
---|
[2898] | 83 | this.detailsViewHost.ViewType = null;
|
---|
[2895] | 84 | if (selectedObjects.Count == 1) {
|
---|
| 85 | IShape shape = selectedObjects[0] as IShape;
|
---|
| 86 | if (shape != null) {
|
---|
[2934] | 87 | IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
|
---|
[2898] | 88 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
[2949] | 89 | this.detailsViewHost.ViewType = null;
|
---|
[2898] | 90 | this.detailsViewHost.Content = op;
|
---|
[2895] | 91 | return;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
[2898] | 94 | IConnector connector = this.graphVisualizationInfoView.Controller.Model.Selection.Connector;
|
---|
| 95 | if (connector != null) {
|
---|
| 96 | IShape shape = connector.Parent as IShape;
|
---|
| 97 | string connectorName = connector.Name;
|
---|
| 98 | if (shape == null) {
|
---|
| 99 | shape = connector.AttachedTo.Parent as IShape; //connection connector selected
|
---|
| 100 | connectorName = connector.AttachedTo.Name;
|
---|
| 101 | }
|
---|
| 102 | if (shape != null) {
|
---|
[2934] | 103 | IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
|
---|
[2898] | 104 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 105 | if (connectorName != "Predecessor") {
|
---|
| 106 | IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
|
---|
[2949] | 107 | this.detailsViewHost.ViewType = null;
|
---|
[2898] | 108 | this.detailsViewHost.Content = parameter;
|
---|
| 109 | return;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | this.detailsViewHost.ViewType = null;
|
---|
| 114 | this.detailsViewHost.Content = null;
|
---|
[2895] | 115 | }
|
---|
| 116 |
|
---|
[3176] | 117 | private void Controller_OnMouseDown(object sender, MouseEventArgs e) {
|
---|
| 118 | if (e.Clicks >= 2) {
|
---|
| 119 | IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.Location);
|
---|
| 120 | if (shape != null) {
|
---|
| 121 | IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
|
---|
| 122 | if (shapeInfo != null) {
|
---|
| 123 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 124 | MainFormManager.CreateDefaultView(op).Show();
|
---|
| 125 | HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
|
---|
| 126 | if (eventArgs != null)
|
---|
| 127 | eventArgs.Handled = true;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[2893] | 133 | #region context menu
|
---|
| 134 | private void Controller_OnShowContextMenu(object sender, EntityMenuEventArgs e) {
|
---|
| 135 | IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.MouseEventArgs.Location);
|
---|
| 136 | if (shape != null) {
|
---|
| 137 | IShapeInfo shapeInfo = shape.Tag as IShapeInfo;
|
---|
| 138 | this.shapeContextMenu.Tag = shapeInfo;
|
---|
| 139 | PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(e.MouseEventArgs.Location);
|
---|
| 140 | this.shapeContextMenu.Show(this, Point.Round(worldPoint));
|
---|
[2861] | 141 | }
|
---|
[2801] | 142 | }
|
---|
| 143 |
|
---|
[2893] | 144 | private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
|
---|
[2934] | 145 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
[2893] | 146 | if (shapeInfo != null) {
|
---|
| 147 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 148 | this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
|
---|
| 149 | this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
|
---|
[2853] | 150 | }
|
---|
[2801] | 151 | }
|
---|
| 152 |
|
---|
[2893] | 153 | private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[2934] | 154 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
[2893] | 155 | if (shapeInfo != null) {
|
---|
| 156 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 157 | MainFormManager.CreateDefaultView(op).Show();
|
---|
[2853] | 158 | }
|
---|
[2801] | 159 | }
|
---|
[2819] | 160 |
|
---|
[2893] | 161 | private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[2934] | 162 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
[2893] | 163 | if (this.VisualizationInfo.InitialShape == shapeInfo)
|
---|
| 164 | this.VisualizationInfo.InitialShape = null;
|
---|
| 165 | else
|
---|
| 166 | this.VisualizationInfo.InitialShape = shapeInfo;
|
---|
[2861] | 167 | }
|
---|
[2819] | 168 |
|
---|
[2893] | 169 | private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[2934] | 170 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
[2893] | 171 | if (shapeInfo != null) {
|
---|
| 172 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 173 | op.Breakpoint = !op.Breakpoint;
|
---|
[2861] | 174 | }
|
---|
| 175 | }
|
---|
[2893] | 176 | #endregion
|
---|
[2819] | 177 |
|
---|
[2853] | 178 | #region drag and drop
|
---|
| 179 | private void OperatorGraphView_DragEnter(object sender, DragEventArgs e) {
|
---|
| 180 | e.Effect = DragDropEffects.None;
|
---|
| 181 | Type type = e.Data.GetData("Type") as Type;
|
---|
| 182 | if ((type != null) && (typeof(IOperator).IsAssignableFrom(type))) {
|
---|
| 183 | e.Effect = DragDropEffects.Copy;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 188 | if (e.Effect != DragDropEffects.None) {
|
---|
| 189 | IOperator op = e.Data.GetData("Value") as IOperator;
|
---|
[2934] | 190 | IOperatorShapeInfo shapeInfo = Factory.CreateOperatorShapeInfo(op);
|
---|
[2893] | 191 | Point mouse = new Point(MousePosition.X, MousePosition.Y);
|
---|
[2934] | 192 | Point screen = this.graphVisualizationInfoView.PointToScreen(new Point(0, 0));
|
---|
| 193 | Point control = new Point(mouse.X - screen.X, mouse.Y - screen.Y);
|
---|
| 194 | PointF worldPoint = this.graphVisualizationInfoView.Controller.View.ViewToWorld(control);
|
---|
[2893] | 195 |
|
---|
[2934] | 196 | if (worldPoint.X < 0)
|
---|
| 197 | worldPoint.X = 0;
|
---|
| 198 | if (worldPoint.Y < 0)
|
---|
| 199 | worldPoint.Y = 0;
|
---|
| 200 |
|
---|
[2893] | 201 | shapeInfo.Location = Point.Round(worldPoint);
|
---|
[2853] | 202 | this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | #endregion
|
---|
[2899] | 206 |
|
---|
[2901] | 207 | private void tool_OnToolActivate(object sender, ToolEventArgs e) {
|
---|
| 208 | Button button = GetButtonForTool(e.Properties.Name);
|
---|
| 209 | if (button != null)
|
---|
| 210 | button.Enabled = false;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | private void tool_OnToolDeactivate(object sender, ToolEventArgs e) {
|
---|
| 214 | Button button = GetButtonForTool(e.Properties.Name);
|
---|
| 215 | if (button != null)
|
---|
| 216 | button.Enabled = true;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | private Button GetButtonForTool(string toolName) {
|
---|
| 220 | Button button = null;
|
---|
| 221 | switch (toolName) {
|
---|
| 222 | case ControllerBase.SelectionToolName:
|
---|
| 223 | button = this.selectButton;
|
---|
| 224 | break;
|
---|
| 225 | case ControllerBase.PanToolName:
|
---|
| 226 | button = this.panButton;
|
---|
| 227 | break;
|
---|
| 228 | case ControllerBase.ConnectionToolName:
|
---|
| 229 | button = this.connectButton;
|
---|
| 230 | break;
|
---|
| 231 | case ControllerBase.ZoomAreaToolName:
|
---|
| 232 | button = this.zoomAreaButton;
|
---|
| 233 | break;
|
---|
| 234 | }
|
---|
| 235 | return button;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[2899] | 238 | private void selectButton_Click(object sender, EventArgs e) {
|
---|
| 239 | ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
|
---|
[2901] | 240 | this.graphVisualizationInfoView.Controller.DeactivateAllTools();
|
---|
[2899] | 241 | }
|
---|
| 242 |
|
---|
| 243 | private void panButton_Click(object sender, EventArgs e) {
|
---|
| 244 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.PanToolName);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | private void connectButton_Click(object sender, EventArgs e) {
|
---|
| 248 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | private void relayoutButton_Click(object sender, EventArgs e) {
|
---|
| 252 | this.graphVisualizationInfoView.RelayoutGraph();
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | private void zoomAreaButton_Click(object sender, EventArgs e) {
|
---|
[2934] | 256 | this.graphVisualizationInfoView.Controller.View.ZoomFit();
|
---|
[2899] | 257 | }
|
---|
| 258 |
|
---|
| 259 | private void zoomInButton_Click(object sender, EventArgs e) {
|
---|
| 260 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | private void zoomOutButton_Click(object sender, EventArgs e) {
|
---|
| 264 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
|
---|
| 265 | }
|
---|
[2934] | 266 |
|
---|
| 267 | private void screenshotButton_Click(object sender, EventArgs e) {
|
---|
[3176] | 268 | Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
|
---|
[2934] | 269 | SaveFileDialog saveFileDialog = new SaveFileDialog();
|
---|
| 270 | saveFileDialog.Title = "Save Screenshot";
|
---|
| 271 | saveFileDialog.DefaultExt = "bmp";
|
---|
| 272 | saveFileDialog.Filter = "Bitmap|*.bmp|All Files|*.*";
|
---|
| 273 | saveFileDialog.FilterIndex = 1;
|
---|
| 274 |
|
---|
| 275 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 276 | bitmap.Save(saveFileDialog.FileName);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
[2801] | 279 | }
|
---|
| 280 | }
|
---|