This commit is contained in:
Achraf k 2022-02-17 04:03:49 +01:00
parent 80f7bbdb47
commit 0fee3cf434
5 changed files with 118 additions and 105 deletions

View File

@ -3,26 +3,28 @@
#include <QtWidgets/QHBoxLayout> #include <QtWidgets/QHBoxLayout>
#include "qtmaterialtabs_internal.h" #include "qtmaterialtabs_internal.h"
#include "lib/qtmaterialstyle.h" #include "lib/qtmaterialstyle.h"
namespace md
{
/*! /*!
* \QtMaterialTabsPrivate * \QtMaterialTabsPrivate
* \internal * \internal
*/ */
QtMaterialTabsPrivate::QtMaterialTabsPrivate(QtMaterialTabs *q) TabsPrivate::TabsPrivate(Tabs *q)
: q_ptr(q) : q_ptr(q)
{ {
} }
QtMaterialTabsPrivate::~QtMaterialTabsPrivate() TabsPrivate::~TabsPrivate()
{ {
} }
void QtMaterialTabsPrivate::QtMaterialTabsPrivate::init() void TabsPrivate::TabsPrivate::init()
{ {
Q_Q(QtMaterialTabs); Q_Q(Tabs);
inkBar = new QtMaterialTabsInkBar(q); inkBar = new TabsInkBar(q);
tabLayout = new QHBoxLayout; tabLayout = new QHBoxLayout;
rippleStyle = Material::CenteredRipple; rippleStyle = Material::CenteredRipple;
tab = -1; tab = -1;
@ -30,7 +32,7 @@ void QtMaterialTabsPrivate::QtMaterialTabsPrivate::init()
useThemeColors = true; useThemeColors = true;
q->setLayout(tabLayout); q->setLayout(tabLayout);
q->setStyle(&QtMaterialStyle::instance()); q->setStyle(&Style::instance());
tabLayout->setSpacing(0); tabLayout->setSpacing(0);
tabLayout->setMargin(0); tabLayout->setMargin(0);
@ -40,64 +42,64 @@ void QtMaterialTabsPrivate::QtMaterialTabsPrivate::init()
* \QtMaterialTabs * \QtMaterialTabs
*/ */
QtMaterialTabs::QtMaterialTabs(QWidget *parent) Tabs::Tabs(QWidget *parent)
: QWidget(parent), : QWidget(parent),
d_ptr(new QtMaterialTabsPrivate(this)) d_ptr(new TabsPrivate(this))
{ {
d_func()->init(); d_func()->init();
} }
QtMaterialTabs::~QtMaterialTabs() Tabs::~Tabs()
{ {
} }
void QtMaterialTabs::setUseThemeColors(bool value) void Tabs::setUseThemeColors(bool value)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
d->useThemeColors = value; d->useThemeColors = value;
} }
bool QtMaterialTabs::useThemeColors() const bool Tabs::useThemeColors() const
{ {
Q_D(const QtMaterialTabs); Q_D(const Tabs);
return d->useThemeColors; return d->useThemeColors;
} }
void QtMaterialTabs::setHaloVisible(bool value) void Tabs::setHaloVisible(bool value)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
d->showHalo = value; d->showHalo = value;
updateTabs(); updateTabs();
} }
bool QtMaterialTabs::isHaloVisible() const bool Tabs::isHaloVisible() const
{ {
Q_D(const QtMaterialTabs); Q_D(const Tabs);
return d->showHalo; return d->showHalo;
} }
void QtMaterialTabs::setRippleStyle(Material::RippleStyle style) void Tabs::setRippleStyle(Material::RippleStyle style)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
d->rippleStyle = style; d->rippleStyle = style;
updateTabs(); updateTabs();
} }
Material::RippleStyle QtMaterialTabs::rippleStyle() const Material::RippleStyle Tabs::rippleStyle() const
{ {
Q_D(const QtMaterialTabs); Q_D(const Tabs);
return d->rippleStyle; return d->rippleStyle;
} }
void QtMaterialTabs::setInkColor(const QColor &color) void Tabs::setInkColor(const QColor &color)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
d->inkColor = color; d->inkColor = color;
@ -106,20 +108,20 @@ void QtMaterialTabs::setInkColor(const QColor &color)
update(); update();
} }
QColor QtMaterialTabs::inkColor() const QColor Tabs::inkColor() const
{ {
Q_D(const QtMaterialTabs); Q_D(const Tabs);
if (d->useThemeColors || !d->inkColor.isValid()) { if (d->useThemeColors || !d->inkColor.isValid()) {
return QtMaterialStyle::instance().themeColor("accent1"); return Style::instance().themeColor("accent1");
} else { } else {
return d->inkColor; return d->inkColor;
} }
} }
void QtMaterialTabs::setBackgroundColor(const QColor &color) void Tabs::setBackgroundColor(const QColor &color)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
d->backgroundColor = color; d->backgroundColor = color;
@ -128,20 +130,20 @@ void QtMaterialTabs::setBackgroundColor(const QColor &color)
update(); update();
} }
QColor QtMaterialTabs::backgroundColor() const QColor Tabs::backgroundColor() const
{ {
Q_D(const QtMaterialTabs); Q_D(const Tabs);
if (d->useThemeColors || !d->backgroundColor.isValid()) { if (d->useThemeColors || !d->backgroundColor.isValid()) {
return QtMaterialStyle::instance().themeColor("primary1"); return Style::instance().themeColor("primary1");
} else { } else {
return d->backgroundColor; return d->backgroundColor;
} }
} }
void QtMaterialTabs::setTextColor(const QColor &color) void Tabs::setTextColor(const QColor &color)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
d->textColor = color; d->textColor = color;
@ -150,27 +152,27 @@ void QtMaterialTabs::setTextColor(const QColor &color)
update(); update();
} }
QColor QtMaterialTabs::textColor() const QColor Tabs::textColor() const
{ {
Q_D(const QtMaterialTabs); Q_D(const Tabs);
if (d->useThemeColors || !d->textColor.isValid()) { if (d->useThemeColors || !d->textColor.isValid()) {
return QtMaterialStyle::instance().themeColor("canvas"); return Style::instance().themeColor("canvas");
} else { } else {
return d->textColor; return d->textColor;
} }
} }
void QtMaterialTabs::setCurrentTab(QtMaterialTab *tab) void Tabs::setCurrentTab(Tab *tab)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
setCurrentTab(d->tabLayout->indexOf(tab)); setCurrentTab(d->tabLayout->indexOf(tab));
} }
void QtMaterialTabs::setCurrentTab(int index) void Tabs::setCurrentTab(int index)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
setTabActive(d->tab, false); setTabActive(d->tab, false);
d->tab = index; d->tab = index;
@ -180,11 +182,11 @@ void QtMaterialTabs::setCurrentTab(int index)
emit currentChanged(index); emit currentChanged(index);
} }
void QtMaterialTabs::addTab(const QString &text, const QIcon &icon) void Tabs::addTab(const QString &text, const QIcon &icon)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
QtMaterialTab *tab = new QtMaterialTab(this); Tab *tab = new Tab(this);
tab->setText(text); tab->setText(text);
tab->setHaloVisible(isHaloVisible()); tab->setHaloVisible(isHaloVisible());
tab->setRippleStyle(rippleStyle()); tab->setRippleStyle(rippleStyle());
@ -204,35 +206,35 @@ void QtMaterialTabs::addTab(const QString &text, const QIcon &icon)
} }
} }
int QtMaterialTabs::currentIndex() const int Tabs::currentIndex() const
{ {
Q_D(const QtMaterialTabs); Q_D(const Tabs);
return d->tab; return d->tab;
} }
void QtMaterialTabs::setTabActive(int index, bool active) void Tabs::setTabActive(int index, bool active)
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
QtMaterialTab *tab; Tab *tab;
if (index > -1) { if (index > -1) {
tab = static_cast<QtMaterialTab *>(d->tabLayout->itemAt(index)->widget()); tab = static_cast<Tab *>(d->tabLayout->itemAt(index)->widget());
if (tab) { if (tab) {
tab->setActive(active); tab->setActive(active);
} }
} }
} }
void QtMaterialTabs::updateTabs() void Tabs::updateTabs()
{ {
Q_D(QtMaterialTabs); Q_D(Tabs);
QtMaterialTab *tab; Tab *tab;
for (int i = 0; i < d->tabLayout->count(); ++i) { for (int i = 0; i < d->tabLayout->count(); ++i) {
QLayoutItem *item = d->tabLayout->itemAt(i); QLayoutItem *item = d->tabLayout->itemAt(i);
if ((tab = static_cast<QtMaterialTab *>(item->widget()))) { if ((tab = static_cast<Tab *>(item->widget()))) {
tab->setRippleStyle(d->rippleStyle); tab->setRippleStyle(d->rippleStyle);
tab->setHaloVisible(d->showHalo); tab->setHaloVisible(d->showHalo);
tab->setBackgroundColor(backgroundColor()); tab->setBackgroundColor(backgroundColor());
@ -240,3 +242,4 @@ void QtMaterialTabs::updateTabs()
} }
} }
} }
}

View File

@ -4,17 +4,19 @@
#include <QtWidgets/QWidget> #include <QtWidgets/QWidget>
#include <QIcon> #include <QIcon>
#include "lib/qtmaterialtheme.h" #include "lib/qtmaterialtheme.h"
namespace md
{
class QtMaterialTabsPrivate; class TabsPrivate;
class QtMaterialTab; class Tab;
class QtMaterialTabs : public QWidget class Tabs : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit QtMaterialTabs(QWidget *parent = 0); explicit Tabs(QWidget *parent = 0);
~QtMaterialTabs(); ~Tabs();
void setUseThemeColors(bool value); void setUseThemeColors(bool value);
bool useThemeColors() const; bool useThemeColors() const;
@ -36,7 +38,7 @@ public:
void addTab(const QString &text, const QIcon &icon = QIcon()); void addTab(const QString &text, const QIcon &icon = QIcon());
void setCurrentTab(QtMaterialTab *tab); void setCurrentTab(Tab *tab);
void setCurrentTab(int index); void setCurrentTab(int index);
int currentIndex() const; int currentIndex() const;
@ -48,11 +50,11 @@ protected:
void setTabActive(int index, bool active = true); void setTabActive(int index, bool active = true);
void updateTabs(); void updateTabs();
const QScopedPointer<QtMaterialTabsPrivate> d_ptr; const QScopedPointer<TabsPrivate> d_ptr;
private: private:
Q_DISABLE_COPY(QtMaterialTabs) Q_DISABLE_COPY(Tabs)
Q_DECLARE_PRIVATE(QtMaterialTabs) Q_DECLARE_PRIVATE(Tabs)
}; };
}
#endif // QTMATERIALTABS_H #endif // QTMATERIALTABS_H

View File

@ -6,14 +6,16 @@
#include <QEvent> #include <QEvent>
#include "qtmaterialtabs.h" #include "qtmaterialtabs.h"
#include <QDebug> #include <QDebug>
namespace md
{
/*! /*!
* \class QtMaterialTabsInkBar * \class QtMaterialTabsInkBar
* \internal * \internal
*/ */
QtMaterialTabsInkBar::QtMaterialTabsInkBar(QtMaterialTabs *parent) TabsInkBar::TabsInkBar(Tabs *parent)
: QtMaterialOverlayWidget(parent), : OverlayWidget(parent),
m_tabs(parent), m_tabs(parent),
m_animation(new QPropertyAnimation(parent)), m_animation(new QPropertyAnimation(parent)),
m_tween(0) m_tween(0)
@ -31,11 +33,11 @@ QtMaterialTabsInkBar::QtMaterialTabsInkBar(QtMaterialTabs *parent)
setAttribute(Qt::WA_NoSystemBackground); setAttribute(Qt::WA_NoSystemBackground);
} }
QtMaterialTabsInkBar::~QtMaterialTabsInkBar() TabsInkBar::~TabsInkBar()
{ {
} }
void QtMaterialTabsInkBar::refreshGeometry() void TabsInkBar::refreshGeometry()
{ {
QLayoutItem *item = m_tabs->layout()->itemAt(m_tabs->currentIndex()); QLayoutItem *item = m_tabs->layout()->itemAt(m_tabs->currentIndex());
@ -55,7 +57,7 @@ void QtMaterialTabsInkBar::refreshGeometry()
} }
} }
void QtMaterialTabsInkBar::animate() void TabsInkBar::animate()
{ {
raise(); raise();
@ -67,7 +69,7 @@ void QtMaterialTabsInkBar::animate()
m_animation->start(); m_animation->start();
} }
bool QtMaterialTabsInkBar::eventFilter(QObject *obj, QEvent *event) bool TabsInkBar::eventFilter(QObject *obj, QEvent *event)
{ {
switch (event->type()) switch (event->type())
{ {
@ -80,10 +82,10 @@ bool QtMaterialTabsInkBar::eventFilter(QObject *obj, QEvent *event)
default: default:
break; break;
} }
return QtMaterialOverlayWidget::eventFilter(obj, event); return OverlayWidget::eventFilter(obj, event);
} }
void QtMaterialTabsInkBar::paintEvent(QPaintEvent *event) void TabsInkBar::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event) Q_UNUSED(event)
@ -98,8 +100,8 @@ void QtMaterialTabsInkBar::paintEvent(QPaintEvent *event)
* \internal * \internal
*/ */
QtMaterialTab::QtMaterialTab(QtMaterialTabs *parent) Tab::Tab(Tabs *parent)
: QtMaterialFlatButton(parent), : FlatButton(parent),
m_tabs(parent), m_tabs(parent),
m_active(false) m_active(false)
{ {
@ -119,25 +121,25 @@ QtMaterialTab::QtMaterialTab(QtMaterialTabs *parent)
connect(this, SIGNAL(clicked(bool)), this, SLOT(activateTab())); connect(this, SIGNAL(clicked(bool)), this, SLOT(activateTab()));
} }
QtMaterialTab::~QtMaterialTab() Tab::~Tab()
{ {
} }
QSize QtMaterialTab::sizeHint() const QSize Tab::sizeHint() const
{ {
if (icon().isNull()) { if (icon().isNull()) {
return QtMaterialFlatButton::sizeHint(); return FlatButton::sizeHint();
} else { } else {
return QSize(40, iconSize().height()+46); return QSize(40, iconSize().height()+46);
} }
} }
void QtMaterialTab::activateTab() void Tab::activateTab()
{ {
m_tabs->setCurrentTab(this); m_tabs->setCurrentTab(this);
} }
void QtMaterialTab::paintForeground(QPainter *painter) void Tab::paintForeground(QPainter *painter)
{ {
painter->setPen(foregroundColor()); painter->setPen(foregroundColor());
@ -176,3 +178,4 @@ void QtMaterialTab::paintForeground(QPainter *painter)
painter->fillRect(rect(), overlay); painter->fillRect(rect(), overlay);
} }
} }
}

View File

@ -4,18 +4,21 @@
#include "lib/qtmaterialoverlaywidget.h" #include "lib/qtmaterialoverlaywidget.h"
#include "qtmaterialflatbutton.h" #include "qtmaterialflatbutton.h"
class QPropertyAnimation; namespace md
class QtMaterialTabs; {
class QtMaterialTabsInkBar : public QtMaterialOverlayWidget class QPropertyAnimation;
class Tabs;
class TabsInkBar : public OverlayWidget
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(qreal tweenValue WRITE setTweenValue READ tweenValue) Q_PROPERTY(qreal tweenValue WRITE setTweenValue READ tweenValue)
public: public:
QtMaterialTabsInkBar(QtMaterialTabs *parent); TabsInkBar(Tabs *parent);
~QtMaterialTabsInkBar(); ~TabsInkBar();
inline void setTweenValue(qreal value); inline void setTweenValue(qreal value);
inline qreal tweenValue() const; inline qreal tweenValue() const;
@ -28,33 +31,33 @@ protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private: private:
Q_DISABLE_COPY(QtMaterialTabsInkBar) Q_DISABLE_COPY(TabsInkBar)
QtMaterialTabs *const m_tabs; Tabs *const m_tabs;
QPropertyAnimation *const m_animation; QPropertyAnimation *const m_animation;
QRect m_geometry; QRect m_geometry;
QRect m_previousGeometry; QRect m_previousGeometry;
qreal m_tween; qreal m_tween;
}; };
inline void QtMaterialTabsInkBar::setTweenValue(qreal value) inline void TabsInkBar::setTweenValue(qreal value)
{ {
m_tween = value; m_tween = value;
refreshGeometry(); refreshGeometry();
} }
inline qreal QtMaterialTabsInkBar::tweenValue() const inline qreal TabsInkBar::tweenValue() const
{ {
return m_tween; return m_tween;
} }
class QtMaterialTab : public QtMaterialFlatButton class Tab : public FlatButton
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit QtMaterialTab(QtMaterialTabs *parent); explicit Tab(Tabs *parent);
~QtMaterialTab(); ~Tab();
inline void setActive(bool state); inline void setActive(bool state);
inline bool isActive() const; inline bool isActive() const;
@ -68,21 +71,21 @@ protected:
void paintForeground(QPainter *painter) Q_DECL_OVERRIDE; void paintForeground(QPainter *painter) Q_DECL_OVERRIDE;
private: private:
Q_DISABLE_COPY(QtMaterialTab) Q_DISABLE_COPY(Tab)
QtMaterialTabs *const m_tabs; Tabs *const m_tabs;
bool m_active; bool m_active;
}; };
inline void QtMaterialTab::setActive(bool state) inline void Tab::setActive(bool state)
{ {
m_active = state; m_active = state;
update(); update();
} }
inline bool QtMaterialTab::isActive() const inline bool Tab::isActive() const
{ {
return m_active; return m_active;
} }
}
#endif // QTMATERIALTABS_INTERNAL_H #endif // QTMATERIALTABS_INTERNAL_H

View File

@ -3,24 +3,26 @@
#include <QtGlobal> #include <QtGlobal>
#include "lib/qtmaterialtheme.h" #include "lib/qtmaterialtheme.h"
namespace md
{
class QHBoxLayout; class QHBoxLayout;
class QtMaterialTabs; class Tabs;
class QtMaterialTabsInkBar; class TabsInkBar;
class QtMaterialTabsPrivate class TabsPrivate
{ {
Q_DISABLE_COPY(QtMaterialTabsPrivate) Q_DISABLE_COPY(TabsPrivate)
Q_DECLARE_PUBLIC(QtMaterialTabs) Q_DECLARE_PUBLIC(Tabs)
public: public:
QtMaterialTabsPrivate(QtMaterialTabs *q); TabsPrivate(Tabs *q);
~QtMaterialTabsPrivate(); ~TabsPrivate();
void init(); void init();
QtMaterialTabs *const q_ptr; Tabs *const q_ptr;
QtMaterialTabsInkBar *inkBar; TabsInkBar *inkBar;
QHBoxLayout *tabLayout; QHBoxLayout *tabLayout;
Material::RippleStyle rippleStyle; Material::RippleStyle rippleStyle;
QColor inkColor; QColor inkColor;
@ -30,5 +32,5 @@ public:
bool showHalo; bool showHalo;
bool useThemeColors; bool useThemeColors;
}; };
}
#endif // QTMATERIALTABS_P_H #endif // QTMATERIALTABS_P_H