diff options
Diffstat (limited to 'lib/addressbar')
-rw-r--r-- | lib/addressbar/completer.cpp | 14 | ||||
-rw-r--r-- | lib/addressbar/completer.h | 3 | ||||
-rw-r--r-- | lib/addressbar/urllineedit.cpp | 16 |
3 files changed, 20 insertions, 13 deletions
diff --git a/lib/addressbar/completer.cpp b/lib/addressbar/completer.cpp index 6c04775..1c1c728 100644 --- a/lib/addressbar/completer.cpp +++ b/lib/addressbar/completer.cpp @@ -14,6 +14,12 @@ Completer::Completer(QWidget *parent) { setObjectName("Completer"); setWindowFlags(Qt::ToolTip); + setEditTriggers(QAbstractItemView::NoEditTriggers); + + connect(this, &Completer::activated, [=](const QModelIndex &index) { + hide(); + emit completionActivated(index.data().toString()); + }); } bool Completer::updateItems(const QStringList &list) @@ -61,8 +67,14 @@ bool Completer::keyPressed(QKeyEvent *event) hide(); break; - default: + case Qt::Key_Enter: + case Qt::Key_Return: + hide(); + emit completionActivated(currentIndex.data().toString()); break; + + default: + return false; } return true; diff --git a/lib/addressbar/completer.h b/lib/addressbar/completer.h index 03ff317..2d3b21d 100644 --- a/lib/addressbar/completer.h +++ b/lib/addressbar/completer.h @@ -25,6 +25,9 @@ public: bool keyPressed(QKeyEvent *event); +signals: + void completionActivated(const QString &url); + private: QStringListModel *completionModel = nullptr; }; diff --git a/lib/addressbar/urllineedit.cpp b/lib/addressbar/urllineedit.cpp index 21f88eb..33bc9dc 100644 --- a/lib/addressbar/urllineedit.cpp +++ b/lib/addressbar/urllineedit.cpp @@ -22,6 +22,8 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) setPlaceholderText(tr("Enter address")); m_listView->setVisible(false); + connect(m_listView, &Completer::completionActivated, this, &UrlLineEdit::setText); + auto *addressbar = qobject_cast<AddressBar *>(parent); Q_CHECK_PTR(addressbar); @@ -120,18 +122,8 @@ void UrlLineEdit::focusOutEvent(QFocusEvent *event) void UrlLineEdit::keyPressEvent(QKeyEvent *event) { if(m_listView->keyPressed(event)) { - int key = event->key(); - QModelIndex currentIndex = m_listView->currentIndex(); - - if(key == Qt::Key::Key_Enter || key == Qt::Key_Return) { - - if(currentIndex.isValid()) { - setText(currentIndex.data().toString()); - } - m_listView->hide(); - event->accept(); - return; - } + event->accept(); + return; } else if(event->key() == Qt::Key::Key_Escape) { clearFocus(); event->accept(); |