diff options
-rw-r--r-- | lib/bookmarks/bookmarkswidget.cpp | 12 | ||||
-rw-r--r-- | lib/bookmarks/bookmarkswidget.h | 4 | ||||
-rw-r--r-- | src/addressbar/completer.cpp | 11 | ||||
-rw-r--r-- | src/addressbar/completer.h | 4 | ||||
-rw-r--r-- | src/addressbar/urllineedit.cpp | 42 | ||||
-rw-r--r-- | src/addressbar/urllineedit.h | 17 | ||||
-rw-r--r-- | src/browser.cpp | 2 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 16 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.h | 7 | ||||
-rw-r--r-- | src/mainwindow/widgets/navigationbar.cpp | 12 | ||||
-rw-r--r-- | src/mainwindow/widgets/navigationbar.h | 2 |
11 files changed, 83 insertions, 46 deletions
diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp index 733c0ef..b5bf471 100644 --- a/lib/bookmarks/bookmarkswidget.cpp +++ b/lib/bookmarks/bookmarkswidget.cpp @@ -128,3 +128,15 @@ void BookmarksWidget::save() bookmarksFile.close(); } } + +QStringList BookmarksWidget::search(const QString& term) const +{ + QStringList ret; + const QList<QTreeWidgetItem *> res = ui->treeWidget->findItems(term, Qt::MatchContains | Qt::MatchRecursive, 1); + for(const QTreeWidgetItem *i : res) { + if(!i->text(1).isEmpty()) + ret.append(i->text(1)); + } + + return ret; +} diff --git a/lib/bookmarks/bookmarkswidget.h b/lib/bookmarks/bookmarkswidget.h index df39e1c..f0cd73d 100644 --- a/lib/bookmarks/bookmarkswidget.h +++ b/lib/bookmarks/bookmarkswidget.h @@ -12,6 +12,7 @@ #include <QShortcut> #include <QWidget> #include <QFile> +#include <functional> namespace Ui { @@ -33,6 +34,9 @@ public: signals: void openUrl(const QUrl &url); +public slots: + QStringList search(const QString &term) const; + private: Ui::BookmarksDialog *ui; QString m_bookmarksPath; diff --git a/src/addressbar/completer.cpp b/src/addressbar/completer.cpp index 5d64dd7..e02610a 100644 --- a/src/addressbar/completer.cpp +++ b/src/addressbar/completer.cpp @@ -15,19 +15,12 @@ Completer::Completer(QWidget *parent) setWindowFlags(Qt::ToolTip); } -bool Completer::updateItems(const QList<QTreeWidgetItem *> &list) +bool Completer::updateItems(QStringList &list) { if(list.isEmpty()) return false; - // list is not empty - QStringList l; - for(QTreeWidgetItem *item : list) { - if(!item->text(1).isEmpty()) - l.append(item->text(1)); - } - - auto *model = new QStringListModel(l, this); + auto *model = new QStringListModel(list, this); setModel(model); delete completionModel; diff --git a/src/addressbar/completer.h b/src/addressbar/completer.h index daef67c..4e338c8 100644 --- a/src/addressbar/completer.h +++ b/src/addressbar/completer.h @@ -21,12 +21,12 @@ class Completer : public QListView public: explicit Completer(QWidget *parent = nullptr); - bool updateItems(const QList<QTreeWidgetItem *> &list); + bool updateItems(QStringList &list); bool keyPressed(QKeyEvent *event); private: - QStringListModel *completionModel; + QStringListModel *completionModel = nullptr; }; #endif //SMOLBOTE_COMPLETER_H diff --git a/src/addressbar/urllineedit.cpp b/src/addressbar/urllineedit.cpp index 8aa03c7..d38acdc 100644 --- a/src/addressbar/urllineedit.cpp +++ b/src/addressbar/urllineedit.cpp @@ -13,16 +13,23 @@ #include <QShortcut> #include <QTimer> #include <QWidgetAction> -#include <bookmarks/bookmarksview.h> +#include <bookmarks/bookmarkswidget.h> UrlLineEdit::UrlLineEdit(const QHash<QString, QString> &config, QWidget *parent) : QLineEdit(parent) + , suggestionTimer(new QTimer(this)) , m_listView(new Completer(this)) { setPlaceholderText(tr("Enter address")); + suggestionTimer->setSingleShot(true); + connect(suggestionTimer, &QTimer::timeout, this, &UrlLineEdit::search); m_listView->setVisible(false); - connect(this, &UrlLineEdit::textEdited, this, &UrlLineEdit::updateCompleter); + connect(this, &QLineEdit::textEdited, this, [this]() { + if(suggestionTimer->isActive()) + suggestionTimer->stop(); + suggestionTimer->start(100); + }); m_pageMenuAction = addAction(style()->standardIcon(QStyle::SP_DriveNetIcon), QLineEdit::LeadingPosition); m_pageMenuAction->setShortcut(QKeySequence("F2")); @@ -61,19 +68,25 @@ UrlLineEdit::UrlLineEdit(const QHash<QString, QString> &config, QWidget *parent) }); } -void UrlLineEdit::setCompleterModel(BookmarksView *model) +UrlLineEdit::~UrlLineEdit() { - Q_CHECK_PTR(model); - m_bookmarksModel = model; + suggestionTimer->stop(); } + void UrlLineEdit::connectWebView(WebView *view) { - Q_CHECK_PTR(view); - m_view = view; - disconnect(urlChangedConnection); + if(view == nullptr) { + clear(); + m_pageMenuAction->setMenu(nullptr); + m_toolsMenuAction->setMenu(nullptr); + return; + } + + m_view = view; + setUrl(view->url()); m_pageMenuAction->setMenu(view->pageMenu()); m_toolsMenuAction->setMenu(view->toolsMenu()); @@ -95,15 +108,16 @@ void UrlLineEdit::setUrl(const QUrl &url) setText(urlText); } -void UrlLineEdit::updateCompleter(const QString &text) +void UrlLineEdit::search() { - if(m_bookmarksModel == nullptr) { - return; - } + Q_ASSERT_X(bookmarks != nullptr, "UrlLineEdit::search", "bookmarks is nullptr"); + updateCompleter(bookmarks->search(text())); +} - const QList<QTreeWidgetItem *> res = m_bookmarksModel->findItems(text, Qt::MatchContains | Qt::MatchRecursive, 1); - if(!m_listView->updateItems(res)) { +void UrlLineEdit::updateCompleter(QStringList l) +{ + if(!m_listView->updateItems(l)) { m_listView->hide(); return; } diff --git a/src/addressbar/urllineedit.h b/src/addressbar/urllineedit.h index 24ad403..1fe3d3c 100644 --- a/src/addressbar/urllineedit.h +++ b/src/addressbar/urllineedit.h @@ -15,27 +15,31 @@ #include <QLineEdit> #include <QTextLayout> -class BookmarksView; class QMenu; -class QLabel; +class QTimer; +class BookmarksWidget; class WebView; class UrlLineEdit : public QLineEdit { Q_OBJECT public: explicit UrlLineEdit(const QHash<QString, QString> &config, QWidget *parent = nullptr); + ~UrlLineEdit() override; - void setCompleterModel(BookmarksView *model); + void setBookmarksManager(BookmarksWidget *w) { + bookmarks = w; + }; signals: - void addressEntered(const QUrl &url); void searchTermEntered(const QString &term); + void complete(const QString &term); public slots: void connectWebView(WebView *view); void setUrl(const QUrl &url); - void updateCompleter(const QString &text); + void search(); + void updateCompleter(QStringList l); protected: void focusInEvent(QFocusEvent *event) override; @@ -46,6 +50,7 @@ private: void setTextFormat(const QTextLayout::FormatRange &format); void clearTextFormat(); + BookmarksWidget *bookmarks = nullptr; WebView *m_view = nullptr; QTextLayout::FormatRange m_hostFormat; @@ -56,7 +61,7 @@ private: QAction *m_toolsMenuAction = nullptr; // completer - BookmarksView *m_bookmarksModel = nullptr; + QTimer *suggestionTimer; Completer *m_listView; QMetaObject::Connection urlChangedConnection; diff --git a/src/browser.cpp b/src/browser.cpp index ab07f2e..659da14 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -16,6 +16,7 @@ #include <downloads/downloadswidget.h> #include <version.h> #include "webengine/urlinterceptor.h" +#include "addressbar/urllineedit.h" Browser::Browser(int &argc, char *argv[]) : SingleApplication(argc, argv) @@ -95,6 +96,7 @@ MainWindow *Browser::createWindow() { // the window will delete itself when it closes, so we don't need to delete it MainWindow *window = new MainWindow(m_config); + window->addressBar->setBookmarksManager(m_bookmarks.get()); window->createSubWindow(WebProfile::defaultProfile()->newtab()); auto *bookmarksAction = new QAction(tr("Bookmarks"), window); diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index c039c8e..6af530a 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -23,6 +23,7 @@ #include <QUrl> #include <about/aboutdialog.h> #include <configuration/configuration.h> +#include "addressbar/urllineedit.h" MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) : QMainWindow(parent) @@ -41,16 +42,28 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) show(); createMenuBar(); + auto *navigationToolBar = new NavigationBar(config->section("navigation"), this); navigationToolBar->setMovable(config->value<bool>("navigation.movable").value()); addToolBar(Qt::TopToolBarArea, navigationToolBar); navigationToolBar->connectWebView(nullptr); + QHash<QString, QString> a; + addressBar = new UrlLineEdit(a, this); + navigationToolBar->addWidget(addressBar); + + auto *focusShortcut = new QShortcut(QKeySequence("F4"), this); + connect(focusShortcut, &QShortcut::activated, this, [this]() { + addressBar->setFocus(); + addressBar->selectAll(); + }); + setCentralWidget(mdiArea); mdiArea->setFocus(); connect(mdiArea, &QMdiArea::subWindowActivated, this, [this, navigationToolBar](QMdiSubWindow *window) { disconnect(titleChangedConnection); + disconnect(addressBarConnection); disconnect(navigationBarConnection); auto *w = qobject_cast<Window *>(window); @@ -60,6 +73,8 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) this->setWindowTitle(title + titleSuffix); }); + addressBar->connectWebView(w->currentView()); + addressBarConnection = connect(w, &Window::currentViewChanged, addressBar, &UrlLineEdit::connectWebView); navigationToolBar->connectWebView(w->currentView()); navigationBarConnection = connect(w, &Window::currentViewChanged, navigationToolBar, &NavigationBar::connectWebView); } @@ -74,6 +89,7 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) MainWindow::~MainWindow() { disconnect(titleChangedConnection); + disconnect(addressBarConnection); disconnect(navigationBarConnection); } diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index 1817fdd..ad879c3 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -12,11 +12,15 @@ #include <QMainWindow> #include <memory> +class Browser; class QMdiArea; class Configuration; class Window; +class UrlLineEdit; class MainWindow : public QMainWindow { + friend class Browser; + Q_OBJECT public: @@ -43,12 +47,13 @@ protected: private: QString titleSuffix; QMenu *toolsMenu = nullptr; + UrlLineEdit *addressBar; QMdiArea *mdiArea; std::shared_ptr<Configuration> m_config; QMetaObject::Connection titleChangedConnection; - QMetaObject::Connection navigationBarConnection; + QMetaObject::Connection addressBarConnection, navigationBarConnection; }; #endif // SMOLBOTE_MAINWINDOW_H diff --git a/src/mainwindow/widgets/navigationbar.cpp b/src/mainwindow/widgets/navigationbar.cpp index e2c714e..98a7932 100644 --- a/src/mainwindow/widgets/navigationbar.cpp +++ b/src/mainwindow/widgets/navigationbar.cpp @@ -77,16 +77,6 @@ NavigationBar::NavigationBar(const QHash<QString, QString> &conf, QWidget *paren connect(homeAction, &QAction::triggered, this, [this]() { m_view->triggerViewAction(WebView::GoHome); }); - - QHash<QString, QString> a; - addressBar = new UrlLineEdit(a, this); - addWidget(addressBar); - - auto *focusShortcut = new QShortcut(QKeySequence("F4"), this); - connect(focusShortcut, &QShortcut::activated, this, [this]() { - addressBar->setFocus(); - addressBar->selectAll(); - }); } void NavigationBar::connectWebView(WebView *view) @@ -115,8 +105,6 @@ void NavigationBar::connectWebView(WebView *view) loadFinishedConnection = connect(view, &WebView::loaded, this, &NavigationBar::update_loadFinished); stopReloadAction->setEnabled(true); homeAction->setEnabled(true); - - addressBar->connectWebView(view); } void NavigationBar::update_loadStarted() diff --git a/src/mainwindow/widgets/navigationbar.h b/src/mainwindow/widgets/navigationbar.h index 1b4cc05..f2c7e61 100644 --- a/src/mainwindow/widgets/navigationbar.h +++ b/src/mainwindow/widgets/navigationbar.h @@ -11,7 +11,6 @@ #include <QToolBar> -class UrlLineEdit; class WebView; class NavigationBar : public QToolBar { @@ -34,7 +33,6 @@ private: QAction *backAction, *forwardAction; QAction *stopReloadAction; QAction *homeAction; - UrlLineEdit *addressBar; QMetaObject::Connection loadStartedConnection, loadFinishedConnection; }; |