qt-material-widgets/components/qtmaterialtabs.cpp

246 lines
4.1 KiB
C++
Raw Normal View History

2017-09-29 17:25:42 +00:00
#include "qtmaterialtabs.h"
#include "qtmaterialtabs_p.h"
#include <QtWidgets/QHBoxLayout>
#include "qtmaterialtabs_internal.h"
#include "lib/qtmaterialstyle.h"
2022-02-17 03:03:49 +00:00
namespace md
{
2017-09-29 17:25:42 +00:00
/*!
* \QtMaterialTabsPrivate
* \internal
*/
2022-02-17 03:03:49 +00:00
TabsPrivate::TabsPrivate(Tabs *q)
2017-09-29 17:25:42 +00:00
: q_ptr(q)
{
}
2022-02-17 03:03:49 +00:00
TabsPrivate::~TabsPrivate()
2017-09-29 17:25:42 +00:00
{
}
2022-02-17 03:03:49 +00:00
void TabsPrivate::TabsPrivate::init()
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_Q(Tabs);
2017-09-29 17:25:42 +00:00
2022-02-17 03:03:49 +00:00
inkBar = new TabsInkBar(q);
2017-09-29 17:25:42 +00:00
tabLayout = new QHBoxLayout;
rippleStyle = Material::CenteredRipple;
tab = -1;
showHalo = true;
useThemeColors = true;
q->setLayout(tabLayout);
2022-02-17 03:03:49 +00:00
q->setStyle(&Style::instance());
2017-09-29 17:25:42 +00:00
tabLayout->setSpacing(0);
tabLayout->setMargin(0);
}
/*!
* \QtMaterialTabs
*/
2022-02-17 03:03:49 +00:00
Tabs::Tabs(QWidget *parent)
2017-09-29 17:25:42 +00:00
: QWidget(parent),
2022-02-17 03:03:49 +00:00
d_ptr(new TabsPrivate(this))
2017-09-29 17:25:42 +00:00
{
d_func()->init();
}
2022-02-17 03:03:49 +00:00
Tabs::~Tabs()
2017-09-29 17:25:42 +00:00
{
}
2022-02-17 03:03:49 +00:00
void Tabs::setUseThemeColors(bool value)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
d->useThemeColors = value;
}
2022-02-17 03:03:49 +00:00
bool Tabs::useThemeColors() const
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(const Tabs);
2017-09-29 17:25:42 +00:00
return d->useThemeColors;
}
2022-02-17 03:03:49 +00:00
void Tabs::setHaloVisible(bool value)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
d->showHalo = value;
updateTabs();
}
2022-02-17 03:03:49 +00:00
bool Tabs::isHaloVisible() const
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(const Tabs);
2017-09-29 17:25:42 +00:00
return d->showHalo;
}
2022-02-17 03:03:49 +00:00
void Tabs::setRippleStyle(Material::RippleStyle style)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
d->rippleStyle = style;
updateTabs();
}
2022-02-17 03:03:49 +00:00
Material::RippleStyle Tabs::rippleStyle() const
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(const Tabs);
2017-09-29 17:25:42 +00:00
return d->rippleStyle;
}
2022-02-17 03:03:49 +00:00
void Tabs::setInkColor(const QColor &color)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
d->inkColor = color;
2017-09-29 17:48:26 +00:00
MATERIAL_DISABLE_THEME_COLORS
2017-09-29 17:25:42 +00:00
d->inkBar->update();
2017-09-29 17:48:26 +00:00
update();
2017-09-29 17:25:42 +00:00
}
2022-02-17 03:03:49 +00:00
QColor Tabs::inkColor() const
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(const Tabs);
2017-09-29 17:25:42 +00:00
if (d->useThemeColors || !d->inkColor.isValid()) {
2022-02-17 03:03:49 +00:00
return Style::instance().themeColor("accent1");
2017-09-29 17:25:42 +00:00
} else {
return d->inkColor;
}
}
2022-02-17 03:03:49 +00:00
void Tabs::setBackgroundColor(const QColor &color)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
d->backgroundColor = color;
2017-09-29 17:48:26 +00:00
MATERIAL_DISABLE_THEME_COLORS
2017-09-29 17:25:42 +00:00
updateTabs();
2017-09-29 17:48:26 +00:00
update();
2017-09-29 17:25:42 +00:00
}
2022-02-17 03:03:49 +00:00
QColor Tabs::backgroundColor() const
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(const Tabs);
2017-09-29 17:25:42 +00:00
if (d->useThemeColors || !d->backgroundColor.isValid()) {
2022-02-17 03:03:49 +00:00
return Style::instance().themeColor("primary1");
2017-09-29 17:25:42 +00:00
} else {
return d->backgroundColor;
}
}
2022-02-17 03:03:49 +00:00
void Tabs::setTextColor(const QColor &color)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
d->textColor = color;
2017-09-29 17:48:26 +00:00
MATERIAL_DISABLE_THEME_COLORS
2017-09-29 17:25:42 +00:00
updateTabs();
2017-09-29 17:48:26 +00:00
update();
2017-09-29 17:25:42 +00:00
}
2022-02-17 03:03:49 +00:00
QColor Tabs::textColor() const
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(const Tabs);
2017-09-29 17:25:42 +00:00
if (d->useThemeColors || !d->textColor.isValid()) {
2022-02-17 03:03:49 +00:00
return Style::instance().themeColor("canvas");
2017-09-29 17:25:42 +00:00
} else {
return d->textColor;
}
}
2022-02-17 03:03:49 +00:00
void Tabs::setCurrentTab(Tab *tab)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
setCurrentTab(d->tabLayout->indexOf(tab));
}
2022-02-17 03:03:49 +00:00
void Tabs::setCurrentTab(int index)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
setTabActive(d->tab, false);
d->tab = index;
setTabActive(index, true);
d->inkBar->animate();
emit currentChanged(index);
}
2022-02-17 03:03:49 +00:00
void Tabs::addTab(const QString &text, const QIcon &icon)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
2022-02-17 03:03:49 +00:00
Tab *tab = new Tab(this);
2017-09-29 17:25:42 +00:00
tab->setText(text);
tab->setHaloVisible(isHaloVisible());
tab->setRippleStyle(rippleStyle());
if (!icon.isNull()) {
tab->setIcon(icon);
tab->setIconSize(QSize(22, 22));
}
d->tabLayout->addWidget(tab);
if (-1 == d->tab) {
d->tab = 0;
d->inkBar->refreshGeometry();
d->inkBar->raise();
tab->setActive(true);
}
}
2022-02-17 03:03:49 +00:00
int Tabs::currentIndex() const
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(const Tabs);
2017-09-29 17:25:42 +00:00
return d->tab;
}
2022-02-17 03:03:49 +00:00
void Tabs::setTabActive(int index, bool active)
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
2022-02-17 03:03:49 +00:00
Tab *tab;
2017-09-29 17:25:42 +00:00
if (index > -1) {
2022-02-17 03:03:49 +00:00
tab = static_cast<Tab *>(d->tabLayout->itemAt(index)->widget());
2017-09-29 17:25:42 +00:00
if (tab) {
tab->setActive(active);
}
}
}
2022-02-17 03:03:49 +00:00
void Tabs::updateTabs()
2017-09-29 17:25:42 +00:00
{
2022-02-17 03:03:49 +00:00
Q_D(Tabs);
2017-09-29 17:25:42 +00:00
2022-02-17 03:03:49 +00:00
Tab *tab;
2017-09-29 17:25:42 +00:00
for (int i = 0; i < d->tabLayout->count(); ++i) {
QLayoutItem *item = d->tabLayout->itemAt(i);
2022-02-17 03:03:49 +00:00
if ((tab = static_cast<Tab *>(item->widget()))) {
2017-09-29 17:25:42 +00:00
tab->setRippleStyle(d->rippleStyle);
tab->setHaloVisible(d->showHalo);
tab->setBackgroundColor(backgroundColor());
tab->setForegroundColor(textColor());
}
}
}
2022-02-17 03:03:49 +00:00
}