From c05d61c766e0e624b695657ce90ba559a5c3fc2a Mon Sep 17 00:00:00 2001 From: luddens Date: Thu, 6 Jun 2019 11:24:22 +0200 Subject: [PATCH] add new tab button with basic css add a new tab with a button inside at the start of the app. When this button is pressed, it inserts a new tab just before this tab. To avoid that this tab/button can be selected when a tab is closed, the SelectionBehaviorOnRemove is set to select the left tab if it's the most right before the tab/button and the right if not --- resources/css/style.css | 8 ++++++++ src/kiwixapp.cpp | 1 + src/tabbar.cpp | 29 +++++++++++++++++++++++++++-- src/tabbar.h | 3 +++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/resources/css/style.css b/resources/css/style.css index 9520dc3..25c317f 100644 --- a/resources/css/style.css +++ b/resources/css/style.css @@ -134,6 +134,14 @@ QTabBar::close-button { subcontrol-position: right; } +QTabBar::tab:last { + border: none; +} + +QTabBar::tab:last QToolButton { + font-size: 30px; + margin-bottom: 5px; +} /* ----------------------------------------- TabWidget diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index 1d7c231..1f82a57 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -88,6 +88,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[]) mp_mainWindow = new MainWindow; mp_tabWidget = mp_mainWindow->getTabBar(); mp_tabWidget->setContentManagerView(m_manager.getView()); + mp_tabWidget->setNewTabButton(); mp_mainWindow->getSideContentManager()->setContentManager(&m_manager); setSideBar(CONTENTMANAGER_BAR); postInit(); diff --git a/src/tabbar.cpp b/src/tabbar.cpp index f80b6ee..74bed11 100644 --- a/src/tabbar.cpp +++ b/src/tabbar.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #define QUITIFNULL(VIEW) if (nullptr==(VIEW)) { return; } #define QUITIFNOTCURRENT(VIEW) if((VIEW)!=currentWidget()) {return;} @@ -83,6 +84,18 @@ void TabBar::setContentManagerView(ContentManagerView* view) setTabButton(0, RightSide, nullptr); } +void TabBar::setNewTabButton() +{ + QToolButton *tb = new QToolButton(); + tb->setDefaultAction(KiwixApp::instance()->getAction(KiwixApp::NewTabAction)); + tb->setText("+"); + addTab(""); + setTabEnabled(1, false); + setTabButton(1, QTabBar::LeftSide, tb); + tabButton(1, QTabBar::RightSide)->deleteLater(); + setTabButton(1, QTabBar::RightSide, 0); +} + WebView* TabBar::createNewTab(bool setCurrent) { WebView* webView = new WebView(); @@ -110,8 +123,9 @@ WebView* TabBar::createNewTab(bool setCurrent) emit webActionEnabledChanged(QWebEnginePage::Forward, webView->isWebActionEnabled(QWebEnginePage::Forward)); }); // Ownership of webview is passed to the tabWidget - mp_stackedWidget->addWidget(webView); - auto index = addTab(""); + auto index = count() - 1; + mp_stackedWidget->insertWidget(index, webView); + insertTab(index, ""); if (setCurrent) { setCurrentIndex(index); } @@ -184,6 +198,7 @@ void TabBar::closeTab(int index) { if (index == 0) return; + setSelectionBehaviorOnRemove(index); auto webview = widget(index); mp_stackedWidget->removeWidget(webview); webview->setParent(nullptr); @@ -192,6 +207,16 @@ void TabBar::closeTab(int index) delete webview; } +void TabBar::setSelectionBehaviorOnRemove(int index) +{ + if (index == count() - 2) + { + QTabBar::setSelectionBehaviorOnRemove(QTabBar::SelectLeftTab); + } else { + QTabBar::setSelectionBehaviorOnRemove(QTabBar::SelectRightTab); + } +} + void TabBar::onCurrentChanged(int index) { if (index == -1) diff --git a/src/tabbar.h b/src/tabbar.h index 01ad98f..013c780 100644 --- a/src/tabbar.h +++ b/src/tabbar.h @@ -17,6 +17,7 @@ public: void setStackedWidget(QStackedWidget* widget); void setContentManagerView(ContentManagerView* view); + void setNewTabButton(); WebView* createNewTab(bool setCurrent); WebView* widget(int index) { return (index != 0) ? static_cast(mp_stackedWidget->widget(index)) : nullptr; } WebView* currentWidget() { auto current = mp_stackedWidget->currentWidget(); @@ -47,6 +48,8 @@ private: ContentManagerView* mp_contentManagerView; QStackedWidget* mp_stackedWidget; + void setSelectionBehaviorOnRemove(int index); + }; #endif // TABWIDGET_H