From 2091b038ef2a23a85d30d056307b3c405c416256 Mon Sep 17 00:00:00 2001 From: Mike Skec Date: Thu, 21 Jan 2021 21:17:08 +1100 Subject: Added '+' button to tab bar --- src/widgets/browsertabbar.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'src/widgets/browsertabbar.cpp') diff --git a/src/widgets/browsertabbar.cpp b/src/widgets/browsertabbar.cpp index 503157f..97fc1a1 100644 --- a/src/widgets/browsertabbar.cpp +++ b/src/widgets/browsertabbar.cpp @@ -2,10 +2,17 @@ #include +static const int NEWTAB_BTN_SIZE = 22, + NEWTAB_BTN_PAD_X = 4; + + BrowserTabBar::BrowserTabBar(QWidget *parent) : QTabBar(parent) { - + newTabBtn = new QPushButton("+", this); + newTabBtn->setFixedSize(NEWTAB_BTN_SIZE, NEWTAB_BTN_SIZE); + connect(newTabBtn, &QPushButton::clicked, this, &BrowserTabBar::on_newTabClicked); + this->newTabBtn->setVisible(true); } void BrowserTabBar::mouseReleaseEvent(QMouseEvent *event) @@ -16,3 +23,38 @@ void BrowserTabBar::mouseReleaseEvent(QMouseEvent *event) QTabBar::mousePressEvent(event); } } + +void BrowserTabBar::moveNewTabButton() +{ + // Find width of all tabs + int size = 0; + for (int i = 0; i < this->count(); ++i) + size += this->tabRect(i).width(); + + // Set location + int h = this->geometry().top(); + int w = this->width(); + if ((size + NEWTAB_BTN_SIZE + NEWTAB_BTN_PAD_X) > w) + { + this->newTabBtn->setVisible(false); + //this->newTabBtn->move(w - 54, h + 22 / 4); + } + else + { + this->newTabBtn->setVisible(true); + this->newTabBtn->move(size + NEWTAB_BTN_PAD_X, + h + NEWTAB_BTN_SIZE / 4); + } +} + +void BrowserTabBar::resizeEvent(QResizeEvent *event) +{ + QTabBar::resizeEvent(event); + this->moveNewTabButton(); +} + +void BrowserTabBar::tabLayoutChange() +{ + QTabBar::tabLayoutChange(); + this->moveNewTabButton(); +} -- cgit v1.2.3