-
Notifications
You must be signed in to change notification settings - Fork 2
/
whiteboard.cpp
101 lines (87 loc) · 2.36 KB
/
whiteboard.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
/*
* Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
* Joshua Henderson <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <QMouseEvent>
#include <QPainter>
#include <QPen>
#include "whiteboard.h"
WhiteBoard::WhiteBoard(QWidget* parent)
: QWidget(parent),
m_drawing(false),
m_modified(false),
m_penWidth(3),
m_penColor(Qt::blue)
{
setAttribute(Qt::WA_StaticContents);
}
void WhiteBoard::setPenColor(const QColor& newColor)
{
m_penColor = newColor;
}
void WhiteBoard::clearImage()
{
m_image.fill(Qt::white);
m_modified = true;
update();
}
void WhiteBoard::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
{
m_lastPoint = event->pos();
m_drawing = true;
}
}
void WhiteBoard::mouseMoveEvent(QMouseEvent* event)
{
if ((event->buttons() & Qt::LeftButton) && m_drawing)
drawLineTo(event->pos());
}
void WhiteBoard::mouseReleaseEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton && m_drawing)
{
drawLineTo(event->pos());
m_drawing = false;
}
}
void WhiteBoard::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, m_image, dirtyRect);
}
void WhiteBoard::resizeEvent(QResizeEvent* event)
{
if (width() > m_image.width() || height() > m_image.height())
{
int newWidth = qMax(width() + 128, m_image.width());
int newHeight = qMax(height() + 128, m_image.height());
resizeImage(&m_image, QSize(newWidth, newHeight));
update();
}
QWidget::resizeEvent(event);
}
void WhiteBoard::resizeImage(QImage* image, const QSize& newSize)
{
if (image->size() == newSize)
return;
QImage newImage(newSize, QImage::Format_RGB32);
newImage.fill(qRgb(255, 255, 255));
QPainter painter(&newImage);
painter.drawImage(QPoint(0, 0), *image);
*image = newImage;
}
void WhiteBoard::drawLineTo(const QPoint &endPoint)
{
QPainter painter(&m_image);
painter.setPen(QPen(m_penColor, m_penWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawLine(m_lastPoint, endPoint);
m_modified = true;
int rad = (m_penWidth / 2) + 2;
update(QRect(m_lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad));
m_lastPoint = endPoint;
}