Skip to content

Commit

Permalink
Refs #10348 Implement autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Jeffery committed Oct 22, 2014
1 parent 89f3a41 commit 9a5fba2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Expand Up @@ -23,10 +23,13 @@ namespace MantidQt
HintingLineEdit(QWidget *parent, const std::map<std::string,std::string> &hints);
virtual ~HintingLineEdit();
protected:
virtual void keyPressEvent(QKeyEvent* e);
void insertSuggestion();
void updateMatches();
std::string m_curKey;
std::map<std::string,std::string> m_matches;
std::map<std::string,std::string> m_hints;
bool m_dontComplete;
public slots:
void showHint();
void updateHint(const QString& text);
Expand Down
38 changes: 31 additions & 7 deletions Code/Mantid/MantidQt/MantidWidgets/src/HintingLineEdit.cpp
Expand Up @@ -8,7 +8,7 @@ namespace MantidQt
namespace MantidWidgets
{

HintingLineEdit::HintingLineEdit(QWidget *parent, const std::map<std::string,std::string> &hints) : QLineEdit(parent), m_hints(hints)
HintingLineEdit::HintingLineEdit(QWidget *parent, const std::map<std::string,std::string> &hints) : QLineEdit(parent), m_hints(hints), m_dontComplete(false)
{
connect(this, SIGNAL(textEdited(const QString&)), this, SLOT(updateHint(const QString&)));
}
Expand All @@ -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();
Expand Down Expand Up @@ -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

0 comments on commit 9a5fba2

Please sign in to comment.