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/bookmarks/bookmarkswidget.cpp | 5 ++ src/lib/bookmarks/bookmarkswidget.h | 2 + src/lib/navigation/urllineedit.cpp | 92 ++++++----------------------------- src/lib/navigation/urllineedit.h | 16 ++---- src/mainwindow.cpp | 1 + src/mainwindow.h | 1 - 6 files changed, 27 insertions(+), 90 deletions(-) diff --git a/src/lib/bookmarks/bookmarkswidget.cpp b/src/lib/bookmarks/bookmarkswidget.cpp index 10a5424..cb191c7 100644 --- a/src/lib/bookmarks/bookmarkswidget.cpp +++ b/src/lib/bookmarks/bookmarkswidget.cpp @@ -75,6 +75,11 @@ QStringList BookmarksWidget::bookmarksFor(const QString &term) return ret; } +QAbstractItemModel *BookmarksWidget::model() const +{ + return ui->treeWidget->model(); +} + QStringList BookmarksWidget::searchItem(QTreeWidgetItem *item, const QString &term) { if(item->text(1).contains(term)) { diff --git a/src/lib/bookmarks/bookmarkswidget.h b/src/lib/bookmarks/bookmarkswidget.h index 5e5af12..aa188b2 100644 --- a/src/lib/bookmarks/bookmarkswidget.h +++ b/src/lib/bookmarks/bookmarkswidget.h @@ -11,6 +11,7 @@ #include #include "xbel.h" +#include namespace Ui { class BookmarksDialog; @@ -31,6 +32,7 @@ signals: public slots: QStringList bookmarksFor(const QString &term); + QAbstractItemModel *model() const; private slots: void openItem(QTreeWidgetItem *item, int column); 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(); -} diff --git a/src/lib/navigation/urllineedit.h b/src/lib/navigation/urllineedit.h index 73b31a3..9dd73ab 100644 --- a/src/lib/navigation/urllineedit.h +++ b/src/lib/navigation/urllineedit.h @@ -11,21 +11,22 @@ #include #include -#include #include +class QAbstractItemModel; class QMenu; class QLabel; -class MainWindow; class UrlLineEdit : public QLineEdit { Q_OBJECT public: - explicit UrlLineEdit(MainWindow *window, QWidget *parent = nullptr); + explicit UrlLineEdit(QWidget *parent = nullptr); QAction *sslAction(); QAction *pageAction(); + void setCompleterModel(QAbstractItemModel *model); + signals: void addressEntered(const QUrl &url); void searchTermEntered(const QString &term); @@ -36,11 +37,6 @@ public slots: protected: void focusInEvent(QFocusEvent *event); - void resizeEvent(QResizeEvent *event); - void keyPressEvent(QKeyEvent *event); - -private slots: - void showCompleter(const QString &text); private: void setTextFormat(const QTextLayout::FormatRange &format); @@ -48,7 +44,6 @@ private: QTextLayout::FormatRange m_hostFormat; - MainWindow *m_window; QAction *m_sslAction = nullptr; QAction *m_pageAction = nullptr; @@ -56,8 +51,7 @@ private: QMenu *m_sslMenu; QLabel *m_sslLabel; - QMenu *m_menu; - QListWidget *listWidget; + QCompleter *m_completer; }; #endif // URLLINEEDIT_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 2dd7628..21914da 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -232,6 +232,7 @@ void MainWindow::setBookmarksWidget(std::shared_ptr &widget) { Q_ASSERT(widget); m_bookmarksWidget = widget; + m_addressBar->setCompleterModel(m_bookmarksWidget->model()); connect(menuBar->bookmarksAction(), &QAction::triggered, this, [this]() { addTabbedDock(Qt::RightDockWidgetArea, m_bookmarksWidget.get()); }); diff --git a/src/mainwindow.h b/src/mainwindow.h index 6ab7f75..1c568bc 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -38,7 +38,6 @@ class MainWindow : public QMainWindow friend class WebView; friend class SearchForm; - friend class UrlLineEdit; friend class MainWindowMenuBar; -- cgit v1.2.1