create animation on TextField

This commit is contained in:
laserpants 2016-04-14 18:15:17 +03:00
parent 800f5d4d33
commit a6038ff9f2
2 changed files with 67 additions and 2 deletions

View File

@ -1,18 +1,55 @@
#include <QPropertyAnimation>
#include <QWidget>
#include <QPainter>
#include <QDebug>
#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<qreal>(width()/2);
painter.drawRect(w, height()-2, width()-w*2, 2);
}
}

View File

@ -3,18 +3,34 @@
#include <QLineEdit>
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