Skip to content

Commit

Permalink
feat: DTabBar add scroll buttons
Browse files Browse the repository at this point in the history
Change-Id: Iae10ed85818f78504e39a2ff2d53588523810852
  • Loading branch information
zccrs authored and deepin-gerrit committed Jan 5, 2018
1 parent e21174a commit 2f44cd2
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 18 deletions.
145 changes: 139 additions & 6 deletions src/widgets/dtabbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ void DMovableTabWidget::paintEvent(QPaintEvent *e)
pa.drawPixmap(0, 0, m_pixmap);
}

class DTabBarAddButton : public QToolButton
{
public:
explicit DTabBarAddButton(QWidget *parent)
: QToolButton(parent) {}

private:
void paintEvent(QPaintEvent *event) override;
};

void DTabBarAddButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)

QStylePainter p(this);
QStyleOptionToolButton opt;
initStyleOption(&opt);
p.drawControl(static_cast<QStyle::ControlElement>(QStyle::CE_CustomBase + 1), opt);
}

class DTabBarPrivate : public QTabBar, public DObjectPrivate
{
Q_OBJECT
Expand All @@ -89,25 +109,48 @@ class DTabBarPrivate : public QTabBar, public DObjectPrivate
explicit DTabBarPrivate(DTabBar* qq)
: QTabBar(qq)
, DObjectPrivate(qq) {
addButton = new DImageButton(qq);
addButton = new DTabBarAddButton(qq);
addButton->setObjectName("AddButton");
addButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

connect(addButton, &DImageButton::clicked,
connect(addButton, &DTabBarAddButton::clicked,
qq, &DTabBar::tabAddRequested);
connect(this, &QTabBar::tabMoved, this, [this] (int from, int to) {
tabMinimumSize.move(from, to);
tabMaximumSize.move(from, to);
});

setAcceptDrops(true);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
setChangeCurrentOnDrag(true);
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding);

QTabBarPrivate *d = reinterpret_cast<QTabBarPrivate *>(qGetPtrHelper(d_ptr));

leftScrollButton = new QToolButton(qq);
rightScrollButton = new QToolButton(qq);

leftScrollButton->setVisible(d->leftB->isVisible());
leftScrollButton->setAutoRepeat(true);
leftScrollButton->setArrowType(Qt::LeftArrow);
leftScrollButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
rightScrollButton->setVisible(d->rightB->isVisible());
rightScrollButton->setAutoRepeat(true);
rightScrollButton->setArrowType(Qt::RightArrow);
rightScrollButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

d->leftB->setFixedSize(0, 0);
d->leftB->installEventFilter(this);
d->rightB->setFixedSize(0, 0);
d->rightB->installEventFilter(this);

connect(leftScrollButton, &QToolButton::clicked, d->leftB, &QToolButton::click);
connect(rightScrollButton, &QToolButton::clicked, d->rightB, &QToolButton::click);

QHBoxLayout *layout = new QHBoxLayout(qq);

layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(leftScrollButton);
layout->addWidget(rightScrollButton);
layout->addWidget(this, Qt::AlignLeft);
layout->addWidget(addButton, Qt::AlignVCenter);
layout->addStretch();
Expand Down Expand Up @@ -150,6 +193,8 @@ class DTabBarPrivate : public QTabBar, public DObjectPrivate
DTabBarPrivate *dpriv;
};

bool eventFilter(QObject *watched, QEvent *event) override;

QSize minimumSizeHint() const override;

void paintEvent(QPaintEvent *e) override;
Expand All @@ -158,13 +203,15 @@ class DTabBarPrivate : public QTabBar, public DObjectPrivate
void dragMoveEvent(QDragMoveEvent *e) override;
void dropEvent(QDropEvent *e) override;
void showEvent(QShowEvent *e) override;
void resizeEvent(QResizeEvent *e) override;

QSize tabSizeHint(int index) const override;
QSize minimumTabSizeHint(int index) const override;
virtual QSize maximumTabSizeHint(int index) const;

void tabInserted(int index) override;
void tabRemoved(int index) override;
void tabLayoutChange() override;

Q_SLOT void startDrag(int tabIndex);

Expand All @@ -174,11 +221,16 @@ class DTabBarPrivate : public QTabBar, public DObjectPrivate
void moveTabFinished(int index);
void layoutWidgets(int start = 0);

void updateTabSize();

QList<QSize> tabMinimumSize;
QList<QSize> tabMaximumSize;
bool visibleAddButton = true;
DImageButton *addButton;
DTabBarAddButton *addButton;
QPointer<QDrag> drag;

QToolButton *leftScrollButton;
QToolButton *rightScrollButton;
};

void DTabBarPrivate::startDrag(int tabIndex)
Expand Down Expand Up @@ -346,6 +398,71 @@ void DTabBarPrivate::layoutWidgets(int start)
}
}

void DTabBarPrivate::updateTabSize()
{
QTabBarPrivate *d = reinterpret_cast<QTabBarPrivate *>(qGetPtrHelper(d_ptr));

bool is_vertical = verticalTabs(d->shape);

for (int i = 0; i < d->tabList.count(); ++i) {
QTabBarPrivate::Tab &tab = d->tabList[i];
const QSize &size_hint = tabSizeHint(i);
const QSize old_size = tab.rect.size();

if (is_vertical) {
tab.rect.setWidth(qMax(size_hint.width(), width()));
} else {
tab.rect.setHeight(qMax(size_hint.height(), height()));
}

if (tab.rect.size() != old_size)
layoutTab(i);
}
}

bool DTabBarPrivate::eventFilter(QObject *watched, QEvent *event)
{
QTabBarPrivate *d = reinterpret_cast<QTabBarPrivate *>(qGetPtrHelper(d_ptr));

if (watched == d->leftB) {
switch (event->type()) {
case QEvent::Show:
leftScrollButton->show();
break;
case QEvent::Hide:
leftScrollButton->hide();
break;
case QEvent::EnabledChange:
leftScrollButton->setEnabled(d->leftB->isEnabled());
break;
case QEvent::UpdateRequest:
leftScrollButton->setArrowType(d->leftB->arrowType());
break;
default:
break;
}
} else if (watched == d->rightB) {
switch (event->type()) {
case QEvent::Show:
rightScrollButton->show();
break;
case QEvent::Hide:
rightScrollButton->hide();
break;
case QEvent::EnabledChange:
rightScrollButton->setEnabled(d->rightB->isEnabled());
break;
case QEvent::UpdateRequest:
rightScrollButton->setArrowType(d->rightB->arrowType());
break;
default:
break;
}
}

return QTabBar::eventFilter(watched, event);
}

QSize DTabBarPrivate::minimumSizeHint() const
{
const QSize &hint = sizeHint();
Expand Down Expand Up @@ -571,8 +688,17 @@ void DTabBarPrivate::showEvent(QShowEvent *e)
QTabBar::showEvent(e);
}

void DTabBarPrivate::resizeEvent(QResizeEvent *e)
{
updateTabSize();

QTabBar::resizeEvent(e);
}

QSize DTabBarPrivate::tabSizeHint(int index) const
{
D_QC(DTabBar);

QSize size = QTabBar::tabSizeHint(index);

if (index >= tabMaximumSize.count())
Expand Down Expand Up @@ -628,11 +754,18 @@ void DTabBarPrivate::tabRemoved(int index)
QTabBar::tabInserted(index);
}

void DTabBarPrivate::tabLayoutChange()
{
updateTabSize();

QTabBar::tabLayoutChange();
}

DTabBar::DTabBar(QWidget *parent)
: QWidget(parent)
, DObject(*new DTabBarPrivate(this))
{
D_THEME_INIT_WIDGET(DTabBar)

}

void DTabBar::setTabMinimumSize(int index, const QSize &size)
Expand Down
5 changes: 0 additions & 5 deletions src/widgets/themes/dark/DTabBar.theme

This file was deleted.

1 change: 0 additions & 1 deletion src/widgets/themes/dui_theme_dark.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
<file>dark/dialogs/DAboutDialog.theme</file>
<file>dark/DAbstractDialog.theme</file>
<file>dark/DTitlebar.theme</file>
<file>dark/DTabBar.theme</file>
<file>dark/images/window/close_disabled.svg</file>
<file>dark/images/window/close_hover.svg</file>
<file>dark/images/window/close_normal.svg</file>
Expand Down
1 change: 0 additions & 1 deletion src/widgets/themes/dui_theme_light.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
<file>light/DTitlebar.theme</file>
<file>light/DBoxWidget.theme</file>
<file>light/DFileChooserEdit.theme</file>
<file>light/DTabBar.theme</file>
</qresource>
<qresource prefix="/images">
<file>light/images/arrow_down_hover.png</file>
Expand Down
5 changes: 0 additions & 5 deletions src/widgets/themes/light/DTabBar.theme

This file was deleted.

0 comments on commit 2f44cd2

Please sign in to comment.