-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathformgps_saveopen.cpp
More file actions
2859 lines (2338 loc) Β· 97.9 KB
/
formgps_saveopen.cpp
File metadata and controls
2859 lines (2338 loc) Β· 97.9 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) 2024 Michael Torrie and the QtAgOpenGPS Dev Team
// SPDX-License-Identifier: GNU General Public License v3.0 or later
//
// Main event loop save/load files from file manager to QtAOG
#include "formgps.h"
#include <QDir>
#include <algorithm>
#include "cboundarylist.h"
#include "classes/settingsmanager.h"
#include "qmlutil.h"
#include <QString>
#include <QLoggingCategory>
#include "backend.h"
#include "backendaccess.h"
#include "mainwindowstate.h"
#include "boundaryinterface.h"
#include "flagsinterface.h"
#include "recordedpath.h"
#include "worldgrid.h"
#include "siminterface.h"
#include "camera.h"
#include "backend/layerservice.h"
#include "scenegraph/layertypes.h"
Q_LOGGING_CATEGORY (formgps_saveopen_log, "formgps_saveopen.qtagopengps")
enum OPEN_FLAGS {
LOAD_MAPPING = 1,
LOAD_HEADLAND = 2,
LOAD_LINES = 4,
LOAD_FLAGS = 8
};
QString caseInsensitiveFilename(const QString &directory, const QString &filename)
{
//A bit of a hack to work with files from AOG that might not have
//the exact case we are expecting. For example, Boundaries.Txt and
//Headland.Txt (vs txt).
QStringList search;
QDir findDir(directory);
search << filename;
findDir.setNameFilters(search); //seems to be case insensitive
if (findDir.count() > 0)
return findDir[0];
else
return filename;
}
void FormGPS::ExportFieldAs_ISOXMLv3()
{
//TODO use xml library
}
void FormGPS::ExportFieldAs_ISOXMLv4()
{
//TODO use xml library
}
void FormGPS::FileSaveHeadLines()
{
#ifdef __ANDROID__
QString directoryName = androidDirectory + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#else
QString directoryName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/" + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#endif
QDir saveDir(directoryName);
if (!saveDir.exists()) {
bool ok = saveDir.mkpath(directoryName);
if (!ok) {
qWarning() << "Couldn't create path " << directoryName;
return;
}
}
QString filename = directoryName + "/" + caseInsensitiveFilename(directoryName, "HeadLines.txt");
QFile headfile(filename);
if (!headfile.open(QIODevice::WriteOnly))
{
qWarning() << "couldn't open " << filename << "for writing!";
return;
}
QTextStream writer(&headfile);
writer.setLocale(QLocale::C);
writer.setRealNumberNotation(QTextStream::FixedNotation);
int cnt = hdl.tracksArr.count();
writer << "$HeadLines" << Qt::endl;
if (cnt > 0)
{
for (int i = 0; i < cnt; i++)
{
//write out the name
writer << hdl.tracksArr[i].name << Qt::endl;
//write out the moveDistance
writer << hdl.tracksArr[i].moveDistance << Qt::endl;
//write out the mode
writer << hdl.tracksArr[i].mode << Qt::endl;
//write out the A_Point index
writer << hdl.tracksArr[i].a_point << Qt::endl;
//write out the points of ref line
int cnt2 = hdl.tracksArr[i].trackPts.count();
writer << cnt2 << Qt::endl;
if (hdl.tracksArr[i].trackPts.count() > 0)
{
for (int j = 0; j < cnt2; j++)
writer << qSetRealNumberPrecision(3) << hdl.tracksArr[i].trackPts[j].easting <<
"," << qSetRealNumberPrecision(3) << hdl.tracksArr[i].trackPts[j].northing <<
"," << qSetRealNumberPrecision(5) << hdl.tracksArr[i].trackPts[j].heading << Qt::endl;
}
}
}
else
{
writer << "$HeadLines" << Qt::endl;
return;
}
if (hdl.idx > (hdl.tracksArr.count() - 1)) hdl.idx = hdl.tracksArr.count() - 1;
headfile.close();
}
void FormGPS::FileLoadHeadLines()
{
#ifdef __ANDROID__
QString directoryName = androidDirectory + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#else
QString directoryName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/" + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#endif
QDir loadDir(directoryName);
if (!loadDir.exists()) {
bool ok = loadDir.mkpath(directoryName);
if (!ok) {
qWarning() << "Couldn't create path " << directoryName;
TimedMessageBox(1000,tr("Cannot create field directory!"),tr("Cannot create field directory at ") + directoryName);
return;
}
}
QString filename = directoryName + "/" + caseInsensitiveFilename(directoryName, "HeadLines.txt");
QFile headfile(filename);
if (!headfile.open(QIODevice::ReadOnly))
{
qWarning() << "Couldn't open " << filename << "for reading! Error was" << headfile.errorString();
TimedMessageBox(1000,tr("Cannot read headlines."),tr("Cannot read headlines.") + tr(" Error was ") + headfile.errorString());
return;
}
QTextStream reader(&headfile);
reader.setLocale(QLocale::C);
lock.lockForWrite();
hdl.tracksArr.clear();
hdl.idx = -1;
//get the file of previous AB Lines
QString line;
//read header $HeadLies
line = reader.readLine();
while (!reader.atEnd())
{
hdl.tracksArr.append(CHeadPath());
hdl.idx = hdl.tracksArr.count() - 1;
hdl.tracksArr[hdl.idx].name = reader.readLine();
line = reader.readLine();
hdl.tracksArr[hdl.idx].moveDistance = line.toDouble();
line = reader.readLine();
hdl.tracksArr[hdl.idx].mode = line.toInt();
line = reader.readLine();
hdl.tracksArr[hdl.idx].a_point = line.toInt();
line = reader.readLine();
int numPoints = (line.toInt());
if (numPoints > 3)
{
hdl.tracksArr[hdl.idx].trackPts.clear();
hdl.tracksArr[hdl.idx].trackPts.reserve(numPoints); // Phase 1.1: Pre-allocate
for (int i = 0; i < numPoints; i++)
{
line = reader.readLine();
// Phase 1.2: Parse without QStringList allocation
int comma1 = line.indexOf(',');
int comma2 = line.indexOf(',', comma1 + 1);
if (comma1 == -1 || comma2 == -1) {
qDebug() << "Corrupt file! Ignoring " << filename << ".";
hdl.tracksArr.clear();
hdl.idx = -1;
TimedMessageBox(1000,tr("Corrupt File!"), tr("Corrupt headline for this field. Deleting lines."));
FileSaveHeadLines();
}
Vec3 vecPt(QStringView(line).left(comma1).toDouble(),
QStringView(line).mid(comma1 + 1, comma2 - comma1 - 1).toDouble(),
QStringView(line).mid(comma2 + 1).toDouble());
hdl.tracksArr[hdl.idx].trackPts.append(vecPt);
}
}
else
{
if (hdl.tracksArr.count() > 0)
{
hdl.tracksArr.removeAt(hdl.idx);
}
}
}
hdl.idx = -1;
lock.unlock();
}
void FormGPS::FileSaveTracks()
{
BACKEND_TRACK(track);
#ifdef __ANDROID__
QString directoryName = androidDirectory + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#else
QString directoryName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/" + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#endif
QDir saveDir(directoryName);
if (!saveDir.exists()) {
bool ok = saveDir.mkpath(directoryName);
if (!ok) {
qWarning() << "Couldn't create path " << directoryName;
return;
}
}
QString filename = directoryName + "/" + caseInsensitiveFilename(directoryName, "TrackLines.txt");
int cnt = track.gArr.count();
QFile curveFile(filename);
if (!curveFile.open(QIODevice::WriteOnly))
{
qWarning() << "couldn't open " << filename << "for writing!";
return;
}
QTextStream writer(&curveFile);
writer.setLocale(QLocale::C);
writer.setRealNumberNotation(QTextStream::FixedNotation);
writer << "$TrackLines" << Qt::endl;
if (cnt > 0)
{
for (int i = 0; i < cnt; i++)
{
//write out the name
writer << track.gArr[i].name << Qt::endl;
//write out the heading
writer << track.gArr[i].heading << Qt::endl;
//A and B
writer << qSetRealNumberPrecision(3) << track.gArr[i].ptA.easting << ","
<< qSetRealNumberPrecision(3) << track.gArr[i].ptA.northing << ","
<< Qt::endl;
writer << qSetRealNumberPrecision(3) << track.gArr[i].ptB.easting << ","
<< qSetRealNumberPrecision(3) << track.gArr[i].ptB.northing << ","
<< Qt::endl;
//write out the nudgedistance
writer << track.gArr[i].nudgeDistance << Qt::endl;
//write out the mode
writer << track.gArr[i].mode << Qt::endl;
//visible?
if (track.gArr[i].isVisible)
writer << "True" << Qt::endl;
else
writer << "False" << Qt::endl;
//write out the points of ref line
int cnt2 = track.gArr[i].curvePts.count();
writer << cnt2 << Qt::endl;
if (track.gArr[i].curvePts.count() > 0)
{
for (int j = 0; j < cnt2; j++)
writer << qSetRealNumberPrecision(3) << track.gArr[i].curvePts[j].easting << ","
<< qSetRealNumberPrecision(3) << track.gArr[i].curvePts[j].northing << ","
<< qSetRealNumberPrecision(3) << track.gArr[i].curvePts[j].heading
<< Qt::endl;
}
}
}
FileSaveABLines();
FileSaveCurveLines();
curveFile.close();
}
void FormGPS::FileLoadTracks()
{
BACKEND_TRACK(track);
track.gArr.clear();
//current field directory should already exist
#ifdef __ANDROID__
QString directoryName = androidDirectory + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#else
QString directoryName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/" + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#endif
QDir loadDir(directoryName);
if (!loadDir.exists()) {
bool ok = loadDir.mkpath(directoryName);
if (!ok) {
qWarning() << "Couldn't create path " << directoryName;
TimedMessageBox(1000,tr("Cannot create field directory!"),tr("Cannot create field directory at ") + directoryName);
return;
}
}
QString filename = directoryName + "/" + caseInsensitiveFilename(directoryName, "TrackLines.txt");
QFile headfile(filename);
if (!headfile.open(QIODevice::ReadOnly))
{
FileLoadABLines();
FileLoadCurveLines();
FileSaveTracks();
track.reloadModel();
return;
}
QTextStream reader(&headfile);
reader.setLocale(QLocale::C);
QString line;
//read header $CurveLine
line = reader.readLine();
lock.lockForWrite();
while (!reader.atEnd())
{
line = reader.readLine();
if(line.isNull()) break; //no more to read
track.gArr.append(CTrk());
track.setIdx(track.gArr.count() - 1);
//read header $CurveLine
track.gArr[track.idx()].name = line;
track.gArr[track.idx()].heading = reader.readLine().toDouble();
line = reader.readLine();
// Phase 1.2: Parse without QStringList allocation
int comma = line.indexOf(',');
if (comma == -1) {
TimedMessageBox(1000,tr("Corrupt File!"), tr("Corrupt TracksList.txt. Not all tracks were loaded."));
track.gArr.pop_back();
track.setIdx(track.gArr.count() - 1);
return;
}
track.gArr[track.idx()].ptA = Vec2(QStringView(line).left(comma).toDouble(),
QStringView(line).mid(comma + 1).toDouble());
line = reader.readLine();
comma = line.indexOf(',');
if (comma == -1) {
TimedMessageBox(1000,tr("Corrupt File!"), tr("Corrupt TracksList.txt. Not all tracks were loaded."));
track.gArr.pop_back();
track.setIdx(track.gArr.count() - 1);
return;
}
track.gArr[track.idx()].ptB = Vec2(QStringView(line).left(comma).toDouble(),
QStringView(line).mid(comma + 1).toDouble());
line = reader.readLine();
track.gArr[track.idx()].nudgeDistance = line.toDouble();
line = reader.readLine();
track.gArr[track.idx()].mode = line.toInt();
line = reader.readLine();
if (line == "True")
track.gArr[track.idx()].isVisible = true;
else
track.gArr[track.idx()].isVisible = false;
line = reader.readLine();
int numPoints = line.toInt();
if (numPoints > 3)
{
track.gArr[track.idx()].curvePts.clear();
track.gArr[track.idx()].curvePts.reserve(numPoints); // Phase 1.1: Pre-allocate
for (int i = 0; i < numPoints; i++)
{
line = reader.readLine();
// Phase 1.2: Parse without QStringList allocation
int comma1 = line.indexOf(',');
int comma2 = line.indexOf(',', comma1 + 1);
if (comma1 == -1 || comma2 == -1) {
TimedMessageBox(1000,tr("Corrupt File!"), tr("Corrupt TracksList.txt. Not all tracks were loaded."));
track.gArr.pop_back();
track.setIdx(track.gArr.count() - 1);
return;
}
track.gArr[track.idx()].curvePts.append(Vec3(QStringView(line).left(comma1).toDouble(),
QStringView(line).mid(comma1 + 1, comma2 - comma1 - 1).toDouble(),
QStringView(line).mid(comma2 + 1).toDouble()));
}
}
}
track.setIdx(-1);
lock.unlock();
track.reloadModel();
}
void FormGPS::FileSaveCurveLines()
{
BACKEND_TRACK(track);
#ifdef __ANDROID__
QString directoryName = androidDirectory + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#else
QString directoryName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/" + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#endif
QDir saveDir(directoryName);
if (!saveDir.exists()) {
bool ok = saveDir.mkpath(directoryName);
if (!ok) {
qWarning() << "Couldn't create path " << directoryName;
return;
}
}
QString filename = directoryName + "/" + caseInsensitiveFilename(directoryName, "CurveLines.txt");
int cnt = track.gArr.count();
QFile curveFile(filename);
if (!curveFile.open(QIODevice::WriteOnly))
{
qWarning() << "couldn't open " << filename << "for writing!";
return;
}
QTextStream writer(&curveFile);
writer.setLocale(QLocale::C);
writer.setRealNumberNotation(QTextStream::FixedNotation);
writer << "$CurveLines" << Qt::endl;
for (int i = 0; i < cnt; i++)
{
if (track.gArr[i].mode != TrackMode::Curve) continue;
//write out the Name
writer << track.gArr[i].name << Qt::endl;
//write out the heading
writer << track.gArr[i].heading << Qt::endl;
//write out the points of ref line
int cnt2 = track.gArr[i].curvePts.count();
writer << cnt2 << Qt::endl;
if (track.gArr[i].curvePts.count() > 0)
{
for (int j = 0; j < cnt2; j++)
writer << qSetRealNumberPrecision(3) << track.gArr[i].curvePts[j].easting << ","
<< qSetRealNumberPrecision(3) << track.gArr[i].curvePts[j].northing << ","
<< qSetRealNumberPrecision(5) << track.gArr[i].curvePts[j].heading << Qt::endl;
}
}
curveFile.close();
}
void FormGPS::FileLoadCurveLines()
{
BACKEND_TRACK(track);
//This method is only used if there is no TrackLines.txt and we are importing the old
//CurveLines.txtfile
//current field directory should already exist
#ifdef __ANDROID__
QString directoryName = androidDirectory + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#else
QString directoryName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/" + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#endif
QDir loadDir(directoryName);
if (!loadDir.exists()) {
bool ok = loadDir.mkpath(directoryName);
if (!ok) {
qWarning() << "Couldn't create path " << directoryName;
return;
}
}
QString filename = directoryName + "/" + caseInsensitiveFilename(directoryName, "CurveLines.txt");
QFile curveFile(filename);
if (!curveFile.open(QIODevice::ReadOnly))
{
TimedMessageBox(1500, tr("Field Error"), (tr("Couldn't open ") + filename + tr("for reading!")));
return;
}
QTextStream reader(&curveFile);
reader.setLocale(QLocale::C);
QString line;
//read header $CurveLine
line = reader.readLine();
lock.lockForWrite();
while (!reader.atEnd())
{
line = reader.readLine();
if(line.isNull()) break; //no more to read
track.gArr.append(CTrk());
//read header $CurveLine
QString nam = reader.readLine();
if (nam.length() > 4 && nam.mid(0,5) == "Bound")
{
track.gArr[track.gArr.count() - 1].name = nam;
track.gArr[track.gArr.count() - 1].mode = TrackMode::bndCurve;
}
else
{
if (nam.length() > 2 && nam.mid(0,2) != "Cu")
track.gArr[track.gArr.count() - 1].name = "Cu " + nam;
else
track.gArr[track.gArr.count() - 1].name = nam;
track.gArr[track.gArr.count() - 1].mode = TrackMode::Curve;
}
// get the average heading
line = reader.readLine();
track.gArr[track.gArr.count() - 1].heading = line.toDouble();
line = reader.readLine();
int numPoints = line.toInt();
if (numPoints > 1)
{
track.gArr[track.gArr.count() - 1].curvePts.clear();
track.gArr[track.gArr.count() - 1].curvePts.reserve(numPoints); // Phase 1.1: Pre-allocate
for (int i = 0; i < numPoints; i++)
{
line = reader.readLine();
// Phase 1.2: Parse without QStringList allocation
int comma1 = line.indexOf(',');
int comma2 = line.indexOf(',', comma1 + 1);
if (comma1 == -1 || comma2 == -1) {
qDebug() << "Corrupt CurvesList.txt.";
track.gArr.pop_back();
track.setIdx(-1);
return;
}
Vec3 vecPt(QStringView(line).left(comma1).toDouble(),
QStringView(line).mid(comma1 + 1, comma2 - comma1 - 1).toDouble(),
QStringView(line).mid(comma2 + 1).toDouble());
track.gArr[track.gArr.count() - 1].curvePts.append(vecPt);
}
track.gArr[track.gArr.count() - 1].ptB.easting = track.gArr[track.gArr.count() - 1].curvePts[0].easting;
track.gArr[track.gArr.count() - 1].ptB.northing = track.gArr[track.gArr.count() - 1].curvePts[0].northing;
track.gArr[track.gArr.count() - 1].ptB.easting = track.gArr[track.gArr.count() - 1].curvePts[track.gArr[track.gArr.count() - 1].curvePts.count() - 1].easting;
track.gArr[track.gArr.count() - 1].ptB.northing = track.gArr[track.gArr.count() - 1].curvePts[track.gArr[track.gArr.count() - 1].curvePts.count() - 1].northing;
track.gArr[track.gArr.count() - 1].isVisible = true;
}
else
{
if (track.gArr.count() > 0)
{
track.gArr.pop_back();
}
}
}
lock.unlock();
curveFile.close();
}
void FormGPS::FileSaveABLines()
{
BACKEND_TRACK(track);
#ifdef __ANDROID__
QString directoryName = androidDirectory + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#else
QString directoryName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/" + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#endif
QDir saveDir(directoryName);
if (!saveDir.exists()) {
bool ok = saveDir.mkpath(directoryName);
if (!ok) {
qWarning() << "Couldn't create path " << directoryName;
return;
}
}
QString filename = directoryName + "/" + caseInsensitiveFilename(directoryName, "ABLines.txt" );
QFile lineFile(filename);
if (!lineFile.open(QIODevice::WriteOnly))
{
qWarning() << "Couldn't open " << filename << "for writing!";
return;
}
QTextStream writer(&lineFile);
writer.setLocale(QLocale::C);
writer.setRealNumberNotation(QTextStream::FixedNotation);
int cnt = track.gArr.count();
if (cnt > 0)
{
for (CTrk &item : track.gArr)
{
if (item.mode == TrackMode::AB)
{
//make it culture invariant
writer << item.name << ","
<< qSetRealNumberPrecision(8) << glm::toDegrees(item.heading) << ","
<< qSetRealNumberPrecision(3) << item.ptA.easting << ","
<< qSetRealNumberPrecision(3) << item.ptA.northing << Qt::endl;
}
}
}
lineFile.close();
}
void FormGPS::FileLoadABLines()
{
//This method is only used if TracksLines.txt is not present. This loads the old ABLines.txt
//into the new unified tracks system. When importing the old lines files, this method must
//run before FileLoadCurveLines().
//current field directory should already exist
BACKEND_TRACK(track);
#ifdef __ANDROID__
QString directoryName = androidDirectory + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#else
QString directoryName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/" + QCoreApplication::applicationName() + "/Fields/" + currentFieldDirectory;
#endif
QDir loadDir(directoryName);
if (!loadDir.exists()) {
bool ok = loadDir.mkpath(directoryName);
if (!ok) {
qWarning() << "Couldn't create path " << directoryName;
return;
}
}
QString filename = directoryName + "/" + caseInsensitiveFilename(directoryName, "ABLines.txt");
QFile linesFile(filename);
if (!linesFile.open(QIODevice::ReadOnly))
{
TimedMessageBox(1500, tr("Field Error"), (tr("Couldn't open ") + filename + tr(" for reading!")));
return;
}
QTextStream reader(&linesFile);
reader.setLocale(QLocale::C);
QString line;
lock.lockForWrite();
//read all the lines
for (int i = 0; !reader.atEnd(); i++)
{
line = reader.readLine();
// Phase 1.2: Parse without QStringList allocation
int comma1 = line.indexOf(',');
int comma2 = line.indexOf(',', comma1 + 1);
int comma3 = line.indexOf(',', comma2 + 1);
if (comma1 == -1 || comma2 == -1 || comma3 == -1) {
qDebug() << "Corrupt ABLines.txt.";
return;
}
track.gArr.append(CTrk());
QString name = line.left(comma1);
if (name.length() > 2 && name.mid(0,2) != "AB")
track.gArr[i].name = "AB " + name;
else
track.gArr[i].name = name;
track.gArr[i].mode = TrackMode::AB;
track.gArr[i].heading = glm::toRadians(QStringView(line).mid(comma1 + 1, comma2 - comma1 - 1).toDouble());
track.gArr[i].ptA.easting = QStringView(line).mid(comma2 + 1, comma3 - comma2 - 1).toDouble();
track.gArr[i].ptB.northing = QStringView(line).mid(comma3 + 1).toDouble();
track.gArr[i].ptB.easting = track.gArr[i].ptA.easting + (sin(track.gArr[i].heading) * 100);
track.gArr[i].ptB.northing = track.gArr[i].ptA.northing + (cos(track.gArr[i].heading) * 100);
track.gArr[i].isVisible = true;
}
lock.unlock();
linesFile.close();
}
QMap<QString,QVariant> FormGPS::FileFieldInfo(QString filename)
{
QMap<QString,QVariant> field_info;
QString directoryName = filename.left(filename.indexOf("/Field.txt"));
QString fieldDir = directoryName.mid(filename.lastIndexOf("Fields/") + 7);
QFile fieldFile(filename);
if (!fieldFile.open(QIODevice::ReadOnly))
{
TimedMessageBox(5000, tr("Field Error"), (fieldDir + tr(" is missing Field.txt! It will be ignored and should probably be deleted.")));
return field_info;
}
QTextStream reader(&fieldFile);
reader.setLocale(QLocale::C);
//start to read the file
QString line;
//Date time line
line = reader.readLine();
//dir header $FieldDir
line = reader.readLine();
//read field directory
line = reader.readLine();
field_info["name"] = line.trimmed();
if (field_info["name"] != fieldDir.trimmed()) {
field_info["name"] = fieldDir;
}
//Offset header
line = reader.readLine();
//read the Offsets
line = reader.readLine();
// Phase 1.2: Parse without QStringList allocation (offsets not used currently)
// QStringList offs = line.split(',');
//convergence angle update
if (!reader.atEnd())
{
line = reader.readLine(); //Convergence
line = reader.readLine();
}
//start positions
if (!reader.atEnd())
{
line = reader.readLine(); //eat StartFix
line = reader.readLine();
// Phase 1.2: Parse without QStringList allocation
int comma = line.indexOf(',');
if (comma != -1) {
field_info["latitude"] = QStringView(line).left(comma).toDouble();
field_info["longitude"] = QStringView(line).mid(comma + 1).toDouble();
}
}
fieldFile.close();
//Boundaries
filename = QDir(directoryName).filePath(caseInsensitiveFilename(directoryName, "Boundary.txt"));
double area = CBoundary::getSavedFieldArea(filename);
if (area>0) {
field_info["hasBoundary"] = true;
field_info["boundaryArea"] = area;
} else {
field_info["hasBoundary"] = false;
field_info["boundaryArea"] = (double)-10;
}
return field_info;
}
bool FormGPS::FileOpenField(QString fieldDir, int flags)
{
CNMEA &pn = *Backend::instance()->pn();
WorldGrid &worldGrid = *WorldGrid::instance();
BACKEND_TRACK(track);
CContour &ct = track.contour;
Camera &camera = *Camera::instance();
#ifdef __ANDROID__
QString directoryName = androidDirectory + QCoreApplication::applicationName() + "/Fields/" + fieldDir;
#else
QString directoryName = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)
+ "/" + QCoreApplication::applicationName() + "/Fields/" + fieldDir;
#endif
QString filename = directoryName + "/" + caseInsensitiveFilename(directoryName, "Field.txt");
QFile fieldFile(filename);
if (!fieldFile.open(QIODevice::ReadOnly))
{
TimedMessageBox(1500, tr("Field Error"), (tr("Couldn't open field ") + filename + tr(" for reading!")));
return false;
}
QTextStream reader(&fieldFile);
reader.setLocale(QLocale::C);
//close the existing job and reset everything
JobClose();
//and open a new job
JobNew();
bnd.loadSettings();
//Saturday, February 11, 2017 --> 7:26:52 AM
//$FieldDir
//Bob_Feb11
//$Offsets
//533172,5927719,12 - offset easting, northing, zone
//start to read the file
QString line;
//Date time line
line = reader.readLine();
//dir header $FieldDir
line = reader.readLine();
//read field directory
line = reader.readLine();
currentFieldDirectory = fieldDir;
SettingsManager::instance()->setF_currentDir(currentFieldDirectory);
//Offset header
line = reader.readLine();
//read the Offsets
line = reader.readLine();
// Phase 1.2: Parse without QStringList allocation (currently commented out)
// QStringList offs = line.split(',');
//pn.utmEast = offs[0].toInt();
//pn.utmNorth = offs[1].toInt();
//pn.actualEasting = offs[0].toDouble();
//pn.actualNorthing = offs[1].toDouble();
//pn.zone = offs[2].toInt();
//isFirstFixPositionSet = true;
//convergence angle update
if (!reader.atEnd())
{
line = reader.readLine(); //Convergence
line = reader.readLine();
//pn.convergenceAngle = line.toDouble();
//TODO lblConvergenceAngle.Text = Math.Round(glm.toDegrees(pn.convergenceAngle), 3).ToString();
}
//start positions
if (!reader.atEnd())
{
line = reader.readLine(); //eat StartFix
line = reader.readLine();
// Phase 1.2: Parse without QStringList allocation
int comma = line.indexOf(',');
if (comma != -1) {
// Phase 6.3.1: Use PropertyWrapper for safe property access
pn.setLatStart(QStringView(line).left(comma).toDouble());
pn.setLonStart(QStringView(line).mid(comma + 1).toDouble());
}
// Qt 6.8 TRACK RESTORATION: Load active track index if present
if (!reader.atEnd())
{
line = reader.readLine(); //check for $ActiveTrackIndex
if (line == "$ActiveTrackIndex")
{
line = reader.readLine();
int activeTrackIndex = line.toInt();
qDebug() << "π― TRACK RESTORE: Found saved active track index:" << activeTrackIndex;
track.setIdx(activeTrackIndex);
}
else
{
// No $ActiveTrackIndex found, reset to no active track
qDebug() << "π TRACK RESTORE: No saved track index found, defaulting to -1";
track.setIdx(-1);
}
}
else
{
// File ended, no track index
qDebug() << "π TRACK RESTORE: Field file ended before track index, defaulting to -1";
track.setIdx(-1);
}
if (SimInterface::instance()->isRunning())
{
// Phase 6.3.1: Use PropertyWrapper for safe property access
pn.latitude = pn.latStart();
pn.longitude = pn.lonStart();
SettingsManager::instance()->setGps_simLatitude(pn.latStart());
SettingsManager::instance()->setGps_simLongitude(pn.lonStart());
SimInterface::instance()->reset();
pn.SetLocalMetersPerDegree();
} else {
// Phase 6.0.4: Use Q_PROPERTY direct access instead of qmlItem
pn.SetLocalMetersPerDegree();
}
}
fieldFile.close();
if (flags & LOAD_LINES) {
// Qt 6.8 TRACK RESTORATION: Save restored track index before FileLoadTracks() overwrites it
int savedActiveTrackIndex = track.idx();
qDebug() << "πΎ TRACK RESTORE: Saving restored index before track loading:" << savedActiveTrackIndex;
// ABLine -------------------------------------------------------------------------------------------------
FileLoadTracks();
// Qt 6.8 TRACK RESTORATION: Restore the saved track index after loading
if (savedActiveTrackIndex >= 0 && savedActiveTrackIndex < track.gArr.count()) {
track.setIdx(savedActiveTrackIndex);
qDebug() << "β
TRACK RESTORE: Restored active track index after loading:" << savedActiveTrackIndex;
} else if (track.gArr.count() > 0) {
// If saved index is invalid but we have tracks, select first track
track.setIdx(0);
qDebug() << "π TRACK RESTORE: Invalid saved index, defaulting to first track (0)";
} else {
// No tracks available, keep -1
qDebug() << "β TRACK RESTORE: No tracks available, keeping idx = -1";
}
}