From 9a5fba2b9344741755abc7a8f9b8b78b639a0647 Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Wed, 22 Oct 2014 11:39:40 +0100 Subject: [PATCH] Refs #10348 Implement autocompletion --- .../MantidQtMantidWidgets/HintingLineEdit.h | 3 ++ .../MantidWidgets/src/HintingLineEdit.cpp | 38 +++++++++++++++---- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/HintingLineEdit.h b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/HintingLineEdit.h index 13df47d8c975..a43c78436592 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/HintingLineEdit.h +++ b/Code/Mantid/MantidQt/MantidWidgets/inc/MantidQtMantidWidgets/HintingLineEdit.h @@ -23,10 +23,13 @@ namespace MantidQt HintingLineEdit(QWidget *parent, const std::map &hints); virtual ~HintingLineEdit(); protected: + virtual void keyPressEvent(QKeyEvent* e); + void insertSuggestion(); void updateMatches(); std::string m_curKey; std::map m_matches; std::map m_hints; + bool m_dontComplete; public slots: void showHint(); void updateHint(const QString& text); diff --git a/Code/Mantid/MantidQt/MantidWidgets/src/HintingLineEdit.cpp b/Code/Mantid/MantidQt/MantidWidgets/src/HintingLineEdit.cpp index dadec397eec1..d955b432fef8 100644 --- a/Code/Mantid/MantidQt/MantidWidgets/src/HintingLineEdit.cpp +++ b/Code/Mantid/MantidQt/MantidWidgets/src/HintingLineEdit.cpp @@ -8,7 +8,7 @@ namespace MantidQt namespace MantidWidgets { - HintingLineEdit::HintingLineEdit(QWidget *parent, const std::map &hints) : QLineEdit(parent), m_hints(hints) + HintingLineEdit::HintingLineEdit(QWidget *parent, const std::map &hints) : QLineEdit(parent), m_hints(hints), m_dontComplete(false) { connect(this, SIGNAL(textEdited(const QString&)), this, SLOT(updateHint(const QString&))); } @@ -17,6 +17,12 @@ namespace MantidQt { } + void HintingLineEdit::keyPressEvent(QKeyEvent* e) + { + m_dontComplete = (e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete || e->key() == Qt::Key_Space); + QLineEdit::keyPressEvent(e); + } + void HintingLineEdit::updateMatches() { m_matches.clear(); @@ -53,18 +59,36 @@ namespace MantidQt showHint(); } + void HintingLineEdit::insertSuggestion() + { + if(m_curKey.length() < 1 || m_matches.size() < 1 || m_dontComplete) + return; + + const std::string key = m_matches.begin()->first; + QString line = text(); + const int curPos = cursorPosition(); + + //Don't perform insertions mid-word + if(curPos + 1 < line.size() && line[curPos+1].isLetterOrNumber()) + return; + + line = line.left(curPos) + QString::fromStdString(key).mid((int)m_curKey.size()) + line.mid(curPos); + + setText(line); + setSelection(curPos, (int)key.size()); + } + void HintingLineEdit::showHint() { updateMatches(); - //If we're typing the key, auto complete and show matching keys - - //If we're typing the value, show the detailed description for this key - QString matchList; + QString hintList; for(auto mIt = m_matches.begin(); mIt != m_matches.end(); ++mIt) - matchList += QString::fromStdString(mIt->first) + " : " + QString::fromStdString(mIt->second) + "\n"; + hintList += QString::fromStdString(mIt->first) + " : " + QString::fromStdString(mIt->second) + "\n"; + + QToolTip::showText(mapToGlobal(QPoint(0, 5)), hintList.trimmed()); - QToolTip::showText(mapToGlobal(QPoint(0, 5)), matchList.trimmed()); + insertSuggestion(); } } //namespace MantidWidgets } //namepsace MantidQt