matrixion/include/ui/TextField.h

176 lines
3.3 KiB
C
Raw Normal View History

#pragma once
2017-04-06 02:06:42 +03:00
#include <QColor>
#include <QLineEdit>
#include <QPaintEvent>
#include <QPropertyAnimation>
#include <QStateMachine>
#include <QtGlobal>
class TextField;
class TextFieldLabel;
class TextFieldStateMachine;
class TextField : public QLineEdit
{
Q_OBJECT
Q_PROPERTY(QColor textColor WRITE setTextColor READ textColor)
Q_PROPERTY(QColor inkColor WRITE setInkColor READ inkColor)
Q_PROPERTY(QColor underlineColor WRITE setUnderlineColor READ underlineColor)
public:
explicit TextField(QWidget *parent = 0);
~TextField();
void setInkColor(const QColor &color);
void setBackgroundColor(const QColor &color);
void setLabel(const QString &label);
void setLabelColor(const QColor &color);
void setLabelFontSize(qreal size);
void setShowLabel(bool value);
void setTextColor(const QColor &color);
void setUnderlineColor(const QColor &color);
QColor inkColor() const;
QColor labelColor() const;
QColor textColor() const;
QColor underlineColor() const;
QColor backgroundColor() const;
QString label() const;
bool hasLabel() const;
qreal labelFontSize() const;
protected:
bool event(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
private:
void init();
QColor ink_color_;
QColor background_color_;
QColor label_color_;
QColor text_color_;
QColor underline_color_;
QString label_text_;
TextFieldLabel *label_;
TextFieldStateMachine *state_machine_;
bool show_label_;
qreal label_font_size_;
};
class TextFieldLabel : public QWidget
{
Q_OBJECT
Q_PROPERTY(qreal scale WRITE setScale READ scale)
Q_PROPERTY(QPointF offset WRITE setOffset READ offset)
Q_PROPERTY(QColor color WRITE setColor READ color)
public:
TextFieldLabel(TextField *parent);
~TextFieldLabel();
inline void setColor(const QColor &color);
inline void setOffset(const QPointF &pos);
inline void setScale(qreal scale);
inline QColor color() const;
inline QPointF offset() const;
inline qreal scale() const;
protected:
void paintEvent(QPaintEvent *event) override;
private:
TextField *const text_field_;
QColor color_;
qreal scale_;
qreal x_;
qreal y_;
};
2017-08-20 13:47:22 +03:00
inline void
TextFieldLabel::setColor(const QColor &color)
2017-04-06 02:06:42 +03:00
{
color_ = color;
update();
}
2017-08-20 13:47:22 +03:00
inline void
TextFieldLabel::setOffset(const QPointF &pos)
2017-04-06 02:06:42 +03:00
{
x_ = pos.x();
y_ = pos.y();
update();
}
2017-08-20 13:47:22 +03:00
inline void
TextFieldLabel::setScale(qreal scale)
2017-04-06 02:06:42 +03:00
{
scale_ = scale;
update();
}
2017-08-20 13:47:22 +03:00
inline QPointF
TextFieldLabel::offset() const
2017-04-06 02:06:42 +03:00
{
return QPointF(x_, y_);
}
2017-08-20 13:47:22 +03:00
inline qreal
TextFieldLabel::scale() const
2017-04-06 02:06:42 +03:00
{
return scale_;
}
2017-08-20 13:47:22 +03:00
inline QColor
TextFieldLabel::color() const
2017-04-06 02:06:42 +03:00
{
return color_;
}
class TextFieldStateMachine : public QStateMachine
{
Q_OBJECT
Q_PROPERTY(qreal progress WRITE setProgress READ progress)
public:
TextFieldStateMachine(TextField *parent);
~TextFieldStateMachine();
inline void setProgress(qreal progress);
void setLabel(TextFieldLabel *label);
inline qreal progress() const;
public slots:
void setupProperties();
private:
QPropertyAnimation *color_anim_;
QPropertyAnimation *offset_anim_;
QState *focused_state_;
QState *normal_state_;
TextField *text_field_;
TextFieldLabel *label_;
qreal progress_;
};
2017-08-20 13:47:22 +03:00
inline void
TextFieldStateMachine::setProgress(qreal progress)
2017-04-06 02:06:42 +03:00
{
progress_ = progress;
text_field_->update();
}
2017-08-20 13:47:22 +03:00
inline qreal
TextFieldStateMachine::progress() const
2017-04-06 02:06:42 +03:00
{
return progress_;
}