changeset 13339:cf95d1db42c6

Added history to terminal.
author Jacob Dawid <jacob.dawid@googlemail.com>
date Tue, 05 Apr 2011 22:07:39 +0200
parents 56a7b194486d
children eb510dd290bf
files gui//octaveterminal.cpp gui//octaveterminal.h
diffstat 2 files changed, 48 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/gui//octaveterminal.cpp
+++ b/gui//octaveterminal.cpp
@@ -52,7 +52,7 @@
 void OctaveTerminal::sendCommand(QString command) {
     m_octaveOutput->setFontUnderline(true);
     m_octaveOutput->append(command);
-    QMetaObject::invokeMethod(m_client, "send", Q_ARG(QString, command));
+    QMetaObject::invokeMethod(m_client, "send", Q_ARG(QString, command + "\n"));
 }
 
 void OctaveTerminal::blockUserInput() {
--- a/gui//octaveterminal.h
+++ b/gui//octaveterminal.h
@@ -35,7 +35,8 @@
     Q_OBJECT
 public:
     TerminalCommandLine(QWidget *parent = 0)
-        : QLineEdit(parent) {
+        : QLineEdit(parent),
+          m_commandHistoryIndex(0) {
     }
 
 signals:
@@ -43,17 +44,61 @@
 
 protected:
     void keyPressEvent(QKeyEvent *keyEvent) {
+        QString command;
         switch(keyEvent->key()) {
             case Qt::Key_Return:
-                emit claimCommand(text() + "\n");
+                command = text();
+                emit claimCommand(command);
+                m_commandHistory.append(command);
+                m_commandHistoryIndex = m_commandHistory.size();
+                m_currentlyEditedCommand = "";
                 setText("");
                 break;
 
+            case Qt::Key_Up:
+                if(!m_commandHistory.empty())
+                {
+                    if(m_commandHistoryIndex == m_commandHistory.size())
+                        m_currentlyEditedCommand = text();
+
+                    m_commandHistoryIndex--;
+                    if(m_commandHistoryIndex < 0)
+                        m_commandHistoryIndex = m_commandHistory.size();
+
+                    if(m_commandHistoryIndex == m_commandHistory.size())
+                        setText(m_currentlyEditedCommand);
+                    else
+                        setText(m_commandHistory.at(m_commandHistoryIndex));
+                }
+                break;
+
+            case Qt::Key_Down:
+                if(!m_commandHistory.empty())
+                {
+                    if(m_commandHistoryIndex == m_commandHistory.size())
+                        m_currentlyEditedCommand = text();
+
+                    m_commandHistoryIndex++;
+                    if(m_commandHistoryIndex > m_commandHistory.size())
+                        m_commandHistoryIndex = 0;
+
+                    if(m_commandHistoryIndex == m_commandHistory.size())
+                        setText(m_currentlyEditedCommand);
+                    else
+                        setText(m_commandHistory.at(m_commandHistoryIndex));
+                }
+                break;
+
             default:
                 QLineEdit::keyPressEvent(keyEvent);
                 break;
         }
     }
+
+private:
+    QList<QString> m_commandHistory;
+    QString m_currentlyEditedCommand;
+    int m_commandHistoryIndex;
 };
 
 class OctaveTerminal : public QMdiSubWindow {