Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9415 was 9415, checked in by bburlacu, 11 years ago

#1265: Added save file functionality in emf and bitmap formats.

File size: 6.8 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.Linq;
27using System.Windows.Forms;
28
29namespace HeuristicLab.Visualization {
30  public class Group : PrimitiveBase, IGroup {
31    private LinkedList<IPrimitive> myPrimitives;
32    private HashSet<IPrimitive> myPrimitivesLookup;
33    public virtual ReadOnlyCollection<IPrimitive> Primitives {
34      get { return new ReadOnlyCollection<IPrimitive>(new List<IPrimitive>(myPrimitives)); }
35    }
36    public virtual ReadOnlyCollection<IPrimitive> SelectedPrimitives {
37      get {
38        List<IPrimitive> selected = new List<IPrimitive>();
39        foreach (IPrimitive primitive in myPrimitives) {
40          if (primitive.Selected) selected.Add(primitive);
41        }
42        return new ReadOnlyCollection<IPrimitive>(selected);
43      }
44    }
45    public override bool Selected {
46      get { return base.Selected; }
47      set {
48        bool updateEnabled = UpdateEnabled;
49        UpdateEnabled = false;
50        foreach (IPrimitive primitive in myPrimitives)
51          primitive.Selected = value;
52        UpdateEnabled = updateEnabled;
53        base.Selected = value;
54      }
55    }
56
57    public Group(IChart chart)
58      : base(chart) {
59      myPrimitives = new LinkedList<IPrimitive>();
60      myPrimitivesLookup = new HashSet<IPrimitive>();
61    }
62
63    public virtual void Add(IPrimitive primitive) {
64      if (Contains(primitive))
65        throw new ArgumentException("Primitive already added");
66
67      myPrimitives.AddFirst(primitive);
68      myPrimitivesLookup.Add(primitive);
69      primitive.Group = this;
70      primitive.Update += new EventHandler(primitive_Update);
71      OnUpdate();
72    }
73    public virtual void AddRange(IEnumerable<IPrimitive> primitives) {
74      foreach (IPrimitive primitive in primitives) {
75        if (Contains(primitive))
76          throw new ArgumentException("Primitive already added");
77
78        myPrimitives.AddFirst(primitive);
79        myPrimitivesLookup.Add(primitive);
80        primitive.Group = this;
81        primitive.Update += new EventHandler(primitive_Update);
82      }
83      OnUpdate();
84    }
85    public virtual bool Contains(IPrimitive primitive) {
86      return myPrimitivesLookup.Contains(primitive);
87    }
88    public virtual bool Remove(IPrimitive primitive) {
89      if (myPrimitives.Remove(primitive)) {
90        primitive.Group = null;
91        primitive.Update -= new EventHandler(primitive_Update);
92        OnUpdate();
93        return true;
94      } else {
95        return false;
96      }
97    }
98    public virtual void Clear() {
99      foreach (IPrimitive primitive in myPrimitives) {
100        primitive.Group = null;
101        primitive.Update -= new EventHandler(primitive_Update);
102      }
103      myPrimitives.Clear();
104      myPrimitivesLookup.Clear();
105      OnUpdate();
106    }
107
108    public virtual IPrimitive GetPrimitive(PointD point) {
109      return myPrimitives.FirstOrDefault(primitive => primitive.ContainsPoint(point));
110    }
111
112    public IPrimitive GetPrimitive(double x, double y) {
113      return GetPrimitive(new PointD(x, y));
114    }
115    public virtual IList<IPrimitive> GetAllPrimitives(PointD point) {
116      var primitives = new List<IPrimitive>();
117      foreach (var primitive in myPrimitives.Where(primitive => primitive.ContainsPoint(point))) {
118        primitives.Add(primitive);
119        var group = primitive as IGroup;
120        if (group != null)
121          primitives.AddRange(group.Primitives);
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      var curr = myPrimitives.Find(primitive);
158      if (curr == null) return;
159      var prev = curr.Previous;
160      var temp = curr.Value;
161      curr.Value = prev.Value;
162      prev.Value = temp;
163
164      OnUpdate();
165    }
166
167    public void OneLayerDown(IPrimitive primitive) {
168      if (!Contains(primitive))
169        throw new ArgumentException("Primitive not found");
170
171      var curr = myPrimitives.Find(primitive);
172      if (curr == null) return;
173      var next = curr.Next;
174      var temp = curr.Value;
175      curr.Value = next.Value;
176      next.Value = temp;
177    }
178    public void IntoForeground(IPrimitive primitive) {
179      if (!Contains(primitive))
180        throw new ArgumentException("Primitive not found");
181
182      myPrimitives.Remove(primitive);
183      myPrimitives.AddFirst(primitive);
184      OnUpdate();
185    }
186    public void IntoBackground(IPrimitive primitive) {
187      if (!Contains(primitive))
188        throw new ArgumentException("Primitive not found");
189
190      myPrimitives.Remove(primitive);
191      myPrimitives.AddLast(primitive);
192      OnUpdate();
193    }
194
195    public override void Draw(Graphics graphics) {
196      foreach (var primitive in myPrimitives) {
197        primitive.PreDraw(graphics);
198        primitive.Draw(graphics);
199        primitive.PostDraw(graphics);
200      }
201    }
202
203    private void primitive_Update(object sender, EventArgs e) {
204      OnUpdate();
205    }
206  }
207}
Note: See TracBrowser for help on using the repository browser.