[2801] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2801] | 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.Drawing;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
[4068] | 26 | using HeuristicLab.Core;
|
---|
[2801] | 27 | using HeuristicLab.MainForm;
|
---|
[4068] | 28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[8000] | 29 | using HeuristicLab.Netron.CustomTools;
|
---|
[2801] | 30 | using Netron.Diagramming.Core;
|
---|
| 31 |
|
---|
[6036] | 32 | namespace HeuristicLab.Operators.Views.GraphVisualization.Views {
|
---|
[2917] | 33 | [View("OperatorGraph View (Chart)")]
|
---|
[3361] | 34 | [Content(typeof(OperatorGraph), true)]
|
---|
[3514] | 35 | public partial class OperatorGraphView : AsynchronousContentView {
|
---|
[8000] | 36 | private ITool lastActiveTool;
|
---|
| 37 |
|
---|
[2853] | 38 | public OperatorGraphView() {
|
---|
[2801] | 39 | InitializeComponent();
|
---|
[4011] | 40 |
|
---|
[2893] | 41 | this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu);
|
---|
[2898] | 42 | this.graphVisualizationInfoView.Controller.Model.Selection.OnNewSelection += new EventHandler(Controller_SelectionChanged);
|
---|
[3176] | 43 | this.graphVisualizationInfoView.Controller.OnMouseDown += new EventHandler<MouseEventArgs>(Controller_OnMouseDown);
|
---|
[8000] | 44 | this.graphVisualizationInfoView.Controller.ParentControl.MouseDown += new MouseEventHandler(ParentControl_MouseDown);
|
---|
| 45 | this.graphVisualizationInfoView.Controller.ParentControl.MouseUp += new MouseEventHandler(ParentControl_MouseUp);
|
---|
[2901] | 46 | foreach (ITool tool in this.graphVisualizationInfoView.Controller.Tools) {
|
---|
| 47 | tool.OnToolActivate += new EventHandler<ToolEventArgs>(tool_OnToolActivate);
|
---|
| 48 | tool.OnToolDeactivate += new EventHandler<ToolEventArgs>(tool_OnToolDeactivate);
|
---|
| 49 | }
|
---|
[2801] | 50 | }
|
---|
| 51 |
|
---|
[2853] | 52 | public new OperatorGraph Content {
|
---|
| 53 | get { return (OperatorGraph)base.Content; }
|
---|
[2801] | 54 | set { base.Content = value; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[2853] | 57 | protected override void OnContentChanged() {
|
---|
[4011] | 58 | if (Content != null) {
|
---|
| 59 | bool createdVisualizationInfo = false;
|
---|
| 60 | if (this.VisualizationInfo == null) {
|
---|
| 61 | this.VisualizationInfo = new OperatorGraphVisualizationInfo(this.Content);
|
---|
| 62 | createdVisualizationInfo = true;
|
---|
| 63 | }
|
---|
| 64 | this.graphVisualizationInfoView.Content = this.VisualizationInfo;
|
---|
| 65 | if (createdVisualizationInfo)
|
---|
| 66 | this.graphVisualizationInfoView.RelayoutGraph();
|
---|
| 67 | } else {
|
---|
| 68 | this.graphVisualizationInfoView.Content = null;
|
---|
[2898] | 69 | }
|
---|
[2801] | 70 | }
|
---|
| 71 |
|
---|
[3904] | 72 | protected override void SetEnabledStateOfControls() {
|
---|
| 73 | base.SetEnabledStateOfControls();
|
---|
[3355] | 74 | if (Content == null) {
|
---|
| 75 | selectButton.Enabled = false;
|
---|
| 76 | relayoutButton.Enabled = false;
|
---|
[3523] | 77 | zoomToFitButton.Enabled = false;
|
---|
[3355] | 78 | zoomInButton.Enabled = false;
|
---|
| 79 | zoomOutButton.Enabled = false;
|
---|
| 80 | screenshotButton.Enabled = false;
|
---|
| 81 | detailsViewHost.Enabled = false;
|
---|
| 82 | connectButton.Enabled = false;
|
---|
| 83 | } else {
|
---|
| 84 | selectButton.Enabled = true;
|
---|
| 85 | relayoutButton.Enabled = true;
|
---|
[3523] | 86 | zoomToFitButton.Enabled = true;
|
---|
[3355] | 87 | zoomInButton.Enabled = true;
|
---|
| 88 | zoomOutButton.Enabled = true;
|
---|
| 89 | screenshotButton.Enabled = true;
|
---|
| 90 | detailsViewHost.Enabled = true;
|
---|
[4435] | 91 | connectButton.Enabled = !ReadOnly;
|
---|
[3355] | 92 | }
|
---|
[3514] | 93 | }
|
---|
[3355] | 94 |
|
---|
[3386] | 95 | private OperatorGraphVisualizationInfo VisualizationInfo {
|
---|
| 96 | get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
|
---|
[2853] | 97 | set { this.Content.VisualizationInfo = value; }
|
---|
| 98 | }
|
---|
[2819] | 99 |
|
---|
[2898] | 100 | private void Controller_SelectionChanged(object sender, EventArgs e) {
|
---|
[3514] | 101 | this.detailsViewHost.ViewType = null;
|
---|
| 102 | this.detailsViewHost.Content = null;
|
---|
| 103 |
|
---|
[2895] | 104 | CollectionBase<IDiagramEntity> selectedObjects = this.graphVisualizationInfoView.Controller.Model.Selection.SelectedItems;
|
---|
| 105 | if (selectedObjects.Count == 1) {
|
---|
| 106 | IShape shape = selectedObjects[0] as IShape;
|
---|
| 107 | if (shape != null) {
|
---|
[2934] | 108 | IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
|
---|
[2898] | 109 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 110 | this.detailsViewHost.Content = op;
|
---|
[2895] | 111 | }
|
---|
| 112 | }
|
---|
[3514] | 113 |
|
---|
[2898] | 114 | IConnector connector = this.graphVisualizationInfoView.Controller.Model.Selection.Connector;
|
---|
| 115 | if (connector != null) {
|
---|
| 116 | IShape shape = connector.Parent as IShape;
|
---|
| 117 | string connectorName = connector.Name;
|
---|
| 118 | if (shape == null) {
|
---|
| 119 | shape = connector.AttachedTo.Parent as IShape; //connection connector selected
|
---|
| 120 | connectorName = connector.AttachedTo.Name;
|
---|
| 121 | }
|
---|
| 122 | if (shape != null) {
|
---|
[2934] | 123 | IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
|
---|
[2898] | 124 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 125 | if (connectorName != "Predecessor") {
|
---|
| 126 | IParameter parameter = op.Parameters.Where(p => p.Name == connectorName).First();
|
---|
[2949] | 127 | this.detailsViewHost.ViewType = null;
|
---|
[2898] | 128 | this.detailsViewHost.Content = parameter;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
[3514] | 132 |
|
---|
[2895] | 133 | }
|
---|
| 134 |
|
---|
[3176] | 135 | private void Controller_OnMouseDown(object sender, MouseEventArgs e) {
|
---|
| 136 | if (e.Clicks >= 2) {
|
---|
| 137 | IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.Location);
|
---|
| 138 | if (shape != null) {
|
---|
| 139 | IOperatorShapeInfo shapeInfo = shape.Tag as IOperatorShapeInfo;
|
---|
| 140 | if (shapeInfo != null) {
|
---|
| 141 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
[10093] | 142 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
| 143 | var outermostViewHost = mainForm.ShowContentInOutermostViewHost(op, this);
|
---|
[10103] | 144 | if (outermostViewHost != null) {
|
---|
| 145 | outermostViewHost.ActiveView.ReadOnly = ReadOnly;
|
---|
| 146 | outermostViewHost.ActiveView.Locked = Locked;
|
---|
| 147 | }
|
---|
[3176] | 148 | HandledMouseEventArgs eventArgs = e as HandledMouseEventArgs;
|
---|
| 149 | if (eventArgs != null)
|
---|
| 150 | eventArgs.Handled = true;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[8000] | 156 | void ParentControl_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 157 | if (e.Button == MouseButtons.Middle) {
|
---|
[8860] | 158 | lastActiveTool = graphVisualizationInfoView.Controller.ActiveTool;
|
---|
| 159 | graphVisualizationInfoView.Controller.ActivateTool(CustomPanTool.ToolName);
|
---|
[8000] | 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | void ParentControl_MouseUp(object sender, MouseEventArgs e) {
|
---|
| 164 | if (e.Button == MouseButtons.Middle) {
|
---|
[8860] | 165 | var activeTool = graphVisualizationInfoView.Controller.ActiveTool;
|
---|
| 166 | graphVisualizationInfoView.Controller.DeactivateTool(activeTool);
|
---|
| 167 | if (lastActiveTool != null && !(lastActiveTool is ZoomToolBase))
|
---|
| 168 | graphVisualizationInfoView.Controller.ActivateTool(lastActiveTool.Name);
|
---|
[8000] | 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[2893] | 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));
|
---|
[2861] | 180 | }
|
---|
[2801] | 181 | }
|
---|
| 182 |
|
---|
[2893] | 183 | private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
|
---|
[2934] | 184 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
[2893] | 185 | if (shapeInfo != null) {
|
---|
| 186 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 187 | this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op;
|
---|
[3514] | 188 | this.initialToolStripMenuItem.Enabled = !ReadOnly && !Locked;
|
---|
[2893] | 189 | this.breakPointToolStripMenuItem.Checked = op.Breakpoint;
|
---|
[5224] | 190 | this.breakPointToolStripMenuItem.Enabled = !Locked;
|
---|
[2853] | 191 | }
|
---|
[2801] | 192 | }
|
---|
| 193 |
|
---|
[2893] | 194 | private void openViewToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[2934] | 195 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
[2893] | 196 | if (shapeInfo != null) {
|
---|
| 197 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
[10093] | 198 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
| 199 | var outermostViewHost = mainForm.ShowContentInOutermostViewHost(op, this);
|
---|
[10103] | 200 | if (outermostViewHost != null) {
|
---|
| 201 | outermostViewHost.ActiveView.ReadOnly = ReadOnly;
|
---|
| 202 | outermostViewHost.ActiveView.Locked = Locked;
|
---|
| 203 | }
|
---|
[2853] | 204 | }
|
---|
[2801] | 205 | }
|
---|
[2819] | 206 |
|
---|
[2893] | 207 | private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[4011] | 208 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
| 209 | if (this.VisualizationInfo.InitialShape == shapeInfo)
|
---|
| 210 | this.VisualizationInfo.InitialShape = null;
|
---|
| 211 | else
|
---|
| 212 | this.VisualizationInfo.InitialShape = shapeInfo;
|
---|
[2861] | 213 | }
|
---|
[2819] | 214 |
|
---|
[2893] | 215 | private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[4011] | 216 | IOperatorShapeInfo shapeInfo = this.shapeContextMenu.Tag as IOperatorShapeInfo;
|
---|
| 217 | if (shapeInfo != null) {
|
---|
| 218 | IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo);
|
---|
| 219 | op.Breakpoint = !op.Breakpoint;
|
---|
| 220 | }
|
---|
[2861] | 221 | }
|
---|
[2893] | 222 | #endregion
|
---|
[2819] | 223 |
|
---|
[2853] | 224 | #region drag and drop
|
---|
[3556] | 225 | private void OperatorGraphView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
[2853] | 226 | e.Effect = DragDropEffects.None;
|
---|
[5837] | 227 | if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IOperator)) {
|
---|
[3694] | 228 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
[3556] | 229 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
[5744] | 230 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
| 231 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
| 232 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
[2853] | 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | private void OperatorGraphView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 237 | if (e.Effect != DragDropEffects.None) {
|
---|
[5837] | 238 | IOperator op = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IOperator;
|
---|
[5744] | 239 | if (e.Effect.HasFlag(DragDropEffects.Copy)) op = (IOperator)op.Clone();
|
---|
[3386] | 240 | IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
|
---|
[2893] | 241 | Point mouse = new Point(MousePosition.X, MousePosition.Y);
|
---|
[2934] | 242 | Point screen = this.graphVisualizationInfoView.PointToScreen(new Point(0, 0));
|
---|
| 243 | Point control = new Point(mouse.X - screen.X, mouse.Y - screen.Y);
|
---|
| 244 | PointF worldPoint = this.graphVisualizationInfoView.Controller.View.ViewToWorld(control);
|
---|
[2893] | 245 |
|
---|
[2934] | 246 | if (worldPoint.X < 0)
|
---|
| 247 | worldPoint.X = 0;
|
---|
| 248 | if (worldPoint.Y < 0)
|
---|
| 249 | worldPoint.Y = 0;
|
---|
| 250 |
|
---|
[2893] | 251 | shapeInfo.Location = Point.Round(worldPoint);
|
---|
[2853] | 252 | this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | #endregion
|
---|
[2899] | 256 |
|
---|
[2901] | 257 | private void tool_OnToolActivate(object sender, ToolEventArgs e) {
|
---|
| 258 | Button button = GetButtonForTool(e.Properties.Name);
|
---|
| 259 | if (button != null)
|
---|
| 260 | button.Enabled = false;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | private void tool_OnToolDeactivate(object sender, ToolEventArgs e) {
|
---|
| 264 | Button button = GetButtonForTool(e.Properties.Name);
|
---|
| 265 | if (button != null)
|
---|
| 266 | button.Enabled = true;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | private Button GetButtonForTool(string toolName) {
|
---|
| 270 | Button button = null;
|
---|
| 271 | switch (toolName) {
|
---|
| 272 | case ControllerBase.SelectionToolName:
|
---|
| 273 | button = this.selectButton;
|
---|
| 274 | break;
|
---|
| 275 | case ControllerBase.ConnectionToolName:
|
---|
| 276 | button = this.connectButton;
|
---|
| 277 | break;
|
---|
| 278 | case ControllerBase.ZoomAreaToolName:
|
---|
[3523] | 279 | button = this.zoomToFitButton;
|
---|
[2901] | 280 | break;
|
---|
| 281 | }
|
---|
| 282 | return button;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[2899] | 285 | private void selectButton_Click(object sender, EventArgs e) {
|
---|
| 286 | ITool tool = this.graphVisualizationInfoView.Controller.Tools.Where(t => t.Name == ControllerBase.SelectionToolName).First();
|
---|
[2901] | 287 | this.graphVisualizationInfoView.Controller.DeactivateAllTools();
|
---|
[2899] | 288 | }
|
---|
| 289 |
|
---|
| 290 | private void connectButton_Click(object sender, EventArgs e) {
|
---|
| 291 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ConnectionToolName);
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | private void relayoutButton_Click(object sender, EventArgs e) {
|
---|
| 295 | this.graphVisualizationInfoView.RelayoutGraph();
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | private void zoomAreaButton_Click(object sender, EventArgs e) {
|
---|
[2934] | 299 | this.graphVisualizationInfoView.Controller.View.ZoomFit();
|
---|
[2899] | 300 | }
|
---|
| 301 |
|
---|
| 302 | private void zoomInButton_Click(object sender, EventArgs e) {
|
---|
| 303 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomInToolName);
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | private void zoomOutButton_Click(object sender, EventArgs e) {
|
---|
| 307 | this.graphVisualizationInfoView.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
|
---|
| 308 | }
|
---|
[2934] | 309 |
|
---|
| 310 | private void screenshotButton_Click(object sender, EventArgs e) {
|
---|
[3176] | 311 | Bitmap bitmap = ImageExporter.FromBundle(new Bundle(this.graphVisualizationInfoView.Controller.Model.Paintables), this.graphVisualizationInfoView.Controller.View.Graphics);
|
---|
[2934] | 312 | SaveFileDialog saveFileDialog = new SaveFileDialog();
|
---|
| 313 | saveFileDialog.Title = "Save Screenshot";
|
---|
[3753] | 314 | saveFileDialog.DefaultExt = "png";
|
---|
| 315 | saveFileDialog.Filter = "Portable Network Graphics|*.png|All Files|*.*";
|
---|
[2934] | 316 | saveFileDialog.FilterIndex = 1;
|
---|
[3514] | 317 | saveFileDialog.AddExtension = true;
|
---|
[2934] | 318 |
|
---|
| 319 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 320 | bitmap.Save(saveFileDialog.FileName);
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
[2801] | 323 | }
|
---|
| 324 | }
|
---|