| 109 | } |
| 110 | |
| 111 | void RDTextEdit::enableCompletion() |
| 112 | { |
| 113 | if(m_Completer) |
| 114 | return; |
| 115 | |
| 116 | m_Completer = new QCompleter(this); |
| 117 | m_Completer->setWidget(this); |
| 118 | m_Completer->setCompletionMode(QCompleter::PopupCompletion); |
| 119 | m_Completer->setCaseSensitivity(Qt::CaseInsensitive); |
| 120 | m_Completer->setWrapAround(false); |
| 121 | |
| 122 | QObject::connect( |
| 123 | m_Completer, OverloadedSlot<const QString &>::of(&QCompleter::activated), |
| 124 | [this](const QString &str) { |
| 125 | QTextCursor cur = textCursor(); |
| 126 | |
| 127 | // if we're in the middle of a word, move to the end of it |
| 128 | QString text = toPlainText(); |
| 129 | if(cur.position() > 0 && |
| 130 | (text[cur.position() - 1].isLetterOrNumber() || |
| 131 | m_WordCharacters.contains(text[cur.position() - 1])) && |
| 132 | (text[cur.position()].isLetterOrNumber() || |
| 133 | m_WordCharacters.contains(text[cur.position()]))) |
| 134 | { |
| 135 | cur.movePosition(QTextCursor::EndOfWord); |
| 136 | } |
| 137 | |
| 138 | // insert what's remaining of the word, after the prefix which is what's already there |
| 139 | cur.insertText(str.right(str.length() - m_Completer->completionPrefix().length())); |
| 140 | setTextCursor(cur); |
| 141 | }); |
| 142 | m_Completer->popup()->installEventFilter(this); |
| 143 | |
| 144 | m_Completer->setCompletionRole(Qt::DisplayRole); |
| 145 | |
| 146 | m_CompletionModel = new QStringListModel(this); |
| 147 | |
| 148 | m_Completer->setModel(m_CompletionModel); |
| 149 | m_Completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel); |
| 150 | } |
| 151 | |
| 152 | bool RDTextEdit::eventFilter(QObject *watched, QEvent *event) |
| 153 | { |
no test coverage detected