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) }
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);
// { // {
// setStyle(&Style::instance()); // painter.drawText(0, height()/2, text());
//
// _animation->setPropertyName("progress");
// _animation->setTargetObject(this);
// _animation->setEasingCurve(QEasingCurve::InCubic);
// _animation->setDuration(350);
// _animation->setStartValue(1);
// _animation->setEndValue(0);
// } // }
//
//TextField::~TextField() //QBrush bgBrush;
//{ //bgBrush.setStyle(Qt::SolidPattern);
//} //bgBrush.setColor(palette().color(QPalette::Window));
// //painter.fillRect(rect(), bgBrush);
//void TextField::setProgress(qreal progress)
//{ const int y = height()-1;
// if (_progress == progress) painter.drawLine(0, y, width(), y);
// return;
// _progress = progress; QBrush brush;
// brush.setStyle(Qt::SolidPattern);
// emit progressChanged(progress);
// update(); const qreal progress = d->machine->progress();
//}
// if (progress > 0) {
//void TextField::focusInEvent(QFocusEvent *event)
//{ painter.setPen(Qt::NoPen);
// _animation->setDirection(QAbstractAnimation::Forward); painter.setBrush(brush);
// _animation->start();
// int w = (1-progress)*static_cast<qreal>(width()/2);
// QLineEdit::focusInEvent(event);
//} painter.drawRect(w, height()-2, width()-w*2, 2);
//
//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);
// }
//}
//

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