Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Core/Scene graph/Page.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 14.4 KB
Line 
1using System;
2using System.Drawing;
3namespace Netron.Diagramming.Core {
4  /// <summary>
5  /// Implementation of the <see cref="IPage"/> interface. The page represents one page the diagram control, pages being usually visualized as tabs in the control.
6  ///
7  /// </summary>
8  public partial class Page : IPage, IVersion {
9    #region Events
10    /// <summary>
11    /// Occurs when the Ambience has changed
12    /// </summary>
13    public event EventHandler<AmbienceEventArgs> OnAmbienceChanged;
14    /// <summary>
15    /// Occurs when an entity is added.
16    /// <remarks>This event usually is bubbled from one of the layers</remarks>
17    /// </summary>
18    public event EventHandler<EntityEventArgs> OnEntityAdded;
19    /// <summary>
20    /// Occurs when an entity is removed.
21    /// <remarks>This event usually is bubbled from one of the layers</remarks>
22    /// </summary>
23    public event EventHandler<EntityEventArgs> OnEntityRemoved;
24    /// <summary>
25    /// Occurs when the page is cleared
26    /// </summary>
27    public event EventHandler OnClear;
28    /// <summary>
29    /// Occurs before an entity is transformed
30    /// </summary>
31    //public event EventHandler<CancelableEntityEventArgs> OnBeforeResize;
32    #endregion
33
34    #region Fields
35
36    // ------------------------------------------------------------------
37    /// <summary>
38    /// Implementation of IVersion - the current version of
39    /// Page.
40    /// </summary>
41    // ------------------------------------------------------------------
42    protected const double pageVersion = 1.0;
43
44    // ------------------------------------------------------------------
45    /// <summary>
46    /// The scaling factor of this page.
47    /// </summary>
48    // ------------------------------------------------------------------
49    protected SizeF mMagnification = new SizeF(71F, 71F);
50
51    // ------------------------------------------------------------------
52    /// <summary>
53    /// Pans the page with the given shift.
54    /// </summary>
55    // ------------------------------------------------------------------
56    protected Point mOrigin = new Point(0, 0);
57
58    /// <summary>
59    /// the Name field
60    /// </summary>
61    protected string mName;
62
63    /// <summary>
64    /// the default layer
65    /// </summary>
66    [NonSerialized]
67    protected ILayer mDefaultLayer;
68
69    /// <summary>
70    /// the Layers field
71    /// </summary>
72    protected CollectionBase<ILayer> mLayers;
73
74    /// <summary>
75    /// the Ambience field
76    /// </summary>
77    protected Ambience mAmbience;
78
79    #endregion
80
81    #region Properties
82
83    // ------------------------------------------------------------------
84    /// <summary>
85    /// Gets the current version.
86    /// </summary>
87    // ------------------------------------------------------------------
88    public virtual double Version {
89      get {
90        return pageVersion;
91      }
92    }
93
94    // ------------------------------------------------------------------
95    /// <summary>
96    /// Gets or sets the scaling factor of this page.
97    /// </summary>
98    // ------------------------------------------------------------------
99    public SizeF Magnification {
100      get {
101        return mMagnification;
102      }
103      set {
104        if (value == SizeF.Empty) {
105          throw new InconsistencyException(
106              "The magnification has to be a positive value " +
107              "greater than zero.");
108        }
109
110        mMagnification = value;
111
112        // Update the magnification for each entity.
113        foreach (IDiagramEntity entity in Entities) {
114          entity.Magnification = mMagnification;
115        }
116      }
117    }
118
119    // ------------------------------------------------------------------
120    /// <summary>
121    /// Gets or sets the origin of this page.
122    /// </summary>
123    // ------------------------------------------------------------------
124    public Point Origin {
125      get {
126        return mOrigin;
127      }
128      set {
129        mOrigin = value;
130      }
131    }
132
133    // ------------------------------------------------------------------
134    /// <summary>
135    /// Gets the page bounds, taking into account landscape and page
136    /// settings (for printing).
137    /// </summary>
138    // ------------------------------------------------------------------
139    public RectangleF Bounds {
140      get {
141        float width = (float)Ambience.PageSize.Width / 1000;
142        float height = (float)Ambience.PageSize.Height / 1000;
143        width = width * Display.DpiX;
144        height = height * Display.DpiY;
145
146        if (Ambience.Landscape) {
147          float w = width;
148          width = height;
149          height = w;
150        }
151
152        // Offset the upper-left corner by 1 inch.
153        //float x = 1F * Display.DpiX;
154        //float y = 1F * Display.DpiY;
155
156        //inserted to remove margin around the drawi-ng aera
157        float x = 0;
158        float y = 0;
159
160        RectangleF bounds = new RectangleF(x, y, width, height);
161        return bounds;
162      }
163    }
164
165    // ------------------------------------------------------------------
166    /// <summary>
167    /// Gets all shapes in this page.
168    /// </summary>
169    // ------------------------------------------------------------------
170    public CollectionBase<IShape> Shapes {
171      get {
172        CollectionBase<IShape> shapes = new CollectionBase<IShape>();
173        foreach (ILayer layer in mLayers) {
174          shapes.AddRange(layer.Shapes);
175        }
176        return shapes;
177      }
178    }
179
180    // ------------------------------------------------------------------
181    /// <summary>
182    /// Gets all entities in this page.
183    /// </summary>
184    // ------------------------------------------------------------------
185    public CollectionBase<IDiagramEntity> Entities {
186      get {
187        //return DefaultLayer.Entities;
188        CollectionBase<IDiagramEntity> entities =
189            new CollectionBase<IDiagramEntity>();
190        foreach (ILayer layer in mLayers) {
191          entities.AddRange(layer.Entities);
192        }
193        return entities;
194      }
195    }
196
197    // ------------------------------------------------------------------
198    /// <summary>
199    /// Gets all connections in this page.
200    /// </summary>
201    // ------------------------------------------------------------------
202    public CollectionBase<IConnection> Connections {
203      get {
204        CollectionBase<IConnection> cons = new CollectionBase<IConnection>();
205        foreach (ILayer layer in mLayers) {
206          cons.AddRange(layer.Connections);
207        }
208        return cons;
209      }
210    }
211
212    /// <summary>
213    /// Gets or sets the Ambience
214    /// </summary>
215    public Ambience Ambience {
216      get {
217        return mAmbience;
218      }
219      set {
220        mAmbience = value;
221        RaiseOnAmbienceChanged(new AmbienceEventArgs(mAmbience));
222      }
223    }
224
225    /// <summary>
226    /// Gets the default layer.
227    /// </summary>
228    /// <value>The default layer.</value>
229    public ILayer DefaultLayer {
230      get {
231        return mDefaultLayer;
232      }
233    }
234    /// <summary>
235    /// Gets or sets the Layers
236    /// </summary>
237    public CollectionBase<ILayer> Layers {
238      get {
239        return mLayers;
240      }
241      set {
242        mLayers = value;
243      }
244    }
245    /// <summary>
246    /// Gets or sets the Name
247    /// </summary>
248    public string Name {
249      get { return mName; }
250      set { mName = value; }
251    }
252    /// <summary>
253    /// the Model field
254    /// </summary>
255    private IModel mModel;
256    /// <summary>
257    /// Gets or sets the Model
258    /// </summary>
259    public IModel Model {
260      get {
261        return mModel;
262      }
263      set {
264        if (value == null)
265          throw new InconsistencyException("The model you want to set has value 'null'.");
266        mModel = value;
267        //this will cascade the setting down the hierarchy
268        foreach (ILayer layer in mLayers)
269          layer.Model = value;
270      }
271    }
272    #endregion
273
274    #region Constructor
275    ///<summary>
276    ///Default constructor
277    ///</summary>
278    public Page(string name, IModel model) {
279      this.mModel = model;
280
281      mName = name;
282      mAmbience = new Ambience(this);
283
284      //the one and only and indestructible layer           
285      mLayers = new CollectionBase<ILayer>();
286      mLayers.Add(new Layer("Default Layer"));
287
288      Init();
289
290
291    }
292
293    /// <summary>
294    /// Initializes this object
295    /// <remarks>See also the <see cref="OnDeserialized"/> event for post-deserialization actions to which this method is related.
296    /// </remarks>
297    /// </summary>
298    private void Init() {
299      if (mLayers == null || mLayers.Count == 0)
300        throw new InconsistencyException("The page object does not contain the expected default layer.");
301      mDefaultLayer = mLayers[0];
302
303      //listen to events
304      AttachToAmbience(mAmbience);
305
306      foreach (ILayer layer in mLayers)
307        AttachToLayer(layer);
308    }
309
310    private void AttachToLayer(ILayer layer) {
311      layer.OnEntityAdded += new EventHandler<EntityEventArgs>(defaultLayer_OnEntityAdded);
312      layer.OnEntityRemoved += new EventHandler<EntityEventArgs>(mDefaultLayer_OnEntityRemoved);
313      layer.OnClear += new EventHandler(mDefaultLayer_OnClear);
314    }
315
316    /// <summary>
317    /// Attaches the model to the ambience class.
318    /// </summary>
319    /// <param name="ambience">The ambience.</param>
320    private void AttachToAmbience(Ambience ambience) {
321      if (ambience == null)
322        throw new ArgumentNullException("The ambience object assigned to the model cannot be 'null'");
323
324      mAmbience.OnAmbienceChanged += new EventHandler<AmbienceEventArgs>(mAmbience_OnAmbienceChanged);
325    }
326
327    // ------------------------------------------------------------------
328    /// <summary>
329    /// Gets the layer that has the entity specified.  If the entity
330    /// specified could not be found, 'null' is returned.
331    /// </summary>
332    /// <param name="entity">IDiagramEntity</param>
333    /// <returns>ILayer</returns>
334    // ------------------------------------------------------------------
335    public ILayer GetLayer(IDiagramEntity entity) {
336      foreach (ILayer layer in mLayers) {
337        if (layer.Entities.Contains(entity)) {
338          return layer;
339        }
340      }
341      return null;
342    }
343
344    public void Paint(Graphics g, bool showGrid) {
345      // First paint the grid.
346      if (showGrid) {
347        float penWidth = 1F;
348        float max = 0;
349
350        if (Magnification != Size.Empty) {
351          max = Math.Max(
352              Magnification.Width / 100F,
353              Magnification.Height / 100F);
354        }
355
356        // Scale the pen size by the magnification so the grid
357        // lines don't get magnified (stays a constant width).
358        if (max > 0) {
359          penWidth = penWidth / max;
360        }
361
362        // Show the grid lines in every 1/4th of the current
363        // measurement units.
364        float xGridSpacing = g.DpiX / 4;
365        float yGridSpacing = g.DpiY / 4;
366
367        Grid.Paint(g, Bounds, xGridSpacing, yGridSpacing, penWidth);
368      }
369
370      // The old way, before multiple layers were allowed.
371      //foreach (IDiagramEntity entity in mModel.Paintables)
372      //{
373      //    entity.Paint(g);               
374      //}
375
376      // I was going to do it this way, but then decided it's easier
377      // to check the 'Layer.IsVisible' property for each entity.
378      //foreach (ILayer layer in mModel.CurrentPage.Layers)
379      //{
380      //    if (layer.IsVisible)
381      //    {
382      //        foreach (IDiagramEntity entity in layer.Entities)
383      //        {
384      //            entity.Paint(g);
385      //        }
386      //    }
387      //}         
388
389      foreach (IDiagramEntity entity in this.Entities) {
390        // If this entity isn't attached to a layer, then paint
391        // it regardless.
392        if ((entity.Layer == null) ||
393            (entity.Layer.IsVisible)) {
394          entity.Paint(g);
395        }
396        //else if (entity.Layer.IsVisible)
397        //{
398        //    //entity.Paint(g);
399        //}
400      }
401
402      g.Transform.Reset();
403      g.ResetClip();
404    }
405
406    /// <summary>
407    /// Raises the <see cref="OnAmbienceChanged"/> event
408    /// </summary>
409    /// <param name="e"></param>
410    protected virtual void RaiseOnAmbienceChanged(AmbienceEventArgs e) {
411      EventHandler<AmbienceEventArgs> handler = OnAmbienceChanged;
412      if (handler != null) {
413        handler(this, e);
414      }
415    }
416
417    /// <summary>
418    /// Handles the OnClear event of the DefaultLayer.
419    /// </summary>
420    /// <param name="sender">The source of the event.</param>
421    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
422    void mDefaultLayer_OnClear(object sender, EventArgs e) {
423      EventHandler handler = OnClear;
424      if (handler != null)
425        handler(sender, e);
426    }
427
428    /// <summary>
429    /// Handles the OnEntityRemoved event of the DefaultLayer.
430    /// </summary>
431    /// <param name="sender">The source of the event.</param>
432    /// <param name="e">The <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance containing the event data.</param>
433    void mDefaultLayer_OnEntityRemoved(object sender, EntityEventArgs e) {
434      EventHandler<EntityEventArgs> handler = OnEntityRemoved;
435      if (handler != null)
436        handler(sender, e);
437    }
438
439    /// <summary>
440    /// Handles the OnEntityAdded event of the defaultLayer.
441    /// </summary>
442    /// <param name="sender">The source of the event.</param>
443    /// <param name="e">The <see cref="T:Netron.Diagramming.Core.EntityEventArgs"/> instance containing the event data.</param>
444    void defaultLayer_OnEntityAdded(object sender, EntityEventArgs e) {
445      EventHandler<EntityEventArgs> handler = OnEntityAdded;
446      if (handler != null)
447        handler(sender, e);
448    }
449
450    /// <summary>
451    /// Handles the OnAmbienceChanged event of the <see cref="Ambience"/>.
452    /// </summary>
453    /// <param name="sender">The source of the event.</param>
454    /// <param name="e">The <see cref="T:Netron.Diagramming.Core.AmbienceEventArgs"/> instance containing the event data.</param>
455    void mAmbience_OnAmbienceChanged(object sender, AmbienceEventArgs e) {
456      //pass on the good news, eventually the View will be notified and the canvas will be redrawn
457      RaiseOnAmbienceChanged(e);
458    }
459    #endregion
460
461  }
462}
Note: See TracBrowser for help on using the repository browser.