Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ TextEditorAutoCompleteComponent::TextEditorAutoCompleteComponent(TextEditorContr
this->setAttribute(Qt::WA_ShowWithoutActivating);

menuRef_ = new QMenu(this);
menuRef_->setFocusPolicy(Qt::NoFocus);
menuRef_->setAttribute(Qt::WA_ShowWithoutActivating);
menuRef_->setAccessibleName("Autocomplete");

listWidgetRef_ = new QListWidget(menuRef_);
listWidgetRef_->setFocusPolicy(Qt::NoFocus);
listWidgetRef_->setAttribute(Qt::WA_ShowWithoutActivating);

editorComponentRef_->installEventFilter(this);
listWidgetRef_->installEventFilter(this);

menuRef_->installEventFilter(this);
Expand Down Expand Up @@ -327,6 +332,16 @@ void TextEditorAutoCompleteComponent::hideEvent(QHideEvent* event)
}


void TextEditorAutoCompleteComponent::sendKeyEventTo(QWidget* target, QKeyEvent* sourceEvent)
{
QKeyEvent event(sourceEvent->type(), sourceEvent->key(), sourceEvent->modifiers(), sourceEvent->text(), sourceEvent->isAutoRepeat(), sourceEvent->count());

eventBeingFiltered_ = true;
QApplication::sendEvent(target, &event);
eventBeingFiltered_ = false;
}


/// we need to intercept keypresses if the widget is visible
bool TextEditorAutoCompleteComponent::eventFilter(QObject *obj, QEvent *event)
{
Expand All @@ -336,14 +351,22 @@ bool TextEditorAutoCompleteComponent::eventFilter(QObject *obj, QEvent *event)
return QObject::eventFilter(obj, event);
}

if(obj == listWidgetRef_ && event->type() == QEvent::KeyPress) {
if (eventBeingFiltered_) {
return QObject::eventFilter(obj, event);
}

if ((obj == editorComponentRef_ || obj == listWidgetRef_ || obj == menuRef_) && event->type() == QEvent::KeyPress && menuRef_->isVisible()) {
QKeyEvent* key = static_cast<QKeyEvent*>(event);
const bool editorHasEvent = obj == editorComponentRef_;

// text keys are allowed
if (!key->text().isEmpty()) {
QChar nextChar = key->text().at(0);
if (nextChar.isLetterOrNumber()) {
QApplication::sendEvent(editorComponentRef_, event);
if (editorHasEvent) {
return false;
}
sendKeyEventTo(editorComponentRef_, key);
return true;
}
}
Expand All @@ -360,7 +383,10 @@ bool TextEditorAutoCompleteComponent::eventFilter(QObject *obj, QEvent *event)
case Qt::Key_Tab:
if (listWidgetRef_->currentItem() && currentWord_ == listWidgetRef_->currentItem()->text()) { // sends normal enter/return/tab if you've typed a full word
menuRef_->close();
QApplication::sendEvent(editorComponentRef_, event);
if (editorHasEvent) {
return false;
}
sendKeyEventTo(editorComponentRef_, key);
return true;
} else if (listWidgetRef_->currentItem()) {
insertCurrentSelectedListItem();
Expand All @@ -371,24 +397,37 @@ bool TextEditorAutoCompleteComponent::eventFilter(QObject *obj, QEvent *event)
break;

case Qt::Key_Backspace:
QApplication::sendEvent(editorComponentRef_, event);
if (editorHasEvent) {
return false;
}
sendKeyEventTo(editorComponentRef_, key);
return true;

case Qt::Key_Shift: //ignore shift, don't hide
QApplication::sendEvent(editorComponentRef_, event);
if (editorHasEvent) {
return false;
}
sendKeyEventTo(editorComponentRef_, key);
return true;

// forward special keys to list
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_PageDown:
case Qt::Key_PageUp:
if (editorHasEvent || obj == menuRef_) {
sendKeyEventTo(listWidgetRef_, key);
return true;
}
return false;
}

// default operation is to hide and continue the event
menuRef_->close();
QApplication::sendEvent(editorComponentRef_, event);
if (editorHasEvent) {
return false;
}
sendKeyEventTo(editorComponentRef_, key);
return true;

}
Expand Down Expand Up @@ -428,7 +467,6 @@ void TextEditorAutoCompleteComponent::updateList()
// fills the autocomplete list with the curent word
if (fillAutoCompleteList(doc, range, currentWord_)) {
menuRef_->popup(menuRef_->pos());
listWidgetRef_->setFocus();

// position the widget
showInfoTip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

class QListWidget;
class QListWidgetItem;
class QKeyEvent;

namespace edbee {

Expand Down Expand Up @@ -76,6 +77,7 @@ class EDBEE_EXPORT TextEditorAutoCompleteComponent : public QWidget
//void moveEvent(QMoveEvent *event);

void insertCurrentSelectedListItem();
void sendKeyEventTo(QWidget* target, QKeyEvent* sourceEvent);
signals:

public slots:
Expand Down
2 changes: 2 additions & 0 deletions edbee-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SET(SOURCES
edbee/util/rangesetlineiteratortest.cpp
edbee/models/dynamicvariablestest.cpp
edbee/util/rangelineiteratortest.cpp
edbee/views/texteditorautocompletecomponenttest.cpp
edbee/views/textthememanagertest.cpp
)

Expand Down Expand Up @@ -67,6 +68,7 @@ SET(HEADERS
edbee/util/rangesetlineiteratortest.h
edbee/models/dynamicvariablestest.h
edbee/util/rangelineiteratortest.h
edbee/views/texteditorautocompletecomponenttest.h
edbee/views/textthememanagertest.h
)

Expand Down
2 changes: 2 additions & 0 deletions edbee-test/edbee-test.pro
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ SOURCES += \
edbee/util/rangesetlineiteratortest.cpp \
edbee/models/dynamicvariablestest.cpp \
edbee/util/rangelineiteratortest.cpp \
edbee/views/texteditorautocompletecomponenttest.cpp \
edbee/views/textthememanagertest.cpp

HEADERS += \
Expand Down Expand Up @@ -79,6 +80,7 @@ HEADERS += \
edbee/util/rangesetlineiteratortest.h \
edbee/models/dynamicvariablestest.h \
edbee/util/rangelineiteratortest.h \
edbee/views/texteditorautocompletecomponenttest.h \
edbee/views/textthememanagertest.h

##OTHER_FILES += ../edbee-data/config/*
Expand Down
Loading