-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvas.cpp
102 lines (84 loc) · 2.4 KB
/
canvas.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
#include <QDebug>
#include <QPainter>
#include <canvas.hpp>
Canvas::Canvas(QWidget *parent) :
QWidget(parent),
m_repaintToSave(false),
m_tool(Pencil),
m_old_mouse_pos(),
m_mouse_pos(),
m_pixmap(),
m_color(),
m_tool_size(5, 5)
{
}
void Canvas::setPaintColor(QColor color) {
m_color = color;
}
void Canvas::setTool(AdaptinkTool tool) {
m_tool = tool;
}
void Canvas::setToolSize(int size) {
m_tool_size.setHeight(size);
m_tool_size.setWidth(size);
}
void Canvas::setPixmap(QPixmap pixmap) {
m_pixmap = pixmap;
}
QPixmap Canvas::getPixmap() {
return m_pixmap;
}
// === protected ===
void Canvas::mousePressEvent(QMouseEvent* event) {
m_old_mouse_pos = event->pos();
m_mouse_pos = event->pos();
draw();
}
void Canvas::mouseMoveEvent(QMouseEvent* event) {
m_old_mouse_pos = m_mouse_pos;
m_mouse_pos = event->pos();
draw();
}
void Canvas::paintEvent(QPaintEvent*) {
QPainter painter(this);
painter.drawPixmap(0, 0, m_pixmap);
if (m_repaintToSave) {
QPoint brush_size(m_tool_size.width(), m_tool_size.height());
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::NoBrush);
switch (m_tool) {
case Pencil:
painter.setBrush(m_color);
painter.drawEllipse(m_mouse_pos - brush_size / 4, brush_size.x(), brush_size.y());
painter.setPen(QPen(m_color, brush_size.x() * 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawLine(m_old_mouse_pos - brush_size / 4, m_mouse_pos - brush_size / 4);
break;
case Paintbrush:
painter.setBrush(m_color);
painter.drawEllipse(m_mouse_pos - brush_size / 4, brush_size.x(), brush_size.y());
painter.setPen(QPen(m_color, brush_size.x() * 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawLine(m_old_mouse_pos - brush_size / 4, m_mouse_pos - brush_size / 4);
break;
case Label:
painter.setPen(m_color);
painter.setFont(QFont("Arial", m_tool_size.height()));
painter.drawText(QRect(m_mouse_pos.x(), m_mouse_pos.y(), width(), height()), Qt::AlignmentFlag::AlignLeft, "WOW");
break;
case Rubber:
painter.setBrush(Qt::GlobalColor::transparent);
painter.setCompositionMode(QPainter::CompositionMode_Clear);
painter.drawRect(QRect(m_mouse_pos - brush_size / 2, m_mouse_pos + brush_size / 2));
break;
default:
qDebug() << "Unexpected default case reached.";
break;
}
}
}
// === private ===
void Canvas::draw() {
m_repaintToSave = true;
m_pixmap = QPixmap(grab());
m_repaintToSave = false;
repaint();
}