-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlotBox.cs
1961 lines (1723 loc) · 77.6 KB
/
PlotBox.cs
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;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace PlotReader
{
/// <summary>
/// Summary description for PlotBox.
/// </summary>
public class PlotBox : System.Windows.Forms.UserControl, PlotReader.IThumbnailOwner
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
protected class AxisPoint
{
public bool HasImageCoordinate, HasValue;
public PointF Point;
public double Value, NonLogValue;
public AxisPoint()
{
HasImageCoordinate = false;
HasValue = false;
}
}
protected AxisPoint[,] m_arrAxesPoints;
public enum PlotBoxAxisType
{
X, Y, None
}
public class AxisWarning
{
public PlotBoxAxisType Axis;
public int PointNumber;
public enum WarningType
{
CoordinateNotSet,
CoordinatesIdentical,
AxesParallel,
ValueNotSet,
ValuesIdentical,
ValueNonPositive
}
public WarningType Warning;
public AxisWarning(PlotBoxAxisType axis, int iNr, WarningType warning)
{
Axis = axis;
PointNumber = iNr;
Warning = warning;
}
}
protected ArrayList m_listAxesWarnings;
public ArrayList AxesWarnings
{
get
{
return m_listAxesWarnings;
}
}
public PlotBox()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// No image.
this.m_objImage = null;
// Set styles.
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint,
true);
this.UpdateStyles();
// No points (empty list)
this.m_listPoints = new ArrayList();
this.m_listPointSel = new ArrayList();
// No warnings for axes (empty list)
this.m_listAxesWarnings = new ArrayList();
// Initialize image-screen transformation matrices (default to identity)
this.m_matTransform = new System.Drawing.Drawing2D.Matrix();
this.m_matTransformInverse = new System.Drawing.Drawing2D.Matrix();
// Default display: 100 % zoom, 0 degrees rotation
this.m_iZoom = 100;
this.m_bZoomToFit = false;
this.m_flAngle = 0;
// Default marker
this.m_iMarkerSize = 7;
this.m_iMarkerType = PlotBox.PlotBoxMarkerType.Dot;
// No axes
this.m_arrAxesLogScale = new bool[2];
this.ClearAxes();
// Default response to click: none.
this.m_MouseState = PlotBoxMouseState.Drag;
// Allocate space for colors.
this.m_arrColors = new Color[4];
// Not being dragged by mouse.
this.m_bMoving = false;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// PlotBox
//
this.AutoScroll = true;
this.Cursor = System.Windows.Forms.Cursors.Cross;
this.Name = "PlotBox";
this.Size = new System.Drawing.Size(560, 432);
this.MouseLeave += new System.EventHandler(this.PlotBox_MouseLeave);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PlotBox_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PlotBox_MouseDown);
this.Resize += new System.EventHandler(this.PlotBox_Resize);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PlotBox_MouseUp);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.PlotBox_KeyDown);
this.ResumeLayout(false);
}
#endregion
protected void InvalidateMarkerScreen(Point pScreen)
{
int iHalf = (m_iMarkerSize - 1) / 2 + 1;
this.Invalidate(new Rectangle(pScreen.X - iHalf, pScreen.Y - iHalf, m_iMarkerSize + 2, m_iMarkerSize + 2));
}
protected int m_iMarkerSize;
public int MarkerSize
{
get
{
return m_iMarkerSize;
}
set
{
m_iMarkerSize = value;
this.Invalidate();
}
}
protected PlotBoxMarkerType m_iMarkerType;
public PlotBoxMarkerType MarkerType
{
get
{
return m_iMarkerType;
}
set
{
m_iMarkerType = value;
this.Invalidate();
}
}
protected void InvalidateMarkerImage(PointF pImage)
{
this.InvalidateMarkerScreen(Point.Round(this.ImageToScreen(pImage)));
}
protected void InvalidateRectangeImage(RectangleF rectImage)
{
PointF topleft = this.ImageToScreen(rectImage.Left, rectImage.Top);
PointF topright = this.ImageToScreen(rectImage.Right, rectImage.Top);
PointF botleft = this.ImageToScreen(rectImage.Left, rectImage.Bottom);
PointF botright = this.ImageToScreen(rectImage.Right, rectImage.Bottom);
int minX = (int)Math.Floor(Math.Min(Math.Min(topleft.X, topright.X), Math.Min(botleft.X, botright.X)));
int maxX = (int)Math.Ceiling(Math.Max(Math.Max(topleft.X, topright.X), Math.Max(botleft.X, botright.X)));
int minY = (int)Math.Floor(Math.Min(Math.Min(topleft.Y, topright.Y), Math.Min(botleft.Y, botright.Y)));
int maxY = (int)Math.Ceiling(Math.Max(Math.Max(topleft.Y, topright.Y), Math.Max(botleft.Y, botright.Y)));
this.Invalidate(new Rectangle(minX, minY, maxX - minX, maxY - minY));
}
protected void DrawMarkerScreen(Graphics g, Point pScreen, Color color, bool bSelected)
{
int iHalf = (this.m_iMarkerSize - 1) / 2;
switch (this.m_iMarkerType)
{
case PlotBox.PlotBoxMarkerType.Cross:
Pen pen2 = new Pen(color, 1);
g.DrawLine(pen2, pScreen.X, pScreen.Y - iHalf, pScreen.X, pScreen.Y + iHalf);
g.DrawLine(pen2, pScreen.X - iHalf, pScreen.Y, pScreen.X + iHalf, pScreen.Y);
break;
case PlotBox.PlotBoxMarkerType.FatCross:
Pen pen3 = new Pen(color, 2);
g.DrawLine(pen3, pScreen.X, pScreen.Y - iHalf, pScreen.X, pScreen.Y + iHalf);
g.DrawLine(pen3, pScreen.X - iHalf, pScreen.Y, pScreen.X + iHalf, pScreen.Y);
break;
default:
// Dot (default)
g.FillEllipse(new SolidBrush(color), pScreen.X - iHalf, pScreen.Y - iHalf, m_iMarkerSize, m_iMarkerSize);
g.DrawEllipse(Pens.Black, pScreen.X - iHalf, pScreen.Y - iHalf, m_iMarkerSize, m_iMarkerSize);
break;
}
}
protected void DrawMarkerImage(Graphics g, PointF pImage, Color color, bool bSelected)
{
this.DrawMarkerScreen(g, Point.Round(this.ImageToScreen(pImage)), color, bSelected);
}
protected System.Drawing.Drawing2D.Matrix m_matTransform, m_matTransformInverse;
protected float m_flAngle;
protected void UpdateTransform(bool bImageChanged)
{
// Save old transformation matrix
Matrix matOld = this.m_matTransform.Clone();
// Reset transformation matrix to identity matrix.
this.m_matTransform.Reset();
// Variables for x- and y-range.
float width = 0, height = 0;
if (this.m_objImage != null)
{
width = this.m_objImage.Width;
height = this.m_objImage.Height;
// Check if we need to do rotation.
if (this.m_flAngle != 0F)
{
// Apply requested rotation to transformation matrix.
this.m_matTransform.Rotate(this.m_flAngle, MatrixOrder.Append);
// Find the transformed position of each image corner.
PointF[] corners = {new PointF(0,0),
new PointF(this.m_objImage.Width,0),
new PointF(0,this.m_objImage.Height),
new PointF(this.m_objImage.Width,this.m_objImage.Height)};
this.m_matTransform.TransformPoints(corners);
// Get new x- and y-range from image corner positions.
float xmin = 0, xmax = 0, ymin = 0, ymax = 0;
foreach (PointF p in corners)
{
if (p.X < xmin) xmin = p.X;
if (p.X > xmax) xmax = p.X;
if (p.Y < ymin) ymin = p.Y;
if (p.Y > ymax) ymax = p.Y;
}
width = xmax - xmin;
height = ymax - ymin;
// Shift to ensure (0,0) lies at the minimum x and y.
this.m_matTransform.Translate(-xmin, -ymin, MatrixOrder.Append);
}
// Apply requested scaling to transformation matrix.
float flZoom = ((float)this.m_iZoom) / 100.0F;
if (this.m_bZoomToFit)
{
float xscale = (float)(this.ClientSize.Width) / width;
float yscale = (float)(this.ClientSize.Height) / height;
if (xscale > 0 && yscale > 0)
{
flZoom = Math.Min(xscale, yscale);
this.m_iZoom = (int)Math.Round(100 * flZoom);
}
}
this.m_matTransform.Scale(flZoom, flZoom, MatrixOrder.Append);
width *= flZoom;
height *= flZoom;
}
if (!this.m_matTransform.Equals(matOld))
{
// Get inverse tranformation matrix (for screen-to-image)
this.m_matTransformInverse = this.m_matTransform.Clone();
this.m_matTransformInverse.Invert();
}
if (!this.m_matTransform.Equals(matOld) || bImageChanged)
{
// Use x- and y-range to set the scrollable area.
this.m_sizeCurrent = new SizeF(width, height);
if (!this.m_bZoomToFit) this.AutoScrollMinSize = Size.Round(this.m_sizeCurrent);
// Update display
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.m_objImage != null)
{
// Anti-alias edges of image, and vector drawings.
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
// Draw image
e.Graphics.PageUnit = GraphicsUnit.Pixel;
e.Graphics.Transform = this.m_matTransform.Clone();
e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y, System.Drawing.Drawing2D.MatrixOrder.Append);
Rectangle rectImage = new Rectangle(0, 0, m_objImage.Width, m_objImage.Height);
e.Graphics.DrawImage(this.m_objImage, rectImage, rectImage, GraphicsUnit.Pixel);
if (this.m_bmOverlay != null) e.Graphics.DrawImage(this.m_bmOverlay.bitmap, this.m_bmOverlay.offset);
e.Graphics.ResetTransform();
if (this.ShownAreaChanged != null) this.ShownAreaChanged(this, null);
// Plot axes.
if (this.AxesState > 0)
{
Pen objPenX = new Pen(this.m_arrColors[2], 1);
Pen objPenY = new Pen(this.m_arrColors[3], 1);
this.DrawAxis(e.Graphics, objPenX, 0);
this.DrawAxis(e.Graphics, objPenY, 1);
}
// Disable anti-aliasing for markers.
e.Graphics.SmoothingMode = SmoothingMode.None;
// Plot specified x-axis points
if (this.m_arrAxesPoints[0, 0].HasImageCoordinate) this.DrawMarkerImage(e.Graphics, this.m_arrAxesPoints[0, 0].Point, this.m_arrColors[2], false);
if (this.m_arrAxesPoints[0, 1].HasImageCoordinate) this.DrawMarkerImage(e.Graphics, this.m_arrAxesPoints[0, 1].Point, this.m_arrColors[2], false);
// Plot specified y-axis points
if (this.m_arrAxesPoints[1, 0].HasImageCoordinate) this.DrawMarkerImage(e.Graphics, this.m_arrAxesPoints[1, 0].Point, this.m_arrColors[3], false);
if (this.m_arrAxesPoints[1, 1].HasImageCoordinate) this.DrawMarkerImage(e.Graphics, this.m_arrAxesPoints[1, 1].Point, this.m_arrColors[3], false);
// Plot data points.
int iPoint = 0;
foreach (PointF point in this.m_listPoints)
{
if ((bool)(this.m_listPointSel[iPoint]))
{
this.DrawMarkerImage(e.Graphics, point, this.m_arrColors[1], true);
}
else
{
this.DrawMarkerImage(e.Graphics, point, this.m_arrColors[0], false);
}
iPoint++;
}
if (!this.m_pointTemp.IsEmpty) this.DrawMarkerImage(e.Graphics, this.m_pointTemp, this.m_arrColors[0], false);
if (this.m_bCaretSelect)
{
Pen p = new Pen(Brushes.White, 1);
p.DashPattern = new float[] { 4F, 4F };
RectangleF caret = this.GetScreenCaret();
e.Graphics.DrawRectangle(Pens.Black, caret.Left, caret.Top, caret.Width, caret.Height);
e.Graphics.DrawRectangle(p, caret.Left, caret.Top, caret.Width, caret.Height);
}
}
}
protected Image m_objImage;
public Image Image
{
get
{
return m_objImage;
}
set
{
this.m_objImage = value;
// Reset the axes.
this.ClearAxes();
// Remove any exiting magic wand object attached to the image.
this.m_magicWand = null;
// Remove all current points.
this.m_listPoints.Clear();
this.m_listPointSel.Clear();
// Zoom at 100 %, rotation at 0.
this.m_iZoom = 100;
this.m_flAngle = 0F;
this.m_bZoomToFit = false;
this.AutoScroll = true;
// Update the transformation matrix.
this.UpdateTransform(true);
// Scroll position at (0,0)
this.AutoScrollPosition = new Point(0, 0);
// If we have any thumbnail control coupled, inform it.
if (this.ImageChanged != null) this.ImageChanged(this, null);
}
}
public void ClearPoints()
{
this.m_listPoints.Clear();
this.m_listPointSel.Clear();
this.Invalidate();
}
protected int m_iZoom;
protected ArrayList m_listPoints, m_listPointSel;
public ArrayList Points
{
get
{
return m_listPoints;
}
}
private void PlotBox_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (this.m_bMoving)
{
this.m_bMoving = false;
}
if (this.m_bCaretSelect && e.Button == MouseButtons.Left)
{
this.m_bCaretSelect = false;
InvalidateCaret();
RectangleF car = this.GetScreenCaret();
int i = 0;
bool bSelectionChanged = false;
foreach (PointF p in this.m_listPoints)
{
PointF sc = this.ImageToScreen(p);
if (car.Contains(sc))
{
this.InvalidateMarkerImage(p);
this.m_listPointSel[i] = !((bool)(this.m_listPointSel[i]));
bSelectionChanged = true;
}
i++;
}
if (bSelectionChanged && this.SelectionChanged != null) this.SelectionChanged(this, null);
}
if (!this.m_pointTemp.IsEmpty && e.Button == MouseButtons.Left)
{
// Add point (not selected by default)
this.m_listPoints.Add(this.m_pointTemp);
this.m_listPointSel.Add(false);
// Tell others interested we have a new point.
if (this.PointAdded != null) this.PointAdded(this, null);
// Reset temporary point.
this.m_pointTemp = new PointF();
}
if (this.m_bmOverlay != null && e.Button == MouseButtons.Left)
{
Rectangle rectOverlay = new Rectangle(this.m_bmOverlay.offset, this.m_bmOverlay.bitmap.Size);
this.m_bmOverlay = null;
this.InvalidateRectangeImage(rectOverlay);
}
if (this.StatusTextChanged != null) this.StatusTextChanged(this, new EventArgs());
}
protected RectangleF GetScreenCaret()
{
PointF[] p = new PointF[] { this.ImageToScreen(this.m_pointCaretStart), this.ImageToScreen(this.m_pointCaretStop) };
PointF topleft = new PointF(Math.Min(p[0].X, p[1].X), Math.Min(p[0].Y, p[1].Y));
SizeF size = new SizeF(Math.Abs(p[0].X - p[1].X), Math.Abs(p[0].Y - p[1].Y));
return new RectangleF(topleft, size);
}
protected void InvalidateCaret()
{
Rectangle car = Rectangle.Round(this.GetScreenCaret());
car.Inflate(4, 4);
car.Offset(-2, -2);
this.Invalidate(car);
}
public event EventHandler PointAdded;
public event EventHandler StatusTextChanged;
public event EventHandler SelectionChanged;
public event EventHandler ZoomChanged;
public event EventHandler AxisCoordinateSet;
protected bool m_bMoving;
protected Point m_pointMoveStart, m_pointScrollPosMoveStart;
protected AbstractMagicWand.Selection.Overlay m_bmOverlay = null;
protected PointF m_pointTemp;
AbstractMagicWand m_magicWand;
private void PlotBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Find the click position in image coordinates.
PointF pointImage = this.ScreenToImage(e.X, e.Y);
if (this.m_iExpCoordinateAxis != 0 && this.m_iExpCoordinateNr != 0 && e.Button == MouseButtons.Left)
{
// If we're not inside the image, do nothing.
if (!this.IsPointInImage(pointImage)) return;
// Get info on the point we should be dealing with.
AxisPoint point = this.m_arrAxesPoints[this.m_iExpCoordinateAxis - 1, this.m_iExpCoordinateNr - 1];
// If we had a previous point, invalidate it so its area will be redrawn.
if (point.HasImageCoordinate) this.InvalidateMarkerImage(point.Point);
// Store the new calibration point.
point.Point = pointImage;
point.HasImageCoordinate = true;
// Invalidate the area of the new point, so it will be drawn.
this.InvalidateMarkerImage(pointImage);
// Stop waiting for new calibration point (we now got it).
this.StopExpectAxisCoordinate();
// Update the axes using our new calibration point.
this.UpdateAxes();
// Notify others we have a new axis coordinate
if (this.AxisCoordinateSet != null) this.AxisCoordinateSet(this, new EventArgs());
}
else if ((this.m_MouseState == PlotBoxMouseState.Drag || e.Button == MouseButtons.Right) && !this.m_bZoomToFit)
{
this.m_pointMoveStart = new Point(e.X, e.Y);
this.m_pointScrollPosMoveStart = new Point(-this.AutoScrollPosition.X, -this.AutoScrollPosition.Y);
this.m_bMoving = true;
}
else if (this.m_MouseState == PlotBoxMouseState.Add && e.Button == MouseButtons.Left)
{
// If we're not inside the image, do nothing.
if (!this.IsPointInImage(pointImage)) return;
if (this.m_bUseMagicWand)
{
if (this.m_magicWand == null) this.m_magicWand = new MagicWandPixel(this.Image);
// Use magic wand to select region of similar points.
AbstractMagicWand.Selection sel = this.m_magicWand.Select(Point.Round(pointImage), this.m_flMagicWandTolerance);
// Replace clicked point by center point in magic wand selected region.
pointImage = sel.GetMeanPoint();
// Retrieve overlay bitmap that shows magic wand selected region.
this.m_bmOverlay = sel.GetOverlay(this.m_arrColors[0], 127);
this.InvalidateRectangeImage(new Rectangle(this.m_bmOverlay.offset, this.m_bmOverlay.bitmap.Size));
}
this.m_pointTemp = pointImage;
// Invalidate the area of the new point, so it will be drawn.
this.InvalidateMarkerImage(pointImage);
}
else if (this.m_MouseState == PlotBoxMouseState.Select && e.Button == MouseButtons.Left)
{
int iPoint = 0;
int iPointClicked = -1;
int iHalf = (m_iMarkerSize - 1) / 2;
// Find point to select.
foreach (PointF p in this.m_listPoints)
{
// Get the data point in image and screen coordinates.
PointF sc = this.ImageToScreen(p);
// Check if the current point has been clicked; stop enumerating if so.
if (e.X >= sc.X - iHalf && e.Y >= sc.Y - iHalf && e.X <= sc.X + iHalf && e.Y <= sc.Y + iHalf)
{
iPointClicked = iPoint;
break;
}
// Increase point index.
iPoint++;
}
// Update point selection
iPoint = 0;
bool bSelectionChanged = false;
foreach (PointF p in this.m_listPoints)
{
if (iPoint == iPointClicked)
{
// Point was clicked.
if ((Control.ModifierKeys & (Keys.Shift | Keys.Control)) != Keys.None)
{
// Control and/or shift were pressed.
this.m_listPointSel[iPoint] = !((bool)(this.m_listPointSel[iPoint]));
this.InvalidateMarkerImage(p);
bSelectionChanged = true;
}
else if (!((bool)(this.m_listPointSel[iPoint])))
{
// No control/shift keys were pressed, and point was previously unselected.
this.m_listPointSel[iPoint] = true;
this.InvalidateMarkerImage(p);
bSelectionChanged = true;
}
}
else
{
// Point was not clicked.
if (((bool)(this.m_listPointSel[iPoint])) && ((Control.ModifierKeys & Keys.Shift) == Keys.None)
&& !(iPointClicked != -1 && (Control.ModifierKeys & Keys.Control) != Keys.None))
{
// Point was not clicked, and
// (1) shift was not pressed
// (2) control was (a) not pressed, or (b) was pressed without a point being clicked.
this.m_listPointSel[iPoint] = false;
this.InvalidateMarkerImage(p);
bSelectionChanged = true;
}
}
iPoint++;
}
// If the selection changed, tell those interested.
if (bSelectionChanged && this.SelectionChanged != null) this.SelectionChanged(this, null);
// If we did not find a point to select, start caret-selection mode.
if (iPointClicked == -1)
{
this.m_bCaretSelect = true;
this.m_pointCaretStart = this.ScreenToImage(e.X, e.Y);
this.m_pointCaretStop = this.m_pointCaretStart;
}
}
if (this.StatusTextChanged != null) this.StatusTextChanged(this, new EventArgs());
}
public int Zoom
{
get
{
return this.m_iZoom;
}
set
{
if ((value > 0 && this.m_iZoom != value) || this.m_bZoomToFit)
{
PointF p = this.ScreenToImage(this.ClientSize.Width / 2F, this.ClientSize.Height / 2F);
this.m_bZoomToFit = false;
this.AutoScroll = true;
this.m_iZoom = value;
this.UpdateTransform(false);
this.CenterOnPoint(p, false);
}
}
}
protected bool m_bZoomToFit;
public bool ZoomToFit
{
get
{
return this.m_bZoomToFit;
}
set
{
if (this.m_bZoomToFit != value)
{
this.m_bZoomToFit = value;
this.AutoScrollPosition = new Point(0, 0);
this.AutoScroll = (!this.m_bZoomToFit);
this.UpdateTransform(false);
}
}
}
public float Rotation
{
get
{
return this.m_flAngle;
}
set
{
if (this.m_flAngle != value)
{
this.m_flAngle = value;
this.UpdateTransform(false);
if (this.m_bZoomToFit && this.ZoomChanged != null) this.ZoomChanged(this, null);
// If we have any thumbnail control coupled, inform it.
if (this.ImageChanged != null) this.ImageChanged(this, null);
}
}
}
public int SelectedPoint
{
get
{
ArrayList selpoints = this.SelectedPoints;
if (selpoints.Count == 0) return -1;
return (int)(selpoints[0]);
}
set
{
ArrayList selpoints = new ArrayList();
if (value != -1) selpoints.Add(value);
this.SelectedPoints = selpoints;
}
}
public ArrayList SelectedPoints
{
get
{
ArrayList res = new ArrayList();
int iPoint = 0;
foreach (bool bSel in this.m_listPointSel)
{
if (bSel) res.Add(iPoint);
iPoint++;
}
return res;
}
set
{
bool bSelectionVisible = false;
for (int iPoint = 0; iPoint < this.m_listPointSel.Count; iPoint++)
{
// Check whether the point should be selected.
bool bVal = false;
foreach (int iIndex in value)
{
if (iIndex == iPoint) bVal = true;
}
// Get the current point (in image coordinates).
PointF point = (PointF)this.m_listPoints[iPoint];
if (bVal)
{
// The point needs to be selected. If it is not yet
// selected, select it, and ensure it will be redrawn.
if (!(bool)(this.m_listPointSel[iPoint]))
{
this.m_listPointSel[iPoint] = true;
this.InvalidateMarkerImage(point);
}
if (this.IsVisible(point)) bSelectionVisible = true;
}
else
{
// The point should not be selected. If it is currently
// selected, deselect it, and ensure it will be redrawn.
if ((bool)(this.m_listPointSel[iPoint]))
{
this.m_listPointSel[iPoint] = false;
this.InvalidateMarkerImage((PointF)this.m_listPoints[iPoint]);
}
}
}
if (value.Count != 0 && !bSelectionVisible) this.CenterOnPoint((PointF)(this.m_listPoints[(int)(value[0])]), false);
}
}
public enum PlotBoxMouseState
{
None, Drag, Select, Add
}
public void ShowPointAt(PointF pImage, PointF pScreen)
{
// Get the current screen position of the image point.
PointF pscr = this.ImageToScreen(pImage);
// Determine difference between current screen position, and
// requested screen position.
Point pOffset = Point.Round(new PointF(pScreen.X - pscr.X, pScreen.Y - pscr.Y));
// Update scroll position.
this.AutoScrollPosition = new Point(-this.AutoScrollPosition.X - pOffset.X, -this.AutoScrollPosition.Y - pOffset.Y);
}
public void CenterOnPoint(PointF pointImage, bool bOnlyIfNotVisible)
{
// Get the current screen position of the image point.
PointF pscr = this.ImageToScreen(pointImage);
// Get the visible area (in screen coordinates)
Rectangle scr = this.ClientRectangle;
// Update scroll position (center on point), if needed.
if (!bOnlyIfNotVisible
|| (pscr.X < scr.Left
|| pscr.X > scr.Right
|| pscr.Y < scr.Top
|| pscr.Y > scr.Bottom))
{
this.AutoScrollPosition = Point.Round(new PointF(pscr.X - scr.Width / 2F - this.AutoScrollPosition.X, pscr.Y - scr.Height / 2F - this.AutoScrollPosition.Y));
}
}
public bool IsVisible(PointF pointImage)
{
// Get the current screen position of the image point.
PointF pscr = this.ImageToScreen(pointImage);
// Get the visible area (in screen coordinates)
Rectangle scr = this.ClientRectangle;
return (pscr.X >= scr.Left
&& pscr.X <= scr.Right
&& pscr.Y >= scr.Top
&& pscr.Y <= scr.Bottom);
}
public enum PlotBoxMarkerType
{
Dot, Cross, FatCross
}
protected PlotBoxMouseState m_MouseState;
public PlotBoxMouseState MouseStatus
{
get
{
return this.m_MouseState;
}
set
{
this.m_MouseState = value;
this.SetCursor();
if (this.StatusTextChanged != null) this.StatusTextChanged(this, new EventArgs());
}
}
public string StatusText
{
get
{
if (this.Image == null) return "";
if (this.m_iExpCoordinateAxis != 0)
{
string action = "Left-click";
if (!this.m_pointTemp.IsEmpty) action = "Release left mouse button";
return action + " to set "
+ (this.m_iExpCoordinateNr == 1 ? "first" : "second")
+ " calibration point for "
+ (this.m_iExpCoordinateAxis == 1 ? "x" : "y")
+ "-axis, drag with right mouse button to scroll.";
}
switch (this.MouseStatus)
{
case PlotBoxMouseState.Add:
if (!this.m_pointTemp.IsEmpty) return "Release left mouse button to add data point, press Escape to cancel.";
return "Left-click to add data point, drag with right mouse button to scroll.";
case PlotBoxMouseState.Drag:
return "Drag to scroll image.";
case PlotBox.PlotBoxMouseState.Select:
if (this.m_bCaretSelect) return "Release left mouse button to select points, press Escape to cancel.";
return "Drag or click with left mouse button to select points, drag with right mouse button to scroll.";
default:
return "";
}
}
}
protected void SetCursor()
{
if (this.m_iExpCoordinateAxis != 0 && this.m_iExpCoordinateNr != 0)
{
this.Cursor = Cursors.Cross;
}
else
{
switch (this.m_MouseState)
{
case (PlotBoxMouseState.Drag):
this.Cursor = Cursors.SizeAll;
break;
case (PlotBoxMouseState.Add):
this.Cursor = Cursors.Cross;
break;
case (PlotBoxMouseState.Select):
this.Cursor = Cursors.Arrow;
break;
}
}
}
public PointF ScreenToImage(PointF pointScreen)
{
return this.ScreenToImage(pointScreen.X, pointScreen.Y);
}
public PointF ScreenToImage(float X, float Y)
{
PointF[] points = { new PointF(X, Y) };
points[0].X -= this.AutoScrollPosition.X;
points[0].Y -= this.AutoScrollPosition.Y;
this.m_matTransformInverse.TransformPoints(points);
return points[0];
}
public bool IsPointInImage(PointF point)
{
if (this.m_objImage == null) return false;
return (point.X >= 0 && point.Y >= 0 && point.X <= this.m_objImage.Width && point.Y <= this.m_objImage.Height);
}
public PointF ImageToScreen(PointF pointImage)
{
return this.ImageToScreen(pointImage.X, pointImage.Y);
}
public PointF ImageToScreen(float X, float Y)
{
PointF[] points = { new PointF(X, Y) };
this.m_matTransform.TransformPoints(points);
points[0].X += this.AutoScrollPosition.X;
points[0].Y += this.AutoScrollPosition.Y;
return points[0];
}
protected int m_iExpCoordinateAxis, m_iExpCoordinateNr;
public void ExpectAxisCoordinate(int iAxis, int iNr)
{
this.m_iExpCoordinateAxis = iAxis;
this.m_iExpCoordinateNr = iNr;
this.SetCursor();
if (this.StatusTextChanged != null) this.StatusTextChanged(this, new EventArgs());
}
public void StopExpectAxisCoordinate()
{
this.m_iExpCoordinateAxis = 0;
this.m_iExpCoordinateNr = 0;
this.SetCursor();
if (this.StatusTextChanged != null) this.StatusTextChanged(this, new EventArgs());
}
protected int m_iAxesState;
public void SetAxisTrueCoordinate(PlotBoxAxisType axis, int iNr, bool bValue, double dValue)
{
int iAxis = (axis == PlotBoxAxisType.X ? 0 : 1);
AxisPoint point = this.m_arrAxesPoints[iAxis, iNr - 1];
point.HasValue = bValue;
if (this.m_arrAxesLogScale[iAxis])
{
point.Value = Math.Log10(dValue);
point.NonLogValue = dValue;
}
else point.Value = dValue;
this.UpdateAxes();
}
protected bool[] m_arrAxesLogScale;
public void SetAxisLogState(PlotBoxAxisType axis, bool bLog)
{
int iAxis = (axis == PlotBoxAxisType.X ? 0 : 1);
if (this.m_arrAxesLogScale[iAxis] != bLog)
{
if (bLog)
{
this.m_arrAxesPoints[iAxis, 0].NonLogValue = this.m_arrAxesPoints[iAxis, 0].Value;
if (this.m_arrAxesPoints[iAxis, 0].HasValue) this.m_arrAxesPoints[iAxis, 0].Value = Math.Log10(this.m_arrAxesPoints[iAxis, 0].Value);
this.m_arrAxesPoints[iAxis, 1].NonLogValue = this.m_arrAxesPoints[iAxis, 1].Value;
if (this.m_arrAxesPoints[iAxis, 1].HasValue) this.m_arrAxesPoints[iAxis, 1].Value = Math.Log10(this.m_arrAxesPoints[iAxis, 1].Value);
}
else
{
this.m_arrAxesPoints[iAxis, 0].Value = this.m_arrAxesPoints[iAxis, 0].NonLogValue;
this.m_arrAxesPoints[iAxis, 1].Value = this.m_arrAxesPoints[iAxis, 1].NonLogValue;
}
this.m_arrAxesLogScale[iAxis] = bLog;
this.UpdateAxes();
}