add toggle color properties
This commit is contained in:
parent
6bfbc342b0
commit
0d270024b8
|
@ -3,6 +3,7 @@
|
|||
#include <QState>
|
||||
#include <QSignalTransition>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QApplication>
|
||||
#include <QDebug>
|
||||
#include "lib/rippleoverlay.h"
|
||||
#include "lib/ripple.h"
|
||||
|
@ -14,7 +15,10 @@ TogglePrivate::TogglePrivate(Toggle *q)
|
|||
: q_ptr(q),
|
||||
track(new ToggleTrack(q)),
|
||||
thumb(new ToggleThumb(q)),
|
||||
orientation(Qt::Horizontal)
|
||||
offState(new QState),
|
||||
onState(new QState),
|
||||
orientation(Qt::Horizontal),
|
||||
useThemeColors(true)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -28,9 +32,6 @@ void TogglePrivate::init()
|
|||
q->setChecked(false);
|
||||
q->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
|
||||
|
||||
QState *offState = new QState;
|
||||
QState *onState = new QState;
|
||||
|
||||
machine.addState(offState);
|
||||
machine.addState(onState);
|
||||
|
||||
|
@ -91,26 +92,29 @@ void TogglePrivate::init()
|
|||
|
||||
//
|
||||
|
||||
const Style &style = Style::instance();
|
||||
|
||||
offState->assignProperty(thumb, "shift", 0);
|
||||
onState->assignProperty(thumb, "shift", 1);
|
||||
|
||||
QColor trackOnColor = style.themeColor("primary1");
|
||||
trackOnColor.setAlpha(100);
|
||||
//
|
||||
|
||||
QColor trackOffColor = style.themeColor("accent3");
|
||||
trackOffColor.setAlpha(170);
|
||||
updatePalette();
|
||||
|
||||
offState->assignProperty(track, "trackColor", trackOffColor);
|
||||
onState->assignProperty(track, "trackColor", trackOnColor);
|
||||
|
||||
offState->assignProperty(thumb, "thumbColor", style.themeColor("canvas"));
|
||||
onState->assignProperty(thumb, "thumbColor", style.themeColor("primary1"));
|
||||
QObject::connect(q, SIGNAL(toggled(bool)), q, SLOT(addRipple()));
|
||||
|
||||
machine.start();
|
||||
|
||||
QObject::connect(q, SIGNAL(toggled(bool)), q, SLOT(addRipple()));
|
||||
QCoreApplication::processEvents();
|
||||
}
|
||||
|
||||
void TogglePrivate::updatePalette()
|
||||
{
|
||||
Q_Q(Toggle);
|
||||
|
||||
offState->assignProperty(track, "trackColor", q->trackColor().lighter(110));
|
||||
onState->assignProperty(track, "trackColor", q->activeColor().lighter(110));
|
||||
|
||||
offState->assignProperty(thumb, "thumbColor", q->inactiveColor());
|
||||
onState->assignProperty(thumb, "thumbColor", q->activeColor());
|
||||
}
|
||||
|
||||
Toggle::Toggle(QWidget *parent)
|
||||
|
@ -124,6 +128,99 @@ Toggle::~Toggle()
|
|||
{
|
||||
}
|
||||
|
||||
void Toggle::setUseThemeColors(bool value)
|
||||
{
|
||||
Q_D(Toggle);
|
||||
|
||||
d->useThemeColors = value;
|
||||
d->updatePalette();
|
||||
}
|
||||
|
||||
bool Toggle::useThemeColors() const
|
||||
{
|
||||
Q_D(const Toggle);
|
||||
|
||||
return d->useThemeColors;
|
||||
}
|
||||
|
||||
void Toggle::setDisabledColor(const QColor &color)
|
||||
{
|
||||
Q_D(Toggle);
|
||||
|
||||
d->disabledColor = color;
|
||||
setUseThemeColors(false);
|
||||
}
|
||||
|
||||
QColor Toggle::disabledColor() const
|
||||
{
|
||||
Q_D(const Toggle);
|
||||
|
||||
if (d->useThemeColors || !d->disabledColor.isValid()) {
|
||||
QColor color = Style::instance().themeColor("disabled");
|
||||
color.setAlpha(30);
|
||||
return color;
|
||||
} else {
|
||||
return d->disabledColor;
|
||||
}
|
||||
}
|
||||
|
||||
void Toggle::setActiveColor(const QColor &color)
|
||||
{
|
||||
Q_D(Toggle);
|
||||
|
||||
d->activeColor = color;
|
||||
setUseThemeColors(false);
|
||||
}
|
||||
|
||||
QColor Toggle::activeColor() const
|
||||
{
|
||||
Q_D(const Toggle);
|
||||
|
||||
if (d->useThemeColors || !d->activeColor.isValid()) {
|
||||
return Style::instance().themeColor("primary1");
|
||||
} else {
|
||||
return d->activeColor;
|
||||
}
|
||||
}
|
||||
|
||||
void Toggle::setInactiveColor(const QColor &color)
|
||||
{
|
||||
Q_D(Toggle);
|
||||
|
||||
d->inactiveColor = color;
|
||||
setUseThemeColors(false);
|
||||
}
|
||||
|
||||
QColor Toggle::inactiveColor() const
|
||||
{
|
||||
Q_D(const Toggle);
|
||||
|
||||
if (d->useThemeColors || !d->inactiveColor.isValid()) {
|
||||
return Style::instance().themeColor("canvas");
|
||||
} else {
|
||||
return d->inactiveColor;
|
||||
}
|
||||
}
|
||||
|
||||
void Toggle::setTrackColor(const QColor &color)
|
||||
{
|
||||
Q_D(Toggle);
|
||||
|
||||
d->trackColor = color;
|
||||
setUseThemeColors(false);
|
||||
}
|
||||
|
||||
QColor Toggle::trackColor() const
|
||||
{
|
||||
Q_D(const Toggle);
|
||||
|
||||
if (d->useThemeColors || !d->trackColor.isValid()) {
|
||||
return Style::instance().themeColor("accent3");
|
||||
} else {
|
||||
return d->trackColor;
|
||||
}
|
||||
}
|
||||
|
||||
QSize Toggle::sizeHint() const
|
||||
{
|
||||
Q_D(const Toggle);
|
||||
|
@ -176,9 +273,7 @@ void Toggle::addRipple()
|
|||
}
|
||||
|
||||
Ripple *ripple = new Ripple(QPoint(10+t, 20+t));
|
||||
ripple->setColor(Style::instance().themeColor(isChecked()
|
||||
? "primary2"
|
||||
: "accent3"));
|
||||
ripple->setColor(isChecked() ? activeColor() : trackColor());
|
||||
ripple->setRadiusEndValue(w);
|
||||
ripple->setOpacityStartValue(0.4);
|
||||
|
||||
|
|
|
@ -9,10 +9,30 @@ class Toggle : public QAbstractButton
|
|||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QColor disabledColor WRITE setDisabledColor READ disabledColor)
|
||||
Q_PROPERTY(QColor activeColor WRITE setActiveColor READ activeColor)
|
||||
Q_PROPERTY(QColor inactiveColor WRITE setInactiveColor READ inactiveColor)
|
||||
Q_PROPERTY(QColor trackColor WRITE setTrackColor READ trackColor)
|
||||
|
||||
public:
|
||||
explicit Toggle(QWidget *parent = 0);
|
||||
~Toggle();
|
||||
|
||||
void setUseThemeColors(bool value);
|
||||
bool useThemeColors() const;
|
||||
|
||||
void setDisabledColor(const QColor &color);
|
||||
QColor disabledColor() const;
|
||||
|
||||
void setActiveColor(const QColor &color);
|
||||
QColor activeColor() const;
|
||||
|
||||
void setInactiveColor(const QColor &color);
|
||||
QColor inactiveColor() const;
|
||||
|
||||
void setTrackColor(const QColor &color);
|
||||
QColor trackColor() const;
|
||||
|
||||
QSize sizeHint() const;
|
||||
|
||||
Qt::Orientation orientation() const;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
#include <QPainter>
|
||||
#include <QEvent>
|
||||
#include <QGraphicsDropShadowEffect>
|
||||
#include <QDebug>
|
||||
#include "lib/style.h"
|
||||
#include "toggle_internal.h"
|
||||
#include "toggle.h"
|
||||
|
@ -33,13 +32,7 @@ void ToggleThumb::setShift(qreal shift)
|
|||
return;
|
||||
|
||||
_shift = shift;
|
||||
|
||||
const QSize s(Qt::Horizontal == _toggle->orientation()
|
||||
? size() : size().transposed());
|
||||
|
||||
_offset = shift*static_cast<qreal>(s.width()-s.height());
|
||||
_toggle->updateOverlayGeometry();
|
||||
update();
|
||||
updateOffset();
|
||||
}
|
||||
|
||||
bool ToggleThumb::eventFilter(QObject *obj, QEvent *event)
|
||||
|
@ -47,6 +40,7 @@ bool ToggleThumb::eventFilter(QObject *obj, QEvent *event)
|
|||
const QEvent::Type type = event->type();
|
||||
if (QEvent::Resize == type || QEvent::Move == type) {
|
||||
setGeometry(parentWidget()->rect().adjusted(8, 8, -8, -8));
|
||||
updateOffset();
|
||||
}
|
||||
return QWidget::eventFilter(obj, event);
|
||||
}
|
||||
|
@ -60,22 +54,39 @@ void ToggleThumb::paintEvent(QPaintEvent *event)
|
|||
|
||||
QBrush brush;
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
if (_toggle->isEnabled()) {
|
||||
brush.setColor(_thumbColor);
|
||||
} else {
|
||||
QColor disabledColor = Style::instance().themeColor("accent3");
|
||||
brush.setColor(disabledColor.lighter(140));
|
||||
}
|
||||
brush.setColor(_toggle->isEnabled() ? _thumbColor : Qt::white);
|
||||
|
||||
painter.setBrush(brush);
|
||||
painter.setPen(Qt::NoPen);
|
||||
|
||||
int s;
|
||||
QRectF r;
|
||||
|
||||
if (Qt::Horizontal == _toggle->orientation()) {
|
||||
const int s = height()-10;
|
||||
painter.drawEllipse(QRectF(5+_offset, 5, s, s));
|
||||
s = height()-10;
|
||||
r = QRectF(5+_offset, 5, s, s);
|
||||
} else {
|
||||
const int s = width()-10;
|
||||
painter.drawEllipse(QRectF(5, 5+_offset, s, s));
|
||||
s = width()-10;
|
||||
r = QRectF(5, 5+_offset, s, s);
|
||||
}
|
||||
|
||||
painter.drawEllipse(r);
|
||||
|
||||
if (!_toggle->isEnabled()) {
|
||||
brush.setColor(_toggle->disabledColor());
|
||||
painter.setBrush(brush);
|
||||
painter.drawEllipse(r);
|
||||
}
|
||||
}
|
||||
|
||||
void ToggleThumb::updateOffset()
|
||||
{
|
||||
const QSize s(Qt::Horizontal == _toggle->orientation()
|
||||
? size() : size().transposed());
|
||||
_offset = shift()*static_cast<qreal>(s.width()-s.height());
|
||||
|
||||
_toggle->updateOverlayGeometry();
|
||||
update();
|
||||
}
|
||||
|
||||
ToggleTrack::ToggleTrack(Toggle *parent)
|
||||
|
@ -108,14 +119,14 @@ void ToggleTrack::paintEvent(QPaintEvent *event)
|
|||
QBrush brush;
|
||||
if (_toggle->isEnabled()) {
|
||||
brush.setColor(_trackColor);
|
||||
painter.setOpacity(0.8);
|
||||
} else {
|
||||
QColor disabledColor = _trackColor;
|
||||
disabledColor.setAlpha(80);
|
||||
QColor disabledColor(Style::instance().themeColor("disabled"));
|
||||
brush.setColor(disabledColor);
|
||||
painter.setOpacity(0.6);
|
||||
}
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
painter.setBrush(brush);
|
||||
|
||||
painter.setPen(Qt::NoPen);
|
||||
|
||||
if (Qt::Horizontal == _toggle->orientation()) {
|
||||
|
|
|
@ -39,9 +39,11 @@ protected:
|
|||
private:
|
||||
Q_DISABLE_COPY(ToggleThumb)
|
||||
|
||||
void updateOffset();
|
||||
|
||||
Toggle *const _toggle;
|
||||
qreal _shift;
|
||||
qreal _offset;
|
||||
qreal _shift;
|
||||
qreal _offset;
|
||||
QColor _thumbColor;
|
||||
};
|
||||
|
||||
|
|
|
@ -18,12 +18,21 @@ public:
|
|||
|
||||
void init();
|
||||
|
||||
Toggle *const q_ptr;
|
||||
ToggleTrack *const track;
|
||||
ToggleThumb *const thumb;
|
||||
RippleOverlay *ripple;
|
||||
QStateMachine machine;
|
||||
void updatePalette();
|
||||
|
||||
Toggle *const q_ptr;
|
||||
ToggleTrack *const track;
|
||||
ToggleThumb *const thumb;
|
||||
QState *const offState;
|
||||
QState *const onState;
|
||||
RippleOverlay *ripple;
|
||||
QStateMachine machine;
|
||||
Qt::Orientation orientation;
|
||||
QColor disabledColor;
|
||||
QColor activeColor;
|
||||
QColor inactiveColor;
|
||||
QColor trackColor;
|
||||
bool useThemeColors;
|
||||
};
|
||||
|
||||
#endif // TOGGLE_P_H
|
||||
|
|
|
@ -164,6 +164,22 @@ FlatButtonExamples::FlatButtonExamples(QWidget *parent)
|
|||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
FlatButton *flatButton = new FlatButton;
|
||||
flatButton->setText("Press me!");
|
||||
flatButton->setDisabled(true);
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(flatButton);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
""
|
||||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
QPushButton *flatButton = new QPushButton;
|
||||
flatButton->setText("Press me!");
|
||||
|
|
|
@ -41,6 +41,25 @@ ToggleExamples::ToggleExamples(QWidget *parent)
|
|||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
Toggle *toggle = new Toggle;
|
||||
toggle->setOrientation(Qt::Vertical);
|
||||
|
||||
toggle->setChecked(true);
|
||||
toggle->setDisabled(true);
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(toggle);
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
"Toggle *toggle = new Toggle;\n"
|
||||
"toggle->setOrientation(Qt::Vertical);"
|
||||
);
|
||||
frame->setWidget(view);
|
||||
|
||||
layout->addWidget(frame);
|
||||
}
|
||||
{
|
||||
Toggle *toggle = new Toggle;
|
||||
toggle->setFixedSize(200, 140);
|
||||
|
|
Loading…
Reference in New Issue