forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
1536 lines (1338 loc) · 42.4 KB
/
Model.cpp
File metadata and controls
1536 lines (1338 loc) · 42.4 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
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020-2023 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 "Model.h"
#include "CodeEditor.h"
#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QFileDialog>
#include <QJsonObject>
#include <QJsonDocument>
#include <AppInfo.h>
#include <IO/Manager.h>
#include <JSON/Generator.h>
#include <Misc/Utilities.h>
//
// For invalid group returns, avoids crashes while creating a new project & the UI
// is in the process of being updated
//
static JSON::Group EMPTY_GROUP;
//----------------------------------------------------------------------------------------
// Constructor/deconstructor & singleton
//----------------------------------------------------------------------------------------
/**
* Constructor function. Initializes internal members and configures the signals/slots so
* that the editor can know if the user modified the JSON document. Finally, the
* constructor configures signals/slots with the JSON Generator to share the same JSON
* document file.
*/
Project::Model::Model()
: m_title("")
, m_separator("")
, m_frameParserCode("")
, m_frameEndSequence("")
, m_frameStartSequence("")
, m_modified(false)
, m_filePath("")
{
// clang-format off
// Connect signals/slots
connect(this, &Project::Model::groupChanged,
this, &Project::Model::onGroupChanged);
connect(this, &Project::Model::titleChanged,
this, &Project::Model::onModelChanged);
connect(this, &Project::Model::datasetChanged,
this, &Project::Model::onDatasetChanged);
connect(this, &Project::Model::separatorChanged,
this, &Project::Model::onModelChanged);
connect(this, &Project::Model::groupCountChanged,
this, &Project::Model::onModelChanged);
connect(this, &Project::Model::groupOrderChanged,
this, &Project::Model::onModelChanged);
connect(this, &Project::Model::frameParserCodeChanged,
this, &Project::Model::onModelChanged);
connect(this, &Project::Model::frameEndSequenceChanged,
this, &Project::Model::onModelChanged);
connect(this, &Project::Model::frameStartSequenceChanged,
this, &Project::Model::onModelChanged);
// Load current JSON map file into C++ model
connect(&JSON::Generator::instance(), &JSON::Generator::jsonFileMapChanged,
this, &Project::Model::onJsonLoaded);
// clang-format on
}
/**
* Returns a pointer to the only instance of the editor class.
*/
Project::Model &Project::Model::instance()
{
static Model singleton;
return singleton;
}
//----------------------------------------------------------------------------------------
// Member access functions
//----------------------------------------------------------------------------------------
/**
* Returns a list with the available group-level widgets. This list is used by the user
* interface to allow the user to build accelerometer, gyro & map widgets directly from
* the UI.
*/
StringList Project::Model::availableGroupLevelWidgets()
{
return StringList { tr("Dataset widgets"), tr("Accelerometer"), tr("Gyroscope"),
tr("GPS"), tr("Multiple data plot") };
}
/**
* Returns a list with the available dataset-level widgets. This list is used by the user
* interface to allow the user to build gauge, bar & compass widgets directly from the UI.
*/
StringList Project::Model::availableDatasetLevelWidgets()
{
return StringList { tr("None"), tr("Gauge"), tr("Bar/level"), tr("Compass") };
}
/**
* Returns the default path for saving JSON project files
*/
QString Project::Model::jsonProjectsPath() const
{
// Get file name and path
QString path = QString("%1/Documents/%2/JSON Projects/")
.arg(QDir::homePath(), qApp->applicationName());
// Generate file path if required
QDir dir(path);
if (!dir.exists())
dir.mkpath(".");
return path;
}
/**
* Returns the title of the current project
*/
QString Project::Model::title() const
{
return m_title;
}
/**
* Returns the data separator sequence for the current project.
*/
QString Project::Model::separator() const
{
return m_separator;
}
/**
* Returns the frame end sequence for the current project.
*/
QString Project::Model::frameEndSequence() const
{
return m_frameEndSequence;
}
/**
* Returns the frame start sequence for the current project.
*/
QString Project::Model::frameStartSequence() const
{
return m_frameStartSequence;
}
/**
* Returns @c true if the user modified the current project. This is
* used to know if Serial Studio shall prompt the user to save his/her
* modifications before closing the editor window.
*/
bool Project::Model::modified() const
{
return m_modified;
}
/**
* Returns the number of groups contained in the current JSON project.
*/
int Project::Model::groupCount() const
{
return m_groups.count();
}
/**
* Returns the full path of the current JSON project file.
*/
QString Project::Model::jsonFilePath() const
{
return m_filePath;
}
/**
* Returns the simplified file name of the current JSON project file.
* This is used to change the title of the Editor window.
*/
QString Project::Model::jsonFileName() const
{
if (!jsonFilePath().isEmpty())
{
auto fileInfo = QFileInfo(m_filePath);
return fileInfo.fileName();
}
return tr("New Project");
}
/**
* Checks if the current project has been modified and prompts the
* user to save his/her changes.
*/
bool Project::Model::askSave()
{
if (!modified())
return true;
auto ret = Misc::Utilities::showMessageBox(
tr("Do you want to save your changes?"),
tr("You have unsaved modifications in this project!"), APP_NAME,
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Cancel)
return false;
if (ret == QMessageBox::Discard)
return true;
return saveJsonFile();
}
/**
* Validates the configuration of the current JSON project and saves the JSON
* document on the hard disk.
*/
bool Project::Model::saveJsonFile()
{
// Validate project title
if (title().isEmpty())
{
Misc::Utilities::showMessageBox(tr("Project error"),
tr("Project title cannot be empty!"));
return false;
}
// Validate group titles
for (int i = 0; i < groupCount(); ++i)
{
if (groupTitle(i).isEmpty())
{
Misc::Utilities::showMessageBox(tr("Project error - Group %1").arg(i + 1),
tr("Group title cannot be empty!"));
return false;
}
}
// Validate dataset titles
for (int i = 0; i < groupCount(); ++i)
{
for (int j = 0; j < datasetCount(i); ++j)
{
if (datasetTitle(i, j).isEmpty())
{
Misc::Utilities::showMessageBox(
tr("Project error - Group %1, Dataset %2").arg(i + 1).arg(j + 1),
tr("Dataset title cannot be empty!"));
return false;
}
}
}
// Validate dataset indexes
QVector<int> indexes;
for (int i = 0; i < groupCount(); ++i)
{
for (int j = 0; j < datasetCount(i); ++j)
{
if (!indexes.contains(datasetIndex(i, j)))
indexes.append(datasetIndex(i, j));
else
{
auto ret = Misc::Utilities::showMessageBox(
tr("Warning - Group %1, Dataset %2").arg(i + 1).arg(j + 1),
tr("Dataset contains duplicate frame index position! Continue?"),
APP_NAME, QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::No)
return false;
}
}
}
// Get file save path
if (jsonFilePath().isEmpty())
{
auto path = QFileDialog::getSaveFileName(Q_NULLPTR, tr("Save JSON project"),
jsonProjectsPath(), "*.json");
if (path.isEmpty())
return false;
m_filePath = path;
}
// Open file for writing
QFile file(m_filePath);
if (!file.open(QFile::WriteOnly))
{
Misc::Utilities::showMessageBox(tr("File open error"), file.errorString());
return false;
}
// Create JSON document & add properties
QJsonObject json;
json.insert("title", title());
json.insert("separator", separator());
json.insert("frameEnd", frameEndSequence());
json.insert("frameParser", frameParserCode());
json.insert("frameStart", frameStartSequence());
// Create group array
QJsonArray groups;
for (int i = 0; i < groupCount(); ++i)
{
// Create group
QJsonObject group;
group.insert("title", groupTitle(i));
group.insert("widget", groupWidget(i));
// Create dataset array
QJsonArray datasets;
for (int j = 0; j < datasetCount(i); ++j)
{
// Create dataset
QJsonObject dataset;
dataset.insert("led", datasetLED(i, j));
dataset.insert("fft", datasetFftPlot(i, j));
dataset.insert("log", datasetLogPlot(i, j));
dataset.insert("title", datasetTitle(i, j));
dataset.insert("units", datasetUnits(i, j));
dataset.insert("graph", datasetGraph(i, j));
dataset.insert("widget", datasetWidget(i, j));
dataset.insert("min", datasetWidgetMin(i, j).toDouble());
dataset.insert("max", datasetWidgetMax(i, j).toDouble());
dataset.insert("alarm", datasetWidgetAlarm(i, j).toDouble());
dataset.insert("fftSamples", datasetFFTSamples(i, j).toInt());
dataset.insert("index", datasetIndex(i, j));
dataset.insert("value", "");
// Add dataset to array
datasets.append(dataset);
}
// Add datasets to group
group.insert("datasets", datasets);
groups.append(group);
}
// Add groups array to JSON
json.insert("groups", groups);
// Write JSON data to file
file.write(QJsonDocument(json).toJson(QJsonDocument::Indented));
file.close();
// Load JSON file to Serial Studio
openJsonFile(file.fileName());
JSON::Generator::instance().loadJsonMap(file.fileName());
return true;
}
/**
* Returns the number of datasets contained by the given @a group index.
*/
int Project::Model::datasetCount(const int group) const
{
return getGroup(group).m_datasets.count();
}
/**
* Returns a pointer to the group object positioned at the given @a index
*/
const JSON::Group &Project::Model::getGroup(const int index) const
{
if (m_groups.count() > index)
return m_groups.at(index);
return EMPTY_GROUP;
}
/**
* Returns a pointer to the dataset object contained by the @a group at
* the given @a index
*/
const JSON::Dataset &Project::Model::getDataset(const int group, const int index) const
{
return getGroup(group).getDataset(index);
}
/**
* Returns the JavaScript code used to parse incoming frames
*/
QString Project::Model::frameParserCode() const
{
return m_frameParserCode;
}
/**
* Returns the title of the given @a group.
*/
QString Project::Model::groupTitle(const int group) const
{
return getGroup(group).title();
}
/**
* Returns the widget of the given @a group.
*/
QString Project::Model::groupWidget(const int group) const
{
return getGroup(group).widget();
}
/**
* Returns the widget ID of the given @a group. The widget ID is a number
* that represents a group-level widget. The ID depends on the widget type
* and the order of the widgets returned by the @c availableGroupLevelWidgets()
* function.
*/
int Project::Model::groupWidgetIndex(const int group) const
{
auto widget = groupWidget(group);
if (widget == "accelerometer")
return 1;
if (widget == "gyro")
return 2;
if (widget == "map")
return 3;
if (widget == "multiplot")
return 4;
return 0;
}
/**
* Returns the position in the frame that holds the value for the given
* @a dataset (which is contained by the specified @a group).
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
int Project::Model::datasetIndex(const int group, const int dataset) const
{
return getDataset(group, dataset).m_index;
}
/**
* Returns @c true if Serial Studio should generate a LED with the given
* @a dataset (which is contained by the specified @a group).
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
bool Project::Model::datasetLED(const int group, const int dataset) const
{
return getDataset(group, dataset).led();
}
/**
* Returns @c true if Serial Studio should graph the data of the given
* @a dataset (which is contained by the specified @a group).
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
bool Project::Model::datasetGraph(const int group, const int dataset) const
{
return getDataset(group, dataset).graph();
}
/**
* Returns @c true if Serial Studio should graph the data of the given
* @a dataset (which is contained by the specified @a group).
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
bool Project::Model::datasetFftPlot(const int group, const int dataset) const
{
return getDataset(group, dataset).fft();
}
/**
* Returns @c true if Serial Studio should graph the data of the given
* @a dataset (which is contained by the specified @a group) with a
* logarithmic scale.
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
bool Project::Model::datasetLogPlot(const int group, const int dataset) const
{
return getDataset(group, dataset).log();
}
/**
* Returns the title of the specified dataset.
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
QString Project::Model::datasetTitle(const int group, const int dataset) const
{
return getDataset(group, dataset).title();
}
/**
* Returns the measurement units of the specified dataset.
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
QString Project::Model::datasetUnits(const int group, const int dataset) const
{
return getDataset(group, dataset).units();
}
/**
* Returns the widget string of the specified dataset.
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
QString Project::Model::datasetWidget(const int group, const int dataset) const
{
return getDataset(group, dataset).widget();
}
/**
* Returns the widget ID of the specified dataset. The widget ID
* corresponds to the list returned by the
* @c availableDatasetLevelWidgets() function.
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
int Project::Model::datasetWidgetIndex(const int group, const int dataset) const
{
auto widget = datasetWidget(group, dataset);
if (widget == "gauge")
return 1;
if (widget == "bar")
return 2;
if (widget == "compass")
return 3;
return 0;
}
/**
* Returns the minimum widget value of the specified dataset.
* This option is used by the bar & gauge widgets.
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
QString Project::Model::datasetWidgetMin(const int group, const int dataset) const
{
return QString::number(getDataset(group, dataset).min());
}
/**
* Returns the maximum widget value of the specified dataset.
* This option is used by the bar & gauge widgets.
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
QString Project::Model::datasetWidgetMax(const int group, const int dataset) const
{
return QString::number(getDataset(group, dataset).max());
}
/**
* Returns the maximum FFT frequency value of the specified dataset.
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
QString Project::Model::datasetFFTSamples(const int group, const int dataset) const
{
return QString::number(getDataset(group, dataset).fftSamples());
}
/**
* Returns the widget alarm value of the specified dataset.
* This option is used by the bar & gauge widgets.
*
* @param group index of the group in which the dataset belongs
* @param dataset index of the dataset
*/
QString Project::Model::datasetWidgetAlarm(const int group, const int dataset) const
{
auto set = getDataset(group, dataset);
if (set.alarm() <= set.min())
return QString::number(set.max());
return QString::number(set.alarm());
}
//----------------------------------------------------------------------------------------
// Public slots
//----------------------------------------------------------------------------------------
/**
* Resets the C++ model used to represent the JSON project file.
*/
void Project::Model::newJsonFile()
{
// Clear groups list
m_groups.clear();
// Reset project properties
setTitle("");
setSeparator("");
setFrameParserCode("");
setFrameEndSequence("");
setFrameStartSequence("");
// Update file path
m_filePath = "";
Q_EMIT jsonFileChanged();
// Update UI
Q_EMIT groupCountChanged();
setModified(false);
}
/**
* Prompts the user to select a JSON project file & generates the appropiate C++
* model that represents the JSON document.
*/
void Project::Model::openJsonFile()
{
// clang-format off
auto path = QFileDialog::getOpenFileName(Q_NULLPTR,
tr("Select JSON file"),
jsonProjectsPath(),
"*.json");
// clang-format on
// Invalid path, abort
if (path.isEmpty())
return;
// Open the JSON file
openJsonFile(path);
}
/**
* Opens the JSON document at the given @a path & generates the appropiate C++
* model that represents the JSON document.
*/
void Project::Model::openJsonFile(const QString &path)
{
// Open file
QFile file(path);
QJsonDocument document;
if (file.open(QFile::ReadOnly))
{
document = QJsonDocument::fromJson(file.readAll());
file.close();
}
// Validate JSON document
if (document.isEmpty())
return;
// Let the generator use the given JSON file
if (JSON::Generator::instance().jsonMapFilepath() != path)
JSON::Generator::instance().loadJsonMap(path);
// Reset C++ model
newJsonFile();
// Update current JSON document
m_filePath = path;
Q_EMIT jsonFileChanged();
// Read data from JSON document
auto json = document.object();
setTitle(json.value("title").toString());
setSeparator(json.value("separator").toString());
setFrameEndSequence(json.value("frameEnd").toString());
setFrameParserCode(json.value("frameParser").toString());
setFrameStartSequence(json.value("frameStart").toString());
// Modify IO manager settings
IO::Manager::instance().setSeparatorSequence(separator());
IO::Manager::instance().setFinishSequence(frameEndSequence());
IO::Manager::instance().setStartSequence(frameStartSequence());
// Set JSON::Generator operation mode to manual
JSON::Generator::instance().setOperationMode(JSON::Generator::kManual);
// Read groups from JSON document
auto groups = json.value("groups").toArray();
for (int g = 0; g < groups.count(); ++g)
{
// Get JSON group data
auto group = groups.at(g).toObject();
// Register group with C++ model
addGroup();
setGroupTitle(g, group.value("title").toString());
setGroupWidgetData(g, group.value("widget").toString());
// Get JSON group datasets
auto datasets = group.value("datasets").toArray();
for (int d = 0; d < datasets.count(); ++d)
{
// Get dataset JSON data
auto dataset = datasets.at(d).toObject();
// Register dataset with C++ model
addDataset(g);
setDatasetLED(g, d, dataset.value("led").toBool());
setDatasetFftPlot(g, d, dataset.value("fft").toBool());
setDatasetLogPlot(g, d, dataset.value("log").toBool());
setDatasetGraph(g, d, dataset.value("graph").toBool());
setDatasetTitle(g, d, dataset.value("title").toString());
setDatasetUnits(g, d, dataset.value("units").toString());
setDatasetWidgetData(g, d, dataset.value("widget").toString());
// Get max/min texts
auto min = dataset.value("min").toDouble();
auto max = dataset.value("max").toDouble();
auto index = dataset.value("index").toInt();
auto alarm = dataset.value("alarm").toDouble();
auto fftSamples = dataset.value("fftSamples").toInt();
setDatasetIndex(g, d, index);
setDatasetWidgetMin(g, d, QString::number(min));
setDatasetWidgetMax(g, d, QString::number(max));
setDatasetWidgetAlarm(g, d, QString::number(alarm));
;
setDatasetFFTSamples(g, d, QString::number(fftSamples));
}
}
// Update UI
Q_EMIT groupCountChanged();
// Reset modified flag
setModified(false);
}
/**
* Changes the title of the JSON project file.
*/
void Project::Model::setTitle(const QString &title)
{
if (title != m_title)
{
m_title = title;
Q_EMIT titleChanged();
}
}
/**
* Changes the data separator sequence of the JSON project file.
*/
void Project::Model::setSeparator(const QString &separator)
{
if (separator != m_separator)
{
m_separator = separator;
Q_EMIT separatorChanged();
}
}
/**
* Updates the JavaScript code used to parse incoming frames
*/
void Project::Model::setFrameParserCode(const QString &code)
{
if (code != m_frameParserCode)
{
// Update internal model
m_frameParserCode = code;
// Load default code if required
if (m_frameParserCode.isEmpty())
m_frameParserCode = CodeEditor::instance().defaultCode();
// Update UI
Q_EMIT frameParserCodeChanged();
}
}
/**
* Changes the frame end sequence of the JSON project file.
*/
void Project::Model::setFrameEndSequence(const QString &sequence)
{
if (sequence != m_frameEndSequence)
{
m_frameEndSequence = sequence;
Q_EMIT frameEndSequenceChanged();
}
}
/**
* Changes the frame start sequence of the JSON project file.
*/
void Project::Model::setFrameStartSequence(const QString &sequence)
{
if (sequence != m_frameStartSequence)
{
m_frameStartSequence = sequence;
Q_EMIT frameStartSequenceChanged();
}
}
/**
* Adds a new group to the C++ model that represents the JSON project file.
*/
void Project::Model::addGroup()
{
m_groups.append(JSON::Group());
setGroupTitle(m_groups.count() - 1, tr("New Group"));
Q_EMIT groupCountChanged();
}
/**
* Removes the given @a group from the C++ model that represents the JSON
* project file.
*/
void Project::Model::deleteGroup(const int group)
{
auto ret = Misc::Utilities::showMessageBox(
tr("Delete group \"%1\"").arg(groupTitle(group)),
tr("Are you sure you want to delete this group?"), APP_NAME,
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes)
{
m_groups.removeAt(group);
Q_EMIT groupCountChanged();
}
}
/**
* Changes the position of the given @a group in the C++ model.
*/
void Project::Model::moveGroupUp(const int group)
{
if (group > 0)
{
m_groups.move(group, group - 1);
Q_EMIT groupOrderChanged();
}
}
/**
* Changes the position of the given @a group in the C++ model.
*/
void Project::Model::moveGroupDown(const int group)
{
if (group < groupCount() - 1)
{
m_groups.move(group, group + 1);
Q_EMIT groupOrderChanged();
}
}
/**
* Changes the group-level widget for the specified @a group.
* If necessary, this function shall generate the appropiate datasets
* needed to implement the widget (e.g. x,y,z for accelerometer widgets).
*/
bool Project::Model::setGroupWidget(const int group, const int widgetId)
{
auto grp = getGroup(group);
// Warn user if group contains existing datasets
if (!(grp.m_datasets.isEmpty()) && widgetId != 4)
{
if (widgetId == 0 && grp.widget() == "multiplot")
grp.m_widget = "";
else
{
auto ret = Misc::Utilities::showMessageBox(
tr("Are you sure you want to change the group-level widget?"),
tr("Existing datasets for this group will be deleted"), APP_NAME,
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::No)
return false;
else
grp.m_datasets.clear();
}
}
// Accelerometer widget
if (widgetId == 1)
{
// Set widget title
grp.m_widget = "accelerometer";
grp.m_title = tr("Accelerometer");
// Create datasets
JSON::Dataset x, y, z;
// Set dataset indexes
x.m_index = nextDatasetIndex();
y.m_index = nextDatasetIndex() + 1;
z.m_index = nextDatasetIndex() + 2;
// Set measurement units
x.m_units = "m/s²";
y.m_units = "m/s²";
z.m_units = "m/s²";
// Set dataset properties
x.m_widget = "x";
y.m_widget = "y";
z.m_widget = "z";
x.m_title = tr("Accelerometer %1").arg("X");
y.m_title = tr("Accelerometer %1").arg("Y");
z.m_title = tr("Accelerometer %1").arg("Z");
// Add datasets to group
grp.m_datasets.append(x);
grp.m_datasets.append(y);
grp.m_datasets.append(z);
}
// Gyroscope widget
else if (widgetId == 2)
{
// Set widget title
grp.m_widget = "gyro";
grp.m_title = tr("Gyroscope");
// Create datasets
JSON::Dataset x, y, z;
// Set dataset indexes
x.m_index = nextDatasetIndex();
y.m_index = nextDatasetIndex() + 1;
z.m_index = nextDatasetIndex() + 2;
// Set measurement units
x.m_units = "°";
y.m_units = "°";
z.m_units = "°";
// Set dataset properties
x.m_widget = "roll";
y.m_widget = "pitch";
z.m_widget = "yaw";
x.m_title = tr("Gyro %1").arg("Roll");
y.m_title = tr("Gyro %1").arg("Pitch");
z.m_title = tr("Gyro %1").arg("Yaw");
// Add datasets to group
grp.m_datasets.append(x);
grp.m_datasets.append(y);
grp.m_datasets.append(z);
}
// Map widget
else if (widgetId == 3)
{
// Set widget title
grp.m_widget = "map";
grp.m_title = tr("GPS");
// Create datasets
JSON::Dataset lat, lon, alt;
// Set dataset indexes
lat.m_index = nextDatasetIndex();
lon.m_index = nextDatasetIndex() + 1;
alt.m_index = nextDatasetIndex() + 2;
// Set measurement units