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
This commit is contained in:
luddens 2019-06-06 11:24:22 +02:00 committed by Matthieu Gautier
parent 8b4acab127
commit c05d61c766
4 changed files with 39 additions and 2 deletions

View File

@ -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

View File

@ -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();

View File

@ -4,6 +4,7 @@
#include <QAction>
#include <QTimer>
#include <QWebEnginePage>
#include <QToolButton>
#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)

View File

@ -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<WebView*>(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