From fcda48e14aee987394e1dbfcd68687ef80f6d2ca Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 18 Jun 2018 12:07:34 +0200 Subject: AddressBar: code cleanup AddressBar: match input for protocol or '.' in addition to url validity --- lib/about/aboutdialog.cpp | 3 ++- lib/addressbar/CMakeLists.txt | 5 +--- lib/addressbar/addressbar.cpp | 57 ++++++++++++++++-------------------------- lib/addressbar/addressbar.h | 12 ++++----- lib/addressbar/urllineedit.cpp | 9 +++---- lib/addressbar/urllineedit.h | 5 ++-- src/mainwindow/mainwindow.cpp | 43 ++++++++++++++++++++++++++++--- src/mainwindow/mainwindow.h | 5 +++- 8 files changed, 79 insertions(+), 60 deletions(-) diff --git a/lib/about/aboutdialog.cpp b/lib/about/aboutdialog.cpp index e716217..9b4d3bc 100644 --- a/lib/about/aboutdialog.cpp +++ b/lib/about/aboutdialog.cpp @@ -53,7 +53,8 @@ AboutDialog::AboutDialog(QWidget *parent) aboutLabel->setWordWrap(true); aboutLabel->setText(tr("

smolbote %1

" "

yet another no-frills browser

" - "

This program is free software, and is built upon other free software: Qt " QT_VERSION_STR " and Boost " BOOST_LIB_VERSION "

") + "

This program is free software, see License for more information.

" + "

This program uses free software: Qt " QT_VERSION_STR " and Boost " BOOST_LIB_VERSION "

") .arg(qApp->applicationVersion())); ui->toolBox->addItem(aboutLabel, tr("About")); diff --git a/lib/addressbar/CMakeLists.txt b/lib/addressbar/CMakeLists.txt index 1122bc3..63cf2bc 100644 --- a/lib/addressbar/CMakeLists.txt +++ b/lib/addressbar/CMakeLists.txt @@ -15,7 +15,4 @@ add_library(addressbar urllineedit.h ) -#target_include_directories(addressbar -# PRIVATE ${Boost_INCLUDE_DIRS}) - -target_link_libraries(addressbar Qt5::Widgets Qt5::WebEngineWidgets) +target_link_libraries(addressbar Qt5::Widgets) diff --git a/lib/addressbar/addressbar.cpp b/lib/addressbar/addressbar.cpp index 2409cb9..7d17462 100644 --- a/lib/addressbar/addressbar.cpp +++ b/lib/addressbar/addressbar.cpp @@ -8,7 +8,6 @@ #include "addressbar.h" #include "urllineedit.h" -#include #include #include #include @@ -35,6 +34,19 @@ AddressBar::AddressBar(const QHash &config, QWidget *parent) emit complete(text, callback); }); + connect(urlBar, &UrlLineEdit::returnPressed, [=]() { + const QUrl url = QUrl::fromUserInput(urlBar->text()); + + // check if url contains \w+:// (matches protocol://) or contains a '.' (matches site.domain) + // this is because single words are valid URLs for QUrl (searchterm becomes http://searchterm) + // check for protocol://site because \. wouldn't match it (localhost is a search term; http://localhost is an address) + if(urlBar->text().contains(QRegularExpression("\\w+://|\\.")) && url.isValid()) { + emit load(url); + } else { + emit search(urlBar->text()); + } + }); + progressBar = new QProgressBar(this); progressBar->setObjectName("LoadingBar"); progressBar->setMaximumHeight(5); @@ -44,46 +56,19 @@ AddressBar::AddressBar(const QHash &config, QWidget *parent) AddressBar::~AddressBar() { - disconnect(urlChangedConnection); - disconnect(loadUrlConnection); - disconnect(progressBarConnection); - disconnect(focusConnection); + disconnect(this); } -void AddressBar::setView(QWebEngineView *view) +void AddressBar::setUrl(const QUrl &url) { - disconnect(urlChangedConnection); - disconnect(loadUrlConnection); - disconnect(progressBarConnection); - disconnect(focusConnection); - - progressBar->setValue(100); - - if(view == nullptr) { + if(url.isEmpty()) urlBar->clear(); - urlBar->pageMenu = nullptr; - urlBar->toolsMenu = nullptr; - return; - } - - urlBar->setUrl(view->url()); - urlChangedConnection = connect(view, &QWebEngineView::urlChanged, urlBar, &UrlLineEdit::setUrl); - loadUrlConnection = connect(urlBar, &UrlLineEdit::returnPressed, [=]() { - const QUrl url = QUrl::fromUserInput(urlBar->text()); - if(url.isValid()) { - view->load(url); - } else { - emit search(urlBar->text()); - } - view->setFocus(); - }); - - progressBar->setValue(100); - progressBarConnection = connect(view, &QWebEngineView::loadProgress, progressBar, &QProgressBar::setValue); + else + urlBar->setUrl(url); +} - focusConnection = connect(urlBar, &UrlLineEdit::focusView, view, [view]() { - view->setFocus(); - }); +void AddressBar::setProgress(int value) { + progressBar->setValue(std::min(value, 100)); } void AddressBar::setPageMenu(QMenu *menu) diff --git a/lib/addressbar/addressbar.h b/lib/addressbar/addressbar.h index d035d8d..e87f72e 100644 --- a/lib/addressbar/addressbar.h +++ b/lib/addressbar/addressbar.h @@ -12,7 +12,6 @@ #include #include -class QWebEngineView; class UrlLineEdit; class QProgressBar; class QMenu; @@ -26,20 +25,21 @@ public: signals: void complete(const QString &term, std::function callback); + void search(const QString &term); + void load(const QUrl &url); + void giveFocus(); public slots: - void setView(QWebEngineView *view); + void setUrl(const QUrl &url); + void setProgress(int value); + void setPageMenu(QMenu *menu); void setToolsMenu(QMenu *menu); private: UrlLineEdit *urlBar; QProgressBar *progressBar; - - QMetaObject::Connection urlChangedConnection, loadUrlConnection; - QMetaObject::Connection progressBarConnection; - QMetaObject::Connection focusConnection; }; #endif // SMOLBOTE_ADDRESSBAR_H diff --git a/lib/addressbar/urllineedit.cpp b/lib/addressbar/urllineedit.cpp index 33bc9dc..29a14cd 100644 --- a/lib/addressbar/urllineedit.cpp +++ b/lib/addressbar/urllineedit.cpp @@ -24,7 +24,7 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) m_listView->setVisible(false); connect(m_listView, &Completer::completionActivated, this, &UrlLineEdit::setText); - auto *addressbar = qobject_cast(parent); + addressbar = qobject_cast(parent); Q_CHECK_PTR(addressbar); auto *copyAction = new QAction(tr("Copy URL"), this); @@ -41,9 +41,8 @@ UrlLineEdit::UrlLineEdit(QWidget *parent) actions.append(pasteAction); auto *loadAction = new QAction(tr("Paste and load"), this); - connect(loadAction, &QAction::triggered, this, [this]() { - this->setText(qApp->clipboard()->text()); - emit returnPressed(); + connect(loadAction, &QAction::triggered, this, [=]() { + emit addressbar->load(QUrl::fromUserInput(qApp->clipboard()->text())); }); actions.append(loadAction); @@ -115,7 +114,7 @@ void UrlLineEdit::focusOutEvent(QFocusEvent *event) if(url.isValid()) setUrl(url); - emit focusView(); + emit addressbar->giveFocus(); QLineEdit::focusOutEvent(event); } diff --git a/lib/addressbar/urllineedit.h b/lib/addressbar/urllineedit.h index 2198a0e..56914c4 100644 --- a/lib/addressbar/urllineedit.h +++ b/lib/addressbar/urllineedit.h @@ -10,6 +10,7 @@ #define SMOLBOTE_URLLINEEDIT_H #include "completer.h" +#include "addressbar.h" #include #include #include @@ -27,9 +28,6 @@ public slots: void updateCompleter(const QStringList &l); -signals: - void focusView(); - public: QAction *menuAction = nullptr; // pageMenu action: zoom, print @@ -52,6 +50,7 @@ private: // completer Completer *m_listView; + AddressBar *addressbar; }; #endif // SMOLBOTE_URLLINEEDIT_H diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index b2c79cc..48c9e9e 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -93,6 +93,28 @@ MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) } }); + // address bar + connect(addressBar, &AddressBar::search, this, [this](const QString &term) { + if(this->currentView) { + currentView->search(term); + currentView->setFocus(); + } + }); + connect(addressBar, &AddressBar::load, this, [this](const QUrl &url) { + if(this->currentView) { + currentView->load(url); + currentView->setFocus(); + } + }); + + connect(addressBar, &AddressBar::giveFocus, this, [this]() { + if(this->currentView) { + currentView->setFocus(); + } + }); + + + // search box auto *searchShortcut = new QShortcut(QKeySequence(config->value("mainwindow.shortcuts.search").value().c_str()), this); connect(searchShortcut, &QShortcut::activated, this, [=]() { /* QTBUG-18665 @@ -112,9 +134,10 @@ MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) MainWindow::~MainWindow() { disconnect(viewChangedConnection); - disconnect(searchConnection); disconnect(searchBoxConnection); disconnect(statusBarConnection); + + disconnect(addressBar); } void MainWindow::createMenuBar() @@ -231,16 +254,28 @@ SubWindow *MainWindow::createSubWindow(const QString &url) void MainWindow::setView(WebView *view) { - disconnect(searchConnection); + if(currentView) { + // disconnect old view + disconnect(currentView, 0, addressBar, 0); + } + currentView = view; - addressBar->setView(view); if(view) { addressBar->setPageMenu(view->pageMenu()); addressBar->setToolsMenu(view->toolsMenu()); - searchConnection = connect(addressBar, &AddressBar::search, view, &WebView::search); + + connect(view, &WebView::urlChanged, addressBar, &AddressBar::setUrl); + addressBar->setUrl(view->url()); + + connect(view, &WebView::loadProgress, addressBar, &AddressBar::setProgress); + addressBar->setProgress(100); + } else { addressBar->setPageMenu(nullptr); addressBar->setToolsMenu(nullptr); + + addressBar->setUrl(QUrl()); + addressBar->setProgress(100); } navigationToolBar->connectWebView(view); diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index af713fc..30d2178 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -56,15 +56,18 @@ protected: private: QAction *subWindowAction = nullptr; QMenu *toolsMenu = nullptr; + NavigationBar *navigationToolBar = nullptr; AddressBar *addressBar = nullptr; SearchForm *searchBox = nullptr; + QMdiArea *mdiArea; + WebView *currentView = nullptr; std::shared_ptr m_config; QMetaObject::Connection viewChangedConnection; - QMetaObject::Connection searchConnection, searchBoxConnection; + QMetaObject::Connection searchBoxConnection; QMetaObject::Connection statusBarConnection; }; -- cgit v1.2.1