/* * 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: https://neueland.iserlohn-fortress.net/smolbote.hg * * SPDX-License-Identifier: GPL-3.0 */ #include "navigationbar.h" #include "addressbar/urllineedit.h" #include "configuration/configuration.h" #include "webengine/webview.h" #include #include #include #include #include #include #include NavigationBar::NavigationBar(const QHash &conf, QWidget *parent) : QToolBar(parent) { qStyle = parent->style(); // Back button backAction = addAction(qStyle->standardIcon(QStyle::SP_ArrowBack), tr("Back")); backAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.back"))); connect(backAction, &QAction::triggered, this, [this]() { m_view->history()->back(); }); auto *backMenu = new QMenu(this); connect(backMenu, &QMenu::aboutToShow, this, [this, backMenu]() { backMenu->clear(); for(const QWebEngineHistoryItem &item : m_view->history()->backItems(10)) { auto *action = backMenu->addAction(item.title()); connect(action, &QAction::triggered, this, [item, this]() { m_view->history()->goToItem(item); }); } }); backAction->setMenu(backMenu); // Forward button forwardAction = addAction(qStyle->standardIcon(QStyle::SP_ArrowForward), tr("Forward")); forwardAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.forward"))); connect(forwardAction, &QAction::triggered, this, [this]() { m_view->history()->forward(); }); auto *forwardMenu = new QMenu(this); connect(forwardMenu, &QMenu::aboutToShow, this, [this, forwardMenu]() { forwardMenu->clear(); for(const QWebEngineHistoryItem &item : m_view->history()->forwardItems(10)) { auto *action = forwardMenu->addAction(item.title()); connect(action, &QAction::triggered, this, [item, this]() { m_view->history()->goToItem(item); }); } }); forwardAction->setMenu(forwardMenu); // Stop/Refresh button stopReloadAction = addAction(qStyle->standardIcon(QStyle::SP_BrowserReload), tr("Reload")); stopReloadAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.refresh"))); connect(stopReloadAction, &QAction::triggered, this, [this]() { if(m_view->isLoaded()) m_view->reload(); else m_view->stop(); }); // Home button homeAction = addAction(qStyle->standardIcon(QStyle::SP_DirHomeIcon), tr("Home")); homeAction->setShortcut(QKeySequence(conf.value("navigation.shortcuts.home"))); connect(homeAction, &QAction::triggered, this, [this]() { m_view->triggerViewAction(WebView::GoHome); }); } void NavigationBar::connectWebView(WebView *view) { m_view = view; disconnect(loadStartedConnection); disconnect(loadFinishedConnection); if(view == nullptr) { backAction->setEnabled(false); forwardAction->setEnabled(false); stopReloadAction->setEnabled(false); homeAction->setEnabled(false); return; } if(view->isLoaded()) { update_loadFinished(); } else { update_loadStarted(); } loadStartedConnection = connect(view, &QWebEngineView::loadStarted, this, &NavigationBar::update_loadStarted); loadFinishedConnection = connect(view, &WebView::loaded, this, &NavigationBar::update_loadFinished); stopReloadAction->setEnabled(true); homeAction->setEnabled(true); } void NavigationBar::update_loadStarted() { backAction->setEnabled(m_view->history()->canGoForward()); forwardAction->setEnabled(m_view->history()->canGoForward()); stopReloadAction->setIcon(qStyle->standardIcon(QStyle::SP_BrowserStop)); } void NavigationBar::update_loadFinished() { backAction->setEnabled(m_view->history()->canGoBack()); forwardAction->setEnabled(m_view->history()->canGoForward()); stopReloadAction->setIcon(qStyle->standardIcon(QStyle::SP_BrowserReload)); }