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 "textfield.h"
#include <QPainter>
#include <QApplication>
#include "textfield_p.h" #include "textfield_p.h"
#include "textfield_internal.h"
TextFieldPrivate::TextFieldPrivate(TextField *q) TextFieldPrivate::TextFieldPrivate(TextField *q)
: q_ptr(q) : q_ptr(q)
{ {
q->setFrame(false); q->setFrame(false);
q->setTextMargins(0, 1, 0, 1);
} }
void TextFieldPrivate::init() void TextFieldPrivate::init()
{ {
Q_Q(TextField);
machine = new TextFieldStateMachine(q);
machine->start();
QCoreApplication::processEvents();
} }
TextField::TextField(QWidget *parent) TextField::TextField(QWidget *parent)
@ -16,91 +27,86 @@ TextField::TextField(QWidget *parent)
d_ptr(new TextFieldPrivate(this)) d_ptr(new TextFieldPrivate(this))
{ {
d_func()->init(); 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() TextField::~TextField()
{ {
} }
//#include <QPropertyAnimation> void TextField::setTextColor(const QColor &color)
//#include <QWidget> {
//#include <QPainter> }
//#include <QDebug>
//#include "textfield.h" QColor TextField::textColor() const
//#include "lib/style.h" {
// }
//TextField::TextField(QWidget *parent)
// : QLineEdit(parent), void TextField::setBackgroundColor(const QColor &color)
// _animation(new QPropertyAnimation(this)), {
// _progress(1) }
//{
// setStyle(&Style::instance()); QColor TextField::backgroundColor() const
// {
// _animation->setPropertyName("progress"); }
// _animation->setTargetObject(this);
// _animation->setEasingCurve(QEasingCurve::InCubic); void TextField::setInkColor(const QColor &color)
// _animation->setDuration(350); {
// _animation->setStartValue(1); }
// _animation->setEndValue(0);
//} QColor TextField::inkColor() const
// {
//TextField::~TextField() }
//{
//} void TextField::setUnderlineColor(const QColor &color)
// {
//void TextField::setProgress(qreal progress) }
//{
// if (_progress == progress) QColor TextField::underlineColor() const
// return; {
// _progress = progress; }
//
// emit progressChanged(progress); void TextField::paintEvent(QPaintEvent *event)
// update(); {
//} Q_D(TextField);
//
//void TextField::focusInEvent(QFocusEvent *event) QLineEdit::paintEvent(event);
//{
// _animation->setDirection(QAbstractAnimation::Forward); QPainter painter(this);
// _animation->start();
// // {
// QLineEdit::focusInEvent(event); // painter.drawText(0, height()/2, text());
//}
//
//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);
// } // }
//}
// //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_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: public:
explicit TextField(QWidget *parent = 0); explicit TextField(QWidget *parent = 0);
~TextField(); ~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: protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
const QScopedPointer<TextFieldPrivate> d_ptr; const QScopedPointer<TextFieldPrivate> d_ptr;
private: private:
@ -21,36 +40,4 @@ private:
Q_DECLARE_PRIVATE(TextField) 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 #endif // TEXTFIELD_H

View File

@ -1,12 +1,48 @@
#include "textfield_internal.h" #include "textfield_internal.h"
#include <QEventTransition>
#include <QPropertyAnimation>
#include <QDebug>
#include "textfield.h" #include "textfield.h"
TextFieldStateMachine::TextFieldStateMachine(TextField *parent) TextFieldStateMachine::TextFieldStateMachine(TextField *parent)
: QStateMachine(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() TextFieldStateMachine::~TextFieldStateMachine()
{ {
} }
void TextFieldStateMachine::setProgress(qreal progress)
{
_progress = progress;
textField->update();
}

View File

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

View File

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