Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1265_HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/Group.cs @ 17717

Last change on this file since 17717 was 13717, checked in by bburlacu, 9 years ago

#1265: Updated license year to 2016.

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Windows.Forms;
28
29namespace HeuristicLab.Visualization {
30  public class Group : PrimitiveBase, IGroup {
31    private readonly LinkedList<IPrimitive> primitives;
32    private readonly HashSet<IPrimitive> primitivesLookup;
33
34    public virtual IEnumerable<IPrimitive> Primitives {
35      get { return primitives.ToArray(); }
36    }
37    public virtual IEnumerable<IPrimitive> SelectedPrimitives {
38      get { return primitives.Where(x => x.Selected).ToArray(); }
39    }
40
41    public override bool Selected {
42      get { return base.Selected; }
43      set {
44        SuppressEvents = true;
45        try {
46          foreach (var primitive in primitives)
47            primitive.Selected = value;
48          base.Selected = value;
49        } finally {
50          SuppressEvents = false;
51          OnRedrawRequired();
52        }
53      }
54    }
55
56    public Group(IChart chart)
57      : base(chart) {
58      primitives = new LinkedList<IPrimitive>();
59      primitivesLookup = new HashSet<IPrimitive>();
60    }
61
62    public virtual void Add(IPrimitive primitive) {
63      if (Contains(primitive)) throw new ArgumentException("Primitive already added");
64      primitives.AddFirst(primitive);
65      primitivesLookup.Add(primitive);
66      primitive.RedrawRequired += PrimitiveOnRedrawRequired;
67      OnRedrawRequired();
68    }
69    public virtual void AddRange(IEnumerable<IPrimitive> primitives) {
70      foreach (var primitive in this.primitives) {
71        if (Contains(primitive)) throw new ArgumentException("Primitive already added");
72        this.primitives.AddFirst(primitive);
73        primitivesLookup.Add(primitive);
74        primitive.RedrawRequired += PrimitiveOnRedrawRequired;
75      }
76      OnRedrawRequired();
77    }
78
79    public virtual bool Contains(IPrimitive primitive) {
80      return primitivesLookup.Contains(primitive);
81    }
82
83    public virtual bool Remove(IPrimitive primitive) {
84      if (!primitives.Remove(primitive)) return false;
85      primitivesLookup.Remove(primitive);
86
87      primitive.RedrawRequired -= PrimitiveOnRedrawRequired;
88      OnRedrawRequired();
89      return true;
90    }
91
92    public virtual void Clear() {
93      foreach (var primitive in primitives) {
94        primitive.RedrawRequired -= PrimitiveOnRedrawRequired;
95      }
96      primitives.Clear();
97      primitivesLookup.Clear();
98      OnRedrawRequired();
99    }
100
101    public virtual IPrimitive GetPrimitive(PointD point) {
102      return primitives.FirstOrDefault(primitive => primitive.ContainsPoint(point));
103    }
104
105    public virtual IEnumerable<IPrimitive> GetAllPrimitives(PointD point) {
106      var result = new List<IPrimitive>();
107      foreach (var primitive in primitives.Where(primitive => primitive.ContainsPoint(point))) {
108        result.Add(primitive);
109        var group = primitive as IGroup;
110        if (group != null)
111          result.AddRange(group.GetAllPrimitives(point));
112      }
113      return result;
114    }
115
116    public override void Move(Offset delta) {
117      SuppressEvents = true;
118      try {
119        foreach (var primitive in primitives)
120          primitive.Move(delta);
121      } finally {
122        SuppressEvents = false;
123        OnRedrawRequired();
124      }
125    }
126
127    public override void Move(PointD point, Offset delta) {
128      SuppressEvents = true;
129      try {
130        foreach (var primitive in primitives)
131          primitive.Move(point, delta);
132      } finally {
133        SuppressEvents = false;
134        OnRedrawRequired();
135      }
136    }
137
138    public override void SnapToGrid(IGrid grid) {
139      SuppressEvents = true;
140      try {
141        foreach (var primitive in primitives)
142          primitive.SnapToGrid(grid);
143      } finally {
144        SuppressEvents = false;
145        OnRedrawRequired();
146      }
147    }
148
149    public override void SnapToGrid(PointD point, IGrid grid) {
150      SuppressEvents = true;
151      try {
152        foreach (var primitive in primitives)
153          primitive.SnapToGrid(point, grid);
154      } finally {
155        SuppressEvents = false;
156        OnRedrawRequired();
157      }
158    }
159
160    public override bool ContainsPoint(PointD point) {
161      return GetPrimitive(point) != null;
162    }
163
164    public override Cursor GetCursor(PointD point) {
165      var primitive = GetPrimitive(point);
166      return primitive != null ? primitive.GetCursor(point) : base.GetCursor(point);
167    }
168
169    public override string GetToolTipText(PointD point) {
170      var primitive = GetPrimitive(point);
171      return primitive != null ? primitive.GetToolTipText(point) : base.GetToolTipText(point);
172    }
173
174    public void OneLayerUp(IPrimitive primitive) {
175      if (!Contains(primitive)) throw new ArgumentException("Primitive not found");
176
177      var curr = primitives.Find(primitive);
178      if (curr == null) return;
179      var prev = curr.Previous;
180      var temp = curr.Value;
181      curr.Value = prev.Value;
182      prev.Value = temp;
183
184      OnRedrawRequired();
185    }
186    public void OneLayerDown(IPrimitive primitive) {
187      if (!Contains(primitive)) throw new ArgumentException("Primitive not found");
188
189      var curr = primitives.Find(primitive);
190      if (curr == null) return;
191      var next = curr.Next;
192      var temp = curr.Value;
193      curr.Value = next.Value;
194      next.Value = temp;
195    }
196    public void IntoForeground(IPrimitive primitive) {
197      if (!Contains(primitive)) throw new ArgumentException("Primitive not found");
198      primitives.Remove(primitive);
199      primitives.AddFirst(primitive);
200      OnRedrawRequired();
201    }
202    public void IntoBackground(IPrimitive primitive) {
203      if (!Contains(primitive)) throw new ArgumentException("Primitive not found");
204
205      primitives.Remove(primitive);
206      primitives.AddLast(primitive);
207      OnRedrawRequired();
208    }
209
210    public override void Draw(Graphics graphics) {
211      var current = primitives.Last;
212      while (current != null) {
213        var primitive = current.Value;
214        primitive.PreDraw(graphics);
215        primitive.Draw(graphics);
216        primitive.PostDraw(graphics);
217        current = current.Previous;
218      }
219    }
220
221    private void PrimitiveOnRedrawRequired(object sender, EventArgs e) {
222      if (SuppressEvents) return;
223      OnRedrawRequired();
224    }
225
226    public IEnumerator<IPrimitive> GetEnumerator() {
227      return primitives.GetEnumerator();
228    }
229
230    IEnumerator IEnumerable.GetEnumerator() {
231      return GetEnumerator();
232    }
233  }
234}
Note: See TracBrowser for help on using the repository browser.