1 /* This file is part of the KDE project
2 * Copyright (C) 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 #include "ScribbleArea.h"
23 #include <QMouseEvent>
24 #include <QPainter>
25
ScribbleArea(QQuickItem * parent)26 ScribbleArea::ScribbleArea(QQuickItem* parent)
27 : QQuickPaintedItem(parent)
28 , scribbling(false)
29 , myPenWidth(10)
30 , myPenColor(QColor(242, 178, 0))
31 {
32 setAcceptedMouseButtons(Qt::LeftButton);\
33 image = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
34 image.fill(Qt::transparent);
35 }
36
~ScribbleArea()37 ScribbleArea::~ScribbleArea()
38 {
39 }
40
clear()41 void ScribbleArea::clear()
42 {
43 image.fill(Qt::transparent);
44 }
45
color() const46 QColor ScribbleArea::color() const
47 {
48 return myPenColor;
49 }
50
setColor(const QColor & newColor)51 void ScribbleArea::setColor(const QColor& newColor)
52 {
53 myPenColor = newColor;
54 emit colorChanged();
55 }
56
penWidth() const57 int ScribbleArea::penWidth() const
58 {
59 return myPenWidth;
60 }
61
setPenWidth(const int & newWidth)62 void ScribbleArea::setPenWidth(const int& newWidth)
63 {
64 myPenWidth = newWidth;
65 emit penWidthChanged();
66 }
67
event(QEvent * event)68 bool ScribbleArea::event(QEvent* event)
69 {
70 if(event->type() == QEvent::Resize) {
71 image = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
72 image.fill(Qt::transparent);
73 }
74 return QQuickPaintedItem::event(event);
75 }
76
mousePressEvent(QMouseEvent * event)77 void ScribbleArea::mousePressEvent(QMouseEvent* event)
78 {
79 if (event->button() == Qt::LeftButton) {
80 lastPoint = event->pos();
81 scribbling = true;
82 emit paintingStarted();
83 }
84 }
85
mouseMoveEvent(QMouseEvent * event)86 void ScribbleArea::mouseMoveEvent(QMouseEvent* event)
87 {
88 if ((event->buttons() & Qt::LeftButton) && scribbling)
89 drawLineTo(event->pos());
90 }
91
mouseReleaseEvent(QMouseEvent * event)92 void ScribbleArea::mouseReleaseEvent(QMouseEvent* event)
93 {
94 if (event->button() == Qt::LeftButton && scribbling) {
95 drawLineTo(event->pos());
96 scribbling = false;
97 emit paintingStopped();
98 }
99 }
100
paint(QPainter * painter)101 void ScribbleArea::paint(QPainter* painter)
102 {
103 painter->drawImage(boundingRect(), image);
104 }
105
drawLineTo(const QPointF & endPoint)106 void ScribbleArea::drawLineTo(const QPointF& endPoint)
107 {
108 if(image.isNull()) {
109 image = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
110 image.fill(Qt::transparent);
111 }
112 QPainter painter(&image);
113 painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
114 Qt::RoundJoin));
115 painter.setRenderHint(QPainter::Antialiasing, true);
116 painter.drawLine(lastPoint, endPoint);
117 painter.end();
118
119 int rad = (myPenWidth / 2) + 2;
120 update(QRectF(lastPoint, endPoint).normalized()
121 .adjusted(-rad, -rad, +rad, +rad).toRect());
122 lastPoint = endPoint;
123 }
124