diff --git a/components/textfield.cpp b/components/textfield.cpp index 49efe06..07e59e9 100644 --- a/components/textfield.cpp +++ b/components/textfield.cpp @@ -1,18 +1,55 @@ +#include #include #include +#include #include "textfield.h" #include "style.h" TextField::TextField(QWidget *parent) - : QLineEdit(parent) + : QLineEdit(parent), + _animation(new QPropertyAnimation(this)), + _progress(1) { setStyle(&Style::instance()); + + _animation->setPropertyName("progress"); + _animation->setTargetObject(this); + _animation->setEasingCurve(QEasingCurve::InCubic); + _animation->setDuration(350); + _animation->setStartValue(1); + _animation->setEndValue(0); } TextField::~TextField() { } +void TextField::setProgress(qreal progress) +{ + if (_progress == progress) + return; + _progress = progress; + + emit progressChanged(progress); + update(); +} + +void TextField::focusInEvent(QFocusEvent *event) +{ + _animation->setDirection(QAbstractAnimation::Forward); + _animation->start(); + + QLineEdit::focusInEvent(event); +} + +void TextField::focusOutEvent(QFocusEvent *event) +{ + _animation->setDirection(QAbstractAnimation::Backward); + _animation->start(); + + QLineEdit::focusOutEvent(event); +} + void TextField::mousePressEvent(QMouseEvent *event) { QLineEdit::mousePressEvent(event); @@ -28,5 +65,17 @@ void TextField::paintEvent(QPaintEvent *event) QLineEdit::paintEvent(event); QPainter painter(this); - painter.drawRect(0, 0, 50, 50); + + QBrush brush; + brush.setStyle(Qt::SolidPattern); + + if (!qFuzzyCompare(1, _progress)) { + + painter.setPen(Qt::NoPen); + painter.setBrush(brush); + + int w = _progress*static_cast(width()/2); + + painter.drawRect(w, height()-2, width()-w*2, 2); + } } diff --git a/components/textfield.h b/components/textfield.h index 3831f87..72a38f0 100644 --- a/components/textfield.h +++ b/components/textfield.h @@ -3,18 +3,34 @@ #include +class QPropertyAnimation; + class TextField : public QLineEdit { Q_OBJECT + Q_PROPERTY(qreal progress WRITE setProgress READ progress NOTIFY progressChanged) + public: explicit TextField(QWidget *parent = 0); ~TextField(); + void setProgress(qreal progress); + inline qreal progress() const { return _progress; } + +signals: + void progressChanged(qreal); + protected: + void focusInEvent(QFocusEvent *event) Q_DECL_OVERRIDE; + void focusOutEvent(QFocusEvent *event) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + +private: + QPropertyAnimation *const _animation; + qreal _progress; }; #endif // TEXTFIELD_H