[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)")]
|
---|
[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 | connectButton.Enabled = !ReadOnly;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[3386] | 108 | private OperatorGraphVisualizationInfo VisualizationInfo {
|
---|
| 109 | get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
|
---|
[2853] | 110 | set { this.Content.VisualizationInfo = value; }
|
---|
| 111 | }
|
---|
[2819] | 112 |
|
---|
[2898] | 113 | private void Controller_SelectionChanged(object sender, EventArgs e) {
|
---|
[2895] | 114 | CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
|
---|
[2898] | 115 | this.detailsViewHost.ViewType = null;
|
---|
[2895] | 116 | if (selectedObjects.Count == 1) {
|
---|
| 117 | IShape shape = selectedObjects[0] as IShape;
|
---|
| 118 | if (shape != null) {
|
---|
[2934] | 119 | IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
|
---|
[2898] | 120 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
[2949] | 121 | this.detailsViewHost.ViewType = null;
|
---|
[2898] | 122 | this.detailsViewHost.Content = op;
|
---|
[2895] | 123 | return;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[2898] | 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) {
|
---|
[2934] | 135 | IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
|
---|
[2898] | 136 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 137 | if (connectorName != "Predecessor") {
|
---|
| 138 | IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
|
---|
[2949] | 139 | this.detailsViewHost.ViewType = null;
|
---|
[2898] | 140 | this.detailsViewHost.Content = parameter;
|
---|
| 141 | return;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | this.detailsViewHost.ViewType = null;
|
---|
| 146 | this.detailsViewHost.Content = null;
|
---|
[2895] | 147 | }
|
---|
| 148 |
|
---|
[3176] | 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);
|
---|
[3423] | 156 | IContentView view = MainFormManager.CreateDefaultView(op);
|
---|
| 157 | if (view != null) {
|
---|
| 158 | view.ReadOnly = this.ReadOnly;
|
---|
| 159 | view.Locked = this.Locked;
|
---|
| 160 | view.Show();
|
---|
| 161 | }
|
---|
[3176] | 162 | HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
|
---|
| 163 | if (eventArgs != null)
|
---|
| 164 | eventArgs.Handled = true;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[2893] | 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));
|
---|
[2861] | 178 | }
|
---|
[2801] | 179 | }
|
---|
| 180 |
|
---|
[2893] | 181 | private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
|
---|
[2934] | 182 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
[2893] | 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;
|
---|
[2853] | 187 | }
|
---|
[2801] | 188 | }
|
---|
| 189 |
|
---|
[2893] | 190 | private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[2934] | 191 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
[2893] | 192 | if (shapeInfo != null) {
|
---|
| 193 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
[3423] | 194 | IContentView view = MainFormManager.CreateDefaultView(op);
|
---|
| 195 | view.ReadOnly = this.ReadOnly;
|
---|
| 196 | view.Locked = this.Locked;
|
---|
| 197 | view.Show();
|
---|
[2853] | 198 | }
|
---|
[2801] | 199 | }
|
---|
[2819] | 200 |
|
---|
[2893] | 201 | private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[3355] | 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 | }
|
---|
[2861] | 209 | }
|
---|
[2819] | 210 |
|
---|
[2893] | 211 | private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[3355] | 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 | }
|
---|
[2861] | 218 | }
|
---|
| 219 | }
|
---|
[2893] | 220 | #endregion
|
---|
[2819] | 221 |
|
---|
[2853] | 222 | #region drag and drop
|
---|
| 223 | private void OperatorGraphView_DragEnter(object sender, DragEventArgs e) {
|
---|
| 224 | e.Effect = DragDropEffects.None;
|
---|
[3355] | 225 | if (ReadOnly)
|
---|
| 226 | return;
|
---|
[2853] | 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;
|
---|
[3386] | 236 | IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
|
---|
[2893] | 237 | Point mouse = new Point(MousePosition.X, MousePosition.Y);
|
---|
[2934] | 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);
|
---|
[2893] | 241 |
|
---|
[2934] | 242 | if (worldPoint.X < 0)
|
---|
| 243 | worldPoint.X = 0;
|
---|
| 244 | if (worldPoint.Y < 0)
|
---|
| 245 | worldPoint.Y = 0;
|
---|
| 246 |
|
---|
[2893] | 247 | shapeInfo.Location = Point.Round(worldPoint);
|
---|
[2853] | 248 | this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 | #endregion
|
---|
[2899] | 252 |
|
---|
[2901] | 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 |
|
---|
[2899] | 284 | private void selectButton_Click(object sender, EventArgs e) {
|
---|
| 285 | ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
|
---|
[2901] | 286 | this.graphVisualizationInfoView.Controller.DeactivateAllTools();
|
---|
[2899] | 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) {
|
---|
[2934] | 302 | this.graphVisualizationInfoView.Controller.View.ZoomFit();
|
---|
[2899] | 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 | }
|
---|
[2934] | 312 |
|
---|
| 313 | private void screenshotButton_Click(object sender, EventArgs e) {
|
---|
[3176] | 314 | Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
|
---|
[2934] | 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 | }
|
---|
[2801] | 325 | }
|
---|
| 326 | }
|
---|