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