diff --git a/components/tabs.cpp b/components/tabs.cpp index e9eb15e..79bf6ad 100644 --- a/components/tabs.cpp +++ b/components/tabs.cpp @@ -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; +} diff --git a/components/tabs.h b/components/tabs.h index 0ea315e..566625a 100644 --- a/components/tabs.h +++ b/components/tabs.h @@ -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 diff --git a/components/tabs_internal.cpp b/components/tabs_internal.cpp index ce9f264..223274b 100644 --- a/components/tabs_internal.cpp +++ b/components/tabs_internal.cpp @@ -1,8 +1,12 @@ #include "tabs_internal.h" #include #include +#include +#include #include +#include #include +#include #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); } diff --git a/components/tabs_internal.h b/components/tabs_internal.h index 9d384a3..facae34 100644 --- a/components/tabs_internal.h +++ b/components/tabs_internal.h @@ -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) diff --git a/examples/tabsexamples.cpp b/examples/tabsexamples.cpp index 8f8a87b..b984f7c 100644 --- a/examples/tabsexamples.cpp +++ b/examples/tabsexamples.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #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);