-
Notifications
You must be signed in to change notification settings - Fork 0
/
configwindow.cc
1172 lines (1010 loc) · 36.1 KB
/
configwindow.cc
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
/* -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*- */
/* vim: set shiftwidth=4 softtabstop=4 expandtab: */
/*
********************************************************************
** NIDAS: NCAR In-situ Data Acquistion Software
**
** 2009, Copyright University Corporation for Atmospheric Research
**
** 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.
**
** The LICENSE.txt file accompanying this software contains
** a copy of the GNU General Public License. If it is not found,
** write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
**
********************************************************************
*/
/*
* This file is part of configedit:
* A Qt based application that allows visualization of a nidas/nimbus
* configuration (e.g. default.xml) file.
*/
#include <QtGui>
#include <ctime>
#include <list>
#include <iostream>
#include <fstream>
#include "sys/stat.h"
#include <QFileDialog>
#include <QInputDialog>
#include <QMenuBar>
#include <QMenu>
#include <QStatusBar>
#include <QHeaderView>
#include "configwindow.h"
#include "exceptions/exceptions.h"
#include "exceptions/QtExceptionHandler.h"
#include "exceptions/CuteLoggingExceptionHandler.h"
#include "exceptions/CuteLoggingStreamHandler.h"
using namespace nidas::core;
using namespace nidas::util;
ConfigWindow::ConfigWindow() :
// Directory paths are relative to $PROJ_DIR
_doc(NULL), _noProjDir(false),
_gvDefault("/Configuration/GV_N677F/default.xml"),
_c130Default("/Configuration/C130_N130AR/default.xml"),
_a2dCalDir("/Configuration/cal_files/A2D/"),
_engCalDirRoot("/Configuration/cal_files/Engineering/"),
_pmsSpecsFile("/Configuration/PMSspecs"),
_filename(""), _fileOpen(false)
{
try {
//if (!(exceptionHandler = new QtExceptionHandler()))
//if (!(exceptionHandler = new CuteLoggingExceptionHandler(this)))
//To allow cerr debugging comments to show in gdb comment out the next two
//lines
if (!(exceptionHandler = new CuteLoggingStreamHandler(std::cerr,0)))
throw 0;
XMLPlatformUtils::Initialize(); //xercesc class
_errorMessage = new QMessageBox(this);
setupDefaultDir();
buildMenus();
sensorComboDialog = new AddSensorComboDialog(_projDir+_a2dCalDir,
_projDir+_pmsSpecsFile, this);
dsmComboDialog = new AddDSMComboDialog(this);
a2dVariableComboDialog = new AddA2DVariableComboDialog(this);
variableComboDialog = new VariableComboDialog(this);
newProjDialog = new NewProjectDialog(_projDir, this);
} catch (InternalProcessingException &e) {
_errorMessage->setText(QString::fromStdString
("Internal Error. Get Help! " + std::string(e.what())));
_errorMessage->exec();
} catch (std::exception& e) {
_errorMessage->setText(QString::fromStdString
("Caught Standard Error: " + std::string(e.what())));
_errorMessage->exec();
} catch (...) {
_errorMessage->setText(QString
("Caught Unknown Initialization Exception"));
_errorMessage->exec();
}
}
/**
* Build the dropdown menus at the top of the main configedit window
*/
void ConfigWindow::buildMenus()
{
buildFileMenu();
buildProjectMenu();
buildWindowMenu();
buildDSMMenu();
buildSensorMenu();
buildA2DVariableMenu();
buildVariableMenu();
}
/**
* Build the "File" dropdown menu
*/
void ConfigWindow::buildFileMenu()
{
QAction * ProjAct = new QAction(tr("New &Proj Config"), this);
ProjAct->setShortcut(tr("Ctrl+P"));
ProjAct->setStatusTip(tr("Create a new ADS configuration file"));
connect(ProjAct, SIGNAL(triggered()), this, SLOT(newProj()));
QAction * openAct = new QAction(tr("&Open"), this);
openAct->setShortcut(tr("Ctrl+O"));
openAct->setStatusTip(tr("Open an existing configuration file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(newFile()));
QAction * saveAct = new QAction(tr("&Save"), this);
saveAct->setShortcut(tr("Ctrl+S"));
saveAct->setStatusTip(tr("Save a configuration file"));
connect(saveAct, SIGNAL(triggered()), this, SLOT(saveOldFile()));
QAction * saveAsAct = new QAction(tr("Save &As..."), this);
saveAsAct->setShortcut(tr("Ctrl+A"));
saveAsAct->setStatusTip(tr("Save configuration as a new file"));
connect(saveAsAct, SIGNAL(triggered()), this, SLOT(saveAsFile()));
QAction * exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(quit()));
QMenu * fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(ProjAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveAsAct);
fileMenu->addAction(exitAct);
}
void ConfigWindow::buildProjectMenu()
{
QMenu * menu = menuBar()->addMenu(tr("&Project"));
QAction * projEditAct = new QAction(tr("&Edit Name"), this);
projEditAct->setShortcut(tr("Ctrl+E"));
projEditAct->setStatusTip(tr("Edit the Project Name"));
connect(projEditAct, SIGNAL(triggered()), this, SLOT(editProjName()));
menu->addAction(projEditAct);
}
void ConfigWindow::quit()
{
cerr<<"ConfigWindow::quit() called \n";
if (_fileOpen)
if (!askSaveFileAndContinue()) return;
QCoreApplication::quit();
}
void ConfigWindow::buildWindowMenu()
{
QMenu * menu = menuBar()->addMenu(tr("&Windows"));
QAction * act;
act = new QAction(tr("&Errors"), this);
act->setStatusTip(tr("Toggle errors window"));
act->setCheckable(true);
act->setChecked(false);
connect(act, SIGNAL(toggled(bool)), this, SLOT(toggleErrorsWindow(bool)));
menu->addAction(act);
}
void ConfigWindow::buildSensorMenu()
{
buildSensorActions();
sensorMenu = menuBar()->addMenu(tr("&Sensor"));
sensorMenu->addAction(addSensorAction);
sensorMenu->addAction(editSensorAction);
sensorMenu->addAction(deleteSensorAction);
sensorMenu->setEnabled(false);
}
void ConfigWindow::buildDSMMenu()
{
buildDSMActions();
dsmMenu = menuBar()->addMenu(tr("&DSM"));
dsmMenu->addAction(addDSMAction);
dsmMenu->addAction(editDSMAction);
dsmMenu->addAction(deleteDSMAction);
dsmMenu->setEnabled(false);
}
void ConfigWindow::buildA2DVariableMenu()
{
buildA2DVariableActions();
a2dVariableMenu = menuBar()->addMenu(tr("&A2DVariable"));
a2dVariableMenu->addAction(addA2DVariableAction);
a2dVariableMenu->addAction(editA2DVariableAction);
a2dVariableMenu->addAction(deleteA2DVariableAction);
a2dVariableMenu->setEnabled(false);
}
void ConfigWindow::buildVariableMenu()
{
buildVariableActions();
variableMenu = menuBar()->addMenu(tr("&Variable"));
variableMenu->addAction(editVariableAction);
variableMenu->setEnabled(false);
}
void ConfigWindow::buildSensorActions()
{
addSensorAction = new QAction(tr("&Add Sensor"), this);
connect(addSensorAction, SIGNAL(triggered()), this,
SLOT(addSensorCombo()));
editSensorAction = new QAction(tr("&Edit Sensor"), this);
connect(editSensorAction, SIGNAL(triggered()), this,
SLOT(editSensorCombo()));
deleteSensorAction = new QAction(tr("&Delete Sensor"), this);
connect(deleteSensorAction, SIGNAL(triggered()), this,
SLOT(deleteSensor()));
}
void ConfigWindow::buildDSMActions()
{
addDSMAction = new QAction(tr("&Add DSM"), this);
connect(addDSMAction, SIGNAL(triggered()), this, SLOT(addDSMCombo()));
editDSMAction = new QAction(tr("&Edit DSM"), this);
connect(editDSMAction, SIGNAL(triggered()), this, SLOT(editDSMCombo()));
deleteDSMAction = new QAction(tr("&Delete DSM"), this);
connect(deleteDSMAction, SIGNAL(triggered()), this, SLOT(deleteDSM()));
}
void ConfigWindow::buildA2DVariableActions()
{
editA2DVariableAction = new QAction(tr("&Edit A2DVariable"), this);
connect(editA2DVariableAction, SIGNAL(triggered()), this,
SLOT(editA2DVariableCombo()));
addA2DVariableAction = new QAction(tr("&Add A2DVariable"), this);
connect(addA2DVariableAction, SIGNAL(triggered()), this,
SLOT(addA2DVariableCombo()));
deleteA2DVariableAction = new QAction(tr("&Delete A2DVariable"), this);
connect(deleteA2DVariableAction, SIGNAL(triggered()), this,
SLOT(deleteA2DVariable()));
}
void ConfigWindow::buildVariableActions()
{
editVariableAction = new QAction(tr("&Edit Variable"), this);
connect(editVariableAction, SIGNAL(triggered()), this,
SLOT(editVariableCombo()));
}
void ConfigWindow::toggleErrorsWindow(bool checked)
{
exceptionHandler->setVisible(checked);
}
void ConfigWindow::addSensorCombo()
{
QModelIndexList indexList; // create an empty list
sensorComboDialog->setModal(true);
sensorComboDialog->show(model, indexList);
cerr<<"after call to addSensorCombo->show\n";
tableview->resizeColumnsToContents();
}
void ConfigWindow::editSensorCombo()
{
// Get selected index list and make sure it's only one
// (see note in editA2DVariableCombo)
QModelIndexList indexList = tableview->selectionModel()->selectedIndexes();
if (indexList.size() > 6) {
cerr << "ConfigWindow::editSensorCombo - found more than " <<
"one row to edit\n";
cerr << "indexList.size() = " << indexList.size() << "\n";
return;
}
// allow user to edit variable
sensorComboDialog->setModal(true);
sensorComboDialog->show(model, indexList);
tableview->resizeColumnsToContents();
}
void ConfigWindow::deleteSensor()
{
model->removeIndexes(tableview->selectionModel()->selectedIndexes());
cerr << "ConfigWindow::deleteSensor after removeIndexes\n";
_doc->setIsChangedBig(true);
tableview->resizeColumnsToContents();
}
void ConfigWindow::addDSMCombo()
{
QModelIndexList indexList; // create an empty list
dsmComboDialog->setModal(true);
dsmComboDialog->show(model, indexList);
tableview->resizeColumnsToContents();
}
void ConfigWindow::editDSMCombo()
{
// Get selected index list and make sure it's only one
// (see note in editA2DVariableCombo)
QModelIndexList indexList = tableview->selectionModel()->selectedIndexes();
if (indexList.size() > 2) {
cerr << "ConfigWindow::editSensorCombo - found more than " <<
"one row to edit\n";
cerr << "indexList.size() = " << indexList.size() << "\n";
return;
}
// allow user to edit DSM
dsmComboDialog->setModal(true);
dsmComboDialog->show(model, indexList);
tableview->resizeColumnsToContents();
}
void ConfigWindow::deleteDSM()
{
model->removeIndexes(tableview->selectionModel()->selectedIndexes());
cerr << "ConfigWindow::deleteDSM after removeIndexes\n";
_doc->setIsChangedBig(true);
tableview->resizeColumnsToContents();
}
void ConfigWindow::addA2DVariableCombo()
{
QModelIndexList indexList; // create an empty list
a2dVariableComboDialog->setModal(true);
a2dVariableComboDialog->show(model,indexList);
tableview->resizeColumnsToContents();
}
void ConfigWindow::editA2DVariableCombo()
{
// Get selected indexes and make sure it's only one
// NOTE: properties should force this, but if it comes up may need to
// provide a GUI indication.
QModelIndexList indexList = tableview->selectionModel()->selectedIndexes();
if (indexList.size() > 8) {
cerr << "ConfigWindow::editA2DVariableCombo - found more than " <<
"one row to edit \n";
cerr << "indexList.size() = " << indexList.size() << "\n";
return;
}
// allow user to edit/add variable
a2dVariableComboDialog->setModal(true);
a2dVariableComboDialog->show(model, indexList);
tableview->resizeColumnsToContents();
}
void ConfigWindow::deleteA2DVariable()
{
model->removeIndexes(tableview->selectionModel()->selectedIndexes());
_doc->setIsChangedBig(true);
cerr << "ConfigWindow::deleteA2DVariable after removeIndexes\n";
tableview->resizeColumnsToContents();
}
void ConfigWindow::editVariableCombo()
{
// Get selected indexes and make sure it's only one
// NOTE: properties should force this, but if it comes up may need to
// provide a GUI indication.
QModelIndexList indexList = tableview->selectionModel()->selectedIndexes();
if (indexList.size() > 6) {
cerr << "ConfigWindow::editVariableCombo - found more than " <<
"one row to edit \n";
cerr << "indexList.size() = " << indexList.size() << "\n";
return;
}
// allow user to edit/add variable
variableComboDialog->setModal(true);
variableComboDialog->show(model, indexList);
tableview->resizeColumnsToContents();
}
/**
* Setup _defaultDir and _defaultCaption class variables for use in
* opening/viewing files.
* @param[in] $PROJ_DIR an environment variable
* @param[in] $PROJECT an environment variable
* @param[in] $AIRCRAFT an environment variable
* @param[out] _defaultDir the default directory to look for default.xml
* @param[out] _defaultCaption the default caption to show at the top of the
* file selection window
* @param[out] _projDir directory where project info is stored, from PROJ_DIR
*/
void ConfigWindow::setupDefaultDir()
{
char * _tmpStr;
_tmpStr = getenv("PROJ_DIR");
if (_tmpStr) {
_defaultDir.append(_tmpStr);
_projDir.append(_tmpStr);
} else { // No $PROJ_DIR - warn user, set default to current and bail.
_defaultCaption.append("No $PROJ_DIR!! ");
QString firstPart("No $PROJ_DIR Environment Variable Defined.\n");
QString secondPart("Configuration Editor will be missing some "
"functionality.\n");
QString thirdPart("Proceeding using current working directory.\n");
_errorMessage->setText(firstPart+secondPart+thirdPart);
_errorMessage->exec();
_tmpStr = getenv("PWD");
_defaultDir.append(_tmpStr);
_noProjDir = true;
return;
}
if (_tmpStr) {
_tmpStr = NULL;
_tmpStr = getenv("PROJECT");
if (_tmpStr)
{
_defaultDir.append("/");
_defaultDir.append(_tmpStr);
}
else
_defaultCaption.append("No $PROJECT.");
}
if (_tmpStr) {
_tmpStr = NULL;
_tmpStr = getenv("AIRCRAFT");
if (_tmpStr)
{
_defaultDir.append("/");
_defaultDir.append(_tmpStr);
_defaultDir.append("/nidas");
}
else
_defaultCaption.append(" No $AIRCRAFT.");
}
return;
}
bool ConfigWindow::fileExists(QString filename)
{
struct stat buffer;
if (stat(filename.toStdString().c_str(), &buffer) == 0) return true;
return false;
}
void ConfigWindow::newProj()
{
QString oldFileName = _filename;
std::string projDir;
char * tmpStr;
tmpStr = getenv("PROJ_DIR");
if (tmpStr) {
projDir.append(tmpStr);
} else {
QString firstPart("No $PROJ_DIR Environment Variable Defined.\n");
QString secondPart("Cannot create a new project.\n");
_errorMessage->setText(firstPart+secondPart);
_errorMessage->exec();
return;
}
newProjDialog->show(); // create new project
return;
}
void ConfigWindow::newFile()
{
if (_fileOpen)
if (!askSaveFileAndContinue()) return;
getFile();
openFile();
}
void ConfigWindow::getFile()
{
QString caption;
caption = _defaultCaption;
caption.append(" Choose a file...");
_filename = QFileDialog::getOpenFileName(
0,
caption,
QString::fromStdString(_defaultDir),
"Config Files (*.xml)");
return;
}
void ConfigWindow::openFile()
{
QString winTitle("configedit: ");
if (_filename.isNull() || _filename.isEmpty()) {
cerr << "filename null/empty ; not opening" << endl;
_errorMessage->setText("No File Selected...");
_errorMessage->exec();
winTitle.append("(no file selected)");
setWindowTitle(winTitle);
return;
}
else if (!a2dVariableComboDialog->setup(_filename.toStdString())) {
cerr << "Problems with a2dVariableDialog setup\n";
winTitle.append("(could not set up a2dVariable Dialog)");
setWindowTitle(winTitle);
return;
}
else {
if (_doc) delete(_doc);
_doc = new Document(_projDir+_engCalDirRoot, this);
_doc->setFilename(_filename.toStdString());
try {
_doc->parseFile();
}
catch (nidas::util::InvalidParameterException &e) {
cerr<<"caught Exception InvalidParam: " << e.toString() << "\n";
_errorMessage->setText(QString::fromStdString
("Caught nidas InvalidParameterException: "
+ e.toString()));
_errorMessage->exec();
return;
}
// Aircraft XML files should have only one site
vector <std::string> siteNames;
siteNames = _doc->getSiteNames();
if (siteNames.size() > 1) {
if (siteNames[0]=="GV_N677F" ||
siteNames[0]=="C130_N130AR") {
cerr<<"XML is for aircraft but has multiple sites\n";
_errorMessage->setText(_filename + QString(
":: ERROR: XML is for aircraft but has multiple sites"));
_errorMessage->exec();
return;
}
}
// Without Engineering Calibrations directory we'd be guessing
// at calfile names
if (!_doc->engCalDirExists()) {
QString engCalDir = _doc->getEngCalDir();
_errorMessage->setText("Could not open Engineering Cal dir:" +
engCalDir +
"\n ERROR: Can't check on Cal files. " +
"\n Would be guessing names - fix problem.");
_errorMessage->exec();
return;
}
try {
cerr<<"printSiteNames\n";
_doc->printSiteNames();
QWidget *oldCentral = centralWidget();
if (oldCentral) {
cerr << "got an old central widget\n";
cerr << "NAME: " << oldCentral->objectName().toStdString() << "\n";
cerr << "INFO::\n";
oldCentral->dumpObjectInfo();
cerr << "\n\nTREE:\n";
oldCentral->dumpObjectTree();
cerr << "\n\n";
/* qt docs say call setCentralWidget() only once,
but we need to dump the old one to make for a new file
*/
setCentralWidget(0);
show();
}
mainSplitter = new QSplitter(this);
mainSplitter->setObjectName(QString("the horizontal splitter!!!"));
buildSensorCatalog();
dsmComboDialog->setDocument(_doc);
//sampleComboDialog->setDocument(_doc);
a2dVariableComboDialog->setDocument(_doc);
variableComboDialog->setDocument(_doc);
setupModelView(mainSplitter);
setCentralWidget(mainSplitter);
show(); // XXX
winTitle.append(_filename);
setWindowTitle(winTitle);
}
catch (const CancelProcessingException & cpe) {
// stop processing, show blank window
_errorMessage->setText(QString::fromStdString
("Caught CancelProcessingException" + cpe.toString()));
_errorMessage->exec();
return;
QStatusBar *sb = statusBar();
if (sb) sb->showMessage(QString::fromLatin1(cpe.what()));
}
catch(...) {
// stop processing, show blank window
_errorMessage->setText(QString::fromStdString
("Caught Unknown Exception"));
_errorMessage->exec();
exceptionHandler->handle("Project configuration file");
return;
}
}
QList<int> sizes = mainSplitter->sizes();
sizes[0] = 300;
sizes[1] = 700;
mainSplitter->setSizes(sizes);
tableview->resizeColumnsToContents ();
_fileOpen = true;
show();
return;
}
void ConfigWindow::show()
{
resize(1400,600);
QMainWindow::show();
}
/**
* @brief Function to allow editing the project name
*
* @warning If called before a config file has been loaded, lets user
* know they need to create/select a config file.
*/
void ConfigWindow::editProjName()
{
cerr<<"In ConfigWindow::editProjName. \n";
string projName;
if (_doc) {
projName = _doc->getProjectName();
cerr<<"In ConfigWindow::editProjName. projName = " << projName << "\n";
bool ok;
QString text = QInputDialog::getText(this, tr("Edit Project Name"),
tr("Project Name:"), QLineEdit::Normal,
QString::fromStdString(projName), &ok);
cerr<< "after call to QInputDialog::getText\n";
if (ok && !text.isEmpty()) {
writeProjectName(text);
}
} else {
_errorMessage->setText(QString::fromStdString
("Error: Create/select a configuration file first."));
_errorMessage->exec();
}
return;
}
void ConfigWindow::writeProjectName(QString projName)
{
_doc->setProjectName(projName.toStdString());
// Now put the project name into a file in the Project Directory
// (needed by nimbus)
std::string dir = _doc->getDirectory();
std::string projDir(dir);
size_t found;
found = projDir.rfind('/');
if (found!=string::npos)
projDir.erase(found);
else {
string error;
error = "Could not find Project Directory (parent of " +
dir + " )" + "\nUnable to write ProjectName file." +
"This will cause problems with nimbus.";
_errorMessage->setText(QString::fromStdString(error));
_errorMessage->exec();
return;
}
std::string projFName;
projFName = projDir + "/ProjectName";
ofstream projFile(projFName.c_str());
if (projFile) {
projFile.close();
if (remove(projFName.c_str()) != 0) {
string error;
error = "Could not remove ProjectName file:" + projFName +
+ "\nUnable to write ProjectName file." +
"This will cause problems with nimbus.";
_errorMessage->setText(QString::fromStdString(error));
_errorMessage->exec();
return;
}
}
projFile.open(projFName.c_str(), ios::out);
if (projFile.is_open()) {
projFile << projName.toStdString().c_str() << "\n";
projFile.close();
}
else {
string error;
error = "Could not open file:" + projFName +
+ "\nUnable to write ProjectName file." +
"This will cause problems with nimbus.";
_errorMessage->setText(QString::fromStdString(error));
_errorMessage->exec();
return;
}
}
/**
* QT oddity wrt argument passing forces this hack
*/
void ConfigWindow::saveOldFile()
{
saveFile("");
}
/**
* \brief Save a configuration file
*
* This interface is a little confusing. The filename that will be saved
* is found in Document. If an origFile is passed in, that argument is used
* to save a copy of the previous file in .confedit directory.
* TODO: There's got to be a less confusing way of doing this.
*/
bool ConfigWindow::saveFile(string origFile)
{
cerr << __func__ << endl;
if (_filename == _projDir+_c130Default || _filename == _projDir+_gvDefault)
{
_errorMessage->setText(
"Attempting to save default aircraft file - please use Save As");
_errorMessage->exec();
return false;
}
if (_doc) { // confirm user has loaded a config file
if (!saveFileCopy(origFile)) {
_errorMessage->setText("FAILED to write copy of file.\n No backups");
_errorMessage->exec();
}
if (!_doc->writeDocument()) {
_errorMessage->setText("FAILED TO WRITE FILE! Check permissions");
_errorMessage->exec();
return false;
}
} else {
_errorMessage->setText(QString::fromStdString
("Error: Create/select a configuration file first."));
_errorMessage->exec();
return false;
}
cerr << " Missing Cal Files:\n";
vector<QString> missingEngCalFiles = _doc->getMissingEngCalFiles();
for (size_t i=0; i<missingEngCalFiles.size(); i++) {
cerr << missingEngCalFiles[i].toStdString();
cerr << "\n";
}
// Now clean up xercesc's bizzare comment formatting
std::string syscmd;
std::string filename = _doc->getFilename();
std::string tmpfilename = filename + ".tmp";
// remove all blank lines
syscmd = "sed '/^ *$/d' " + filename + " > " + tmpfilename;
system(syscmd.c_str());
syscmd.clear();
// move all start xml comments to first column
syscmd = "sed 's/^ *<\\!/<\\!/' " + tmpfilename + " > " + filename;
system(syscmd.c_str());
syscmd.clear();
// insert a blank line ahead of xml comments
syscmd = "sed '/^ *<\\!/{x;p;x;}' " + filename + " > " + tmpfilename;
system(syscmd.c_str());
syscmd.clear();
// final step in cleanup
syscmd = "mv -f " + tmpfilename + " " + filename;
system(syscmd.c_str());
_doc->setIsChanged(false);
_doc->setIsChangedBig(false);
return true;
}
bool ConfigWindow::saveAsFile()
{
QString qfilename;
QString _caption;
std::string curFileName;
if (_doc) { // confirm user has loaded a config file
curFileName=_doc->getFilename();
} else {
_errorMessage->setText(QString::fromStdString
("Error: Create/select a configuration file first."));
_errorMessage->exec();
return false;
}
string filename(_doc->getDirectory());
filename.append("/default.xml");
qfilename = QFileDialog::getSaveFileName(
0,
_caption,
QString::fromStdString(filename),
"Config Files (*.xml)");
cerr << "saveAs dialog returns " << qfilename.toStdString() << endl;
if (qfilename.isNull() || qfilename.isEmpty()) {
cerr << "qfilename null/empty ; not saving" << endl;
_errorMessage->setText("No Filename chosen - not saving");
_errorMessage->exec();
return(false);
}
if (!qfilename.endsWith(".xml"))
qfilename.append(".xml");
_doc->setFilename(qfilename.toStdString().c_str());
_filename=qfilename;
if (saveFile(curFileName)) {
QString winTitle("configedit: ");
winTitle.append(_filename);
setWindowTitle(winTitle);
return true;
} else {
_doc->setFilename(curFileName);
_filename=QString::fromStdString(curFileName);
return false;
}
}
bool ConfigWindow::saveFileCopy(string origFile)
{
std::string saveFileName = _doc->getFilename();
size_t fn = saveFileName.rfind("/");
std::string saveFname = saveFileName.substr(fn+1);
std::string saveDir = saveFileName.substr(0, fn+1);
std::string copyDir = saveDir + ".confedit";
std::string copyFname;
std::string copyFile;
std::string fromFile;
// Make copy directory if it doesn't already exist
umask(0);
struct stat st;
if (stat(copyDir.c_str(), &st) != 0) { // create copy dir
if (mkdir(copyDir.c_str(), S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH) == -1) {
cerr << "Could not create directory " << copyDir << " to save copies\n";
return false;
}
}
// set up the copy filename
time_t now;
char dateTime[64];
dateTime[0] = '\0';
now = time(NULL);
if (now != -1)
strftime(dateTime, 64, "%Y%m%d-%H%M%S", gmtime(&now));
else
cerr << "couldn't get timestamp for copy filename\n";
copyFname = saveFname+"."+std::string(dateTime);
copyFile = copyDir + "/" +copyFname;
if (origFile.length() == 0)
fromFile = saveFileName;
else
fromFile = origFile;
ifstream src(fromFile.c_str(), ifstream::in);
if (!src) {
// see if a .xml was added by configwindow
size_t found;
found = fromFile.rfind(".xml");
if (found!=string::npos) {
string::iterator it;
it = fromFile.begin()+found;
fromFile.erase(it,fromFile.end());
}
else
return false;
src.open(fromFile.c_str(), ifstream::in);
if (!src) {
cerr << "Could not open source file : " << fromFile << "\n";
return false;
}
}
ofstream dest(copyFile.c_str(), ifstream::out);
if (!dest) {
cerr << "Could not open destination file: " << copyFile << "\n";
return false;
}
dest << src.rdbuf();
if (!dest)
{
cerr << "Error while copying from: \n" << fromFile <<
"\n to: \n" << copyFile << "\n";
return false;
}
cerr << "copied from: \n" << fromFile <<
"\n to: \n" << copyFile << "\n";
return true;
}
void ConfigWindow::setupModelView(QSplitter *splitter)
{
model = new NidasModel(Project::getInstance(), _doc->getDomDocument(), this);
treeview = new QTreeView(splitter);
treeview->setModel(model);
treeview->header()->hide();
tableview = new QTableView(splitter);
tableview->setModel( model );
tableview->setSelectionModel( treeview->selectionModel() ); /* common selection model */
tableview->setSelectionBehavior( QAbstractItemView::SelectRows );
tableview->setSelectionMode( QAbstractItemView::SingleSelection );
//connect(treeview, SIGNAL(pressed(const QModelIndex &)), this, SLOT(changeToIndex(const QModelIndex &)));
connect(treeview->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), this, SLOT(changeToIndex(const QItemSelection &)));
treeview->setCurrentIndex(treeview->rootIndex().child(0,0));
splitter->addWidget(treeview);
splitter->addWidget(tableview);
}
/*!
* \brief Display and setup the correct actions for the index in \a selections.
*
* This version is for selectionModel's selectionChanged signal.
* We use only the first element of \a selections,
* expecting that the view(s) allow only one selection at a time.
*/
void ConfigWindow::changeToIndex(const QItemSelection & selections)
{
QModelIndexList il = selections.indexes();
if (il.size()) {
changeToIndex(il.at(0));
tableview->resizeColumnsToContents ();
}
else throw InternalProcessingException("selectionChanged signal provided no"
" selections");
}
/*!
* \brief Display and setup the correct actions for the current \a index.
*
* Set the table view's root index to the parent of \a index
* and tell the model same so it returns correct headerData.
* En/dis-able appropriate actions, e.g. add or delete choices...
*/
void ConfigWindow::changeToIndex(const QModelIndex & index)