From 4d958ae602315fd9d78af35c40b27d9c6ae6e656 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 7 Jan 2018 20:02:33 +0100 Subject: Searching through BookmarkModel elements --- lib/navigation/urllineedit.cpp | 79 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 4 deletions(-) (limited to 'lib/navigation/urllineedit.cpp') diff --git a/lib/navigation/urllineedit.cpp b/lib/navigation/urllineedit.cpp index 095431b..85f443d 100644 --- a/lib/navigation/urllineedit.cpp +++ b/lib/navigation/urllineedit.cpp @@ -15,11 +15,19 @@ // ssl menu #include +#include +#include + UrlLineEdit::UrlLineEdit(QWidget *parent) : - QLineEdit(parent) + QLineEdit(parent), + m_listView(new QListView(this)) { setPlaceholderText(tr("Enter address")); + m_listView->setWindowFlags(Qt::ToolTip); + m_listView->setVisible(false); + connect(this, &UrlLineEdit::textEdited, this, &UrlLineEdit::updateCompleter); + // ssl menu m_sslMenu = new QMenu(this); m_sslLabel = new QLabel(m_sslMenu); @@ -70,9 +78,7 @@ QAction *UrlLineEdit::pageAction() void UrlLineEdit::setCompleterModel(QAbstractItemModel *model) { Q_CHECK_PTR(model); - m_completer = new UrlCompleter(model, this); - m_completer->setCompletionColumn(1); - this->setCompleter(m_completer); + m_bookmarksModel = model; } void UrlLineEdit::setUrl(const QUrl &url) @@ -95,6 +101,31 @@ void UrlLineEdit::showSslError(const QString &message) m_sslAction->trigger(); } +void UrlLineEdit::updateCompleter(const QString &text) +{ + if(m_bookmarksModel == nullptr) { + return; + } + + const QModelIndexList res = m_bookmarksModel->match(QModelIndex(), Qt::EditRole, text, 7); + QStringList l; + for(const QModelIndex &idx : res) { + l.append(idx.data(Qt::EditRole).toString()); + } + + if(!text.isEmpty()) { + l.append(text); + } + QStringListModel *m = new QStringListModel(l, this); + + m_listView->setModel(m); + + // positioning + m_listView->setFixedWidth(width()); + m_listView->move(mapToGlobal(QPoint(0, height()))); + m_listView->show(); +} + void UrlLineEdit::focusInEvent(QFocusEvent *event) { clearTextFormat(); @@ -107,6 +138,46 @@ void UrlLineEdit::focusInEvent(QFocusEvent *event) //QTimer::singleShot(0, this, SLOT(selectAll())); } +void UrlLineEdit::keyPressEvent(QKeyEvent *event) +{ + if(!m_listView->isHidden()) { + int key = event->key(); + int count = m_listView->model()->rowCount(); + QModelIndex currentIndex = m_listView->currentIndex(); + + switch (key) { + case Qt::Key_Down: + if(currentIndex.row() + 1 >= count) { + m_listView->setCurrentIndex(m_listView->model()->index(0, 0)); + } else { + m_listView->setCurrentIndex(m_listView->model()->index(currentIndex.row() + 1, 0)); + } + break; + case Qt::Key_Up: + if(currentIndex.row() == 0) { + m_listView->setCurrentIndex(m_listView->model()->index(count - 1, 0)); + } else { + m_listView->setCurrentIndex(m_listView->model()->index(currentIndex.row() - 1, 0)); + } + break; + + case Qt::Key_Enter: + case Qt::Key_Return: + if(currentIndex.isValid()) { + setText(currentIndex.data().toString()); + } + m_listView->hide(); + return; + case Qt::Key_Escape: + m_listView->hide(); + break; + default: + break; + } + } + QLineEdit::keyPressEvent(event); +} + // formatting taken from: https://forum.qt.io/topic/60962/setting-qlineedit-text-bold void UrlLineEdit::setTextFormat(const QTextLayout::FormatRange &format) { -- cgit v1.2.1