-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
973 lines (830 loc) · 31.5 KB
/
Copy pathmainwindow.cpp
File metadata and controls
973 lines (830 loc) · 31.5 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
#include "mainwindow.h"
#include "wifi_scanner.h"
#include <QtCharts/QAreaSeries>
#include <QtCharts/QCategoryAxis>
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
#include <QColor>
#include <QComboBox>
#include <QEvent>
#include <QHeaderView>
#include <QIcon>
#include <QHBoxLayout>
#include <QLabel>
#include <QMouseEvent>
#include <QPainter>
#include <QPen>
#include <QPixmap>
#include <QPushButton>
#include <QScrollBar>
#include <QSignalBlocker>
#include <QStackedWidget>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QToolButton>
#include <QToolTip>
#include <QVBoxLayout>
#include <QThread>
#include <QtMath>
#include <algorithm>
#include <utility>
namespace {
constexpr int DefaultScanIntervalMs = 30000;
constexpr int CombinedBandMinimumWidth = 980;
QColor blendWithWindow(const QColor &base, bool darkMode)
{
return darkMode ? base.lighter(150) : base.darker(105);
}
QColor distinctColorForIndex(int index)
{
// Spread hues around the wheel with a golden-angle step so nearby rows still look distinct.
const int hue = (37 + index * 137) % 360;
const int saturation = 185 + (index % 3) * 20;
const int value = 210 + (index % 2) * 25;
return QColor::fromHsv(hue, qMin(saturation, 255), qMin(value, 255));
}
class SortableTableWidgetItem : public QTableWidgetItem
{
public:
using QTableWidgetItem::QTableWidgetItem;
bool operator<(const QTableWidgetItem &other) const override
{
const QVariant leftValue = data(Qt::UserRole);
const QVariant rightValue = other.data(Qt::UserRole);
if (leftValue.isValid() && rightValue.isValid()) {
return leftValue.toDouble() < rightValue.toDouble();
}
return QTableWidgetItem::operator<(other);
}
};
QVector<int> standardChannelsForBand(WiFiBand band)
{
QVector<int> channels;
if (band == WiFiBand::Band24) {
for (int channel = 1; channel <= 14; ++channel) {
channels.append(channel);
}
return channels;
}
if (band == WiFiBand::Band5) {
return {36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 149, 153, 157, 161, 165, 169, 173};
}
for (int channel = 1; channel <= 233; channel += 4) {
channels.append(channel);
}
return channels;
}
QVector<WiFiBand> activeBandsFromFlags(bool show24GHz, bool show5GHz, bool show6GHz)
{
QVector<WiFiBand> bands;
if (show24GHz) {
bands.append(WiFiBand::Band24);
}
if (show5GHz) {
bands.append(WiFiBand::Band5);
}
if (show6GHz) {
bands.append(WiFiBand::Band6);
}
if (bands.isEmpty()) {
bands.append(WiFiBand::Band24);
}
return bands;
}
qreal lowerChannelBoundary(int channel, WiFiBand band)
{
return band == WiFiBand::Band24 ? channel - 0.5 : channel - 2.0;
}
qreal upperChannelBoundary(int channel, WiFiBand band)
{
return band == WiFiBand::Band24 ? channel + 0.5 : channel + 2.0;
}
void clearCategoryAxis(QCategoryAxis *axis)
{
const QStringList labels = axis->categoriesLabels();
for (const QString &label : labels) {
axis->remove(label);
}
}
} // namespace
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi();
updateBandButtonVisibility();
syncBandButtons();
updateChartTheme();
scanTimer = new QTimer(this);
connect(scanTimer, &QTimer::timeout, this, &MainWindow::refreshScan);
scanTimer->start(DefaultScanIntervalMs);
scanSpinnerTimer = new QTimer(this);
connect(scanSpinnerTimer, &QTimer::timeout, this, &MainWindow::updateScanSpinner);
refreshScan();
}
MainWindow::~MainWindow()
{
if (scanThread != nullptr) {
scanThread->wait(2000);
}
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (watched == chartView->viewport()) {
if (event->type() == QEvent::MouseMove) {
const auto *mouseEvent = static_cast<QMouseEvent *>(event);
updateHoverTooltip(mouseEvent->pos());
} else if (event->type() == QEvent::Leave) {
QToolTip::hideText();
}
}
return QMainWindow::eventFilter(watched, event);
}
void MainWindow::changeEvent(QEvent *event)
{
if (event->type() == QEvent::PaletteChange) {
updateChartTheme();
}
QMainWindow::changeEvent(event);
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
updateCombinedBandAvailability();
}
void MainWindow::setupUi()
{
setWindowTitle("WiFiperson");
resize(1180, 760);
pageStack = new QStackedWidget(this);
listPage = new QWidget(this);
detailPage = new QWidget(this);
chart = new QChart();
chart->legend()->hide();
chart->setAnimationOptions(QChart::SeriesAnimations);
axisX = new QCategoryAxis();
axisX->setTitleText("Wi-Fi Channel");
axisX->setLabelsPosition(QCategoryAxis::AxisLabelsPositionCenter);
axisY = new QValueAxis();
axisY->setTitleText("Signal Strength (dBm)");
axisY->setLabelFormat("%d");
axisY->setRange(-100, -20);
axisY->setTickCount(9);
chart->addAxis(axisX, Qt::AlignBottom);
chart->addAxis(axisY, Qt::AlignLeft);
chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->setMouseTracking(true);
chartView->viewport()->setMouseTracking(true);
chartView->viewport()->installEventFilter(this);
invalidStateLabel = new QLabel(this);
invalidStateLabel->setAlignment(Qt::AlignCenter);
invalidStateLabel->setWordWrap(true);
invalidStateLabel->hide();
statusLabel = new QLabel(this);
statusLabel->setWordWrap(true);
statusLabel->hide();
refreshButton = new QPushButton("Refresh Scan", this);
refreshButton->setMinimumHeight(44);
connect(refreshButton, &QPushButton::clicked, this, &MainWindow::refreshScan);
band24Button = new QToolButton(this);
band24Button->setMinimumSize(88, 44);
band24Button->setText("2.4 GHz");
band24Button->setCheckable(true);
connect(band24Button, &QToolButton::toggled, this, [this](bool checked) {
if (!checked) {
if (show24GHz && !show5GHz && !show6GHz) {
const QSignalBlocker blocker(band24Button);
band24Button->setChecked(true);
}
return;
}
show24GHz = checked;
show5GHz = false;
show6GHz = false;
syncBandButtons();
applyFilters();
});
band5Button = new QToolButton(this);
band5Button->setMinimumSize(88, 44);
band5Button->setText("5 GHz");
band5Button->setCheckable(true);
connect(band5Button, &QToolButton::toggled, this, [this](bool checked) {
if (!checked) {
if (!show24GHz && show5GHz && !show6GHz) {
const QSignalBlocker blocker(band5Button);
band5Button->setChecked(true);
}
return;
}
show5GHz = checked;
show24GHz = false;
show6GHz = false;
syncBandButtons();
applyFilters();
});
band6Button = new QToolButton(this);
band6Button->setMinimumSize(88, 44);
band6Button->setText("6 GHz");
band6Button->setCheckable(true);
connect(band6Button, &QToolButton::toggled, this, [this](bool checked) {
if (!checked) {
if (!show24GHz && !show5GHz && show6GHz) {
const QSignalBlocker blocker(band6Button);
band6Button->setChecked(true);
}
return;
}
show6GHz = checked;
show24GHz = false;
show5GHz = false;
syncBandButtons();
applyFilters();
});
combinedBandButton = new QToolButton(this);
combinedBandButton->setMinimumSize(112, 44);
combinedBandButton->setText("2.4 + 5 GHz");
combinedBandButton->setCheckable(true);
connect(combinedBandButton, &QToolButton::toggled, this, [this](bool checked) {
if (!checked) {
if (show24GHz && show5GHz && !show6GHz) {
const QSignalBlocker blocker(combinedBandButton);
combinedBandButton->setChecked(true);
}
return;
}
show24GHz = true;
show5GHz = true;
show6GHz = false;
syncBandButtons();
applyFilters();
});
scanIntervalCombo = new QComboBox(this);
scanIntervalCombo->setMinimumHeight(44);
scanIntervalCombo->addItem("Poll: 30s", DefaultScanIntervalMs);
scanIntervalCombo->addItem("Poll: 60s", 60000);
scanIntervalCombo->addItem("Poll: 5 min", 300000);
scanIntervalCombo->addItem("Poll: Off", 0);
connect(scanIntervalCombo, qOverload<int>(&QComboBox::currentIndexChanged), this, &MainWindow::updateScanInterval);
auto *controlsLayout = new QHBoxLayout();
controlsLayout->addWidget(refreshButton);
controlsLayout->addWidget(band24Button);
controlsLayout->addWidget(band5Button);
controlsLayout->addWidget(combinedBandButton);
controlsLayout->addWidget(band6Button);
controlsLayout->addWidget(scanIntervalCombo);
controlsLayout->addStretch(1);
networkTable = new QTableWidget(this);
networkTable->setColumnCount(7);
networkTable->setHorizontalHeaderLabels(
{"Color", "SSID", "BSSID", "Band", "Channel", "Signal (dBm)", "Frequency (MHz)"});
networkTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
networkTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
networkTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
networkTable->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
networkTable->horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeToContents);
networkTable->horizontalHeader()->setSectionResizeMode(5, QHeaderView::ResizeToContents);
networkTable->horizontalHeader()->setSectionResizeMode(6, QHeaderView::ResizeToContents);
networkTable->verticalHeader()->setVisible(false);
networkTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
networkTable->setSelectionBehavior(QAbstractItemView::SelectRows);
networkTable->setAlternatingRowColors(true);
networkTable->setSortingEnabled(true);
networkTable->horizontalHeader()->setSortIndicatorShown(true);
networkTable->verticalHeader()->setDefaultSectionSize(44);
networkTable->setIconSize(QSize(36, 36));
networkTable->setTextElideMode(Qt::ElideRight);
networkTable->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
networkTable->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
connect(networkTable, &QTableWidget::cellClicked, this, &MainWindow::showNetworkDetails);
auto *layout = new QVBoxLayout();
layout->addWidget(chartView, 3);
layout->addWidget(invalidStateLabel, 3);
layout->addLayout(controlsLayout);
layout->addWidget(statusLabel);
layout->addWidget(networkTable, 2);
listPage->setLayout(layout);
backButton = new QPushButton("Back to networks", this);
backButton->setMinimumHeight(44);
connect(backButton, &QPushButton::clicked, this, &MainWindow::showNetworkList);
detailTable = new QTableWidget(this);
detailTable->setColumnCount(2);
detailTable->setHorizontalHeaderLabels({"Property", "Value"});
detailTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
detailTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
detailTable->verticalHeader()->setVisible(false);
detailTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
detailTable->setSelectionBehavior(QAbstractItemView::SelectRows);
detailTable->setAlternatingRowColors(true);
detailTable->verticalHeader()->setDefaultSectionSize(44);
detailTable->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
detailTable->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
interferenceLabel = new QLabel("Interfering SSIDs", this);
interferenceLabel->setWordWrap(true);
interferenceTable = new QTableWidget(this);
interferenceTable->setColumnCount(5);
interferenceTable->setHorizontalHeaderLabels({"SSID", "BSSID", "Band", "Channel", "Signal (dBm)"});
interferenceTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
interferenceTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
interferenceTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
interferenceTable->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
interferenceTable->horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeToContents);
interferenceTable->verticalHeader()->setVisible(false);
interferenceTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
interferenceTable->setSelectionBehavior(QAbstractItemView::SelectRows);
interferenceTable->setAlternatingRowColors(true);
interferenceTable->verticalHeader()->setDefaultSectionSize(44);
interferenceTable->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
interferenceTable->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
auto *detailLayout = new QVBoxLayout();
detailLayout->addWidget(backButton);
detailLayout->addWidget(detailTable, 1);
detailLayout->addWidget(interferenceLabel);
detailLayout->addWidget(interferenceTable, 1);
detailPage->setLayout(detailLayout);
pageStack->addWidget(listPage);
pageStack->addWidget(detailPage);
pageStack->setCurrentWidget(listPage);
setCentralWidget(pageStack);
setStyleSheet(QStringLiteral(
"QPushButton, QToolButton { padding: 10px 16px; font-size: 15px; }"
"QTableView { font-size: 15px; }"
"QHeaderView::section { padding: 8px; font-size: 14px; }"));
}
void MainWindow::refreshScan()
{
if (scanInProgress) {
refreshQueued = true;
return;
}
scanInProgress = true;
refreshQueued = false;
setRefreshButtonLoading(true);
setStatusMessage(QString());
scanThread = new WiFiScanThread(this);
connect(scanThread, &WiFiScanThread::scanFinished, this, &MainWindow::handleScanFinished);
connect(scanThread, &WiFiScanThread::scanError, this, &MainWindow::handleScanError);
connect(scanThread, &QThread::finished, scanThread, &QObject::deleteLater);
connect(scanThread, &QThread::finished, this, [this]() { scanThread = nullptr; });
scanThread->start();
}
void MainWindow::handleScanFinished(const QVector<WiFiNetwork> &networks, const BandSupport &support)
{
scanInProgress = false;
restoreRefreshButton();
bandSupport = support;
finishScanWithNetworks(networks);
if (networks.isEmpty()) {
setStatusMessage(
"Scan completed, but no SSIDs were found. The channel axis is still shown for the selected band.");
}
if (scanThread != nullptr) {
scanThread->quit();
}
if (refreshQueued) {
refreshQueued = false;
refreshScan();
}
}
void MainWindow::handleScanError(const QString &message, const BandSupport &support)
{
scanInProgress = false;
restoreRefreshButton();
bandSupport = support;
updateBandButtonVisibility();
syncBandButtons();
if (message.contains("Turn on Wi-Fi", Qt::CaseInsensitive)) {
showInvalidState(message);
} else if (!allNetworks.isEmpty()) {
invalidStateLabel->hide();
chartView->show();
networkTable->show();
setStatusMessage(QString());
} else {
showInvalidState(QString("Wi-Fi scanning failed.\n\n%1").arg(message));
}
if (scanThread != nullptr) {
scanThread->quit();
}
if (refreshQueued) {
refreshQueued = false;
refreshScan();
}
}
QVector<WiFiNetwork> MainWindow::filteredNetworks() const
{
QVector<WiFiNetwork> filtered;
for (const WiFiNetwork &network : allNetworks) {
if (network.band == WiFiBand::Band24 && show24GHz) {
filtered.append(network);
} else if (network.band == WiFiBand::Band5 && show5GHz) {
filtered.append(network);
} else if (network.band == WiFiBand::Band6 && show6GHz) {
filtered.append(network);
}
}
return filtered;
}
QVector<QPointF> MainWindow::buildSpectrumEnvelope(const WiFiNetwork &network) const
{
const qreal centerChannel = network.channel;
const qreal halfWidth = 2.0;
const qreal baseLevel = -100.0;
const qreal shoulderLevel = qMax(baseLevel, network.signalDbm - 12.0);
return {
QPointF(centerChannel - halfWidth, baseLevel),
QPointF(centerChannel - halfWidth * 0.5, shoulderLevel),
QPointF(centerChannel, network.signalDbm),
QPointF(centerChannel + halfWidth * 0.5, shoulderLevel),
QPointF(centerChannel + halfWidth, baseLevel)
};
}
QVector<WiFiNetwork> MainWindow::interferingNetworks(const WiFiNetwork &selected) const
{
QVector<WiFiNetwork> interferers;
const qreal selectedLower = lowerChannelBoundary(selected.channel, selected.band);
const qreal selectedUpper = upperChannelBoundary(selected.channel, selected.band);
for (const WiFiNetwork &candidate : allNetworks) {
if (candidate.bssid == selected.bssid || candidate.band != selected.band) {
continue;
}
const qreal candidateLower = lowerChannelBoundary(candidate.channel, candidate.band);
const qreal candidateUpper = upperChannelBoundary(candidate.channel, candidate.band);
if (candidateLower <= selectedUpper && candidateUpper >= selectedLower) {
interferers.append(candidate);
}
}
std::sort(interferers.begin(), interferers.end(), [](const WiFiNetwork &left, const WiFiNetwork &right) {
if (left.signalDbm == right.signalDbm) {
return left.channel < right.channel;
}
return left.signalDbm > right.signalDbm;
});
return interferers;
}
QString MainWindow::bandLabel(WiFiBand band) const
{
switch (band) {
case WiFiBand::Band24:
return "2.4 GHz";
case WiFiBand::Band5:
return "5 GHz";
case WiFiBand::Band6:
return "6 GHz";
}
return "Unknown";
}
QString MainWindow::channelLabel(const WiFiNetwork &network) const
{
return QString::number(network.channel);
}
QColor MainWindow::colorForBand(WiFiBand band, int index) const
{
Q_UNUSED(band);
return distinctColorForIndex(index);
}
qreal MainWindow::horizontalDistanceToSegment(const QPointF &point, const QPointF &a, const QPointF &b) const
{
const QPointF ab = b - a;
const qreal abSquared = QPointF::dotProduct(ab, ab);
if (qFuzzyIsNull(abSquared)) {
return QLineF(point, a).length();
}
const qreal t = qBound<qreal>(0.0, QPointF::dotProduct(point - a, ab) / abSquared, 1.0);
const QPointF projection = a + ab * t;
return QLineF(point, projection).length();
}
void MainWindow::applyFilters()
{
visibleNetworks = filteredNetworks();
updateChart(visibleNetworks);
updateTable(visibleNetworks);
setStatusMessage(QString());
}
void MainWindow::finishScanWithNetworks(const QVector<WiFiNetwork> &networks)
{
allNetworks = networks;
invalidStateLabel->hide();
chartView->show();
networkTable->show();
for (const WiFiNetwork &network : allNetworks) {
if (network.band == WiFiBand::Band5) {
bandSupport.has5GHz = true;
} else if (network.band == WiFiBand::Band6) {
bandSupport.has6GHz = true;
}
}
updateBandButtonVisibility();
if (!band24Button->isVisible() && !band5Button->isVisible() && !band6Button->isVisible()) {
band24Button->setVisible(true);
bandSupport.has24GHz = true;
}
if (!show24GHz && !show5GHz && !show6GHz) {
show24GHz = bandSupport.has24GHz;
show5GHz = false;
show6GHz = false;
}
syncBandButtons();
applyFilters();
setStatusMessage(QString());
}
void MainWindow::showInvalidState(const QString &message)
{
allNetworks.clear();
visibleNetworks.clear();
chartView->hide();
networkTable->hide();
band24Button->hide();
band5Button->hide();
band6Button->hide();
combinedBandButton->hide();
invalidStateLabel->setText(message);
invalidStateLabel->show();
updateChart({});
updateTable({});
setStatusMessage(QString());
}
void MainWindow::restoreRefreshButton()
{
if (scanSpinnerTimer != nullptr) {
scanSpinnerTimer->stop();
}
refreshButton->setEnabled(true);
refreshButton->setText("Refresh Scan");
refreshButton->setIcon(QIcon());
}
void MainWindow::setRefreshButtonLoading(bool loading)
{
refreshButton->setEnabled(!loading);
if (!loading) {
restoreRefreshButton();
return;
}
scanSpinnerFrame = 0;
refreshButton->setText("Scanning");
updateScanSpinner();
scanSpinnerTimer->start(120);
}
void MainWindow::updateScanSpinner()
{
QPixmap pixmap(24, 24);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
QPen pen(palette().color(QPalette::ButtonText));
pen.setWidth(3);
pen.setCapStyle(Qt::RoundCap);
painter.setPen(pen);
painter.drawArc(QRect(4, 4, 16, 16), (scanSpinnerFrame * 30) * 16, 270 * 16);
refreshButton->setIcon(QIcon(pixmap));
scanSpinnerFrame = (scanSpinnerFrame + 1) % 12;
}
void MainWindow::syncBandButtons()
{
const QSignalBlocker block24(band24Button);
const QSignalBlocker block5(band5Button);
const QSignalBlocker block6(band6Button);
const QSignalBlocker blockCombined(combinedBandButton);
band24Button->setChecked(show24GHz && !show5GHz);
band5Button->setChecked(show5GHz && !show24GHz);
band6Button->setChecked(show6GHz);
combinedBandButton->setChecked(show24GHz && show5GHz && !show6GHz);
}
void MainWindow::updateBandButtonVisibility()
{
band24Button->setVisible(bandSupport.has24GHz);
band5Button->setVisible(bandSupport.has5GHz);
band6Button->setVisible(bandSupport.has6GHz);
updateCombinedBandAvailability();
}
void MainWindow::updateCombinedBandAvailability()
{
if (combinedBandButton == nullptr) {
return;
}
const bool combinedAllowed = width() >= CombinedBandMinimumWidth && bandSupport.has24GHz && bandSupport.has5GHz;
combinedBandButton->setVisible(combinedAllowed);
if (!combinedAllowed && show24GHz && show5GHz) {
show5GHz = false;
show6GHz = false;
syncBandButtons();
applyFilters();
}
}
void MainWindow::updateChart(const QVector<WiFiNetwork> &networks)
{
while (!chart->series().isEmpty()) {
QAbstractSeries *series = chart->series().constFirst();
chart->removeSeries(series);
delete series;
}
const qreal baseLevel = -100.0;
int colorIndex = 0;
int minSignal = 0;
int maxSignal = 0;
bool haveSignals = false;
for (const WiFiNetwork &network : networks) {
const QColor color = colorForBand(network.band, colorIndex++);
const QVector<QPointF> envelope = buildSpectrumEnvelope(network);
if (!haveSignals) {
minSignal = network.signalDbm;
maxSignal = network.signalDbm;
haveSignals = true;
} else {
minSignal = qMin(minSignal, network.signalDbm);
maxSignal = qMax(maxSignal, network.signalDbm);
}
auto *upperSeries = new QLineSeries();
upperSeries->append(envelope);
QPen pen(color);
pen.setWidth(3);
upperSeries->setPen(pen);
auto *lowerSeries = new QLineSeries();
lowerSeries->append(QPointF(envelope.first().x(), baseLevel));
lowerSeries->append(QPointF(envelope.last().x(), baseLevel));
auto *areaSeries = new QAreaSeries(upperSeries, lowerSeries);
areaSeries->setPen(pen);
QColor fill = color;
fill.setAlpha(55);
areaSeries->setBrush(fill);
chart->addSeries(areaSeries);
areaSeries->attachAxis(axisX);
areaSeries->attachAxis(axisY);
}
const QVector<WiFiBand> activeBands = activeBandsFromFlags(show24GHz, show5GHz, show6GHz);
clearCategoryAxis(axisX);
bool axisStarted = false;
qreal axisStart = 0.5;
qreal axisEnd = 14.5;
for (WiFiBand activeBand : activeBands) {
const QVector<int> standardChannels = standardChannelsForBand(activeBand);
for (int channel : standardChannels) {
const qreal lowerBoundary = lowerChannelBoundary(channel, activeBand);
const qreal upperBoundary = upperChannelBoundary(channel, activeBand);
if (!axisStarted) {
axisX->setStartValue(lowerBoundary);
axisStart = lowerBoundary;
axisStarted = true;
}
axisEnd = upperBoundary;
axisX->append(QString::number(channel), upperBoundary);
}
}
axisX->setRange(axisStart, axisEnd);
if (!haveSignals) {
axisY->setRange(-100, -20);
} else {
const int paddedMin = qMax(-100, static_cast<int>(qFloor((minSignal - 10) / 5.0) * 5.0));
const int paddedMax = qMin(-20, static_cast<int>(qCeil((maxSignal + 10) / 5.0) * 5.0));
axisY->setRange(paddedMin, qMax(paddedMax, paddedMin + 20));
}
chart->setTitle(QStringLiteral("Wi-Fi Spectrum Utilization (%1 networks)").arg(networks.size()));
}
void MainWindow::updateChartTheme()
{
const QPalette palette = this->palette();
const bool darkMode = palette.color(QPalette::Window).lightness() < 128;
const QColor windowColor = palette.color(QPalette::Window);
const QColor panelColor = blendWithWindow(windowColor, darkMode);
const QColor textColor = palette.color(QPalette::WindowText);
const QColor gridColor = darkMode ? QColor(255, 255, 255, 55) : QColor(0, 0, 0, 45);
chart->setBackgroundBrush(panelColor);
chart->setPlotAreaBackgroundVisible(true);
chart->setPlotAreaBackgroundBrush(darkMode ? QColor(18, 24, 33) : QColor(248, 250, 252));
chart->setTitleBrush(textColor);
axisX->setLabelsBrush(textColor);
axisX->setTitleBrush(textColor);
axisX->setGridLineColor(gridColor);
axisY->setLabelsBrush(textColor);
axisY->setTitleBrush(textColor);
axisY->setGridLineColor(gridColor);
}
void MainWindow::updateDetailTable(const WiFiNetwork &network)
{
const QVector<QPair<QString, QString>> rows = {
{QStringLiteral("SSID"), network.ssid},
{QStringLiteral("BSSID / access point MAC address"), network.bssid},
{QStringLiteral("Band"), bandLabel(network.band)},
{QStringLiteral("Channel"), channelLabel(network)},
{QStringLiteral("Frequency"), QStringLiteral("%1 MHz").arg(network.frequencyMHz)},
{QStringLiteral("Signal strength"), QStringLiteral("%1 dBm").arg(network.signalDbm)},
};
detailTable->setRowCount(rows.size());
for (int row = 0; row < rows.size(); ++row) {
detailTable->setItem(row, 0, new QTableWidgetItem(rows.at(row).first));
detailTable->setItem(row, 1, new QTableWidgetItem(rows.at(row).second));
}
const QVector<WiFiNetwork> interferers = interferingNetworks(network);
interferenceLabel->setText(
interferers.isEmpty()
? QStringLiteral("No overlapping SSIDs were found for this channel.")
: QStringLiteral("Interfering SSIDs (%1)").arg(interferers.size()));
interferenceTable->setRowCount(interferers.size());
for (int row = 0; row < interferers.size(); ++row) {
const WiFiNetwork &interferer = interferers.at(row);
interferenceTable->setItem(row, 0, new QTableWidgetItem(interferer.ssid));
interferenceTable->setItem(row, 1, new QTableWidgetItem(interferer.bssid));
interferenceTable->setItem(row, 2, new QTableWidgetItem(bandLabel(interferer.band)));
auto *channelItem = new SortableTableWidgetItem(channelLabel(interferer));
channelItem->setData(Qt::UserRole, interferer.channel);
interferenceTable->setItem(row, 3, channelItem);
auto *signalItem = new SortableTableWidgetItem(QString::number(interferer.signalDbm));
signalItem->setData(Qt::UserRole, interferer.signalDbm);
interferenceTable->setItem(row, 4, signalItem);
}
}
void MainWindow::showNetworkDetails(int row, int column)
{
Q_UNUSED(column);
QTableWidgetItem *bssidItem = networkTable->item(row, 2);
if (bssidItem == nullptr) {
return;
}
const QString bssid = bssidItem->text();
for (const WiFiNetwork &network : std::as_const(visibleNetworks)) {
if (network.bssid == bssid) {
updateDetailTable(network);
pageStack->setCurrentWidget(detailPage);
return;
}
}
}
void MainWindow::showNetworkList()
{
pageStack->setCurrentWidget(listPage);
}
void MainWindow::updateHoverTooltip(const QPoint &mousePos)
{
if (visibleNetworks.isEmpty()) {
QToolTip::hideText();
return;
}
const QRectF plotArea = chart->plotArea();
if (!plotArea.contains(mousePos)) {
QToolTip::hideText();
return;
}
const QPointF chartValue = chart->mapToValue(mousePos);
qreal bestDistance = 1e9;
QString bestLabel;
for (const WiFiNetwork &network : visibleNetworks) {
const QVector<QPointF> envelope = buildSpectrumEnvelope(network);
for (int index = 0; index + 1 < envelope.size(); ++index) {
const qreal distance = horizontalDistanceToSegment(chartValue, envelope.at(index), envelope.at(index + 1));
if (distance < bestDistance) {
bestDistance = distance;
bestLabel = QStringLiteral("%1\n%2").arg(network.ssid, network.bssid);
}
}
}
if (bestDistance < 1.2) {
QToolTip::showText(chartView->viewport()->mapToGlobal(mousePos + QPoint(16, 16)), bestLabel, chartView);
} else {
QToolTip::hideText();
}
}
void MainWindow::updateTable(const QVector<WiFiNetwork> &networks)
{
const bool sortingEnabled = networkTable->isSortingEnabled();
networkTable->setSortingEnabled(false);
networkTable->setRowCount(networks.size());
int colorIndex = 0;
for (int row = 0; row < networks.size(); ++row) {
const WiFiNetwork &network = networks.at(row);
const QColor color = colorForBand(network.band, colorIndex++);
auto *colorItem = new QTableWidgetItem();
colorItem->setBackground(color);
networkTable->setItem(row, 0, colorItem);
networkTable->setItem(row, 1, new QTableWidgetItem(network.ssid));
networkTable->setItem(row, 2, new QTableWidgetItem(network.bssid));
networkTable->setItem(row, 3, new QTableWidgetItem(bandLabel(network.band)));
auto *channelItem = new SortableTableWidgetItem(channelLabel(network));
channelItem->setData(Qt::UserRole, network.channel);
networkTable->setItem(row, 4, channelItem);
auto *signalItem = new SortableTableWidgetItem(QString::number(network.signalDbm));
signalItem->setData(Qt::UserRole, network.signalDbm);
networkTable->setItem(row, 5, signalItem);
auto *frequencyItem = new SortableTableWidgetItem(QString::number(network.frequencyMHz));
frequencyItem->setData(Qt::UserRole, network.frequencyMHz);
networkTable->setItem(row, 6, frequencyItem);
}
networkTable->setSortingEnabled(sortingEnabled);
}
void MainWindow::updateScanInterval(int index)
{
if (scanTimer == nullptr) {
return;
}
const int intervalMs = scanIntervalCombo->itemData(index).toInt();
if (intervalMs <= 0) {
scanTimer->stop();
return;
}
scanTimer->start(intervalMs);
}
void MainWindow::setStatusMessage(const QString &message) const
{
statusLabel->setText(message);
statusLabel->setVisible(!message.isEmpty());
}