parent
edf9f5242a
commit
6ffb747421
@ -107,7 +107,7 @@ FilteredTextEdit::showResults(const QVector<SearchResult> &results)
|
|||||||
{
|
{
|
||||||
QPoint pos;
|
QPoint pos;
|
||||||
|
|
||||||
if (atTriggerPosition_ != -1) {
|
if (isAnchorValid()) {
|
||||||
auto cursor = textCursor();
|
auto cursor = textCursor();
|
||||||
cursor.setPosition(atTriggerPosition_);
|
cursor.setPosition(atTriggerPosition_);
|
||||||
pos = viewport()->mapToGlobal(cursorRect(cursor).topLeft());
|
pos = viewport()->mapToGlobal(cursorRect(cursor).topLeft());
|
||||||
@ -134,7 +134,7 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// calculate the new query
|
// calculate the new query
|
||||||
if (textCursor().position() < atTriggerPosition_ || atTriggerPosition_ == -1) {
|
if (textCursor().position() < atTriggerPosition_ || !isAnchorValid()) {
|
||||||
resetAnchor();
|
resetAnchor();
|
||||||
closeSuggestions();
|
closeSuggestions();
|
||||||
}
|
}
|
||||||
@ -165,9 +165,31 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
|
|||||||
switch (event->key()) {
|
switch (event->key()) {
|
||||||
case Qt::Key_At:
|
case Qt::Key_At:
|
||||||
atTriggerPosition_ = textCursor().position();
|
atTriggerPosition_ = textCursor().position();
|
||||||
|
anchorType_ = AnchorType::Sigil;
|
||||||
|
|
||||||
QTextEdit::keyPressEvent(event);
|
QTextEdit::keyPressEvent(event);
|
||||||
break;
|
break;
|
||||||
|
case Qt::Key_Tab: {
|
||||||
|
auto cursor = textCursor();
|
||||||
|
const int initialPos = cursor.position();
|
||||||
|
|
||||||
|
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
|
||||||
|
auto word = cursor.selectedText();
|
||||||
|
|
||||||
|
const int startOfWord = cursor.position();
|
||||||
|
|
||||||
|
// There is a word to complete.
|
||||||
|
if (initialPos != startOfWord) {
|
||||||
|
atTriggerPosition_ = startOfWord;
|
||||||
|
anchorType_ = AnchorType::Tab;
|
||||||
|
|
||||||
|
emit showSuggestions(word);
|
||||||
|
} else {
|
||||||
|
QTextEdit::keyPressEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
case Qt::Key_Return:
|
case Qt::Key_Return:
|
||||||
case Qt::Key_Enter:
|
case Qt::Key_Enter:
|
||||||
if (!(event->modifiers() & Qt::ShiftModifier)) {
|
if (!(event->modifiers() & Qt::ShiftModifier)) {
|
||||||
@ -213,26 +235,27 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event)
|
|||||||
default:
|
default:
|
||||||
QTextEdit::keyPressEvent(event);
|
QTextEdit::keyPressEvent(event);
|
||||||
|
|
||||||
// Check if the current word should be autocompleted.
|
if (isModifier)
|
||||||
auto cursor = textCursor();
|
return;
|
||||||
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
|
|
||||||
auto word = cursor.selectedText();
|
|
||||||
|
|
||||||
if (cursor.position() == 0) {
|
if (textCursor().position() == 0) {
|
||||||
resetAnchor();
|
resetAnchor();
|
||||||
closeSuggestions();
|
closeSuggestions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cursor.position() == atTriggerPosition_ + 1) {
|
// Check if the current word should be autocompleted.
|
||||||
const auto q = query();
|
auto cursor = textCursor();
|
||||||
|
cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
|
||||||
|
auto word = cursor.selectedText();
|
||||||
|
|
||||||
if (q.isEmpty()) {
|
if (hasAnchor(cursor.position(), anchorType_) && isAnchorValid()) {
|
||||||
|
if (word.isEmpty()) {
|
||||||
closeSuggestions();
|
closeSuggestions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
emit showSuggestions(query());
|
emit showSuggestions(word);
|
||||||
} else {
|
} else {
|
||||||
resetAnchor();
|
resetAnchor();
|
||||||
closeSuggestions();
|
closeSuggestions();
|
||||||
|
@ -94,8 +94,23 @@ private:
|
|||||||
|
|
||||||
SuggestionsPopup popup_;
|
SuggestionsPopup popup_;
|
||||||
|
|
||||||
|
enum class AnchorType
|
||||||
|
{
|
||||||
|
Tab = 0,
|
||||||
|
Sigil = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
AnchorType anchorType_ = AnchorType::Sigil;
|
||||||
|
|
||||||
|
int anchorWidth(AnchorType anchor) { return static_cast<int>(anchor); }
|
||||||
|
|
||||||
void closeSuggestions() { popup_.hide(); }
|
void closeSuggestions() { popup_.hide(); }
|
||||||
void resetAnchor() { atTriggerPosition_ = -1; }
|
void resetAnchor() { atTriggerPosition_ = -1; }
|
||||||
|
bool isAnchorValid() { return atTriggerPosition_ != -1; }
|
||||||
|
bool hasAnchor(int pos, AnchorType anchor)
|
||||||
|
{
|
||||||
|
return pos == atTriggerPosition_ + anchorWidth(anchor);
|
||||||
|
}
|
||||||
|
|
||||||
QString query()
|
QString query()
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user