You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nheko/include/ui/TextField.h

177 lines
3.9 KiB

#pragma once
8 years ago
#include <QColor>
#include <QLineEdit>
#include <QPaintEvent>
#include <QPropertyAnimation>
#include <QStateMachine>
#include <QtGlobal>
class TextField;
class TextFieldLabel;
class TextFieldStateMachine;
class TextField : public QLineEdit
{
Q_OBJECT
8 years ago
Q_PROPERTY(QColor textColor WRITE setTextColor READ textColor)
Q_PROPERTY(QColor inkColor WRITE setInkColor READ inkColor)
Q_PROPERTY(QColor underlineColor WRITE setUnderlineColor READ underlineColor)
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor)
8 years ago
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;
8 years ago
protected:
bool event(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
8 years ago
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_;
8 years ago
};
class TextFieldLabel : public QWidget
{
Q_OBJECT
8 years ago
Q_PROPERTY(qreal scale WRITE setScale READ scale)
Q_PROPERTY(QPointF offset WRITE setOffset READ offset)
Q_PROPERTY(QColor color WRITE setColor READ color)
8 years ago
public:
TextFieldLabel(TextField *parent);
~TextFieldLabel();
8 years ago
inline void setColor(const QColor &color);
inline void setOffset(const QPointF &pos);
inline void setScale(qreal scale);
8 years ago
inline QColor color() const;
inline QPointF offset() const;
inline qreal scale() const;
8 years ago
protected:
void paintEvent(QPaintEvent *event) override;
8 years ago
private:
TextField *const text_field_;
8 years ago
QColor color_;
qreal scale_;
qreal x_;
qreal y_;
8 years ago
};
inline void
TextFieldLabel::setColor(const QColor &color)
8 years ago
{
color_ = color;
update();
8 years ago
}
inline void
TextFieldLabel::setOffset(const QPointF &pos)
8 years ago
{
x_ = pos.x();
y_ = pos.y();
update();
8 years ago
}
inline void
TextFieldLabel::setScale(qreal scale)
8 years ago
{
scale_ = scale;
update();
8 years ago
}
inline QPointF
TextFieldLabel::offset() const
8 years ago
{
return QPointF(x_, y_);
8 years ago
}
inline qreal
TextFieldLabel::scale() const
8 years ago
{
return scale_;
8 years ago
}
inline QColor
TextFieldLabel::color() const
8 years ago
{
return color_;
8 years ago
}
class TextFieldStateMachine : public QStateMachine
{
Q_OBJECT
8 years ago
Q_PROPERTY(qreal progress WRITE setProgress READ progress)
8 years ago
public:
TextFieldStateMachine(TextField *parent);
~TextFieldStateMachine();
8 years ago
inline void setProgress(qreal progress);
void setLabel(TextFieldLabel *label);
8 years ago
inline qreal progress() const;
8 years ago
public slots:
void setupProperties();
8 years ago
private:
QPropertyAnimation *color_anim_;
QPropertyAnimation *offset_anim_;
8 years ago
QState *focused_state_;
QState *normal_state_;
8 years ago
TextField *text_field_;
TextFieldLabel *label_;
8 years ago
qreal progress_;
8 years ago
};
inline void
TextFieldStateMachine::setProgress(qreal progress)
8 years ago
{
progress_ = progress;
text_field_->update();
8 years ago
}
inline qreal
TextFieldStateMachine::progress() const
8 years ago
{
return progress_;
8 years ago
}