-
Notifications
You must be signed in to change notification settings - Fork 12
/
GraphUnit.cpp
2829 lines (2448 loc) · 84.7 KB
/
GraphUnit.cpp
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
#include <vcl.h>
#include <Clipbrd.hpp>
#ifdef __BORLANDC__
#if (__BORLANDC__ < 0x0600)
#include <pngimage.hpp>
#include <Jpeg.hpp>
#include <GIFImg.hpp>
#else
#include <Vcl.Imaging.pngimage.hpp>
#include <Vcl.Imaging.jpeg.hpp>
#include <Vcl.Imaging.GIFImg.hpp>
#endif
#endif
#include <math.h>
#pragma hdrstop
#include "GraphUnit.h"
#include "Unit1.h"
#include "DataUnit.h"
#include "LCMatch.h"
#include "spline.h"
#include "Graphs.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
#define POPUP_MENU_LINE_PADDING 5
TGraphForm *GraphForm = NULL;
__fastcall TGraphForm::TGraphForm(TComponent* Owner, TWinControl *parent)
: TForm(Owner)
{
m_parent = this->Parent;
if (parent)
{
this->SetParent(parent);
this->BorderStyle = bsNone;
this->FormStyle = fsNormal;
// this->Align = alClient;
}
}
void __fastcall TGraphForm::FormCreate(TObject *Sender)
{
gdiplusToken = 0;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Caption = Application->Title + " " + this->Caption;
{ // get the phsyical screen size
m_screen_width = 0;
m_screen_height = 0;
SYSTEM_INFO m_system_info;
HDC hDC = GetDC(0);
if (hDC != NULL)
{
//ScreenBitsPerPixel = ::GetDeviceCaps(hDC, BITSPIXEL);
m_screen_width = ::GetDeviceCaps(hDC, HORZRES);
m_screen_height = ::GetDeviceCaps(hDC, VERTRES);
ReleaseDC(0, hDC);
}
}
// stop flicker
this->ControlStyle = this->ControlStyle << csOpaque;
GraphPaintBox->ControlStyle = GraphPaintBox->ControlStyle << csOpaque;
SaveImageImage->ControlStyle = SaveImageImage->ControlStyle << csOpaque;
CopyImageImage->ControlStyle = CopyImageImage->ControlStyle << csOpaque;
CloseImage->ControlStyle = CloseImage->ControlStyle << csOpaque;
m_graph_bm = NULL;
/*
m_graph_bm = new Graphics::TBitmap();
if (m_graph_bm)
{
m_graph_bm->Monochrome = false;
m_graph_bm->Transparent = false;
m_graph_bm->PixelFormat = pf32bit;
}
*/
m_popup_menu_mouse_graph = -1;
m_popup_menu_mouse_Hz = -1;
m_popup_menu_mouse_marker = -1;
m_popup_menu_marker_index = -1;
m_popup_menu_graph_type_graph = -1;
m_popup_menu_mouse_graph = -1;
// m_graph_type = GRAPH_TYPE_LOGMAG_S11S21;
m_graph_type = GRAPH_TYPE_SMITH_S11;
// m_graph_type = GRAPH_TYPE_TDR_LIN_LP_IMPULSE_S11;
// m_graph_type = GRAPH_TYPE_TDR_LIN_BP_S11;
// m_graph_type = GRAPH_TYPE_TDR_IMPEDANCE_S11;
createGraphTypeMenus();
// this->OnMessage = OnMessage;
}
void __fastcall TGraphForm::FormDestroy(TObject *Sender)
{
if (m_parent)
{
this->SetParent(m_parent);
m_parent = NULL;
}
else
this->SetParent(NULL);
if (m_graph_bm != NULL)
delete m_graph_bm;
m_graph_bm = NULL;
if (gdiplusToken != 0)
Gdiplus::GdiplusShutdown(gdiplusToken);
gdiplusToken = 0;
if (Application->MainForm)
::PostMessage(Application->MainForm->Handle, WM_GRAPH_FORM_DESTROYED, (WPARAM)this, 0);
}
void __fastcall TGraphForm::OnMessage(tagMSG &msg, bool &handled)
{
switch (msg.message)
{
// case WM_MAP_SHOW:
// MapSpeedButton->Down = true;
// handled = true;
// break;
// case WM_MAP_HIDE:
// MapSpeedButton->Down = false;
// handled = true;
// break;
case WM_ERASEBKGND:
handled = true;
break;
}
}
void __fastcall TGraphForm::WMWindowPosChanging(TWMWindowPosChanging &msg)
{
const int thresh = WINDOW_SNAP;
if (msg.WindowPos->flags & SWP_STATECHANGED)
{
if (msg.WindowPos->flags & SWP_FRAMECHANGED)
{
if (msg.WindowPos->x < 0 && msg.WindowPos->y < 0)
{ // Window state is about to change to MAXIMIZED
if ((msg.WindowPos->flags & (SWP_SHOWWINDOW | SWP_NOACTIVATE)) == (SWP_SHOWWINDOW | SWP_NOACTIVATE))
{ // about to minimize
return;
}
else
{ // about to maximize
return;
}
}
}
}
if (msg.WindowPos->hwnd != this->Handle || Screen == NULL)
return;
const int dtLeft = Screen->DesktopRect.left;
// const int dtRight = Screen->DesktopRect.right;
const int dtTop = Screen->DesktopRect.top;
const int dtBottom = Screen->DesktopRect.bottom;
const int dtWidth = Screen->DesktopRect.Width();
const int dtHeight = Screen->DesktopRect.Height();
// const int waLeft = Screen->WorkAreaRect.left;
// const int waRight = Screen->WorkAreaRect.right;
// const int waTop = Screen->WorkAreaRect.top;
// const int waBottom = Screen->WorkAreaRect.bottom;
// const int waWidth = Screen->WorkAreaRect.Width();
// const int waHeight = Screen->WorkAreaRect.Height();
int x = msg.WindowPos->x;
int y = msg.WindowPos->y;
int w = msg.WindowPos->cx;
int h = msg.WindowPos->cy;
for (int i = 0; i < Screen->MonitorCount; i++)
{ // sticky screen edges
const int mLeft = Screen->Monitors[i]->WorkareaRect.left;
const int mRight = Screen->Monitors[i]->WorkareaRect.right;
const int mTop = Screen->Monitors[i]->WorkareaRect.top;
const int mBottom = Screen->Monitors[i]->WorkareaRect.bottom;
const int mWidth = Screen->Monitors[i]->WorkareaRect.Width();
const int mHeight = Screen->Monitors[i]->WorkareaRect.Height();
if (ABS(x - mLeft) < thresh)
x = mLeft; // stick left to left side
else
if (ABS((x + w) - mRight) < thresh)
x = mRight - w; // stick right to right side
if (ABS(y - mTop) < thresh)
y = mTop; // stick top to top side
else
if (ABS((y + h) - mBottom) < thresh)
y = mBottom - h; // stick bottom to bottm side
// stick the right side to the right side of the screen if the left side is stuck to the left side of the screen
if (x == mLeft)
if ((w >= (mWidth - thresh)) && (w <= (mWidth + thresh)))
w = mWidth;
// stick the bottom to the bottom of the screen if the top is stuck to the top of the screen
if (y == mTop)
if ((h >= (mHeight - thresh)) && (h <= (mHeight + thresh)))
h = mHeight;
}
/*
{ // sticky screen edges
if (ABS(x - waLeft) < thresh)
x = waLeft; // stick left to left side
else
if (ABS((x + w) - waRight) < thresh)
x = waRight - w; // stick right to right side
if (ABS(y - waTop) < thresh)
y = waTop; // stick top to top side
else
if (ABS((y + h) - waBottom) < thresh)
y = waBottom - h; // stick bottom to bottm side
// stick the right side to the right side of the screen if the left side is stuck to the left side of the screen
if (x == waLeft)
if ((w >= (waWidth - thresh)) && (w <= (waWidth + thresh)))
w = waWidth;
// stick the bottom to the bottom of the screen if the top is stuck to the top of the screen
if (y == waTop)
if ((h >= (waHeight - thresh)) && (h <= (waHeight + thresh)))
h = waHeight;
}
*/
// limit minimum size
if (w < Constraints->MinWidth)
w = Constraints->MinWidth;
if (h < Constraints->MinHeight)
h = Constraints->MinHeight;
// limit maximum size
if (w > Constraints->MaxWidth && Constraints->MaxWidth > Constraints->MinWidth)
w = Constraints->MaxWidth;
if (h > Constraints->MaxHeight && Constraints->MaxHeight > Constraints->MinHeight)
h = Constraints->MaxHeight;
// limit maximum size
if (w > dtWidth)
w = dtWidth;
if (h > dtHeight)
h = dtHeight;
/*
if (Application->MainForm && this != Application->MainForm)
{ // stick to our main form sides
const TRect rect = Application->MainForm->BoundsRect;
if (ABS(x - rect.left) < thresh)
x = rect.left; // stick to left to left side
else
if (ABS((x + w) - rect.left) < thresh)
x = rect.left - w; // stick right to left side
else
if (ABS(x - rect.right) < thresh)
x = rect.right; // stick to left to right side
else
if (ABS((x + w) - rect.right) < thresh)
x = rect.right - w; // stick to right to right side
if (ABS(y - rect.top) < thresh)
y = rect.top; // stick top to top side
else
if (ABS((y + h) - rect.top) < thresh)
y = rect.top - h; // stick bottom to top side
else
if (ABS(y - rect.bottom) < thresh)
y = rect.bottom; // stick top to bottom side
else
if (ABS((y + h) - rect.bottom) < thresh)
y = rect.bottom - h; // stick bottom to bottom side
}
*/
// stop it completely leaving the desktop area
if (x < (dtLeft - Width + (dtWidth / 15)))
x = dtLeft - Width + (dtWidth / 15);
if (x > (dtWidth - (Screen->Width / 15)))
x = dtWidth - (Screen->Width / 15);
if (y < dtTop)
y = dtTop;
if (y > (dtBottom - (dtHeight / 10)))
y = dtBottom - (dtHeight / 10);
msg.WindowPos->x = x;
msg.WindowPos->y = y;
msg.WindowPos->cx = w;
msg.WindowPos->cy = h;
}
void __fastcall TGraphForm::FormClose(TObject *Sender, TCloseAction &Action)
{
this->SetParent(m_parent);
m_parent = NULL;
Action = caFree;
if (Application->MainForm)
::PostMessage(Application->MainForm->Handle, WM_GRAPH_FORM_CLOSED, (WPARAM)this, 0);
}
void __fastcall TGraphForm::FormShow(TObject *Sender)
{
//
}
void __fastcall TGraphForm::FormHide(TObject *Sender)
{
Close();
}
void __fastcall TGraphForm::createGraphTypeMenus()
{
// reduce the item vertical spacing
//GraphTypePopupMenu->OwnerDraw = true;
//GraphPopupMenu->OwnerDraw = true;
Graphtype1->Clear();
// for (int i = Graphtype1->Count - 1; i >= 0; i--)
// Graphtype1->Delete(i);
GraphTypePopupMenu->Items->Clear();
// for (int i = GraphTypePopupMenu->Items->Count - 1; i >= 0; i--)
// GraphTypePopupMenu->Items->Delete(i);
if (data_unit.m_vna_data.type != UNIT_TYPE_TINYSA)
{
for (int i = 0; i < GRAPH_TYPE_SIZE; i++)
{
int graph_type = -1;
String s = "None";
switch (i)
{
case 1: graph_type = GRAPH_TYPE_LOGMAG_S11; s = "Log mag S11"; break;
case 2: graph_type = GRAPH_TYPE_LOGMAG_S21; s = "Log mag S21"; break;
case 3: graph_type = GRAPH_TYPE_LOGMAG_S11S21; s = "Log mag S11 and S21"; break;
case 4: graph_type = GRAPH_TYPE_LINMAG_S11; s = "Lin mag S11"; break;
case 5: graph_type = GRAPH_TYPE_LINMAG_S21; s = "Lin mag S21"; break;
case 6: graph_type = GRAPH_TYPE_LINMAG_S11S21; s = "Lin mag S11 and S21"; break;
case 7: graph_type = GRAPH_TYPE_PHASE_S11; s = "Phase S11"; break;
case 8: graph_type = GRAPH_TYPE_PHASE_S21; s = "Phase S21"; break;
case 9: graph_type = GRAPH_TYPE_PHASE_S11S21; s = "Phase S11 and S21"; break;
case 10: graph_type = GRAPH_TYPE_PHASE_UNWRAP_S11; s = "Phase unwrap S11"; break;
case 11: graph_type = GRAPH_TYPE_PHASE_UNWRAP_S21; s = "Phase unwrap S21"; break;
case 12: graph_type = GRAPH_TYPE_PHASE_UNWRAP_S11S21; s = "Phase unwrap S11 and S21"; break;
case 13: graph_type = GRAPH_TYPE_GROUP_DELAY_S11; s = "Group delay S11"; break;
case 14: graph_type = GRAPH_TYPE_GROUP_DELAY_S21; s = "Group delay S21"; break;
case 15: graph_type = GRAPH_TYPE_GROUP_DELAY_S11S21; s = "Group delay S11 and S21"; break;
case 16: graph_type = GRAPH_TYPE_REAL_IMAG_S11; s = "S-Parameters S11"; break;
case 17: graph_type = GRAPH_TYPE_REAL_IMAG_S21; s = "S-Parameters S21"; break;
case 18: graph_type = GRAPH_TYPE_VSWR_S11; s = "VSWR S11"; break;
case 19: graph_type = GRAPH_TYPE_RETLOSS_S11; s = "Return Loss S11"; break;
case 20: graph_type = GRAPH_TYPE_IMPEDANCE_S11; s = "|Impedance S11|"; break;
case 21: graph_type = GRAPH_TYPE_SERIES_RJX_S11; s = "Series R+jX S11"; break;
case 22: graph_type = GRAPH_TYPE_GJB_S11; s = "Admittance G+jB S11"; break;
case 23: graph_type = GRAPH_TYPE_PARALLEL_RJX_S11; s = "Parallel R||jX S11"; break;
case 24: graph_type = GRAPH_TYPE_SERIES_RESISTANCE_S11; s = "Series resistance S11"; break;
case 25: graph_type = GRAPH_TYPE_SERIES_REACTANCE_S11; s = "Series reactance S11"; break;
case 26: graph_type = GRAPH_TYPE_QUALITY_FACTOR_S11; s = "Quality factor S11"; break;
case 27: graph_type = GRAPH_TYPE_SERIES_CAPACITANCE_S11; s = "Series capacitance S11"; break;
case 28: graph_type = GRAPH_TYPE_SERIES_INDUCTANCE_S11; s = "Series inductance S11"; break;
case 29: graph_type = GRAPH_TYPE_COAX_LOSS_S11; s = "Coax loss S11"; break;
case 30: graph_type = GRAPH_TYPE_CAL_LOGMAG; s = "Calibrations log mag"; break;
case 31: graph_type = GRAPH_TYPE_SMITH_S11; s = "Smith S11"; break;
case 32: graph_type = GRAPH_TYPE_SMITH_S21; s = "Smith S21"; break;
case 33: graph_type = GRAPH_TYPE_ADMITTANCE_S11; s = "Admittance S11"; break;
case 34: graph_type = GRAPH_TYPE_ADMITTANCE_S21; s = "Admittance S21"; break;
case 35: graph_type = GRAPH_TYPE_POLAR_S11; s = "Polar S11"; break;
case 36: graph_type = GRAPH_TYPE_POLAR_S21; s = "Polar S21"; break;
case 37: graph_type = GRAPH_TYPE_TDR_LIN_LP_IMPULSE_S11; s = "TDR linear low pass impulse S11"; break;
case 38: graph_type = GRAPH_TYPE_TDR_LIN_BP_S11; s = "TDR linear band pass S11"; break;
case 39: graph_type = GRAPH_TYPE_TDR_LOG_LP_IMPULSE_S11; s = "TDR log low pass impulse S11"; break;
case 40: graph_type = GRAPH_TYPE_TDR_LOG_BP_S11; s = "TDR log band pass S11"; break;
case 41: graph_type = GRAPH_TYPE_TDR_IMPEDANCE_S11; s = "TDR |impedance S11|"; break;
// case 42: graph_type = GRAPH_TYPE_PHASE_VECTOR_S11; s = "Phase vector S11"; break;
// case 43: graph_type = GRAPH_TYPE_PHASE_VECTOR_S21; s = "Phase vector S21"; break;
}
if (graph_type < 0)
continue;
TMenuItem *menu_item;
const bool column_break = (i == ((3 + GRAPH_TYPE_SIZE) / 2)) ? true : false;
const bool line_break = (graph_type == GRAPH_TYPE_SMITH_S11 || graph_type == GRAPH_TYPE_PHASE_VECTOR_S11 || graph_type == GRAPH_TYPE_TDR_LIN_LP_IMPULSE_S11 || graph_type == GRAPH_TYPE_GJB_S11) ? true : false;
// add it to the graph popup menu
if (column_break || line_break)
{
menu_item = new TMenuItem(GraphPopupMenu);
menu_item->Caption = "-";
menu_item->Tag = -1;
menu_item->Break = column_break ? mbBreak : mbNone;
menu_item->Enabled = false;
menu_item->AutoLineReduction = maAutomatic; //maManual
Graphtype1->Add(menu_item);
}
menu_item = new TMenuItem(GraphPopupMenu);
if (menu_item != NULL)
{
menu_item->OnClick = MenuItemGraphTypeClick;
menu_item->Caption = s;
menu_item->Tag = graph_type;
menu_item->RadioItem = true;
Graphtype1->Add(menu_item);
}
// add it to the right click popup menu
if (column_break || line_break)
{
menu_item = new TMenuItem(GraphTypePopupMenu);
menu_item->Caption = "-";
menu_item->Tag = -1;
menu_item->Break = column_break ? mbBreak : mbNone;
menu_item->Enabled = false;
menu_item->AutoLineReduction = maAutomatic; //maManual
GraphTypePopupMenu->Items->Add(menu_item);
}
menu_item = new TMenuItem(GraphTypePopupMenu);
if (menu_item != NULL)
{
menu_item->OnClick = MenuItemChangeGraphTypeClick;
menu_item->Caption = s;
menu_item->Tag = graph_type;
menu_item->RadioItem = true;
GraphTypePopupMenu->Items->Add(menu_item);
}
}
}
else
{
for (int i = 0; i < 2; i++)
{
int graph_type = -1;
String s = "None";
switch (i)
{
case 0: graph_type = GRAPH_TYPE_LOGMAG_S11; s = "Log mag"; break;
case 1: graph_type = GRAPH_TYPE_LINMAG_S11; s = "Lin mag"; break;
}
TMenuItem *menu_item;
// add it to the graph popup menu
menu_item = new TMenuItem(GraphPopupMenu);
if (menu_item != NULL)
{
menu_item->OnClick = MenuItemGraphTypeClick;
menu_item->Caption = s;
menu_item->Tag = graph_type;
menu_item->RadioItem = true;
Graphtype1->Add(menu_item);
}
menu_item = new TMenuItem(GraphTypePopupMenu);
if (menu_item != NULL)
{
menu_item->OnClick = MenuItemChangeGraphTypeClick;
menu_item->Caption = s;
menu_item->Tag = graph_type;
menu_item->RadioItem = true;
GraphTypePopupMenu->Items->Add(menu_item);
}
}
settings.graphArrangement = GRAPH_ARRANGE_1;
settings.graphType[0] = GRAPH_TYPE_LOGMAG_S11;
settings.graphType[1] = GRAPH_TYPE_LINMAG_S11;
}
}
void __fastcall TGraphForm::saveBitmap(Graphics::TBitmap *bm, String ID, String dialog_title)
{
if (bm == NULL)
return;
String filename;
ID = ID.Trim();
dialog_title = dialog_title.Trim();
if (!ID.IsEmpty())
filename += ID;
if (!filename.IsEmpty())
filename += "_";
filename += FormatDateTime("yyyy-mm-dd_hh-nn-ss", Now()) + ".png";
filename = common.cleanFilename(filename, false, true);
SaveDialog1->Title = dialog_title.IsEmpty() ? String("Save the image too ..") : dialog_title;
SaveDialog1->FileName = filename;
Application->NormalizeTopMosts();
const bool ok = SaveDialog1->Execute();
Application->RestoreTopMosts();
if (!ok)
return;
filename = SaveDialog1->FileName;
String ext = ExtractFileExt(filename).LowerCase();
if (ext == ".bmp")
{
try
{
bm->SaveToFile(filename);
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return;
}
if (ext == ".jpg" || ext == ".jpeg")
{
TJPEGImage *jpg;
try
{
jpg = new TJPEGImage;
if (jpg == NULL)
return;
jpg->Assign(bm);
jpg->CompressionQuality = 100; // 0 to 100
jpg->SaveToFile(filename);
}
__finally
{
if (jpg != NULL)
delete jpg;
}
return;
}
if (ext == ".png")
{
TPngImage *png;
try
{
png = new TPngImage();
if (png == NULL)
return;
png->Assign(bm);
png->CompressionLevel = 9; // 0 to 9
png->SaveToFile(filename);
}
__finally
{
if (png != NULL)
delete png;
}
return;
}
Application->NormalizeTopMosts();
Application->MessageBox(L"Only PNG, JPG, JPEG and BMP formats are supported", L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
}
void __fastcall TGraphForm::saveImage(const bool to_clipboard)
{
if (graphs.m_graph_bm == NULL)
{
Application->NormalizeTopMosts();
Application->MessageBox(L"No image to save", L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
String filename = data_unit.m_vna_data.name.LowerCase();
// String sweep_name = SweepNameEdit->Text.Trim();
// if (!sweep_name.IsEmpty())
// filename += "_" + sweep_name;
#if 0
// add the graph type into the filename
switch (m_graph_type)
{
case GRAPH_TYPE_LOGMAG_S11: filename += "_LOGMAG_S11"; break;
case GRAPH_TYPE_LOGMAG_S21: filename += "_LOGMAG_S21"; break;
case GRAPH_TYPE_LOGMAG_S11S21: filename += "_LOGMAG_S11S21"; break;
case GRAPH_TYPE_LINMAG_S11: filename += "_LINMAG_S11"; break;
case GRAPH_TYPE_LINMAG_S21: filename += "_LINMAG_S21"; break;
case GRAPH_TYPE_LINMAG_S11S21: filename += "_LINMAG_S11S21"; break;
case GRAPH_TYPE_PHASE_S11: filename += "_PHASE_S11"; break;
case GRAPH_TYPE_PHASE_S21: filename += "_PHASE_S21"; break;
case GRAPH_TYPE_PHASE_S11S21: filename += "_PHASE_S11S21"; break;
case GRAPH_TYPE_PHASE_UNWRAP_S11: filename += "_PHASE_UNWRAP_S11"; break;
case GRAPH_TYPE_PHASE_UNWRAP_S21: filename += "_PHASE_UNWRAP_S21"; break;
case GRAPH_TYPE_PHASE_UNWRAP_S11S21: filename += "_PHASE_UNWRAP_S11S21"; break;
case GRAPH_TYPE_GROUP_DELAY_S11: filename += "_GROUP_DELAY_S11"; break;
case GRAPH_TYPE_GROUP_DELAY_S21: filename += "_GROUP_DELAY_S21"; break;
case GRAPH_TYPE_GROUP_DELAY_S11S21: filename += "_GROUP_DELAY_S11S21"; break;
case GRAPH_TYPE_VSWR_S11: filename += "_VSWR_S11"; break;
case GRAPH_TYPE_IMPEDANCE_S11: filename += "_IMPEDANCE_S11"; break;
case GRAPH_TYPE_SERIES_RJX_S11: filename += "_SERIES_RJX_S11"; break;
case GRAPH_TYPE_PARALLEL_RJX_S11: filename += "_PARALLEL_RJX_S11"; break;
case GRAPH_TYPE_SERIES_RESISTANCE_S11: filename += "_SERIES_RESISTANCE_S11"; break;
case GRAPH_TYPE_SERIES_REACTANCE_S11: filename += "_SERIES_REACTANCE_S11"; break;
case GRAPH_TYPE_QUALITY_FACTOR_S11: filename += "_QUALITY_FACTOR_S11"; break;
case GRAPH_TYPE_SMITH_S11: filename += "_SMITH_S11"; break;
case GRAPH_TYPE_SMITH_S21: filename += "_SMITH_S21"; break;
case GRAPH_TYPE_ADMITTANCE_S11: filename += "_ADMITTANCE_S11"; break;
case GRAPH_TYPE_ADMITTANCE_S21: filename += "_ADMITTANCE_S21"; break;
case GRAPH_TYPE_POLAR_S11: filename += "_POLAR_S11"; break;
case GRAPH_TYPE_POLAR_S21: filename += "_POLAR_S21"; break;
case GRAPH_TYPE_TDR_LIN_LP_IMPULSE_S11: filename += "_TDR_LIN_LP_IMPULSE_S11"; break;
case GRAPH_TYPE_TDR_LIN_BP_S11: filename += "_TDR_LIN_BP_S11"; break;
case GRAPH_TYPE_TDR_LOG_LP_IMPULSE_S11: filename += "_TDR_LOG_LP_IMPULSE_S11"; break;
case GRAPH_TYPE_TDR_LOG_BP_S11: filename += "_TDR_LOG_BP_S11"; break;
case GRAPH_TYPE_TDR_IMPEDANCE_S11: filename += "_TDR_IMPEDANCE_S11"; break;
case GRAPH_TYPE_REAL_IMAG_S11: filename += "_REAL_IMAG_S11"; break;
case GRAPH_TYPE_REAL_IMAG_S21: filename += "_REAL_IMAG_S21"; break;
case GRAPH_TYPE_SERIES_CAPACITANCE_S11: filename += "_SERIES_CAPACITANCE_S11"; break;
case GRAPH_TYPE_SERIES_INDUCTANCE_S11: filename += "_SERIES_INDUCTANCE_S11"; break;
case GRAPH_TYPE_COAX_LOSS_S11: filename += "_COAX_LOSS_S11"; break;
case GRAPH_TYPE_CAL_LOGMAG: filename += "_CALIBRATIONS"; break;
case GRAPH_TYPE_PHASE_VECTOR_S11: filename += "_PHASE_VECTOR_S11"; break;
case GRAPH_TYPE_PHASE_VECTOR_S21: filename += "_PHASE_VECTOR_S21"; break;
case GRAPH_TYPE_GJB_S11: filename += "_GJB_S11"; break;
case GRAPH_TYPE_RETLOSS_S11: filename += "_RETURNLOSS_S11"; break;
}
#endif
// add the date & time onto the image
Graphics::TBitmap *bm1 = graphs.m_graph_bm;
Graphics::TBitmap *bm2 = bm2 = new Graphics::TBitmap();
if (bm2)
{ // make a copy of the image but with extra height so as to add a title and date/time
// ****************
// date time string
String datetime_s = FormatDateTime("ddd dd mmm yyyy hh:nn:ss", Now());
// ***************
bm2->Monochrome = false;
bm2->Transparent = false;
bm2->PixelFormat = pf32bit;
//bm2->Canvas->Font = SweepNameFontLabel->Font;
// bm2->Canvas->Font->Style = bm2->Canvas->Font->Style << fsBold;
//const TSize sz1 = bm2->Canvas->TextExtent(sweep_name);
// bm2->Canvas->Font->Style = bm2->Canvas->Font->Style >> fsBold;
bm2->Canvas->Font = bm1->Canvas->Font;
// bm2->Canvas->Font->Style = bm2->Canvas->Font->Style << fsBold;
const TSize sz2 = bm2->Canvas->TextExtent(datetime_s);
// bm2->Canvas->Font->Style = bm2->Canvas->Font->Style >> fsBold;
int y1 = 3;
int y2 = y1;
// if (!sweep_name.IsEmpty())
// y2 += sz1.cy + 3;
y2 += sz2.cy + 3;
bm2->Width = bm1->Width;
bm2->Height = bm1->Height + y2;
// background colour
bm2->Canvas->Brush->Style = bsSolid;
bm2->Canvas->Brush->Color = settings.m_colours.background;
bm2->Canvas->FillRect(bm2->Canvas->ClipRect);
// draw the current graph image onto the new image
bm2->Canvas->CopyMode = cmSrcCopy;
bm2->Canvas->Draw(0, y2, bm1);
// remove the border gradient
bm2->Canvas->Brush->Style = bsClear;
bm2->Canvas->Pen->Color = settings.m_colours.background;
bm2->Canvas->Pen->Style = psSolid;
bm2->Canvas->Pen->Width = 1;
for (int i = 0; i < settings.borderWidth; i++)
bm2->Canvas->Rectangle(i, y2 + i, bm1->Width - i, y2 + bm1->Height - i);
// add the sweep name text onto the new image
//if (!sweep_name.IsEmpty())
//{
// bm2->Canvas->Font = SweepNameFontLabel->Font;
// bm2->Canvas->Font->Color = settings.m_colours.font;
// bm2->Canvas->Brush->Style = bsClear;
// bm2->Canvas->TextOutA((bm2->Width - sz1.cx) / 2, y1, sweep_name);
// y1 += sz1.cy + 3;
//}
// add the date//time text onto the new image
bm2->Canvas->Font = bm1->Canvas->Font;
bm2->Canvas->Font->Color = settings.m_colours.font;
bm2->Canvas->Brush->Style = bsClear;
bm2->Canvas->TextOutA((bm2->Width - sz2.cx) / 2, y1, datetime_s);
//y1 += sz2.cy + 3;
bm1 = bm2;
}
if (to_clipboard)
{
try
{
Clipboard()->Assign(bm1); // copy the image to the clipboard
}
catch (Exception &exception)
{
Application->ShowException(&exception);
//String s = exception.ToString();
}
}
else
saveBitmap(bm1, filename, "Save the graph image too .."); // save the image to a file
if (bm2 != NULL)
delete bm2;
}
void __fastcall TGraphForm::GraphPaintBoxMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
TPaintBox *pb = dynamic_cast<TPaintBox *>(Sender);
if (pb == NULL)
return;
graphs.m_mouse.down_x = X;
graphs.m_mouse.down_y = Y;
graphs.m_mouse.x = X;
graphs.m_mouse.y = Y;
graphs.m_mouse.down_graph = graphs.m_mouse.graph;
graphs.m_mouse.down_marker = -1;
if (graphs.m_mouse.graph_v_scale >= 0)
{ // start dragging the v-scale
const int graph = graphs.m_mouse.graph_v_scale;
const int graph_type = m_graph_type;
if (Shift.Contains(ssLeft))
{
graphs.m_mouse.graph_scale_pos_drag = graphs.m_mouse.graph_scale_pos;
graphs.m_mouse.graph_v_scale_drag = graph;
if (graphs.isFrequencyGraph(graph_type) || graphs.isTDRGraph(graph_type))
{
if (graphs.m_mouse.down_y >= 0)
{
t_graph_setting *gs = &settings.m_graph_setting[graph_type];
bool center_gamma = false;
switch (graph_type)
{
case GRAPH_TYPE_LINMAG_S11:
case GRAPH_TYPE_LINMAG_S21:
case GRAPH_TYPE_LINMAG_S11S21:
case GRAPH_TYPE_IMPEDANCE_S11:
case GRAPH_TYPE_QUALITY_FACTOR_S11:
case GRAPH_TYPE_VSWR_S11:
case GRAPH_TYPE_TDR_LIN_LP_IMPULSE_S11:
case GRAPH_TYPE_TDR_LIN_BP_S11:
case GRAPH_TYPE_TDR_IMPEDANCE_S11:
center_gamma = true;
break;
default:
break;
}
// disable auto mode - maybe
bool disable_auto_mode = true;
if (center_gamma && graphs.m_mouse.graph_scale_pos == 1)
disable_auto_mode = false;
if (disable_auto_mode)
{
if (gs->auto_max && graphs.m_mouse.graph_scale_pos_drag <= 1)
{
if (graphs.m_max[graph].index >= 0 && graphs.m_max[graph].value != graphs.m_min[graph].value)
gs->max = graphs.m_max[graph].value;
gs->auto_max = false;
}
if (gs->auto_min && graphs.m_mouse.graph_scale_pos_drag >= 1)
{
if (graphs.m_min[graph].index >= 0 && graphs.m_max[graph].value != graphs.m_min[graph].value)
gs->min = graphs.m_min[graph].value;
gs->auto_min = false;
}
}
graphs.m_mouse.down_max = gs->max;
graphs.m_mouse.down_min = gs->min;
graphs.m_mouse.down_val = gs->gamma;
}
TCursor cursor = crHandPoint;
if (pb->Cursor != cursor)
{
pb->Cursor = cursor;
// force Windows to change the cursor
pb->Parent->Perform(WM_SETCURSOR, (unsigned int)pb->Parent->Handle, MAKELPARAM(HTCLIENT, WM_MOUSEMOVE));
}
}
}
}
else
if (graphs.m_mouse.graph_h_scale >= 0)
{ // start dragging the h-scale
const int graph = graphs.m_mouse.graph_h_scale;
const int graph_type = m_graph_type;
if (Shift.Contains(ssLeft))
{
graphs.m_mouse.graph_scale_pos_drag = graphs.m_mouse.graph_scale_pos;
graphs.m_mouse.graph_h_scale_drag = graph;
if (graphs.isFrequencyGraph(graph_type))
{
if (graphs.m_mouse.down_x >= 0)
{
graphs.m_mouse.down_Hz = data_unit.m_freq_center_Hz;
graphs.m_mouse.down_max = data_unit.m_freq_stop_Hz;
graphs.m_mouse.down_min = data_unit.m_freq_start_Hz;
}
}
else
if (graphs.isSmithGraph(graph_type) || graphs.isAdmittanceGraph(graph_type) || graphs.isPolarGraph(graph_type))
{
}
else
if (graphs.isTDRGraph(graph_type))
{
}
TCursor cursor = crHandPoint;
if (pb->Cursor != cursor)
{
pb->Cursor = cursor;
// force Windows to change the cursor
pb->Parent->Perform(WM_SETCURSOR, (unsigned int)pb->Parent->Handle, MAKELPARAM(HTCLIENT, WM_MOUSEMOVE));
}
}
}
else
if (graphs.m_mouse.graph_type_select >= 0)
{ // selecting a graph type
const TPoint point = pb->ClientToScreen(TPoint(X, Y));
GraphTypePopupMenu->Popup(point.X, point.Y);
}
else
if (graphs.m_mouse.graph >= 0)
{
graphs.m_mouse.graph_scale_pos_drag = -1;
graphs.m_mouse.graph_v_scale_drag = -1;
graphs.m_mouse.graph_h_scale_drag = -1;
graphs.m_mouse.graph_drag = -1;
const int graph_type = m_graph_type;
graphs.m_mouse.down_marker = settings.m_graph_setting[graph_type].show_markers ? graphs.m_mouse.marker_index : -1;
if (graphs.m_mouse.down_marker >= 0)
{ // the user has clicked on a marker
/*
if (Shift.Contains(ssLeft))
{ // toggle it's selected state it if left mouse button clicked
const int i = MarkerListBox->Items->IndexOfObject((TObject *)graphs.m_mouse.down_marker);
if (i >= 0)
{
MarkerListBox->Selected[i] = !MarkerListBox->Selected[i];
updateInfoPanel();
}
}
*/
TCursor cursor = crHandPoint;
if (pb->Cursor != cursor)
{
pb->Cursor = cursor;
// force Windows to change the cursor
pb->Parent->Perform(WM_SETCURSOR, (unsigned int)pb->Parent->Handle, MAKELPARAM(HTCLIENT, WM_MOUSEMOVE));
}
}
else
{ // user has not clicked on a marker .. deselect all markers
// for (int k = 0; k < MarkerListBox->Items->Count; k++)
// MarkerListBox->Selected[k] = false;
// updateInfoPanel();
}
if (graphs.isFrequencyGraph(graph_type))
{
if (graphs.m_mouse.down_x >= 0)
{
//m_graph_mouse_down_Hz = xyToFreq(m_mouse_down_graph, graph_type, m_graph_mouse_down_x, graph_y[m_mouse_down_graph]);
graphs.m_mouse.down_Hz = data_unit.m_freq_center_Hz;
//Label33->Caption = common.freqToStrMhz(m_graph_mouse_down_Hz);
}
}
else
if (graphs.isSmithGraph(graph_type) || graphs.isAdmittanceGraph(graph_type) || graphs.isPolarGraph(graph_type))
{
if (graphs.m_mouse.down_marker >= 0)
graphs.m_mouse.down_Hz = settings.m_markers_freq[graphs.m_mouse.down_marker].Hz;
}
else
if (graphs.isTDRGraph(graph_type))
{
}
}
pb->Invalidate();
GraphPaintBox->Invalidate();
// if (Application->MainForm)
// ::PostMessage(Application->MainForm->Handle, WM_UPDATE_GRAPH, 0, 0);
}
void __fastcall TGraphForm::GraphPaintBoxMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
TPaintBox *pb = dynamic_cast<TPaintBox *>(Sender);
if (pb == NULL)
return;
if (graphs.m_mouse.down_marker >= 0)
{ // dragging a marker
const int graph_type = m_graph_type;
const int mi = graphs.m_mouse.down_marker;
{ // delete the marker if it's been dragged off the graph
const int64_t Hz = settings.m_markers_freq[mi].Hz;
if (Hz < data_unit.m_freq_start_Hz || Hz > data_unit.m_freq_stop_Hz)
{
settings.m_markers_freq.erase(settings.m_markers_freq.begin() + mi);
// deselect all markers
// for (int k = 0; k < MarkerListBox->Items->Count; k++)
// MarkerListBox->Selected[k] = false;
}
}
// buildMarkerListBox();
// ClearMarkersButton->Enabled = !settings.m_markers_freq.empty();
if (graphs.isFrequencyGraph(graph_type))
{
}
else
if (graphs.isSmithGraph(graph_type) || graphs.isAdmittanceGraph(graph_type) || graphs.isPolarGraph(graph_type))
{
}
else
if (graphs.isTDRGraph(graph_type))
{
}
}
graphs.m_mouse.graph_scale_pos_drag = -1;
graphs.m_mouse.graph_v_scale_drag = -1;
graphs.m_mouse.graph_h_scale_drag = -1;
graphs.m_mouse.graph_drag = -1;
graphs.m_mouse.down_graph = -1;
graphs.m_mouse.down_x = -1;