forked from sandsmark/recrossable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawablecell.cpp
188 lines (159 loc) · 4.8 KB
/
drawablecell.cpp
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
#include "drawablecell.h"
#include <QImage>
#include <QPainter>
#include "characterrecognizer.h"
#include <QDebug>
#ifdef REMARKABLE_DEVICE
#include <epframebuffer.h>
#endif
DrawableCell::DrawableCell()
{
setAcceptedMouseButtons(Qt::AllButtons);
m_drawn = QImage(width(), height(), QImage::Format_Grayscale8);
m_drawn.fill(Qt::white);
}
void DrawableCell::paint(QPainter *painter)
{
painter->drawImage(boundingRect(), m_drawn);
if (!m_recognized.isEmpty()) {
QFont font;
font.setPixelSize(height() / 4);
painter->setFont(font);
painter->drawText(boundingRect().marginsRemoved(QMarginsF(2, 2, 2, 2)), Qt::AlignRight | Qt::AlignBottom, m_recognized);
}
}
void DrawableCell::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
Q_UNUSED(oldGeometry);
m_drawn = QImage(newGeometry.size().toSize(), QImage::Format_Grayscale8);
m_drawn.fill(Qt::white);
}
void DrawableCell::mousePressEvent(QMouseEvent *event)
{
if (event->source() == Qt::MouseEventSynthesizedByQt) {
return;
}
if (!isEnabled()) {
return;
}
m_lastPoint = event->localPos();
}
void DrawableCell::mouseMoveEvent(QMouseEvent *event)
{
if (event->source() == Qt::MouseEventSynthesizedByQt) {
return;
}
if (!isEnabled()) {
return;
}
if (!contains(event->localPos())) {
return;
}
m_recognized.clear();
QPainter p(&m_drawn);
p.setPen(QPen(Qt::black, 6));
p.drawLine(m_lastPoint, event->localPos());
#ifdef REMARKABLE_DEVICE
{
QPainter fbPainter(EPFrameBuffer::framebuffer());
fbPainter.setClipRect(mapRectToScene(boundingRect()));
fbPainter.setPen(QPen(Qt::black, 6));
const QPoint globalStart = mapToScene(m_lastPoint).toPoint();
const QPoint globalEnd = event->globalPos();
fbPainter.drawLine(globalStart, globalEnd);
fbPainter.end();
EPFrameBuffer::sendUpdate(QRect(globalStart, globalEnd).normalized().marginsAdded(QMargins(24, 24, 24, 24 )), EPFrameBuffer::Mono, EPFrameBuffer::PartialUpdate);
}
#else
update();
#endif
m_lastPoint = event->localPos();
}
void DrawableCell::mouseReleaseEvent(QMouseEvent *event)
{
if (event->source() == Qt::MouseEventSynthesizedByQt) {
return;
}
if (!isEnabled()) {
return;
}
Q_UNUSED(event);
if (m_drawn.format() != QImage::Format_Grayscale8) {
qWarning() << "Invalid format" << m_drawn.format();
m_drawn = m_drawn.convertToFormat(QImage::Format_Grayscale8);
}
int beginY = m_drawn.height();
int beginX = m_drawn.width();
int endY = 0;
int endX = 0;
int black = 0;
for (int y=0; y<m_drawn.height(); y++) {
const uchar *line = m_drawn.scanLine(y);
for (int x=0; x<m_drawn.width(); x++) {
if (line[x] < 128) {
beginY = std::min(y, beginY);
beginX = std::min(x, beginX);
endY = std::max(endY, y);
endX = std::max(endX, x);
black++;
}
}
}
beginX++;
beginY++;
endX--;
endY--;
if (endX == 0 || endY == 0) {
qWarning() << "empty";
return;
}
const int width = endX - beginX;
const int centerX = beginX + width / 2;
const int height = endY - beginY;
const int centerY = beginY + height / 2;
const int edge = std::max(width, height);
if (beginX >= endX || beginY >= endY) {
qWarning() << "invalid";
return;
}
QRect cropRect(0, 0, edge, edge);
cropRect.moveCenter(QPoint(centerX, centerY));
if (cropRect.top() < 0) {
cropRect.moveTop(0);
}
if (cropRect.left() < 0) {
cropRect.moveLeft(0);
}
if (cropRect.right() >= m_drawn.width()) {
cropRect.moveRight(m_drawn.width() - 1);
}
if (cropRect.bottom() >= m_drawn.height()) {
cropRect.moveBottom(m_drawn.height() - 1);
}
QImage scaled = m_drawn.copy(cropRect).scaled(20, 20);
scaled.invertPixels();
scaled = scaled.copy(-4, -4, 28, 28).convertToFormat(QImage::Format_Grayscale8);
for (int y=0; y<scaled.height(); y++) {
const uchar *line = scaled.scanLine(y);
for (int x=0; x<scaled.width(); x++) {
if (line[x] > 128) {
black++;
}
}
}
// Check if the user has tried to cover up something wrong
if (black > (m_drawn.width() * m_drawn.height()) / 4) {
qDebug() << "Clearing";
m_drawn.fill(Qt::white);
m_recognized.clear();
emit recognizedChanged();
update();
return;
}
QString recognized = CharacterRecognizer::instance()->recognize(scaled);
if (recognized != m_recognized) {
m_recognized = recognized;
emit recognizedChanged();
update();
}
}