Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/Group.cs @ 8215

Last change on this file since 8215 was 8215, checked in by bburlacu, 12 years ago

#1265: Introduced a hash for primitives in order to improve lookup performance (Contains method).

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