Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/25/10 17:28:31 (14 years ago)
Author:
mkommend
Message:

finished mapping from OperatorGraph to GraphVisualizationInfo (ticket #867)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/Utils/Selection.cs

    r2768 r2868  
    55using System.Drawing;
    66using System.Drawing.Drawing2D;
    7 namespace Netron.Diagramming.Core
    8 {
    9     /// <summary>
    10     /// This static class collects functions related to bundle selection
    11     /// </summary>
    12     public static class Selection
    13     {
    14         #region Events
    15         public static event EventHandler OnNewSelection;
     7namespace Netron.Diagramming.Core {
     8  /// <summary>
     9  /// This static class collects functions related to bundle selection
     10  /// </summary>
     11  public class Selection {
     12
     13    public Selection(IController controller, IModel model) {
     14      this.mController = controller;
     15      this.mModel = model;
     16    }
     17
     18    #region Events
     19    public event EventHandler OnNewSelection;
     20    #endregion
     21
     22    #region Fields
     23    // ------------------------------------------------------------------
     24    /// <summary>
     25    /// Specifies the way entities are selected.
     26    /// </summary>
     27    // ------------------------------------------------------------------
     28    private SelectionTypes mSelectionType = SelectionTypes.Partial;
     29
     30    // ------------------------------------------------------------------
     31    /// <summary>
     32    /// The selected entities.
     33    /// </summary>
     34    // ------------------------------------------------------------------
     35    private CollectionBase<IDiagramEntity> mSelection =
     36        new CollectionBase<IDiagramEntity>();
     37
     38    // ------------------------------------------------------------------
     39    /// <summary>
     40    /// A pointer to the model.
     41    /// </summary>
     42    // ------------------------------------------------------------------
     43    private IModel mModel;
     44    private IController mController;
     45
     46    // ------------------------------------------------------------------
     47    /// <summary>
     48    /// A pointer to a selected connector.
     49    /// </summary>
     50    // ------------------------------------------------------------------
     51    private IConnector connector;
     52
     53    #endregion
     54
     55    #region Properties
     56
     57    // ------------------------------------------------------------------
     58    /// <summary>
     59    /// Gets or sets the connector selected by the user.
     60    /// </summary>
     61    /// <value>The connector.</value>
     62    // ------------------------------------------------------------------
     63    public IConnector Connector {
     64      get {
     65        return connector;
     66      }
     67      set {
     68        connector = value;
     69      }
     70    }
     71
     72    // ------------------------------------------------------------------
     73    /// <summary>
     74    /// Gets the Model we're attached to.
     75    /// </summary>
     76    // ------------------------------------------------------------------
     77    public IModel Model {
     78      get {
     79        return mModel;
     80      }
     81    }
     82
     83    public IController Controller {
     84      get {
     85        return mController;
     86      }
     87    }
     88
     89    // ------------------------------------------------------------------
     90    /// <summary>
     91    /// Gets or sets the selected items.
     92    /// </summary>
     93    /// <value>The selected items.</value>
     94    // ------------------------------------------------------------------
     95    public CollectionBase<IDiagramEntity> SelectedItems {
     96      get {
     97        return mSelection;
     98      }
     99      internal set {
     100        if (value == null || value.Count == 0)
     101          return;
     102        //clear the current selection
     103        Clear();
     104
     105        mSelection = value;
     106        foreach (IDiagramEntity entity in value) {
     107          if (entity.Group != null)
     108            entity.Group.IsSelected = true;
     109          else
     110            entity.IsSelected = true;
     111        }
     112      }
     113    }
     114
     115    // ------------------------------------------------------------------
     116    /// <summary>
     117    /// Gets the selected items but in flat form, i.e. the entities
     118    /// inside an <see cref="IGroup"/> are collected.
     119    /// </summary>
     120    // ------------------------------------------------------------------
     121    public CollectionBase<IDiagramEntity> FlattenedSelectionItems {
     122      get {
     123        CollectionBase<IDiagramEntity> flatList =
     124            new CollectionBase<IDiagramEntity>();
     125        foreach (IDiagramEntity entity in mSelection) {
     126          if (entity is IGroup)
     127            Utils.TraverseCollect(entity as IGroup, ref flatList);
     128          else
     129            flatList.Add(entity);
     130        }
     131        return flatList;
     132      }
     133    }
     134
     135    #endregion
     136
     137    #region Methods
     138
     139    // ------------------------------------------------------------------
     140    /// <summary>
     141    /// Creates a Bitmap from the selected entities.  If no entities are
     142    /// selected, then 'null' is returned.
     143    /// </summary>
     144    /// <returns>Bitmap</returns>
     145    // ------------------------------------------------------------------
     146    public Bitmap ToBitmap() {
     147      if (SelectedItems.Count <= 0) {
     148        return null;
     149      }
     150
     151      Graphics g = mController.View.Graphics;
     152      g.Transform = mController.View.ViewMatrix;
     153      g.SmoothingMode = SmoothingMode.HighQuality;
     154      Bundle bundle = new Bundle(SelectedItems.Copy());
     155      return ImageExporter.FromBundle(bundle, g);
     156    }
     157
     158    public IConnector FindConnector(Predicate<IConnector> predicate) {
     159      IConnection con;
     160      IShape sh;
     161
     162      foreach (IDiagramEntity entity in Model.Paintables) {
     163        if (typeof(IShape).IsInstanceOfType(entity)) {
     164          sh = entity as IShape;
     165          foreach (IConnector cn in sh.Connectors) {
     166            if (predicate(cn))
     167              return cn;
     168          }
     169        } else if (typeof(IConnection).IsInstanceOfType(entity)) {
     170          con = entity as IConnection;
     171          if (predicate(con.From))
     172            return con.From;
     173          if (predicate(con.To))
     174            return con.To;
     175        }
     176      }
     177      return null;
     178    }
     179
     180    /// <summary>
     181    /// Finds the first connector with the highest z-order under the given point
     182    /// </summary>
     183    /// <returns></returns>
     184    public IConnector FindShapeConnector(Point surfacePoint) {
     185
     186
     187      IShape sh;
     188
     189      foreach (IDiagramEntity entity in Model.Paintables) {
     190        if (typeof(IShape).IsInstanceOfType(entity)) {
     191          sh = entity as IShape;
     192          foreach (IConnector cn in sh.Connectors) {
     193            if (cn.Hit(surfacePoint))
     194              return cn;
     195          }
     196        }
     197      }
     198      return null;
     199    }
     200
     201    public IConnector FindConnectorAt(Point surfacePoint) {
     202      IConnection con;
     203      IShape sh;
     204
     205      foreach (IDiagramEntity entity in Model.Paintables) {
     206        if (entity is IShape) {
     207          sh = entity as IShape;
     208          foreach (IConnector cn in sh.Connectors) {
     209            if (cn.Hit(surfacePoint))
     210              return cn;
     211          }
     212        } else if (entity is IConnection) {
     213          con = entity as IConnection;
     214          if (con.From.Hit(surfacePoint))
     215            return con.From;
     216          if (con.To.Hit(surfacePoint))
     217            return con.To;
     218        }
     219      }
     220      return null;
     221    }
     222    /// <summary>
     223    /// Collects the shapes at the given (transformed surface) location.
     224    /// The shapes selected in this way are available
     225    /// </summary>
     226    /// <param name="surfacePoint">The surface point.</param>
     227    public void CollectEntitiesAt(
     228        Point surfacePoint,
     229        bool clearSelectionFirst) {
     230      if (surfacePoint == Point.Empty)
     231        return;
     232      if (mModel == null)
     233        return;
     234
     235      // Only change the current selection if the mouse did not hit an
     236      // already selected element and the element is not a group.  This
     237      // allows drilling down into group's children.
     238      if (mSelection.Count > 0) {
     239        foreach (IDiagramEntity entity in mSelection) {
     240          if ((entity.Hit(surfacePoint)) &&
     241              ((entity is IGroup) == false)) {
     242            return;
     243          }
     244        }
     245      }
     246
     247      // Clear the current selection only if we're supposed to.
     248      if (clearSelectionFirst) {
     249        Clear();
     250      }
     251
     252      IConnection con;
     253      IShape sh;
     254
     255      //we use the paintables here rather than traversing the scene-graph because only
     256      //visible things can be collected
     257      //We traverse the paintables from top to bottom since the highest z-order
     258      //is at the top of the stack.
     259
     260      for (int k = Model.Paintables.Count - 1; k >= 0; k--) {
     261        IDiagramEntity entity = Model.Paintables[k];
     262
     263        #region we give priority to the connector selection
     264        if (typeof(IConnection).IsInstanceOfType(entity)) {
     265          con = entity as IConnection;
     266          if (con.From.Hit(surfacePoint)) {
     267            connector = con.From;
     268            connector.IsSelected = true;
     269            Invalidate();
     270            return;
     271          }
     272          if (con.To.Hit(surfacePoint)) {
     273            connector = con.To;
     274            connector.IsSelected = true;
     275            Invalidate();
     276            return;
     277          }
     278        } else if (entity is IGroup) {
     279          //should I care about the connectors at this point...?
     280        } else if (entity is IShape) {
     281          sh = entity as IShape;
     282          foreach (IConnector cn in sh.Connectors) {
     283            //if there are connectors attached to the shape connector, the attached ones should be picked up and not the one of the shape
     284            if (cn.Hit(surfacePoint) && cn.AttachedConnectors.Count == 0) {
     285              connector = cn;
     286              connector.IsSelected = true;
     287              Invalidate();//this will invalidate only the selected connector
     288              return; //we hit a connector and quit the selection. If the user intended to select the entity it had to be away from the connector!
     289            }
     290          }
     291        }
     292
    16293        #endregion
    17294
    18         #region Fields
    19 
    20         // ------------------------------------------------------------------
    21         /// <summary>
    22         /// A pointer to the controller.
    23         /// </summary>
    24         // ------------------------------------------------------------------
    25         private static IController mController;
    26 
    27         // ------------------------------------------------------------------
    28         /// <summary>
    29         /// Specifies the way entities are selected.
    30         /// </summary>
    31         // ------------------------------------------------------------------
    32         private static SelectionTypes mSelectionType = SelectionTypes.Partial;
    33 
    34         // ------------------------------------------------------------------
    35         /// <summary>
    36         /// The selected entities.
    37         /// </summary>
    38         // ------------------------------------------------------------------
    39         private static CollectionBase<IDiagramEntity> mSelection =
    40             new CollectionBase<IDiagramEntity>();
    41 
    42         // ------------------------------------------------------------------
    43         /// <summary>
    44         /// A pointer to the model.
    45         /// </summary>
    46         // ------------------------------------------------------------------
    47         private static IModel mModel;
    48 
    49         // ------------------------------------------------------------------
    50         /// <summary>
    51         /// A pointer to a selected connector.
    52         /// </summary>
    53         // ------------------------------------------------------------------
    54         private static IConnector connector;
    55 
     295        #region no connector was hit, maybe the entity itself
     296        if (entity.Hit(surfacePoint)) {
     297          SelectEntity(entity, surfacePoint);
     298          break;
     299        }
    56300        #endregion
    57 
    58         #region Properties
    59 
    60         // ------------------------------------------------------------------
    61         /// <summary>
    62         /// Gets or sets the connector selected by the user.
    63         /// </summary>
    64         /// <value>The connector.</value>
    65         // ------------------------------------------------------------------
    66         public static IConnector Connector
    67         {
    68             get
    69             {
    70                 return Selection.connector;
    71             }
    72             set
    73             {
    74                 Selection.connector = value;
    75             }
    76         }
    77 
    78         // ------------------------------------------------------------------
    79         /// <summary>
    80         /// Gets the Model we're attached to.
    81         /// </summary>
    82         // ------------------------------------------------------------------
    83         public static IModel Model
    84         {
    85             get
    86             {
    87                 return mModel;
    88             }
    89         }
    90 
    91         // ------------------------------------------------------------------
    92         /// <summary>
    93         /// Gets or sets the selected items.
    94         /// </summary>
    95         /// <value>The selected items.</value>
    96         // ------------------------------------------------------------------
    97         public static CollectionBase<IDiagramEntity> SelectedItems
    98         {
    99             get
    100             {
    101                 return Selection.mSelection;
    102             }
    103             internal set
    104             {
    105                 if (value == null || value.Count == 0)
    106                     return;
    107                 //clear the current selection
    108                 Clear();
    109 
    110                 Selection.mSelection = value;
    111                 foreach (IDiagramEntity entity in value)
     301      }
     302      RaiseOnNewSelection();
     303
     304      // Using a full invalidate is rather expensive, so we'll only
     305      // refresh the current selection.
     306      //Controller.View.Invalidate();
     307      Invalidate();
     308    }
     309
     310    internal void SelectEntity(IDiagramEntity entity, Point surfacePoint) {
     311      // Groups are treated specially because we can drill-down
     312      // into the group.  The process of drilling is the first
     313      // mouse hit will select the group.  The second mouse hit
     314      // will select a child, if there's a child at that point.
     315      if (entity is IGroup) {
     316        if (entity.IsSelected == false) {
     317          entity.IsSelected = true;
     318          mSelection.Add(entity);
     319        } else {
     320          IGroup group = entity as IGroup;
     321          for (int j = group.Entities.Count - 1; j >= 0; j--) {
     322            IDiagramEntity child = group.Entities[j];
     323            if (child.Hit(surfacePoint)) {
     324              // Repeat the process because what if this
     325              // child is too a group!
     326              SelectEntity(child, surfacePoint);
     327              group.IsSelected = false;
     328              if (mSelection.Contains(group)) {
     329                mSelection.Remove(group);
     330              }
     331              break;
     332            }
     333          }
     334        }
     335      }
     336        //else if (entity.Group != null)
     337        //{
     338        //    //entity.Group.IsSelected = true;
     339        //    //mSelection.Add(entity.Group);
     340        //}
     341      else {
     342        entity.IsSelected = true;
     343        mSelection.Add(entity);
     344      }
     345    }
     346
     347    private void RaiseOnNewSelection() {
     348      if (OnNewSelection != null)
     349        OnNewSelection(null, EventArgs.Empty);
     350    }
     351    /// <summary>
     352    /// Invalidates the current selection (either a connector or a set of entities).
     353    /// </summary>
     354    public void Invalidate() {
     355      if (connector != null)
     356        connector.Invalidate();
     357
     358      foreach (IDiagramEntity entity in mSelection) {
     359        entity.Invalidate();
     360      }
     361    }
     362    /// <summary>
     363    /// Collects the entities inside the given rectangle.
     364    /// </summary>
     365    /// <param name="surfaceRectangle">The surface rectangle.</param>
     366    public void CollectEntitiesInside(Rectangle surfaceRectangle) {
     367      if (surfaceRectangle == Rectangle.Empty)
     368        return;
     369      this.Clear();
     370      foreach (IDiagramEntity entity in Model.Paintables) {
     371        //if the entity is part of a group we have to look at the bigger picture
     372        if (mSelectionType == SelectionTypes.Inclusion) {
     373          if (entity.Group != null) {
     374            //the rectangle must contain the whole group
     375            if (surfaceRectangle.Contains(entity.Group.Rectangle)) {
     376              //add the group if not already present via another group member
     377              if (!mSelection.Contains(entity.Group))
     378                mSelection.Add(entity.Group);
     379              continue;
     380            }
     381          } else {
     382            if (surfaceRectangle.Contains(entity.Rectangle)) {
     383
     384              mSelection.Add(entity);
     385              entity.IsSelected = true;
     386            }
     387          }
     388        } else //the selection requires only partial overlap with the rectangle
    112389                {
    113                     if (entity.Group != null)
    114                         entity.Group.IsSelected = true;
    115                     else
    116                         entity.IsSelected = true;
    117                 }
    118             }
    119         }
    120 
    121         // ------------------------------------------------------------------
    122         /// <summary>
    123         /// Gets the selected items but in flat form, i.e. the entities
    124         /// inside an <see cref="IGroup"/> are collected.
    125         /// </summary>
    126         // ------------------------------------------------------------------
    127         public static CollectionBase<IDiagramEntity> FlattenedSelectionItems
    128         {
    129             get
    130             {
    131                 CollectionBase<IDiagramEntity> flatList =
    132                     new CollectionBase<IDiagramEntity>();
    133                 foreach (IDiagramEntity entity in mSelection)
    134                 {
    135                     if (entity is IGroup)
    136                         Utils.TraverseCollect(entity as IGroup, ref flatList);
    137                     else
    138                         flatList.Add(entity);
    139                 }
    140                 return flatList;
    141             }
    142         }
    143 
    144         // ------------------------------------------------------------------
    145         /// <summary>
    146         /// Gets or sets the Controller.
    147         /// </summary>
    148         /// <value>The model.</value>
    149         // ------------------------------------------------------------------
    150         public static IController Controller
    151         {
    152             get
    153             {
    154                 return Selection.mController;
    155             }
    156             set
    157             {
    158                 Selection.mController = value;
    159                 mModel = value.Model;
    160             }
    161         }
    162         #endregion
    163 
    164         #region Methods
    165 
    166         // ------------------------------------------------------------------
    167         /// <summary>
    168         /// Creates a Bitmap from the selected entities.  If no entities are
    169         /// selected, then 'null' is returned.
    170         /// </summary>
    171         /// <returns>Bitmap</returns>
    172         // ------------------------------------------------------------------
    173         public static Bitmap ToBitmap()
    174         {
    175             if (SelectedItems.Count <= 0)
    176             {
    177                 return null;
    178             }
    179 
    180             Graphics g = Controller.View.Graphics;
    181             g.Transform = Controller.View.ViewMatrix;
    182             g.SmoothingMode = SmoothingMode.HighQuality;
    183             Bundle bundle = new Bundle(SelectedItems.Copy());
    184             return ImageExporter.FromBundle(bundle, g);
    185         }
    186 
    187         public static IConnector FindConnector(Predicate<IConnector> predicate)
    188         {
    189             IConnection con;
    190             IShape sh;
    191 
    192             foreach (IDiagramEntity entity in Model.Paintables)
    193             {
    194                 if (typeof(IShape).IsInstanceOfType(entity))
    195                 {
    196                     sh = entity as IShape;
    197                     foreach (IConnector cn in sh.Connectors)
    198                     {
    199                         if (predicate(cn))
    200                             return cn;
    201                     }
    202                 }
    203                 else if (typeof(IConnection).IsInstanceOfType(entity))
    204                 {
    205                     con = entity as IConnection;
    206                     if (predicate(con.From))
    207                         return con.From;
    208                     if (predicate(con.To))
    209                         return con.To;
    210                 }
    211             }
    212             return null;
    213         }
    214 
    215         /// <summary>
    216         /// Finds the first connector with the highest z-order under the given point
    217         /// </summary>
    218         /// <returns></returns>
    219         public static IConnector FindShapeConnector(Point surfacePoint)
    220         {
    221 
    222 
    223             IShape sh;
    224 
    225             foreach (IDiagramEntity entity in Model.Paintables)
    226             {
    227                 if (typeof(IShape).IsInstanceOfType(entity))
    228                 {
    229                     sh = entity as IShape;
    230                     foreach (IConnector cn in sh.Connectors)
    231                     {
    232                         if (cn.Hit(surfacePoint))
    233                             return cn;
    234                     }
    235                 }
    236             }
    237             return null;
    238         }
    239 
    240         public static IConnector FindConnectorAt(Point surfacePoint)
    241         {
    242             IConnection con;
    243             IShape sh;
    244 
    245             foreach (IDiagramEntity entity in Model.Paintables)
    246             {
    247                 if (entity is IShape)
    248                 {
    249                     sh = entity as IShape;
    250                     foreach (IConnector cn in sh.Connectors)
    251                     {
    252                         if (cn.Hit(surfacePoint))
    253                             return cn;
    254                     }
    255                 }
    256                 else if (entity is IConnection)
    257                 {
    258                     con = entity as IConnection;
    259                     if (con.From.Hit(surfacePoint))
    260                         return con.From;
    261                     if (con.To.Hit(surfacePoint))
    262                         return con.To;
    263                 }
    264             }
    265             return null;
    266         }
    267         /// <summary>
    268         /// Collects the shapes at the given (transformed surface) location.
    269         /// The shapes selected in this way are available
    270         /// </summary>
    271         /// <param name="surfacePoint">The surface point.</param>
    272         public static void CollectEntitiesAt(
    273             Point surfacePoint,
    274             bool clearSelectionFirst)
    275         {
    276             if (surfacePoint == Point.Empty)
    277                 return;
    278             if (Selection.mController == null)
    279                 return;
    280 
    281             // Only change the current selection if the mouse did not hit an
    282             // already selected element and the element is not a group.  This
    283             // allows drilling down into group's children.
    284             if (mSelection.Count > 0)
    285             {
    286                 foreach (IDiagramEntity entity in mSelection)
    287                 {
    288                     if ((entity.Hit(surfacePoint)) &&
    289                         ((entity is IGroup) == false))
    290                     {
    291                         return;
    292                     }
    293                 }
    294             }
    295 
    296             // Clear the current selection only if we're supposed to.
    297             if (clearSelectionFirst)
    298             {
    299                 Selection.Clear();
    300             }
    301 
    302             IConnection con;
    303             IShape sh;
    304 
    305             //we use the paintables here rather than traversing the scene-graph because only
    306             //visible things can be collected
    307             //We traverse the paintables from top to bottom since the highest z-order
    308             //is at the top of the stack.
    309 
    310             for (int k = Model.Paintables.Count - 1; k >= 0; k--)
    311             {
    312                 IDiagramEntity entity = Model.Paintables[k];
    313 
    314                 #region we give priority to the connector selection
    315                 if (typeof(IConnection).IsInstanceOfType(entity))
    316                 {
    317                     con = entity as IConnection;
    318                     if (con.From.Hit(surfacePoint))
    319                     {
    320                         connector = con.From;
    321                         connector.IsSelected = true;
    322                         Invalidate();
    323                         return;
    324                     }
    325                     if (con.To.Hit(surfacePoint))
    326                     {
    327                         connector = con.To;
    328                         connector.IsSelected = true;
    329                         Invalidate();
    330                         return;
    331                     }
    332                 }
    333                 else if (entity is IGroup)
    334                 {
    335                   //should I care about the connectors at this point...?
    336                 }
    337                 else if (entity is IShape)
    338                 {
    339                     sh = entity as IShape;
    340                     foreach (IConnector cn in sh.Connectors)
    341                     {
    342                         //if there are connectors attached to the shape connector, the attached ones should be picked up and not the one of the shape
    343                         if (cn.Hit(surfacePoint) && cn.AttachedConnectors.Count == 0)
    344                         {
    345                             connector = cn;
    346                             connector.IsSelected = true;
    347                             Invalidate();//this will invalidate only the selected connector
    348                             return; //we hit a connector and quit the selection. If the user intended to select the entity it had to be away from the connector!
    349                         }
    350                     }
    351                 }
    352 
    353                 #endregion
    354 
    355                 #region no connector was hit, maybe the entity itself
    356                 if (entity.Hit(surfacePoint))
    357                 {
    358                     SelectEntity(entity, surfacePoint);
    359                     break;
    360                 }
    361                 #endregion
    362             }
    363             RaiseOnNewSelection();
    364 
    365             // Using a full invalidate is rather expensive, so we'll only
    366             // refresh the current selection.
    367             //Controller.View.Invalidate();
    368             Invalidate();
    369         }
    370 
    371         static void SelectEntity(IDiagramEntity entity, Point surfacePoint)
    372         {
    373             // Groups are treated specially because we can drill-down
    374             // into the group.  The process of drilling is the first
    375             // mouse hit will select the group.  The second mouse hit
    376             // will select a child, if there's a child at that point.
    377             if (entity is IGroup)
    378             {
    379                 if (entity.IsSelected == false)
    380                 {
    381                     entity.IsSelected = true;
    382                     mSelection.Add(entity);
    383                 }
    384                 else
    385                 {
    386                     IGroup group = entity as IGroup;
    387                     for (int j = group.Entities.Count - 1; j >= 0; j--)
    388                     {
    389                         IDiagramEntity child = group.Entities[j];
    390                         if (child.Hit(surfacePoint))
    391                         {
    392                             // Repeat the process because what if this
    393                             // child is too a group!
    394                             SelectEntity(child, surfacePoint);
    395                             group.IsSelected = false;
    396                             if (mSelection.Contains(group))
     390          if (entity.Group != null) {
     391            if (surfaceRectangle.IntersectsWith(entity.Group.Rectangle)) {
     392              if (!mSelection.Contains(entity.Group))
     393                mSelection.Add(entity.Group);
     394              continue;
     395            }
     396          } else {
     397            if (surfaceRectangle.IntersectsWith(entity.Rectangle)) {
     398              if (!mSelection.Contains(entity))//it could be a group which got already selected by one of its children
    397399                            {
    398                                 mSelection.Remove(group);
    399                             }
    400                             break;
    401                         }
    402                     }
    403                 }
    404             }
    405             //else if (entity.Group != null)
    406             //{
    407             //    //entity.Group.IsSelected = true;
    408             //    //mSelection.Add(entity.Group);
    409             //}
    410             else
    411             {
     400                mSelection.Add(entity);
    412401                entity.IsSelected = true;
    413                 mSelection.Add(entity);
    414             }
    415         }
    416 
    417         private static void RaiseOnNewSelection()
    418         {
    419             if (OnNewSelection != null)
    420                 OnNewSelection(null, EventArgs.Empty);
    421         }
    422         /// <summary>
    423         /// Invalidates the current selection (either a connector or a set of entities).
    424         /// </summary>
    425         public static void Invalidate()
    426         {
    427             if (connector != null)
    428                 connector.Invalidate();
    429 
    430             foreach (IDiagramEntity entity in mSelection)
    431             {
    432                 entity.Invalidate();
    433             }
    434         }
    435         /// <summary>
    436         /// Collects the entities inside the given rectangle.
    437         /// </summary>
    438         /// <param name="surfaceRectangle">The surface rectangle.</param>
    439         public static void CollectEntitiesInside(Rectangle surfaceRectangle)
    440         {
    441             if (surfaceRectangle == Rectangle.Empty)
    442                 return;
    443             Selection.Clear();
    444             foreach (IDiagramEntity entity in Selection.Controller.Model.Paintables)
    445             {
    446                 //if the entity is part of a group we have to look at the bigger picture
    447                 if (mSelectionType == SelectionTypes.Inclusion)
    448                 {
    449                     if (entity.Group != null)
    450                     {
    451                         //the rectangle must contain the whole group
    452                         if (surfaceRectangle.Contains(entity.Group.Rectangle))
    453                         {
    454                             //add the group if not already present via another group member
    455                             if (!mSelection.Contains(entity.Group))
    456                                 mSelection.Add(entity.Group);
    457                             continue;
    458                         }
    459                     }
    460                     else
    461                     {
    462                         if (surfaceRectangle.Contains(entity.Rectangle))
    463                         {
    464 
    465                             mSelection.Add(entity);
    466                             entity.IsSelected = true;
    467                         }
    468                     }
    469                 }
    470                 else //the selection requires only partial overlap with the rectangle
    471                 {
    472                     if (entity.Group != null)
    473                     {
    474                         if (surfaceRectangle.IntersectsWith(entity.Group.Rectangle))
    475                         {
    476                             if (!mSelection.Contains(entity.Group))
    477                                 mSelection.Add(entity.Group);
    478                             continue;
    479                         }
    480                     }
    481                     else
    482                     {
    483                         if (surfaceRectangle.IntersectsWith(entity.Rectangle))
    484                         {
    485                             if (!mSelection.Contains(entity))//it could be a group which got already selected by one of its children
    486                             {
    487                                 mSelection.Add(entity);
    488                                 entity.IsSelected = true;
    489                             }
    490                         }
    491                     }
    492                 }
    493 
    494 
    495 
    496             }
    497             RaiseOnNewSelection();
    498 
    499         }
    500         /// <summary>
    501         /// Clears the current selection
    502         /// </summary>
    503         public static void Clear()
    504         {
    505             if (connector != null)
    506             {
    507                 connector.IsSelected = false;
    508                 connector = null;
    509             }
    510 
    511 
    512 
    513             if (Selection.mController == null)
    514                 return;
    515             //deselect the current ones
    516             foreach (IDiagramEntity entity in SelectedItems)
    517             {
    518                 entity.IsSelected = false;
    519             }
    520             //forget the current state
    521             mSelection.Clear();
    522             if (Controller.View != null)
    523                 Controller.View.HideTracker();
    524         }
    525 
    526 
    527         #endregion
    528     }
     402              }
     403            }
     404          }
     405        }
     406
     407
     408
     409      }
     410      RaiseOnNewSelection();
     411
     412    }
     413    /// <summary>
     414    /// Clears the current selection
     415    /// </summary>
     416    public void Clear() {
     417      if (connector != null) {
     418        connector.IsSelected = false;
     419        connector = null;
     420      }
     421
     422      if (mController == null || mModel == null)
     423        return;
     424      //deselect the current ones
     425      foreach (IDiagramEntity entity in SelectedItems) {
     426        entity.IsSelected = false;
     427      }
     428      //forget the current state
     429      mSelection.Clear();
     430      if (Controller.View != null)
     431        Controller.View.HideTracker();
     432    }
     433
     434
     435    #endregion
     436  }
    529437}
Note: See TracChangeset for help on using the changeset viewer.