qt-material-widgets/components/textfield.cpp

82 lines
1.7 KiB
C++
Raw Normal View History

2016-04-14 15:15:17 +00:00
#include <QPropertyAnimation>
2016-04-14 13:47:27 +00:00
#include <QWidget>
#include <QPainter>
2016-04-14 15:15:17 +00:00
#include <QDebug>
2016-03-19 08:32:49 +00:00
#include "textfield.h"
2016-04-30 09:53:58 +00:00
#include "lib/style.h"
2016-04-14 13:47:27 +00:00
TextField::TextField(QWidget *parent)
2016-04-14 15:15:17 +00:00
: QLineEdit(parent),
_animation(new QPropertyAnimation(this)),
_progress(1)
2016-04-14 13:47:27 +00:00
{
2016-04-14 14:11:41 +00:00
setStyle(&Style::instance());
2016-04-14 15:15:17 +00:00
_animation->setPropertyName("progress");
_animation->setTargetObject(this);
_animation->setEasingCurve(QEasingCurve::InCubic);
_animation->setDuration(350);
_animation->setStartValue(1);
_animation->setEndValue(0);
2016-04-14 13:47:27 +00:00
}
TextField::~TextField()
{
}
2016-04-14 15:15:17 +00:00
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);
}
2016-04-14 13:47:27 +00:00
void TextField::mousePressEvent(QMouseEvent *event)
{
2016-04-14 14:11:41 +00:00
QLineEdit::mousePressEvent(event);
2016-04-14 13:47:27 +00:00
}
void TextField::mouseReleaseEvent(QMouseEvent *event)
{
2016-04-14 14:11:41 +00:00
QLineEdit::mouseReleaseEvent(event);
2016-04-14 13:47:27 +00:00
}
void TextField::paintEvent(QPaintEvent *event)
{
2016-04-14 14:11:41 +00:00
QLineEdit::paintEvent(event);
2016-04-14 13:47:27 +00:00
2016-04-14 14:11:41 +00:00
QPainter painter(this);
2016-04-14 15:15:17 +00:00
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);
}
2016-04-14 13:47:27 +00:00
}