refactor theme hooks
This commit is contained in:
parent
0a79208ab4
commit
e3517e11d1
|
@ -9,6 +9,15 @@
|
|||
#include "lib/ripple.h"
|
||||
#include "flatbutton_p.h"
|
||||
|
||||
FlatButtonPrivate::FlatButtonPrivate(FlatButton *q)
|
||||
: q_ptr(q),
|
||||
role(Material::Default),
|
||||
rippleStyle(Material::PositionedRipple),
|
||||
cornerRadius(3),
|
||||
bgMode(Qt::TransparentMode)
|
||||
{
|
||||
}
|
||||
|
||||
void FlatButtonPrivate::init()
|
||||
{
|
||||
Q_Q(FlatButton);
|
||||
|
@ -27,26 +36,27 @@ void FlatButtonPrivate::init()
|
|||
font.setPointSizeF(10.5);
|
||||
font.setStyleName("Medium");
|
||||
q->setFont(font);
|
||||
|
||||
QPalette palette;
|
||||
palette.setColor(QPalette::Disabled, QPalette::ButtonText,
|
||||
style.themeColor("disabled"));
|
||||
q->setPalette(palette);
|
||||
}
|
||||
|
||||
/*
|
||||
void FlatButtonPrivate::setPaletteColor(QPalette::ColorGroup group,
|
||||
QPalette::ColorRole role,
|
||||
const QString &themeColor)
|
||||
const QColor &color)
|
||||
{
|
||||
Q_Q(FlatButton);
|
||||
|
||||
QPalette palette(q->palette());
|
||||
Style &style = Style::instance();
|
||||
|
||||
palette.setColor(group, role, style.themeColor(themeColor));
|
||||
|
||||
palette.setColor(group, role, color);
|
||||
q->setPalette(palette);
|
||||
|
||||
// QPalette palette(q->palette());
|
||||
// Style &style = Style::instance();
|
||||
//
|
||||
// palette.setColor(group, role, style.themeColor(themeColor));
|
||||
//
|
||||
// q->setPalette(palette);
|
||||
}
|
||||
*/
|
||||
|
||||
FlatButton::FlatButton(QWidget *parent)
|
||||
: QPushButton(parent),
|
||||
|
@ -112,26 +122,72 @@ Qt::BGMode FlatButton::bgMode() const
|
|||
return d->bgMode;
|
||||
}
|
||||
|
||||
void FlatButton::setPrimaryTextColor(const QColor &color)
|
||||
{
|
||||
Q_D(FlatButton);
|
||||
|
||||
d->primaryTextColor = color;
|
||||
d->delegate->updatePalette();
|
||||
}
|
||||
|
||||
QColor FlatButton::primaryTextColor() const
|
||||
{
|
||||
Q_D(const FlatButton);
|
||||
|
||||
return d->primaryTextColor;
|
||||
}
|
||||
|
||||
void FlatButton::setSecondaryTextColor(const QColor &color)
|
||||
{
|
||||
Q_D(FlatButton);
|
||||
|
||||
d->secondaryTextColor = color;
|
||||
d->delegate->updatePalette();
|
||||
}
|
||||
|
||||
QColor FlatButton::secondaryTextColor() const
|
||||
{
|
||||
Q_D(const FlatButton);
|
||||
|
||||
return d->secondaryTextColor;
|
||||
}
|
||||
|
||||
void FlatButton::setDefaultTextColor(const QColor &color)
|
||||
{
|
||||
Q_D(FlatButton);
|
||||
|
||||
d->defaultTextColor = color;
|
||||
d->delegate->updatePalette();
|
||||
}
|
||||
|
||||
QColor FlatButton::defaultTextColor() const
|
||||
{
|
||||
Q_D(const FlatButton);
|
||||
|
||||
return d->defaultTextColor;
|
||||
}
|
||||
|
||||
void FlatButton::setDisabledTextColor(const QColor &color)
|
||||
{
|
||||
Q_D(FlatButton);
|
||||
|
||||
d->disabledTextColor = color;
|
||||
d->delegate->updatePalette();
|
||||
}
|
||||
|
||||
QColor FlatButton::disabledTextColor() const
|
||||
{
|
||||
Q_D(const FlatButton);
|
||||
|
||||
return d->disabledTextColor;
|
||||
}
|
||||
|
||||
void FlatButton::setRole(Material::Role role)
|
||||
{
|
||||
Q_D(FlatButton);
|
||||
|
||||
d->role = role;
|
||||
|
||||
switch (role)
|
||||
{
|
||||
case Material::Primary:
|
||||
d->setPaletteColor(QPalette::Active, QPalette::ButtonText, "primary1");
|
||||
break;
|
||||
case Material::Secondary:
|
||||
d->setPaletteColor(QPalette::Active, QPalette::ButtonText, "accent1");
|
||||
break;
|
||||
default:
|
||||
d->setPaletteColor(QPalette::Active, QPalette::ButtonText, "text");
|
||||
break;
|
||||
}
|
||||
d->delegate->assignProperties();
|
||||
update();
|
||||
d->delegate->updatePalette();
|
||||
}
|
||||
|
||||
Material::Role FlatButton::role() const
|
||||
|
|
|
@ -15,6 +15,10 @@ class FlatButton : public QPushButton
|
|||
Q_PROPERTY(Material::RippleStyle rippleStyle WRITE setRippleStyle READ rippleStyle)
|
||||
Q_PROPERTY(qreal cornerRadius WRITE setCornerRadius READ cornerRadius)
|
||||
Q_PROPERTY(Qt::BGMode bgMode WRITE setBgMode READ bgMode)
|
||||
Q_PROPERTY(QColor primaryTextColor WRITE setPrimaryTextColor READ primaryTextColor)
|
||||
Q_PROPERTY(QColor secondaryTextColor WRITE setSecondaryTextColor READ secondaryTextColor)
|
||||
Q_PROPERTY(QColor defaultTextColor WRITE setDefaultTextColor READ defaultTextColor)
|
||||
Q_PROPERTY(QColor disabledTextColor WRITE setDisabledTextColor READ disabledTextColor)
|
||||
|
||||
public:
|
||||
explicit FlatButton(QWidget *parent = 0);
|
||||
|
@ -33,6 +37,18 @@ public:
|
|||
void setBgMode(Qt::BGMode mode);
|
||||
Qt::BGMode bgMode() const;
|
||||
|
||||
void setPrimaryTextColor(const QColor &color);
|
||||
QColor primaryTextColor() const;
|
||||
|
||||
void setSecondaryTextColor(const QColor &color);
|
||||
QColor secondaryTextColor() const;
|
||||
|
||||
void setDefaultTextColor(const QColor &color);
|
||||
QColor defaultTextColor() const;
|
||||
|
||||
void setDisabledTextColor(const QColor &color);
|
||||
QColor disabledTextColor() const;
|
||||
|
||||
protected:
|
||||
FlatButton(FlatButtonPrivate &d, QWidget *parent = 0);
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent)
|
|||
{
|
||||
setInitialState(_normalState);
|
||||
|
||||
assignProperties();
|
||||
updatePalette();
|
||||
|
||||
addTransition(QEvent::Enter, _normalFocusedState, _hoveredFocusedState);
|
||||
addTransition(QEvent::FocusIn, _normalState, _normalFocusedState);
|
||||
|
@ -105,30 +105,49 @@ qreal FlatButtonDelegate::focusHaloSize() const
|
|||
return _focusHaloSize;
|
||||
}
|
||||
|
||||
void FlatButtonDelegate::assignProperties()
|
||||
void FlatButtonDelegate::updatePalette()
|
||||
{
|
||||
QColor textColor = button->palette().color(QPalette::Active,
|
||||
QPalette::ButtonText);
|
||||
QColor color;
|
||||
|
||||
switch (button->role())
|
||||
{
|
||||
case Material::Primary:
|
||||
color = button->primaryTextColor();
|
||||
break;
|
||||
case Material::Secondary:
|
||||
color = button->secondaryTextColor();
|
||||
break;
|
||||
case Material::Default:
|
||||
default:
|
||||
color = button->defaultTextColor();
|
||||
}
|
||||
|
||||
QPalette palette(button->palette());
|
||||
palette.setColor(QPalette::Active, QPalette::ButtonText, color);
|
||||
palette.setColor(QPalette::Disabled, QPalette::ButtonText, button->disabledTextColor());
|
||||
button->setPalette(palette);
|
||||
|
||||
_normalState->assignProperty(this, "backgroundOpacity", 0);
|
||||
_normalState->assignProperty(this, "backgroundColor", textColor);
|
||||
_normalState->assignProperty(this, "backgroundColor", color);
|
||||
_normalState->assignProperty(this, "focusHaloOpacity", 0);
|
||||
|
||||
_normalFocusedState->assignProperty(this, "backgroundOpacity", 0);
|
||||
_normalFocusedState->assignProperty(this, "backgroundColor", textColor);
|
||||
_normalFocusedState->assignProperty(this, "backgroundColor", color);
|
||||
_normalFocusedState->assignProperty(this, "focusHaloOpacity", 0.15);
|
||||
|
||||
_hoveredState->assignProperty(this, "backgroundOpacity", 0.15);
|
||||
_hoveredState->assignProperty(this, "backgroundColor", textColor);
|
||||
_hoveredState->assignProperty(this, "backgroundColor", color);
|
||||
_hoveredState->assignProperty(this, "focusHaloOpacity", 0);
|
||||
|
||||
_hoveredFocusedState->assignProperty(this, "backgroundOpacity", 0.15);
|
||||
_hoveredFocusedState->assignProperty(this, "backgroundColor", textColor);
|
||||
_hoveredFocusedState->assignProperty(this, "backgroundColor", color);
|
||||
_normalFocusedState->assignProperty(this, "focusHaloOpacity", 0.15);
|
||||
|
||||
_pressedState->assignProperty(this, "backgroundOpacity", 0.15);
|
||||
_pressedState->assignProperty(this, "backgroundColor", textColor);
|
||||
_pressedState->assignProperty(this, "backgroundColor", color);
|
||||
_pressedState->assignProperty(this, "focusHaloOpacity", 0);
|
||||
|
||||
button->update();
|
||||
}
|
||||
|
||||
void FlatButtonDelegate::addTransition(QEvent::Type eventType,
|
||||
|
|
|
@ -32,7 +32,7 @@ public:
|
|||
void setFocusHaloSize(qreal size);
|
||||
qreal focusHaloSize() const;
|
||||
|
||||
void assignProperties();
|
||||
void updatePalette();
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(FlatButtonDelegate)
|
||||
|
|
|
@ -15,16 +15,13 @@ class FlatButtonPrivate
|
|||
Q_DECLARE_PUBLIC(FlatButton)
|
||||
|
||||
public:
|
||||
FlatButtonPrivate(FlatButton *q)
|
||||
: q_ptr(q),
|
||||
role(Material::Default),
|
||||
rippleStyle(Material::PositionedRipple),
|
||||
cornerRadius(3),
|
||||
bgMode(Qt::TransparentMode)
|
||||
{}
|
||||
FlatButtonPrivate(FlatButton *q);
|
||||
|
||||
void init();
|
||||
void setPaletteColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QString &themeColor);
|
||||
|
||||
/*
|
||||
void setPaletteColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QColor &color);
|
||||
*/
|
||||
|
||||
FlatButton *const q_ptr;
|
||||
RippleOverlay *ripple;
|
||||
|
@ -33,6 +30,10 @@ public:
|
|||
Material::RippleStyle rippleStyle;
|
||||
qreal cornerRadius;
|
||||
Qt::BGMode bgMode;
|
||||
QColor primaryTextColor;
|
||||
QColor secondaryTextColor;
|
||||
QColor defaultTextColor;
|
||||
QColor disabledTextColor;
|
||||
};
|
||||
|
||||
#endif // FLATBUTTON_P_H
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef ICONBUTTON_P_H
|
||||
#define ICONBUTTON_P_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class IconButton;
|
||||
class RippleOverlay;
|
||||
|
||||
|
@ -14,7 +16,7 @@ public:
|
|||
|
||||
void init();
|
||||
|
||||
IconButton *const q_ptr;
|
||||
IconButton *const q_ptr;
|
||||
RippleOverlay *ripple;
|
||||
};
|
||||
|
||||
|
|
|
@ -103,6 +103,7 @@ void RaisedButton::setRole(Material::Role role)
|
|||
|
||||
d->role = role;
|
||||
|
||||
/*
|
||||
switch (role)
|
||||
{
|
||||
case Material::Primary:
|
||||
|
@ -120,6 +121,7 @@ void RaisedButton::setRole(Material::Role role)
|
|||
}
|
||||
d->delegate->assignProperties();
|
||||
update();
|
||||
*/
|
||||
}
|
||||
|
||||
bool RaisedButton::event(QEvent *event)
|
||||
|
|
|
@ -25,6 +25,7 @@ void TogglePrivate::init()
|
|||
ripple = new RippleOverlay(q->parentWidget());
|
||||
|
||||
q->setCheckable(true);
|
||||
q->setChecked(false);
|
||||
q->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
|
||||
|
||||
QState *offState = new QState;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <QPainter>
|
||||
#include <QEvent>
|
||||
#include <QGraphicsDropShadowEffect>
|
||||
#include <QDebug>
|
||||
#include "lib/style.h"
|
||||
#include "toggle_internal.h"
|
||||
#include "toggle.h"
|
||||
|
@ -84,6 +85,7 @@ void ToggleThumb::mouseReleaseEvent(QMouseEvent *event)
|
|||
if (_toggle->isEnabled()) {
|
||||
const bool newChecked = !_toggle->isChecked();
|
||||
_toggle->setChecked(newChecked);
|
||||
qDebug() << "checked : " << newChecked;
|
||||
emit clicked(newChecked);
|
||||
}
|
||||
QWidget::mouseReleaseEvent(event);
|
||||
|
|
Loading…
Reference in New Issue