From 9ddb4d6fb02b111c7311954aff03c156ffb22b9a Mon Sep 17 00:00:00 2001 From: laserpants Date: Sat, 11 Jun 2016 18:43:33 +0300 Subject: [PATCH] shade inactive tabs --- components/tabs.cpp | 9 ++++++++- components/tabs_internal.cpp | 32 ++++++++++++++++++++++---------- components/tabs_internal.h | 5 +++++ qt-material-widgets.pro | 2 +- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/components/tabs.cpp b/components/tabs.cpp index 79bf6ad..9461b11 100644 --- a/components/tabs.cpp +++ b/components/tabs.cpp @@ -135,7 +135,7 @@ void Tabs::addTab(const QString &text, const QIcon &icon) Tab *tab = createTab(text); tab->setIcon(icon); - tab->setIconSize(QSize(24, 24)); + tab->setIconSize(QSize(22, 22)); } void Tabs::setRippleStyle(Material::RippleStyle style) @@ -188,8 +188,14 @@ void Tabs::switchTab() { Q_D(Tabs); + Tab *oldTab = static_cast(d->tabLayout->itemAt(d->tab)->widget()); + if (oldTab) { + oldTab->setActive(false); + } + Tab *tab = static_cast(sender()); if (tab) { + tab->setActive(true); d->tab = d->tabLayout->indexOf(tab); d->inkBar->animate(); emit currentChanged(d->tab); @@ -212,6 +218,7 @@ Tab *Tabs::createTab(const QString &text) if (-1 == d->tab) { d->tab = 0; d->inkBar->refreshGeometry(); + tab->setActive(true); } connect(tab, SIGNAL(clicked()), this, SLOT(switchTab())); diff --git a/components/tabs_internal.cpp b/components/tabs_internal.cpp index 5ce54ab..3e82457 100644 --- a/components/tabs_internal.cpp +++ b/components/tabs_internal.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include "tabs.h" TabsInkBar::TabsInkBar(Tabs *parent) @@ -110,7 +109,7 @@ QSize Tab::sizeHint() const if (icon().isNull()) { return FlatButton::sizeHint(); } else { - return QSize(40, iconSize().height() + 48); + return QSize(40, iconSize().height() + 46); } } @@ -134,28 +133,41 @@ void Tab::paintEvent(QPaintEvent *event) QStylePainter style(this); if (!icon().isNull()) { - style.translate(0, 10); + style.translate(0, 12); } QStyleOptionButton option; initStyleOption(&option); option.features |= QStyleOptionButton::Flat; - option.iconSize = QSize(-1, -1); // Let's not draw the icon right now + option.iconSize = QSize(-1, -1); // Prevent icon from being drawn twice style.drawControl(QStyle::CE_PushButtonLabel, option); if (!icon().isNull()) { const QSize &size = iconSize(); - QRect iconPos(QPoint((width()-size.width())/2, 0), size); - icon().paint(&painter, iconPos, Qt::AlignCenter, QIcon::Normal); + QRect iconRect(QPoint((width()-size.width())/2, 0), size); + icon().paint(&painter, iconRect, Qt::AlignCenter, QIcon::Normal); } + if (!_active) { + QColor overlayColor = backgroundColor(); + overlayColor.setAlphaF(0.36); + + QBrush overlay; + overlay.setStyle(Qt::SolidPattern); + overlay.setColor(overlayColor); + painter.fillRect(rect(), overlay); + } + +#ifdef DEBUG_LAYOUT + QPainter debug(this); QPen pen; pen.setColor(Qt::red); - pen.setWidth(5); - painter.setPen(pen); - painter.setBrush(Qt::NoBrush); - painter.drawRect(rect()); + pen.setWidth(2); + debug.setPen(pen); + debug.setBrush(Qt::NoBrush); + debug.drawRect(rect()); +#endif } void Tab::init() diff --git a/components/tabs_internal.h b/components/tabs_internal.h index facae34..b393c03 100644 --- a/components/tabs_internal.h +++ b/components/tabs_internal.h @@ -45,6 +45,9 @@ public: explicit Tab(QString text, QWidget *parent = 0); ~Tab(); + inline void setActive(bool state) { _active = state; update(); } + inline bool isActive() const { return _active; } + QSize sizeHint() const Q_DECL_OVERRIDE; protected: @@ -54,6 +57,8 @@ private: Q_DISABLE_COPY(Tab) void init(); + + bool _active; }; #endif // TABS_INTERNAL_H diff --git a/qt-material-widgets.pro b/qt-material-widgets.pro index de25fec..27006a5 100644 --- a/qt-material-widgets.pro +++ b/qt-material-widgets.pro @@ -1,4 +1,4 @@ -QT += core gui qml svg +QT += core gui qml greaterThan(QT_MAJOR_VERSION, 4): QT += widgets