OSDN Git Service

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