diff options
| author | Mike Skec <skec@protonmail.ch> | 2021-01-21 21:17:08 +1100 |
|---|---|---|
| committer | Felix Queißner <felix@ib-queissner.de> | 2021-01-21 13:49:54 +0100 |
| commit | 2091b038ef2a23a85d30d056307b3c405c416256 (patch) | |
| tree | 36520f2e35228fd1acdf469c68a04be5ec93c42c /src/widgets | |
| parent | ef592c41848ded6253502a536abedb298aed1105 (diff) | |
| download | kristall-2091b038ef2a23a85d30d056307b3c405c416256.tar.gz | |
Added '+' button to tab bar
Diffstat (limited to 'src/widgets')
| -rw-r--r-- | src/widgets/browsertabbar.cpp | 44 | ||||
| -rw-r--r-- | src/widgets/browsertabbar.hpp | 14 | ||||
| -rw-r--r-- | src/widgets/browsertabwidget.cpp | 5 | ||||
| -rw-r--r-- | src/widgets/browsertabwidget.hpp | 5 |
4 files changed, 62 insertions, 6 deletions
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 <QMouseEvent> +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(); +} diff --git a/src/widgets/browsertabbar.hpp b/src/widgets/browsertabbar.hpp index f1cd49b..674089b 100644 --- a/src/widgets/browsertabbar.hpp +++ b/src/widgets/browsertabbar.hpp @@ -2,6 +2,7 @@ #define BROWSERTABS_HPP #include <QTabBar> +#include <QPushButton> class BrowserTabBar : public QTabBar { @@ -10,6 +11,19 @@ public: explicit BrowserTabBar(QWidget * parent); void mouseReleaseEvent(QMouseEvent *event) override; + + void resizeEvent(QResizeEvent *event) override; + + void tabLayoutChange() override; + +signals: + void on_newTabClicked(); + +private: + void moveNewTabButton(); + +private: + QPushButton *newTabBtn; }; #endif // BROWSERTABS_HPP diff --git a/src/widgets/browsertabwidget.cpp b/src/widgets/browsertabwidget.cpp index 7419c52..9a6ee94 100644 --- a/src/widgets/browsertabwidget.cpp +++ b/src/widgets/browsertabwidget.cpp @@ -1,8 +1,7 @@ #include "browsertabwidget.hpp" -#include "browsertabbar.hpp" - BrowserTabWidget::BrowserTabWidget(QWidget *parent) : QTabWidget(parent) { - this->setTabBar(new BrowserTabBar(this)); + this->tab_bar = new BrowserTabBar(this); + this->setTabBar(this->tab_bar); } diff --git a/src/widgets/browsertabwidget.hpp b/src/widgets/browsertabwidget.hpp index dd4142c..7ada869 100644 --- a/src/widgets/browsertabwidget.hpp +++ b/src/widgets/browsertabwidget.hpp @@ -2,6 +2,7 @@ #define BROWSERTABWIDGET_HPP #include <QTabWidget> +#include "browsertabbar.hpp" class BrowserTabWidget : public QTabWidget { @@ -9,8 +10,8 @@ class BrowserTabWidget : public QTabWidget public: explicit BrowserTabWidget(QWidget *parent = nullptr); -signals: - +public: + BrowserTabBar *tab_bar; }; #endif // BROWSERTABWIDGET_HPP |
