OSDN Git Service

Added licence and README.
[kods/KODS_GUI.git] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include "nkdialog.h"
3 #include "optimize.h"
4 #include "lidialog.h"
5 #include "ui_mainwindow.h"
6 #include <QtWidgets>
7 #include <QDebug>
8 #include <QLineEdit>
9 #include <QFont>
10 #include <QMenu>
11
12 MainWindow::MainWindow(QMainWindow *parent)
13     : QMainWindow( parent )
14 {
15
16     hdir="/usr/local/KODS";   //For Linux, MacOSX
17
18 //    hdir="/sdcard/KODS";     //For Android
19
20
21
22     setupUi(this);
23
24     msgView->setFont(QFont("courier",10,QFont::Bold));
25
26     proc = new QProcess(this);
27     QObject::connect( proc, SIGNAL(readyReadStandardOutput()), this, SLOT(slot_appendView()) ); //display stdout
28     QObject::connect( proc, SIGNAL(readyReadStandardError()), this, SLOT(slot_appendView()) ); //display stderr
29
30     table->installEventFilter(this);  //install event filter
31
32     table->setColumnCount( 6 );
33     table->setRowCount( 20 );
34
35     table ->setContextMenuPolicy(Qt::CustomContextMenu);
36     connect(table, SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(slot_ShowContextMenu(QPoint)));    //context menu
37
38     addcontextmenu();
39
40     proc->start(hdir+"/kods");
41     proc->write("RTG ALL\n");
42     ReadFileToTable(hdir+"/CURLENS/LENSTEXT.DAT");
43
44     for(int i=0; i<=nol-1; i++){
45             tableitem=table->item(i,2);
46             tableitem->setFlags(Qt::ItemIsEnabled);    // material is not editable
47     }
48
49     for(int i=0; i<=1; i++){
50             tableitem=table->item(0,i);
51             tableitem->setFlags(Qt::ItemIsEnabled);    // infinity distance is not editable
52     }
53
54     for(int i=0; i<=1; i++){
55             tableitem=table->item(nol-1,i);
56             tableitem->setFlags(Qt::ItemIsEnabled);    // last surface is not editable
57     }
58
59 }
60
61 void MainWindow::addcontextmenu()
62 {
63
64     Glass("CHANCE.BIN");
65     GN1 << GN;
66     GN1.sort();
67     NoGlass1=NoGlass;
68
69     Glass("CORNIN.BIN");
70     GN2 << GN;
71     GN2.sort();
72     NoGlass2=NoGlass;
73
74     Glass("HIKARI.BIN");
75     GN3 << GN;
76     GN3.sort();
77     NoGlass3=NoGlass;
78
79     Glass("HOYA.BIN");
80     GN4 << GN;
81     GN4.sort();
82     NoGlass4=NoGlass;
83
84     Glass("OHARA.BIN");
85     GN5 << GN;
86     GN5.sort();
87     NoGlass5=NoGlass;
88
89     Glass("OHARA-O.BIN");
90     GN6 << GN;
91     GN6.sort();
92     NoGlass6=NoGlass;
93
94     Glass("RADHARD.BIN");
95     GN7 << GN;
96     GN7.sort();
97     NoGlass7=NoGlass;
98
99     Glass("SCH2000.BIN");
100     GN8 << GN;
101     GN8.sort();
102     NoGlass8=NoGlass;
103
104     Glass("SCHOTT.BIN");
105     GN9 << GN;
106     GN9.sort();
107     NoGlass9=NoGlass;
108
109     MaterialMenu=menuEdit->addMenu("Input Material");
110
111     Airaction=new QAction("Air",this);
112     MaterialMenu->addAction(Airaction);
113     connect(Airaction, SIGNAL(triggered()), this, SLOT(InputAir()));
114
115     SubMenu1=MaterialMenu->addMenu("CHANCE");
116     SubMenu2=MaterialMenu->addMenu("CORNIN");
117     SubMenu3=MaterialMenu->addMenu("HIKARI");
118     SubMenu4=MaterialMenu->addMenu("HOYA");
119     SubMenu5=MaterialMenu->addMenu("OHARA");
120     SubMenu6=MaterialMenu->addMenu("OHARA-O");
121     SubMenu7=MaterialMenu->addMenu("RADHARD");
122     SubMenu8=MaterialMenu->addMenu("SCH2000");
123     SubMenu9=MaterialMenu->addMenu("SCHOTT");
124
125     menuEdit->insertMenu(actionInput_Lens_Idenfier, MaterialMenu);
126
127     for (int i=0;i<=NoGlass1;i++){
128
129         Makeraction=new QAction(QString(GN1.at(i)),this);
130         Makeraction->setData(QString(GN1.at(i)));
131         this->SubMenu1->addAction(Makeraction);
132
133     }
134
135     connect(SubMenu1, SIGNAL(triggered(QAction*)),this,SLOT(ShowContextMenu(QAction*)));
136
137     SubMenu1->setStyleSheet("QMenu {menu-scrollable: 1;}");
138
139
140     for (int i=0;i<=NoGlass2;i++){
141
142         Makeraction=new QAction(QString(GN2.at(i)),this);
143         Makeraction->setData(QString(GN2.at(i)));
144         this->SubMenu2->addAction(Makeraction);
145
146     }
147
148     connect(SubMenu2, SIGNAL(triggered(QAction*)),this,SLOT(ShowContextMenu2(QAction*)));
149
150     SubMenu2->setStyleSheet("QMenu {menu-scrollable: 1;}");
151
152
153     for (int i=0;i<=NoGlass3;i++){
154
155         Makeraction=new QAction(QString(GN3.at(i)),this);
156         Makeraction->setData(QString(GN3.at(i)));
157         this->SubMenu3->addAction(Makeraction);
158
159     }
160
161     connect(SubMenu3, SIGNAL(triggered(QAction*)),this,SLOT(ShowContextMenu3(QAction*)));
162
163     SubMenu3->setStyleSheet("QMenu {menu-scrollable: 1;}");
164
165
166     for (int i=0;i<=NoGlass4;i++){
167
168         Makeraction=new QAction(QString(GN4.at(i)),this);
169         Makeraction->setData(QString(GN4.at(i)));
170         this->SubMenu4->addAction(Makeraction);
171
172     }
173
174     connect(SubMenu4, SIGNAL(triggered(QAction*)),this,SLOT(ShowContextMenu4(QAction*)));
175
176     SubMenu4->setStyleSheet("QMenu {menu-scrollable: 1;}");
177
178
179     for (int i=0;i<=NoGlass5;i++){
180
181         Makeraction=new QAction(QString(GN5.at(i)),this);
182         Makeraction->setData(QString(GN5.at(i)));
183         this->SubMenu5->addAction(Makeraction);
184
185     }
186
187     connect(SubMenu5, SIGNAL(triggered(QAction*)),this,SLOT(ShowContextMenu5(QAction*)));
188
189     SubMenu5->setStyleSheet("QMenu {menu-scrollable: 1;}");
190
191
192     for (int i=0;i<=NoGlass6;i++){
193
194         Makeraction=new QAction(QString(GN6.at(i)),this);
195         Makeraction->setData(QString(GN6.at(i)));
196         this->SubMenu6->addAction(Makeraction);
197
198     }
199
200     connect(SubMenu6, SIGNAL(triggered(QAction*)),this,SLOT(ShowContextMenu6(QAction*)));
201
202     SubMenu6->setStyleSheet("QMenu {menu-scrollable: 1;}");
203
204
205     for (int i=0;i<=NoGlass7;i++){
206
207         Makeraction=new QAction(QString(GN7.at(i)),this);
208         Makeraction->setData(QString(GN7.at(i)));
209         this->SubMenu7->addAction(Makeraction);
210
211     }
212
213     connect(SubMenu7, SIGNAL(triggered(QAction*)),this,SLOT(ShowContextMenu7(QAction*)));
214
215     SubMenu7->setStyleSheet("QMenu {menu-scrollable: 1;}");
216
217
218     for (int i=0;i<=NoGlass8;i++){
219
220         Makeraction=new QAction(QString(GN8.at(i)),this);
221         Makeraction->setData(QString(GN8.at(i)));
222         this->SubMenu8->addAction(Makeraction);
223
224     }
225
226     connect(SubMenu8, SIGNAL(triggered(QAction*)),this,SLOT(ShowContextMenu8(QAction*)));
227
228     SubMenu8->setStyleSheet("QMenu {menu-scrollable: 1;}");
229
230
231
232     for (int i=0;i<=NoGlass9;i++){
233
234         Makeraction=new QAction(QString(GN9.at(i)),this);
235         Makeraction->setData(QString(GN9.at(i)));
236         this->SubMenu9->addAction(Makeraction);
237
238     }
239
240     connect(SubMenu9, SIGNAL(triggered(QAction*)),this,SLOT(ShowContextMenu9(QAction*)));
241
242     SubMenu9->setStyleSheet("QMenu {menu-scrollable: 1;}");
243
244 }
245
246
247 void MainWindow::slot_commandExec()
248 {
249     QString cmd;
250
251     cmd = cmdLine->text();
252
253     QByteArray input(cmd.toLatin1()+"\n");
254     msgView->append(cmd);
255
256     proc->write(input);
257
258     if (input == "exit")
259     {
260         proc->write("EXIT\n");
261     }
262
263     cmdLine->clear();
264 }
265
266
267 void MainWindow::slot_appendView()
268 {
269     QTextCodec *codec = QTextCodec::codecForName( "UTF8" );
270     QString str( codec->toUnicode(proc->readAllStandardOutput()) );
271     QString str2( codec->toUnicode(proc->readAllStandardError()) );
272     msgView->append(str);
273     msgView->append(str2);
274 }
275
276
277 void MainWindow::ShowContextMenu(QAction *Action)
278
279 {
280     QString str = Action->text();
281     row = table->currentRow();          //get number of row
282     QString Qrow = QString::number(row);
283     qDebug() << Action->parent()->objectName();
284
285     LF=18;
286     DataRead("CHANCE.BIN",str,row);
287     table->setItem(row, 2, new QTableWidgetItem("CHANCE "+str));  //Change table value to MODEL
288     table->resizeColumnToContents(2);
289     tableitem=table->item(row,2);
290     tableitem->setFlags(Qt::ItemIsEnabled);
291     proc->write("U L\n");
292     proc->write("CHG "+Qrow.toLatin1()+"\n");
293     proc->write("CHANCE "+str.toLatin1()+"\n");
294     proc->write("EOS\n");
295     proc->write("RTG ALL\n");
296 }
297
298 void MainWindow::ShowContextMenu2(QAction *Action)
299
300 {
301     QString str = Action->text();
302     row = table->currentRow();          //get number of row
303     QString Qrow = QString::number(row);
304     qDebug() << Action->parent()->objectName();
305
306     LF=18;
307     DataRead("CORNIN.BIN",str,row);
308     table->setItem(row, 2, new QTableWidgetItem("CORNIN "+str));  //Change table value to MODEL
309     table->resizeColumnToContents(2);
310     tableitem=table->item(row,2);
311     tableitem->setFlags(Qt::ItemIsEnabled);
312     proc->write("U L\n");
313     proc->write("CHG "+Qrow.toLatin1()+"\n");
314     proc->write("CORNIN "+str.toLatin1()+"\n");
315     proc->write("EOS\n");
316     proc->write("RTG ALL\n");
317 }
318
319 void MainWindow::ShowContextMenu3(QAction *Action)
320
321 {
322     QString str = Action->text();
323     row = table->currentRow();          //get number of row
324     QString Qrow = QString::number(row);
325     qDebug() << Action->parent()->objectName();
326
327     LF=18;
328     DataRead("HIKARI.BIN",str,row);
329     table->setItem(row, 2, new QTableWidgetItem("HIKARI "+str));  //Change table value to MODEL
330     table->resizeColumnToContents(2);
331     tableitem=table->item(row,2);
332     tableitem->setFlags(Qt::ItemIsEnabled);
333     proc->write("U L\n");
334     proc->write("CHG "+Qrow.toLatin1()+"\n");
335     proc->write("HIKARI "+str.toLatin1()+"\n");
336     proc->write("EOS\n");
337     proc->write("RTG ALL\n");
338 }
339
340 void MainWindow::ShowContextMenu4(QAction *Action)
341
342 {
343     QString str = Action->text();
344     row = table->currentRow();          //get number of row
345     QString Qrow = QString::number(row);
346     qDebug() << Action->parent()->objectName();
347
348     LF=18;
349     DataRead("HOYA.BIN",str,row);
350     table->setItem(row, 2, new QTableWidgetItem("HOYA "+str));  //Change table value to MODEL
351     table->resizeColumnToContents(2);
352     tableitem=table->item(row,2);
353     tableitem->setFlags(Qt::ItemIsEnabled);
354     proc->write("U L\n");
355     proc->write("CHG "+Qrow.toLatin1()+"\n");
356     proc->write("HOYA "+str.toLatin1()+"\n");
357     proc->write("EOS\n");
358     proc->write("RTG ALL\n");
359 }
360
361 void MainWindow::ShowContextMenu5(QAction *Action)
362
363 {
364     QString str = Action->text();
365     row = table->currentRow();          //get number of row
366     QString Qrow = QString::number(row);
367     qDebug() << Action->parent()->objectName();
368
369     LF=18;
370     DataRead("OHARA.BIN",str,row);
371     table->setItem(row, 2, new QTableWidgetItem("OHARA "+str));  //Change table value to MODEL
372     table->resizeColumnToContents(2);
373     tableitem=table->item(row,2);
374     tableitem->setFlags(Qt::ItemIsEnabled);
375     proc->write("U L\n");
376     proc->write("CHG "+Qrow.toLatin1()+"\n");
377     proc->write("OHARA "+str.toLatin1()+"\n");
378     proc->write("EOS\n");
379     proc->write("RTG ALL\n");
380 }
381
382 void MainWindow::ShowContextMenu6(QAction *Action)
383
384 {
385     QString str = Action->text();
386     row = table->currentRow();          //get number of row
387     QString Qrow = QString::number(row);
388     qDebug() << Action->parent()->objectName();
389
390     LF=18;
391     DataRead("OHARA-O.BIN",str,row);
392     table->setItem(row, 2, new QTableWidgetItem("OHARA-O "+str));  //Change table value to MODEL
393     table->resizeColumnToContents(2);
394     tableitem=table->item(row,2);
395     tableitem->setFlags(Qt::ItemIsEnabled);
396     proc->write("U L\n");
397     proc->write("CHG "+Qrow.toLatin1()+"\n");
398     proc->write("OHARA-O "+str.toLatin1()+"\n");
399     proc->write("EOS\n");
400     proc->write("RTG ALL\n");
401 }
402
403 void MainWindow::ShowContextMenu7(QAction *Action)
404
405 {
406     QString str = Action->text();
407     row = table->currentRow();          //get number of row
408     QString Qrow = QString::number(row);
409     qDebug() << Action->parent()->objectName();
410
411     LF=18;
412     DataRead("RADHARD.BIN",str,row);
413     table->setItem(row, 2, new QTableWidgetItem("RADHARD "+str));  //Change table value to MODEL
414     table->resizeColumnToContents(2);
415     tableitem=table->item(row,2);
416     tableitem->setFlags(Qt::ItemIsEnabled);
417     proc->write("U L\n");
418     proc->write("CHG "+Qrow.toLatin1()+"\n");
419     proc->write("RADHARD "+str.toLatin1()+"\n");
420     proc->write("EOS\n");
421     proc->write("RTG ALL\n");
422 }
423
424 void MainWindow::ShowContextMenu8(QAction *Action)
425
426 {
427     QString str = Action->text();
428     row = table->currentRow();          //get number of row
429     QString Qrow = QString::number(row);
430     qDebug() << Action->parent()->objectName();
431
432     LF=18;
433     DataRead("SCH2000.BIN",str,row);
434     table->setItem(row, 2, new QTableWidgetItem("SCH2000 "+str));  //Change table value to MODEL
435     table->resizeColumnToContents(2);
436     tableitem=table->item(row,2);
437     tableitem->setFlags(Qt::ItemIsEnabled);
438     proc->write("U L\n");
439     proc->write("CHG "+Qrow.toLatin1()+"\n");
440     proc->write("SCH2000 "+str.toLatin1()+"\n");
441     proc->write("EOS\n");
442     proc->write("RTG ALL\n");
443 }
444
445 void MainWindow::ShowContextMenu9(QAction *Action)
446
447 {
448     QString str = Action->text();
449     row = table->currentRow();          //get number of row
450     QString Qrow = QString::number(row);
451     qDebug() << Action->parent()->objectName();
452
453     LF=18;
454     DataRead("SCHOTT.BIN",str,row);
455     table->setItem(row, 2, new QTableWidgetItem("SCHOTT "+str));  //Change table value to MODEL
456     table->resizeColumnToContents(2);
457     tableitem=table->item(row,2);
458     tableitem->setFlags(Qt::ItemIsEnabled);
459     proc->write("U L\n");
460     proc->write("CHG "+Qrow.toLatin1()+"\n");
461     proc->write("SCHOTT "+str.toLatin1()+"\n");
462     proc->write("EOS\n");
463     proc->write("RTG ALL\n");
464 }
465
466
467
468 void MainWindow::InputAir()
469 {
470     row = table->currentRow();          //get number of row
471     QString Qrow = QString::number(row);
472
473     table->setItem(row, 2, new QTableWidgetItem("AIR"));
474     tableitem=table->item(row,2);
475     tableitem->setFlags(Qt::ItemIsEnabled);
476     table->setItem(row, 3, new QTableWidgetItem(""));
477     table->setItem(row, 4, new QTableWidgetItem(""));
478     proc->write("U L\n");
479     proc->write("CHG "+QString(Qrow).toLatin1()+"\n");
480     proc->write("AIR\n");
481     proc->write("EOS\n");
482     proc->write("RTG ALL\n");
483 }
484
485
486 void MainWindow::slot_ShowContextMenu(const QPoint& Pos){
487
488     row = table->currentRow();          //get number of row
489     QString Qrow = QString::number(row);
490     column = table->currentColumn();    //get number of column
491
492     QMenu ContextMenu(this);
493     QMenu SubMenu1("CHANCE");
494     QMenu SubMenu2("CORNIN");
495     QMenu SubMenu3("HIKARI");
496     QMenu SubMenu4("HOYA");
497     QMenu SubMenu5("OHARA");
498     QMenu SubMenu6("OHARA-O");
499     QMenu SubMenu7("RADHARD");
500     QMenu SubMenu8("SCH2000");
501     QMenu SubMenu9("SCHOTT");
502
503     ContextMenu.addAction("Insert Surface");
504     ContextMenu.addAction("Delete Surface");
505     ContextMenu.addSeparator();
506     ContextMenu.addAction("Model");
507     ContextMenu.addAction("AIR");
508
509     ContextMenu.addMenu(&SubMenu1);
510     for (int i=0;i<=NoGlass1;i++){
511         SubMenu1.addAction(QString(GN1.at(i)).toLatin1());
512         SubMenu1.setStyleSheet("QMenu {menu-scrollable: 1;}");
513     }
514
515     ContextMenu.addMenu(&SubMenu2);
516     for (int i=0;i<=NoGlass2;i++){
517         SubMenu2.addAction(QString(GN2.at(i)).toLatin1());
518         SubMenu2.setStyleSheet("QMenu {menu-scrollable: 1;}");
519     }
520
521     ContextMenu.addMenu(&SubMenu3);
522     for (int i=0;i<=NoGlass3;i++){
523         SubMenu3.addAction(QString(GN3.at(i)).toLatin1());
524         SubMenu3.setStyleSheet("QMenu {menu-scrollable: 1;}");
525     }
526
527     ContextMenu.addMenu(&SubMenu4);
528     for (int i=0;i<=NoGlass4;i++){
529         SubMenu4.addAction(QString(GN4.at(i)).toLatin1());
530         SubMenu4.setStyleSheet("QMenu {menu-scrollable: 1;}");
531     }
532
533     ContextMenu.addMenu(&SubMenu5);
534     for (int i=0;i<=NoGlass5;i++){
535         SubMenu5.addAction(QString(GN5.at(i)).toLatin1());
536         SubMenu5.setStyleSheet("QMenu {menu-scrollable: 1;}");
537     }
538
539     ContextMenu.addMenu(&SubMenu6);
540       for (int i=0;i<=NoGlass6;i++){
541         SubMenu6.addAction(QString(GN6.at(i)).toLatin1());
542         SubMenu6.setStyleSheet("QMenu {menu-scrollable: 1;}");
543     }
544
545     ContextMenu.addMenu(&SubMenu7);
546     for (int i=0;i<=NoGlass7;i++){
547         SubMenu7.addAction(QString(GN7.at(i)).toLatin1());
548         SubMenu7.setStyleSheet("QMenu {menu-scrollable: 1;}");
549     }
550
551     ContextMenu.addMenu(&SubMenu8);
552     for (int i=0;i<=NoGlass8;i++){
553         SubMenu8.addAction(QString(GN8.at(i)).toLatin1());
554         SubMenu8.setStyleSheet("QMenu {menu-scrollable: 1;}");
555     }
556
557     ContextMenu.addMenu(&SubMenu9);
558     for (int i=0;i<=NoGlass9;i++){
559         SubMenu9.addAction(QString(GN9.at(i)).toLatin1());
560         SubMenu9.setStyleSheet("QMenu {menu-scrollable: 1;}");
561     }
562
563
564     QAction* selectedItem = ContextMenu.exec(table->mapToGlobal(Pos));
565
566     if (selectedItem==NULL){
567         return;
568     }
569
570
571     if (selectedItem->text()==QString("Insert Surface")){
572         slot_actionInsert_surface();
573     }
574
575     if (selectedItem->text()==QString("Delete Surface")){
576         slot_actionDelete_surface();
577     }
578
579     if (selectedItem->text()==QString("Model")){
580         slot_actionModeldialog();
581         table->resizeColumnToContents(2);
582     }
583
584     if (selectedItem->text()==QString("AIR")){
585         InputAir();
586     }
587
588     if (selectedItem->parent()==&SubMenu1){
589         LF=18;
590         DataRead("CHANCE.BIN",selectedItem->text(),row);
591         table->setItem(row, 2, new QTableWidgetItem("CHANCE "+selectedItem->text()));  //Change table value to MODEL
592         table->resizeColumnToContents(2);
593         tableitem=table->item(row,2);
594         tableitem->setFlags(Qt::ItemIsEnabled);
595         proc->write("U L\n");
596         proc->write("CHG "+Qrow.toLatin1()+"\n");
597         proc->write("CHANCE "+selectedItem->text().toLatin1()+"\n");
598         proc->write("EOS\n");
599         proc->write("RTG ALL\n");
600         return;
601     }
602
603     if (selectedItem->parent()==&SubMenu2){
604         LF=18;
605         DataRead("CORNIN.BIN",selectedItem->text(),row);
606         table->setItem(row, 2, new QTableWidgetItem("CORNIN "+selectedItem->text()));  //Change table value to MODEL
607         table->resizeColumnToContents(2);
608         tableitem=table->item(row,2);
609         tableitem->setFlags(Qt::ItemIsEnabled);
610         proc->write("U L\n");
611         proc->write("CHG "+Qrow.toLatin1()+"\n");
612         proc->write("CORNIN "+selectedItem->text().toLatin1()+"\n");
613         proc->write("EOS\n");
614         proc->write("RTG ALL\n");
615         return;
616     }
617
618     if (selectedItem->parent()==&SubMenu3){
619         LF=18;
620         DataRead("HIKARI.BIN",selectedItem->text(),row);
621         table->setItem(row, 2, new QTableWidgetItem("HIKARI "+selectedItem->text()));  //Change table value to MODEL
622         table->resizeColumnToContents(2);
623         tableitem=table->item(row,2);
624         tableitem->setFlags(Qt::ItemIsEnabled);
625         proc->write("U L\n");
626         proc->write("CHG "+Qrow.toLatin1()+"\n");
627         proc->write("HIKARI "+selectedItem->text().toLatin1()+"\n");
628         proc->write("EOS\n");
629         proc->write("RTG ALL\n");
630         return;
631     }
632
633     if (selectedItem->parent()==&SubMenu4){
634         LF=18;
635         DataRead("HOYA.BIN",selectedItem->text(),row);
636         table->setItem(row, 2, new QTableWidgetItem("HOYA "+selectedItem->text()));  //Change table value to MODEL
637         table->resizeColumnToContents(2);
638         tableitem=table->item(row,2);
639         tableitem->setFlags(Qt::ItemIsEnabled);
640         proc->write("U L\n");
641         proc->write("CHG "+Qrow.toLatin1()+"\n");
642         proc->write("HOYA "+selectedItem->text().toLatin1()+"\n");
643         proc->write("EOS\n");
644         proc->write("RTG ALL\n");
645         return;
646     }
647
648     if (selectedItem->parent()==&SubMenu5){
649         LF=18;
650         DataRead("OHARA.BIN",selectedItem->text(),row);
651         table->setItem(row, 2, new QTableWidgetItem("OHARA "+selectedItem->text()));  //Change table value to MODEL
652         table->resizeColumnToContents(2);
653         tableitem=table->item(row,2);
654         tableitem->setFlags(Qt::ItemIsEnabled);
655         proc->write("U L\n");
656         proc->write("CHG "+Qrow.toLatin1()+"\n");
657         proc->write("OHARA "+selectedItem->text().toLatin1()+"\n");
658         proc->write("EOS\n");
659         proc->write("RTG ALL\n");
660         return;
661     }
662
663     if (selectedItem->parent()==&SubMenu6){
664         LF=18;
665         DataRead("OHARA-O.BIN",selectedItem->text(),row);
666         table->setItem(row, 2, new QTableWidgetItem("OHARA-O "+selectedItem->text()));  //Change table value to MODEL
667         table->resizeColumnToContents(2);
668         tableitem=table->item(row,2);
669         tableitem->setFlags(Qt::ItemIsEnabled);
670         proc->write("U L\n");
671         proc->write("CHG "+Qrow.toLatin1()+"\n");
672         proc->write("OHARA "+selectedItem->text().toLatin1()+"\n");
673         proc->write("EOS\n");
674         proc->write("RTG ALL\n");
675         return;
676     }
677
678     if (selectedItem->parent()==&SubMenu7){
679         LF=18;
680         DataRead("RADHARD.BIN",selectedItem->text(),row);
681         table->setItem(row, 2, new QTableWidgetItem("RADHARD "+selectedItem->text()));  //Change table value to MODEL
682         table->resizeColumnToContents(2);
683         tableitem=table->item(row,2);
684         tableitem->setFlags(Qt::ItemIsEnabled);
685         proc->write("U L\n");
686         proc->write("CHG "+Qrow.toLatin1()+"\n");
687         proc->write("RADHARD "+selectedItem->text().toLatin1()+"\n");
688         proc->write("EOS\n");
689         proc->write("RTG ALL\n");
690         return;
691     }
692
693     if (selectedItem->parent()==&SubMenu8){
694         LF=18;
695         DataRead("SCH2000.BIN",selectedItem->text(),row);
696         table->setItem(row, 2, new QTableWidgetItem("SCH2000 "+selectedItem->text()));  //Change table value to MODEL
697         table->resizeColumnToContents(2);
698         tableitem=table->item(row,2);
699         tableitem->setFlags(Qt::ItemIsEnabled);
700         proc->write("U L\n");
701         proc->write("CHG "+Qrow.toLatin1()+"\n");
702         proc->write("SCH2000 "+selectedItem->text().toLatin1()+"\n");
703         proc->write("EOS\n");
704         proc->write("RTG ALL\n");
705         return;
706     }
707
708     if (selectedItem->parent()==&SubMenu9){
709         LF=18;
710         DataRead("SCHOTT.BIN",selectedItem->text(),row);
711         table->setItem(row, 2, new QTableWidgetItem("SCHOTT "+selectedItem->text()));  //Change table value to MODEL
712         table->resizeColumnToContents(2);
713         tableitem=table->item(row,2);
714         tableitem->setFlags(Qt::ItemIsEnabled);
715         proc->write("U L\n");
716         proc->write("CHG "+Qrow.toLatin1()+"\n");
717         proc->write("SCHOTT "+selectedItem->text().toLatin1()+"\n");
718         proc->write("EOS\n");
719         proc->write("RTG ALL\n");
720         return;
721     }
722
723     else return;
724
725 }
726
727
728 void MainWindow::Glass(QString BinName){
729
730     QByteArray HexData;
731     GN.clear();
732
733     QFile GlassData(hdir+"/LIBGLA/" + BinName);                 //set data file
734
735     GlassData.open(QIODevice::ReadOnly);                    //file open
736     HexData = GlassData.readAll();                          //Read all data to Hex
737     GlassData.close();
738
739     int DataSize = int(GlassData.size());
740     int j=0;                                                //skip first 2 words ('CA')
741
742     QByteArray GlassName;
743
744     int i=132;
745
746     while (i<=DataSize){
747
748
749         while (HexData[i]==char(0x00)){                                   //skip '00'
750             i++;
751         }
752
753         for (int k=i;k<=i+8;k++){
754             GlassName= GlassName+HexData[k];
755         }
756         i=i+8;
757
758         GN << GlassName;
759         j++;
760         i+=100;
761
762         GlassName.clear();
763     }
764
765     NoGlass=j-2;
766
767     return;
768
769 }
770
771
772 void MainWindow::DataRead(QString BinName, QString name, int k){
773
774     QByteArray HexData;
775     QByteArray GlassName;
776     GlassName.clear();
777     GN.clear();
778
779     double nF,nD,nC;
780
781
782     QFile GlassData(hdir+"/LIBGLA/"+ BinName);                 //set data file
783     GlassData.open(QIODevice::ReadOnly);                    //file open
784     HexData = GlassData.readAll();                          //Read all data to Hex
785     GlassData.close();
786
787     int i=2;                                                 //skip first 2 word
788
789     while (GlassName!=name.trimmed()){
790
791         while (HexData[i]==char(0x00)){                                   //skip '00'
792             i++;
793         }
794
795         GlassName.clear();
796
797             for(int j=i;j<=i+10;j++){                                       //Read GlassName (10 letters)
798                 GlassName=(GlassName+HexData[j]).trimmed();
799             }
800                 qDebug()<<GlassName;
801             if (GlassName==name.trimmed()) {
802                 break;
803              }
804
805             i=i+74;
806     }
807
808       i=i+10;
809       GN << GlassName;
810     qDebug() << "GlassName= "+GlassName;
811
812       while (HexData[i]==char(0x20)){                                 //skip '0x20'
813           i++;
814       }
815
816
817       while (HexData[i]!=char(0x20)){                                 //read catalog No. until '0x20'
818               qDebug() << char(HexData[i]);
819         i++;
820       }
821
822
823       while (HexData[i]==char(0x20)){                                   //skip '0x20'
824         i++;
825       }
826
827
828     double A0,A1,A2,A3,A4,A5;
829                                                                        // Read binary data
830         A0=numconv(i,HexData);
831         A1=numconv(i+8,HexData);
832         A2=numconv(i+16,HexData);
833         A3=numconv(i+24,HexData);
834         A4=numconv(i+32,HexData);
835         A5=numconv(i+40,HexData);
836
837     if (BinName=="SCHOTT.BIN"||BinName=="SCH2000.BIN"||BinName=="OHARA.BIN"){
838         qDebug() << BinName;
839         nF=sqrt(1+((A0*lF*lF)/(lF*lF-A3))+((A1*lF*lF)/(lF*lF-A4))+((A2*lF*lF)/(lF*lF-A5)));
840         nD=sqrt(1+((A0*lD*lD)/(lD*lD-A3))+((A1*lD*lD)/(lD*lD-A4))+((A2*lD*lD)/(lD*lD-A5)));
841         nC=sqrt(1+((A0*lC*lC)/(lC*lC-A3))+((A1*lC*lC)/(lC*lC-A4))+((A2*lC*lC)/(lC*lC-A5)));
842         }
843
844     else{
845         qDebug() << BinName;
846         nF=sqrt(A0+A1*pow(lF,2)+A2*1/pow(lF,2)+A3*1/pow(lF,4)+A4*1/pow(lF,6)+A5*1/pow(lF,8));
847         nD=sqrt(A0+A1*pow(lD,2)+A2*1/pow(lD,2)+A3*1/pow(lD,4)+A4*1/pow(lD,6)+A5*1/pow(lD,8));
848         nC=sqrt(A0+A1*pow(lC,2)+A2*1/pow(lC,2)+A3*1/pow(lC,4)+A4*1/pow(lC,6)+A5*1/pow(lC,8));
849     }
850
851     double Abbe=(nD-1)/(nF-nC);
852
853     qDebug() << nD;
854     qDebug() << Abbe;
855     table->setItem(k,3,new QTableWidgetItem(QString::number(nD)));
856     table->setItem(k,4,new QTableWidgetItem(QString::number(Abbe)));
857
858     table->item(k,3)->setFlags(Qt::ItemIsEnabled); //Index & Abbe are not editable.
859     table->item(k,4)->setFlags(Qt::ItemIsEnabled);
860
861     return;
862
863 }
864
865
866 double MainWindow::numconv(int k,QByteArray HexData){
867
868     QByteArray HEXarray0="";
869     QByteArray HEXarray="";
870
871     for (int j=k;j<=k+7;j++){
872         HEXarray0=HEXarray0+HexData[j];                         //read numerical data
873     }
874
875     HEXarray0=HEXarray0.toHex();
876
877     bool ok;
878     int sign = 1;
879
880     for (int i=15;i>0;i-=2){
881         HEXarray[15-i] = HEXarray0[i-1];
882         HEXarray[15-i+1] = HEXarray0[i];
883     }
884
885
886     QByteArray array = QByteArray::number(HEXarray.toULongLong(&ok,16),2); //convert hex to binary -you don't need this since your incoming data is binary
887
888     if(array.length()==64) {
889         if(array.at(0)=='1') sign =-1; // if bit 0 is 1 number is negative
890         array.remove(0,1); // remove sign bit
891     }
892     QByteArray fraction0 =array.right(52); //get the fractional part
893     QByteArray fraction = fraction0;
894
895     double mantissa = 1;
896
897     for(int i=0;i<fraction.length();i++){ // iterate through the array to calculate the fraction as a decimal.
898         if(fraction.at(i)=='1') mantissa += 1.0/(pow(2,i+1));
899     }
900
901     int exponent = array.left(array.length()-52).toULongLong(&ok,2); //calculate the exponent
902     QString Ax = QString::number( sign*pow(2,exponent-1023)*(mantissa),'e', 10 );
903     double num = Ax.toDouble();
904
905     return num;
906
907 }
908
909
910 void MainWindow::ReadFileToTable(QString pathname)
911 {
912
913     QFile textFile(pathname);
914     QString buffer = "";
915     textFile.open(QIODevice::ReadOnly); // file open
916
917     QTextStream input(&textFile);
918     buffer = input.readAll();    // read data
919     textFile.close();
920
921     QStringList lines;
922     nol = buffer.count("CV"); //number of lines of lens data file
923     int nof = buffer.count("\n"); //total number of lines in lens data file
924     lines << buffer.split("\n");  //separate single lines
925
926     int k=0;
927     QString item;
928
929     int comma;
930     int comma2;
931     QString separeter;
932
933     QStringList label;
934     for (int i=0; i<=nol-1; i++){
935         label << QString::number(i);    //set table vetical label start to 0
936     }
937
938     table->clear();
939     table->setRowCount( nol );          //number of row = nol
940     table->setVerticalHeaderLabels(label);
941     table->setHorizontalHeaderLabels( QStringList() << tr("Radius") << tr("Thickness" ) << tr("Material") << tr("Index n")<< tr("Abbe V")<< tr("aperture"));
942
943     QStringList lambda;
944     QRegExp RegExp("WV *");
945     RegExp.setPatternSyntax(QRegExp::Wildcard); //pick up Wavelength
946
947     for(int i=0; i<=nof; i++){
948         item = lines[i];
949
950         if (RegExp.exactMatch(item)){
951
952             for(int j=0; j<=100; j++){
953                 separeter = item.mid(j,1);
954
955                 if (separeter == "."){
956                 lambda << item.mid(j-1,7);
957
958                 }
959
960             }
961
962             lD=lambda[0].toDouble();
963             lF=lambda[1].toDouble();
964             lC=lambda[2].toDouble();
965
966             qDebug() << lD;
967             qDebug() << lF;
968             qDebug() << lC;
969
970         }
971     }
972
973
974     QString radius;
975     QString curveture;
976     QRegExp RegExp1("CV *");
977     RegExp1.setPatternSyntax(QRegExp::Wildcard); //pick up lens curveture
978
979     k=0;
980
981     for(int i=0; i<=nof; i++){        
982
983         item = lines[i];
984
985         comma = item.indexOf(",")+1; // pick up numenical value
986         curveture = item.trimmed().mid(comma,25);
987         radius = radius.setNum(1.0E0/curveture.toDouble(),'g',6); //convert curveture to radius
988
989         if (RegExp1.exactMatch(item)){
990         table->setItem( k, 0, new QTableWidgetItem(radius));
991         }
992
993     QString thickness;
994     QRegExp RegExp2("TH *");
995     RegExp2.setPatternSyntax(QRegExp::Wildcard);  //pick up lens thickness
996
997
998         item = lines[i];
999         comma = item.indexOf(",")+1;
1000
1001         if (RegExp2.exactMatch(item)){
1002         thickness = item.trimmed().mid(comma,25);
1003         thickness = thickness.setNum(thickness.toDouble(),'g',comma2-comma);
1004         table->setItem( k, 1, new QTableWidgetItem(thickness));
1005         k++;
1006         }
1007
1008     QString aperture;
1009     QRegExp RegExp4("CLAP *");
1010     RegExp4.setPatternSyntax(QRegExp::Wildcard);  //pick up lens aperture radius
1011
1012         item = lines[i];
1013         comma = item.indexOf(",")+1;
1014         comma2 =comma+item.indexOf(",");
1015
1016         if (RegExp4.exactMatch(item)){
1017             aperture = item.trimmed().mid(comma,comma2-comma);
1018             aperture = aperture.setNum(aperture.toDouble(),'g',6);
1019             table->setItem( k-1, 5, new QTableWidgetItem(aperture));
1020         }
1021     }
1022
1023
1024     QRegExp RegExp3("RAYERROR*");        //pick up glass name/air. Material name is just below "RAYERROR"
1025     RegExp3.setPatternSyntax(QRegExp::Wildcard);
1026
1027     QString material;
1028     QString name;
1029     QString index;
1030     QString Abbe;
1031     k=0;
1032
1033     for(int i=0; i<nof; i++){
1034         item = lines[i];
1035         comma = lines[i+1].indexOf(",");
1036         material = lines[i+1].trimmed();
1037         LF = lines[i+1].length();
1038         name = lines[i+1].mid(7,LF).trimmed();
1039
1040         if (k==nol){
1041             table->setItem(k-1,2,new QTableWidgetItem("LAST SURFACE"));
1042             table->resizeColumnToContents(2);
1043             return;
1044         }
1045
1046
1047         if (material.left(3)=="AIR"){
1048             table->setItem(k,2,new QTableWidgetItem("AIR"));
1049             k++;
1050         }
1051
1052
1053         if (material.left(4)=="REFS"){
1054
1055             if((lines[i+2].trimmed()=="ASTOP")*(lines[i+3].trimmed()=="REFL")){
1056                 table->setItem(k,2,new QTableWidgetItem("REFS STOP REFL"));
1057                 k++;
1058                 i=i+3;
1059             }
1060
1061             if(lines[i+2].trimmed()=="ASTOP"){
1062                 table->setItem(k,2,new QTableWidgetItem("REFS STOP"));
1063                 k++;
1064                 i=i+2;
1065             }
1066             if(lines[i+2].trimmed()=="REFL"){
1067                 table->setItem(k,2,new QTableWidgetItem("REFS REFL"));
1068                 k++;
1069                 i=i+2;
1070             }
1071
1072         }
1073
1074
1075         if (material.left(4)=="REFL"){
1076             table->setItem(k,2,new QTableWidgetItem("REFL"));
1077             k++;
1078         }
1079
1080
1081         if (material.left(3)=="GLA"){
1082             table->setItem(k,2,new QTableWidgetItem(material));
1083             table->setItem(k,3,new QTableWidgetItem("1.00000"));
1084             table->setItem(k,4,new QTableWidgetItem("0.00000"));
1085             k++;
1086         }
1087
1088         if (material.left(5)=="MODEL"){
1089             index = lines[i+1].mid(comma+4,20);
1090             Abbe =  lines[i+1].mid(comma+28,20);
1091             table->setItem(k,2,new QTableWidgetItem(material.left(15)));
1092             table->setItem(k,3,new QTableWidgetItem(index.setNum(index.toDouble(),'g',6)));
1093             table->setItem(k,4,new QTableWidgetItem(Abbe.setNum(Abbe.toDouble(),'g',6)));
1094             table->resizeColumnToContents(2);
1095             k++;
1096         }
1097
1098         if (material.left(3)=="SCH"){
1099             DataRead("SCHOTT.BIN",name,k);
1100             table->setItem(k,2,new QTableWidgetItem(material));
1101             table->resizeColumnToContents(2);
1102             k++;
1103             continue;
1104         }
1105
1106
1107         if (material.left(7)=="SCH2000"){
1108             DataRead("SCH2000.BIN",name,k);
1109             table->setItem(k,2,new QTableWidgetItem(material));
1110             table->resizeColumnToContents(2);
1111             k++;
1112             continue;
1113         }
1114
1115
1116         if (material.left(6)=="CHANCE"){
1117             DataRead("CHANCE.BIN",name,k);
1118             table->setItem(k,2,new QTableWidgetItem(material));
1119             table->resizeColumnToContents(2);
1120             k++;
1121             continue;
1122         }
1123
1124         if (material.left(6)=="CORNIN"){
1125             DataRead("CORNIN.BIN",name,k);
1126             table->setItem(k,2,new QTableWidgetItem(material));
1127             table->resizeColumnToContents(2);
1128             k++;
1129             continue;
1130         }
1131
1132         if (material.left(6)=="HIKARI"){
1133             DataRead("HIKARI.BIN",name,k);
1134             table->setItem(k,2,new QTableWidgetItem(material));
1135             table->resizeColumnToContents(2);
1136             k++;
1137             continue;
1138         }
1139
1140         if (material.left(4)=="HOYA"){
1141             DataRead("HOYA.BIN",name,k);
1142             table->setItem(k,2,new QTableWidgetItem(material));
1143             table->resizeColumnToContents(2);
1144             k++;
1145             continue;
1146         }
1147
1148         if (material.left(5)=="OHARA"){
1149             qDebug()<<material;
1150             DataRead("OHARA-O.BIN",name,k);
1151             table->setItem(k,2,new QTableWidgetItem(material));
1152             table->resizeColumnToContents(2);
1153             k++;
1154             continue;
1155         }
1156
1157
1158         if (material.left(5)=="OHARA"){
1159                         qDebug()<<material;
1160                         qDebug()<<name;
1161             DataRead("OHARA.BIN",name,k);
1162             qDebug()<<"Hello";
1163             table->setItem(k,2,new QTableWidgetItem(material));
1164             table->resizeColumnToContents(2);
1165             k++;
1166             continue;
1167         }
1168
1169
1170         if (material.left(7)=="RADHARD"){
1171             DataRead("RADHARD.BIN",name,k);
1172             table->setItem(k,2,new QTableWidgetItem(material));
1173             table->resizeColumnToContents(2);
1174             k++;
1175             continue;
1176         }
1177
1178     }
1179
1180 }
1181
1182
1183 // File Menu
1184
1185
1186 void MainWindow::slot_actionOpen()
1187 {
1188
1189     QFileDialog dlg;
1190     QString pathname = dlg.getOpenFileName(this,tr("Open Lens File"), hdir+"/LENSES",tr("LENS (*.PRG);;All Files (*)"));
1191
1192         QFileInfo info1 (pathname);
1193         QString filename = info1.baseName();
1194
1195     if (filename!=""){                                             // OPEN Button
1196         proc->write("\n");
1197         proc->write("LENSREST "+filename.toUtf8()+"\n");
1198         proc->write("RTG ALL\n");
1199
1200         ReadFileToTable(pathname);
1201
1202         proc->write("\n");
1203         proc->write("VIE XZ\n");
1204         return;
1205     }
1206
1207         if (filename==""){                                              //CANCEL Button
1208         return;
1209     }
1210 }
1211
1212
1213
1214 void MainWindow::slot_actionSave()
1215 {
1216
1217     QString pathname = QFileDialog::getSaveFileName(this,tr("Save Lens File"), hdir+"/LENSES",tr("LENS (*.PRG);;All Files (*)"));
1218     QFileInfo info1 (pathname);
1219     QString filename = info1.baseName();
1220
1221     if (filename!=""){
1222         proc->write("\n");
1223         proc->write("LENSSAVE "+filename.toUtf8()+"\n");
1224         return;
1225     }
1226
1227     if (filename==""){                                              //CANCEL Button
1228         return;
1229     }
1230
1231 }
1232
1233 void MainWindow::slot_actionImport_Zemax()
1234 {
1235
1236     QString pathname = QFileDialog::getOpenFileName(this,tr("Import ZEMAX File"), hdir+"/LENSES",tr("ZEMAX (*.ZMX);;All Files (*)"));
1237     QFileInfo info1 (pathname);
1238     QString filename = info1.fileName();
1239
1240     if (filename!=""){
1241         proc->write("\n");
1242         proc->write("ZMX2PRG "+filename.toUtf8()+"\n");
1243         proc->write("LENSSAVE\n");
1244         proc->write("RTG ALL\n");
1245
1246         ReadFileToTable(hdir+"/LENSES/LENS.PRG");
1247         proc->write("VIE XZ\n");
1248         return;
1249     }
1250
1251     if (filename==""){                                              //CANCEL Button
1252         return;
1253     }
1254
1255 }
1256
1257
1258 void MainWindow::slot_actionImport_CODE_V()
1259 {
1260
1261     QString pathname = QFileDialog::getOpenFileName(this,tr("Import Code-V File"), hdir+"/LENSES",tr("Code-V (*.SEQ);;All Files (*)"));
1262     QFileInfo info1 (pathname);
1263     QString filename = info1.fileName();
1264
1265     if (filename!=""){
1266         proc->write("\n");
1267         proc->write("CV2PRG "+filename.toUtf8()+"\n");
1268         proc->write("RTG ALL\n");
1269         proc->write("LENSSAVE\n");
1270         ReadFileToTable(hdir+"/LENSES/LENS.PRG");
1271         proc->write("VIE XZ\n");
1272         return;
1273     }
1274
1275     if (filename==""){                                              //CANCEL Button
1276         return;
1277     }
1278
1279 }
1280
1281
1282 void MainWindow::slot_actionExport_Zemax()
1283 {
1284
1285     QString pathname = QFileDialog::getSaveFileName(this,tr("Export ZEMAX File"), hdir+"/LENSES",tr("ZEMAX (*.ZMX);;All Files (*)"));
1286     QFileInfo info1 (pathname);
1287     QString filename = info1.fileName();
1288
1289     if (filename!=""){
1290         proc->write("\n");
1291         proc->write("OUT FILE "+filename.toUtf8()+"\nLENO ZMX\nOUT TP\n");
1292     return;
1293     }
1294
1295     if (filename==""){                                              //CANCEL Button
1296         return;
1297     }
1298
1299 }
1300
1301 void MainWindow::slot_actionExport_CODE_V()
1302 {
1303
1304     QString pathname = QFileDialog::getSaveFileName(this,tr("Export Code-V File"), hdir+"/LENSES",tr("Code-V (*.SEQ);;All Files (*)"));
1305     QFileInfo info1 (pathname);
1306     QString filename = info1.fileName();
1307
1308     if (filename!=""){
1309         proc->write("\n");
1310         proc->write("OUT FILE "+filename.toUtf8()+"\nLENO CV\nOUT TP\n");
1311     return;
1312     }
1313
1314     if (filename==""){                                              //CANCEL Button
1315         return;
1316     }
1317
1318 }
1319
1320
1321 //Edit Menu
1322
1323 void MainWindow::slot_actionInsert_surface()
1324 {
1325     proc->write("\n");
1326
1327     int row;
1328
1329     row = table->currentRow();              //get number of row
1330
1331     if (row < 0){
1332         row=1;
1333     }
1334
1335     if (row != 0){
1336         table->insertRow(row);              //insert row to GUI table
1337         table->setItem(row,0,new QTableWidgetItem("inf"));
1338         table->setItem(row,1,new QTableWidgetItem("0"));
1339         table->setItem(row,2,new QTableWidgetItem("AIR"));
1340         tableitem=table->item(row,2);
1341         tableitem->setFlags(Qt::ItemIsEnabled);
1342         nol++;
1343     }
1344
1345     QStringList label;
1346     for (int i=0; i<=nol-1; i++){
1347         label << QString::number(i);        //set table vetical label start to 0
1348     }
1349
1350     table->setRowCount( nol );              //number of row = nol
1351     table->setVerticalHeaderLabels(label);
1352
1353     QString Qrow = QString::number(row);    //get number of insert surface
1354     QString cmd = "INS "+Qrow+"\n";
1355     proc->write("\n");
1356     proc->write("U L\n");                   //move to ULN mode
1357     proc->write(cmd.toLatin1());
1358     proc->write("EOS\n");                   //return to CMD mode
1359     proc->write("RTG ALL\n");
1360 }
1361
1362
1363 void MainWindow::slot_actionDelete_surface()
1364 {
1365     int row;
1366     row = table->currentRow();              //get number of row
1367
1368     if (row < 0) return;
1369
1370     proc->write("\n");
1371
1372     if ((row != 0)&&(row != nol-1)){
1373         table->removeRow(row);              //delete row from GUI table
1374         nol--;
1375     }
1376     QStringList label;
1377     for (int i=0; i<=nol-1; i++){
1378         label << QString::number(i);        //set table vetical label start to 0
1379     }
1380
1381     table->setRowCount( nol );              //number of row = nol
1382     table->setVerticalHeaderLabels(label);
1383
1384
1385     QString Qrow = QString::number(row);    //get number of delete surface
1386     QString cmd = "DEL "+Qrow+"\n";
1387     proc->write("\n");
1388     proc->write("U L\n");                   //move to ULN mode
1389     proc->write(cmd.toLatin1());
1390     proc->write("EOS\n");                   //return to CMD mode
1391     proc->write("RTG ALL\n");
1392 }
1393
1394
1395 void MainWindow::slot_actionDisplayLensData()
1396 {
1397     proc->write("\n");
1398     proc->write("LIS\n");
1399 }
1400
1401
1402 //View Menu
1403
1404 void MainWindow::slot_actionXZ()
1405 {
1406     proc->write("\n");
1407     proc->write("VIE XZ\n");
1408 }
1409
1410 void MainWindow::slot_actionOrtho()
1411 {
1412     proc->write("\n");
1413     proc->write("VIE ORTHO\n");
1414 }
1415
1416 void MainWindow::slot_focus()
1417 {
1418     QString cmd;
1419     QString opr = "CHG ";               //set edit surface
1420     QString num = QString::number(nol-2);
1421     QString lf = "\n";
1422     const char *input_cmd;
1423     qDebug()<< num;
1424     cmd = "";
1425     cmd = opr + num + lf;               //generate command
1426     input_cmd = cmd.toLatin1();
1427
1428     proc->write("U L\n");
1429     proc->write(input_cmd);
1430     proc->write("PY\n");
1431     proc->write("EOS\n");
1432     proc->write("RTG ALL\n");
1433     proc->write("VIE\n");
1434
1435 }
1436
1437
1438 //Graph Menu
1439
1440 void MainWindow::slot_actionXZ_YZ_Transverce_Aberrations()
1441 {
1442     proc->write("\n");
1443     proc->write("FANS\n");
1444 }
1445
1446 void MainWindow::slot_actionDistortion()
1447 {
1448     proc->write("\n");
1449     proc->write("DIST\nPLTDIST\n");
1450 }
1451
1452 void MainWindow::slot_actionField_Curvature()
1453 {
1454     proc->write("\n");
1455     proc->write("FLDCV\nPLTFLDCV\n");
1456 }
1457
1458 void MainWindow::slot_actionAstigmatism()
1459 {
1460     proc->write("\n");
1461     proc->write("AST\nPLTAST\n");
1462 }
1463
1464 void MainWindow::slot_actionGeometical()
1465 {
1466     proc->write("\n");
1467     proc->write("GOTF\nPLTGOTF\n");
1468 }
1469
1470 void MainWindow::slot_actionGeometical_Leica()
1471 {
1472     proc->write("\n");
1473     proc->write("GOTF\nPLTGOTF LEICA\n");
1474 }
1475
1476 void MainWindow::slot_actionDiffraction()
1477 {
1478     proc->write("\n");
1479     proc->write("DOTF\nPLTDOTF\n");
1480 }
1481
1482 void MainWindow::slot_actionDiffraction_Leica()
1483 {
1484     proc->write("\n");
1485     proc->write("DOTF\nPLTDOTF LEICA\n");
1486 }
1487
1488 void MainWindow::slot_actionSpot_Diagram()
1489 {
1490     proc->write("\n");
1491     proc->write("SPD\nPLTSPD\n");
1492 }
1493
1494 void MainWindow::slot_actionWavefront_Phase()
1495 {
1496     proc->write("\n");
1497     proc->write("CAPFN\nPLOT CAPFNOPD\n");
1498 }
1499
1500 void MainWindow::slot_actionWavefront_Intensity()
1501 {
1502     proc->write("\n");
1503     proc->write("CAPFN\nPLOT CAPFNAPD\n");
1504 }
1505
1506 void MainWindow::slot_actionPoint_Spread_Function()
1507 {
1508     proc->write("\n");
1509     proc->write("PSF\n");
1510 }
1511
1512
1513 void MainWindow::slot_action_value_entered()
1514 {
1515     proc->write("\n");
1516
1517     row = table->currentRow();          //get number of row
1518     column = table->currentColumn();    //get number of column
1519
1520     if (row==0) return;
1521
1522     QString cmd;
1523     QString opr = "CHG ";               //set edit surface
1524     QString num = QString::number(row);
1525     QString lf = "\n";
1526
1527     const char *input_cmd;
1528
1529
1530     cmd = "";
1531     cmd = opr + num + lf;               //generate command
1532     input_cmd = cmd.toLatin1();
1533
1534     proc->write("U L\n");
1535     proc->write(input_cmd);             //input command (edit surface)
1536
1537     switch (column){
1538         case 0:
1539             opr="RD ";                  //edit radius
1540             break;
1541         case 1:
1542             opr="TH ";                  //edit thickness
1543             break;
1544         case 2:                         //edit glass data
1545             dialog = new nkDialog();
1546             dialog->exec();
1547
1548         if (dialog->Button1 == 1024){
1549
1550             proc->write("U L\n");
1551             proc->write("CHG 2\n");
1552             proc->write("MODEL "+dialog->GlassName.toLatin1()+","+dialog->Index.toLatin1()+","+dialog->Abbe.toLatin1()+",\n");
1553             proc->write("EOS\n");
1554             proc->write("RTG ALL\n");
1555
1556             table->setItem(row, 2, new QTableWidgetItem(dialog->GlassName.toUpper()));  //Change table value to MODEL
1557             table->setItem(row, 3, new QTableWidgetItem(dialog->Index.trimmed()));
1558             table->setItem(row, 4, new QTableWidgetItem(dialog->Abbe.trimmed()));
1559             break;
1560         }
1561
1562         case 5:
1563         opr="CLAP ";                  //edit aperture radius
1564         break;
1565
1566     }
1567
1568     num = table->item(row,column)->text();  //get input value
1569
1570     cmd = "";
1571     cmd = opr + num + lf;
1572     input_cmd = cmd.toLatin1();
1573
1574     proc->write(input_cmd);
1575     proc->write("EOS\n");                   //exit LENS INPUT mode (Return to CMD mode)
1576     proc->write("RTG ALL\n");
1577
1578 }
1579
1580
1581 //Input Model menu
1582
1583 void MainWindow::slot_actionModeldialog()
1584 {
1585
1586     int row;
1587     row = table->currentRow();              //get number of row
1588     QString Qrow = QString::number(row);    //get number of modify surface
1589
1590     dialog = new nkDialog();
1591 //    dialog->setWindowState(dialog->windowState() | Qt::WindowMaximized); //for android
1592     dialog->exec();
1593
1594     if (dialog->Button1 == 1024){
1595
1596     proc->write("\n");
1597     proc->write("U L\n");
1598     proc->write("CHG "+Qrow.toLatin1()+"\n");
1599     proc->write("MODEL "+dialog->GlassName.toLatin1()+","+dialog->Index.toLatin1()+","+dialog->Abbe.toLatin1()+",\n");
1600     proc->write("EOS\n");
1601     proc->write("RTG ALL\n");
1602     proc->write("FINDGLASS "+Qrow.toLatin1()+"\n");
1603
1604     table->setItem(Qrow.toInt(), 2, new QTableWidgetItem("MODEL "+dialog->GlassName.toUpper()));  //Change table value to MODEL
1605     tableitem=table->item(row,2);
1606     tableitem->setFlags(Qt::ItemIsEnabled);
1607     table->setItem(Qrow.toInt(), 3, new QTableWidgetItem(dialog->Index.trimmed()));
1608     tableitem=table->item(row,3);
1609     tableitem->setFlags(Qt::ItemIsEnabled);
1610     table->setItem(Qrow.toInt(), 4, new QTableWidgetItem(dialog->Abbe.trimmed()));
1611     tableitem=table->item(row,4);
1612     tableitem->setFlags(Qt::ItemIsEnabled);
1613
1614     }
1615
1616 }
1617
1618
1619 void MainWindow::slot_actionInput_Variables()
1620 {
1621
1622     dialog2 = new Optimize();
1623 //    dialog2->setWindowState(dialog2->windowState() | Qt::WindowMaximized); //for android
1624     dialog2->exec();
1625
1626     if (dialog2->Button2 == 1024){
1627
1628     proc->write("\n");
1629     proc->write("MERIT\n");
1630     proc->write("FLCLTH "+dialog2->FocalLength.toLatin1()+" 1 0 "+QString::number(nol-1).toLatin1()+"\n");
1631     proc->write(dialog2->Target.toLatin1()+" 0.0 1\n");
1632     proc->write("EOS\n");
1633
1634     proc->write("VARIABLES\n");
1635     proc->write(dialog2->Parameter1.toLatin1()+" "+dialog2->ParamValue1.toLatin1()+"\n");
1636     proc->write(dialog2->Parameter2.toLatin1()+" "+dialog2->ParamValue2.toLatin1()+"\n");
1637     proc->write(dialog2->Parameter3.toLatin1()+" "+dialog2->ParamValue3.toLatin1()+"\n");
1638     proc->write(dialog2->Parameter4.toLatin1()+" "+dialog2->ParamValue4.toLatin1()+"\n");
1639     proc->write(dialog2->Parameter5.toLatin1()+" "+dialog2->ParamValue5.toLatin1()+"\n");
1640     proc->write("EOS\n");
1641
1642     proc->write("VB\n");
1643     proc->write("OPRD\n");
1644
1645     proc->write("ITER FULL\n");
1646     proc->write("RTG ALL\n");
1647
1648     }
1649 }
1650
1651
1652 void MainWindow::slot_actionInput_LensIdentifier()
1653 {
1654
1655     dialog3 = new LIDialog();
1656 //    dialog3->setWindowState(dialog3->windowState() | Qt::WindowMaximized); //for android
1657     dialog3->exec();
1658
1659     if (dialog3->Button3 == 1024){
1660
1661     proc->write("\n");
1662     proc->write("U L\n");
1663     proc->write("LI "+dialog3->LensIdentifier.toLatin1()+" \n");
1664     proc->write("EOS\n");
1665
1666     proc->write("LI\n");
1667
1668     }
1669 }
1670
1671 void MainWindow::slot_actionRay_input_angle()
1672 {
1673
1674     dialog4 = new rayinputDialog();
1675 //    dialog4->setWindowState(dialog4->windowState() | Qt::WindowMaximized); //for android
1676     dialog4->exec();
1677
1678     if (dialog4->Button4 == 1024){
1679
1680     proc->write("\n");
1681     proc->write("U L\n");
1682     proc->write("SCY FANG "+dialog4->inputangle.toLatin1()+"\n");
1683     qDebug()<< dialog4->inputangle.toLatin1();
1684     proc->write("EOS\n");
1685
1686     proc->write("VIE\n");
1687
1688     }
1689 }
1690
1691
1692 bool MainWindow::eventFilter(QObject* object, QEvent* event)
1693 {
1694
1695     if (object==table){
1696     if (event->type()==QEvent::KeyRelease)
1697     {
1698         // key pressed
1699         // transforms QEvent into QKeyEvent
1700         QKeyEvent* pKeyEvent=static_cast<QKeyEvent*>(event);
1701         switch(pKeyEvent->key())
1702         {
1703             case Qt::Key_Return:
1704             {
1705                 // Return key pressed - record update
1706
1707                 qDebug() << "Return key pressed";
1708                 slot_action_value_entered();
1709                 break;
1710             }
1711             case Qt::Key_Enter:
1712             {
1713                // Enter key pressed - record update
1714
1715                 qDebug() << "Enter key pressed";
1716                 slot_action_value_entered();
1717                 break;
1718             }
1719         }
1720     }
1721     }
1722         return QWidget::eventFilter(object, event);
1723 }
1724