add Text Field properties

This commit is contained in:
laserpants 2016-06-14 20:29:20 +03:00
parent 8581180fbc
commit 9a544efa2c
5 changed files with 155 additions and 114 deletions

View File

@ -1,14 +1,25 @@
#include "textfield.h"
#include <QPainter>
#include <QApplication>
#include "textfield_p.h"
#include "textfield_internal.h"
TextFieldPrivate::TextFieldPrivate(TextField *q)
: q_ptr(q)
{
q->setFrame(false);
q->setTextMargins(0, 1, 0, 1);
}
void TextFieldPrivate::init()
{
Q_Q(TextField);
machine = new TextFieldStateMachine(q);
machine->start();
QCoreApplication::processEvents();
}
TextField::TextField(QWidget *parent)
@ -16,91 +27,86 @@ TextField::TextField(QWidget *parent)
d_ptr(new TextFieldPrivate(this))
{
d_func()->init();
//
setPlaceholderText("This is a placeholder");
QPalette p;
p.setColor(QPalette::Normal, QPalette::Base, p.color(QPalette::Window));
// p.setColor(QPalette::Normal, QPalette::Text, Qt::blue);
setPalette(p);
}
TextField::~TextField()
{
}
//#include <QPropertyAnimation>
//#include <QWidget>
//#include <QPainter>
//#include <QDebug>
//#include "textfield.h"
//#include "lib/style.h"
//
//TextField::TextField(QWidget *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);
//}
//
//void TextField::mouseReleaseEvent(QMouseEvent *event)
//{
// QLineEdit::mouseReleaseEvent(event);
//}
//
//void TextField::paintEvent(QPaintEvent *event)
//{
// QLineEdit::paintEvent(event);
//
// QPainter painter(this);
//
// 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);
void TextField::setTextColor(const QColor &color)
{
}
QColor TextField::textColor() const
{
}
void TextField::setBackgroundColor(const QColor &color)
{
}
QColor TextField::backgroundColor() const
{
}
void TextField::setInkColor(const QColor &color)
{
}
QColor TextField::inkColor() const
{
}
void TextField::setUnderlineColor(const QColor &color)
{
}
QColor TextField::underlineColor() const
{
}
void TextField::paintEvent(QPaintEvent *event)
{
Q_D(TextField);
QLineEdit::paintEvent(event);
QPainter painter(this);
// {
// painter.drawText(0, height()/2, text());
// }
//}
//
//QBrush bgBrush;
//bgBrush.setStyle(Qt::SolidPattern);
//bgBrush.setColor(palette().color(QPalette::Window));
//painter.fillRect(rect(), bgBrush);
const int y = height()-1;
painter.drawLine(0, y, width(), y);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
const qreal progress = d->machine->progress();
if (progress > 0) {
painter.setPen(Qt::NoPen);
painter.setBrush(brush);
int w = (1-progress)*static_cast<qreal>(width()/2);
painter.drawRect(w, height()-2, width()-w*2, 2);
}
}

View File

@ -9,11 +9,30 @@ class TextField : public QLineEdit
{
Q_OBJECT
Q_PROPERTY(QColor textColor WRITE setTextColor READ progress NOTIFY textColor)
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ progress NOTIFY backgroundColor)
Q_PROPERTY(QColor inkColor WRITE setInkColor READ progress NOTIFY inkColor)
Q_PROPERTY(QColor underlineColor WRITE setUnderlineColor READ progress NOTIFY underlineColor)
public:
explicit TextField(QWidget *parent = 0);
~TextField();
void setTextColor(const QColor &color);
QColor textColor() const;
void setBackgroundColor(const QColor &color);
QColor backgroundColor() const;
void setInkColor(const QColor &color);
QColor inkColor() const;
void setUnderlineColor(const QColor &color);
QColor underlineColor() const;
protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
const QScopedPointer<TextFieldPrivate> d_ptr;
private:
@ -21,36 +40,4 @@ private:
Q_DECLARE_PRIVATE(TextField)
};
//#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

View File

@ -1,12 +1,48 @@
#include "textfield_internal.h"
#include <QEventTransition>
#include <QPropertyAnimation>
#include <QDebug>
#include "textfield.h"
TextFieldStateMachine::TextFieldStateMachine(TextField *parent)
: QStateMachine(parent),
textField(parent)
textField(parent),
_progress(0)
{
QState *normalState = new QState;
QState *focusedState = new QState;
addState(normalState);
addState(focusedState);
setInitialState(normalState);
QEventTransition *transition;
QPropertyAnimation *animation;
transition = new QEventTransition(parent, QEvent::FocusIn);
transition->setTargetState(focusedState);
normalState->addTransition(transition);
transition = new QEventTransition(parent, QEvent::FocusOut);
transition->setTargetState(normalState);
focusedState->addTransition(transition);
normalState->assignProperty(this, "progress", 0);
focusedState->assignProperty(this, "progress", 1);
animation = new QPropertyAnimation(this, "progress");
animation->setEasingCurve(QEasingCurve::InCubic);
animation->setDuration(340);
addDefaultAnimation(animation);
}
TextFieldStateMachine::~TextFieldStateMachine()
{
}
void TextFieldStateMachine::setProgress(qreal progress)
{
_progress = progress;
textField->update();
}

View File

@ -9,14 +9,20 @@ class TextFieldStateMachine : public QStateMachine
{
Q_OBJECT
Q_PROPERTY(qreal progress WRITE setProgress READ progress)
public:
TextFieldStateMachine(TextField *parent);
~TextFieldStateMachine();
void setProgress(qreal progress);
inline qreal progress() const { return _progress; }
private:
Q_DISABLE_COPY(TextFieldStateMachine)
TextField *const textField;
qreal _progress;
};
#endif // TEXTFIELD_INTERNAL_H

View File

@ -4,6 +4,7 @@
#include <QObject>
class TextField;
class TextFieldStateMachine;
class TextFieldPrivate
{
@ -15,6 +16,11 @@ public:
void init();
TextField *const q_ptr;
TextFieldStateMachine *machine;
QColor textColor;
QColor backgroundColor;
QColor inkColor;
QColor underlineColor;
};
#endif // TEXTFIELD_P_H