# HG changeset patch # User Jacob Dawid # Date 1302034059 -7200 # Node ID cf95d1db42c6e156875e4180edbe53b7526ff163 # Parent 56a7b194486df6afea2bd7abd0dfa28abd618445 Added history to terminal. diff --git a/gui//octaveterminal.cpp b/gui//octaveterminal.cpp --- 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() { diff --git a/gui//octaveterminal.h b/gui//octaveterminal.h --- 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 m_commandHistory; + QString m_currentlyEditedCommand; + int m_commandHistoryIndex; }; class OctaveTerminal : public QMdiSubWindow {