From f875ad5313b86275c3fd48884063aefb8849235d Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 2 Feb 2017 14:13:14 +0100 Subject: Added loading bar to status bar Hovered links now display in status bar --- src/mainwindow.cpp | 23 +++++++++++++++++---- src/mainwindow.h | 4 +++- src/smolbote.qbs | 4 ++++ src/webengine/webview.cpp | 39 +++++++++++++++++++++++++++++++++++ src/webengine/webview.h | 40 ++++++++++++++++++++++++++++++++++++ src/widgets/loadingbar.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++ src/widgets/loadingbar.h | 41 ++++++++++++++++++++++++++++++++++++ src/widgets/urllineedit.cpp | 6 ++++++ src/widgets/webviewtabbar.cpp | 8 ++++---- src/widgets/webviewtabbar.h | 10 ++++----- util/pre-commit.py | 4 ++-- 11 files changed, 211 insertions(+), 16 deletions(-) create mode 100644 src/webengine/webview.cpp create mode 100644 src/webengine/webview.h create mode 100644 src/widgets/loadingbar.cpp create mode 100644 src/widgets/loadingbar.h diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index cc7003d..4cd8975 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -30,6 +30,7 @@ #include #include #include +#include MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : QMainWindow(parent), @@ -39,7 +40,8 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : navigationToolBar(new QToolBar(tr("Navigation"), this)), tabToolBar(new QToolBar(tr("Tab bar"), this)), tabBar(new WebViewTabBar(this)), - urlLineEdit(new UrlLineEdit(navigationToolBar)) + urlLineEdit(new UrlLineEdit(navigationToolBar)), + progressBar(new LoadingBar(this)) { browserInstance = instance; Settings settings; @@ -82,7 +84,9 @@ MainWindow::MainWindow(Browser *instance, QUrl defaultUrl, QWidget *parent) : connect(urlLineEdit, SIGNAL(returnPressed()), this, SLOT(handleUrlChanged())); tabToolBar->addWidget(tabBar); - connect(tabBar, SIGNAL(currentTabChanged(QWebEngineView*)), this, SLOT(handleTabChanged(QWebEngineView*))); + connect(tabBar, SIGNAL(currentTabChanged(WebView*)), this, SLOT(handleTabChanged(WebView*))); + + ui->statusBar->addPermanentWidget(progressBar); if(!defaultUrl.isEmpty()) { createNewTab(defaultUrl); @@ -160,16 +164,27 @@ void MainWindow::handleNewWindow(const QUrl &url) browserInstance->addWindow(new MainWindow(browserInstance, url)); } -void MainWindow::handleTabChanged(QWebEngineView *view) +void MainWindow::handleTabChanged(WebView *view) { + // clear the parent of the central widget so it doesn't get deleted centralWidget()->setParent(0); + + // disconnect signals disconnect(centralWidget()); + + // set new central widget setCentralWidget(view); + + // connect signals connect(view, SIGNAL(urlChanged(QUrl)), urlLineEdit, SLOT(setUrl(QUrl))); connect(view, SIGNAL(titleChanged(QString)), this, SLOT(handleTitleUpdated(QString))); + connect(view, SIGNAL(linkHovered(QString)), ui->statusBar, SLOT(showMessage(QString))); + + progressBar->connectWebView(view); + + // update UI urlLineEdit->setUrl(view->url()); this->handleTitleUpdated(view->title()); - centralWidget()->setFocus(); } diff --git a/src/mainwindow.h b/src/mainwindow.h index a9446be..e217c1f 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -31,6 +31,7 @@ #include "forms/downloaddialog.h" #include "webengine/urlinterceptor.h" #include "forms/blockerdialog.h" +#include "widgets/loadingbar.h" namespace Ui { class MainWindow; @@ -59,7 +60,7 @@ private slots: void execProfileEditor(); void handleNewWindow(const QUrl &url = QUrl("")); - void handleTabChanged(QWebEngineView *view); + void handleTabChanged(WebView *view); void handleUrlChanged(); void handleTitleUpdated(const QString &title); @@ -75,6 +76,7 @@ private: QToolBar *navigationToolBar, *tabToolBar; WebViewTabBar *tabBar; UrlLineEdit *urlLineEdit; + LoadingBar *progressBar; }; #endif // MAINWINDOW_H diff --git a/src/smolbote.qbs b/src/smolbote.qbs index c8ecb24..e3b9d44 100644 --- a/src/smolbote.qbs +++ b/src/smolbote.qbs @@ -57,6 +57,10 @@ Project { "webengine/urlinterceptor.h", "webengine/webengineprofile.cpp", "webengine/webengineprofile.h", + "webengine/webview.cpp", + "webengine/webview.h", + "widgets/loadingbar.cpp", + "widgets/loadingbar.h", "widgets/urllineedit.cpp", "widgets/urllineedit.h", "widgets/webviewtabbar.cpp", diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp new file mode 100644 index 0000000..71708b7 --- /dev/null +++ b/src/webengine/webview.cpp @@ -0,0 +1,39 @@ +/** LICENSE ******************************************************************** + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#include "webview.h" +#include + +WebView::WebView(QWidget *parent) : + QWebEngineView(parent) +{ +} + +void WebView::setPage(QWebEnginePage *page) +{ + disconnect(this->page(), SIGNAL(linkHovered(QString)), this, SLOT(handleLinkHovered(QString))); + connect(page, SIGNAL(linkHovered(QString)), this, SLOT(handleLinkHovered(QString))); + QWebEngineView::setPage(page); +} + +void WebView::handleLinkHovered(const QString &url) +{ + emit linkHovered(url); +} diff --git a/src/webengine/webview.h b/src/webengine/webview.h new file mode 100644 index 0000000..c833067 --- /dev/null +++ b/src/webengine/webview.h @@ -0,0 +1,40 @@ +/** LICENSE ******************************************************************** + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#ifndef WEBVIEW_H +#define WEBVIEW_H + +#include + +class WebView : public QWebEngineView +{ + Q_OBJECT +public: + explicit WebView(QWidget *parent = 0); + void setPage(QWebEnginePage *page); + +signals: + void linkHovered(const QString &url); + +private slots: + void handleLinkHovered(const QString &url); +}; + +#endif // WEBVIEW_H diff --git a/src/widgets/loadingbar.cpp b/src/widgets/loadingbar.cpp new file mode 100644 index 0000000..31208b8 --- /dev/null +++ b/src/widgets/loadingbar.cpp @@ -0,0 +1,48 @@ +/** LICENSE ******************************************************************** + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#include "loadingbar.h" +#include + +LoadingBar::LoadingBar(QWidget *parent) : + QProgressBar(parent) +{ + setMaximum(100); +} + +void LoadingBar::connectWebView(QWebEngineView *view) +{ + disconnect(this); + + connect(view, SIGNAL(loadStarted()), this, SLOT(loadStarted())); + connect(view, SIGNAL(loadProgress(int)), this, SLOT(setValue(int))); + connect(view, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished())); +} + +void LoadingBar::loadStarted() +{ + show(); + setValue(0); +} + +void LoadingBar::loadFinished() +{ + hide(); +} diff --git a/src/widgets/loadingbar.h b/src/widgets/loadingbar.h new file mode 100644 index 0000000..938549f --- /dev/null +++ b/src/widgets/loadingbar.h @@ -0,0 +1,41 @@ +/** LICENSE ******************************************************************** + ** + ** smolbote: yet another qute browser + ** Copyright (C) 2017 Xian Nox + ** + ** This program is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** This program is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with this program. If not, see . + ** + ******************************************************************************/ + +#ifndef LOADINGBAR_H +#define LOADINGBAR_H + +#include + +class QWebEngineView; +class LoadingBar : public QProgressBar +{ + Q_OBJECT +public: + explicit LoadingBar(QWidget *parent = 0); + void connectWebView(QWebEngineView *view); + +signals: + +public slots: + void loadStarted(); + void loadFinished(); +}; + +#endif // LOADINGBAR_H diff --git a/src/widgets/urllineedit.cpp b/src/widgets/urllineedit.cpp index 5c1f5c4..6f07a4a 100644 --- a/src/widgets/urllineedit.cpp +++ b/src/widgets/urllineedit.cpp @@ -20,6 +20,7 @@ #include "urllineedit.h" #include +#include UrlLineEdit::UrlLineEdit(QWidget *parent) : QLineEdit(parent) @@ -54,6 +55,11 @@ void UrlLineEdit::focusInEvent(QFocusEvent *event) { clearTextFormat(); QLineEdit::focusInEvent(event); + + // select the contents when receiving focus + // http://stackoverflow.com/a/35725950/1054406 + // mousePressEvent triggers right after focusInEvent so text selected in focusInEvent unselects by mousePressEvent + QTimer::singleShot(0, this, SLOT(selectAll())); } void UrlLineEdit::focusOutEvent(QFocusEvent *event) diff --git a/src/widgets/webviewtabbar.cpp b/src/widgets/webviewtabbar.cpp index aef2bd8..c091e56 100644 --- a/src/widgets/webviewtabbar.cpp +++ b/src/widgets/webviewtabbar.cpp @@ -37,7 +37,7 @@ WebViewTabBar::~WebViewTabBar() int WebViewTabBar::addTab(QWebEngineProfile *profile, const QUrl &url) { - QWebEngineView *view = new QWebEngineView(0); + WebView *view = new WebView(0); QWebEnginePage *page = new QWebEnginePage(profile); view->setPage(page); page->load(url); @@ -65,7 +65,7 @@ void WebViewTabBar::setProfile(QWebEngineProfile *profile) } } -QWebEngineView *WebViewTabBar::currentView() +WebView *WebViewTabBar::currentView() { return m_views.at(currentIndex()); } @@ -78,7 +78,7 @@ QSize WebViewTabBar::tabSizeHint(int index) const void WebViewTabBar::handleCurrentChanged(int index) { - emit(currentTabChanged(m_views.at(index))); + emit currentTabChanged(m_views.at(index)); } void WebViewTabBar::handleTabClose(int index) @@ -88,7 +88,7 @@ void WebViewTabBar::handleTabClose(int index) m_views.remove(index); } -void WebViewTabBar::updateTabText(QWebEngineView *view, const QString &text) +void WebViewTabBar::updateTabText(WebView *view, const QString &text) { int index = m_views.indexOf(view); setTabText(index, text); diff --git a/src/widgets/webviewtabbar.h b/src/widgets/webviewtabbar.h index bf7edb2..260100b 100644 --- a/src/widgets/webviewtabbar.h +++ b/src/widgets/webviewtabbar.h @@ -22,7 +22,7 @@ #define WEBVIEWTABBAR_H #include -#include +#include "webengine/webview.h" class WebViewTabBar : public QTabBar { @@ -35,10 +35,10 @@ public: int addTab(QWebEngineProfile *profile, const QUrl &url); void setProfile(QWebEngineProfile *profile); - QWebEngineView *currentView(); + WebView *currentView(); signals: - void currentTabChanged(QWebEngineView *view); + void currentTabChanged(WebView *view); protected: QSize tabSizeHint(int index) const; @@ -47,10 +47,10 @@ private slots: void handleCurrentChanged(int index); void handleTabClose(int index); - void updateTabText(QWebEngineView *view, const QString &text); + void updateTabText(WebView *view, const QString &text); private: - QVector m_views; + QVector m_views; }; #endif // WEBVIEWTABBAR_H diff --git a/util/pre-commit.py b/util/pre-commit.py index 9282854..ca1dc35 100755 --- a/util/pre-commit.py +++ b/util/pre-commit.py @@ -7,7 +7,7 @@ import glob import license # stash unstaged files before running test -os.system("git stash -q --keep-index") +#os.system("git stash -q --keep-index") #print("Running pre-commit hook in {0}".format(os.getcwd())) problems = False @@ -28,7 +28,7 @@ if len(astyle.stdout.splitlines()) > 0: print("Run <> to autofix") # restore stash -os.system("git stash pop -q") +#os.system("git stash pop -q") if problems: sys.exit(-1) -- cgit v1.2.1