-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpainter.cpp
More file actions
146 lines (125 loc) · 3.17 KB
/
painter.cpp
File metadata and controls
146 lines (125 loc) · 3.17 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
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
#include "painter.h"
#include <QPainter>
#include <QDebug>
#include <QSizePolicy>
Painter::Painter(QWidget *parent) : QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
}
void Painter::setSize(int width, int height)
{
setFixedSize(width, height);
if (frame != nullptr)
{
}
}
void Painter::setFrame(AnimationFrame *f)
{
frame = f;
setSize(frame->getWidth(), frame->getHeight());
//QPainter p{this};
//p.setPen(pen);
//QRect r{0, 0, frame->getWidth(), frame->getHeight()};
//p.drawImage(r, frame->image, r);
//qDebug() << "Redrew" << r;
update();
}
void Painter::clearImage()
{
if (frame == nullptr) return;
frame->image.fill(qRgb(255, 255, 255));
modified = true;
update();
}
void Painter::setPenWidth(int width)
{
pen.setWidth(width);
}
int Painter::getPenWidth()
{
return pen.width();
}
void Painter::setPenColor(QColor &color)
{
penColor = color;
pen.setColor(penColor);
}
QColor Painter::getPenColor()
{
return penColor;
}
void Painter::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
lastPoint = event->pos();
drawing = true;
drawLineTo(event->pos());
}
qDebug() << "Clicked at" << event->pos();
}
void Painter::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::LeftButton) && drawing)
{
drawLineTo(event->pos());
}
}
void Painter::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton && drawing)
{
drawLineTo(event->pos());
drawing = false;
}
}
void Painter::paintEvent(QPaintEvent *event)
{
if (frame == nullptr) return;
QPainter painter(this);
painter.setPen(pen);
QRect r = event->rect();
painter.drawImage(r, frame->image, r);
}
void Painter::resizeEvent(QResizeEvent *event)
{
if (frame == nullptr) return;
auto image = &frame->image;
if (width() > image->width() || height() > image->height()) {
int newWidth = qMax(width() + 128, image->width());
int newHeight = qMax(height() + 128, image->height());
resizeImage(image, QSize(newWidth, newHeight));
update();
}
QWidget::resizeEvent(event);
}
void Painter::drawLineTo(const QPoint &point)
{
if (frame == nullptr) return;
qDebug() << "Is frame image null?" << frame->image.isNull();
QPainter painter(&frame->image);
if (tool == Tool::pen)
pen = QPen(penColor, getPenWidth(), Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin);
else
pen = QPen(Qt::white, getPenWidth(), Qt::SolidLine,
Qt::RoundCap, Qt::RoundJoin);
painter.drawLine(lastPoint, point);
modified = true;
frame->update();
int rad = (getPenWidth() / 2) + 2;
update(QRect(lastPoint, point)
.normalized()
.adjusted(-rad, -rad, +rad, +rad));
lastPoint = point;
}
void Painter::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;
}