From 62a6f626620b46a8649f2ebbaa4748afeb558e48 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 29 Dec 2017 17:33:47 +0100 Subject: Using QCompleter to provide address bar completions - known issue: it doesn't search trees well --- src/lib/navigation/urllineedit.cpp | 92 ++++++-------------------------------- 1 file changed, 14 insertions(+), 78 deletions(-) (limited to 'src/lib/navigation/urllineedit.cpp') diff --git a/src/lib/navigation/urllineedit.cpp b/src/lib/navigation/urllineedit.cpp index 300a1e1..c9f11c6 100644 --- a/src/lib/navigation/urllineedit.cpp +++ b/src/lib/navigation/urllineedit.cpp @@ -18,15 +18,13 @@ // ssl menu #include -#include "mainwindow.h" #include "lib/bookmarks/bookmarkswidget.h" -UrlLineEdit::UrlLineEdit(MainWindow *window, QWidget *parent) : +#include + +UrlLineEdit::UrlLineEdit(QWidget *parent) : QLineEdit(parent) { - Q_CHECK_PTR(window); - m_window = window; - setPlaceholderText(tr("Enter address")); // ssl menu @@ -44,8 +42,6 @@ UrlLineEdit::UrlLineEdit(MainWindow *window, QWidget *parent) : m_sslMenu->exec(this->mapToGlobal(QPoint(0, height()))); }); - QAction *completerAction = addAction(style()->standardIcon(QStyle::SP_TitleBarMinButton), QLineEdit::TrailingPosition); - m_pageAction = addAction(style()->standardIcon(QStyle::SP_FileIcon), QLineEdit::TrailingPosition); m_pageAction->setShortcut(QKeySequence("F10")); m_pageAction->setToolTip(tr("Page Actions")); @@ -60,40 +56,13 @@ UrlLineEdit::UrlLineEdit(MainWindow *window, QWidget *parent) : hostnameFormat.setFontWeight(QFont::Bold); m_hostFormat.format = hostnameFormat; - m_menu = new QMenu(this); - m_menu->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool); - - listWidget = new QListWidget(); -// listWidget->addItem("start.duckduckgo.com"); -// listWidget->addItem("neueland.iserlohn-fortress.net"); - - connect(listWidget, &QListWidget::itemSelectionChanged, this, [&]() { - setText(listWidget->currentItem()->text()); - }); - connect(listWidget, &QListWidget::itemActivated, this, [&](QListWidgetItem *item) { - setText(item->text());; - m_menu->hide(); - }); - - QWidgetAction *listAction = new QWidgetAction(m_menu); - listAction->setDefaultWidget(listWidget); - m_menu->addAction(listAction); - QAction *closeAction = m_menu->addAction("Close"); - connect(closeAction, SIGNAL(triggered()), m_menu, SLOT(hide())); - - connect(completerAction, &QAction::triggered, this, [this]() { - this->showCompleter(this->text()); - }); - // connect signals - connect(this, SIGNAL(textEdited(QString)), this, SLOT(showCompleter(QString))); connect(this, &QLineEdit::returnPressed, [this]() { if(this->text().startsWith('#')) { emit searchTermEntered(this->text().mid(1)); } else { emit addressEntered(QUrl::fromUserInput(this->text())); } - m_menu->hide(); this->clearFocus(); }); @@ -111,6 +80,17 @@ QAction *UrlLineEdit::pageAction() return m_pageAction; } +void UrlLineEdit::setCompleterModel(QAbstractItemModel *model) +{ + Q_CHECK_PTR(model); + QCompleter *m_completer = new QCompleter(this); + m_completer->setCompletionMode(QCompleter::PopupCompletion); + m_completer->setModel(model); + m_completer->setCompletionColumn(1); + + this->setCompleter(m_completer); +} + void UrlLineEdit::setUrl(const QUrl &url) { QString urlText = url.toString(); @@ -143,38 +123,6 @@ void UrlLineEdit::focusInEvent(QFocusEvent *event) //QTimer::singleShot(0, this, SLOT(selectAll())); } -void UrlLineEdit::resizeEvent(QResizeEvent *event) -{ - QLineEdit::resizeEvent(event); - m_menu->setFixedWidth(width()); -} - -void UrlLineEdit::keyPressEvent(QKeyEvent *event) -{ - if(event->key() == Qt::Key_Down) { - if(!listWidget->isVisible()) { - showCompleter(text()); - return; - } else { - // listWidget is visible - int newIndex = listWidget->currentRow()+1; - if(newIndex < listWidget->count()) { - listWidget->setCurrentRow(newIndex, QItemSelectionModel::SelectCurrent); - } - return; - } - } else if(event->key() == Qt::Key_Up) { - if(listWidget->isVisible()) { - int newIndex = listWidget->currentRow()-1; - if(newIndex >= 0) { - listWidget->setCurrentRow(newIndex, QItemSelectionModel::SelectCurrent); - } - return; - } - } - QLineEdit::keyPressEvent(event); -} - // formatting taken from: https://forum.qt.io/topic/60962/setting-qlineedit-text-bold void UrlLineEdit::setTextFormat(const QTextLayout::FormatRange &format) { @@ -189,15 +137,3 @@ void UrlLineEdit::clearTextFormat() { setTextFormat(QTextLayout::FormatRange()); } - -// Completer - -void UrlLineEdit::showCompleter(const QString &text) -{ - m_menu->move(mapToGlobal(QPoint(0, height()))); - listWidget->clear(); - for(const QString &url : m_window->m_bookmarksWidget->bookmarksFor(text)) { - listWidget->addItem(url); - } - m_menu->exec(); -} -- cgit v1.2.1