-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawingcanvas.cpp
165 lines (131 loc) · 3.99 KB
/
drawingcanvas.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
#include "drawingcanvas.h"
#include <QPainter>
#include <QPainterPath>
#include <QtSvg/QSvgGenerator>
#include <QFileDialog>
#include <QElapsedTimer>
#include <QOpenGLFramebufferObject>
#include <math.h>
DrawingCanvas::DrawingCanvas(QQuickItem *parent) : QQuickPaintedItem(parent)
{
m_penWidth = 4;
setRenderTarget(FramebufferObject);
}
bool DrawingCanvas::drawing() const
{
return m_drawing;
}
void DrawingCanvas::penPressed(QPointF pos)
{
setDrawing(true);
m_currentOutline.addPoint(pos);
m_lastPoint = pos;
}
void DrawingCanvas::penMoved(QPointF pos)
{
if(drawing()){
m_currentOutline.addPoint(pos);
// draw the points on the buffer
drawOnBuffer(pos);
}
m_lastPoint = pos;
}
void DrawingCanvas::penReleased()
{
setDrawing(false);
m_outlines.append(m_currentOutline);
m_currentOutline.clear();
m_lastPoint = QPointF();
}
void DrawingCanvas::paint(QPainter *painter)
{
painter->drawImage(m_updateRect, m_buffer, m_updateRect);
m_updateRect = QRect();
}
QString DrawingCanvas::penColor() const
{
return m_penColor;
}
int DrawingCanvas::penWidth() const
{
return m_penWidth;
}
void DrawingCanvas::setDrawing(bool drawing)
{
if (m_drawing == drawing)
return;
m_drawing = drawing;
emit drawingChanged(m_drawing);
}
void DrawingCanvas::setPenWidth(int penWidth)
{
if (m_penWidth == penWidth)
return;
m_penWidth = penWidth;
emit penWidthChanged(m_penWidth);
}
void DrawingCanvas::setPenColor(QString penColor)
{
if (m_penColor == penColor)
return;
m_penColor = penColor;
emit penColorChanged(m_penColor);
}
void DrawingCanvas::initiateBuffer()
{
qDebug() << this << "Initiating buffer" << width() << height();
m_buffer = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
m_buffer.fill(Qt::transparent);
}
void DrawingCanvas::drawOnBuffer(QPointF pos)
{
int rad = (penWidth()) + 50;
QPainter bufferPainter;
if(bufferPainter.begin(&m_buffer)){
bufferPainter.setRenderHints(QPainter::SmoothPixmapTransform);
bufferPainter.setRenderHint(QPainter::Antialiasing);
bufferPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
int pointsLength = m_currentOutline.points.length();
QPainterPath path;
if(pointsLength > 2){
auto previousPoint = m_currentOutline.points.at(pointsLength - 3);
auto mid1 = (m_lastPoint + previousPoint)/2;
auto mid2 = (pos + m_lastPoint)/2;
path.moveTo(m_lastPoint);
path.lineTo(pos);
qreal centerRadius = 1.5;
auto pathLength = path.length();
qreal step = std::min(centerRadius*2/(pathLength)/10, 1.0);
for (qreal i = 0.0; i <= 1; i+=step){
QPointF centerPoint = path.pointAtPercent(i);
QRadialGradient radialGrad(centerPoint, centerRadius);
radialGrad.setColorAt(0.000, QColor(0, 0, 0, 255));
radialGrad.setColorAt(0.2, QColor(0, 0, 0, 0.2 * 255));
radialGrad.setColorAt(1.000, QColor(0, 0, 0, 0));
QPen pen;
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
pen.setWidth(centerRadius * 2);
pen.setBrush(radialGrad);
bufferPainter.setPen(pen);
bufferPainter.drawPoint(centerPoint);
}
}
// update the canvas
auto dirtyRect = path.boundingRect().toRect().normalized()
.adjusted(-rad, -rad, +rad, +rad);
// change the canvas dirty region
if(m_updateRect.isNull()){
m_updateRect = dirtyRect;
}
else{
m_updateRect = m_updateRect.united(dirtyRect);
}
update(m_updateRect);
m_lastPoint = pos;
}
else{
qDebug() << this << "Error initiating buffer painter";
}
// qDebug() << "DRAWING IN " << timer.elapsed() << "ms";
}