Fully fool-proof TabBar::closeTab()

Made `TabBar::closeTab()` responsible for all the checks so that none of
them have to be performed before calling that function.
This commit is contained in:
Veloman Yunkan 2024-03-12 15:50:48 +04:00
parent 94d95b68ca
commit f62fc3b626

View File

@ -39,16 +39,7 @@ TabBar::TabBar(QWidget *parent) :
}); });
connect(app->getAction(KiwixApp::CloseTabAction), &QAction::triggered, connect(app->getAction(KiwixApp::CloseTabAction), &QAction::triggered,
this, [=]() { this, [=]() {
auto index = currentIndex(); this->closeTab(currentIndex());
if (index < 0)
return;
// library tab cannot be closed
QWidget *w = mp_stackedWidget->widget(index);
if (qobject_cast<ContentManagerView*>(w)) {
return;
}
this->closeTab(index);
}); });
connect(app->getAction(KiwixApp::OpenHomePageAction), &QAction::triggered, connect(app->getAction(KiwixApp::OpenHomePageAction), &QAction::triggered,
this, [=]() { this, [=]() {
@ -278,21 +269,16 @@ void TabBar::closeTabsByZimId(const QString &id)
void TabBar::closeTab(int index) void TabBar::closeTab(int index)
{ {
// the last tab is + button, cannot be closed // The first and last tabs (i.e. the library tab and the + (new tab) button)
if (index == this->realTabCount()) // cannot be closed
if (index <= 0 || index >= this->realTabCount())
return; return;
QWidget *view = mp_stackedWidget->widget(index);
// library tab cannot be closed
if (qobject_cast<ContentManagerView*>(view)) {
return;
}
if ( index == currentIndex() ) { if ( index == currentIndex() ) {
setCurrentIndex(index + 1 == realTabCount() ? index - 1 : index + 1); setCurrentIndex(index + 1 == realTabCount() ? index - 1 : index + 1);
} }
QWidget *view = mp_stackedWidget->widget(index);
mp_stackedWidget->removeWidget(view); mp_stackedWidget->removeWidget(view);
view->setParent(nullptr); view->setParent(nullptr);
removeTab(index); removeTab(index);