forked from jedi98/sark-100-antenna-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
587 lines (464 loc) · 17.7 KB
/
Copy pathmainwindow.cpp
File metadata and controls
587 lines (464 loc) · 17.7 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
/*
(C) Copyright 2015 Jeremy Burton
This file is part of Sark-100-antenna-analyzer.
Sark-100-antenna-analyzer is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Sark-100-antenna-analyzer is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <locale.h>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QClipboard>
#include "scandata.h"
#include "settingsdlg.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
const Version
MainWindow::version = Version(0,11,15,"");
ScanData scandata;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
setlinebuf(stdout);
//{
//char *p;
//printf("setlocale=%p\n",p=setlocale(LC_ALL, "sv_SE.UTF-8"));
//if (p) printf("\t%s\n",p);
//std::setlocale(LC_ALL, "sv_SE");
//}
Config::read();
ui->setupUi(this);
ui->statusBar->addWidget(ui->progressBar);
setWindowTitle(Config::App);
connect(ui->actionLoad, SIGNAL(triggered()), this, SLOT(Slot_Load()));
connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(Slot_Save()));
connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(Slot_Settings()));
connect(ui->actionQuit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(ui->actionAbout_QT, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(ui->actionAbout_Analyzer, SIGNAL(triggered()), this, SLOT(Slot_about()));
connect(ui->menuDevice, SIGNAL(aboutToShow()), this, SLOT(Slot_menuDevice_Show()));
connect(ui->menuDevice, SIGNAL(triggered(QAction *)), this, SLOT(Slot_menuDevice_Select(QAction *)));
connect(ui->canvas1, SIGNAL(cursorMoved(double)), this, SLOT(Slot_cursor_move(double)));
connect(ui->scanBtn,SIGNAL(clicked()),this,SLOT(Slot_scanBtn_click()));
connect(ui->scanDummyBtn,SIGNAL(clicked()),this,SLOT(Slot_scanDummyBtn_click()));
connect(ui->copyBtn, SIGNAL(clicked()), this, SLOT(Slot_copy()));
connect(ui->band_cb, SIGNAL(currentIndexChanged(int)), this, SLOT(Slot_band_change(int)));
connect(ui->fcentre, SIGNAL(valueChanged(double)), this, SLOT(Slot_fcentre_change(double)));
connect(ui->fspan, SIGNAL(valueChanged(double)), this, SLOT(Slot_fspan_change(double)));
connect(ui->point_count, SIGNAL(valueChanged(int)), this, SLOT(Slot_point_count_change(int)));
connect(ui->monStartBtn,SIGNAL(clicked()),this,SLOT(Slot_monStart_click()));
connect(ui->monStopBtn,SIGNAL(clicked()),this,SLOT(Slot_monStop_click()));
QCheckBox *ctrls[] = {ui->plotz_chk,ui->plotx_chk,ui->plotr_chk,NULL};
for (int i=0; ctrls[i]; i++)
connect(ctrls[i], SIGNAL(stateChanged(int)), this, SLOT(Slot_plot_change(int)));
link = new SerialLink("/dev/ttyUSB0",57600);
if (link->IsUp())
link->Cmd_Off();
ui->band_cb->setCurrentIndex(16);
ui->scan_data->setHorizontalHeaderLabels(QStringList() << "freq" << "SWR" << "Z" << "R" << "X");
#ifdef ENABLE_TEST_DATA
Slot_scanDummyBtn_click();
#else
ui->scanDummyBtn->setVisible(false);
#endif
ui->canvas1->cursor = ui->cursor;
ui->tabWidget->setCurrentIndex(1);
//ui->tabWidget->setTabEnabled(0,false);
montimer.setParent(this);
connect(&montimer, SIGNAL(timeout()), this, SLOT(Slot_montimer_timeout()));
//std::setlocale(LC_ALL, "sv_SE.UTF-8");
}
MainWindow::~MainWindow()
{
delete link;
delete ui;
}
void MainWindow::RaiseEvent(event_t event,int arg)
{
switch (event)
{
case progress_event:
ui->progressBar->setValue(arg);
//draw_graph1();
break;
}
}
void MainWindow::populate_table()
{
//Populate data table
ui->scan_data->setRowCount(scandata.points.size());
for (unsigned int i=0;i<scandata.points.size();i++)
{
Sample *point = &scandata.points[i];
ui->scan_data->setItem(i, 0, new QTableWidgetItem(QString("%1").arg(point->freq/1000000.0,0,'f')));
ui->scan_data->setItem(i, 1, new QTableWidgetItem(QString("%1").arg(point->swr)));
ui->scan_data->setItem(i, 2, new QTableWidgetItem(QString("%1").arg(point->Z)));
ui->scan_data->setItem(i, 3, new QTableWidgetItem(QString("%1").arg(point->R)));
ui->scan_data->setItem(i, 4, new QTableWidgetItem(QString("%1").arg(point->X)));
//ui->scan_data->setItem(i, 5, new QTableWidgetItem(QString("%1").arg(point->X2)));
}
}
void MainWindow::draw_graph1()
{
GraphScale *scale;
//double n;
scale = ui->canvas1->xscale;
scale->vmin = scandata.freq_start;
scale->vmax = scandata.freq_end;
scale->SetIncAuto();
scale = ui->canvas1->yscale1;
scale->vmin = 1.0;
scale->vmax = scandata.points[scandata.swr_max_idx].swr>Config::swr_max ? Config::swr_max : scandata.points[scandata.swr_max_idx].swr;
scale->SetIncAuto();
scale = ui->canvas1->yscale2;
scale->vmin = 0.0; //scandata.points[scandata.Z_min_idx].Z;
scale->vmax = 0.0;
if (ui->canvas1->ztrace->enabled && scandata.points[scandata.Z_max_idx].Z>scale->vmax) scale->vmax=scandata.points[scandata.Z_max_idx].Z;
//if (ui->canvas1->xtrace->enabled && scandata.points[scandata.X_max_idx].X>scale->vmax) scale->vmax=scandata.points[scandata.X_max_idx].X;
if (ui->canvas1->xtrace->enabled) scale->Expand(scandata.points[scandata.X_min_idx].X,scandata.points[scandata.X_max_idx].X);
if (ui->canvas1->rtrace->enabled && scandata.points[scandata.R_max_idx].R>scale->vmax) scale->vmax=scandata.points[scandata.R_max_idx].R;
if (scale->vmax==0.0) scale->vmax=1.0;
scale->SetIncAuto();
scale->SetMinAuto();
ui->canvas1->swrtrace->points.resize(scandata.points.size());
for (unsigned int i=0;i<scandata.points.size();i++)
ui->canvas1->swrtrace->points[i] = scandata.points[i].swr;
ui->canvas1->ztrace->points.resize(scandata.points.size());
for (unsigned int i=0;i<scandata.points.size();i++)
ui->canvas1->ztrace->points[i] = scandata.points[i].Z;
ui->canvas1->xtrace->points.resize(scandata.points.size());
for (unsigned int i=0;i<scandata.points.size();i++)
ui->canvas1->xtrace->points[i] = scandata.points[i].X;
ui->canvas1->rtrace->points.resize(scandata.points.size());
for (unsigned int i=0;i<scandata.points.size();i++)
ui->canvas1->rtrace->points[i] = scandata.points[i].R;
ui->canvas1->ZTargetline->val = Config::Z_Target;
ui->canvas1->SWRTargetline->val = Config::swr_bw_max;
ui->canvas1->swrminline->val = scandata.points[scandata.swr_min_idx].freq;
ui->canvas1->update();
//Show stats at the bottom of the graph
ui->swr_min_disp->setText(QString("%1 (f=%2MHz, Z=%3%4, bw=%5MHz)")
.arg(scandata.points[scandata.swr_min_idx].swr,0,'f',2)
.arg(scandata.points[scandata.swr_min_idx].freq/1000000)
.arg(scandata.points[scandata.swr_min_idx].Z,0,'f',2).arg(QChar(0x03A9))
.arg((scandata.points[scandata.swr_bw_hi_idx].freq-scandata.points[scandata.swr_bw_lo_idx].freq)/1000000,0,'f',2));
}
void MainWindow::Slot_scanBtn_click()
{
scandata.freq_start = (ui->fcentre->value()-ui->fspan->value()/2.0)*1000000;
scandata.freq_end = (ui->fcentre->value()+ui->fspan->value()/2.0)*1000000;
scandata.SetPointCount(ui->point_count->value());
if (link->IsUp())
{
link->Cmd_Scan((long)(scandata.freq_start),
(long)(scandata.freq_end),
(long)((scandata.freq_end-scandata.freq_start)/scandata.GetPointCount()),
ui->useraw_chk->checkState()==Qt::Checked,
this);
link->Cmd_Off();
}
populate_table();
draw_graph1();
}
#ifdef ENABLE_TEST_DATA
void MainWindow::Slot_scanDummyBtn_click()
{
scandata.freq_start = (ui->fcentre->value()-ui->fspan->value()/2.0)*1000000;
scandata.freq_end = (ui->fcentre->value()+ui->fspan->value()/2.0)*1000000;
scandata.SetPointCount(ui->point_count->value());
scandata.dummy_data(this);
populate_table();
draw_graph1();
}
#endif
void MainWindow::Slot_cursor_move(double pos)
{
//printf("pos=%lf\n",pos);
int n = pos*(double)(scandata.points.size()-1);
if (n<0 || n>=scandata.GetPointCount())
return;
Sample *sample = &scandata.points[n];
ui->cursor_disp->setText(QString("f=%1MHz, swr=%2, Z=%3%4")
.arg(sample->freq/1000000)
.arg(sample->swr,0,'f',2)
.arg(sample->Z,0,'f',2).arg(QChar(0x03A9)));
}
void MainWindow::set_band(double f, double span)
{
ui->fcentre->setValue(f);
ui->fspan->setValue(span);
}
void MainWindow::set_scan_disp()
{
ui->fstart_disp->setText(QString("%1").arg(ui->fcentre->value()-ui->fspan->value()/2.0));
ui->fend_disp->setText(QString("%1").arg(ui->fcentre->value()+ui->fspan->value()/2.0));
ui->fstep_disp->setText(QString("%1").arg(ui->fspan->value()*1000.0/(double)ui->point_count->value()));
}
void MainWindow::Slot_band_change(int idx)
{
ui->band_cb->blockSignals(true);
ui->fcentre->blockSignals(true);
ui->fspan->blockSignals(true);
switch (idx)
{
case 0: break;
case 1: set_band(1.5, 1.0); break; //160m
case 2: set_band(3.5, 3.0); break; //80m
case 3: set_band(6.5, 3.0); break; //40m
case 4: set_band(9.5, 3.0); break; //30m
case 5: set_band(12.0, 2.0); break; //25m
case 6: set_band(15.0, 4.0); break; //20m
case 7: set_band(18.0, 2.0); break; //17m
case 8: set_band(21.0, 4.0); break; //15m
case 9: set_band(24.5, 3.0); break; //12m
case 10: set_band(27.0, 2.0); break; //11m
case 11: set_band(29.5, 3.0); break; //10m
case 12: set_band(40, 18.0); break; //8m
case 13: set_band(51, 4.0); break; //6m
case 14: set_band(30.5, 59.0); break;
case 15: set_band(16.5, 27.0); break;
case 16: set_band(27.5, 5.0); break;
}
ui->band_cb->blockSignals(false);
ui->fcentre->blockSignals(false);
ui->fspan->blockSignals(false);
set_scan_disp();
}
void MainWindow::Slot_fcentre_change(double v)
{
ui->fcentre->blockSignals(true);
double n = ui->fspan->value()/2.0;
if ((v-n)<1.0) { v=1.0+n; ui->fcentre->setValue(v); }
if ((v+n)>60.0) { v=60.0-n; ui->fcentre->setValue(v); }
ui->band_cb->setCurrentIndex(0);
ui->fcentre->blockSignals(false);
set_scan_disp();
}
void MainWindow::Slot_fspan_change(double v)
{
ui->fspan->blockSignals(true);
double centre = ui->fcentre->value();
if ((centre-v/2.0)<1.0) { v=2.0*(centre-1); ui->fspan->setValue(v); }
if ((centre+v/2.0)>60.0) { v=2.0*(60.0-centre); ui->fspan->setValue(v); }
ui->band_cb->setCurrentIndex(0);
ui->fspan->blockSignals(false);
set_scan_disp();
}
void MainWindow::Slot_point_count_change(int)
{
set_scan_disp();
}
void MainWindow::Slot_plot_change(int)
{
ui->canvas1->ztrace->enabled = ui->plotz_chk->checkState()==Qt::Checked;
ui->canvas1->xtrace->enabled = ui->plotx_chk->checkState()==Qt::Checked;
ui->canvas1->rtrace->enabled = ui->plotr_chk->checkState()==Qt::Checked;
draw_graph1();
// ui->canvas1->update();
}
void MainWindow::Slot_Load()
{
QString fileName = QFileDialog::getOpenFileName(this,"Open Layout",Config::dir_data,"Scan Data (*.analyzer);;All files (*)");
if (!fileName.isEmpty())
{
QDomDocument doc( "AnalyzerML" );
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this, tr("Analyzer"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
if (!doc.setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
if (root.tagName() != "analyzer")
{
QMessageBox::warning(this, "Analyzer",
"Cannot load file\nThis is not a valid analyzer file.");
return;
}
fromDom(root);
//setCurrentFile(fileName);
//fileName.replace('\\','/');
//addRecentFile(fileName);
QFileInfo fi(fileName);
if (Config::dir_data != fi.dir().path())
{
Config::dir_data = fi.dir().path();
Config::write();
}
populate_table();
draw_graph1();
ui->fcentre->setValue((scandata.freq_end+scandata.freq_start)/2000000.0);
ui->fspan->setValue((scandata.freq_end-scandata.freq_start)/1000000.0);
statusBar()->showMessage(tr("Analyzer data loaded"), 5000);
}
}
void MainWindow::fromDom(QDomElement &e0)
{
//freq_start = e0.attribute("fstart", "0").toDouble();
for (QDomNode n1 = e0.firstChild(); !n1.isNull(); n1 = n1.nextSibling())
{
if (!n1.isElement()) continue; // Skip any non-element nodes
QDomElement e1 = n1.toElement();
if (e1.tagName() == "scandata")
scandata.fromDom(e1);
else if (e1.tagName() == "notes")
ui->notes_txt->setPlainText(e1.text());
}
}
void MainWindow::toDom(QDomDocument &doc)
{
QDomElement element = doc.createElement("analyzer");
toDom_Text(doc,element,"notes",ui->notes_txt->toPlainText());
scandata.toDom(doc,element);
doc.appendChild(element);
}
void MainWindow::Slot_Save()
{
QFileInfo *fi;
QString filename = QFileDialog::getSaveFileName(this,"Save Scan Data As",Config::dir_data,"Scan Data (*.analyzer);;All files (*)");
if (filename.isEmpty())
return;
// if (!copy)
// setCurrentFile(fileName); // Set filename & layoutname here because layoutname is written to the file.
fi = new QFileInfo(filename);
if (fi->completeSuffix().isEmpty())
filename += ".analyzer";
delete fi;
QDomDocument doc( "AnalyzerML" );
toDom(doc);
QFile file(filename);
if( !file.open( QIODevice::WriteOnly ) )
return;
QTextStream ts( &file );
ts.setCodec(Config::DOM_ENCODING); //If we always save as UTF-8 unicode filenames should work. Othewise on windows it saves in some other encoding.
ts << doc.toString();
file.close();
//statusBar()->showMessage(tr("Data saved"), 2000);
fi = new QFileInfo(filename);
if (Config::dir_data != fi->dir().path())
{
Config::dir_data = fi->dir().path();
Config::write();
}
delete fi;
}
void MainWindow::Slot_menuDevice_Show()
{
unsigned int i;
//QDir dir("/dev","ttyUSB*");
QDir dir("/dev",Config::SERIAL_DEV_FILTER,QDir::Name | QDir::IgnoreCase,QDir::System);
printf("dir: %d\n",dir.count());
ui->menuDevice->clear();
for (i=0;i<dir.count();i++)
ui->menuDevice->addAction(dir[i]);
}
void MainWindow::Slot_menuDevice_Select(QAction *act)
{
if (link)
delete link;
link = new SerialLink(QString("/dev/" + act->text()).toLatin1().data(),57600);
if (link->IsUp())
link->Cmd_Off();
}
void MainWindow::Slot_about()
{
QMessageBox::about(this, "About Antenna Analyzer",
QString("Antenna Analyzer - scanning & graph plotting program for the SARK100 antenna analyzer.<br><br>"
"Version %1.%2.%3 %4<br><br>"
"© 2015 jedi98.uk").arg(version.major).arg(version.minor).arg(version.build).arg(version.subversion));
}
void MainWindow::Slot_Settings()
{
SettingsDlg dlg;
if (dlg.exec() == QDialog::Accepted)
{
scandata.UpdateStats();
draw_graph1();
}
}
void MainWindow::Slot_copy()
{
QString txt("freq\tSWR\tZ\tR\tX\n");
for (unsigned int i=0;i<scandata.points.size();i++)
txt += QString("%1\t%2\t%3\t%4\t%5\n")
.arg(scandata.points[i].freq/1000000.0,0,'f')
.arg(scandata.points[i].swr)
.arg(scandata.points[i].Z)
.arg(scandata.points[i].R)
.arg(scandata.points[i].X);
qApp->clipboard()->setText(txt);
}
void MainWindow::Slot_monStart_click()
{
ui->SWR_Bar->vmin = ui->SWR_Bar->vorig = 1;
ui->SWR_Bar->vmax = 10;
ui->SWR_Bar->SetIncAuto();
ui->SWR_Bar->value = 1;
ui->Z_Bar->brush = QBrush(qRgb(255,85,0));
ui->Z_Bar->vmin = 0;
ui->Z_Bar->vmax = 200;
ui->Z_Bar->value = 50;
ui->Z_Bar->SetIncAuto();
ui->R_Bar->brush = QBrush(Qt::darkGreen);
ui->R_Bar->vmin = 0;
ui->R_Bar->vmax = 200;
ui->R_Bar->value = 50;
ui->R_Bar->SetIncAuto();
ui->X_Bar->brush = QBrush(Qt::red);
ui->X_Bar->vmin = -100;
ui->X_Bar->vmax = 200;
ui->X_Bar->value = 0;
ui->X_Bar->SetIncAuto();
if (link->IsUp())
{
link->Cmd_Freq((long)(ui->monfreq->value()*1000000));
link->Cmd_On();
}
Slot_montimer_timeout();
montimer.start((long)(ui->monrate->value()));
}
void MainWindow::Slot_monStop_click()
{
montimer.stop();
if (link->IsUp())
link->Cmd_Off();
}
void MainWindow::Slot_montimer_timeout()
{
Sample sample;
if (link->IsUp())
{
link->Cmd_Raw(sample);
ui->SWR_lbl->setText(QString("%1:1").arg(sample.swr, 0,'f',1));
ui->SWR_Bar->value = sample.swr;
ui->SWR_Bar->update();
ui->Z_lbl->setText(QString("%1").arg(sample.Z, 0,'f',1));
ui->Z_Bar->value = sample.Z;
ui->Z_Bar->update();
ui->R_lbl->setText(QString("%1").arg(sample.R, 0,'f',1));
ui->R_Bar->value = sample.R;
ui->R_Bar->update();
ui->X_lbl->setText(QString("%1").arg(sample.X, 0,'f',1));
ui->X_Bar->value = sample.X;
ui->X_Bar->update();
}
}