nheko/src/ui/TextField.h

201 lines
4.3 KiB
C
Raw Normal View History

2021-03-05 00:35:15 +01:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
2017-04-06 01:06:42 +02:00
#include <QColor>
#include <QLineEdit>
#include <QPaintEvent>
#include <QPropertyAnimation>
#include <QRegularExpression>
2017-04-06 01:06:42 +02:00
#include <QStateMachine>
#include <QtGlobal>
class TextField;
class TextFieldLabel;
class TextFieldStateMachine;
class TextField : public QLineEdit
{
2021-09-18 00:22:33 +02:00
Q_OBJECT
2017-04-06 01:06:42 +02:00
2021-12-28 22:30:12 +01:00
Q_PROPERTY(QColor inkColor WRITE setInkColor READ inkColor NOTIFY inkColorChanged)
Q_PROPERTY(QColor labelColor WRITE setLabelColor READ labelColor NOTIFY labelColorChanged)
Q_PROPERTY(QColor underlineColor WRITE setUnderlineColor READ underlineColor NOTIFY
underlineColorChanged)
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor NOTIFY
backgroundColorChanged)
2017-04-06 01:06:42 +02:00
public:
2021-09-18 00:22:33 +02:00
explicit TextField(QWidget *parent = nullptr);
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);
QColor inkColor() const;
QColor labelColor() const;
QColor underlineColor() const;
QColor backgroundColor() const;
QString label() const;
bool hasLabel() const;
bool isValid() const;
qreal labelFontSize() const;
2017-04-06 01:06:42 +02:00
protected:
2021-09-18 00:22:33 +02:00
bool event(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
2017-04-06 01:06:42 +02:00
2021-12-28 22:30:12 +01:00
signals:
void inkColorChanged();
void labelColorChanged();
void underlineColorChanged();
void backgroundColorChanged();
2017-04-06 01:06:42 +02:00
private:
2021-09-18 00:22:33 +02: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_;
qreal label_font_size_;
2017-04-06 01:06:42 +02:00
};
class TextFieldLabel : public QWidget
{
2021-09-18 00:22:33 +02:00
Q_OBJECT
2017-04-06 01:06:42 +02:00
2021-12-28 22:30:12 +01:00
Q_PROPERTY(qreal scale WRITE setScale READ scale NOTIFY scaleChanged)
Q_PROPERTY(QPointF offset WRITE setOffset READ offset NOTIFY offsetChanged)
Q_PROPERTY(QColor color WRITE setColor READ color NOTIFY colorChanged)
2017-04-06 01:06:42 +02:00
public:
2021-09-18 00:22:33 +02:00
TextFieldLabel(TextField *parent);
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
inline void setColor(const QColor &color);
2021-12-29 00:36:43 +01:00
inline void setOffset(QPointF pos);
2021-09-18 00:22:33 +02:00
inline void setScale(qreal scale);
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
inline QColor color() const;
inline QPointF offset() const;
inline qreal scale() const;
2017-04-06 01:06:42 +02:00
protected:
2021-09-18 00:22:33 +02:00
void paintEvent(QPaintEvent *event) override;
2017-04-06 01:06:42 +02:00
2021-12-28 22:30:12 +01:00
signals:
void scaleChanged();
void offsetChanged();
void colorChanged();
2017-04-06 01:06:42 +02:00
private:
2021-09-18 00:22:33 +02:00
TextField *const text_field_;
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
QColor color_;
qreal scale_;
qreal x_;
qreal y_;
2017-04-06 01:06:42 +02:00
};
2017-08-20 12:47:22 +02:00
inline void
TextFieldLabel::setColor(const QColor &color)
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
color_ = color;
2021-12-28 22:30:12 +01:00
emit colorChanged();
2021-09-18 00:22:33 +02:00
update();
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
inline void
2021-12-29 00:36:43 +01:00
TextFieldLabel::setOffset(QPointF pos)
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
x_ = pos.x();
y_ = pos.y();
2021-12-28 22:30:12 +01:00
emit offsetChanged();
2021-09-18 00:22:33 +02:00
update();
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
inline void
TextFieldLabel::setScale(qreal scale)
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
scale_ = scale;
2021-12-28 22:30:12 +01:00
emit scaleChanged();
2021-09-18 00:22:33 +02:00
update();
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
inline QPointF
TextFieldLabel::offset() const
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
return QPointF(x_, y_);
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
inline qreal
TextFieldLabel::scale() const
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
return scale_;
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
inline QColor
TextFieldLabel::color() const
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
return color_;
2017-04-06 01:06:42 +02:00
}
class TextFieldStateMachine : public QStateMachine
{
2021-09-18 00:22:33 +02:00
Q_OBJECT
2017-04-06 01:06:42 +02:00
2021-12-28 22:30:12 +01:00
Q_PROPERTY(qreal progress WRITE setProgress READ progress NOTIFY progressChanged)
2017-04-06 01:06:42 +02:00
public:
2021-09-18 00:22:33 +02:00
TextFieldStateMachine(TextField *parent);
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
inline void setProgress(qreal progress);
void setLabel(TextFieldLabel *label);
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
inline qreal progress() const;
2017-04-06 01:06:42 +02:00
public slots:
2021-09-18 00:22:33 +02:00
void setupProperties();
2017-04-06 01:06:42 +02:00
2021-12-28 22:30:12 +01:00
signals:
void progressChanged();
2017-04-06 01:06:42 +02:00
private:
2021-09-18 00:22:33 +02:00
QPropertyAnimation *color_anim_;
QPropertyAnimation *offset_anim_;
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
QState *focused_state_;
QState *normal_state_;
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
TextField *text_field_;
TextFieldLabel *label_;
2017-04-06 01:06:42 +02:00
2021-09-18 00:22:33 +02:00
qreal progress_;
2017-04-06 01:06:42 +02:00
};
2017-08-20 12:47:22 +02:00
inline void
TextFieldStateMachine::setProgress(qreal progress)
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
progress_ = progress;
2021-12-28 22:30:12 +01:00
emit progressChanged();
2021-09-18 00:22:33 +02:00
text_field_->update();
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
inline qreal
TextFieldStateMachine::progress() const
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
return progress_;
2017-04-06 01:06:42 +02:00
}