TextField moved to md

This commit is contained in:
Achraf k 2022-02-17 04:08:05 +01:00
parent 0fee3cf434
commit a910b39a2f
5 changed files with 118 additions and 105 deletions

View File

@ -5,26 +5,28 @@
#include "qtmaterialtextfield_internal.h" #include "qtmaterialtextfield_internal.h"
#include "lib/qtmaterialstyle.h" #include "lib/qtmaterialstyle.h"
#include <QDebug> #include <QDebug>
namespace md
{
/*! /*!
* \class QtMaterialTextFieldPrivate * \class QtMaterialTextFieldPrivate
* \internal * \internal
*/ */
QtMaterialTextFieldPrivate::QtMaterialTextFieldPrivate(QtMaterialTextField *q) TextFieldPrivate::TextFieldPrivate(TextField *q)
: q_ptr(q) : q_ptr(q)
{ {
} }
QtMaterialTextFieldPrivate::~QtMaterialTextFieldPrivate() TextFieldPrivate::~TextFieldPrivate()
{ {
} }
void QtMaterialTextFieldPrivate::init() void TextFieldPrivate::init()
{ {
Q_Q(QtMaterialTextField); Q_Q(TextField);
stateMachine = new QtMaterialTextFieldStateMachine(q); stateMachine = new TextFieldStateMachine(q);
label = 0; label = 0;
labelFontSize = 9.5; labelFontSize = 9.5;
showLabel = false; showLabel = false;
@ -32,7 +34,7 @@ void QtMaterialTextFieldPrivate::init()
useThemeColors = true; useThemeColors = true;
q->setFrame(false); q->setFrame(false);
q->setStyle(&QtMaterialStyle::instance()); q->setStyle(&Style::instance());
q->setAttribute(Qt::WA_Hover); q->setAttribute(Qt::WA_Hover);
q->setMouseTracking(true); q->setMouseTracking(true);
q->setTextMargins(0, 2, 0, 4); q->setTextMargins(0, 2, 0, 4);
@ -47,20 +49,20 @@ void QtMaterialTextFieldPrivate::init()
* \class QtMaterialTextField * \class QtMaterialTextField
*/ */
QtMaterialTextField::QtMaterialTextField(QWidget *parent) TextField::TextField(QWidget *parent)
: QLineEdit(parent), : QLineEdit(parent),
d_ptr(new QtMaterialTextFieldPrivate(this)) d_ptr(new TextFieldPrivate(this))
{ {
d_func()->init(); d_func()->init();
} }
QtMaterialTextField::~QtMaterialTextField() TextField::~TextField()
{ {
} }
void QtMaterialTextField::setUseThemeColors(bool value) void TextField::setUseThemeColors(bool value)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
if (d->useThemeColors == value) { if (d->useThemeColors == value) {
return; return;
@ -70,16 +72,16 @@ void QtMaterialTextField::setUseThemeColors(bool value)
d->stateMachine->setupProperties(); d->stateMachine->setupProperties();
} }
bool QtMaterialTextField::useThemeColors() const bool TextField::useThemeColors() const
{ {
Q_D(const QtMaterialTextField); Q_D(const TextField);
return d->useThemeColors; return d->useThemeColors;
} }
void QtMaterialTextField::setShowLabel(bool value) void TextField::setShowLabel(bool value)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
if (d->showLabel == value) { if (d->showLabel == value) {
return; return;
@ -88,7 +90,7 @@ void QtMaterialTextField::setShowLabel(bool value)
d->showLabel = value; d->showLabel = value;
if (!d->label && value) { if (!d->label && value) {
d->label = new QtMaterialTextFieldLabel(this); d->label = new TextFieldLabel(this);
d->stateMachine->setLabel(d->label); d->stateMachine->setLabel(d->label);
} }
@ -99,16 +101,16 @@ void QtMaterialTextField::setShowLabel(bool value)
} }
} }
bool QtMaterialTextField::hasLabel() const bool TextField::hasLabel() const
{ {
Q_D(const QtMaterialTextField); Q_D(const TextField);
return d->showLabel; return d->showLabel;
} }
void QtMaterialTextField::setLabelFontSize(qreal size) void TextField::setLabelFontSize(qreal size)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
d->labelFontSize = size; d->labelFontSize = size;
@ -121,32 +123,32 @@ void QtMaterialTextField::setLabelFontSize(qreal size)
} }
} }
qreal QtMaterialTextField::labelFontSize() const qreal TextField::labelFontSize() const
{ {
Q_D(const QtMaterialTextField); Q_D(const TextField);
return d->labelFontSize; return d->labelFontSize;
} }
void QtMaterialTextField::setLabel(const QString &label) void TextField::setLabel(const QString &label)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
d->labelString = label; d->labelString = label;
setShowLabel(true); setShowLabel(true);
d->label->update(); d->label->update();
} }
QString QtMaterialTextField::label() const QString TextField::label() const
{ {
Q_D(const QtMaterialTextField); Q_D(const TextField);
return d->labelString; return d->labelString;
} }
void QtMaterialTextField::setTextColor(const QColor &color) void TextField::setTextColor(const QColor &color)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
d->textColor = color; d->textColor = color;
setStyleSheet(QString("QLineEdit { color: %1; }").arg(color.name())); setStyleSheet(QString("QLineEdit { color: %1; }").arg(color.name()));
@ -155,20 +157,20 @@ void QtMaterialTextField::setTextColor(const QColor &color)
d->stateMachine->setupProperties(); d->stateMachine->setupProperties();
} }
QColor QtMaterialTextField::textColor() const QColor TextField::textColor() const
{ {
Q_D(const QtMaterialTextField); Q_D(const TextField);
if (d->useThemeColors || !d->textColor.isValid()) { if (d->useThemeColors || !d->textColor.isValid()) {
return QtMaterialStyle::instance().themeColor("text"); return Style::instance().themeColor("text");
} else { } else {
return d->textColor; return d->textColor;
} }
} }
void QtMaterialTextField::setLabelColor(const QColor &color) void TextField::setLabelColor(const QColor &color)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
d->labelColor = color; d->labelColor = color;
@ -176,20 +178,20 @@ void QtMaterialTextField::setLabelColor(const QColor &color)
d->stateMachine->setupProperties(); d->stateMachine->setupProperties();
} }
QColor QtMaterialTextField::labelColor() const QColor TextField::labelColor() const
{ {
Q_D(const QtMaterialTextField); Q_D(const TextField);
if (d->useThemeColors || !d->labelColor.isValid()) { if (d->useThemeColors || !d->labelColor.isValid()) {
return QtMaterialStyle::instance().themeColor("accent3"); return Style::instance().themeColor("accent3");
} else { } else {
return d->labelColor; return d->labelColor;
} }
} }
void QtMaterialTextField::setInkColor(const QColor &color) void TextField::setInkColor(const QColor &color)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
d->inkColor = color; d->inkColor = color;
@ -197,20 +199,20 @@ void QtMaterialTextField::setInkColor(const QColor &color)
d->stateMachine->setupProperties(); d->stateMachine->setupProperties();
} }
QColor QtMaterialTextField::inkColor() const QColor TextField::inkColor() const
{ {
Q_D(const QtMaterialTextField); Q_D(const TextField);
if (d->useThemeColors || !d->inkColor.isValid()) { if (d->useThemeColors || !d->inkColor.isValid()) {
return QtMaterialStyle::instance().themeColor("primary1"); return Style::instance().themeColor("primary1");
} else { } else {
return d->inkColor; return d->inkColor;
} }
} }
void QtMaterialTextField::setInputLineColor(const QColor &color) void TextField::setInputLineColor(const QColor &color)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
d->inputLineColor = color; d->inputLineColor = color;
@ -218,20 +220,20 @@ void QtMaterialTextField::setInputLineColor(const QColor &color)
d->stateMachine->setupProperties(); d->stateMachine->setupProperties();
} }
QColor QtMaterialTextField::inputLineColor() const QColor TextField::inputLineColor() const
{ {
Q_D(const QtMaterialTextField); Q_D(const TextField);
if (d->useThemeColors || !d->inputLineColor.isValid()) { if (d->useThemeColors || !d->inputLineColor.isValid()) {
return QtMaterialStyle::instance().themeColor("border"); return Style::instance().themeColor("border");
} else { } else {
return d->inputLineColor; return d->inputLineColor;
} }
} }
void QtMaterialTextField::setShowInputLine(bool value) void TextField::setShowInputLine(bool value)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
if (d->showInputLine == value) { if (d->showInputLine == value) {
return; return;
@ -241,14 +243,14 @@ void QtMaterialTextField::setShowInputLine(bool value)
update(); update();
} }
bool QtMaterialTextField::hasInputLine() const bool TextField::hasInputLine() const
{ {
Q_D(const QtMaterialTextField); Q_D(const TextField);
return d->showInputLine; return d->showInputLine;
} }
QtMaterialTextField::QtMaterialTextField(QtMaterialTextFieldPrivate &d, QWidget *parent) TextField::TextField(TextFieldPrivate &d, QWidget *parent)
: QLineEdit(parent), : QLineEdit(parent),
d_ptr(&d) d_ptr(&d)
{ {
@ -258,9 +260,9 @@ QtMaterialTextField::QtMaterialTextField(QtMaterialTextFieldPrivate &d, QWidget
/*! /*!
* \reimp * \reimp
*/ */
bool QtMaterialTextField::event(QEvent *event) bool TextField::event(QEvent *event)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
switch (event->type()) switch (event->type())
{ {
@ -279,9 +281,9 @@ bool QtMaterialTextField::event(QEvent *event)
/*! /*!
* \reimp * \reimp
*/ */
void QtMaterialTextField::paintEvent(QPaintEvent *event) void TextField::paintEvent(QPaintEvent *event)
{ {
Q_D(QtMaterialTextField); Q_D(TextField);
QLineEdit::paintEvent(event); QLineEdit::paintEvent(event);
@ -324,3 +326,4 @@ void QtMaterialTextField::paintEvent(QPaintEvent *event)
} }
} }
} }
}

View File

@ -3,10 +3,12 @@
#include <QtWidgets/QLineEdit> #include <QtWidgets/QLineEdit>
#include <QColor> #include <QColor>
namespace md
{
class QtMaterialTextFieldPrivate; class TextFieldPrivate;
class QtMaterialTextField : public QLineEdit class TextField : public QLineEdit
{ {
Q_OBJECT Q_OBJECT
@ -15,8 +17,8 @@ class QtMaterialTextField : public QLineEdit
Q_PROPERTY(QColor inputLineColor WRITE setInputLineColor READ inputLineColor) Q_PROPERTY(QColor inputLineColor WRITE setInputLineColor READ inputLineColor)
public: public:
explicit QtMaterialTextField(QWidget *parent = 0); explicit TextField(QWidget *parent = 0);
~QtMaterialTextField(); ~TextField();
void setUseThemeColors(bool value); void setUseThemeColors(bool value);
bool useThemeColors() const; bool useThemeColors() const;
@ -46,16 +48,16 @@ public:
bool hasInputLine() const; bool hasInputLine() const;
protected: protected:
QtMaterialTextField(QtMaterialTextFieldPrivate &d, QWidget *parent = 0); TextField(TextFieldPrivate &d, QWidget *parent = 0);
bool event(QEvent *event) Q_DECL_OVERRIDE; bool event(QEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
const QScopedPointer<QtMaterialTextFieldPrivate> d_ptr; const QScopedPointer<TextFieldPrivate> d_ptr;
private: private:
Q_DISABLE_COPY(QtMaterialTextField) Q_DISABLE_COPY(TextField)
Q_DECLARE_PRIVATE(QtMaterialTextField) Q_DECLARE_PRIVATE(TextField)
}; };
}
#endif // QTMATERIALTEXTFIELD_H #endif // QTMATERIALTEXTFIELD_H

View File

@ -3,13 +3,15 @@
#include <QEventTransition> #include <QEventTransition>
#include <QPainter> #include <QPainter>
#include "qtmaterialtextfield.h" #include "qtmaterialtextfield.h"
namespace md
{
/*! /*!
* \class QtMaterialTextFieldStateMachine * \class QtMaterialTextFieldStateMachine
* \internal * \internal
*/ */
QtMaterialTextFieldStateMachine::QtMaterialTextFieldStateMachine(QtMaterialTextField *parent) TextFieldStateMachine::TextFieldStateMachine(TextField *parent)
: QStateMachine(parent), : QStateMachine(parent),
m_textField(parent), m_textField(parent),
m_normalState(new QState), m_normalState(new QState),
@ -55,11 +57,11 @@ QtMaterialTextFieldStateMachine::QtMaterialTextFieldStateMachine(QtMaterialTextF
connect(m_textField, SIGNAL(textChanged(QString)), this, SLOT(setupProperties())); connect(m_textField, SIGNAL(textChanged(QString)), this, SLOT(setupProperties()));
} }
QtMaterialTextFieldStateMachine::~QtMaterialTextFieldStateMachine() TextFieldStateMachine::~TextFieldStateMachine()
{ {
} }
void QtMaterialTextFieldStateMachine::setLabel(QtMaterialTextFieldLabel *label) void TextFieldStateMachine::setLabel(TextFieldLabel *label)
{ {
if (m_label) { if (m_label) {
delete m_label; delete m_label;
@ -92,7 +94,7 @@ void QtMaterialTextFieldStateMachine::setLabel(QtMaterialTextFieldLabel *label)
setupProperties(); setupProperties();
} }
void QtMaterialTextFieldStateMachine::setupProperties() void TextFieldStateMachine::setupProperties()
{ {
if (m_label) if (m_label)
{ {
@ -123,7 +125,7 @@ void QtMaterialTextFieldStateMachine::setupProperties()
* \internal * \internal
*/ */
QtMaterialTextFieldLabel::QtMaterialTextFieldLabel(QtMaterialTextField *parent) TextFieldLabel::TextFieldLabel(TextField *parent)
: QWidget(parent), : QWidget(parent),
m_textField(parent), m_textField(parent),
m_scale(1), m_scale(1),
@ -138,14 +140,14 @@ QtMaterialTextFieldLabel::QtMaterialTextFieldLabel(QtMaterialTextField *parent)
setFont(font); setFont(font);
} }
QtMaterialTextFieldLabel::~QtMaterialTextFieldLabel() TextFieldLabel::~TextFieldLabel()
{ {
} }
/*! /*!
* \reimp * \reimp
*/ */
void QtMaterialTextFieldLabel::paintEvent(QPaintEvent *event) void TextFieldLabel::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event) Q_UNUSED(event)
@ -162,3 +164,4 @@ void QtMaterialTextFieldLabel::paintEvent(QPaintEvent *event)
QPointF pos(2+m_posX, height()-36+m_posY); QPointF pos(2+m_posX, height()-36+m_posY);
painter.drawText(pos.x(), pos.y(), m_textField->label()); painter.drawText(pos.x(), pos.y(), m_textField->label());
} }
}

View File

@ -4,21 +4,23 @@
#include <QStateMachine> #include <QStateMachine>
#include <QtWidgets/QWidget> #include <QtWidgets/QWidget>
#include "qtmaterialtextfield.h" #include "qtmaterialtextfield.h"
namespace md
{
class QPropertyAnimation; class QPropertyAnimation;
class QtMaterialTextFieldLabel; class TextFieldLabel;
class QtMaterialTextFieldStateMachine : public QStateMachine class TextFieldStateMachine : public QStateMachine
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(qreal progress WRITE setProgress READ progress) Q_PROPERTY(qreal progress WRITE setProgress READ progress)
public: public:
QtMaterialTextFieldStateMachine(QtMaterialTextField *parent); TextFieldStateMachine(TextField *parent);
~QtMaterialTextFieldStateMachine(); ~TextFieldStateMachine();
void setLabel(QtMaterialTextFieldLabel *label); void setLabel(TextFieldLabel *label);
inline void setProgress(qreal progress); inline void setProgress(qreal progress);
inline qreal progress() const; inline qreal progress() const;
@ -27,29 +29,29 @@ public slots:
void setupProperties(); void setupProperties();
private: private:
Q_DISABLE_COPY(QtMaterialTextFieldStateMachine) Q_DISABLE_COPY(TextFieldStateMachine)
QtMaterialTextField *const m_textField; TextField *const m_textField;
QState *const m_normalState; QState *const m_normalState;
QState *const m_focusedState; QState *const m_focusedState;
QtMaterialTextFieldLabel *m_label; TextFieldLabel *m_label;
QPropertyAnimation *m_offsetAnimation; QPropertyAnimation *m_offsetAnimation;
QPropertyAnimation *m_colorAnimation; QPropertyAnimation *m_colorAnimation;
qreal m_progress; qreal m_progress;
}; };
inline void QtMaterialTextFieldStateMachine::setProgress(qreal progress) inline void TextFieldStateMachine::setProgress(qreal progress)
{ {
m_progress = progress; m_progress = progress;
m_textField->update(); m_textField->update();
} }
inline qreal QtMaterialTextFieldStateMachine::progress() const inline qreal TextFieldStateMachine::progress() const
{ {
return m_progress; return m_progress;
} }
class QtMaterialTextFieldLabel : public QWidget class TextFieldLabel : public QWidget
{ {
Q_OBJECT Q_OBJECT
@ -58,8 +60,8 @@ class QtMaterialTextFieldLabel : public QWidget
Q_PROPERTY(QColor color WRITE setColor READ color) Q_PROPERTY(QColor color WRITE setColor READ color)
public: public:
QtMaterialTextFieldLabel(QtMaterialTextField *parent); TextFieldLabel(TextField *parent);
~QtMaterialTextFieldLabel(); ~TextFieldLabel();
inline void setScale(qreal scale); inline void setScale(qreal scale);
inline qreal scale() const; inline qreal scale() const;
@ -74,47 +76,47 @@ protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private: private:
Q_DISABLE_COPY(QtMaterialTextFieldLabel) Q_DISABLE_COPY(TextFieldLabel)
QtMaterialTextField *const m_textField; TextField *const m_textField;
qreal m_scale; qreal m_scale;
qreal m_posX; qreal m_posX;
qreal m_posY; qreal m_posY;
QColor m_color; QColor m_color;
}; };
inline void QtMaterialTextFieldLabel::setScale(qreal scale) inline void TextFieldLabel::setScale(qreal scale)
{ {
m_scale = scale; m_scale = scale;
update(); update();
} }
inline qreal QtMaterialTextFieldLabel::scale() const inline qreal TextFieldLabel::scale() const
{ {
return m_scale; return m_scale;
} }
inline void QtMaterialTextFieldLabel::setOffset(const QPointF &pos) inline void TextFieldLabel::setOffset(const QPointF &pos)
{ {
m_posX = pos.x(); m_posX = pos.x();
m_posY = pos.y(); m_posY = pos.y();
update(); update();
} }
inline QPointF QtMaterialTextFieldLabel::offset() const inline QPointF TextFieldLabel::offset() const
{ {
return QPointF(m_posX, m_posY); return QPointF(m_posX, m_posY);
} }
inline void QtMaterialTextFieldLabel::setColor(const QColor &color) inline void TextFieldLabel::setColor(const QColor &color)
{ {
m_color = color; m_color = color;
update(); update();
} }
inline QColor QtMaterialTextFieldLabel::color() const inline QColor TextFieldLabel::color() const
{ {
return m_color; return m_color;
} }
}
#endif // QTMATERIALTEXTFIELD_INTERNAL_H #endif // QTMATERIALTEXTFIELD_INTERNAL_H

View File

@ -4,24 +4,26 @@
#include <QtGlobal> #include <QtGlobal>
#include <QColor> #include <QColor>
class QtMaterialTextField; namespace md
class QtMaterialTextFieldStateMachine;
class QtMaterialTextFieldLabel;
class QtMaterialTextFieldPrivate
{ {
Q_DISABLE_COPY(QtMaterialTextFieldPrivate) class TextField;
Q_DECLARE_PUBLIC(QtMaterialTextField) class TextFieldStateMachine;
class TextFieldLabel;
class TextFieldPrivate
{
Q_DISABLE_COPY(TextFieldPrivate)
Q_DECLARE_PUBLIC(TextField)
public: public:
QtMaterialTextFieldPrivate(QtMaterialTextField *q); TextFieldPrivate(TextField *q);
virtual ~QtMaterialTextFieldPrivate(); virtual ~TextFieldPrivate();
void init(); void init();
QtMaterialTextField *const q_ptr; TextField *const q_ptr;
QtMaterialTextFieldStateMachine *stateMachine; TextFieldStateMachine *stateMachine;
QtMaterialTextFieldLabel *label; TextFieldLabel *label;
QColor textColor; QColor textColor;
QColor labelColor; QColor labelColor;
QColor inkColor; QColor inkColor;
@ -32,5 +34,6 @@ public:
bool showInputLine; bool showInputLine;
bool useThemeColors; bool useThemeColors;
}; };
}
#endif // QTMATERIALTEXTFIELD_P_H #endif // QTMATERIALTEXTFIELD_P_H