/
canvas.cpp
74 lines (63 loc) · 3.34 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
#include <QtGui>
#include "canvas.h"
QColor Canvas::Palette[16] = {
QColor("#FFFFFF"),
QColor("#000000"),
QColor("#00007F"),
QColor("#009300"),
QColor("#FF0000"),
QColor("#7F0000"),
QColor("#9C009C"),
QColor("#FC7F00"),
QColor("#FFFF00"),
QColor("#00FC00"),
QColor("#009393"),
QColor("#00FFFF"),
QColor("#0000FC"),
QColor("#FF00FF"),
QColor("#7F7F7F"),
QColor("#D2D2D2")
};
Canvas::Canvas(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
image = QImage(width(), height(), QImage::Format_RGB32);
image.fill(qRgb(255,255,255));
}
void Canvas::drawLines(const int color, const int lineWidth, const QVector<QPoint> &points)
{
QColor penColor = Canvas::Palette[color % 16];
QPen pen = QPen(penColor, lineWidth);
QPainter brush(&image);
brush.setPen(pen);
brush.drawLines(points);
update();
}
void Canvas::paintEvent(QPaintEvent *event)
{
QPainter brush(this);
QRect myrect = event->rect();
brush.drawImage(myrect, image, myrect);
}
void Canvas::resizeEvent(QResizeEvent *event)
{
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 Canvas::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;
}