You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MoneroDaemonService/src/CommandLineEdit.cpp

61 lines
1.6 KiB

// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2021, The Monero Project.
#include "CommandLineEdit.h"
#include <QKeyEvent>
CommandLineEdit::CommandLineEdit(QWidget *parent) : QLineEdit(parent)
{
connect(this, &QLineEdit::returnPressed, this, &CommandLineEdit::updateHistory);
}
void CommandLineEdit::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Up) {
if (m_historyIndex == m_history.size()) {
m_currentLine = this->text();
}
if (this->decreaseIndex()) {
this->setText(this->getCommand(m_historyIndex));
}
} else if (event->key() == Qt::Key_Down) {
if (this->increaseIndex()) {
this->setText(this->getCommand(m_historyIndex));
}
}
QLineEdit::keyPressEvent(event);
}
bool CommandLineEdit::decreaseIndex() {
if (m_historyIndex > 0 && m_history.size() >= m_historyIndex) {
m_historyIndex -= 1;
return true;
}
return false;
}
bool CommandLineEdit::increaseIndex() {
if (m_historyIndex < m_history.size()) {
m_historyIndex += 1;
return true;
}
return false;
}
QString CommandLineEdit::getCommand(int index) {
if (index < m_history.size()) {
return m_history[index];
} else {
return m_currentLine;
}
}
void CommandLineEdit::updateHistory() {
QString command = this->text();
if (m_history.isEmpty() || m_history.last() != command) {
m_history << command;
m_historyIndex = m_history.size();
}
this->setText("");
emit commandExecuted(command.split(" "));
}