1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Text;
|
---|
5 | using System.Drawing;
|
---|
6 | using System.Drawing.Drawing2D;
|
---|
7 | namespace 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 |
|
---|
293 | #endregion
|
---|
294 |
|
---|
295 | #region no connector was hit, maybe the entity itself
|
---|
296 | if (entity.Hit(surfacePoint)) {
|
---|
297 | SelectEntity(entity, surfacePoint);
|
---|
298 | break;
|
---|
299 | }
|
---|
300 | #endregion
|
---|
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
|
---|
389 | {
|
---|
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
|
---|
399 | {
|
---|
400 | mSelection.Add(entity);
|
---|
401 | entity.IsSelected = true;
|
---|
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 | }
|
---|
437 | }
|
---|