aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/widgets
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-01-19 02:10:31 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-01-19 02:10:31 +0100
commit49ee5ed6e80b8f06337f92d14e2cab1c1512c1e3 (patch)
treee35d5472c2a3ed129f5a6a6826f9999b033cc548 /src/mainwindow/widgets
parentConfiguration creates missing items based on defaults (diff)
downloadsmolbote-49ee5ed6e80b8f06337f92d14e2cab1c1512c1e3.tar.xz
Refactoring MainWindow
- Added NavigationBar object that manages the navigation buttons - Removed NavigationButton class that it obsoleted
Diffstat (limited to 'src/mainwindow/widgets')
-rw-r--r--src/mainwindow/widgets/loadingbar.cpp50
-rw-r--r--src/mainwindow/widgets/loadingbar.h30
-rw-r--r--src/mainwindow/widgets/navigationbar.cpp115
-rw-r--r--src/mainwindow/widgets/navigationbar.h55
4 files changed, 250 insertions, 0 deletions
diff --git a/src/mainwindow/widgets/loadingbar.cpp b/src/mainwindow/widgets/loadingbar.cpp
new file mode 100644
index 0000000..99f44d2
--- /dev/null
+++ b/src/mainwindow/widgets/loadingbar.cpp
@@ -0,0 +1,50 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "loadingbar.h"
+#include <QTimer>
+#include "webengine/webview.h"
+
+LoadingBar::LoadingBar(QWidget *parent)
+ : QProgressBar(parent)
+{
+ setMaximum(100);
+}
+
+void LoadingBar::connectWebView(WebView *view)
+{
+ Q_CHECK_PTR(view);
+
+ disconnect(loadStartedConnection);
+ disconnect(loadProgressConnection);
+ disconnect(loadFinishedConnection);
+
+ if(view->isLoaded()) {
+ this->hide();
+ } else {
+ loadStarted();
+ setValue(view->loadProgress());
+ }
+
+ loadStartedConnection = connect(view, &QWebEngineView::loadStarted, this, &LoadingBar::loadStarted);
+ loadProgressConnection = connect(view, &QWebEngineView::loadProgress, this, &QProgressBar::setValue);
+ loadFinishedConnection = connect(view, &QWebEngineView::loadFinished, this, &LoadingBar::loadFinished);
+}
+
+void LoadingBar::loadStarted()
+{
+ resetFormat();
+ show();
+ setValue(0);
+}
+
+void LoadingBar::loadFinished(bool ok)
+{
+ setFormat(QString("%p% %1").arg(ok ? tr("Finished") : tr("Failed")));
+ QTimer::singleShot(2000, this, SLOT(hide()));
+}
diff --git a/src/mainwindow/widgets/loadingbar.h b/src/mainwindow/widgets/loadingbar.h
new file mode 100644
index 0000000..8ae314e
--- /dev/null
+++ b/src/mainwindow/widgets/loadingbar.h
@@ -0,0 +1,30 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#ifndef LOADINGBAR_H
+#define LOADINGBAR_H
+
+#include <QProgressBar>
+
+class WebView;
+class LoadingBar : public QProgressBar
+{
+ Q_OBJECT
+public:
+ explicit LoadingBar(QWidget *parent = nullptr);
+ void connectWebView(WebView *view);
+
+private slots:
+ void loadStarted();
+ void loadFinished(bool ok);
+
+private:
+ QMetaObject::Connection loadStartedConnection, loadProgressConnection, loadFinishedConnection;
+};
+
+#endif // LOADINGBAR_H
diff --git a/src/mainwindow/widgets/navigationbar.cpp b/src/mainwindow/widgets/navigationbar.cpp
new file mode 100644
index 0000000..648bb23
--- /dev/null
+++ b/src/mainwindow/widgets/navigationbar.cpp
@@ -0,0 +1,115 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+#include "navigationbar.h"
+#include "mainwindow/mainwindow.h"
+#include "webengine/webview.h"
+#include <QHBoxLayout>
+#include <QStyle>
+#include <QToolBar>
+#include <QToolButton>
+#include <QWebEngineHistory>
+
+NavigationBar::NavigationBar(MainWindow *parent)
+ : QObject(parent)
+{
+ qStyle = parent->style();
+
+ backButton = new QToolButton(parent);
+ backButton->setIcon(qStyle->standardIcon(QStyle::SP_ArrowBack));
+ connect(backButton, &QToolButton::clicked, this, [this]() {
+ m_view->history()->back();
+ });
+
+ auto *backMenu = new QMenu(backButton);
+ backButton->setMenu(backMenu);
+ connect(backMenu, &QMenu::aboutToShow, this, [this, backMenu]() {
+ backMenu->clear();
+ const QList<QWebEngineHistoryItem> items = m_view->history()->backItems(10);
+ for(const QWebEngineHistoryItem &i : items) {
+ QAction *a = backMenu->addAction(i.title());
+ connect(a, &QAction::triggered, this, [i, this]() {
+ m_view->history()->goToItem(i);
+ });
+ }
+ });
+
+ forwardButton = new QToolButton(parent);
+ forwardButton->setIcon(qStyle->standardIcon(QStyle::SP_ArrowForward));
+ connect(forwardButton, &QToolButton::clicked, this, [this]() {
+ m_view->history()->forward();
+ });
+
+ auto *forwardMenu = new QMenu(forwardButton);
+ forwardButton->setMenu(forwardMenu);
+ connect(forwardMenu, &QMenu::aboutToShow, this, [this, forwardMenu]() {
+ forwardMenu->clear();
+ const QList<QWebEngineHistoryItem> items = m_view->history()->forwardItems(10);
+ for(const QWebEngineHistoryItem &i : items) {
+ QAction *a = forwardMenu->addAction(i.title());
+ connect(a, &QAction::triggered, this, [i, this]() {
+ m_view->history()->goToItem(i);
+ });
+ }
+ });
+
+ stopReloadButton = new QToolButton(parent);
+ stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload));
+ connect(stopReloadButton, &QToolButton::clicked, this, [this]() {
+ if(m_view->isLoaded())
+ m_view->reload();
+ else
+ m_view->stop();
+ });
+
+ homeButton = new QToolButton(parent);
+ homeButton->setIcon(qStyle->standardIcon(QStyle::SP_DirHomeIcon));
+ connect(homeButton, &QToolButton::clicked, this, [this, parent]() {
+ m_view->load(parent->m_profile->homepage());
+ });
+}
+
+void NavigationBar::addWidgetsTo(QToolBar *toolBar)
+{
+ toolBar->addWidget(backButton);
+ toolBar->addWidget(forwardButton);
+ toolBar->addWidget(stopReloadButton);
+ toolBar->addWidget(homeButton);
+}
+
+void NavigationBar::connectWebView(WebView *view)
+{
+ Q_CHECK_PTR(view);
+ m_view = view;
+
+ disconnect(loadStartedConnection);
+ disconnect(loadFinishedConnection);
+
+ if(view->isLoaded()) {
+ update_loadFinished();
+ } else {
+ update_loadStarted();
+ }
+
+ loadStartedConnection = connect(view, &QWebEngineView::loadStarted, this, &NavigationBar::update_loadStarted);
+ loadFinishedConnection = connect(view, &QWebEngineView::loadFinished, this, &NavigationBar::update_loadFinished);
+}
+
+void NavigationBar::update_loadStarted()
+{
+ backButton->setEnabled(m_view->history()->canGoForward());
+ forwardButton->setEnabled(m_view->history()->canGoForward());
+ stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserStop));
+}
+
+void NavigationBar::update_loadFinished()
+{
+ backButton->setEnabled(m_view->history()->canGoBack());
+ forwardButton->setEnabled(m_view->history()->canGoForward());
+ stopReloadButton->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload));
+}
diff --git a/src/mainwindow/widgets/navigationbar.h b/src/mainwindow/widgets/navigationbar.h
new file mode 100644
index 0000000..15a9c7b
--- /dev/null
+++ b/src/mainwindow/widgets/navigationbar.h
@@ -0,0 +1,55 @@
+/*
+ * This file is part of smolbote. It's copyrighted by the contributors recorded
+ * in the version control history of the file, available from its original
+ * location: git://neueland.iserlohn-fortress.net/smolbote.git
+ *
+ * SPDX-License-Identifier: GPL-3.0
+ */
+
+/*
+ * Why is this class a QObject and not a QWidget, and why don't we add the
+ * NavigationBar itself to the toolbar?
+ * That was the original idea: make a NavBar class, friend it from MainWindow
+ * to gain access to the config, but there's a couple of issues:
+ * - adding QToolButtons to a widget that's inside a toolbar makes the buttons
+ * look absolutely hideous: they're smaller and have a border for some reason
+ * - if you stylesheet the border away (which is a pain), they're still not
+ * the same size
+ * And so we ended up having this class only exist to contain all of the logic
+ * for the nav buttons (which cuts down the code in MainWindow).
+ */
+
+#ifndef NAVIGATIONBAR_H
+#define NAVIGATIONBAR_H
+
+#include <QObject>
+
+class QStyle;
+class QToolBar;
+class QToolButton;
+class MainWindow;
+class WebView;
+class NavigationBar : public QObject
+{
+ Q_OBJECT
+public:
+ explicit NavigationBar(MainWindow *parent = nullptr);
+
+ void addWidgetsTo(QToolBar *toolBar);
+ void connectWebView(WebView *view);
+
+private slots:
+ void update_loadStarted();
+ void update_loadFinished();
+
+private:
+ QStyle *qStyle;
+ WebView *m_view;
+ QToolButton *backButton, *forwardButton;
+ QToolButton *stopReloadButton;
+ QToolButton *homeButton;
+
+ QMetaObject::Connection loadStartedConnection, loadFinishedConnection;
+};
+
+#endif //NAVIGATIONBAR_H