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.
This commit is contained in:
parent
fd71ff6689
commit
941f46db96
|
|
@ -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<QKeyEvent*>(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());
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue