summary refs log tree commit diff
path: root/src/ui/TextField.cpp
blob: 941d00a3dab53f9a8cb38018e54213e6ae062b72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "TextField.h"

#include <QCoreApplication>
#include <QEventTransition>
#include <QFontDatabase>
#include <QPaintEvent>
#include <QPainter>
#include <QPropertyAnimation>

TextField::TextField(QWidget *parent)
  : QLineEdit(parent)
{
        // Get rid of the focus border on macOS.
        setAttribute(Qt::WA_MacShowFocusRect, 0);

        QPalette pal;

        state_machine_    = new TextFieldStateMachine(this);
        label_            = nullptr;
        label_font_size_  = 15;
        show_label_       = false;
        background_color_ = pal.color(QPalette::Window);
        is_valid_         = true;

        setFrame(false);
        setAttribute(Qt::WA_Hover);
        setMouseTracking(true);
        setTextMargins(0, 4, 0, 6);

        state_machine_->start();
        QCoreApplication::processEvents();
}

void
TextField::setBackgroundColor(const QColor &color)
{
        background_color_ = color;
}

QColor
TextField::backgroundColor() const
{
        return background_color_;
}

void
TextField::setShowLabel(bool value)
{
        if (show_label_ == value) {
                return;
        }

        show_label_ = value;

        if (!label_ && value) {
                label_ = new TextFieldLabel(this);
                state_machine_->setLabel(label_);
        }

        if (value) {
                setContentsMargins(0, 23, 0, 0);
        } else {
                setContentsMargins(0, 0, 0, 0);
        }
}

bool
TextField::hasLabel() const
{
        return show_label_;
}

bool
TextField::isValid() const
{
        return is_valid_;
}

void
TextField::setValid(bool valid)
{
        is_valid_ = valid;
}

void
TextField::setLabelFontSize(qreal size)
{
        label_font_size_ = size;

        if (label_) {
                QFont font(label_->font());
                font.setPointSizeF(size);
                label_->setFont(font);
                label_->update();
        }
}

qreal
TextField::labelFontSize() const
{
        return label_font_size_;
}

void
TextField::setLabel(const QString &label)
{
        label_text_ = label;
        setShowLabel(true);
        label_->update();
}

QString
TextField::label() const
{
        return label_text_;
}

void
TextField::setLabelColor(const QColor &color)
{
        label_color_ = color;
        update();
}

QColor
TextField::labelColor() const
{
        if (!label_color_.isValid()) {
                return QPalette().color(QPalette::Text);
        }

        return label_color_;
}

void
TextField::setInkColor(const QColor &color)
{
        ink_color_ = color;
        update();
}

QColor
TextField::inkColor() const
{
        if (!ink_color_.isValid()) {
                return QPalette().color(QPalette::Text);
        }

        return ink_color_;
}

void
TextField::setUnderlineColor(const QColor &color)
{
        underline_color_ = color;
        update();
}

QColor
TextField::underlineColor() const
{
        if (!underline_color_.isValid()) {
                if ((hasAcceptableInput() && isValid()) || !isModified())
                        return QPalette().color(QPalette::Highlight);
                else
                        return Qt::red;
        }

        return underline_color_;
}

bool
TextField::event(QEvent *event)
{
        switch (event->type()) {
        case QEvent::Resize:
        case QEvent::Move: {
                if (label_)
                        label_->setGeometry(rect());
                break;
        }
        default:
                break;
        }

        return QLineEdit::event(event);
}

void
TextField::paintEvent(QPaintEvent *event)
{
        QLineEdit::paintEvent(event);

        QPainter painter(this);

        if (text().isEmpty()) {
                painter.setOpacity(1 - state_machine_->progress());
                painter.fillRect(rect(), backgroundColor());
        }

        const int y  = height() - 1;
        const int wd = width() - 5;

        QPen pen;
        pen.setWidth(1);
        pen.setColor(underlineColor());
        painter.setPen(pen);
        painter.setOpacity(1);
        painter.drawLine(2, y, wd, y);

        QBrush brush;
        brush.setStyle(Qt::SolidPattern);
        brush.setColor(inkColor());

        const qreal progress = state_machine_->progress();

        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 - 2 * w, 2);
        }
}

TextFieldStateMachine::TextFieldStateMachine(TextField *parent)
  : QStateMachine(parent)
  , text_field_(parent)
{
        normal_state_  = new QState;
        focused_state_ = new QState;

        label_       = nullptr;
        offset_anim_ = nullptr;
        color_anim_  = nullptr;
        progress_    = 0.0;

        addState(normal_state_);
        addState(focused_state_);

        setInitialState(normal_state_);

        QEventTransition *transition;
        QPropertyAnimation *animation;

        transition = new QEventTransition(parent, QEvent::FocusIn);
        transition->setTargetState(focused_state_);
        normal_state_->addTransition(transition);

        animation = new QPropertyAnimation(this, "progress", this);
        animation->setEasingCurve(QEasingCurve::InCubic);
        animation->setDuration(310);
        transition->addAnimation(animation);

        transition = new QEventTransition(parent, QEvent::FocusOut);
        transition->setTargetState(normal_state_);
        focused_state_->addTransition(transition);

        animation = new QPropertyAnimation(this, "progress", this);
        animation->setEasingCurve(QEasingCurve::OutCubic);
        animation->setDuration(310);
        transition->addAnimation(animation);

        normal_state_->assignProperty(this, "progress", 0);
        focused_state_->assignProperty(this, "progress", 1);

        setupProperties();

        connect(text_field_, SIGNAL(textChanged(QString)), this, SLOT(setupProperties()));
}

void
TextFieldStateMachine::setLabel(TextFieldLabel *label)
{
        if (label_) {
                delete label_;
        }

        if (offset_anim_) {
                removeDefaultAnimation(offset_anim_);
                delete offset_anim_;
        }

        if (color_anim_) {
                removeDefaultAnimation(color_anim_);
                delete color_anim_;
        }

        label_ = label;

        if (label_) {
                offset_anim_ = new QPropertyAnimation(label_, "offset", this);
                offset_anim_->setDuration(210);
                offset_anim_->setEasingCurve(QEasingCurve::OutCubic);
                addDefaultAnimation(offset_anim_);

                color_anim_ = new QPropertyAnimation(label_, "color", this);
                color_anim_->setDuration(210);
                addDefaultAnimation(color_anim_);
        }

        setupProperties();
}

void
TextFieldStateMachine::setupProperties()
{
        if (label_) {
                const int m = text_field_->textMargins().top();

                if (text_field_->text().isEmpty()) {
                        normal_state_->assignProperty(label_, "offset", QPointF(0, 26));
                } else {
                        normal_state_->assignProperty(label_, "offset", QPointF(0, 0 - m));
                }

                focused_state_->assignProperty(label_, "offset", QPointF(0, 0 - m));
                focused_state_->assignProperty(label_, "color", text_field_->inkColor());
                normal_state_->assignProperty(label_, "color", text_field_->labelColor());

                if (0 != label_->offset().y() && !text_field_->text().isEmpty()) {
                        label_->setOffset(QPointF(0, 0 - m));
                } else if (!text_field_->hasFocus() && label_->offset().y() <= 0 &&
                           text_field_->text().isEmpty()) {
                        label_->setOffset(QPointF(0, 26));
                }
        }

        text_field_->update();
}

TextFieldLabel::TextFieldLabel(TextField *parent)
  : QWidget(parent)
  , text_field_(parent)
{
        x_     = 0;
        y_     = 26;
        scale_ = 1;
        color_ = parent->labelColor();

        QFont font;
        font.setWeight(60);
        font.setLetterSpacing(QFont::PercentageSpacing, 102);
        setFont(font);
}

void
TextFieldLabel::paintEvent(QPaintEvent *)
{
        if (!text_field_->hasLabel())
                return;

        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        painter.scale(scale_, scale_);
        painter.setPen(color_);
        painter.setOpacity(1);

        QPointF pos(2 + x_, height() - 36 + y_);
        painter.drawText(pos.x(), pos.y(), text_field_->label());
}