Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/Drawing/CompositeShape.cs @ 2701

Last change on this file since 2701 was 1964, checked in by mstoeger, 16 years ago

moved the canvas and the basic types of shapes to their own namespace. #498

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4
5namespace HeuristicLab.Visualization.Drawing {
6  public class CompositeShape : IShape {
7    private IShape parent;
8    private bool showChildShapes = true;
9
10    protected readonly List<IShape> shapes = new List<IShape>();
11    protected RectangleD boundingBox = RectangleD.Empty;
12
13
14
15    public virtual void Draw(Graphics graphics) {
16      if(!showChildShapes)
17        return;
18      foreach (IShape shape in shapes) {
19        shape.Draw(graphics);
20      }
21    }
22
23    public RectangleD BoundingBox {
24      get {
25        if (shapes.Count == 0) {
26          throw new InvalidOperationException("No shapes, no bounding box.");
27        }
28
29        return boundingBox;
30      }
31    }
32
33    public RectangleD ClippingArea {
34      get { return Parent.ClippingArea; }
35    }
36
37    public Rectangle Viewport {
38      get { return Parent.Viewport; }
39    }
40
41    public IShape Parent {
42      get { return parent; }
43      set { parent = value; }
44    }
45
46    public bool ShowChildShapes {
47      get { return showChildShapes; }
48      set { showChildShapes = value; }
49    }
50
51    public void ClearShapes() {
52      shapes.Clear();
53      boundingBox = RectangleD.Empty;
54    }
55
56    public IShape GetShape(int index) {
57      return shapes[index];
58    }
59
60
61    /// <summary>
62    /// Adds a shape to the container
63    /// </summary>
64    /// <param name="shape"> the Shape to add</param>
65    public void AddShape(IShape shape) {
66      shape.Parent = this;
67
68      if (shapes.Count == 0) {
69        boundingBox = shape.BoundingBox;
70      } else {
71        boundingBox = new RectangleD(Math.Min(boundingBox.X1, shape.BoundingBox.X1),
72                                     Math.Min(boundingBox.Y1, shape.BoundingBox.Y1),
73                                     Math.Max(boundingBox.X2, shape.BoundingBox.X2),
74                                     Math.Max(boundingBox.Y2, shape.BoundingBox.Y2));
75      }
76      shapes.Add(shape);
77    }
78
79    /// <summary>
80    /// Recalculate the bounding box
81    /// </summary>
82    private void InitBoundingBox() {
83      if (shapes.Count > 0)
84        boundingBox = shapes[0].BoundingBox;
85      foreach (var shape in shapes) {
86        boundingBox = new RectangleD(Math.Min(boundingBox.X1, shape.BoundingBox.X1),
87                                     Math.Min(boundingBox.Y1, shape.BoundingBox.Y1),
88                                     Math.Max(boundingBox.X2, shape.BoundingBox.X2),
89                                     Math.Max(boundingBox.Y2, shape.BoundingBox.Y2));
90      }
91    }
92
93    /// <summary>
94    /// Removes a Shape from the container and reinitializes the bounding box
95    /// </summary>
96    /// <param name="shape">the Shape to remove</param>
97    public void RemoveShape(IShape shape) {
98      shapes.Remove(shape);
99      InitBoundingBox();
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.