From 941f46db96b8edea90837f2f052140a435b5520e Mon Sep 17 00:00:00 2001 From: Herzeh Date: Sun, 13 Dec 2015 13:33:03 +0100 Subject: [PATCH] CommandLineEdit : Added ability to autocomplete when pressing TAB. Either an item is selected in the suggestion list and its put in the lineEdit, or a command is in the lineEdit and pressing tab allows to loop through previous suggestions. --- src/gui/Src/Gui/CommandLineEdit.cpp | 57 +++++++++++++++++++++++++++++ src/gui/Src/Gui/CommandLineEdit.h | 2 + 2 files changed, 59 insertions(+) diff --git a/src/gui/Src/Gui/CommandLineEdit.cpp b/src/gui/Src/Gui/CommandLineEdit.cpp index 5a92a1ab..a7795b8c 100644 --- a/src/gui/Src/Gui/CommandLineEdit.cpp +++ b/src/gui/Src/Gui/CommandLineEdit.cpp @@ -16,6 +16,63 @@ CommandLineEdit::CommandLineEdit(QWidget* parent) : HistoryLineEdit(parent) connect(Bridge::getBridge(), SIGNAL(autoCompleteClearAll()), this, SLOT(autoCompleteClearAll())); } +void CommandLineEdit::keyPressEvent(QKeyEvent *event) +{ + if(event->type() == QEvent::KeyPress) + { + QKeyEvent *keyEvent = static_cast(event); + if(keyEvent->key() == Qt::Key_Tab) + { + QStringListModel *strListModel = (QStringListModel*)(mCompleter->model()); + QStringList stringList = strListModel->stringList(); + + if(stringList.size()) + { + QModelIndex currentModelIndex = mCompleter->popup()->currentIndex(); + + // If not item selected, select first one in the list + if(currentModelIndex.row() < 0) + currentModelIndex = mCompleter->currentIndex(); + + + // If popup list is not visible, selected next suggested command + if(!mCompleter->popup()->isVisible()) + { + for(int row=0; row < mCompleter->popup()->model()->rowCount(); row++) + { + QModelIndex modelIndex = mCompleter->popup()->model()->index(row, 0); + + // If the lineedit contains a suggested command, get the next suggested one + if(mCompleter->popup()->model()->data(modelIndex) == this->text()) + { + int nextModelIndexRow = (currentModelIndex.row() + 1) % mCompleter->popup()->model()->rowCount(); + currentModelIndex = mCompleter->popup()->model()->index(nextModelIndexRow, 0); + break; + } + } + } + + mCompleter->popup()->setCurrentIndex(currentModelIndex); + mCompleter->popup()->hide(); + + int currentRow = mCompleter->currentRow(); + mCompleter->setCurrentRow(currentRow); + } + } + else + HistoryLineEdit::keyPressEvent(event); + } + else + HistoryLineEdit::keyPressEvent(event); +} + +// Disables moving to Prev/Next child when pressing tab +bool CommandLineEdit::focusNextPrevChild(bool next) +{ + Q_UNUSED(next); + return false; +} + void CommandLineEdit::autoCompleteAddCmd(const QString cmd) { QStringListModel* model = (QStringListModel*)(mCompleter->model()); diff --git a/src/gui/Src/Gui/CommandLineEdit.h b/src/gui/Src/Gui/CommandLineEdit.h index 41ce53ad..cb0f2c54 100644 --- a/src/gui/Src/Gui/CommandLineEdit.h +++ b/src/gui/Src/Gui/CommandLineEdit.h @@ -9,6 +9,8 @@ class CommandLineEdit : public HistoryLineEdit Q_OBJECT public: explicit CommandLineEdit(QWidget* parent = 0); + void keyPressEvent(QKeyEvent *event); + bool focusNextPrevChild(bool next); public slots: void autoCompleteAddCmd(const QString cmd);