Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/BaseClasses/Shape.cs
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/BaseClasses/Shape.cs
r2868 r4068 1 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 2 using System.Drawing; 4 3 using System.Drawing.Drawing2D; 5 using System.Drawing; 6 7 namespace Netron.Diagramming.Core 8 { 9 public class Shape : ShapeBase 10 { 11 // ------------------------------------------------------------------ 12 /// <summary> 13 /// The collection of shapes. 14 /// </summary> 15 // ------------------------------------------------------------------ 16 CollectionBase<IDiagramEntity> myEntities; 17 18 // ------------------------------------------------------------------ 19 /// <summary> 20 /// Gets the friendly name of this shape. 21 /// </summary> 22 // ------------------------------------------------------------------ 23 public override string EntityName 24 { 25 get 26 { 27 return "Shape"; 28 } 4 5 namespace Netron.Diagramming.Core { 6 public class Shape : ShapeBase { 7 // ------------------------------------------------------------------ 8 /// <summary> 9 /// The collection of shapes. 10 /// </summary> 11 // ------------------------------------------------------------------ 12 CollectionBase<IDiagramEntity> myEntities; 13 14 // ------------------------------------------------------------------ 15 /// <summary> 16 /// Gets the friendly name of this shape. 17 /// </summary> 18 // ------------------------------------------------------------------ 19 public override string EntityName { 20 get { 21 return "Shape"; 22 } 23 } 24 25 // ------------------------------------------------------------------ 26 /// <summary> 27 /// Gets or set the owned shapes. 28 /// </summary> 29 // ------------------------------------------------------------------ 30 public CollectionBase<IDiagramEntity> Entities { 31 get { 32 return myEntities; 33 } 34 set { 35 myEntities = value; 36 } 37 } 38 39 public virtual GraphicsPath GraphicsPath { 40 get { 41 GraphicsPath path = new GraphicsPath(); 42 path.AddRectangle(mRectangle); 43 return path; 44 } 45 } 46 47 // ------------------------------------------------------------------ 48 /// <summary> 49 /// Default constructor. 50 /// </summary> 51 // ------------------------------------------------------------------ 52 public Shape() 53 : base() { 54 } 55 56 // ------------------------------------------------------------------ 57 /// <summary> 58 /// Constructor that receives the model this shape belongs to. 59 /// </summary> 60 /// <param name="model">IModel</param> 61 // ------------------------------------------------------------------ 62 public Shape(IModel model) 63 : base(model) { 64 } 65 66 protected override void Initialize() { 67 base.Initialize(); 68 myEntities = new CollectionBase<IDiagramEntity>(); 69 AttachEventsToShapeCollection(); 70 } 71 72 protected virtual void AttachEventsToShapeCollection() { 73 myEntities.OnClear += new EventHandler(OnEntitiesClear); 74 myEntities.OnItemAdded += 75 new EventHandler<CollectionEventArgs<IDiagramEntity>>( 76 OnEntityAdded); 77 myEntities.OnItemRemoved += 78 new EventHandler<CollectionEventArgs<IDiagramEntity>>( 79 OnEntityRemoved); 80 } 81 82 void OnEntityRemoved(object sender, CollectionEventArgs<IDiagramEntity> e) { 83 CalculateRectangle(); 84 } 85 86 void OnEntityAdded(object sender, CollectionEventArgs<IDiagramEntity> e) { 87 if (myEntities.Count == 1) { 88 mRectangle = e.Item.Rectangle; 89 } else { 90 mRectangle = Rectangle.Union( 91 (Rectangle)mRectangle, 92 e.Item.Rectangle); 93 } 94 } 95 96 void OnEntitiesClear(object sender, EventArgs e) { 97 mRectangle = Rectangle.Empty; 98 } 99 100 // ------------------------------------------------------------------ 101 /// <summary> 102 /// Calculates the bounding rectangle of this shape from all children. 103 /// </summary> 104 // ------------------------------------------------------------------ 105 public void CalculateRectangle() { 106 if (myEntities == null || myEntities.Count == 0) 107 return; 108 Rectangle rec = myEntities[0].Rectangle; 109 foreach (IDiagramEntity entity in Entities) { 110 //cascade the calculation if necessary 111 if (entity is IGroup) (entity as IGroup).CalculateRectangle(); 112 113 rec = Rectangle.Union(rec, entity.Rectangle); 114 } 115 this.mRectangle = rec; 116 this.mRectangle.Inflate(20, 20); 117 } 118 119 // ------------------------------------------------------------------ 120 /// <summary> 121 /// Tests whether the group is hit by the mouse 122 /// </summary> 123 /// <param name="p">Point</param> 124 /// <returns>bool</returns> 125 // ------------------------------------------------------------------ 126 public override bool Hit(Point p) { 127 foreach (IDiagramEntity entity in myEntities) { 128 if (entity.Hit(p)) { 129 return true; 29 130 } 30 31 // ------------------------------------------------------------------ 32 /// <summary> 33 /// Gets or set the owned shapes. 34 /// </summary> 35 // ------------------------------------------------------------------ 36 public CollectionBase<IDiagramEntity> Entities 37 { 38 get 39 { 40 return myEntities; 41 } 42 set 43 { 44 myEntities = value; 45 } 131 } 132 return false; 133 } 134 135 // ------------------------------------------------------------------ 136 /// <summary> 137 /// Invalidates the entity 138 /// </summary> 139 // ------------------------------------------------------------------ 140 public override void Invalidate() { 141 142 if (mRectangle == null) 143 return; 144 145 Rectangle rec = mRectangle; 146 rec.Inflate(20, 20); 147 Model.RaiseOnInvalidateRectangle(rec); 148 } 149 150 // ------------------------------------------------------------------ 151 /// <summary> 152 /// Moves the entity on the canvas 153 /// </summary> 154 /// <param name="p">Point</param> 155 // ------------------------------------------------------------------ 156 public override void MoveBy(Point p) { 157 158 Rectangle recBefore = mRectangle; 159 recBefore.Inflate(20, 20); 160 161 // No need to invalidate since it'll be done by the individual 162 // move actions. 163 foreach (IDiagramEntity entity in myEntities) { 164 entity.MoveBy(p); 165 } 166 mRectangle.X += p.X; 167 mRectangle.Y += p.Y; 168 169 //refresh things 170 this.Invalidate(recBefore);//position before the move 171 this.Invalidate();//current position 172 173 } 174 175 public override void Paint(Graphics g) { 176 base.Paint(g); 177 178 GraphicsPath path = GraphicsPath; 179 if (PaintStyle != null) { 180 g.FillPath(mPaintStyle.GetBrush(mRectangle), path); 181 } 182 183 if (Hovered) { 184 g.DrawPath(ArtPalette.HighlightPen, path); 185 } else if (PenStyle != null) { 186 g.DrawPath(mPenStyle.DrawingPen(), path); 187 } 188 189 foreach (IDiagramEntity entity in myEntities) { 190 entity.Paint(g); 191 } 192 } 193 194 public override bool MouseDown(System.Windows.Forms.MouseEventArgs e) { 195 bool result = base.MouseDown(e); 196 197 foreach (IDiagramEntity entity in myEntities) { 198 if (entity.Hit(e.Location)) { 199 this.Model.Selection.SelectedItems.Add(entity); 200 if (entity.MouseDown(e)) { 201 result = true; 202 } 46 203 } 47 48 public virtual GraphicsPath GraphicsPath 49 { 50 get 51 { 52 GraphicsPath path = new GraphicsPath(); 53 path.AddRectangle(mRectangle); 54 return path; 55 } 56 } 57 58 // ------------------------------------------------------------------ 59 /// <summary> 60 /// Default constructor. 61 /// </summary> 62 // ------------------------------------------------------------------ 63 public Shape() 64 : base() 65 { 66 } 67 68 // ------------------------------------------------------------------ 69 /// <summary> 70 /// Constructor that receives the model this shape belongs to. 71 /// </summary> 72 /// <param name="model">IModel</param> 73 // ------------------------------------------------------------------ 74 public Shape(IModel model) 75 : base(model) 76 { 77 } 78 79 protected override void Initialize() 80 { 81 base.Initialize(); 82 myEntities = new CollectionBase<IDiagramEntity>(); 83 AttachEventsToShapeCollection(); 84 } 85 86 protected virtual void AttachEventsToShapeCollection() 87 { 88 myEntities.OnClear += new EventHandler(OnEntitiesClear); 89 myEntities.OnItemAdded += 90 new EventHandler<CollectionEventArgs<IDiagramEntity>>( 91 OnEntityAdded); 92 myEntities.OnItemRemoved += 93 new EventHandler<CollectionEventArgs<IDiagramEntity>>( 94 OnEntityRemoved); 95 } 96 97 void OnEntityRemoved(object sender, CollectionEventArgs<IDiagramEntity> e) 98 { 99 CalculateRectangle(); 100 } 101 102 void OnEntityAdded(object sender, CollectionEventArgs<IDiagramEntity> e) 103 { 104 if (myEntities.Count == 1) 105 { 106 mRectangle = e.Item.Rectangle; 107 } 108 else 109 { 110 mRectangle = Rectangle.Union( 111 (Rectangle)mRectangle, 112 e.Item.Rectangle); 113 } 114 } 115 116 void OnEntitiesClear(object sender, EventArgs e) 117 { 118 mRectangle = Rectangle.Empty; 119 } 120 121 // ------------------------------------------------------------------ 122 /// <summary> 123 /// Calculates the bounding rectangle of this shape from all children. 124 /// </summary> 125 // ------------------------------------------------------------------ 126 public void CalculateRectangle() 127 { 128 if (myEntities == null || myEntities.Count == 0) 129 return; 130 Rectangle rec = myEntities[0].Rectangle; 131 foreach (IDiagramEntity entity in Entities) 132 { 133 //cascade the calculation if necessary 134 if (entity is IGroup) (entity as IGroup).CalculateRectangle(); 135 136 rec = Rectangle.Union(rec, entity.Rectangle); 137 } 138 this.mRectangle = rec; 139 this.mRectangle.Inflate(20, 20); 140 } 141 142 // ------------------------------------------------------------------ 143 /// <summary> 144 /// Tests whether the group is hit by the mouse 145 /// </summary> 146 /// <param name="p">Point</param> 147 /// <returns>bool</returns> 148 // ------------------------------------------------------------------ 149 public override bool Hit(Point p) 150 { 151 foreach (IDiagramEntity entity in myEntities) 152 { 153 if (entity.Hit(p)) 154 { 155 return true; 156 } 157 } 158 return false; 159 } 160 161 // ------------------------------------------------------------------ 162 /// <summary> 163 /// Invalidates the entity 164 /// </summary> 165 // ------------------------------------------------------------------ 166 public override void Invalidate() 167 { 168 169 if (mRectangle == null) 170 return; 171 172 Rectangle rec = mRectangle; 173 rec.Inflate(20, 20); 174 Model.RaiseOnInvalidateRectangle(rec); 175 } 176 177 // ------------------------------------------------------------------ 178 /// <summary> 179 /// Moves the entity on the canvas 180 /// </summary> 181 /// <param name="p">Point</param> 182 // ------------------------------------------------------------------ 183 public override void MoveBy(Point p) 184 { 185 186 Rectangle recBefore = mRectangle; 187 recBefore.Inflate(20, 20); 188 189 // No need to invalidate since it'll be done by the individual 190 // move actions. 191 foreach (IDiagramEntity entity in myEntities) 192 { 193 entity.MoveBy(p); 194 } 195 mRectangle.X += p.X; 196 mRectangle.Y += p.Y; 197 198 //refresh things 199 this.Invalidate(recBefore);//position before the move 200 this.Invalidate();//current position 201 202 } 203 204 public override void Paint(Graphics g) 205 { 206 base.Paint(g); 207 208 GraphicsPath path = GraphicsPath; 209 if (PaintStyle != null) 210 { 211 g.FillPath(mPaintStyle.GetBrush(mRectangle), path); 212 } 213 214 if (Hovered) 215 { 216 g.DrawPath(ArtPalette.HighlightPen, path); 217 } 218 else if (PenStyle != null) 219 { 220 g.DrawPath(mPenStyle.DrawingPen(), path); 221 } 222 223 foreach (IDiagramEntity entity in myEntities) 224 { 225 entity.Paint(g); 226 } 227 } 228 229 public override bool MouseDown(System.Windows.Forms.MouseEventArgs e) 230 { 231 bool result = base.MouseDown(e); 232 233 foreach (IDiagramEntity entity in myEntities) 234 { 235 if (entity.Hit(e.Location)) 236 { 237 this.Model.Selection.SelectedItems.Add(entity); 238 if (entity.MouseDown(e)) 239 { 240 result = true; 241 } 242 } 243 } 244 245 return result; 246 } 247 } 204 } 205 206 return result; 207 } 208 } 248 209 }
Note: See TracChangeset
for help on using the changeset viewer.