- Timestamp:
- 02/24/10 17:58:03 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Core/Scene graph/Model.cs
r2768 r2861 490 490 get 491 491 { 492 throw new System.NotImplementedException();492 return CurrentPage.Connections; 493 493 } 494 494 -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Diagram elements/Connectors/Connector.cs
r2768 r2861 3 3 using System.Drawing.Drawing2D; 4 4 5 namespace Netron.Diagramming.Core 6 { 7 // ---------------------------------------------------------------------- 8 /// <summary> 9 /// The default connector. Represents an endpoint of a connection or a 10 /// location of a bundle to which a connection can be attached. 11 /// </summary> 12 // ---------------------------------------------------------------------- 13 public partial class Connector : ConnectorBase 14 { 15 #region Fields 16 17 // ------------------------------------------------------------------ 18 /// <summary> 19 /// Implementation of IVersion - the current version of 20 /// Connector. 21 /// </summary> 22 // ------------------------------------------------------------------ 23 protected const double connectorVersion = 1.0; 24 25 // ------------------------------------------------------------------ 26 /// <summary> 27 /// Specifies how the connector is drawn on the canvas when 'IsVisible' 28 /// is true. The default style is 'Simple', which is a transparent 29 /// background with a blue 'x' (similar to Visio). 30 /// </summary> 31 // ------------------------------------------------------------------ 32 protected ConnectorStyle myStyle = ConnectorStyle.Simple; 33 34 #endregion 35 36 #region Properties 37 38 // ------------------------------------------------------------------ 39 /// <summary> 40 /// Gets the current version. 41 /// </summary> 42 // ------------------------------------------------------------------ 43 public override double Version 44 { 45 get 46 { 47 return connectorVersion; 48 } 5 namespace Netron.Diagramming.Core { 6 // ---------------------------------------------------------------------- 7 /// <summary> 8 /// The default connector. Represents an endpoint of a connection or a 9 /// location of a bundle to which a connection can be attached. 10 /// </summary> 11 // ---------------------------------------------------------------------- 12 public partial class Connector : ConnectorBase { 13 #region Fields 14 15 // ------------------------------------------------------------------ 16 /// <summary> 17 /// Implementation of IVersion - the current version of 18 /// Connector. 19 /// </summary> 20 // ------------------------------------------------------------------ 21 protected const double connectorVersion = 1.0; 22 23 // ------------------------------------------------------------------ 24 /// <summary> 25 /// Specifies how the connector is drawn on the canvas when 'IsVisible' 26 /// is true. The default style is 'Simple', which is a transparent 27 /// background with a blue 'x' (similar to Visio). 28 /// </summary> 29 // ------------------------------------------------------------------ 30 protected ConnectorStyle myStyle = ConnectorStyle.Simple; 31 32 #endregion 33 34 #region Properties 35 36 // ------------------------------------------------------------------ 37 /// <summary> 38 /// Gets the current version. 39 /// </summary> 40 // ------------------------------------------------------------------ 41 public override double Version { 42 get { 43 return connectorVersion; 44 } 45 } 46 47 // ------------------------------------------------------------------ 48 /// <summary> 49 /// Gets or sets how the connector is drawn on the canvas when 50 /// 'IsVisible' is true. 51 /// </summary> 52 // ------------------------------------------------------------------ 53 public ConnectorStyle ConnectorStyle { 54 get { 55 return this.myStyle; 56 } 57 set { 58 this.myStyle = value; 59 } 60 } 61 62 // ------------------------------------------------------------------ 63 /// <summary> 64 /// Gets the friendly name of the entity to be displayed in the UI 65 /// </summary> 66 /// <value></value> 67 // ------------------------------------------------------------------ 68 public override string EntityName { 69 get { return "Connector"; } 70 } 71 72 // ------------------------------------------------------------------ 73 /// <summary> 74 /// The bounds of the paintable entity. 75 /// </summary> 76 /// <value></value> 77 // ------------------------------------------------------------------ 78 public override Rectangle Rectangle { 79 get { 80 return new Rectangle(Point.X - 2, Point.Y - 2, 4, 4); 81 } 82 //set { Point = value.Location; 83 //TODO: think about what to do when setting the size } 84 } 85 86 #endregion 87 88 #region Constructor 89 90 // ------------------------------------------------------------------ 91 /// <summary> 92 /// Initializes a new instance of the <see cref="T:Connector"/> class. 93 /// </summary> 94 /// <param name="site">The site.</param> 95 // ------------------------------------------------------------------ 96 public Connector(IModel site) 97 : base(site) { 98 } 99 100 // ------------------------------------------------------------------ 101 /// <summary> 102 /// Initializes a new instance of the <see cref="T:Connector"/> class. 103 /// </summary> 104 /// <param name="p">The p.</param> 105 /// <param name="site">The site.</param> 106 // ------------------------------------------------------------------ 107 public Connector(Point p, IModel site) 108 : base(p, site) { 109 } 110 111 // ------------------------------------------------------------------ 112 /// <summary> 113 /// Initializes a new instance of the <see cref="T:Connector"/> class. 114 /// </summary> 115 /// <param name="p">The p.</param> 116 // ------------------------------------------------------------------ 117 public Connector(Point p) 118 : base(p) { 119 } 120 121 #endregion 122 123 #region Methods 124 125 // ------------------------------------------------------------------ 126 /// <summary> 127 /// Paints the connector on the canvas. 128 /// </summary> 129 /// <param name="g"></param> 130 // ------------------------------------------------------------------ 131 public override void Paint(Graphics g) { 132 if (g == null) { 133 throw new ArgumentNullException( 134 "The Graphics object is 'null'"); 135 } 136 137 if (Hovered || IsSelected) { 138 Rectangle area = Rectangle; 139 area.Inflate(3, 3); 140 g.DrawRectangle( 141 ArtPalette.ConnectionHighlightPen, 142 area); 143 //g.FillRectangle( 144 // Brushes.Green, 145 // Point.X - 4, 146 // Point.Y - 4, 147 // 8, 148 // 8); 149 } else { 150 if (Visible) { 151 switch (this.myStyle) { 152 case ConnectorStyle.Simple: 153 DrawSimpleConnector(g); 154 break; 155 156 case ConnectorStyle.Round: 157 break; 158 159 case ConnectorStyle.Square: 160 DrawSquareConnector(g); 161 break; 162 } 163 164 if (this.mShowName) { 165 DrawName(g); 166 } 49 167 } 50 51 // ------------------------------------------------------------------ 52 /// <summary> 53 /// Gets or sets how the connector is drawn on the canvas when 54 /// 'IsVisible' is true. 55 /// </summary> 56 // ------------------------------------------------------------------ 57 public ConnectorStyle ConnectorStyle 58 { 59 get 60 { 61 return this.myStyle; 62 } 63 set 64 { 65 this.myStyle = value; 66 } 168 } 169 } 170 171 // ------------------------------------------------------------------ 172 /// <summary> 173 /// Draws the name of this connector. 174 /// </summary> 175 /// <param name="g">Graphics</param> 176 // ------------------------------------------------------------------ 177 void DrawName(Graphics g) { 178 Size size = Size.Round( 179 g.MeasureString(mName, mFont)); 180 181 int xOffset = (size.Width - Rectangle.Width) / 2; 182 int yOffset = (size.Height - Rectangle.Height) / 2; 183 184 System.Drawing.Point location = Rectangle.Location; 185 186 switch (this.mNameLocation) { 187 case ConnectorNameLocation.Top: 188 location = new Point( 189 Rectangle.X - xOffset, 190 Rectangle.Y - size.Height); 191 break; 192 193 case ConnectorNameLocation.Bottom: 194 location = new Point( 195 Rectangle.X - xOffset, 196 Rectangle.Bottom + size.Height); 197 break; 198 199 case ConnectorNameLocation.Left: 200 location = new Point( 201 Rectangle.X - size.Width, 202 Rectangle.Y - yOffset); 203 break; 204 205 case ConnectorNameLocation.Right: 206 location = new Point( 207 Rectangle.Right, 208 Rectangle.Y - yOffset); 209 break; 210 } 211 212 Rectangle textArea = new Rectangle(location, size); 213 StringFormat format = new StringFormat(); 214 format.FormatFlags = StringFormatFlags.FitBlackBox; 215 g.DrawString( 216 mName, 217 mFont, 218 new SolidBrush(mForeColor), 219 location); 220 } 221 222 // ------------------------------------------------------------------ 223 /// <summary> 224 /// Draws a blue 'x' using 'Dot' as the line style, with a transparent 225 /// color. 226 /// </summary> 227 /// <param name="g">Graphics</param> 228 // ------------------------------------------------------------------ 229 protected virtual void DrawSimpleConnector(Graphics g) { 230 Pen pen = ArtPalette.GetSimpleConnectorPenStyle().DrawingPen(); 231 Brush brush = 232 ArtPalette.GetSimpleConnectorPaintStyle().GetBrush( 233 this.Rectangle); 234 235 GraphicsPath path = new GraphicsPath(); 236 // Diagonal line from top left to bottom right. 237 g.DrawLine(pen, this.TopLeftCorner, this.BottomRightCorner); 238 239 240 // Diagonal line from top right to bottom lrft. 241 g.DrawLine(pen, this.TopRightCorner, this.BottomLeftCorner); 242 } 243 244 protected virtual void DrawSquareConnector(Graphics g) { 245 Pen pen = ArtPalette.GetSimpleConnectorPenStyle().DrawingPen(); 246 Brush brush = ArtPalette.GetSimpleConnectorPaintStyle().GetBrush(this.Rectangle); 247 g.DrawRectangle(pen, this.Rectangle); 248 } 249 250 251 // ------------------------------------------------------------------ 252 /// <summary> 253 /// Tests if the mouse hits this connector. 254 /// </summary> 255 /// <param name="p">Point</param> 256 /// <returns>bool</returns> 257 // ------------------------------------------------------------------ 258 public override bool Hit(Point p) { 259 Point a = p; 260 Point b = Point; 261 b.Offset(-7, -7); 262 //a.Offset(-1,-1); 263 Rectangle r = new Rectangle(a, new Size(0, 0)); 264 Rectangle d = new Rectangle(b, new Size(15, 15)); 265 return d.Contains(r); 266 } 267 268 // ------------------------------------------------------------------ 269 /// <summary> 270 /// Invalidates the connector 271 /// </summary> 272 // ------------------------------------------------------------------ 273 public override void Invalidate() { 274 Point p = Point; 275 p.Offset(-5, -5); 276 if (Model != null) 277 Model.RaiseOnInvalidateRectangle( 278 new Rectangle(p, new Size(10, 10))); 279 } 280 281 // ------------------------------------------------------------------ 282 /// <summary> 283 /// Moves the connector with the given shift-vector. 284 /// </summary> 285 /// <param name="p">Point</param> 286 // ------------------------------------------------------------------ 287 public override void MoveBy(Point p) { 288 Point pt = new Point(this.Point.X + p.X, this.Point.Y + p.Y); 289 IConnection con = null; 290 Point p1 = Point.Empty, p2 = Point.Empty; 291 292 Rectangle rec = new Rectangle( 293 Point.X - 10, 294 Point.Y - 10, 295 20, 296 20); 297 298 this.Point = pt; 299 300 #region Case of connection 301 if (typeof(IConnection).IsInstanceOfType(this.Parent)) { 302 (Parent as IConnection).Invalidate(); 303 } 304 #endregion 305 306 #region Case of attached connectors 307 for (int k = 0; k < AttachedConnectors.Count; k++) { 308 if (typeof(IConnection).IsInstanceOfType(AttachedConnectors[k].Parent)) { 309 //keep a reference to the two points so we can invalidate the region afterwards 310 con = AttachedConnectors[k].Parent as IConnection; 311 p1 = con.From.Point; 312 p2 = con.To.Point; 67 313 } 68 69 // ------------------------------------------------------------------ 70 /// <summary> 71 /// Gets the friendly name of the entity to be displayed in the UI 72 /// </summary> 73 /// <value></value> 74 // ------------------------------------------------------------------ 75 public override string EntityName 76 { 77 get { return "Connector"; } 314 AttachedConnectors[k].MoveBy(p); 315 if (con != null) { 316 //invalidate the 'before the move'-region 317 Rectangle f = new Rectangle(p1, new Size(10, 10)); 318 Rectangle t = new Rectangle(p2, new Size(10, 10)); 319 Model.RaiseOnInvalidateRectangle(Rectangle.Union(f, t)); 320 //finally, invalidate the region where the connection is now 321 (AttachedConnectors[k].Parent as IConnection).Invalidate(); 78 322 } 79 80 // ------------------------------------------------------------------ 81 /// <summary> 82 /// The bounds of the paintable entity. 83 /// </summary> 84 /// <value></value> 85 // ------------------------------------------------------------------ 86 public override Rectangle Rectangle 87 { 88 get 89 { 90 return new Rectangle(Point.X - 2, Point.Y - 2, 4, 4); 91 } 92 //set { Point = value.Location; 93 //TODO: think about what to do when setting the size } 94 } 95 96 #endregion 97 98 #region Constructor 99 100 // ------------------------------------------------------------------ 101 /// <summary> 102 /// Initializes a new instance of the <see cref="T:Connector"/> class. 103 /// </summary> 104 /// <param name="site">The site.</param> 105 // ------------------------------------------------------------------ 106 public Connector(IModel site):base(site) 107 { 108 } 109 110 // ------------------------------------------------------------------ 111 /// <summary> 112 /// Initializes a new instance of the <see cref="T:Connector"/> class. 113 /// </summary> 114 /// <param name="p">The p.</param> 115 /// <param name="site">The site.</param> 116 // ------------------------------------------------------------------ 117 public Connector(Point p, IModel site):base(p, site) 118 { 119 } 120 121 // ------------------------------------------------------------------ 122 /// <summary> 123 /// Initializes a new instance of the <see cref="T:Connector"/> class. 124 /// </summary> 125 /// <param name="p">The p.</param> 126 // ------------------------------------------------------------------ 127 public Connector(Point p) : base(p) 128 { 129 } 130 131 #endregion 132 133 #region Methods 134 135 // ------------------------------------------------------------------ 136 /// <summary> 137 /// Paints the connector on the canvas. 138 /// </summary> 139 /// <param name="g"></param> 140 // ------------------------------------------------------------------ 141 public override void Paint(Graphics g) 142 { 143 if (g == null) 144 { 145 throw new ArgumentNullException( 146 "The Graphics object is 'null'"); 147 } 148 149 if (Hovered || IsSelected) 150 { 151 Rectangle area = Rectangle; 152 area.Inflate(3, 3); 153 g.DrawRectangle( 154 ArtPalette.ConnectionHighlightPen, 155 area); 156 //g.FillRectangle( 157 // Brushes.Green, 158 // Point.X - 4, 159 // Point.Y - 4, 160 // 8, 161 // 8); 162 } 163 else 164 { 165 if (Visible) 166 { 167 switch (this.myStyle) 168 { 169 case ConnectorStyle.Simple : 170 DrawSimpleConnector(g); 171 break; 172 173 case ConnectorStyle.Round : 174 break; 175 176 case ConnectorStyle.Square : 177 break; 178 } 179 180 if (this.mShowName) 181 { 182 DrawName(g); 183 } 184 } 185 } 186 } 187 188 // ------------------------------------------------------------------ 189 /// <summary> 190 /// Draws the name of this connector. 191 /// </summary> 192 /// <param name="g">Graphics</param> 193 // ------------------------------------------------------------------ 194 void DrawName(Graphics g) 195 { 196 Size size = Size.Round( 197 g.MeasureString(mName, mFont)); 198 199 int xOffset = (size.Width - Rectangle.Width) / 2; 200 int yOffset = (size.Height - Rectangle.Height) / 2; 201 202 System.Drawing.Point location = Rectangle.Location; 203 204 switch (this.mNameLocation) 205 { 206 case ConnectorNameLocation.Top: 207 location = new Point( 208 Rectangle.X - xOffset, 209 Rectangle.Y - size.Height); 210 break; 211 212 case ConnectorNameLocation.Bottom: 213 location = new Point( 214 Rectangle.X - xOffset, 215 Rectangle.Bottom + size.Height); 216 break; 217 218 case ConnectorNameLocation.Left: 219 location = new Point( 220 Rectangle.X - size.Width, 221 Rectangle.Y - yOffset); 222 break; 223 224 case ConnectorNameLocation.Right: 225 location = new Point( 226 Rectangle.Right, 227 Rectangle.Y - yOffset); 228 break; 229 } 230 231 Rectangle textArea = new Rectangle(location, size); 232 StringFormat format = new StringFormat(); 233 format.FormatFlags = StringFormatFlags.FitBlackBox; 234 g.DrawString( 235 mName, 236 mFont, 237 new SolidBrush(mForeColor), 238 location); 239 } 240 241 // ------------------------------------------------------------------ 242 /// <summary> 243 /// Draws a blue 'x' using 'Dot' as the line style, with a transparent 244 /// color. 245 /// </summary> 246 /// <param name="g">Graphics</param> 247 // ------------------------------------------------------------------ 248 protected virtual void DrawSimpleConnector(Graphics g) 249 { 250 Pen pen = ArtPalette.GetSimpleConnectorPenStyle().DrawingPen(); 251 Brush brush = 252 ArtPalette.GetSimpleConnectorPaintStyle().GetBrush( 253 this.Rectangle); 254 255 GraphicsPath path = new GraphicsPath(); 256 // Diagonal line from top left to bottom right. 257 g.DrawLine(pen, this.TopLeftCorner, this.BottomRightCorner); 258 259 260 // Diagonal line from top right to bottom lrft. 261 g.DrawLine(pen, this.TopRightCorner, this.BottomLeftCorner); 262 } 263 264 // ------------------------------------------------------------------ 265 /// <summary> 266 /// Tests if the mouse hits this connector. 267 /// </summary> 268 /// <param name="p">Point</param> 269 /// <returns>bool</returns> 270 // ------------------------------------------------------------------ 271 public override bool Hit(Point p) 272 { 273 Point a = p; 274 Point b = Point; 275 b.Offset(-7,-7); 276 //a.Offset(-1,-1); 277 Rectangle r = new Rectangle(a,new Size(0,0)); 278 Rectangle d = new Rectangle(b, new Size(15,15)); 279 return d.Contains(r); 280 } 281 282 // ------------------------------------------------------------------ 283 /// <summary> 284 /// Invalidates the connector 285 /// </summary> 286 // ------------------------------------------------------------------ 287 public override void Invalidate() 288 { 289 Point p = Point; 290 p.Offset(-5,-5); 291 if(Model!=null) 292 Model.RaiseOnInvalidateRectangle( 293 new Rectangle(p,new Size(10,10))); 294 } 295 296 // ------------------------------------------------------------------ 297 /// <summary> 298 /// Moves the connector with the given shift-vector. 299 /// </summary> 300 /// <param name="p">Point</param> 301 // ------------------------------------------------------------------ 302 public override void MoveBy(Point p) 303 { 304 Point pt = new Point(this.Point.X + p.X, this.Point.Y + p.Y); 305 IConnection con = null; 306 Point p1 = Point.Empty, p2 = Point.Empty; 307 308 Rectangle rec = new Rectangle( 309 Point.X - 10, 310 Point.Y - 10, 311 20, 312 20); 313 314 this.Point = pt; 315 316 #region Case of connection 317 if (typeof(IConnection).IsInstanceOfType(this.Parent)) 318 { 319 (Parent as IConnection).Invalidate(); 320 } 321 #endregion 322 323 #region Case of attached connectors 324 for (int k = 0; k < AttachedConnectors.Count; k++) 325 { 326 if (typeof(IConnection).IsInstanceOfType(AttachedConnectors[k].Parent)) 327 { 328 //keep a reference to the two points so we can invalidate the region afterwards 329 con = AttachedConnectors[k].Parent as IConnection; 330 p1 = con.From.Point; 331 p2 = con.To.Point; 332 } 333 AttachedConnectors[k].MoveBy(p); 334 if (con != null) 335 { 336 //invalidate the 'before the move'-region 337 Rectangle f = new Rectangle(p1, new Size(10, 10)); 338 Rectangle t = new Rectangle(p2, new Size(10, 10)); 339 Model.RaiseOnInvalidateRectangle(Rectangle.Union(f, t)); 340 //finally, invalidate the region where the connection is now 341 (AttachedConnectors[k].Parent as IConnection).Invalidate(); 342 } 343 } 344 #endregion 345 //invalidate this connector, since it's been moved 346 Invalidate(rec);//before the move 347 this.Invalidate();//after the move 348 349 } 350 351 // ------------------------------------------------------------------ 352 /// <summary> 353 /// Moves the connector with the given shift-vector 354 /// </summary> 355 /// <param name="x">The x.</param> 356 /// <param name="y">The y.</param> 357 // ------------------------------------------------------------------ 358 public void MoveBy(int x, int y) 359 { 360 Point pt = new Point( x, y); 361 MoveBy(pt); 362 } 363 364 #endregion 365 } 323 } 324 #endregion 325 //invalidate this connector, since it's been moved 326 Invalidate(rec);//before the move 327 this.Invalidate();//after the move 328 329 } 330 331 // ------------------------------------------------------------------ 332 /// <summary> 333 /// Moves the connector with the given shift-vector 334 /// </summary> 335 /// <param name="x">The x.</param> 336 /// <param name="y">The y.</param> 337 // ------------------------------------------------------------------ 338 public void MoveBy(int x, int y) { 339 Point pt = new Point(x, y); 340 MoveBy(pt); 341 } 342 343 #endregion 344 } 366 345 } -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Layout/BalloonTreeLayout.cs
r2768 r2861 49 49 : base("Balloon TreeLayout", controller) 50 50 { 51 52 51 } 53 52 #endregion -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Layout/FruchtermanReingoldLayout.cs
r2768 r2861 43 43 : base("FruchtermanReingold Layout", controller) 44 44 { 45 46 45 } 47 46 #endregion -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Layout/RadialTreeLayout.cs
r2768 r2861 69 69 : base("Radial TreeLayout", controller) 70 70 { 71 72 71 } 73 72 private bool Init() -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Layout/RandomLayout.cs
r2768 r2861 39 39 : base("Random layout", controller) 40 40 { 41 42 41 } 43 42 #endregion -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Layout/StandardTreeLayout.cs
r2819 r2861 103 103 { 104 104 105 105 Orientation = TreeOrientation.LeftRight; 106 106 107 107 } -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/ConnectionTool.cs
r2768 r2861 5 5 using System.Windows.Forms; 6 6 7 namespace Netron.Diagramming.Core 8 { 9 10 class ConnectionTool : AbstractTool, IMouseListener 11 { 7 namespace Netron.Diagramming.Core { 12 8 13 #region Fields 14 /// <summary> 15 /// the location of the mouse when the motion starts 16 /// </summary> 17 private Point initialPoint; 18 private bool doDraw; 9 class ConnectionTool : AbstractTool, IMouseListener { 10 11 #region Fields 12 /// <summary> 13 /// the location of the mouse when the motion starts 14 /// </summary> 15 private Point initialPoint; 16 private bool doDraw; 17 #endregion 18 19 #region Constructor 20 /// <summary> 21 /// Initializes a new instance of the <see cref="T:ConnectionTool"/> class. 22 /// </summary> 23 /// <param name="name">The name of the tool.</param> 24 public ConnectionTool(string name) 25 : base(name) { 26 } 27 #endregion 28 29 #region Methods 30 31 /// <summary> 32 /// Called when the tool is activated. 33 /// </summary> 34 protected override void OnActivateTool() { 35 Controller.View.CurrentCursor = CursorPalette.Grip; 36 this.SuspendOtherTools(); 37 doDraw = false; 38 } 39 40 /// <summary> 41 /// Handles the mouse down event 42 /// </summary> 43 /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> 44 /// <returns>Returns 'true' if the event was handled, otherwise 'false'.</returns> 45 public bool MouseDown(MouseEventArgs e) { 46 if (e == null) 47 throw new ArgumentNullException("The argument object is 'null'"); 48 if (e.Button == MouseButtons.Left && Enabled && !IsSuspended) { 49 50 initialPoint = e.Location; 51 doDraw = true; 52 return true; 53 } 54 return false; 55 } 56 57 /// <summary> 58 /// Handles the mouse move event 59 /// </summary> 60 /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> 61 public void MouseMove(MouseEventArgs e) { 62 if (e == null) 63 throw new ArgumentNullException("The argument object is 'null'"); 64 Point point = e.Location; 65 if (IsActive) { 66 if (foundConnector != null) 67 foundConnector.Hovered = false; 68 foundConnector = Selection.FindConnectorAt(e.Location); 69 if (foundConnector != null) 70 foundConnector.Hovered = true; 71 } 72 if (IsActive && doDraw) { 73 Controller.View.PaintGhostLine(initialPoint, point); 74 Controller.View.Invalidate(System.Drawing.Rectangle.Inflate(Controller.View.Ghost.Rectangle, 20, 20)); 75 76 77 } 78 } 79 private IConnector foundConnector; 80 /// <summary> 81 /// 82 /// </summary> 83 /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> 84 public void MouseUp(MouseEventArgs e) { 85 if (IsActive) { 86 DeactivateTool(); 87 88 // First, make sure the initial point is far enough away from 89 // the final point to make a connection. 90 int maxX = Math.Abs(Math.Max(initialPoint.X, e.Location.X)); 91 int maxY = Math.Abs(Math.Max(initialPoint.Y, e.Location.Y)); 92 93 if (!(maxX > ConnectionBase.MinLength) || 94 !(maxY > ConnectionBase.MinLength)) { 95 return; 96 } 97 98 //whatever comes hereafter, a compund command is the most economic approach 99 CompoundCommand package = new CompoundCommand(this.Controller); 100 101 //let's see if the connection endpoints hit other connectors 102 //note that the following can be done because the actual connection has not been created yet 103 //otherwise the search would find the endpoints of the newly created connection, which 104 //would create a loop and a stack overflow! 105 IConnector startConnector = Selection.FindConnectorAt(initialPoint); 106 IConnector endConnector = Selection.FindConnectorAt(e.Location); 107 108 #region Create the new connection 109 Connection cn = new Connection(this.initialPoint, e.Location, this.Controller.Model); 110 AddConnectionCommand newcon = new AddConnectionCommand(this.Controller, cn); 111 19 112 #endregion 20 113 21 #region Constructor 22 /// <summary> 23 /// Initializes a new instance of the <see cref="T:ConnectionTool"/> class. 24 /// </summary> 25 /// <param name="name">The name of the tool.</param> 26 public ConnectionTool(string name) : base(name) 27 { 114 #region Initial attachment? 115 if (startConnector != null) { 116 BindConnectorsCommand bindStart = new BindConnectorsCommand(this.Controller, startConnector, cn.From); 117 package.Commands.Add(bindStart); 28 118 } 29 119 #endregion 30 120 31 #region Methods 32 33 /// <summary> 34 /// Called when the tool is activated. 35 /// </summary> 36 protected override void OnActivateTool() 37 { 38 Controller.View.CurrentCursor =CursorPalette.Grip; 39 this.SuspendOtherTools(); 40 doDraw = false; 41 } 42 43 /// <summary> 44 /// Handles the mouse down event 45 /// </summary> 46 /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> 47 /// <returns>Returns 'true' if the event was handled, otherwise 'false'.</returns> 48 public bool MouseDown(MouseEventArgs e) 49 { 50 if (e == null) 51 throw new ArgumentNullException("The argument object is 'null'"); 52 if (e.Button == MouseButtons.Left && Enabled && !IsSuspended) 53 { 54 55 initialPoint = e.Location; 56 doDraw = true; 57 return true; 58 } 59 return false; 60 } 61 62 /// <summary> 63 /// Handles the mouse move event 64 /// </summary> 65 /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> 66 public void MouseMove(MouseEventArgs e) 67 { 68 if (e == null) 69 throw new ArgumentNullException("The argument object is 'null'"); 70 Point point = e.Location; 71 if (IsActive) 72 { 73 if (foundConnector != null) 74 foundConnector.Hovered = false; 75 foundConnector = Selection.FindConnectorAt(e.Location); 76 if (foundConnector != null) 77 foundConnector.Hovered = true; 78 } 79 if(IsActive && doDraw) 80 { 81 Controller.View.PaintGhostLine(initialPoint, point); 82 Controller.View.Invalidate(System.Drawing.Rectangle.Inflate(Controller.View.Ghost.Rectangle, 20, 20)); 83 84 85 } 86 } 87 private IConnector foundConnector; 88 /// <summary> 89 /// 90 /// </summary> 91 /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param> 92 public void MouseUp(MouseEventArgs e) 93 { 94 if (IsActive) 95 { 96 DeactivateTool(); 97 98 // First, make sure the initial point is far enough away from 99 // the final point to make a connection. 100 int maxX = Math.Abs(Math.Max(initialPoint.X, e.Location.X)); 101 int maxY = Math.Abs(Math.Max(initialPoint.Y, e.Location.Y)); 102 103 if (!(maxX > ConnectionBase.MinLength) || 104 !(maxY > ConnectionBase.MinLength)) 105 { 106 return; 107 } 108 109 //whatever comes hereafter, a compund command is the most economic approach 110 CompoundCommand package = new CompoundCommand(this.Controller); 111 112 //let's see if the connection endpoints hit other connectors 113 //note that the following can be done because the actual connection has not been created yet 114 //otherwise the search would find the endpoints of the newly created connection, which 115 //would create a loop and a stack overflow! 116 IConnector startConnector = Selection.FindConnectorAt(initialPoint); 117 IConnector endConnector = Selection.FindConnectorAt(e.Location); 118 119 #region Create the new connection 120 Connection cn = new Connection(this.initialPoint, e.Location, this.Controller.Model); 121 AddConnectionCommand newcon = new AddConnectionCommand(this.Controller, cn); 122 package.Commands.Add(newcon); 123 #endregion 124 125 #region Initial attachment? 126 if(startConnector != null) 127 { 128 BindConnectorsCommand bindStart = new BindConnectorsCommand(this.Controller, startConnector, cn.From); 129 package.Commands.Add(bindStart); 130 } 131 #endregion 132 133 #region Final attachment? 134 if(endConnector != null) 135 { 136 BindConnectorsCommand bindEnd = new BindConnectorsCommand(this.Controller, endConnector, cn.To); 137 package.Commands.Add(bindEnd); 138 } 139 #endregion 140 package.Text = "New connection"; 141 this.Controller.UndoManager.AddUndoCommand(package); 142 143 //do it all 144 package.Redo(); 145 146 //reset highlight of the found connector 147 if (foundConnector != null) 148 foundConnector.Hovered = false; 149 //drop the painted ghost 150 Controller.View.ResetGhost(); 151 //release other tools 152 this.UnsuspendTools(); 153 } 121 #region Final attachment? 122 if (endConnector != null) { 123 BindConnectorsCommand bindEnd = new BindConnectorsCommand(this.Controller, endConnector, cn.To); 124 package.Commands.Add(bindEnd); 154 125 } 155 126 #endregion 127 package.Text = "New connection"; 128 package.Commands.Add(newcon); 129 this.Controller.UndoManager.AddUndoCommand(package); 130 131 //do it all 132 package.Redo(); 133 134 //reset highlight of the found connector 135 if (foundConnector != null) 136 foundConnector.Hovered = false; 137 //drop the painted ghost 138 Controller.View.ResetGhost(); 139 //release other tools 140 this.UnsuspendTools(); 141 } 156 142 } 143 #endregion 144 } 157 145 158 146 }
Note: See TracChangeset
for help on using the changeset viewer.