forked from SharpMap/SharpMap.DeltaShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapControl.cs
More file actions
1160 lines (966 loc) · 35 KB
/
MapControl.cs
File metadata and controls
1160 lines (966 loc) · 35 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.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;
using DelftTools.Utils.Aop;
using DelftTools.Utils.Collections;
using DelftTools.Utils.Collections.Generic;
using DelftTools.Utils.Threading;
using GeoAPI.Extensions.Feature;
using GeoAPI.Geometries;
using SharpMap.Api;
using SharpMap.Api.Editors;
using SharpMap.Editors.FallOff;
using log4net;
using SharpMap.Layers;
using SharpMap.Rendering.Thematics;
using SharpMap.Styles;
using SharpMap.UI.Tools;
using SharpMap.UI.Tools.Zooming;
namespace SharpMap.UI.Forms
{
/// <summary>
/// MapControl Class - MapControl control for Windows forms
/// </summary>
[DesignTimeVisible(true)]///, NotifyPropertyChange]
[Serializable]
public class MapControl : Control, IMapControl
{
#region Delegates
/// <summary>
/// MouseEventtype fired from the MapImage control
/// </summary>
/// <param name="worldPos"></param>
/// <param name="imagePos"></param>
public delegate void MouseEventHandler(Coordinate worldPos, MouseEventArgs imagePos);
#endregion
private static readonly ILog Log = LogManager.GetLogger(typeof(MapControl));
private static readonly Color[] MDefaultColors = new[]
{
Color.DarkRed, Color.DarkGreen, Color.DarkBlue,
Color.Orange, Color.Cyan, Color.Black, Color.Purple,
Color.Yellow, Color.LightBlue, Color.Fuchsia
};
private static int mDefaultColorIndex;
// other commonly-used specific tools
private readonly CurvePointTool curvePointTool;
private readonly FixedZoomInTool fixedZoomInTool;
private readonly FixedZoomOutTool fixedZoomOutTool;
private readonly LegendTool legendTool;
private readonly MoveTool linearMoveTool;
private readonly SolidBrush mRectangleBrush = new SolidBrush(Color.FromArgb(210, 244, 244, 244));
private readonly Pen mRectanglePen = new Pen(Color.FromArgb(244, 244, 244), 1);
private readonly MeasureTool measureTool;
private readonly MoveTool moveTool;
private readonly PanZoomTool panZoomTool;
private readonly CoverageProfileTool profileTool;
private readonly QueryTool queryTool;
private readonly ZoomUsingRectangleTool rectangleZoomTool;
private readonly SelectTool selectTool;
private readonly DeleteTool deleteTool;
private readonly SnapTool snapTool;
private readonly EventedList<IMapTool> tools;
private readonly PanZoomUsingMouseWheelTool wheelPanZoomTool;
private readonly ZoomHistoryTool zoomHistoryTool;
// TODO: fieds below should be moved to some more specific tools?
private int mQueryLayerIndex;
private Map map;
private DelayedEventHandler<NotifyCollectionChangingEventArgs> mapCollectionChangedEventHandler;
private DelayedEventHandler<PropertyChangedEventArgs> mapPropertyChangedEventHandler;
private IList<IFeature> selectedFeatures = new List<IFeature>();
private Timer refreshTimer = new Timer() { Interval = 300 };
private Point _lastHoverPostiton;
private Point _cmLocation;
private ContextMenuStrip _cmStrip;
public void WaitUntilAllEventsAreProcessed()
{
while (mapCollectionChangedEventHandler != null && mapPropertyChangedEventHandler != null
&& (mapCollectionChangedEventHandler.HasEventsToProcess || mapCollectionChangedEventHandler.IsRunning
|| mapPropertyChangedEventHandler.HasEventsToProcess || mapPropertyChangedEventHandler.IsRunning))
{
Application.DoEvents();
}
}
/// <summary>
/// Initializes a new map
/// </summary>
public MapControl()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
LostFocus += MapBox_LostFocus;
base.AllowDrop = true;
tools = new EventedList<IMapTool>();
tools.CollectionChanged += tools_CollectionChanged;
var northArrowTool = new NorthArrowTool()
{
Anchor = AnchorStyles.Right | AnchorStyles.Top,
Visible = false
};
Tools.Add(northArrowTool);
var scaleBarTool = new ScaleBarTool()
{
Size = new Size(230, 50),
Anchor = AnchorStyles.Right | AnchorStyles.Bottom,
Visible = true
};
Tools.Add(scaleBarTool);
legendTool = new LegendTool { Anchor = AnchorStyles.Left | AnchorStyles.Top, Visible = false };
Tools.Add(legendTool);
queryTool = new QueryTool();
Tools.Add(queryTool);
// add commonly used tools
zoomHistoryTool = new ZoomHistoryTool();
Tools.Add(zoomHistoryTool);
panZoomTool = new PanZoomTool();
Tools.Add(panZoomTool);
wheelPanZoomTool = new PanZoomUsingMouseWheelTool() { WheelZoomMagnitude = 0.8 };
Tools.Add(wheelPanZoomTool);
rectangleZoomTool = new ZoomUsingRectangleTool();
Tools.Add(rectangleZoomTool);
fixedZoomInTool = new FixedZoomInTool();
Tools.Add(fixedZoomInTool);
fixedZoomOutTool = new FixedZoomOutTool();
Tools.Add(fixedZoomOutTool);
selectTool = new SelectTool { IsActive = true };
Tools.Add(selectTool);
deleteTool = new DeleteTool();
Tools.Add(deleteTool);
moveTool = new MoveTool { Name = "Move selected vertices", FallOffPolicy = FallOffType.None };
Tools.Add(moveTool);
linearMoveTool = new MoveTool
{
Name = "Move selected vertices (linear)",
FallOffPolicy = FallOffType.Linear
};
Tools.Add(linearMoveTool);
measureTool = new MeasureTool();
tools.Add(measureTool);
profileTool = new CoverageProfileTool { Name = "Make grid profile" };
tools.Add(profileTool);
curvePointTool = new CurvePointTool();
Tools.Add(curvePointTool);
snapTool = new SnapTool();
Tools.Add(snapTool);
var toolTipTool = new ToolTipTool();
Tools.Add(toolTipTool);
Width = 100;
Height = 100;
mapPropertyChangedEventHandler =
new DelayedEventHandler<PropertyChangedEventArgs>(map_PropertyChanged_Delayed)
{
SynchronizingObject = this,
FireLastEventOnly = true,
Delay = 300,
Filter = (sender, e) => sender is ILayer ||
sender is VectorStyle ||
sender is ITheme ||
sender is IList<ILayer>,
Enabled = false
};
mapCollectionChangedEventHandler =
new DelayedEventHandler<NotifyCollectionChangingEventArgs>(map_CollectionChanged_Delayed)
{
SynchronizingObject = this,
FireLastEventOnly = true,
Delay = 300,
FullRefreshEventHandler = (sender, e) => OnFullRefresh(sender, e),
Filter = (sender, e) => sender is Map ||
sender is ILayer ||
sender is IList<ILayer>,
Enabled = false
};
Map = new Map(ClientSize) { Zoom = 100 };
}
private void OnFullRefresh(object sender, EventArgs e)
{
SelectTool.RefreshSelection();
}
void RefreshTimerTick(object sender, EventArgs e)
{
if (Map == null)
{
return;
}
if (Visible && !Tools.Any(t => t.IsBusy))
{
if (Map.Layers.Any(l => l.Visible && l.RenderRequired) || Map.RenderRequired)
{
RefreshSelectTool();
Refresh();
}
}
}
private void RefreshSelectTool()
{
if (SelectTool != null)
{
SelectTool.RefreshFeatureInteractors();
}
}
[Description("The color of selecting rectangle.")]
[Category("Appearance")]
public Color SelectionBackColor
{
get { return mRectangleBrush.Color; }
set
{
//if (value != mRectangleBrush.Color)
mRectangleBrush.Color = value;
}
}
[Description("The color of selection rectangle frame.")]
[Category("Appearance")]
public Color SelectionForeColor
{
get { return mRectanglePen.Color; }
set
{
//if (value != mRectanglePen.Color)
mRectanglePen.Color = value;
}
}
/// <summary>
/// Gets or sets the index of the active query layer
/// </summary>
public int QueryLayerIndex
{
get { return mQueryLayerIndex; }
set { mQueryLayerIndex = value; }
}
#region IMapControl Members
[Description("The map image currently visualized.")]
[Category("Appearance")]
public Image Image
{
get
{
if (Map == null || Width <= 0 || Height <= 0)
{
return null;
}
var bitmap = new Bitmap(Width, Height);
DrawToBitmap(bitmap, ClientRectangle);
return bitmap;
}
}
public override Color BackColor
{
get { return base.BackColor; }
set
{
base.BackColor = value;
if (Map != null)
{
Map.BackColor = value;
}
}
}
/// <summary>
/// Sets whether the mapcontrol should automatically grab focus when mouse is hovering the control
/// </summary>
[Description("Sets whether the mapcontrol should automatically grab focus when mouse is hovering the control")]
[DefaultValue(true)]
[Category("Behavior")]
public bool TakeFocusOnHover { get; set; } = true;
/// <summary>
/// Map reference
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Map Map
{
get { return map; }
set
{
if (map != null)
{
//unsubscribe from changes in the map layercollection
UnSubscribeMapEvents();
map.ClearImage();
if (refreshTimer != null)
{
refreshTimer.Stop();
refreshTimer.Tick -= RefreshTimerTick;
}
}
map = value;
if (map == null)
{
return;
}
map.Size = ClientSize;
SubScribeMapEvents();
SetMapStyles();
Refresh();
if (Visible)
{
if (refreshTimer != null)
{
refreshTimer.Tick += RefreshTimerTick;
if (Visible)
{
refreshTimer.Start();
}
}
}
}
}
private void SetMapStyles()
{
DoubleBuffered = true;
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
private void UnSubscribeMapEvents()
{
map.CollectionChanged -= mapCollectionChangedEventHandler;
((INotifyPropertyChanged)map).PropertyChanged -= mapPropertyChangedEventHandler;
map.MapRendered -= OnMapRendered;
map.MapLayerRendered -= OnMapLayerRendered;
}
private void SubScribeMapEvents()
{
map.CollectionChanged += mapCollectionChangedEventHandler;
((INotifyPropertyChanged)map).PropertyChanged += mapPropertyChangedEventHandler;
map.MapRendered += OnMapRendered;
map.MapLayerRendered += OnMapLayerRendered;
}
private void OnMapLayerRendered(Graphics g, ILayer layer)
{
foreach (var tool in tools.Where(tool => tool.IsActive))
{
tool.OnMapLayerRendered(g, layer);
}
}
public IList<IMapTool> Tools
{
get { return tools; }
}
public IMapTool GetToolByName(string toolName)
{
return Tools.FirstOrDefault(tool => tool.Name == toolName);
// Do not throw ArgumentOutOfRangeException UI handlers (button checked) can ask for not existing tool
}
public IMapTool GetToolByType(Type type)
{
foreach (var tool in Tools.Where(tool => tool.GetType() == type))
{
return tool;
}
throw new ArgumentOutOfRangeException(type.ToString());
}
public T GetToolByType<T>() where T : class
{
//change it to support interfaces..
return Tools.Where(tool => tool is T).Cast<T>().FirstOrDefault();
}
public void ActivateTool(IMapTool tool)
{
if (tool == null || tool.IsActive)
{
return;
}
if (tool.AlwaysActive)
{
throw new InvalidOperationException("Tool is AlwaysActive, use IMapTool.Execute() to make it work");
}
// deactivate other tools
foreach (var t in tools.Where(t => t.IsActive && !t.AlwaysActive))
{
t.IsActive = false;
}
tool.IsActive = true;
}
public QueryTool QueryTool
{
get { return queryTool; }
}
public ZoomHistoryTool ZoomHistoryTool
{
get { return zoomHistoryTool; }
}
public PanZoomTool PanZoomTool
{
get { return panZoomTool; }
}
public PanZoomUsingMouseWheelTool WheelPanZoomTool
{
get { return wheelPanZoomTool; }
}
public ZoomUsingRectangleTool RectangleZoomTool
{
get { return rectangleZoomTool; }
}
public FixedZoomInTool FixedZoomInTool
{
get { return fixedZoomInTool; }
}
public FixedZoomOutTool FixedZoomOutTool
{
get { return fixedZoomOutTool; }
}
public MoveTool MoveTool
{
get { return moveTool; }
}
public MoveTool LinearMoveTool
{
get { return linearMoveTool; }
}
public SelectTool SelectTool
{
get { return selectTool; }
}
public DeleteTool DeleteTool
{
get { return deleteTool; }
}
public CoverageProfileTool CoverageProfileTool
{
get { return profileTool; }
}
public LegendTool LegendTool
{
get { return legendTool; }
}
public SnapTool SnapTool
{
get { return snapTool; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IEnumerable<IFeature> SelectedFeatures
{
get { return selectedFeatures; }
set
{
selectedFeatures = value.ToList();
FireSelectedFeaturesChanged();
if (Visible)
{
Refresh();
}
}
}
private void FireSelectedFeaturesChanged()
{
if (SelectedFeaturesChanged != null)
{
SelectedFeaturesChanged(this, EventArgs.Empty);
}
}
public event EventHandler SelectedFeaturesChanged;
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
mapPropertyChangedEventHandler.Enabled = true;
mapCollectionChangedEventHandler.Enabled = true;
}
protected override void OnHandleDestroyed(EventArgs e)
{
mapPropertyChangedEventHandler.Enabled = false;
mapCollectionChangedEventHandler.Enabled = false;
if (refreshTimer != null)
{
refreshTimer.Tick -= RefreshTimerTick;
refreshTimer.Stop();
refreshTimer.Dispose();
refreshTimer = null;
}
base.OnHandleDestroyed(e);
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (disposingActive)
return;
if (refreshTimer == null)
{
refreshTimer = new Timer() { Interval = 300 };
}
if (Visible)
{
refreshTimer.Stop();
refreshTimer.Tick -= RefreshTimerTick;
refreshTimer.Tick += RefreshTimerTick;
refreshTimer.Start();
}
else
{
refreshTimer.Stop();
refreshTimer.Tick -= RefreshTimerTick;
}
}
/// <summary>
/// Refreshes the map
/// </summary>
[InvokeRequired]
public override void Refresh()
{
if (!DelayedEventHandlerController.FireEvents || disposed)
{
return; //no refreshing..bleh
}
if (refreshTimer != null)
{
refreshTimer.Stop();
}
try
{
if (map == null)
{
return;
}
map.Render();
base.Refresh();
// log.DebugFormat("Refreshed");
if (MapRefreshed != null)
{
MapRefreshed(this, null);
}
}
finally
{
if (Visible)
{
if (refreshTimer != null)
{
refreshTimer.Start();
}
}
}
}
public bool IsProcessing
{
get
{
var processingPropertyChangedEvents = mapPropertyChangedEventHandler != null &&
(mapPropertyChangedEventHandler.IsRunning || mapPropertyChangedEventHandler.HasEventsToProcess);
var processingCollectionChangedEvents = mapCollectionChangedEventHandler != null &&
(mapCollectionChangedEventHandler.IsRunning || mapCollectionChangedEventHandler.HasEventsToProcess);
return processingPropertyChangedEvents || processingCollectionChangedEvents;
}
}
#endregion
private void tools_CollectionChanged(object sender, NotifyCollectionChangingEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangeAction.Add:
((IMapTool)e.Item).MapControl = this;
break;
case NotifyCollectionChangeAction.Remove:
((IMapTool)e.Item).MapControl = null;
break;
default:
break;
}
}
/*
private void MapMapLayerRendered(Graphics g, ILayer layer)
{
foreach (var tool in tools.Where(tool => tool.IsActive))
{
tool.OnMapLayerRendered(g, layer);
}
}
*/
private void OnMapRendered(Graphics g)
{
// TODO: review, migrated from GeometryEditor
if (g == null)
{
return;
}
//UserLayer.Render(g, this.mapbox.Map);
// always draw Trackers when they exist -> full redraw when Trackers are deleted
SelectTool.Render(g, Map);
zoomHistoryTool.MapRendered(Map);
}
private void map_PropertyChanged_Delayed(object sender, PropertyChangedEventArgs e)
{
if (IsDisposed || !IsHandleCreated) // must be called before InvokeRequired
{
return;
}
//Log.DebugFormat("IsDisposed: {0}, IsHandleCreated: {1}, Disposing: {2}", IsDisposed, IsHandleCreated, Disposing);
map_PropertyChanged(sender, e);
}
[InvokeRequired]
private void map_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (IsDisposed)
{
return;
}
if (sender is ILayer && e.PropertyName == "Map")
{
return; // performance optimization, avoid double rendering
}
foreach (var tool in tools.ToArray())
{
tool.OnMapPropertyChanged(sender, e); // might be a problem, events are skipped
}
if (Visible)
{
Refresh();
}
else
{
map.Layers.ForEach(l => { if (!l.RenderRequired) l.RenderRequired = true; });
}
}
private void map_CollectionChanged_Delayed(object sender, NotifyCollectionChangingEventArgs e)
{
if (IsDisposed || !IsHandleCreated) // must be called before InvokeRequired
{
return;
}
map_CollectionChanged(sender, e);
}
[InvokeRequired]
private void map_CollectionChanged(object sender, NotifyCollectionChangingEventArgs e)
{
if (IsDisposed)
{
return;
}
// hack: some tools add extra tools and can remove them in response to a layer
// change. For example NetworkEditorMapTool adds NewLineTool for NetworkMapLayer
foreach (var tool in tools.ToArray().Where(tool => tools.Contains(tool)))
{
tool.OnMapCollectionChanged(sender, e);
}
var layer = e.Item as ILayer;
if (layer == null)
{
return;
}
if (Map == null)
{
return; // may happen in multi-threaded environment
}
switch (e.Action)
{
case NotifyCollectionChangeAction.Add:
var allLayersWereEmpty = Map.Layers.Except(new[] { layer }).All(l => l.Envelope != null && l.Envelope.IsNull);
if (allLayersWereEmpty && layer.Envelope != null && !layer.Envelope.IsNull)
{
map.ZoomToExtents(); //HACK: OOPS, changing domain model from separate thread!
}
break;
case NotifyCollectionChangeAction.Replace:
throw new NotImplementedException();
}
Refresh();
}
public static void RandomizeLayerColors(VectorLayer layer)
{
layer.Style.EnableOutline = true;
layer.Style.Fill =
new SolidBrush(Color.FromArgb(80, MDefaultColors[mDefaultColorIndex % MDefaultColors.Length]));
layer.Style.Outline =
new Pen(
Color.FromArgb(100,
MDefaultColors[
(mDefaultColorIndex + ((int)(MDefaultColors.Length * 0.5))) %
MDefaultColors.Length]), 1f);
mDefaultColorIndex++;
}
// TODO: add smart resize here, probably can cache some area around map
protected override void OnResize(EventArgs e)
{
if (map != null && ClientSize.Width > 0 && ClientSize.Height > 0)
{
//log.DebugFormat("Resizing map '{0}' from {1} to {2}: ", map.Name, map.Size, ClientSize);
map.Size = ClientSize;
map.Layers.ForEach(l => l.RenderRequired = true);
}
base.OnResize(e);
}
private void MapBox_LostFocus(object sender, EventArgs e)
{
}
// TODO handle arrow keys. MapTool should handle key
protected override bool ProcessDialogKey(Keys keyData)
{
switch (keyData)
{
case Keys.Down:
break;
case Keys.Up:
break;
case Keys.Left:
break;
case Keys.Right:
break;
default:
break;
}
return base.ProcessDialogKey(keyData);
}
/// <summary>
/// Handles the key pressed by the user
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
var shouldRefresh = false;
// cache list of tools (it can change during execute of OnKeyDown)
var toolsList = tools.ToList();
foreach (var tool in toolsList)
{
if (e.KeyCode == Keys.Escape)
{
// if the user presses the escape key first cancel an operation in progress
if (tool.IsBusy)
{
tool.Cancel();
shouldRefresh = true;
}
continue;
}
tool.OnKeyDown(e);
}
if ((!toolsList.Any(t => t.IsBusy)) && (e.KeyCode == Keys.Escape) && (!SelectTool.IsActive))
{
// if the user presses the escape key and there was no operation in progress switch to select.
ActivateTool(SelectTool);
shouldRefresh = true;
}
if (shouldRefresh)
{
Refresh();
}
}
protected override void OnKeyUp(KeyEventArgs e)
{
WithActiveToolsDo(tool => tool.OnKeyUp(e));
base.OnKeyUp(e);
}
/// <summary>
/// Private method to check if we need to reenable the <see cref="Control.MouseHover"/> event.
/// </summary>
/// <param name="position">The current position of the cursor</param>
private void CheckEnableHover(Point position)
{
var delta = new Size(position.X - _lastHoverPostiton.X,
position.Y - _lastHoverPostiton.Y);
if (Math.Abs(delta.Width) > SystemInformation.MouseHoverSize.Width ||
Math.Abs(delta.Height) > SystemInformation.MouseHoverSize.Height)
{
ResetMouseEventArgs();
}
}
protected override void OnMouseHover(EventArgs e)
{
base.OnMouseHover(e);
if (TakeFocusOnHover)
TestAndGrabFocus();
var location = PointToClient(MousePosition);
Debug.WriteLine($"S:{MousePosition} M:{location}");
var worldPosition = map.ImageToWorld(location);
WithActiveToolsDo(tool => tool.OnMouseHover(worldPosition, e));
_lastHoverPostiton = location;
}
private void TestAndGrabFocus()
{
if (!Focused)
{
var isFocused = Focus();
//_.Debug("Focused: " + isFocused);
}
}
private void WithActiveToolsDo(Action<IMapTool> mapToolAction)
{
var activeTools = tools.Where(tool => tool.IsActive).ToList();
foreach (var tool in activeTools)
{
mapToolAction(tool);
}
}
protected override void OnMouseDoubleClick(MouseEventArgs e)
{
if (map == null)
{
return;
}
WithActiveToolsDo(tool => tool.OnMouseDoubleClick(this, e));
// todo (TOOLS-1151) move implemention in mapView_MouseDoubleClick to SelectTool::OnMouseDoubleClick?
if (SelectTool.IsActive)
{
base.OnMouseDoubleClick(e);
}
}
protected override void OnMouseWheel(MouseEventArgs e)
{
if (map == null)
{
return;
}
// sometimes map control is focused even when mouse is outside - then skip it
if (e.X < 0 || e.Y < 0 || e.X > Width || e.Y > Height)
{
return;
}
var mousePosition = map.ImageToWorld(new Point(e.X, e.Y));
if (_cmStrip?.Visible ?? false)
{
_cmStrip.Close();
_cmStrip = null;
}
WithActiveToolsDo(tool => tool.OnMouseWheel(mousePosition, e));
base.OnMouseWheel(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (map == null || !Visible || disposingActive)
{
return;
}
var worldPosition = map.ImageToWorld(new Point(e.X, e.Y));
if (_cmStrip != null)
{
if (Math.Abs(_cmLocation.X - e.X) > 5 || Math.Abs(_cmLocation.Y - e.Y) > 5)
{
_cmLocation = Point.Empty;
_cmStrip.Close();
_cmStrip = null;