OSDN Git Service

Added command history.
authorENDO Michiaki <dinosaur@db3.so-net.ne.jp>
Fri, 13 Mar 2015 06:58:48 +0000 (15:58 +0900)
committerENDO Michiaki <dinosaur@db3.so-net.ne.jp>
Fri, 13 Mar 2015 06:58:48 +0000 (15:58 +0900)
KODS_GUI.pro.user
mainwindow.cpp
mainwindow.h
mainwindow.ui

index c315fa7..f20b307 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE QtCreatorProject>
-<!-- Written by QtCreator 3.1.2, 2015-03-11T08:41:04. -->
+<!-- Written by QtCreator 3.1.2, 2015-03-13T15:56:29. -->
 <qtcreator>
  <data>
   <variable>ProjectExplorer.Project.ActiveTarget</variable>
index 8875127..8368b3f 100644 (file)
@@ -14,6 +14,8 @@ MainWindow::MainWindow(QMainWindow *parent)
     : QMainWindow( parent )
 {
 
+    histnum=0;
+    curhist=histnum;
 
     hdir="/usr/local/KODS";   //For Linux, MacOSX
 
@@ -31,6 +33,7 @@ MainWindow::MainWindow(QMainWindow *parent)
     QObject::connect( proc, SIGNAL(readyReadStandardError()), this, SLOT(slot_appendView()) ); //display stderr
 
     table->installEventFilter(this);  //install event filter
+    cmdLine->installEventFilter(this);  //install event filter
 
     table->setColumnCount( 6 );
     table->setRowCount( 20 );
@@ -266,7 +269,7 @@ void MainWindow::slot_commandExec()
 
     if (input == "exit\n")
     {
-        QApplication::quit();
+        slot_quit2();
     }
 
     if (this->isActiveWindow())
@@ -1813,6 +1816,62 @@ bool MainWindow::eventFilter(QObject* object, QEvent* event)
         }
     }
     }
+
+    if (object == cmdLine){
+        if (event->type()==QEvent::KeyRelease)
+        {
+            // key pressed
+            // transforms QEvent into QKeyEvent
+            QKeyEvent* pKeyEvent=static_cast<QKeyEvent*>(event);
+            switch(pKeyEvent->key())
+            {
+                case Qt::Key_Up:
+                {
+                    // Up arrow key pressed - record update
+
+                    qDebug() << "Up arrow key pressed";
+                    curhist--;
+                    if (curhist < 0) curhist=histnum-1;
+                    history(curhist);
+                    cmdLine->setText(DispHistory);
+                    break;
+                }
+                case Qt::Key_Down:
+                {
+                   // Down arrow key pressed - record update
+
+                    qDebug() << "Down arrow key pressed";
+                    curhist++;
+                    if (curhist >= histnum) curhist=0;
+                    history(curhist);
+                    cmdLine->setText(DispHistory);
+                    break;
+
+                }
+
+                case Qt::Key_Return:
+                {
+                   // Enter key pressed - record update
+
+                    qDebug() << "Return key pressed";
+
+                QFile HistData(hdir+"/HISTORY.DAT");                 //set data file
+                HistData.open(QIODevice::WriteOnly|QIODevice::Append);
+
+                QTextStream out(&HistData);
+                if ((cmdLine->text().toUtf8()) == "") break;
+                out << cmdLine->text()+"\n";
+                HistData.close();
+
+                    histnum++;
+                    curhist++;
+                    break;
+                 }
+
+            }
+        }
+
+    }
         return QWidget::eventFilter(object, event);
 }
 
@@ -1823,3 +1882,29 @@ void MainWindow::delay()  //Code from StackOverFlow
     QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
 }
 
+void MainWindow::history(int curhist)
+{
+
+    QFile HistData(hdir+"/HISTORY.DAT");                 //set data file
+
+    HistData.open(QIODevice::ReadOnly);                    //file open
+    QTextStream input(&HistData);
+
+    for (int i=0; i<=curhist; i++)
+    {
+        DispHistory = input.readLine(0);    // read data
+        qDebug() << DispHistory;
+        qDebug() << histnum;
+    }
+
+    HistData.close();
+
+}
+
+void MainWindow::slot_quit2()
+{
+    proc->write("EXIT\n");
+    delay();
+    QFile::remove(hdir+"/HISTORY.DAT");
+    QApplication::quit();
+}
index 4efbfc3..317d1e1 100644 (file)
@@ -47,18 +47,26 @@ private:
     int row;
     int column;
     int LF;
+    int histnum;
+    int curhist;
+
     double numconv(int,QByteArray);
+
     void ndex();
     void Glass(QString);
     void DataRead(QString, QString, int);
     void ReadFileToTable(QString);
     void addcontextmenu();
     void menu();
+    void delay();
+    void history(int);
+
     nkDialog *dialog;
     Optimize *dialog2;
     LIDialog *dialog3;
     rayinputDialog *dialog4;
     QStringList GN,GN1,GN2,GN3,GN4,GN5,GN6,GN7,GN8,GN9;
+    QString DispHistory;
     bool eventFilter(QObject* object, QEvent* event);
 
     QMouseEvent *event;
@@ -74,6 +82,7 @@ private slots:
     void slot_actionImport_CODE_V();
     void slot_actionExport_Zemax();
     void slot_actionExport_CODE_V();
+    void slot_quit2();
 
     void slot_actionInsert_surface();
     void slot_actionDelete_surface();
@@ -122,8 +131,6 @@ private slots:
     void InputAir();
     void InputReflector();
 
-    void delay();
-
 };
 
 
index 55b3c52..c79128e 100644 (file)
    <sender>actionQuit</sender>
    <signal>triggered()</signal>
    <receiver>MainWindow</receiver>
-   <slot>close()</slot>
+   <slot>slot_quit2()</slot>
    <hints>
     <hint type="sourcelabel">
      <x>-1</x>
   <slot>slot_focus()</slot>
   <slot>slot_actionRay_input_angle()</slot>
   <slot>slot_actionPltchrsh()</slot>
+  <slot>slot_quit2()</slot>
  </slots>
 </ui>