diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 874cd76..9b9116f 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -250,15 +250,7 @@ void ContentManager::eraseBookFilesFromComputer(const QString dirPath, const QSt void ContentManager::eraseBook(const QString& id) { auto tabBar = KiwixApp::instance()->getTabWidget(); - int i = 1; - while (i < tabBar->count() - 1) { - WebView* webView = tabBar->widget(i)->getWebView(); - if (webView->zimId() == id) { - tabBar->closeTab(i); - } else { - i++; - } - } + tabBar->closeTabsByZimId(id); kiwix::Book book = mp_library->getBookById(id); QString dirPath = QString::fromStdString(removeLastPathElement(book.getPath())); QString filename = QString::fromStdString(getLastPathElement(book.getPath())) + "*"; diff --git a/src/tabbar.cpp b/src/tabbar.cpp index 8904522..c0d905c 100644 --- a/src/tabbar.cpp +++ b/src/tabbar.cpp @@ -200,6 +200,19 @@ void TabBar::triggerWebPageAction(QWebEnginePage::WebAction action, ZimView *wid widget->getWebView()->setFocus(); } +void TabBar::closeTabsByZimId(const QString &id) +{ + // the last tab is + button, skip it + for (int i = count() - 2 ; i >= 0 ; i--) { + auto *zv = qobject_cast(mp_stackedWidget->widget(i)); + if (!zv) + continue; + if (zv->getWebView()->zimId() == id) { + closeTab(i); + } + } +} + void TabBar::closeTab(int index) { setSelectionBehaviorOnRemove(index); @@ -211,7 +224,7 @@ void TabBar::closeTab(int index) if (index < m_settingsIndex) { m_settingsIndex--; } - auto webview = widget(index); + auto webview = mp_stackedWidget->widget(index); mp_stackedWidget->removeWidget(webview); webview->setParent(nullptr); removeTab(index); @@ -239,7 +252,7 @@ void TabBar::onCurrentChanged(int index) KiwixApp::instance()->setSideBar(KiwixApp::NONE); QTimer::singleShot(0, [=](){emit currentTitleChanged("");}); } else if (index) { - auto view = widget(index)->getWebView(); + auto view = static_cast(mp_stackedWidget->widget(index))->getWebView(); emit webActionEnabledChanged(QWebEnginePage::Back, view->isWebActionEnabled(QWebEnginePage::Back)); emit webActionEnabledChanged(QWebEnginePage::Forward, view->isWebActionEnabled(QWebEnginePage::Forward)); emit libraryPageDisplayed(false); diff --git a/src/tabbar.h b/src/tabbar.h index 6c8efa4..722a8eb 100644 --- a/src/tabbar.h +++ b/src/tabbar.h @@ -23,7 +23,6 @@ public: void setContentManagerView(ContentManagerView* view); void setNewTabButton(); ZimView* createNewTab(bool setCurrent); - ZimView* widget(int index) { return (index != 0) ? static_cast(mp_stackedWidget->widget(index)) : nullptr; } WebView* currentWebView() { auto current = mp_stackedWidget->currentWidget(); if (mp_stackedWidget->currentIndex() == 0 || mp_stackedWidget->currentIndex() == m_settingsIndex) return nullptr; @@ -46,6 +45,7 @@ public: QString currentArticleTitle(); virtual QSize tabSizeHint(int index) const; void openFindInPageBar(); + void closeTabsByZimId(const QString &id); protected: void mousePressEvent(QMouseEvent *event);