-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathribbon.cpp
More file actions
105 lines (91 loc) · 2.21 KB
/
ribbon.cpp
File metadata and controls
105 lines (91 loc) · 2.21 KB
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
#include "ribbon.h"
#include <QBrush>
#include <QEvent>
#include <QPainter>
#include <QPen>
#include <QRectF>
QFont ribbon::font{"Consolas", 16};
ribbon::ribbon(QWidget *parent)
: QWidget{parent}
{
setFixedHeight(HEIGHT);
for (int i = 0; i < SIZE; ++i) {
arr[i] = '_';
}
}
uchar ribbon::get() const noexcept
{
std::lock_guard lk(m);
return arr[cursor];
}
void ribbon::move_right() noexcept
{
std::lock_guard lk(m);
if (++cursor >= SIZE) {
cursor = 0;
}
update();
}
void ribbon::move_left() noexcept
{
std::lock_guard lk(m);
if (--cursor < 0) {
cursor = SIZE - 1;
}
update();
}
void ribbon::set(uchar value) noexcept
{
std::lock_guard lk(m);
arr[cursor] = value;
update();
}
void ribbon::paintEvent(QPaintEvent *e) noexcept
{
std::lock_guard lk(m);
const int width = this->width();
const int height = HEIGHT;
const int number_cells = width / BATCH;
const int begin = (width % BATCH) / 2;
const int half_width = number_cells / 2;
QPainter p(this);
p.setPen(QPen(Qt::black));
p.setBrush(QBrush(Qt::black));
p.setFont(font);
p.drawRect(0, 0, width, height);
p.setPen(QPen(Qt::white));
p.setBrush(QBrush(Qt::white));
for (int i = 0; i < number_cells; ++i) {
p.setPen(QPen(Qt::white));
p.drawRect(begin + (BATCH * i) + 1, 1, BATCH - 2, BATCH - 2);
p.setPen(QPen(Qt::black));
p.drawText(
QRectF{static_cast<qreal>(begin + (BATCH * i) + 2), 2, BATCH - 4, BATCH - 4},
QString(QChar{at(-half_width + i)}),
QTextOption{Qt::AlignHCenter | Qt::AlignVCenter}
);
if (-half_width + i == 0) {
p.setPen(QPen(Qt::green));
p.setBrush(QBrush(Qt::green));
p.drawRect(begin + (BATCH * i)+1, BATCH, BATCH-2, UNDERLINE);
p.setPen(QPen(Qt::white));
p.setBrush(QBrush(Qt::white));
}
}
}
void ribbon::enterEvent(QEnterEvent *) noexcept
{
setFocus();
}
void ribbon::leaveEvent(QEvent *) noexcept
{
clearFocus();
}
uchar ribbon::at(int index) noexcept
{
index = (cursor + index) % SIZE;
if (index < 0) {
index += SIZE;
}
return arr[index];
}