-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.cpp
More file actions
73 lines (55 loc) · 1.92 KB
/
Copy pathcanvas.cpp
File metadata and controls
73 lines (55 loc) · 1.92 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
#include <Canvas.h>
Canvas::Canvas(QRect canvasPosition):offScreenBuffer(this) {
// Set window size including top left corner position, height, and width
setGeometry(canvasPosition);
setTitle("SimpleCpp");
// Set screen buffer size to window's size
auto screenSize = QSize(width(), height());
offScreenBuffer.resize(screenSize);
// Fill the screen buffer with a background colour
auto brush = QGuiApplication::palette().brush(QPalette::Window);
auto fillRegion = QRegion(0, 0, screenSize.width(), screenSize.height());
offScreenBuffer.beginPaint(fillRegion);
QPaintDevice *device = offScreenBuffer.paintDevice();
Q_ASSERT(device != nullptr);
QPainter painter(device);
painter.fillRect(fillRegion.boundingRect(), brush);
painter.end();
offScreenBuffer.endPaint();
// Show the window
show();
// Wait till window is exposed
while(!isExposed()) {
QGuiApplication::processEvents();
QThread::msleep(100);
}
}
void Canvas::exposeEvent(QExposeEvent *e)
{
if (isExposed())
render(e->region());
}
void Canvas::render(const QRegion &dirtyRegion) {
if (!isExposed())
return;
// Flush screen buffer to window to update dirty region
offScreenBuffer.flush(dirtyRegion.boundingRect());
}
void Canvas::drawLine(XPoint start, XPoint end, Color lineColor, unsigned int lineWidth) {
auto fillRegion = QRegion(0, 0, width(), height());
// Get resources
QPen pen(lineColor);
pen.setWidth(lineWidth);
offScreenBuffer.beginPaint(fillRegion);
QPaintDevice *device = offScreenBuffer.paintDevice();
Q_ASSERT(device != nullptr);
QPainter painter(device);
// Do the drawing
painter.setPen(pen);
painter.drawLine(start, end);
painter.end();
offScreenBuffer.endPaint();
// Update the window
offScreenBuffer.flush(fillRegion.boundingRect());
QGuiApplication::processEvents();
}