refactor tabs to support centered icons

This commit is contained in:
laserpants 2016-06-11 15:01:48 +03:00
parent 0229ce9891
commit 4cfd16485c
5 changed files with 106 additions and 18 deletions

View File

@ -125,24 +125,17 @@ QColor Tabs::textColor() const
}
void Tabs::addTab(const QString &text)
{
createTab(text);
}
void Tabs::addTab(const QString &text, const QIcon &icon)
{
Q_D(Tabs);
Tab *tab = new Tab(text);
tab->setCornerRadius(0);
tab->setRippleStyle(Material::CenteredRipple);
tab->setRole(Material::Primary);
tab->setBackgroundMode(Qt::OpaqueMode);
tab->setPeakOpacity(0.25);
d->tabLayout->addWidget(tab);
if (-1 == d->tab) {
d->tab = 0;
d->inkBar->refreshGeometry();
}
connect(tab, SIGNAL(clicked()), this, SLOT(switchTab()));
Tab *tab = createTab(text);
tab->setIcon(icon);
tab->setIconSize(QSize(24, 24));
}
void Tabs::setRippleStyle(Material::RippleStyle style)
@ -202,3 +195,26 @@ void Tabs::switchTab()
emit currentChanged(d->tab);
}
}
Tab *Tabs::createTab(const QString &text)
{
Q_D(Tabs);
Tab *tab = new Tab(text);
tab->setCornerRadius(0);
tab->setRippleStyle(Material::CenteredRipple);
tab->setRole(Material::Primary);
tab->setBackgroundMode(Qt::OpaqueMode);
tab->setPeakOpacity(0.25);
d->tabLayout->addWidget(tab);
if (-1 == d->tab) {
d->tab = 0;
d->inkBar->refreshGeometry();
}
connect(tab, SIGNAL(clicked()), this, SLOT(switchTab()));
return tab;
}

View File

@ -5,6 +5,7 @@
#include "lib/theme.h"
class TabsPrivate;
class Tab;
class Tabs : public QWidget
{
@ -31,6 +32,7 @@ public:
QColor textColor() const;
void addTab(const QString &text);
void addTab(const QString &text, const QIcon &icon);
void setRippleStyle(Material::RippleStyle style);
@ -52,6 +54,8 @@ private slots:
private:
Q_DISABLE_COPY(Tabs)
Q_DECLARE_PRIVATE(Tabs)
Tab *createTab(const QString &text);
};
#endif // TABS_H

View File

@ -1,8 +1,12 @@
#include "tabs_internal.h"
#include <QLayout>
#include <QPainter>
#include <QStylePainter>
#include <QStyleOptionButton>
#include <QEvent>
#include <QDebug>
#include <QPropertyAnimation>
#include <QtSvg/QSvgRenderer>
#include "tabs.h"
TabsInkBar::TabsInkBar(Tabs *parent)
@ -101,7 +105,65 @@ Tab::~Tab()
{
}
QSize Tab::sizeHint() const
{
if (icon().isNull()) {
return FlatButton::sizeHint();
} else {
return QSize(40, iconSize().height() + 48);
}
}
void Tab::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
//FlatButton::paintEvent(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(backgroundColor());
painter.setOpacity(1);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
painter.drawRect(rect());
QStylePainter style(this);
QStyleOptionButton option;
initStyleOption(&option);
option.features |= QStyleOptionButton::Flat;
option.iconSize = QSize(-1, -1); // Let's not draw the icon right now
style.drawControl(QStyle::CE_PushButtonLabel, option);
if (!icon().isNull()) {
const QSize &size = iconSize();
icon().paint(&painter,
QRect(QPoint((width()-size.width())/2, 0), size),
Qt::AlignCenter,
QIcon::Normal);
}
//
QPen pen;
pen.setColor(Qt::red);
pen.setWidth(2);
painter.setPen(pen);
painter.drawRect(rect());
}
void Tab::init()
{
setMinimumHeight(50);
QFont fnt(font());
fnt.setStyleName("Normal");
setFont(fnt);
}

View File

@ -45,6 +45,11 @@ public:
explicit Tab(QString text, QWidget *parent = 0);
~Tab();
QSize sizeHint() const Q_DECL_OVERRIDE;
protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
Q_DISABLE_COPY(Tab)

View File

@ -1,6 +1,7 @@
#include <QVBoxLayout>
#include <QStackedLayout>
#include <QLabel>
#include <QIcon>
#include "tabsexamples.h"
#include "components/tabs.h"
#include "frame.h"
@ -84,9 +85,9 @@ TabsExamples::TabsExamples(QWidget *parent)
layout->addLayout(stack);
layout->setContentsMargins(0, 0, 0, 0);
tabs->addTab("First");
tabs->addTab("Second");
tabs->addTab("Third");
tabs->addTab("Listen", QIcon("../qt-material-widgets/ic_volume_up_white_24px.svg"));
tabs->addTab("Talk", QIcon("../qt-material-widgets/ic_message_white_24px.svg"));
tabs->addTab("Eat", QIcon("../qt-material-widgets/ic_local_dining_white_24px.svg"));
QScrollArea *area = new QScrollArea;
area->setWidget(widget);