From c081b3a66526eafafa0ed62e0e8c60b604d2b984 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Thu, 13 Dec 2018 18:06:11 +0100 Subject: [PATCH] Disable back/forward buttons when there is no history Fix #82 --- src/kiwixapp.cpp | 2 ++ src/mainwindow.cpp | 5 +++++ src/mainwindow.h | 2 ++ src/tabbar.cpp | 14 ++++++++++++-- src/topwidget.cpp | 2 ++ src/topwidget.h | 2 +- 6 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index 49e5a49..30af0b2 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -364,6 +364,8 @@ void KiwixApp::createAction() } void KiwixApp::postInit() { + connect(mp_tabWidget, &TabBar::webActionEnabledChanged, + mp_mainWindow->getTopWidget(), &TopWidget::handleWebActionEnabledChanged); connect(mp_tabWidget, &TabBar::currentTitleChanged, this, [=](const QString& title) { emit currentTitleChanged(title); }); emit(m_library.booksChanged()); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 93639a3..e26a622 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -45,6 +45,11 @@ TabBar* MainWindow::getTabBar() return mp_ui->tabBar; } +TopWidget *MainWindow::getTopWidget() +{ + return mp_ui->mainToolBar; +} + QStackedWidget *MainWindow::getSideDockWidget() { return mp_ui->sideBar; diff --git a/src/mainwindow.h b/src/mainwindow.h index 391861d..87b7c04 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -5,6 +5,7 @@ #include #include "webview.h" #include "tabbar.h" +#include "topwidget.h" #include "tocsidebar.h" #include "about.h" #include "contentmanagerside.h" @@ -22,6 +23,7 @@ public: ~MainWindow(); TabBar* getTabBar(); + TopWidget* getTopWidget(); QStackedWidget* getSideDockWidget(); ContentManagerSide* getSideContentManager(); diff --git a/src/tabbar.cpp b/src/tabbar.cpp index 0ba18ad..788cc7c 100644 --- a/src/tabbar.cpp +++ b/src/tabbar.cpp @@ -3,6 +3,7 @@ #include "kiwixapp.h" #include #include +#include #define QUITIFNULL(VIEW) if (nullptr==(VIEW)) { return; } #define QUITIFNOTCURRENT(VIEW) if((VIEW)!=currentWidget()) {return;} @@ -95,8 +96,17 @@ WebView* TabBar::createNewTab(bool setCurrent) [=](const QString& zimId) { QUITIFNOTCURRENT(webView); emit currentZimIdChanged(zimId); - } - ); + }); + connect(webView->page()->action(QWebEnginePage::Back), &QAction::changed, + [=]() { + QUITIFNOTCURRENT(webView); + emit webActionEnabledChanged(QWebEnginePage::Back, webView->isWebActionEnabled(QWebEnginePage::Back)); + }); + connect(webView->page()->action(QWebEnginePage::Forward), &QAction::changed, + [=]() { + QUITIFNOTCURRENT(webView); + emit webActionEnabledChanged(QWebEnginePage::Forward, webView->isWebActionEnabled(QWebEnginePage::Forward)); + }); // Ownership of webview is passed to the tabWidget mp_stackedWidget->addWidget(webView); auto index = addTab(""); diff --git a/src/topwidget.cpp b/src/topwidget.cpp index 1a44644..c2f264e 100644 --- a/src/topwidget.cpp +++ b/src/topwidget.cpp @@ -14,6 +14,7 @@ TopWidget::TopWidget(QWidget *parent) : mp_historyBackAction->setIcon(QIcon(":/icons/back.svg")); mp_historyBackAction->setText(tr("back")); mp_historyBackAction->setToolTip(tr("back")); + mp_historyBackAction->setEnabled(false); connect(mp_historyBackAction, &QAction::triggered, [](){ KiwixApp::instance()->getTabWidget()->triggerWebPageAction(QWebEnginePage::Back); }); @@ -22,6 +23,7 @@ TopWidget::TopWidget(QWidget *parent) : mp_historyForwardAction->setIcon(QIcon(":/icons/forward.svg")); mp_historyForwardAction->setText(tr("forward")); mp_historyForwardAction->setToolTip(tr("forward")); + mp_historyForwardAction->setEnabled(false); connect(mp_historyForwardAction, &QAction::triggered, [](){ KiwixApp::instance()->getTabWidget()->triggerWebPageAction(QWebEnginePage::Forward); }); diff --git a/src/topwidget.h b/src/topwidget.h index e492193..4b95058 100644 --- a/src/topwidget.h +++ b/src/topwidget.h @@ -25,7 +25,7 @@ private: QPoint m_cursorPos; ulong m_timestamp; -private slots: +public slots: void handleWebActionEnabledChanged(QWebEnginePage::WebAction action, bool enabled); };