qt-material-widgets/components/textfield.cpp

330 lines
5.9 KiB
C++
Raw Normal View History

2022-02-17 15:17:19 +00:00
#include "textfield.h"
#include "textfield_p.h"
2017-09-29 17:15:25 +00:00
#include <QtWidgets/QApplication>
#include <QPainter>
2022-02-17 15:17:19 +00:00
#include "textfield_internal.h"
#include "lib/style.h"
2017-09-29 17:15:25 +00:00
#include <QDebug>
2022-02-17 03:08:05 +00:00
namespace md
{
2017-09-29 17:15:25 +00:00
/*!
* \class QtMaterialTextFieldPrivate
* \internal
*/
2022-02-17 03:08:05 +00:00
TextFieldPrivate::TextFieldPrivate(TextField *q)
2017-09-29 17:15:25 +00:00
: q_ptr(q)
{
}
2022-02-17 03:08:05 +00:00
TextFieldPrivate::~TextFieldPrivate()
2017-09-29 17:15:25 +00:00
{
}
2022-02-17 03:08:05 +00:00
void TextFieldPrivate::init()
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_Q(TextField);
2017-09-29 17:15:25 +00:00
2022-02-17 03:08:05 +00:00
stateMachine = new TextFieldStateMachine(q);
2017-09-29 17:15:25 +00:00
label = 0;
labelFontSize = 9.5;
showLabel = false;
2017-10-06 05:29:38 +00:00
showInputLine = true;
2017-09-29 17:15:25 +00:00
useThemeColors = true;
q->setFrame(false);
2022-02-17 03:08:05 +00:00
q->setStyle(&Style::instance());
2017-09-29 17:15:25 +00:00
q->setAttribute(Qt::WA_Hover);
q->setMouseTracking(true);
q->setTextMargins(0, 2, 0, 4);
q->setFont(QFont("Roboto", 11, QFont::Normal));
2017-09-29 17:15:25 +00:00
stateMachine->start();
QCoreApplication::processEvents();
}
/*!
* \class QtMaterialTextField
*/
2022-02-17 03:08:05 +00:00
TextField::TextField(QWidget *parent)
2017-09-29 17:15:25 +00:00
: QLineEdit(parent),
2022-02-17 03:08:05 +00:00
d_ptr(new TextFieldPrivate(this))
2017-09-29 17:15:25 +00:00
{
d_func()->init();
}
2022-02-17 03:08:05 +00:00
TextField::~TextField()
2017-09-29 17:15:25 +00:00
{
}
2022-02-17 03:08:05 +00:00
void TextField::setUseThemeColors(bool value)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
if (d->useThemeColors == value) {
return;
}
d->useThemeColors = value;
d->stateMachine->setupProperties();
}
2022-02-17 03:08:05 +00:00
bool TextField::useThemeColors() const
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(const TextField);
2017-09-29 17:15:25 +00:00
return d->useThemeColors;
}
2022-02-17 03:08:05 +00:00
void TextField::setShowLabel(bool value)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
if (d->showLabel == value) {
return;
}
d->showLabel = value;
if (!d->label && value) {
2022-02-17 03:08:05 +00:00
d->label = new TextFieldLabel(this);
2017-09-29 17:15:25 +00:00
d->stateMachine->setLabel(d->label);
}
if (value) {
setContentsMargins(0, 23, 0, 0);
} else {
setContentsMargins(0, 0, 0, 0);
}
}
2022-02-17 03:08:05 +00:00
bool TextField::hasLabel() const
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(const TextField);
2017-09-29 17:15:25 +00:00
return d->showLabel;
}
2022-02-17 03:08:05 +00:00
void TextField::setLabelFontSize(qreal size)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
d->labelFontSize = size;
if (d->label)
{
QFont font(d->label->font());
font.setPointSizeF(size);
d->label->setFont(font);
d->label->update();
}
}
2022-02-17 03:08:05 +00:00
qreal TextField::labelFontSize() const
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(const TextField);
2017-09-29 17:15:25 +00:00
return d->labelFontSize;
}
2022-02-17 03:08:05 +00:00
void TextField::setLabel(const QString &label)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
d->labelString = label;
setShowLabel(true);
d->label->update();
}
2022-02-17 03:08:05 +00:00
QString TextField::label() const
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(const TextField);
2017-09-29 17:15:25 +00:00
return d->labelString;
}
2022-02-17 03:08:05 +00:00
void TextField::setTextColor(const QColor &color)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
d->textColor = color;
setStyleSheet(QString("QLineEdit { color: %1; }").arg(color.name()));
MATERIAL_DISABLE_THEME_COLORS
d->stateMachine->setupProperties();
}
2022-02-17 03:08:05 +00:00
QColor TextField::textColor() const
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(const TextField);
2017-09-29 17:15:25 +00:00
if (d->useThemeColors || !d->textColor.isValid()) {
2022-02-17 03:08:05 +00:00
return Style::instance().themeColor("text");
2017-09-29 17:15:25 +00:00
} else {
return d->textColor;
}
}
2022-02-17 03:08:05 +00:00
void TextField::setLabelColor(const QColor &color)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
d->labelColor = color;
MATERIAL_DISABLE_THEME_COLORS
d->stateMachine->setupProperties();
}
2022-02-17 03:08:05 +00:00
QColor TextField::labelColor() const
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(const TextField);
2017-09-29 17:15:25 +00:00
if (d->useThemeColors || !d->labelColor.isValid()) {
2022-02-17 03:08:05 +00:00
return Style::instance().themeColor("accent3");
2017-09-29 17:15:25 +00:00
} else {
return d->labelColor;
}
}
2022-02-17 03:08:05 +00:00
void TextField::setInkColor(const QColor &color)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
d->inkColor = color;
MATERIAL_DISABLE_THEME_COLORS
d->stateMachine->setupProperties();
}
2022-02-17 03:08:05 +00:00
QColor TextField::inkColor() const
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(const TextField);
2017-09-29 17:15:25 +00:00
if (d->useThemeColors || !d->inkColor.isValid()) {
2022-02-17 03:08:05 +00:00
return Style::instance().themeColor("primary1");
2017-09-29 17:15:25 +00:00
} else {
return d->inkColor;
}
}
2022-02-17 03:08:05 +00:00
void TextField::setInputLineColor(const QColor &color)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
2017-10-06 05:29:38 +00:00
d->inputLineColor = color;
2017-09-29 17:15:25 +00:00
MATERIAL_DISABLE_THEME_COLORS
d->stateMachine->setupProperties();
}
2022-02-17 03:08:05 +00:00
QColor TextField::inputLineColor() const
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(const TextField);
2017-09-29 17:15:25 +00:00
2017-10-06 05:29:38 +00:00
if (d->useThemeColors || !d->inputLineColor.isValid()) {
2022-02-17 03:08:05 +00:00
return Style::instance().themeColor("border");
2017-09-29 17:15:25 +00:00
} else {
2017-10-06 05:29:38 +00:00
return d->inputLineColor;
2017-09-29 17:15:25 +00:00
}
}
2022-02-17 03:08:05 +00:00
void TextField::setShowInputLine(bool value)
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-10-06 05:29:38 +00:00
if (d->showInputLine == value) {
return;
}
2017-10-06 05:29:38 +00:00
d->showInputLine = value;
update();
}
2022-02-17 03:08:05 +00:00
bool TextField::hasInputLine() const
2017-10-05 14:35:22 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(const TextField);
2017-10-05 14:35:22 +00:00
2017-10-06 05:29:38 +00:00
return d->showInputLine;
2017-10-05 14:35:22 +00:00
}
2022-02-17 03:08:05 +00:00
TextField::TextField(TextFieldPrivate &d, QWidget *parent)
: QLineEdit(parent),
d_ptr(&d)
{
d_func()->init();
}
2017-09-29 17:15:25 +00:00
/*!
* \reimp
*/
2022-02-17 03:08:05 +00:00
bool TextField::event(QEvent *event)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
switch (event->type())
{
case QEvent::Resize:
case QEvent::Move: {
if (d->label) {
d->label->setGeometry(rect());
}
}
default:
break;
}
return QLineEdit::event(event);
}
/*!
* \reimp
*/
2022-02-17 03:08:05 +00:00
void TextField::paintEvent(QPaintEvent *event)
2017-09-29 17:15:25 +00:00
{
2022-02-17 03:08:05 +00:00
Q_D(TextField);
2017-09-29 17:15:25 +00:00
QLineEdit::paintEvent(event);
QPainter painter(this);
const qreal progress = d->stateMachine->progress();
if (text().isEmpty() && progress < 1)
{
painter.setOpacity(1-progress);
painter.fillRect(rect(), parentWidget()->palette().color(backgroundRole()));
}
const int y = height()-1;
const int wd = width()-5;
2017-10-06 05:29:38 +00:00
if (d->showInputLine)
{
QPen pen;
pen.setWidth(1);
2017-10-06 05:29:38 +00:00
pen.setColor(inputLineColor());
2017-10-06 07:28:05 +00:00
if (!isEnabled())
pen.setStyle(Qt::DashLine);
2017-10-06 07:28:05 +00:00
painter.setPen(pen);
painter.setOpacity(1);
2017-10-06 05:03:01 +00:00
painter.drawLine(QLineF(2.5, y, wd, y));
2017-09-29 17:15:25 +00:00
2017-10-05 14:35:22 +00:00
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(inkColor());
2017-09-29 17:15:25 +00:00
2017-10-05 14:35:22 +00:00
if (progress > 0)
{
painter.setPen(Qt::NoPen);
painter.setBrush(brush);
const int w = (1-progress)*static_cast<qreal>(wd/2);
painter.drawRect(w+2.5, height()-2, wd-w*2, 2);
}
}
2017-09-29 17:15:25 +00:00
}
2022-02-17 03:08:05 +00:00
}