-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdat_file.cpp
More file actions
1113 lines (984 loc) · 34.6 KB
/
dat_file.cpp
File metadata and controls
1113 lines (984 loc) · 34.6 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
/*
File : dat_file.cpp
Project : splat_GUI
-----------------------------------------------------------
Copyright 2022, Agafangelos Dmitriy, Vladimirov Sergey <vladimirov.opds@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
Description : .dat file creating form
*/
#include "dat_file.h"
#include "ui_dat_file.h"
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QAbstractItemView>
#include <QProcess>
bool is_not_empty_d(QString str1);
QString DD_to_DMS_d(QString param);
QString DMS_to_DD_d(QString param);
bool isDMS_format_d(QString str);
bool isDD_format_d(QString str);
bool islongitude_d(QString str);
bool islatitude_d(QString str);
dat_file::dat_file(QWidget *parent) :
QDialog(parent),
ui(new Ui::dat_file)
{
ui->setupUi(this);
QString file_name = tr("Untitled");
set = new Settings(this);
this->setWindowTitle(file_name);
ui->DD_format_latitude->setChecked(true);
ui->DD_format_longitude->setChecked(true);
ui->city_line->addItem("None");
ui->city_line->view()->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
refresh_file_data();
}
dat_file::~dat_file()
{
delete ui;
}
bool is_not_empty_d(QString str){
if (str.trimmed() == ""){
return false;
} else{
return true;
}
}
bool isDD_format_d(QString str)
{
QStringList str_s = str.split(" ");
if ( str_s.length() != 1){
return false;
} else {
QRegularExpression re("\\d*");
QRegularExpression str_t("\\D");
if (!re.match(str_s[0]).hasMatch()){
return false;
} else if (str_s[0].toFloat() >= 0 && (!str_t.match(str_s[0]).hasMatch() || (str_s[0].contains(".") && str_s[0].toFloat() != 0 ))) {
return true;
} else {
return false;
}
}
}
bool isDMS_format_d(QString str)
{
QStringList str_s = str.split(" ");
if ( str_s.length()!=3){
return false;
} else {
QRegularExpression re("\\d*");
if (!re.match(str_s[0]).hasMatch()){
return false;
} else if (!re.match(str_s[1]).hasMatch()){
return false;
} else if (!re.match(str_s[2]).hasMatch()){
return false;
} else if (DMS_to_DD_d(str).toFloat() <= 180 && DMS_to_DD_d(str).toFloat() >= 0 && str_s[1].toInt() >= 0 && str_s[1].toInt() < 60 && str_s[2].toFloat() >= 0 && str_s[2].toFloat() < 60 ) {
return true;
} else {
return false;
}
}
}
bool islongitude_d(QString str)
{
QStringList str_s = str.split(" ");
float longitude = str.toFloat();
if (isDMS_format_d(str))
{
if (str_s[0].toInt() >= 0 && str_s[0].toInt()<= 180 )
{
return true;
} else
{
return false;
}
} else if (isDD_format_d(str))
{
if (longitude >= 0 && longitude <= 180)
{
return true;
} else
{
return false;
}
} else {
return false;
}
}
bool islatitude_d(QString str)
{
QStringList str_s = str.split(" ");
float longitude = str.toFloat();
if (isDMS_format_d(str))
{
if (str_s[0].toInt() >= 0 && str_s[0].toInt() <= 90 )
{
return true;
} else
{
return false;
}
} else if (isDD_format_d(str))
{
if (longitude >= 0 && longitude <= 90)
{
return true;
} else
{
return false;
}
} else {
return false;
}
}
QString DD_to_DMS_d(QString param){
std::string str = param.toStdString();
float param_f = param.toFloat();
int D = std::stoi( str );
if (D < 0){
D *= -1;
}
if (param_f < 0 ){
param_f *= -1;
}
float arg_M = (param_f - D) * 60;
int M = (int) arg_M;
float S = (arg_M - M ) * 60;
QString D_str = QString::number(D);
QString M_str = QString::number(M);
QString S_str = QString::number(S);
QString DM = D_str + " " + M_str;
QString DMS = DM + " " + S_str;
if (D < 0){
return "-" + DMS;
} else {
return DMS;
}
}
QString DMS_to_DD_d(QString param){
QString D_m = param.section(" ",0,0);
if (D_m.toInt() < 0){
D_m = D_m.section("-",1,1);
}
QString M = param.section(" ",1,1);
QString S = param.section(" ",2,2);
float D_arg = D_m.toFloat();
float M_arg = M.toFloat();
float S_arg = S.toFloat();
float DD_str = D_arg + M_arg / 60.0 + S_arg / 3600.0;
QString DD = QString::number(DD_str);
if (D_m.contains("-")){
return "-" + DD;
} else {
return DD;
}
}
void dat_file::set_to_none()
{
ui->file_name_2->clear();
ui->field->clear();
ui-> file_name->clear();
ui->city_line->clear();
ui->city_line->addItem("None");
ui->site_name->clear();
ui->latitude->clear();
ui->longitude->clear();
ui->DD_format_latitude->setChecked(true);
ui->DD_format_longitude->setChecked(true);
ui->DMS_format_latitude->setChecked(false);
ui->DMS_format_longitude->setChecked(false);
ui->latitude_choose->setCurrentIndex(0);
ui->longitude_choose->setCurrentIndex(0);
this->setWindowTitle(tr("Untitled"));
}
void dat_file::set_to_none_city()
{
int none_line = ui->city_line->findText("None");
ui->city_line->setCurrentIndex(none_line);
ui->site_name->clear();
ui->latitude->clear();
ui->longitude->clear();
ui->DD_format_latitude->setChecked(true);
ui->DD_format_longitude->setChecked(true);
ui->DMS_format_latitude->setChecked(false);
ui->DMS_format_longitude->setChecked(false);
ui->latitude_choose->setCurrentIndex(0);
ui->longitude_choose->setCurrentIndex(0);
}
void dat_file::openFile(QString file_name){
QFile file_check(file_name);
if (file_check.exists()){
if (is_city_format(file_name) ){
refresh_file_data();
this->setWindowTitle(file_name);
} else {
QMessageBox::critical(this,tr("Error"),tr("Invalid format"));
set_to_none();
refresh_file_data();
}
} else {
QMessageBox::critical(this,tr("Error"),tr("File is not exist"));
set_to_none();
refresh_file_data();
}
}
void dat_file::refresh_file_data()
{
QString str_l;
QStringList list;
QFile fileIn(ui->file_name->text());
if (fileIn.open(QIODevice::ReadOnly)){
str_l = fileIn.readAll();
list = str_l.split("\n");
for (int i = 0; i < list.length();i++){
if (ui->city_line->findText(list[i].section(", ",0,0).trimmed()) == -1 && list[i].section(", ",0,0).trimmed() != ""){
ui->city_line->insertItem(i+1,list[i].section(", ",0,0).trimmed());
}
}
fileIn.close();
}
int count = 0;
for (int i = 0; i< ui->city_line->count(); i++){
if (fileIn.open(QIODevice::ReadOnly)){
str_l = fileIn.readAll();
list = str_l.split("\n");
for (int j = 0; j < list.length();j++){
if (list[j].section(", ",0,0).trimmed() == ui->city_line->itemText(i) || ui->city_line->itemText(i) == "None"){
count ++;
}
}
if (count == 0){
ui->city_line->removeItem(i);
count = 0;
} else {
count = 0;
}
}
fileIn.close();
}
if (windowTitle() != tr("Untitled")){
ui->file_name->setText(this->windowTitle());
ui->file_name_2->setText(this->windowTitle());
QString read_params;
if (fileIn.open(QIODevice::ReadOnly)){
read_params = fileIn.readAll();
}
fileIn.close();
ui->field->clear();
ui->field->appendPlainText(read_params);
}
if (windowTitle() != tr("Untitled") && ui->city_line->currentText() != "None") {
if (is_city_format(ui->file_name->text())){
ui->tab->setEnabled(true);
QFile fileIn(ui->file_name->text()); //file in stream
QString read_params; //input stream's variable
QStringList list_str;
QString name_site;
QString latitude;
QString longitude;
int index_line = ui->city_line->currentIndex();
if (fileIn.open(QIODevice::ReadOnly)){
read_params = fileIn.readAll();
list_str = read_params.split("\n");
for (int i = 0; i < list_str.length();i++){
if (i == index_line - 1){
latitude = list_str[i].section(", ",1,1).trimmed();
longitude = list_str[i].section(", ",2,2).trimmed();
name_site =list_str[i].section(", ",0,0).trimmed();
}}
if (latitude.contains("-")){
latitude = latitude.section("-",1,1);
ui->latitude_choose->setCurrentIndex(1);
} else {
ui->latitude_choose->setCurrentIndex(0);
}
if (longitude.contains("-")){
longitude = longitude.section("-",1,1);
ui->longitude_choose->setCurrentIndex(0);
} else {
ui->longitude_choose->setCurrentIndex(1);
}
ui->site_name->setText(name_site);
ui->latitude->setText(latitude);
ui->longitude->setText(longitude);
if (isDD_format_d(latitude)){ //Set combobox DD or DMS format
ui->DD_format_latitude->setChecked(true);
ui->DMS_format_latitude->setChecked(false);
} else {
ui->DD_format_latitude->setChecked(false);
ui->DMS_format_latitude->setChecked(true);
}
if (isDD_format_d(longitude)){ //Set combobox DD or DMS format
ui->DD_format_longitude->setChecked(true);
ui->DMS_format_longitude->setChecked(false);
} else {
ui->DD_format_longitude->setChecked(false);
ui->DMS_format_longitude->setChecked(true);
}
}
fileIn.close();
} else {
QString read_params;
QString file_name = ui->file_name_2->text().trimmed();
QFile fileIn(file_name);
if (fileIn.open(QIODevice::ReadOnly)){
read_params = fileIn.readAll();
ui->field->clear();
ui->field->appendPlainText(read_params);
}
fileIn.close();
ui->tab->setEnabled(false);
ui->tabWidget->setCurrentIndex(1);
} } else {
set_to_none_city();
}
}
void dat_file::on_Resfresh_file_list_clicked()
{
refresh_file_data();
}
void dat_file::on_add_line_clicked()
{
QString title = this->windowTitle();
if (title == tr("Untitled")){
QString site_n = ui->site_name->text().trimmed() + ", ";
QString latitude = ui->latitude->text().trimmed() + ", ";
QString longitude = ui->longitude->text().trimmed();
if (ui->city_line->findText(ui->site_name->text()) != -1){
QMessageBox::critical(this,tr ("Error"),tr("Invalid site name (duplicate)"));
} else if (!islatitude_d(ui->latitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid latitude value"));
} else if (!islongitude_d(ui->longitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid longitude value"));
} else if (!is_not_empty_d(ui->site_name->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid site name"));
}
else{
QString file_name = QFileDialog::getSaveFileName(this,
tr("Save dat file"),".cdat",
tr("*.cdat"));
QFile fileout(file_name);
if (islatitude_d(ui->latitude->text().trimmed()) && ui->latitude_choose->currentIndex() == 1){
latitude = "-"+latitude;
}
if (islongitude_d(ui->longitude->text().trimmed()) && ui->longitude_choose->currentIndex() == 0){
longitude = "-" + longitude;
}
QString str_ll = latitude + longitude;
QString str = site_n + str_ll;
if (fileout.open(QIODevice::WriteOnly)){
QTextStream out(&fileout);
out << str.trimmed()<<Qt::endl;
fileout.close();
} else {
qDebug()<< "Don't open this file";
}
this->setWindowTitle(file_name);
ui->city_line->addItem(site_n.section(", ",0,0));
int city_index = ui->city_line->findText(site_n.section(", ",0,0));
refresh_file_data();
ui->city_line->setCurrentIndex(city_index);
refresh_file_data();
}
} else {
QString file_name = title;
QFile file_check(file_name);
if (file_check.exists()){
QString site_n = ui->site_name->text().trimmed() + ", ";
QString latitude = ui->latitude->text().trimmed() + ", ";
QString longitude = ui->longitude->text().trimmed();
if (ui->city_line->findText(ui->site_name->text()) != -1){
QMessageBox::critical(this,tr ("Error"),tr("Invalid site name (duplicate)"));
} else if (!islatitude_d(ui->latitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid latitude value"));
} else if (!islongitude_d(ui->longitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid longitude value"));
} else if (!is_not_empty_d(ui->site_name->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid site name"));
}
else{
QFile fileout(file_name);
if (islatitude_d(ui->latitude->text().trimmed()) && ui->latitude_choose->currentIndex() == 1){
latitude = "-"+latitude;
}
if (islongitude_d(ui->longitude->text().trimmed()) && ui->longitude_choose->currentIndex() == 0){
longitude = "-" + longitude;
}
QString str_ll = latitude + longitude;
QString str = site_n + str_ll;
if (fileout.open(QIODevice::Append)){
QTextStream out(&fileout);
out << str.trimmed()<<Qt::endl;
fileout.close();
} else {
qDebug()<< "Don't open this file";
}
ui->city_line->addItem(site_n.section(", ",0,0));
int city_index = ui->city_line->findText(site_n.section(", ",0,0));
refresh_file_data();
ui->city_line->setCurrentIndex(city_index);
refresh_file_data();
}
} else {
QMessageBox::critical(this,tr("Error"), tr("File is not exist"));
refresh_file_data();
set_to_none();
}
}
}
void dat_file::on_save_line_clicked()
{
QString title = this->windowTitle();
if (title == tr("Untitled")){
QString site_n = ui->site_name->text().trimmed() + ", ";
QString latitude = ui->latitude->text().trimmed() + ", ";
QString longitude = ui->longitude->text().trimmed();
if (ui->city_line->findText(ui->site_name->text()) != -1){
QMessageBox::critical(this,tr ("Error"),tr("Invalid site name (duplicate)"));
} else if (!islatitude_d(ui->latitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid latitude value"));
} else if (!islongitude_d(ui->longitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid longitude value"));
} else if (!is_not_empty_d(ui->site_name->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid site name"));
}
else {
QString file_name = QFileDialog::getSaveFileName(this,
tr("Save dat file"),".cdat",
tr("*.cdat"));
QFile fileout(file_name);
if (islatitude_d(ui->latitude->text().trimmed()) && ui->latitude_choose->currentIndex() == 1){
latitude = "-"+latitude;
}
if (islongitude_d(ui->longitude->text().trimmed()) && ui->longitude_choose->currentIndex() == 0){
longitude = "-" + longitude;
}
QString str_ll = latitude + longitude;
QString str = site_n + str_ll;
if (fileout.open(QIODevice::WriteOnly)){
QTextStream out(&fileout);
out << str.trimmed()<<Qt::endl;
fileout.close();
} else {
qDebug()<< "Don't open this file";
}
this->setWindowTitle(file_name);
ui->city_line->addItem(site_n.section(", ",0,0));
int city_index = ui->city_line->findText(site_n.section(", ",0,0).trimmed());
ui->city_line->setCurrentIndex(city_index);
refresh_file_data();
}
} else if (ui->city_line->currentText() == "None") {
QString file_name = title;
QFile file_check(file_name);
if (file_check.exists()){
QString site_n = ui->site_name->text().trimmed()+ ", ";
QString latitude = ui->latitude->text().trimmed() + ", ";
QString longitude = ui->longitude->text().trimmed();
if (ui->city_line->findText(ui->site_name->text()) != -1){
QMessageBox::critical(this,tr ("Error"),tr("Invalid site name (duplicate)"));
} else if (!islatitude_d(ui->latitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid latitude value"));
} else if (!islongitude_d(ui->longitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid longitude value"));
} else if (!is_not_empty_d(ui->site_name->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid site name"));
}
else{
if (islatitude_d(ui->latitude->text().trimmed()) && ui->latitude_choose->currentIndex() == 1){
latitude = "-"+latitude;
}
if (islongitude_d(ui->longitude->text().trimmed()) && ui->longitude_choose->currentIndex() == 0){
longitude = "-" + longitude;
}
QString str_ll = latitude + longitude;
QString str = site_n + str_ll;
QFile fileout(file_name);
if (fileout.open(QIODevice::Append)){
QTextStream out(&fileout);
out << str.trimmed()<<Qt::endl;
fileout.close();
} else {
qDebug()<< "Don't open this file";
}
ui->city_line->addItem(site_n.section(", ",0,0));
int city_index = ui->city_line->findText(site_n.section(", ",0,0).trimmed());
ui->city_line->setCurrentIndex(city_index);
refresh_file_data();
}
} else {
QMessageBox::critical(this,tr("Error"), tr("File is not exist"));
refresh_file_data();
set_to_none();
}
} else {
QString file_name = title;
QFile file_check(file_name);
if (file_check.exists()){
QString site_n = ui->site_name->text().trimmed()+ ", ";
QString latitude = ui->latitude->text().trimmed() + ", ";
QString longitude = ui->longitude->text().trimmed();
QStringList list;
if (ui->city_line->findText(ui->site_name->text())!= -1 && ui->city_line->currentText() != ui->site_name->text()){
QMessageBox::critical(this,tr ("Error"),tr("Invalid site name (duplicate)"));
} else if (!islatitude_d(ui->latitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid latitude value"));
} else if (!islongitude_d(ui->longitude->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid longitude value"));
} else if (!is_not_empty_d(ui->site_name->text().trimmed())){
QMessageBox::critical(this,tr("Error"),tr("Invalid site name"));
}
else{
if (islatitude_d(ui->latitude->text().trimmed()) && ui->latitude_choose->currentIndex() == 1){
latitude = "-"+latitude;
}
if (islongitude_d(ui->longitude->text().trimmed()) && ui->longitude_choose->currentIndex() == 0){
longitude = "-" + longitude;
}
if (is_exist_line( ui->city_line->currentText(),title)){
QString str_ll = latitude + longitude;
QString str = site_n + str_ll;
QString str_read;
QFile fileout(file_name);
if (fileout.open(QIODevice::ReadOnly)){
str_read = fileout.readAll();
fileout.close();
}
str_read = str_read.trimmed();
list = str_read.split("\n");
for (int i = 0; i < list.length(); i++){
if (i+1 == ui->city_line->currentIndex()){
list[i] = str;
}
}
if (fileout.open(QIODevice::WriteOnly)){
QTextStream out(&fileout);
for (int i = 0; i < list.length(); i++){
out << list[i] << Qt::endl;
}
}
fileout.close();
refresh_file_data();
int city_index = ui->city_line->findText(site_n.section(", ",0,0).trimmed());
ui->city_line->setCurrentIndex(city_index);
refresh_file_data();
} else {
QMessageBox::critical(this,tr("Error"), tr("line is not exist"));
refresh_file_data();
set_to_none_city();
}
}
} else {
QMessageBox::critical(this,tr("Error"), tr("File is not exist"));
refresh_file_data();
set_to_none();
}
}
}
void dat_file::on_city_line_activated(int index)
{
QString city_name = ui->city_line->itemText(index);
if (city_name == "None"){
set_to_none_city();
refresh_file_data();
} else{
refresh_file_data();
}
}
void dat_file::on_dat_file_finished(int result)
{
refresh_file_data();
set_to_none();
}
void dat_file::on_DD_format_latitude_clicked()
{
QString latitude;
if (ui->DD_format_latitude->isChecked() == false){
ui->DMS_format_latitude->setChecked(true);
latitude = ui->latitude ->text();
if (isDD_format_d(latitude) == true && latitude != ""){
ui->latitude->setText(DD_to_DMS_d(latitude));
} else if (isDMS_format_d(latitude) == true && latitude != "") {
ui->latitude->setText(latitude);
} else {
ui->latitude->clear();
}
} else {
ui->DMS_format_latitude->setChecked(false);
ui->DD_format_latitude->setChecked(true);
latitude = ui->latitude ->text();
if (isDD_format_d(latitude) == true && latitude != ""){
ui->latitude->setText(latitude);
} else if (isDMS_format_d(latitude) == true && latitude != ""){
ui->latitude->setText(DMS_to_DD_d(latitude));
} else {
ui->latitude->clear();
}
}
}
void dat_file::on_DMS_format_latitude_clicked()
{
QString latitude;
if (ui->DMS_format_latitude->isChecked() == false){
ui->DD_format_latitude->setChecked(true);
latitude = ui->latitude ->text();
if (isDD_format_d(latitude) == true && latitude != ""){
ui->latitude->setText(latitude);
} else if (isDMS_format_d(latitude) == true && latitude != ""){
ui->latitude->setText(DMS_to_DD_d(latitude));
} else {
ui->latitude->clear();
}
} else {
ui->DD_format_latitude->setChecked(false);
ui->DMS_format_latitude->setChecked(true);
latitude = ui->latitude ->text();
if (isDD_format_d(latitude) == true && latitude != ""){
ui->latitude->setText(DD_to_DMS_d(latitude));
} else if (isDMS_format_d(latitude) == true && latitude != "") {
ui->latitude->setText(latitude);
} else {
ui->latitude->clear();
}
}
}
void dat_file::on_DD_format_longitude_clicked()
{
QString longitude;
if (ui->DD_format_longitude->isChecked() == false){
ui->DMS_format_longitude->setChecked(true);
longitude = ui->longitude ->text();
if (isDD_format_d(longitude) == true && longitude != ""){
ui->longitude->setText(DD_to_DMS_d(longitude));
} else if (isDMS_format_d(longitude) == true && longitude != "") {
ui->longitude->setText(longitude);
} else {
ui->longitude->clear();
}
} else {
ui->DMS_format_longitude->setChecked(false);
ui->DD_format_longitude->setChecked(true);
longitude = ui->longitude ->text();
if (isDD_format_d(longitude) == true && longitude != ""){
ui->longitude->setText(longitude);
} else if (isDMS_format_d(longitude) == true && longitude != ""){
ui->longitude->setText(DMS_to_DD_d(longitude));
} else {
ui->longitude->clear();
}
}
}
void dat_file::on_DMS_format_longitude_clicked()
{
QString longitude;
if (ui->DMS_format_longitude->isChecked() == false){
ui->DD_format_longitude->setChecked(true);
longitude = ui->longitude ->text();
if (isDD_format_d(longitude) == true && longitude != ""){
ui->longitude->setText(longitude);
} else if (isDMS_format_d(longitude) == true && longitude != ""){
ui->longitude->setText(DMS_to_DD_d(longitude));
} else {
ui->longitude->clear();
}
} else {
ui->DD_format_longitude->setChecked(false);
ui->DMS_format_longitude->setChecked(true);
longitude = ui->longitude ->text();
if (isDD_format_d(longitude) == true && longitude != ""){
ui->longitude->setText(DD_to_DMS_d(longitude));
} else if (isDMS_format_d(longitude) == true && longitude != "") {
ui->longitude->setText(longitude);
} else {
ui->longitude->clear();
}
}
}
void dat_file::on_Delete_line_clicked()
{
if (ui->city_line->currentText() == "None"){
QMessageBox::warning(this,tr("Warning"),tr("Select line to delete"));
} else {
if (is_exist_line(ui->city_line->currentText(), ui->file_name->text())){
QFile fileout(this->windowTitle());
QString str_read;
QStringList list;
if (fileout.open(QIODevice::ReadOnly)){
str_read = fileout.readAll();
fileout.close();
}
list = str_read.split("\n");
for (int i = 0; i < list.length(); i++){
if (i + 1 == ui->city_line->currentIndex()){
list.remove(i);
ui->city_line->removeItem(i+1);
set_to_none_city();
}
}
if (fileout.open(QIODevice::WriteOnly)){
QTextStream out(&fileout);
for (int i = 0; i < list.length(); i++){
out << list[i] << Qt::endl;
}
}
fileout.close();
refresh_file_data();
} else {
QMessageBox::critical(this,tr("Error"), tr("line is not exist"));
refresh_file_data();
set_to_none_city();
}
}
}
void dat_file::on_delete_file_clicked()
{
QString title = this->windowTitle();
QFile file_check(title);
if (title == tr("Untitled" )){
QString file_name = QFileDialog::getOpenFileName(this,
tr("Open file"),"",
tr("*.cdat"));
this->setWindowTitle(file_name);
QProcess *process = new QProcess;
process->start("rm",QStringList()<< file_name);
refresh_file_data();
set_to_none();
this->setWindowTitle(tr("Untitled"));
process->close();
delete process;
} else {
if (file_check.exists()){
QProcess *process = new QProcess;
process->start("rm",QStringList()<< title);
refresh_file_data();
set_to_none();
process->close();
delete process;
} else {
QMessageBox::critical(this,tr("Error"), tr("File is not exist"));
refresh_file_data();
set_to_none();
}
}
}
void dat_file::on_save_as_file_clicked()
{
QString file_name = QFileDialog::getSaveFileName(this,
tr("Save dat file"),".dat");
QString str_read;
QStringList list;
QString file_name_source = this->windowTitle();
QFile fileout(file_name);
QFile fileout_source(file_name_source);
if (fileout_source.open(QIODevice::ReadOnly)){
str_read = fileout_source.readAll();
fileout_source.close();
}
str_read = str_read.trimmed();
list = str_read.split("\n");
if (fileout.open(QIODevice::WriteOnly)){
QTextStream out(&fileout);
for (int i = 0; i < list.length(); i++){
out << list[i] << Qt::endl;
}
}
fileout.close();
refresh_file_data();
set_to_none_city();
this->setWindowTitle(file_name);
}
bool dat_file::is_exist_line(QString string_name, QString file_name)
{
QFile fileIn(file_name);
QString str;
QStringList list;
int counter = 0;
if (fileIn.open(QIODevice::ReadOnly)){
str = fileIn.readAll();
list = str.split("\n");
for (int i = 0; i<list.length(); i++){
if (list[i].contains(string_name)){
counter ++;
}
}
if (counter == 0 ) {
return false;
} else {
return true;
}
fileIn.close();
}
}
bool dat_file::is_city_format(QString file_name)
{
int count = 0;
QString str;
QString latitude,longitude, site_name;
QStringList list;
QFile fileIn(file_name);
if (fileIn.open(QIODevice::ReadOnly)){
str = fileIn.readAll().trimmed();
list = str.split("\n");
for (int i = 0; i < list.length();i++){
site_name = list[i].section(", ",0,0);
if (list[i].section(", ", 1, 1).contains("-")) {
latitude = list[i].section(", ", 1, 1).section("-",1,1);
} else {
latitude = list[i].section(", ",1,1);
}
if (list[i].section(", ", 2, 2).contains("-")) {
longitude = list[i].section(", ", 2, 2).section("-",1,1);
} else {
longitude = list[i].section(", ", 2, 2);
}
if (!islatitude_d(latitude)|| !is_not_empty_d(latitude) || !islongitude_d(longitude) || !is_not_empty_d(latitude) || !is_not_empty_d(site_name)){
count ++;
}
}
if (count == 0){
return true;
} else {
return false;
}
fileIn.close();
} else {
return false;
}
}
void dat_file::retranslate(){
ui->retranslateUi(this);
}
void dat_file::on_file_name_ch_clicked()
{
QString file_name = QFileDialog::getOpenFileName(this,
tr("Open file"));
ui->file_name->setText(file_name);
ui->file_name_2->setText(file_name);
this->setWindowTitle(file_name);
if (!is_city_format(file_name)){
ui->tabWidget->setCurrentIndex(1);
ui->tab->setEnabled(false);
QMessageBox::warning(this,tr("Warning"), tr("Invalid file content format"));
} else {
ui->tab->setEnabled(true);
}
refresh_file_data();
}
void dat_file::on_file_name_ch_2_clicked()
{
QString file_name = QFileDialog::getOpenFileName(this,
tr("Open file"));
ui->file_name->setText(file_name);
ui->file_name_2->setText(file_name);
this->setWindowTitle(file_name);