From 49ee5ed6e80b8f06337f92d14e2cab1c1512c1e3 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 19 Jan 2018 02:10:31 +0100 Subject: Refactoring MainWindow - Added NavigationBar object that manages the navigation buttons - Removed NavigationButton class that it obsoleted --- src/mainwindow/widgets/navigationbar.cpp | 115 +++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/mainwindow/widgets/navigationbar.cpp (limited to 'src/mainwindow/widgets/navigationbar.cpp') 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 +#include +#include +#include +#include + +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 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 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)); +} -- cgit v1.2.1