forked from SharpMap/SharpMap.DeltaShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectTool.cs
More file actions
1018 lines (914 loc) · 38.3 KB
/
SelectTool.cs
File metadata and controls
1018 lines (914 loc) · 38.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using GeoAPI.Extensions.Networks;
using GeoAPI.Geometries;
using SharpMap.Api;
using SharpMap.Api.Editors;
using SharpMap.CoordinateSystems.Transformations;
using SharpMap.Editors;
using log4net;
using SharpMap.Data.Providers;
using SharpMap.Layers;
using SharpMap.Rendering;
using SharpMap.Rendering.Thematics;
using SharpMap.Styles;
using SharpMap.UI.Forms;
using SharpMap.UI.Helpers;
using GeoAPI.Extensions.Feature;
using System.ComponentModel;
using DelftTools.Utils.Collections;
using NetTopologySuite.Extensions.Geometries;
using GeometryFactory = SharpMap.Converters.Geometries.GeometryFactory;
using Point = NetTopologySuite.Geometries.Point;
namespace SharpMap.UI.Tools
{
public enum MultiSelectionMode
{
Rectangle = 0,
Lasso
}
/// <summary>
/// SelectTool enables users to select features in the map
/// The current implementation supports:
/// - single selection feature by click on feature
/// - multiple selection of feature by dragging a rectangle
/// - adding features to the selection (KeyExtendSelection; normally the SHIFT key)
/// - toggling selection of features (KeyToggleSelection; normally the CONTROL key)
/// if featues is not in selection it is added to selection
/// if feature is in selection it is removed from selection
/// - Selection is visible to the user via Trackers. Features with an IPoint geometry have 1
/// tracker, based on ILineString and IPolygon have a tracker for each coordinate
/// - Trackers can have focus.
/// If a Trackers has focus is visible to the user via another symbol (or same symbol in other color)
/// A tracker that has the focus is the tracker leading during special operation such as moving.
/// For single selection a feature with an IPoint geometry automatically get the focus to the
/// only tracker
/// - Multiple Trackers with focus
/// - adding focus Trackers (KeyExtendSelection; normally the SHIFT key)
/// - * KeyExtendSelection can be used to select all branches between the most recent two selected branches,
/// using shortest path.
/// - toggling focus Trackers (KeyToggleSelection; normally the CONTROL key)
/// - Selection cycling, When multiple features overlap clicking on a selected feature will
/// result in the selection of the next feature. Compare behavior in Sobek Netter.
///
/// TODO
/// - functionality reasonably ok, but TOO complex : refactor using tests
/// - Selection cycling can be improved:
/// - for a ILineString the focus tracker is not set initially which can be set in the second
/// click. Thus a ILineString (and IPolygon) can eat a click
/// - if feature must be taken into account by selection cycling should be an option
/// (topology rule?)
/// </summary>
public class SelectTool : MapTool
{
private static readonly ILog log = LogManager.GetLogger(typeof(SelectTool));
public MultiSelectionMode MultiSelectionMode { get; set; }
/// <summary>
/// Interactors created for selected features.
/// </summary>
public IList<IFeatureInteractor> SelectedFeatureInteractors { get; private set; }
private readonly Collection<TrackerFeature> trackers = new Collection<TrackerFeature>();
/// <summary>
/// Current layer where features are being selected (branch, nodes, etc.)
/// </summary>
private VectorLayer TrackingLayer // will be TrackingLayer containing tracking geometries
{
get { return trackingLayer; }
}
public int SelectedTrackersCount
{
get { return SelectedTrackerIndices.Count; }
}
public IList<int> SelectedTrackerIndices
{
get
{
return SelectedFeatureInteractors.Count == 1
? SelectedFeatureInteractors[0].Trackers
.Where(t => t.Selected)
.Select(t => t.Index).ToList()
: new List<int>();
}
}
public bool KeyToggleSelection
{
get { return ((Control.ModifierKeys & Keys.Control) == Keys.Control); }
}
public bool KeyExtendSelection
{
get { return ((Control.ModifierKeys & Keys.Shift) == Keys.Shift); }
}
public SelectTool()
{
orgClickTime = DateTime.Now;
SelectedFeatureInteractors = new List<IFeatureInteractor>();
Name = "Select";
trackingLayer.Name = "Trackers";
var trackerProvider = new FeatureCollection { Features = trackers };
trackingLayer.DataSource = trackerProvider;
var iTheme = new CustomTheme(GetTrackerStyle);
trackingLayer.Theme = iTheme;
}
private bool IsMultiSelect { get; set; }
public override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Render(e.Graphics, MapControl.Map);
}
public override void Render(Graphics graphics, Map map)
{
if (trackers.Count == 0)
return;
// Render the selectionLayer and trackingLayer
// Bypass ILayer.Render and call OnRender directly; this is more efficient
foreach (var tracker in trackers)
{
if (null != tracker.FeatureInteractor.SourceFeature)
{
// TODO: add transformation
// todo optimize this; only necessary when map extent has changed.
tracker.FeatureInteractor.UpdateTracker(tracker.FeatureInteractor.SourceFeature.Geometry);
}
}
//trackingLayer.CoordinateTransformation = null; //trackers[0].FeatureInteractor.Layer.CoordinateTransformation;
SynchronizeTrackers();
trackingLayer.OnRender(graphics, map);
}
public TrackerFeature GetTrackerAtCoordinate(Coordinate worldPos)
{
TrackerFeature trackerFeature = null;
foreach (IFeatureInteractor featureInteractor in SelectedFeatureInteractors)
{
trackerFeature = featureInteractor.GetTrackerAtCoordinate(worldPos);
if (null != trackerFeature)
break;
}
return trackerFeature;
}
private Coordinate orgMouseDownLocation;
private DateTime orgClickTime;
private bool clickOnExistingSelection;
private void SetClickOnExistingSelection(bool set, Coordinate worldPosition)
{
clickOnExistingSelection = set;
if (clickOnExistingSelection)
{
orgMouseDownLocation = (Coordinate)worldPosition.Clone();
}
else
{
orgMouseDownLocation = null;
}
}
private IFeatureInteractor GetActiveFeatureInteractor(IFeature feature)
{
return SelectedFeatureInteractors.FirstOrDefault(t => ReferenceEquals(t.SourceFeature, feature));
}
private INode sourceNode, targetNode;
public override void OnMouseDown(Coordinate worldPosition, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
{
return;
}
var oldSelectedTrackerIndicesCount = SelectedTrackersCount;
var oldTrackerFeatureCount = trackers.Count;
IsBusy = true;
ILayer selectedLayer;
mouseDownLocation = worldPosition;
// Check first if an object is already selected and if the mousedown has occured at this object.
var trackerFeature = GetTrackerAtCoordinate(worldPosition);
if (SelectedFeatureInteractors.Count > 1)
{
// hack: if multiple selection toggle/select complete feature
trackerFeature = null;
}
SetClickOnExistingSelection(false, null);
if (null != trackerFeature)
{
if (1 == SelectedFeatureInteractors.Count)
{
SetClickOnExistingSelection(true, worldPosition);
FocusTracker(trackerFeature);
MapControl.Refresh();
}
return;
}
// single selection. Find the nearest geometry and give
var limit = (float)MapHelper.ImageToWorld(Map, 4);
var nearest = FindNearestFeature(worldPosition, limit, out selectedLayer, ol => ol.Visible);
if (null != nearest)
{
// Create or add a new FeatureInteractor
if (SelectedFeatureInteractors.Count > 0)
{
IFeatureInteractor currentFeatureInteractor = GetActiveFeatureInteractor(nearest);
if (KeyExtendSelection) // Shift key
{
if (null == currentFeatureInteractor)
{
var selectedBranch = nearest as IBranch;
if (selectedBranch != null)
{
SelectBranchesAlongShortestPath(worldPosition, selectedLayer, nearest, selectedBranch);
}
else
{
// not in selection; add
AddSelection(selectedLayer, nearest);
targetNode = null;
}
} // else possibly set default focus tracker
}
else if (KeyToggleSelection) // CTRL key
{
if (null == currentFeatureInteractor)
{
// not in selection; add
AddSelection(selectedLayer, nearest);
SetTargetNodeIfBranch(worldPosition, nearest);
}
else
{
// in selection; remove
RemoveSelection(nearest);
targetNode = null;
}
}
else
{
// no special key processing; handle as a single select.
Clear(false);
if (!StartSelection(selectedLayer, nearest))
{
StartMultiSelect();
}
SetTargetNodeIfBranch(worldPosition, nearest);
}
}
else
{
if (!StartSelection(selectedLayer, nearest))
{
StartMultiSelect();
}
SetTargetNodeIfBranch(worldPosition, nearest);
}
}
else
{
// We didn't find an object at the position of the mouse button -> start a multiple select
if (!KeyExtendSelection)
{
// we are not extending the current selection
Clear(false);
}
if (e.Button == MouseButtons.Left)
{
StartMultiSelect();
}
}
if ((oldSelectedTrackerIndicesCount != SelectedTrackersCount
|| oldTrackerFeatureCount != trackers.Count) && trackingLayer.DataSource.Features.Count != 0)
{
MapControl.Refresh();
}
}
private void SelectBranchesAlongShortestPath(Coordinate worldPosition, ILayer selectedLayer, IFeature nearest,
IBranch selectedBranch)
{
// TODO: add transformation
sourceNode = targetNode;
targetNode = selectedBranch.Source.Geometry.Distance(new Point(worldPosition)) <
selectedBranch.Target.Geometry.Distance(new Point(worldPosition))
? selectedBranch.Source
: selectedBranch.Target;
var result = selectedBranch.Network.GetShortestPath(sourceNode, targetNode, null);
foreach (var branch in result)
{
AddSelection(selectedLayer, branch);
}
// Ensure 'nearest' will be added to the selection
if (!result.Contains(selectedBranch))
{
AddSelection(selectedLayer, nearest);
}
}
private void SetTargetNodeIfBranch(Coordinate worldPosition, IFeature nearest)
{
var selectedBranch = nearest as IBranch;
if (nearest is IBranch)
{
// TODO: add transformation
targetNode = selectedBranch.Source.Geometry.Distance(new Point(worldPosition)) <
selectedBranch.Target.Geometry.Distance(new Point(worldPosition))
? selectedBranch.Source
: selectedBranch.Target;
}
else
{
// Not selecting a branch, thus clear 'targetNode'
targetNode = null;
}
}
private void StartMultiSelect()
{
IsMultiSelect = true;
selectPoints.Clear();
UpdateMultiSelection(mouseDownLocation);
StartDrawing();
}
private void StopMultiSelect()
{
IsMultiSelect = false;
StopDrawing();
}
/// <summary>
/// Returns styles used by tracker features.
/// </summary>
/// <param name="feature"></param>
/// <returns></returns>
private static VectorStyle GetTrackerStyle(IFeature feature)
{
var trackerFeature = (TrackerFeature)feature;
VectorStyle style;
// styles are stored in the cache for performance reasons
lock (stylesCache)
{
if (!stylesCache.ContainsKey(trackerFeature.Bitmap))
{
style = new VectorStyle { Symbol = trackerFeature.Bitmap };
stylesCache[trackerFeature.Bitmap] = style;
}
else
{
style = stylesCache[trackerFeature.Bitmap];
}
}
return style;
}
static readonly IDictionary<Bitmap, VectorStyle> stylesCache = new Dictionary<Bitmap, VectorStyle>();
/// <summary>
/// Note: this method seems to assume the whole network is in a non-editing state.
/// Calling it for a network in the middle of an edit-action might cause bugs.
/// </summary>
public void Clear()
{
Clear(true);
}
private void Clear(bool fireSelectionChangedEvent)
{
SelectedFeatureInteractors.Clear();
if (trackingLayer.DataSource.GetFeatureCount() <= 0)
return;
trackers.Clear();
trackingLayer.RenderRequired = true;
UpdateMapControlSelection(fireSelectionChangedEvent);
}
private void SynchronizeTrackers()
{
trackers.Clear();
foreach (IFeatureInteractor featureInteractor in SelectedFeatureInteractors)
{
foreach (TrackerFeature trackerFeature in featureInteractor.Trackers)
{
if (featureInteractor.Layer.CoordinateTransformation != null)
{
var trackerFeature2 = (TrackerFeature)trackerFeature.Clone();
trackerFeature2.Geometry = GeometryTransform.TransformGeometry(trackerFeature.Geometry, featureInteractor.Layer.CoordinateTransformation.MathTransform);
trackers.Add(trackerFeature2);
}
else
{
trackers.Add(trackerFeature);
}
}
}
trackingLayer.RenderRequired = true;
}
public IFeatureInteractor GetFeatureInteractor(ILayer layer, IFeature feature)
{
try
{
if (layer.FeatureEditor == null) return null;
return layer.FeatureEditor.CreateInteractor(layer, feature);
}
catch (Exception exception)
{
log.Error("Error creating feature interactor: " + exception.Message);
return null;
}
}
private bool StartSelection(ILayer layer, IFeature feature)
{
var featureInteractor = GetFeatureInteractor(layer, feature);
if (null == featureInteractor) return false;
if (featureInteractor.AllowSingleClickAndMove())
{
// do not yet select, but allow MltiSelect
SelectedFeatureInteractors.Add(featureInteractor);
SynchronizeTrackers();
UpdateMapControlSelection();
return true;
}
return false;
}
public void AddSelection(IEnumerable<IFeature> features)
{
foreach (IFeature feature in features)
{
var layer = Map.GetLayerByFeature(feature);
if (layer == null)
{
throw new ArgumentOutOfRangeException("features", "Can't find layer for feature: " + feature);
}
AddSelection(layer, feature, false);
}
UpdateMapControlSelection();
}
public void AddSelection(ILayer layer, IFeature feature, bool synchronizeUI = true)
{
if (!layer.Visible) return;
var featureInteractor = GetFeatureInteractor(layer, feature);
if (null == featureInteractor) return;
SelectedFeatureInteractors.Add(featureInteractor);
if (synchronizeUI)
{
UpdateMapControlSelection();
}
}
public IEnumerable<IFeature> Selection
{
get { return SelectedFeatureInteractors.Select(interactor => interactor.SourceFeature); }
}
private void RemoveSelection(IFeature feature)
{
for (int i = 0; i < SelectedFeatureInteractors.Count; i++)
{
if (ReferenceEquals(SelectedFeatureInteractors[i].SourceFeature, feature))
{
SelectedFeatureInteractors.RemoveAt(i);
break;
}
}
UpdateMapControlSelection();
}
/// <summary>
/// Sets the selected object in the selectTool. SetSelection supports also the toggling/extending the
/// selected Trackers.
/// </summary>
/// <param name="feature"></param>
/// <param name="featureLayer"></param>
/// <returns>A clone of the original object.</returns>
/// special cases
/// feature is ILineString or IPolygon and trackerIndex != 1 : user clicked an already selected
/// features -> only selected tracker changes.
private void SetSelection(IFeature feature, ILayer featureLayer)
{
if (null != feature)
{
// store selected Trackers
IList<int> featureTrackers = new List<int>();
for (int i = 0; i < TrackingLayer.DataSource.Features.Count; i++)
{
var trackerFeature = (TrackerFeature)TrackingLayer.DataSource.Features[i];
if (ReferenceEquals(trackerFeature, feature))
{
featureTrackers.Add(i);
}
}
// store selected objects
AddSelection(featureLayer, feature);
}
}
private void FocusTracker(TrackerFeature trackFeature)
{
if (null == trackFeature)
return;
if (!((KeyToggleSelection) || (KeyExtendSelection)))
{
foreach (IFeatureInteractor featureInteractor in SelectedFeatureInteractors)
{
foreach (TrackerFeature trackerFeature in featureInteractor.Trackers)
{
featureInteractor.SetTrackerSelection(trackerFeature, false);
}
}
}
foreach (IFeatureInteractor featureInteractor in SelectedFeatureInteractors)
{
foreach (TrackerFeature trackerFeature in featureInteractor.Trackers)
{
if (trackerFeature == trackFeature)
{
if (KeyToggleSelection)
{
featureInteractor.SetTrackerSelection(trackFeature, !trackFeature.Selected);
}
else
{
featureInteractor.SetTrackerSelection(trackFeature, true);
}
}
}
}
}
private readonly List<PointF> selectPoints = new List<PointF>();
private void UpdateMultiSelection(Coordinate worldPosition)
{
if (MultiSelectionMode == MultiSelectionMode.Lasso)
{
selectPoints.Add(Map.WorldToImage(worldPosition));
}
else
{
WORLDPOSITION = worldPosition;
}
}
private IPolygon CreatePolygon(double left, double top, double right, double bottom)
{
var vertices = new List<Coordinate>
{
GeometryFactoryEx.CreateCoordinate(left, bottom),
GeometryFactoryEx.CreateCoordinate(right, bottom),
GeometryFactoryEx.CreateCoordinate(right, top),
GeometryFactoryEx.CreateCoordinate(left, top)
};
vertices.Add((Coordinate)vertices[0].Clone());
ILinearRing newLinearRing = GeometryFactory.CreateLinearRing(vertices.ToArray());
return GeometryFactory.CreatePolygon(newLinearRing, null);
}
private IPolygon CreateSelectionPolygon(Coordinate worldPosition)
{
if (MultiSelectionMode == MultiSelectionMode.Rectangle)
{
if (0 == Math.Abs(mouseDownLocation.X - worldPosition.X))
{
return null;
}
if (0 == Math.Abs(mouseDownLocation.Y - worldPosition.Y))
{
return null;
}
return CreatePolygon(Math.Min(mouseDownLocation.X, worldPosition.X),
Math.Max(mouseDownLocation.Y, worldPosition.Y),
Math.Max(mouseDownLocation.X, worldPosition.X),
Math.Min(mouseDownLocation.Y, worldPosition.Y));
}
var vertices = selectPoints.Select(point => Map.ImageToWorld(point)).ToList();
if (vertices.Count == 1)
{
// too few points to create a polygon
return null;
}
vertices.Add((Coordinate)worldPosition.Clone());
vertices.Add((Coordinate)vertices[0].Clone());
ILinearRing newLinearRing = GeometryFactory.CreateLinearRing(vertices.ToArray());
return GeometryFactory.CreatePolygon(newLinearRing, null);
}
private Coordinate mouseDownLocation; // TODO: remove me
private Coordinate WORLDPOSITION;
public override void OnDraw(Graphics graphics)
{
if (MultiSelectionMode == MultiSelectionMode.Lasso)
{
GraphicsHelper.DrawSelectionLasso(graphics, KeyExtendSelection ? Color.Magenta : Color.DeepSkyBlue, selectPoints.ToArray());
}
else
{
Coordinate coordinate1 = GeometryFactoryEx.CreateCoordinate(mouseDownLocation.X, mouseDownLocation.Y);
Coordinate coordinate2 = GeometryFactoryEx.CreateCoordinate(WORLDPOSITION.X, WORLDPOSITION.Y);
PointF point1 = Map.WorldToImage(coordinate1);
PointF point2 = Map.WorldToImage(coordinate2);
GraphicsHelper.DrawSelectionRectangle(graphics, KeyExtendSelection ? Color.Magenta : Color.DeepSkyBlue, point1, point2);
}
}
public override void OnMouseMove(Coordinate worldPosition, MouseEventArgs e)
{
if (IsMultiSelect)
{
//WORLDPOSITION = worldPosition;
UpdateMultiSelection(worldPosition);
DoDrawing(false);
return;
}
if (MapControl.Tools.Any(t => t != this && t.IsBusy))
return; //don't influence cursor when other tools are busy
Cursor cursor = null;
foreach (IFeatureInteractor featureInteractor in SelectedFeatureInteractors)
{
TrackerFeature trackerFeature = featureInteractor.GetTrackerAtCoordinate(worldPosition);
if (null != trackerFeature)
{
cursor = ((FeatureInteractor)featureInteractor).GetCursor(trackerFeature);
}
}
if (null == cursor)
{
cursor = Cursors.Default;
}
MapControl.Cursor = cursor;
}
private void UpdateMapControlSelection()
{
UpdateMapControlSelection(true);
}
private void UpdateMapControlSelection(bool fireSelectionChangedEvent)
{
SynchronizeTrackers();
IList<IFeature> selectedFeatures = SelectedFeatureInteractors.Select(t => t.SourceFeature).ToList();
MapControl.SelectedFeatures = selectedFeatures;
if (fireSelectionChangedEvent && SelectionChanged != null)
{
SelectionChanged(this, null);
}
}
public override void OnMouseDoubleClick(object sender, MouseEventArgs e)
{
orgMouseDownLocation = null;
}
public override void OnMouseUp(Coordinate worldPosition, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
if (IsMultiSelect)
{
HandleMultiSelectMouseUp(worldPosition);
}
else
{
HandleMouseUp(worldPosition, e);
}
IsBusy = false;
orgClickTime = DateTime.Now;
}
protected virtual void HandleMouseUp(Coordinate worldPosition, MouseEventArgs e)
{
if ((null != orgMouseDownLocation) && (orgMouseDownLocation.X == worldPosition.X) &&
(orgMouseDownLocation.Y == worldPosition.Y) && (e.Button == MouseButtons.Left))
{
// check if mouse was pressed at a selected object without moving the mouse. The default behaviour
// should be to select 'the next' object
TimeSpan timeSpan = DateTime.Now - orgClickTime;
int dc = SystemInformation.DoubleClickTime;
if (dc < timeSpan.TotalMilliseconds)
{
if (1 == SelectedFeatureInteractors.Count)
{
// check if selection exists; could be toggled
Layer outLayer;
IFeature nextFeature = GetNextFeatureAtPosition(worldPosition,
// set limit from 4 to 10: TOOLS-1499
(float)MapHelper.ImageToWorld(Map, 10),
out outLayer,
SelectedFeatureInteractors[0].SourceFeature,
ol => ol.Visible);
if (null != nextFeature)
{
Clear(false);
SetSelection(nextFeature, outLayer); //-1 for ILineString
//MapControl.Refresh();
}
}
}
}
UpdateMapControlSelection(true);
}
/// TODO: note if no features are selected the selection rectangle maintains visible after mouse up
/// ISSUE 2373
private void HandleMultiSelectMouseUp(Coordinate worldPosition)
{
StopMultiSelect();
List<IFeature> selectedFeatures = null;
if (!KeyExtendSelection)
{
selectedFeatures = new List<IFeature>(SelectedFeatureInteractors.Select(fe => fe.SourceFeature).ToArray());
Clear(false);
}
var selectionPolygon = CreateSelectionPolygon(worldPosition);
if (null != selectionPolygon)
{
foreach (ILayer layer in Map.GetAllVisibleLayers(false))
{
//make sure parent layer is selectable or null
var parentLayer = Map.GetGroupLayerContainingLayer(layer);
if ( (parentLayer == null || parentLayer.IsSelectable) && (layer.IsSelectable) && (layer is VectorLayer))
{
// do not use the maptool provider but the datasource of each layer.
var vectorLayer = (VectorLayer)layer;
var multiFeatures = vectorLayer.GetFeatures(selectionPolygon).Take(5000);
foreach (IFeature feature in multiFeatures)
{
if ((null != selectedFeatures) && (selectedFeatures.Contains(feature)))
{
continue;
}
AddSelection(vectorLayer, feature, false);
}
}
}
}
else
{
// if mouse hasn't moved handle as single select. A normal multi select uses the envelope
// of the geometry and this has as result that unwanted features will be selected.
ILayer selectedLayer;
var limit = (float)MapHelper.ImageToWorld(Map, 4);
var nearest = FindNearestFeature(worldPosition, limit, out selectedLayer, ol => ol.Visible);
if (null != nearest)
{
AddSelection(selectedLayer, nearest, false);
}
}
// synchronize with map selection, possible check if selection is already set; do not remove
UpdateMapControlSelection(true);
}
readonly VectorLayer trackingLayer = new VectorLayer(String.Empty);
public override IMapControl MapControl
{
get { return base.MapControl; }
set
{
base.MapControl = value;
trackingLayer.Map = MapControl.Map;
}
}
/// <summary>
/// Selects the given features on the map. Will search all layers for the features when no vector layer is provided
/// </summary>
/// <param name="featuresToSelect">The feature to select on the map.</param>
/// <param name="vectorLayer">The layer on which the features reside.</param>
public bool Select(IEnumerable<IFeature> featuresToSelect, ILayer vectorLayer = null)
{
if (featuresToSelect == null)
{
Clear(true);
return false;
}
Clear(false);
foreach (var feature in featuresToSelect)
{
var foundLayer = vectorLayer ?? Map.GetLayerByFeature(feature);
if (foundLayer != null && foundLayer is VectorLayer)
{
AddSelection(foundLayer, feature, ReferenceEquals(feature, featuresToSelect.Last()));
}
}
return true;
}
/// <summary>
/// Selects the given feature on the map. Will search all layers for the feature.
/// </summary>
/// <param name="featureToSelect">The feature to select on the map.</param>
public bool Select(IFeature featureToSelect)
{
if (null == featureToSelect)
{
Clear(true);
return false;
}
// Find the layer that this feature is on
ILayer foundLayer = MapControl.Map.GetLayerByFeature(featureToSelect);
if (foundLayer != null && foundLayer is VectorLayer)
{
// Select the feature
Select(foundLayer, featureToSelect);
return true;
}
return false;
}
public void Select(ILayer vectorLayer, IFeature feature)
{
if (IsBusy)
{
return;
}
Clear(false);
SetSelection(feature, vectorLayer);
UpdateMapControlSelection(true);
}
/// <summary>
/// Handles changes to the map (or bubbled up from ITheme, ILayer) properties.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public override void OnMapPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var layer = sender as ILayer;
if (layer != null)
{
if (e.PropertyName == "Visible" && !layer.Visible)
{
RefreshSelection();
}
if (e.PropertyName == "Enabled")
{
// If a layer is enabled of disables and features of the layer are selected
// the selection is cleared. Another solution is to remove only features of layer
// from the selection, but this simple and effective.
if (layer is GroupLayer)
{
var layerGroup = (GroupLayer)layer;
foreach (ILayer layerGroupLayer in layerGroup.Layers)
{
HandleLayerStatusChanged(layerGroupLayer);
}
}
else
{
HandleLayerStatusChanged(layer);
}
}
}
}
private void HandleLayerStatusChanged(ILayer layer)
{
if (trackers.Any(trackerFeature => layer == trackerFeature.FeatureInteractor.Layer))
{
Clear();
}
}
public override void OnMapCollectionChanged(object sender, NotifyCollectionChangingEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangeAction.Remove:
{
if(e.Item is ILayer)
{
RefreshSelection();
}
if (sender is Map)
{
var layer = (ILayer)e.Item;
if (layer is GroupLayer)
{
var layerGroup = (GroupLayer)layer;
foreach (ILayer layerGroupLayer in layerGroup.Layers)
{
HandleLayerStatusChanged(layerGroupLayer);
}
}
else
{
HandleLayerStatusChanged(layer);
}
}
break;
}
case NotifyCollectionChangeAction.Replace:
throw new NotImplementedException();
}
}
/// <summary>
/// todo add cancel method to IMapTool
/// todo mousedown clears selection -> complex selection -> start multi select -> cancel -> original selection lost
/// </summary>
public override void Cancel()
{
if (IsBusy)
{
if (IsMultiSelect)
{
StopMultiSelect();
}
IsBusy = false;
}
Clear(true);
}
public event EventHandler SelectionChanged;
public override bool IsActive
{
get
{
return base.IsActive;
}
set
{
base.IsActive = value;
if (false == IsActive)
{
MultiSelectionMode = MultiSelectionMode.Rectangle;
}
}
}