Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.7/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Tools/SendBackwardsTool.cs @ 18242

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

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

File size: 4.2 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Diagnostics;
5namespace Netron.Diagramming.Core {
6  /// <summary>
7  /// Group tool
8  /// </summary>
9  class SendBackwardsTool : AbstractTool {
10
11    #region Fields
12
13    #endregion
14
15    #region Constructor
16    /// <summary>
17    /// Initializes a new instance of the <see cref="T:SendBackwards"/> class.
18    /// </summary>
19    /// <param name="name">The name of the tool.</param>
20    public SendBackwardsTool(string name)
21      : base(name) {
22    }
23    #endregion
24
25    #region Methods
26
27    /// <summary>
28    /// Called when the tool is activated.
29    /// </summary>
30    protected override void OnActivateTool() {
31      if (this.Controller.Model.Selection.SelectedItems != null && this.Controller.Model.Selection.SelectedItems.Count > 0) {
32        /*
33       * They should give me a Nobel prize for so much thinking early in the morning...
34       */
35        #region Preparation of the ordering
36        Debug.Assert(this.Controller.Model.Selection.SelectedItems[0] != null, "A selection cannot contain a 'null' entity.");
37        //the items have to be moved in the order of the Paintables; the SortedList automatically orders things for us.
38        SortedList<int, IDiagramEntity> list = new SortedList<int, IDiagramEntity>();
39        //We fetch a flattened selection, which means that if there is a group the constituents will be
40        //returned rather than the group itself.
41        foreach (IDiagramEntity entity in this.Controller.Model.Selection.FlattenedSelectionItems) {
42          //the addition will automatically put the item in increasing order
43          list.Add(this.Controller.Model.Paintables.IndexOf(entity), entity);
44        }
45        //if the lowest z-value is the first one in the paintables we cannot shift anything, so we quit
46        if (list.Keys[0] == 0) return;
47
48        /*Send them forwards but make sure it's a visible effect!
49        It's not enough to move it only once since the shape(s) above might be of
50        high z-order degree, so we have to find which is the first shape overlapping with
51        the selection and take as many steps as it takes to surpass it.
52         If there is no overlap we'll shift the z-order with just one unit.
53        */
54        int delta = 1;
55        int lowestZInSelection = list.Keys[0];
56        bool found = false;
57        //we can speed up the loop by noticing that the previous shape in the z-stack is necessarily
58        //below the first one of the selection
59        for (int m = lowestZInSelection - 1; m >= 0 && !found; m--) {
60          //the overlap has to be with an entity, not from the selection
61          if (list.ContainsValue(this.Controller.Model.Paintables[m])) continue;
62          for (int s = 0; s < list.Count; s++) {
63            //if there is an overlap we found the required index
64            if (this.Controller.Model.Paintables[m].Rectangle.IntersectsWith(list.Values[s].Rectangle)) {
65              //an additional complication here; if the found shape is part of a group we have
66              //to take the upper z-value of the group...
67              if (this.Controller.Model.Paintables[m].Group != null) {
68                int min = int.MaxValue;
69                CollectionBase<IDiagramEntity> leafs = new CollectionBase<IDiagramEntity>();
70                Utils.TraverseCollect(this.Controller.Model.Paintables[m].Group, ref leafs);
71                foreach (IDiagramEntity groupMember in leafs) {
72                  min = Math.Min(min, this.Controller.Model.Paintables.IndexOf(groupMember));
73                }
74                //take the found z-value of the group rather than the one of the group-child
75                m = min;
76              }
77              delta = lowestZInSelection - m;
78              found = true;
79              break;
80            }
81
82          }
83        }
84        #endregion
85        Debug.Assert(delta >= 1, "The shift cannot be less than one since we checked previous situations earlier.");
86        for (int k = 0; k < list.Count; k++) {
87          this.Controller.Model.SendBackwards(list.Values[k], delta);
88        }
89      }
90      DeactivateTool();
91    }
92
93    #endregion
94
95
96  }
97
98}
Note: See TracBrowser for help on using the repository browser.