void TabCompleter::complete() { if (!enabled) { buildCompletionList(); enabled = true; } if (nextCompletion != completionMap.end()) { // clear previous completion auto cur = msgEdit->textCursor(); cur.setPosition(cur.selectionEnd()); msgEdit->setTextCursor(cur); for (int i = 0; i < lastCompletionLength; ++i) msgEdit->textCursor().deletePreviousChar(); // insert completion msgEdit->insertPlainText(*nextCompletion); // remember charcount to delete next time and advance to next completion lastCompletionLength = nextCompletion->length(); ++nextCompletion; // we're completing the first word of the line if (msgEdit->textCursor().position() == lastCompletionLength) { msgEdit->insertPlainText(nickSuffix); lastCompletionLength += nickSuffix.length(); } } else { // we're at the end of the list -> start over again if (!completionMap.isEmpty()) { nextCompletion = completionMap.begin(); complete(); } } }
void SyntaxLineEdit::completionKeyPressEvent(QKeyEvent *event) { // Forward to the completer if needed... if (completer_ && completer_->popup()->isVisible()) { switch (event->key()) { case Qt::Key_Enter: case Qt::Key_Return: case Qt::Key_Tab: focusNextChild(); break; case Qt::Key_Escape: case Qt::Key_Backtab: event->ignore(); return; default: break; } } // ...otherwise process the key ourselves. SyntaxLineEdit::keyPressEvent(event); if (!completer_ || !completion_model_) return; // Do nothing on bare shift. if ((event->modifiers() & Qt::ShiftModifier) && event->text().isEmpty()) return; if (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)) { completer_->popup()->hide(); return; } QPoint token_coords(getTokenUnderCursor()); QString token_word = text().mid(token_coords.x(), token_coords.y()); buildCompletionList(token_word); if (completion_model_->stringList().length() < 1) { completer_->popup()->hide(); return; } QRect cr = cursorRect(); cr.setWidth(completer_->popup()->sizeHintForColumn(0) + completer_->popup()->verticalScrollBar()->sizeHint().width()); completer_->complete(cr); }