File indexing completed on 2024-05-05 17:04:27

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public License as
0006  * published by the Free Software Foundation; either version 2 of
0007  * the License or (at your option) version 3 or any later version
0008  * accepted by the membership of KDE e.V. (or its successor approved
0009  * by the membership of KDE e.V.), which shall act as a proxy
0010  * defined in Section 14 of version 3 of the license.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  *
0020  */
0021 
0022 #include "ScribbleArea.h"
0023 #include <QMouseEvent>
0024 #include <QPainter>
0025 
0026 ScribbleArea::ScribbleArea(QQuickItem* parent)
0027     : QQuickPaintedItem(parent)
0028     , scribbling(false)
0029     , myPenWidth(10)
0030     , myPenColor(QColor(242, 178, 0))
0031 {
0032     setAcceptedMouseButtons(Qt::LeftButton);\
0033     image = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
0034     image.fill(Qt::transparent);
0035 }
0036 
0037 ScribbleArea::~ScribbleArea()
0038 {
0039 }
0040 
0041 void ScribbleArea::clear()
0042 {
0043     image.fill(Qt::transparent);
0044 }
0045 
0046 QColor ScribbleArea::color() const
0047 {
0048     return myPenColor;
0049 }
0050 
0051 void ScribbleArea::setColor(const QColor& newColor)
0052 {
0053     myPenColor = newColor;
0054     emit colorChanged();
0055 }
0056 
0057 int ScribbleArea::penWidth() const
0058 {
0059     return myPenWidth;
0060 }
0061 
0062 void ScribbleArea::setPenWidth(const int& newWidth)
0063 {
0064     myPenWidth = newWidth;
0065     emit penWidthChanged();
0066 }
0067 
0068 bool ScribbleArea::event(QEvent* event)
0069 {
0070     if(event->type() == QEvent::Resize) {
0071         image = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
0072         image.fill(Qt::transparent);
0073     }
0074     return QQuickPaintedItem::event(event);
0075 }
0076 
0077 void ScribbleArea::mousePressEvent(QMouseEvent* event)
0078 {
0079     if (event->button() == Qt::LeftButton) {
0080         lastPoint = event->pos();
0081         scribbling = true;
0082         emit paintingStarted();
0083     }
0084 }
0085 
0086 void ScribbleArea::mouseMoveEvent(QMouseEvent* event)
0087 {
0088     if ((event->buttons() & Qt::LeftButton) && scribbling)
0089         drawLineTo(event->pos());
0090 }
0091 
0092 void ScribbleArea::mouseReleaseEvent(QMouseEvent* event)
0093 {
0094     if (event->button() == Qt::LeftButton && scribbling) {
0095         drawLineTo(event->pos());
0096         scribbling = false;
0097         emit paintingStopped();
0098     }
0099 }
0100 
0101 void ScribbleArea::paint(QPainter* painter)
0102 {
0103     painter->drawImage(boundingRect(), image);
0104 }
0105 
0106 void ScribbleArea::drawLineTo(const QPointF& endPoint)
0107 {
0108     if(image.isNull()) {
0109         image = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
0110         image.fill(Qt::transparent);
0111     }
0112     QPainter painter(&image);
0113     painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
0114                         Qt::RoundJoin));
0115     painter.setRenderHint(QPainter::Antialiasing, true);
0116     painter.drawLine(lastPoint, endPoint);
0117     painter.end();
0118 
0119     int rad = (myPenWidth / 2) + 2;
0120     update(QRectF(lastPoint, endPoint).normalized()
0121                                     .adjusted(-rad, -rad, +rad, +rad).toRect());
0122     lastPoint = endPoint;
0123 }