qt-material-widgets/components/textfield_internal.cpp

154 lines
4.0 KiB
C++
Raw Normal View History

#include "textfield_internal.h"
2016-06-14 17:29:20 +00:00
#include <QEventTransition>
#include <QPropertyAnimation>
2016-06-16 16:08:54 +00:00
#include <QPainter>
2016-06-14 17:29:20 +00:00
#include <QDebug>
2016-06-14 15:37:41 +00:00
#include "textfield.h"
TextFieldStateMachine::TextFieldStateMachine(TextField *parent)
: QStateMachine(parent),
2016-06-14 17:29:20 +00:00
textField(parent),
2016-06-16 16:08:54 +00:00
_normalState(new QState),
_focusedState(new QState),
2016-06-21 12:42:49 +00:00
_label(0),
_offsetAnimation(0),
_colorAnimation(0),
2016-06-14 17:29:20 +00:00
_progress(0)
2016-06-14 15:37:41 +00:00
{
2016-06-16 16:08:54 +00:00
addState(_normalState);
addState(_focusedState);
2016-06-14 17:29:20 +00:00
2016-06-16 16:08:54 +00:00
setInitialState(_normalState);
2016-06-14 17:29:20 +00:00
QEventTransition *transition;
QPropertyAnimation *animation;
transition = new QEventTransition(parent, QEvent::FocusIn);
2016-06-16 16:08:54 +00:00
transition->setTargetState(_focusedState);
_normalState->addTransition(transition);
2016-06-14 17:29:20 +00:00
2016-06-21 12:42:49 +00:00
animation = new QPropertyAnimation(this, "progress", this);
2016-06-16 16:08:54 +00:00
animation->setEasingCurve(QEasingCurve::InCubic);
animation->setDuration(340);
transition->addAnimation(animation);
2016-06-14 17:29:20 +00:00
2016-06-16 16:08:54 +00:00
transition = new QEventTransition(parent, QEvent::FocusOut);
transition->setTargetState(_normalState);
_focusedState->addTransition(transition);
2016-06-14 17:29:20 +00:00
2016-06-21 12:42:49 +00:00
animation = new QPropertyAnimation(this, "progress", this);
2016-06-16 16:08:54 +00:00
animation->setEasingCurve(QEasingCurve::OutCubic);
2016-06-14 17:29:20 +00:00
animation->setDuration(340);
2016-06-16 16:08:54 +00:00
transition->addAnimation(animation);
_normalState->assignProperty(this, "progress", 0);
_focusedState->assignProperty(this, "progress", 1);
assignProperties();
connect(textField, SIGNAL(textChanged(QString)), this, SLOT(assignProperties()));
2016-06-14 15:37:41 +00:00
}
TextFieldStateMachine::~TextFieldStateMachine()
{
}
2016-06-14 17:29:20 +00:00
2016-06-16 16:08:54 +00:00
void TextFieldStateMachine::setLabel(TextFieldLabel *widget)
{
2016-06-21 12:42:49 +00:00
if (_label) {
delete _label;
}
if (_offsetAnimation) {
removeDefaultAnimation(_offsetAnimation);
delete _offsetAnimation;
}
if (_colorAnimation) {
removeDefaultAnimation(_colorAnimation);
delete _colorAnimation;
}
2016-06-16 16:08:54 +00:00
2016-06-21 12:42:49 +00:00
_label = widget;
2016-06-16 16:08:54 +00:00
2016-06-21 12:42:49 +00:00
if (_label)
{
_offsetAnimation = new QPropertyAnimation(_label, "offset", this);
_offsetAnimation->setDuration(210);
_offsetAnimation->setEasingCurve(QEasingCurve::OutCubic);
addDefaultAnimation(_offsetAnimation);
_colorAnimation = new QPropertyAnimation(_label, "color", this);
_colorAnimation->setDuration(210);
addDefaultAnimation(_colorAnimation);
2016-06-16 16:08:54 +00:00
}
assignProperties();
}
2016-06-14 17:29:20 +00:00
void TextFieldStateMachine::setProgress(qreal progress)
{
_progress = progress;
textField->update();
}
2016-06-16 16:08:54 +00:00
void TextFieldStateMachine::assignProperties()
{
QPalette p;
p.setColor(QPalette::Normal, QPalette::Base, textField->backgroundColor());
textField->setPalette(p);
2016-06-21 12:42:49 +00:00
if (_label)
2016-06-16 16:08:54 +00:00
{
const int m = textField->textMargins().top();
2016-06-21 12:42:49 +00:00
_focusedState->assignProperty(_label, "offset", QPointF(0, 0-m));
2016-06-16 16:08:54 +00:00
if (textField->text().isEmpty()) {
2016-06-21 12:42:49 +00:00
_normalState->assignProperty(_label, "offset", QPointF(0, 26));
2016-06-16 16:08:54 +00:00
} else {
2016-06-21 12:42:49 +00:00
_normalState->assignProperty(_label, "offset", QPointF(0, 0-m));
2016-06-16 16:08:54 +00:00
}
2016-06-21 12:42:49 +00:00
_focusedState->assignProperty(_label, "color", textField->inkColor());
_normalState->assignProperty(_label, "color", textField->hintColor());
2016-06-16 16:08:54 +00:00
}
}
TextFieldLabel::TextFieldLabel(TextField *parent)
: QWidget(parent),
label(parent),
_scale(1),
_posX(0),
_posY(26),
_color(parent->hintColor())
{
QFont f;
f.setPointSizeF(parent->labelFontSize());
f.setStyleName("Medium");
2016-06-19 09:16:44 +00:00
f.setLetterSpacing(QFont::PercentageSpacing, 102);
2016-06-16 16:08:54 +00:00
setFont(f);
}
TextFieldLabel::~TextFieldLabel()
{
}
void TextFieldLabel::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.scale(_scale, _scale);
painter.setPen(_color);
painter.setOpacity(1);
QPointF pos(2 + _posX, height()-36 + _posY);
painter.drawText(pos.x(), pos.y(), label->label());
#ifdef DEBUG_LAYOUT
painter.setPen(Qt::red);
painter.setBrush(Qt::NoBrush);
painter.drawRect(rect());
#endif
}