From 89506207907fd5a2081596eac2f6bf4c3a77cf2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Sat, 26 May 2018 15:28:45 +0200 Subject: [PATCH] Mavlink Console: add command history --- src/AnalyzeView/MavlinkConsoleController.cc | 53 +++++++++++++++++++++ src/AnalyzeView/MavlinkConsoleController.h | 22 +++++++-- src/AnalyzeView/MavlinkConsolePage.qml | 9 ++++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/AnalyzeView/MavlinkConsoleController.cc b/src/AnalyzeView/MavlinkConsoleController.cc index 6bc300437..12d8c2822 100644 --- a/src/AnalyzeView/MavlinkConsoleController.cc +++ b/src/AnalyzeView/MavlinkConsoleController.cc @@ -33,12 +33,25 @@ MavlinkConsoleController::~MavlinkConsoleController() void MavlinkConsoleController::sendCommand(QString command) { + _history.append(command); command.append("\n"); _sendSerialData(qPrintable(command)); _cursor_home_pos = -1; _cursor = rowCount(); } +QString +MavlinkConsoleController::historyUp(const QString& current) +{ + return _history.up(current); +} + +QString +MavlinkConsoleController::historyDown(const QString& current) +{ + return _history.down(current); +} + void MavlinkConsoleController::_setActiveVehicle(Vehicle* vehicle) { @@ -192,3 +205,43 @@ MavlinkConsoleController::writeLine(int line, const QByteArray &text) auto idx = index(line); setData(idx, data(idx, Qt::DisplayRole).toString() + text); } + +void MavlinkConsoleController::CommandHistory::append(const QString& command) +{ + if (command.length() > 0) { + + // do not append duplicates + if (_history.length() == 0 || _history.last() != command) { + + if (_history.length() >= maxHistoryLength) { + _history.removeFirst(); + } + _history.append(command); + } + } + _index = _history.length(); +} + +QString MavlinkConsoleController::CommandHistory::up(const QString& current) +{ + if (_index <= 0) + return current; + + --_index; + if (_index < _history.length()) { + return _history[_index]; + } + return ""; +} + +QString MavlinkConsoleController::CommandHistory::down(const QString& current) +{ + if (_index >= _history.length()) + return current; + + ++_index; + if (_index < _history.length()) { + return _history[_index]; + } + return ""; +} diff --git a/src/AnalyzeView/MavlinkConsoleController.h b/src/AnalyzeView/MavlinkConsoleController.h index 950b3786b..46cc5d42a 100644 --- a/src/AnalyzeView/MavlinkConsoleController.h +++ b/src/AnalyzeView/MavlinkConsoleController.h @@ -27,13 +27,12 @@ class MavlinkConsoleController : public QStringListModel public: MavlinkConsoleController(); - ~MavlinkConsoleController(); + virtual ~MavlinkConsoleController(); -public slots: - void sendCommand(QString command); + Q_INVOKABLE void sendCommand(QString command); -signals: - void cursorChanged(int); + Q_INVOKABLE QString historyUp(const QString& current); + Q_INVOKABLE QString historyDown(const QString& current); private slots: void _setActiveVehicle (Vehicle* vehicle); @@ -44,10 +43,23 @@ private: void _sendSerialData(QByteArray, bool close = false); void writeLine(int line, const QByteArray &text); + class CommandHistory + { + public: + void append(const QString& command); + QString up(const QString& current); + QString down(const QString& current); + private: + static constexpr int maxHistoryLength = 100; + QList _history; + int _index = 0; + }; + int _cursor_home_pos; int _cursor; QByteArray _incoming_buffer; Vehicle* _vehicle; QList _uas_connections; + CommandHistory _history; }; diff --git a/src/AnalyzeView/MavlinkConsolePage.qml b/src/AnalyzeView/MavlinkConsolePage.qml index 254b8f445..c2dd92a56 100644 --- a/src/AnalyzeView/MavlinkConsolePage.qml +++ b/src/AnalyzeView/MavlinkConsolePage.qml @@ -96,6 +96,15 @@ AnalyzePage { conController.sendCommand(text) text = "" } + Keys.onPressed: { + if (event.key == Qt.Key_Up) { + text = conController.historyUp(text); + event.accepted = true; + } else if (event.key == Qt.Key_Down) { + text = conController.historyDown(text); + event.accepted = true; + } + } } QGCButton { -- 2.22.0