-
Notifications
You must be signed in to change notification settings - Fork 0
/
paintablecanvas.cpp
166 lines (137 loc) · 4.71 KB
/
paintablecanvas.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
#include "paintablecanvas.h"
#include <QPainter>
#include <QMimeData>
#include <QFileInfo>
#include <QImageReader>
PaintableCanvas::PaintableCanvas(QWidget* parent) : QWidget(parent), paintingEnabled(false)
{
brushSize = 10;
setAcceptDrops(true);
}
void PaintableCanvas::loadImage(const QString& imagePath) {
originalImage.load(imagePath);
Initialize();
emit OnImageSet();
}
void PaintableCanvas::loadImage(const QImage& Img)
{
originalImage = Img.copy();
Initialize();
emit OnImageSet();
}
void PaintableCanvas::setPaintingEnabled(bool enabled) {
paintingEnabled = enabled;
}
QImage PaintableCanvas::getImage() const
{
return originalImage;
}
QImage PaintableCanvas::getMask() const {
return maskImage;
}
void PaintableCanvas::enterEvent(QEnterEvent* event) {
QWidget::enterEvent(event);
if (!paintingEnabled)
return;
setCustomCursor();
}
void PaintableCanvas::leaveEvent(QEvent* event) {
QWidget::leaveEvent(event);
unsetCursor();
}
void PaintableCanvas::setCustomCursor() {
QPixmap pixmap(brushSize * 2 + 2, brushSize * 2 + 2);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
QPen pen(Qt::white, 2);
painter.setPen(pen);
painter.setBrush(Qt::NoBrush);
painter.drawEllipse(1, 1, brushSize * 2 - 1, brushSize * 2 - 1);
QCursor cursor(pixmap);
setCursor(cursor);
}
void PaintableCanvas::paintEvent(QPaintEvent* event) {
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawImage(0, 0, displayedImage);
if (!paintingEnabled)
return;
QImage scaledMask = maskImage.scaled(460, 460, Qt::KeepAspectRatio, Qt::SmoothTransformation);
painter.setOpacity(0.65f);
painter.drawImage(0, 0, scaledMask);
painter.setOpacity(1.0f);
}
void PaintableCanvas::mousePressEvent(QMouseEvent* event) {
if (!paintingEnabled) return;
lastPoint = event->pos();
}
void PaintableCanvas::mouseMoveEvent(QMouseEvent* event) {
if (!paintingEnabled || !(event->buttons() & Qt::LeftButton)) return;
double scaleFactorX = static_cast<double>(originalImage.width()) / displayedImage.width();
double scaleFactorY = static_cast<double>(originalImage.height()) / displayedImage.height();
QPoint scaledLastPoint(lastPoint.x() * scaleFactorX, lastPoint.y() * scaleFactorY);
QPoint scaledCurrentPoint(event->pos().x() * scaleFactorX, event->pos().y() * scaleFactorY);
QPainter painter(&maskImage);
QPen pen(QColor(255, 255, 255, 255), (brushSize + 4) * scaleFactorX, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); // 50% opacity
painter.setPen(pen);
painter.drawLine(scaledLastPoint, scaledCurrentPoint);
lastPoint = event->pos();
update();
}
void PaintableCanvas::mouseReleaseEvent(QMouseEvent* event) {
if (!paintingEnabled) return;
}
void PaintableCanvas::dragEnterEvent(QDragEnterEvent* event) {
if (event->mimeData()->hasUrls() && !event->mimeData()->urls().isEmpty()) {
QList<QUrl> urls = event->mimeData()->urls();
for (const QUrl& url : urls) {
QFileInfo fileInfo(url.toLocalFile());
if (fileInfo.exists() && fileInfo.isFile() && QImageReader::imageFormat(fileInfo.filePath()).isEmpty() == false) {
event->acceptProposedAction();
return;
}
}
}
}
void PaintableCanvas::dropEvent(QDropEvent* event) {
const QMimeData* mimeData = event->mimeData();
if (mimeData->hasUrls()) {
QList<QUrl> urls = mimeData->urls();
for (const QUrl& url : urls) {
QString filePath = url.toLocalFile();
QFileInfo fileInfo(filePath);
if (fileInfo.exists() && fileInfo.isFile() && QImageReader::imageFormat(filePath).isEmpty() == false) {
loadImage(filePath);
event->acceptProposedAction();
return;
}
}
}
}
void PaintableCanvas::wheelEvent(QWheelEvent* event) {
const int delta = event->angleDelta().y();
if (delta > 0) {
brushSize += 2;
}
else if (delta < 0 && brushSize > 2) {
brushSize -= 2;
}
setCustomCursor();
event->accept();
}
void PaintableCanvas::clearStrokes()
{
maskImage = QImage(originalImage.size(), QImage::Format_ARGB32_Premultiplied);
maskImage.fill(Qt::transparent);
update();
}
void PaintableCanvas::Initialize()
{
displayedImage = originalImage.scaled(460, 460, Qt::KeepAspectRatio, Qt::SmoothTransformation);
setMinimumSize(displayedImage.size());
maskImage = QImage(originalImage.size(), QImage::Format_ARGB32_Premultiplied);
maskImage.fill(Qt::transparent);
update();
}