Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Charting/Primitives/Group.cs @ 2578

Last change on this file since 2578 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 6.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Collections.ObjectModel;
25using System.Text;
26using System.Drawing;
27using System.Windows.Forms;
28
29namespace HeuristicLab.Charting {
30  public class Group : PrimitiveBase, IGroup {
31    private IList<IPrimitive> myPrimitives;
32    public virtual ReadOnlyCollection<IPrimitive> Primitives {
33      get { return new ReadOnlyCollection<IPrimitive>(myPrimitives); }
34    }
35    public virtual ReadOnlyCollection<IPrimitive> SelectedPrimitives {
36      get {
37        List<IPrimitive> selected = new List<IPrimitive>();
38        foreach (IPrimitive primitive in myPrimitives) {
39          if (primitive.Selected) selected.Add(primitive);
40        }
41        return new ReadOnlyCollection<IPrimitive>(selected);
42      }
43    }
44    public override bool Selected {
45      get { return base.Selected; }
46      set {
47        bool updateEnabled = UpdateEnabled;
48        UpdateEnabled = false;
49        foreach (IPrimitive primitive in myPrimitives)
50          primitive.Selected = value;
51        UpdateEnabled = updateEnabled;
52        base.Selected = value;
53      }
54    }
55
56    public Group(IChart chart)
57      : base(chart) {
58      myPrimitives = new List<IPrimitive>();
59    }
60
61    public virtual void Add(IPrimitive primitive) {
62      if (Contains(primitive))
63        throw new ArgumentException("Primitive already added");
64
65      myPrimitives.Insert(0, primitive);
66      primitive.Group = this;
67      primitive.Update += new EventHandler(primitive_Update);
68      OnUpdate();
69    }
70    public virtual void AddRange(IEnumerable<IPrimitive> primitives) {
71      foreach (IPrimitive primitive in primitives) {
72        if (Contains(primitive))
73          throw new ArgumentException("Primitive already added");
74
75        myPrimitives.Insert(0, primitive);
76        primitive.Group = this;
77        primitive.Update += new EventHandler(primitive_Update);
78      }
79      OnUpdate();
80    }
81    public virtual bool Contains(IPrimitive primitive) {
82      return myPrimitives.Contains(primitive);
83    }
84    public virtual bool Remove(IPrimitive primitive) {
85      if (myPrimitives.Remove(primitive)) {
86        primitive.Group = null;
87        primitive.Update -= new EventHandler(primitive_Update);
88        OnUpdate();
89        return true;
90      } else {
91        return false;
92      }
93    }
94    public virtual void Clear() {
95      foreach (IPrimitive primitive in myPrimitives) {
96        primitive.Group = null;
97        primitive.Update -= new EventHandler(primitive_Update);
98      }
99      myPrimitives.Clear();
100      OnUpdate();
101    }
102
103    public virtual IPrimitive GetPrimitive(PointD point) {
104      int i = 0;
105      while ((i < myPrimitives.Count) && (!myPrimitives[i].ContainsPoint(point)))
106        i++;
107      if (i == myPrimitives.Count) return null;
108      else return myPrimitives[i];
109    }
110    public IPrimitive GetPrimitive(double x, double y) {
111      return GetPrimitive(new PointD(x, y));
112    }
113    public virtual IList<IPrimitive> GetAllPrimitives(PointD point) {
114      List<IPrimitive> primitives = new List<IPrimitive>();
115      for (int i = 0; i < myPrimitives.Count; i++) {
116        if (myPrimitives[i].ContainsPoint(point)) {
117          primitives.Add(myPrimitives[i]);
118          if (myPrimitives[i] is IGroup) {
119            primitives.AddRange(((IGroup)myPrimitives[i]).Primitives);
120          }
121        }
122      }
123      return primitives;
124    }
125    public IList<IPrimitive> GetAllPrimitives(double x, double y) {
126      return GetAllPrimitives(new PointD(x, y));
127    }
128
129    public override void Move(Offset delta) {
130      bool updateEnabled = UpdateEnabled;
131      UpdateEnabled = false;
132      foreach (IPrimitive primitive in myPrimitives)
133        primitive.Move(delta);
134      UpdateEnabled = updateEnabled;
135      OnUpdate();
136    }
137
138    public override bool ContainsPoint(PointD point) {
139      return GetPrimitive(point) != null;
140    }
141
142    public override Cursor GetCursor(PointD point) {
143      IPrimitive primitive = GetPrimitive(point);
144      if (primitive != null) return primitive.GetCursor(point);
145      else return base.GetCursor(point);
146    }
147    public override string GetToolTipText(PointD point) {
148      IPrimitive primitive = GetPrimitive(point);
149      if (primitive != null) return primitive.GetToolTipText(point);
150      else return base.GetToolTipText(point);
151    }
152
153    public void OneLayerUp(IPrimitive primitive) {
154      if (!Contains(primitive))
155        throw new ArgumentException("Primitive not found");
156
157      int index = myPrimitives.IndexOf(primitive);
158      if (index > 0) {
159        myPrimitives.Remove(primitive);
160        myPrimitives.Insert(index - 1, primitive);
161        OnUpdate();
162      }
163    }
164    public void OneLayerDown(IPrimitive primitive) {
165      if (!Contains(primitive))
166        throw new ArgumentException("Primitive not found");
167
168      int index = myPrimitives.IndexOf(primitive);
169      if (index < myPrimitives.Count - 1) {
170        myPrimitives.Remove(primitive);
171        myPrimitives.Insert(index + 1, primitive);
172        OnUpdate();
173      }
174    }
175    public void IntoForeground(IPrimitive primitive) {
176      if (!Contains(primitive))
177        throw new ArgumentException("Primitive not found");
178
179      myPrimitives.Remove(primitive);
180      myPrimitives.Insert(0, primitive);
181      OnUpdate();
182    }
183    public void IntoBackground(IPrimitive primitive) {
184      if (!Contains(primitive))
185        throw new ArgumentException("Primitive not found");
186
187      myPrimitives.Remove(primitive);
188      myPrimitives.Add(primitive);
189      OnUpdate();
190    }
191
192    public override void Draw(Graphics graphics) {
193      for (int i = myPrimitives.Count - 1; i >= 0; i--) {
194        myPrimitives[i].PreDraw(graphics);
195        myPrimitives[i].Draw(graphics);
196        myPrimitives[i].PostDraw(graphics);
197      }
198    }
199
200    private void primitive_Update(object sender, EventArgs e) {
201      OnUpdate();
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.