matrixion/src/ui/TextField.h

181 lines
4 KiB
C
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
2017-04-06 02:06:42 +03:00
#include <QColor>
#include <QLineEdit>
#include <QPaintEvent>
#include <QPropertyAnimation>
#include <QRegularExpression>
2017-04-06 02:06:42 +03:00
#include <QStateMachine>
#include <QtGlobal>
class TextField;
class TextFieldLabel;
class TextFieldStateMachine;
class TextField : public QLineEdit
{
2017-09-10 12:59:21 +03:00
Q_OBJECT
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
Q_PROPERTY(QColor inkColor WRITE setInkColor READ inkColor)
2017-12-10 16:22:01 +03:00
Q_PROPERTY(QColor labelColor WRITE setLabelColor READ labelColor)
2017-09-10 12:59:21 +03:00
Q_PROPERTY(QColor underlineColor WRITE setUnderlineColor READ underlineColor)
2017-11-25 16:14:37 +03:00
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor)
2017-04-06 02:06:42 +03:00
public:
2020-02-04 06:58:43 +03:00
explicit TextField(QWidget *parent = nullptr);
2017-09-10 12:59:21 +03:00
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 setUnderlineColor(const QColor &color);
void setRegexp(const QRegularExpression &regexp);
void setValid(bool valid);
2017-09-10 12:59:21 +03:00
QColor inkColor() const;
QColor labelColor() const;
QColor underlineColor() const;
QColor backgroundColor() const;
QString label() const;
bool hasLabel() const;
bool isValid() const;
2017-09-10 12:59:21 +03:00
qreal labelFontSize() const;
2017-04-06 02:06:42 +03:00
protected:
2017-09-10 12:59:21 +03:00
bool event(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
2017-04-06 02:06:42 +03:00
private:
2017-09-10 12:59:21 +03:00
void init();
QColor ink_color_;
QColor background_color_;
QColor label_color_;
QColor underline_color_;
QString label_text_;
TextFieldLabel *label_;
TextFieldStateMachine *state_machine_;
bool show_label_;
QRegularExpression regexp_;
bool is_valid_;
2017-09-10 12:59:21 +03:00
qreal label_font_size_;
2017-04-06 02:06:42 +03:00
};
class TextFieldLabel : public QWidget
{
2017-09-10 12:59:21 +03:00
Q_OBJECT
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
Q_PROPERTY(qreal scale WRITE setScale READ scale)
Q_PROPERTY(QPointF offset WRITE setOffset READ offset)
Q_PROPERTY(QColor color WRITE setColor READ color)
2017-04-06 02:06:42 +03:00
public:
2017-09-10 12:59:21 +03:00
TextFieldLabel(TextField *parent);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
inline void setColor(const QColor &color);
inline void setOffset(const QPointF &pos);
inline void setScale(qreal scale);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
inline QColor color() const;
inline QPointF offset() const;
inline qreal scale() const;
2017-04-06 02:06:42 +03:00
protected:
2017-09-10 12:59:21 +03:00
void paintEvent(QPaintEvent *event) override;
2017-04-06 02:06:42 +03:00
private:
2017-09-10 12:59:21 +03:00
TextField *const text_field_;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
QColor color_;
qreal scale_;
qreal x_;
qreal y_;
2017-04-06 02:06:42 +03:00
};
2017-08-20 13:47:22 +03:00
inline void
TextFieldLabel::setColor(const QColor &color)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
color_ = color;
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
inline void
TextFieldLabel::setOffset(const QPointF &pos)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
x_ = pos.x();
y_ = pos.y();
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
inline void
TextFieldLabel::setScale(qreal scale)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
scale_ = scale;
update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
inline QPointF
TextFieldLabel::offset() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return QPointF(x_, y_);
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
inline qreal
TextFieldLabel::scale() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return scale_;
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
inline QColor
TextFieldLabel::color() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return color_;
2017-04-06 02:06:42 +03:00
}
class TextFieldStateMachine : public QStateMachine
{
2017-09-10 12:59:21 +03:00
Q_OBJECT
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
Q_PROPERTY(qreal progress WRITE setProgress READ progress)
2017-04-06 02:06:42 +03:00
public:
2017-09-10 12:59:21 +03:00
TextFieldStateMachine(TextField *parent);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
inline void setProgress(qreal progress);
void setLabel(TextFieldLabel *label);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
inline qreal progress() const;
2017-04-06 02:06:42 +03:00
public slots:
2017-09-10 12:59:21 +03:00
void setupProperties();
2017-04-06 02:06:42 +03:00
private:
2017-09-10 12:59:21 +03:00
QPropertyAnimation *color_anim_;
QPropertyAnimation *offset_anim_;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
QState *focused_state_;
QState *normal_state_;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
TextField *text_field_;
TextFieldLabel *label_;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
qreal progress_;
2017-04-06 02:06:42 +03:00
};
2017-08-20 13:47:22 +03:00
inline void
TextFieldStateMachine::setProgress(qreal progress)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
progress_ = progress;
text_field_->update();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
inline qreal
TextFieldStateMachine::progress() const
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
return progress_;
2017-04-06 02:06:42 +03:00
}