forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboard.cpp
More file actions
987 lines (877 loc) · 33.7 KB
/
Dashboard.cpp
File metadata and controls
987 lines (877 loc) · 33.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
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
/*
* Copyright (c) 2020-2022 Alex Spataru <https://github.com/alex-spataru>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <IO/Manager.h>
#include <IO/Console.h>
#include <CSV/Player.h>
#include <UI/Dashboard.h>
#include <JSON/Generator.h>
#include <Misc/TimerEvents.h>
//----------------------------------------------------------------------------------------
// Constructor/deconstructor & singleton
//----------------------------------------------------------------------------------------
/**
* Constructor of the class.
*/
UI::Dashboard::Dashboard()
: m_points(100)
, m_precision(2)
{
// clang-format off
connect(&CSV::Player::instance(), &CSV::Player::openChanged,
this, &UI::Dashboard::resetData);
connect(&IO::Manager::instance(), &IO::Manager::connectedChanged,
this, &UI::Dashboard::resetData);
connect(&JSON::Generator::instance(), &JSON::Generator::jsonChanged,
this, &UI::Dashboard::processLatestJSON);
connect(&JSON::Generator::instance(), &JSON::Generator::jsonFileMapChanged,
this, &UI::Dashboard::resetData);
// clang-format on
}
/**
* Returns a pointer to the only instance of the class.
*/
UI::Dashboard &UI::Dashboard::instance()
{
static Dashboard singleton;
return singleton;
}
//----------------------------------------------------------------------------------------
// Group/Dataset access functions
//----------------------------------------------------------------------------------------
QFont UI::Dashboard::monoFont() const
{
return QFont("Roboto Mono");
}
// clang-format off
const JSON::Group &UI::Dashboard::getLED(const int index) const { return m_ledWidgets.at(index); }
const JSON::Group &UI::Dashboard::getGPS(const int index) const { return m_gpsWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getBar(const int index) const { return m_barWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getFFT(const int index) const { return m_fftWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getPlot(const int index) const { return m_plotWidgets.at(index); }
const JSON::Group &UI::Dashboard::getGroups(const int index) const { return m_groupWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getGauge(const int index) const { return m_gaugeWidgets.at(index); }
const JSON::Group &UI::Dashboard::getGyroscope(const int index) const { return m_gyroscopeWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getCompass(const int index) const { return m_compassWidgets.at(index); }
const JSON::Group &UI::Dashboard::getMultiplot(const int index) const { return m_multiPlotWidgets.at(index); }
const JSON::Group &UI::Dashboard::getAccelerometer(const int index) const { return m_accelerometerWidgets.at(index); }
// clang-format on
//----------------------------------------------------------------------------------------
// Misc member access functions
//----------------------------------------------------------------------------------------
/**
* Returns the title of the current JSON project/frame.
*/
QString UI::Dashboard::title()
{
return m_currentFrame.title();
}
/**
* Returns @c true if there is any data available to generate the QML dashboard.
*/
bool UI::Dashboard::available()
{
return totalWidgetCount() > 0;
}
/**
* Returns the number of points displayed by the graphs
*/
int UI::Dashboard::points() const
{
return m_points;
}
/**
* Returns the number of decimal digits displayed by the widgets
*/
int UI::Dashboard::precision() const
{
return m_precision;
}
/**
* Returns @c true if the current JSON frame is valid and ready-to-use by the QML
* interface.
*/
bool UI::Dashboard::frameValid() const
{
return m_currentFrame.isValid();
}
//----------------------------------------------------------------------------------------
// Widget count functions
//----------------------------------------------------------------------------------------
/**
* Returns the total number of widgets that compose the current JSON frame.
* This function is used as a helper to the functions that use the global-index widget
* system.
*
* In the case of this function, we do not care about the order of the items that generate
* the summatory count of all widgets. But in the other global-index functions we need to
* be careful to sincronize the order of the widgets in order to allow the global-index
* system to work correctly.
*/
int UI::Dashboard::totalWidgetCount() const
{
// clang-format off
const int count =
gpsCount() +
ledCount() +
barCount() +
fftCount() +
plotCount() +
gaugeCount() +
groupCount() +
compassCount() +
multiPlotCount() +
gyroscopeCount() +
accelerometerCount();
// clang-format on
return count;
}
// clang-format off
int UI::Dashboard::gpsCount() const { return m_gpsWidgets.count(); }
int UI::Dashboard::ledCount() const { return m_ledWidgets.count(); }
int UI::Dashboard::barCount() const { return m_barWidgets.count(); }
int UI::Dashboard::fftCount() const { return m_fftWidgets.count(); }
int UI::Dashboard::plotCount() const { return m_plotWidgets.count(); }
int UI::Dashboard::gaugeCount() const { return m_gaugeWidgets.count(); }
int UI::Dashboard::groupCount() const { return m_groupWidgets.count(); }
int UI::Dashboard::compassCount() const { return m_compassWidgets.count(); }
int UI::Dashboard::gyroscopeCount() const { return m_gyroscopeWidgets.count(); }
int UI::Dashboard::multiPlotCount() const { return m_multiPlotWidgets.count(); }
int UI::Dashboard::accelerometerCount() const { return m_accelerometerWidgets.count(); }
// clang-format on
//----------------------------------------------------------------------------------------
// Relative-to-global widget index utility functions
//----------------------------------------------------------------------------------------
/**
* Returns a @c list with the titles of all the widgets that compose the current JSON
* frame.
*
* We need to be careful to sincronize the order of the widgets in order to allow
* the global-index system to work correctly.
*/
StringList UI::Dashboard::widgetTitles()
{
// Warning: maintain same order as the view option repeaters in ViewOptions.qml!
// clang-format off
return groupTitles() +
multiPlotTitles() +
ledTitles() +
fftTitles() +
plotTitles() +
barTitles() +
gaugeTitles() +
compassTitles() +
gyroscopeTitles() +
accelerometerTitles() +
gpsTitles();
// clang-format on
}
/**
* Returns the widget-specific index for the widget with the specified @a index.
*
* To simplify operations and user interface generation in QML, this class represents
* the widgets in two manners:
*
* - A global list of all widgets
* - A widget-specific list for each type of widget
*
* The global index allows us to use the @c WidgetLoader class to load any type of
* widget, which reduces the need of implementing QML-specific code for each widget
* that Serial Studio implements.
*
* The relative index is used by the visibility switches in the QML user interface.
*
* We need to be careful to sincronize the order of the widgets in order to allow
* the global-index system to work correctly.
*/
int UI::Dashboard::relativeIndex(const int globalIndex) const
{
//
// Warning: relative widget index should be calculated using the same order as defined
// by the UI::Dashboard::widgetTitles() function.
//
// Check if we should return group widget
int index = globalIndex;
if (index < groupCount())
return index;
// Check if we should return multi-plot widget
index -= groupCount();
if (index < multiPlotCount())
return index;
// Check if we should return LED widget
index -= multiPlotCount();
if (index < ledCount())
return index;
// Check if we should return FFT widget
index -= ledCount();
if (index < fftCount())
return index;
// Check if we should return plot widget
index -= fftCount();
if (index < plotCount())
return index;
// Check if we should return bar widget
index -= plotCount();
if (index < barCount())
return index;
// Check if we should return gauge widget
index -= barCount();
if (index < gaugeCount())
return index;
// Check if we should return compass widget
index -= gaugeCount();
if (index < compassCount())
return index;
// Check if we should return gyro widget
index -= compassCount();
if (index < gyroscopeCount())
return index;
// Check if we should return accelerometer widget
index -= gyroscopeCount();
if (index < accelerometerCount())
return index;
// Check if we should return map widget
index -= accelerometerCount();
if (index < gpsCount())
return index;
// Return unknown widget
return -1;
}
/**
* Returns @c true if the widget with the specifed @a globalIndex should be
* displayed in the QML user interface.
*
* To simplify operations and user interface generation in QML, this class represents
* the widgets in two manners:
*
* - A global list of all widgets
* - A widget-specific list for each type of widget
*
* The global index allows us to use the @c WidgetLoader class to load any type of
* widget, which reduces the need of implementing QML-specific code for each widget
* that Serial Studio implements.
*
* We need to be careful to sincronize the order of the widgets in order to allow
* the global-index system to work correctly.
*/
bool UI::Dashboard::widgetVisible(const int globalIndex) const
{
bool visible = false;
auto index = relativeIndex(globalIndex);
switch (widgetType(globalIndex))
{
case WidgetType::Group:
visible = groupVisible(index);
break;
case WidgetType::MultiPlot:
visible = multiPlotVisible(index);
break;
case WidgetType::FFT:
visible = fftVisible(index);
break;
case WidgetType::Plot:
visible = plotVisible(index);
break;
case WidgetType::Bar:
visible = barVisible(index);
break;
case WidgetType::Gauge:
visible = gaugeVisible(index);
break;
case WidgetType::Compass:
visible = compassVisible(index);
break;
case WidgetType::Gyroscope:
visible = gyroscopeVisible(index);
break;
case WidgetType::Accelerometer:
visible = accelerometerVisible(index);
break;
case WidgetType::GPS:
visible = gpsVisible(index);
break;
case WidgetType::LED:
visible = ledVisible(index);
break;
default:
visible = false;
break;
}
return visible;
}
/**
* Returns the appropiate SVG-icon to load for the widget at the specified @a globalIndex.
*
* To simplify operations and user interface generation in QML, this class represents
* the widgets in two manners:
*
* - A global list of all widgets
* - A widget-specific list for each type of widget
*
* The global index allows us to use the @c WidgetLoader class to load any type of
* widget, which reduces the need of implementing QML-specific code for each widget
* that Serial Studio implements.
*
* We need to be careful to sincronize the order of the widgets in order to allow
* the global-index system to work correctly.
*/
QString UI::Dashboard::widgetIcon(const int globalIndex) const
{
switch (widgetType(globalIndex))
{
case WidgetType::Group:
return "qrc:/icons/group.svg";
break;
case WidgetType::MultiPlot:
return "qrc:/icons/multiplot.svg";
break;
case WidgetType::FFT:
return "qrc:/icons/fft.svg";
break;
case WidgetType::Plot:
return "qrc:/icons/plot.svg";
break;
case WidgetType::Bar:
return "qrc:/icons/bar.svg";
break;
case WidgetType::Gauge:
return "qrc:/icons/gauge.svg";
break;
case WidgetType::Compass:
return "qrc:/icons/compass.svg";
break;
case WidgetType::Gyroscope:
return "qrc:/icons/gyro.svg";
break;
case WidgetType::Accelerometer:
return "qrc:/icons/accelerometer.svg";
break;
case WidgetType::GPS:
return "qrc:/icons/gps.svg";
break;
case WidgetType::LED:
return "qrc:/icons/led.svg";
break;
default:
return "qrc:/icons/close.svg";
break;
}
}
/**
* Returns the type of widget that corresponds to the given @a globalIndex.
*
* Possible return values are:
* - @c WidgetType::Unknown
* - @c WidgetType::Group
* - @c WidgetType::MultiPlot
* - @c WidgetType::FFT
* - @c WidgetType::Plot
* - @c WidgetType::Bar
* - @c WidgetType::Gauge
* - @c WidgetType::Compass
* - @c WidgetType::Gyroscope
* - @c WidgetType::Accelerometer
* - @c WidgetType::GPS
* - @c WidgetType::LED
*
* To simplify operations and user interface generation in QML, this class represents
* the widgets in two manners:
*
* - A global list of all widgets
* - A widget-specific list for each type of widget
*
* The global index allows us to use the @c WidgetLoader class to load any type of
* widget, which reduces the need of implementing QML-specific code for each widget
* that Serial Studio implements.
*
* We need to be careful to sincronize the order of the widgets in order to allow
* the global-index system to work correctly.
*/
UI::Dashboard::WidgetType UI::Dashboard::widgetType(const int globalIndex) const
{
//
// Warning: relative widget index should be calculated using the same order as defined
// by the UI::Dashboard::widgetTitles() function.
//
// Unitialized widget loader class
if (globalIndex < 0)
return WidgetType::Unknown;
// Check if we should return group widget
int index = globalIndex;
if (index < groupCount())
return WidgetType::Group;
// Check if we should return multi-plot widget
index -= groupCount();
if (index < multiPlotCount())
return WidgetType::MultiPlot;
// Check if we should return LED widget
index -= multiPlotCount();
if (index < ledCount())
return WidgetType::LED;
// Check if we should return FFT widget
index -= ledCount();
if (index < fftCount())
return WidgetType::FFT;
// Check if we should return plot widget
index -= fftCount();
if (index < plotCount())
return WidgetType::Plot;
// Check if we should return bar widget
index -= plotCount();
if (index < barCount())
return WidgetType::Bar;
// Check if we should return gauge widget
index -= barCount();
if (index < gaugeCount())
return WidgetType::Gauge;
// Check if we should return compass widget
index -= gaugeCount();
if (index < compassCount())
return WidgetType::Compass;
// Check if we should return gyro widget
index -= compassCount();
if (index < gyroscopeCount())
return WidgetType::Gyroscope;
// Check if we should return accelerometer widget
index -= gyroscopeCount();
if (index < accelerometerCount())
return WidgetType::Accelerometer;
// Check if we should return map widget
index -= accelerometerCount();
if (index < gpsCount())
return WidgetType::GPS;
// Return unknown widget
return WidgetType::Unknown;
}
//----------------------------------------------------------------------------------------
// Widget visibility access functions
//----------------------------------------------------------------------------------------
// clang-format off
bool UI::Dashboard::barVisible(const int index) const { return getVisibility(m_barVisibility, index); }
bool UI::Dashboard::fftVisible(const int index) const { return getVisibility(m_fftVisibility, index); }
bool UI::Dashboard::gpsVisible(const int index) const { return getVisibility(m_gpsVisibility, index); }
bool UI::Dashboard::ledVisible(const int index) const { return getVisibility(m_ledVisibility, index); }
bool UI::Dashboard::plotVisible(const int index) const { return getVisibility(m_plotVisibility, index); }
bool UI::Dashboard::groupVisible(const int index) const { return getVisibility(m_groupVisibility, index); }
bool UI::Dashboard::gaugeVisible(const int index) const { return getVisibility(m_gaugeVisibility, index); }
bool UI::Dashboard::compassVisible(const int index) const { return getVisibility(m_compassVisibility, index); }
bool UI::Dashboard::gyroscopeVisible(const int index) const { return getVisibility(m_gyroscopeVisibility, index); }
bool UI::Dashboard::multiPlotVisible(const int index) const { return getVisibility(m_multiPlotVisibility, index); }
bool UI::Dashboard::accelerometerVisible(const int index) const { return getVisibility(m_accelerometerVisibility, index); }
// clang-format on
//----------------------------------------------------------------------------------------
// Widget title functions
//----------------------------------------------------------------------------------------
// clang-format off
StringList UI::Dashboard::gpsTitles() { return groupTitles(m_gpsWidgets); }
StringList UI::Dashboard::ledTitles() { return groupTitles(m_ledWidgets); }
StringList UI::Dashboard::groupTitles() { return groupTitles(m_groupWidgets); }
StringList UI::Dashboard::barTitles() { return datasetTitles(m_barWidgets); }
StringList UI::Dashboard::fftTitles() { return datasetTitles(m_fftWidgets); }
StringList UI::Dashboard::plotTitles() { return datasetTitles(m_plotWidgets); }
StringList UI::Dashboard::gaugeTitles() { return datasetTitles(m_gaugeWidgets); }
StringList UI::Dashboard::compassTitles() { return datasetTitles(m_compassWidgets); }
StringList UI::Dashboard::gyroscopeTitles() { return groupTitles(m_gyroscopeWidgets); }
StringList UI::Dashboard::multiPlotTitles() { return groupTitles(m_multiPlotWidgets); }
StringList UI::Dashboard::accelerometerTitles() { return groupTitles(m_accelerometerWidgets); }
// clang-format on
//----------------------------------------------------------------------------------------
// Plot & widget options
//----------------------------------------------------------------------------------------
void UI::Dashboard::setPoints(const int points)
{
if (m_points != points)
{
// Update number of points
m_points = points;
// Clear values
m_fftPlotValues.clear();
m_linearPlotValues.clear();
// Regenerate x-axis values
m_xData.resize(points);
for (int i = 0; i < points; ++i)
m_xData[i] = i;
// Update plots
Q_EMIT pointsChanged();
}
}
void UI::Dashboard::setPrecision(const int precision)
{
if (m_precision != precision)
{
m_precision = precision;
Q_EMIT precisionChanged();
}
}
//----------------------------------------------------------------------------------------
// Visibility-related slots
//----------------------------------------------------------------------------------------
// clang-format off
void UI::Dashboard::setBarVisible(const int i, const bool v) { setVisibility(m_barVisibility, i, v); }
void UI::Dashboard::setFFTVisible(const int i, const bool v) { setVisibility(m_fftVisibility, i, v); }
void UI::Dashboard::setGpsVisible(const int i, const bool v) { setVisibility(m_gpsVisibility, i, v); }
void UI::Dashboard::setLedVisible(const int i, const bool v) { setVisibility(m_ledVisibility, i, v); }
void UI::Dashboard::setPlotVisible(const int i, const bool v) { setVisibility(m_plotVisibility, i, v); }
void UI::Dashboard::setGroupVisible(const int i, const bool v) { setVisibility(m_groupVisibility, i, v); }
void UI::Dashboard::setGaugeVisible(const int i, const bool v) { setVisibility(m_gaugeVisibility, i, v); }
void UI::Dashboard::setCompassVisible(const int i, const bool v) { setVisibility(m_compassVisibility, i, v); }
void UI::Dashboard::setGyroscopeVisible(const int i, const bool v) { setVisibility(m_gyroscopeVisibility, i, v); }
void UI::Dashboard::setMultiplotVisible(const int i, const bool v) { setVisibility(m_multiPlotVisibility, i, v); }
void UI::Dashboard::setAccelerometerVisible(const int i, const bool v) { setVisibility(m_accelerometerVisibility, i, v); }
// clang-format on
//----------------------------------------------------------------------------------------
// Frame data handling slots
//----------------------------------------------------------------------------------------
/**
* Removes all available data from the UI when the device is disconnected or the CSV
* file is closed.
*/
void UI::Dashboard::resetData()
{
// Make latest frame invalid
m_currentFrame.read(QJsonObject {});
// Clear plot data
m_fftPlotValues.clear();
m_linearPlotValues.clear();
// Clear widget data
m_barWidgets.clear();
m_fftWidgets.clear();
m_gpsWidgets.clear();
m_ledWidgets.clear();
m_plotWidgets.clear();
m_gaugeWidgets.clear();
m_groupWidgets.clear();
m_compassWidgets.clear();
m_gyroscopeWidgets.clear();
m_multiPlotWidgets.clear();
m_accelerometerWidgets.clear();
// Clear widget visibility data
m_barVisibility.clear();
m_fftVisibility.clear();
m_gpsVisibility.clear();
m_ledVisibility.clear();
m_plotVisibility.clear();
m_gaugeVisibility.clear();
m_groupVisibility.clear();
m_compassVisibility.clear();
m_gyroscopeVisibility.clear();
m_multiPlotVisibility.clear();
m_accelerometerVisibility.clear();
// Update UI
Q_EMIT updated();
Q_EMIT dataReset();
Q_EMIT titleChanged();
Q_EMIT widgetCountChanged();
Q_EMIT widgetVisibilityChanged();
}
/**
* Regenerates the data displayed on the dashboard plots
*/
void UI::Dashboard::updatePlots()
{
// Initialize arrays that contain pointers to the
// datasets that need to be plotted.
QVector<JSON::Dataset> fftDatasets;
QVector<JSON::Dataset> linearDatasets;
// Create list with datasets that need to be graphed
for (int i = 0; i < m_currentFrame.groupCount(); ++i)
{
auto group = m_currentFrame.groups().at(i);
for (int j = 0; j < group.datasetCount(); ++j)
{
auto dataset = group.getDataset(j);
if (dataset.fft())
fftDatasets.append(dataset);
if (dataset.graph())
linearDatasets.append(dataset);
}
}
// Check if we need to update dataset points
if (m_linearPlotValues.count() != linearDatasets.count())
{
m_linearPlotValues.clear();
for (int i = 0; i < linearDatasets.count(); ++i)
{
m_linearPlotValues.append(PlotData());
m_linearPlotValues.last().resize(points());
// clang-format off
std::fill(m_linearPlotValues.last().begin(),
m_linearPlotValues.last().end(),
0.0001);
// clang-format on
}
}
// Check if we need to update FFT dataset points
if (m_fftPlotValues.count() != fftDatasets.count())
{
m_fftPlotValues.clear();
for (int i = 0; i < fftDatasets.count(); ++i)
{
m_fftPlotValues.append(PlotData());
m_fftPlotValues.last().resize(fftDatasets[i].fftSamples());
// clang-format off
std::fill(m_fftPlotValues.last().begin(),
m_fftPlotValues.last().end(),
0);
// clang-format on
}
}
// Append latest values to linear plot data
for (int i = 0; i < linearDatasets.count(); ++i)
{
auto data = m_linearPlotValues[i].data();
auto count = m_linearPlotValues[i].count();
memmove(data, data + 1, count * sizeof(double));
m_linearPlotValues[i][count - 1] = linearDatasets[i].value().toDouble();
}
// Append latest values to FFT plot data
for (int i = 0; i < fftDatasets.count(); ++i)
{
auto data = m_fftPlotValues[i].data();
auto count = m_fftPlotValues[i].count();
memmove(data, data + 1, count * sizeof(double));
m_fftPlotValues[i][count - 1] = fftDatasets[i].value().toDouble();
}
}
/**
* Regenerates the data displayed on the dashboard widgets
*/
void UI::Dashboard::processLatestJSON(const QJsonObject &json)
{
// Save widget count
const int barC = barCount();
const int fftC = fftCount();
const int gpsC = gpsCount();
const int ledC = ledCount();
const int plotC = plotCount();
const int groupC = groupCount();
const int gaugeC = gaugeCount();
const int compassC = compassCount();
const int gyroscopeC = gyroscopeCount();
const int multiPlotC = multiPlotCount();
const int accelerometerC = accelerometerCount();
// Save previous title
auto pTitle = title();
// Try to read latest frame for widget updating
if (!m_currentFrame.read(json))
return;
// Regenerate plot data
updatePlots();
// Update widget vectors
m_fftWidgets = getFFTWidgets();
m_ledWidgets = getLEDWidgets();
m_plotWidgets = getPlotWidgets();
m_groupWidgets = getWidgetGroups("");
m_gpsWidgets = getWidgetGroups("map");
m_barWidgets = getWidgetDatasets("bar");
m_gaugeWidgets = getWidgetDatasets("gauge");
m_gyroscopeWidgets = getWidgetGroups("gyro");
m_compassWidgets = getWidgetDatasets("compass");
m_multiPlotWidgets = getWidgetGroups("multiplot");
m_accelerometerWidgets = getWidgetGroups("accelerometer");
// Add accelerometer widgets to multiplot
for (int i = 0; i < m_accelerometerWidgets.count(); ++i)
m_multiPlotWidgets.append(m_accelerometerWidgets.at(i));
// Add gyroscope widgets to multiplot
for (int i = 0; i < m_gyroscopeWidgets.count(); ++i)
m_multiPlotWidgets.append(m_gyroscopeWidgets.at(i));
// Check if we need to update title
if (pTitle != title())
Q_EMIT titleChanged();
// Check if we need to regenerate widgets
bool regenerateWidgets = false;
regenerateWidgets |= (barC != barCount());
regenerateWidgets |= (fftC != fftCount());
regenerateWidgets |= (gpsC != gpsCount());
regenerateWidgets |= (ledC != ledCount());
regenerateWidgets |= (plotC != plotCount());
regenerateWidgets |= (gaugeC != gaugeCount());
regenerateWidgets |= (groupC != groupCount());
regenerateWidgets |= (compassC != compassCount());
regenerateWidgets |= (gyroscopeC != gyroscopeCount());
regenerateWidgets |= (multiPlotC != multiPlotCount());
regenerateWidgets |= (accelerometerC != accelerometerCount());
// Regenerate widget visiblity models
if (regenerateWidgets)
{
m_barVisibility.resize(barCount());
m_fftVisibility.resize(fftCount());
m_gpsVisibility.resize(gpsCount());
m_ledVisibility.resize(ledCount());
m_plotVisibility.resize(plotCount());
m_gaugeVisibility.resize(gaugeCount());
m_groupVisibility.resize(groupCount());
m_compassVisibility.resize(compassCount());
m_gyroscopeVisibility.resize(gyroscopeCount());
m_multiPlotVisibility.resize(multiPlotCount());
m_accelerometerVisibility.resize(accelerometerCount());
std::fill(m_barVisibility.begin(), m_barVisibility.end(), 1);
std::fill(m_fftVisibility.begin(), m_fftVisibility.end(), 1);
std::fill(m_gpsVisibility.begin(), m_gpsVisibility.end(), 1);
std::fill(m_ledVisibility.begin(), m_ledVisibility.end(), 1);
std::fill(m_plotVisibility.begin(), m_plotVisibility.end(), 1);
std::fill(m_gaugeVisibility.begin(), m_gaugeVisibility.end(), 1);
std::fill(m_groupVisibility.begin(), m_groupVisibility.end(), 1);
std::fill(m_compassVisibility.begin(), m_compassVisibility.end(), 1);
std::fill(m_gyroscopeVisibility.begin(), m_gyroscopeVisibility.end(), 1);
std::fill(m_multiPlotVisibility.begin(), m_multiPlotVisibility.end(), 1);
std::fill(m_accelerometerVisibility.begin(), m_accelerometerVisibility.end(), 1);
Q_EMIT widgetCountChanged();
Q_EMIT widgetVisibilityChanged();
}
// Update UI;
Q_EMIT updated();
}
//----------------------------------------------------------------------------------------
// Widget utility functions
//----------------------------------------------------------------------------------------
/**
* Returns a group with all the datasets that need to be shown in the LED status panel.
*
* @note We return a vector with a single group item because we want to display a title on
* the window without breaking the current software architecture.
*/
QVector<JSON::Group> UI::Dashboard::getLEDWidgets()
{
QVector<JSON::Dataset> widgets;
Q_FOREACH (auto group, m_currentFrame.groups())
{
Q_FOREACH (auto dataset, group.datasets())
{
if (dataset.led())
widgets.append(dataset);
}
}
QVector<JSON::Group> groups;
if (widgets.count() > 0)
{
JSON::Group group;
group.m_title = tr("Status Panel");
group.m_datasets = widgets;
groups.append(group);
}
return groups;
}
/**
* Returns a vector with all the datasets that need to be shown in the FFT widgets.
*/
QVector<JSON::Dataset> UI::Dashboard::getFFTWidgets()
{
QVector<JSON::Dataset> widgets;
Q_FOREACH (auto group, m_currentFrame.groups())
{
Q_FOREACH (auto dataset, group.datasets())
{
if (dataset.fft())
widgets.append(dataset);
}
}
return widgets;
}
/**
* Returns a vector with all the datasets that need to be plotted.
*/
QVector<JSON::Dataset> UI::Dashboard::getPlotWidgets()
{
QVector<JSON::Dataset> widgets;
Q_FOREACH (auto group, m_currentFrame.groups())
{
Q_FOREACH (auto dataset, group.datasets())
{
if (dataset.graph())
widgets.append(dataset);
}
}
return widgets;
}
/**
* Returns a vector with all the groups that implement the widget with the specied
* @a handle.
*/
QVector<JSON::Group> UI::Dashboard::getWidgetGroups(const QString &handle)
{
QVector<JSON::Group> widgets;
Q_FOREACH (auto group, m_currentFrame.groups())
{
if (group.widget() == handle)
widgets.append(group);
}
return widgets;
}
/**
* Returns a vector with all the datasets that implement a widget with the specified
* @a handle.
*/
QVector<JSON::Dataset> UI::Dashboard::getWidgetDatasets(const QString &handle)
{
QVector<JSON::Dataset> widgets;
Q_FOREACH (auto group, m_currentFrame.groups())
{
Q_FOREACH (auto dataset, group.datasets())
{
if (dataset.widget() == handle)
widgets.append(dataset);
}
}
return widgets;
}
/**
* Returns the titles of the datasets contained in the specified @a vector.
*/
StringList UI::Dashboard::datasetTitles(const QVector<JSON::Dataset> &vector)
{
StringList list;
Q_FOREACH (auto set, vector)
list.append(set.title());
return list;
}
/**
* Returns the titles of the groups contained in the specified @a vector.
*/
StringList UI::Dashboard::groupTitles(const QVector<JSON::Group> &vector)
{
StringList list;
Q_FOREACH (auto group, vector)
list.append(group.title());
return list;
}
/**
* Returns @c true if the widget at the specifed @a index of the @a vector should be
* displayed in the QML user interface.
*/
bool UI::Dashboard::getVisibility(const QVector<bool> &vector, const int index) const
{
return vector[index];
}
/**
* Changes the @a visible flag of the widget at the specified @a index of the given @a
* vector. Calling this function with @a visible set to @c false will hide the widget in
* the QML user interface.
*/
void UI::Dashboard::setVisibility(QVector<bool> &vector, const int index,
const bool visible)
{
vector[index] = visible;
Q_EMIT widgetVisibilityChanged();
}
#ifdef SERIAL_STUDIO_INCLUDE_MOC
# include "moc_Dashboard.cpp"
#endif