-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathWiggly.h
80 lines (69 loc) · 2.22 KB
/
Wiggly.h
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
#pragma once
#include <QBasicTimer>
#include <QFrame>
#include <QLineEdit>
#include <QMainWindow>
#include <QPaintEvent>
#include <QPainter>
#include <QVBoxLayout>
namespace Examples {
class WigglyWidget : public QWidget {
Q_OBJECT
public:
WigglyWidget(QWidget *parent = nullptr) : QWidget(parent) {
setBackgroundRole(QPalette::Midlight);
setAutoFillBackground(true);
auto newFont = font();
newFont.setPointSize(newFont.pointSize() + 20);
setFont(newFont);
timer.start(60, this);
}
public slots:
void setText(const QString &newText) {text = newText;}
protected:
void paintEvent(QPaintEvent* event) override {
static const std::vector<int> sins = {0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38};
QFontMetrics metrics(font());
auto pos = QPoint {(event->rect().width() - metrics.horizontalAdvance(text)) / 2, (event->rect().height() + metrics.ascent() - metrics.descent()) / 2};
QPainter painter(this);
for (auto i = 0; i < text.size(); ++i) {
auto index = (step + i) % sins.size();
painter.setPen(QColor::fromHsv(360.0f / sins.size() * index, 255, 191));
painter.drawText(pos - QPoint {0, (sins[index] * metrics.height()) / 400}, QString(text[i]));
pos.setX(pos.x() + metrics.horizontalAdvance(text[i]));
}
}
void timerEvent(QTimerEvent* event) override {
if (event->timerId() == timer.timerId()) {
++step;
update();
} else
QWidget::timerEvent(event);
}
private:
QBasicTimer timer;
QString text;
int step = 0;
};
class Window1 : public QMainWindow {
Q_OBJECT
public:
Window1() {
setCentralWidget(&frame);
setWindowTitle("Wiggly");
resize(330, 130);
connect(&lineEdit, &QLineEdit::textChanged, &wigglyWidget, &WigglyWidget::setText);
wigglyWidget.setGeometry(20, 20, 290, 60);
layout.addWidget(&wigglyWidget);
layout.addWidget(&lineEdit);
lineEdit.move(20, 90);
lineEdit.setGeometry(20, 90, 290, lineEdit.height());
lineEdit.setText("Habemus papam");
}
private:
QFrame frame;
QVBoxLayout layout {&frame};
WigglyWidget wigglyWidget;
QLineEdit lineEdit;
};
}