aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/widgets/navigationbar.cpp
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/navigationbar.cpp
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/navigationbar.cpp')
-rw-r--r--src/mainwindow/widgets/navigationbar.cpp115
1 files changed, 115 insertions, 0 deletions
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));
+}