Warning, file /office/calligra/gemini/ScribbleArea.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 * SPDX-FileCopyrightText: 2014 Dan Leinir Turthra Jensen <admin@leinir.dk> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 * 0006 */ 0007 0008 #include "ScribbleArea.h" 0009 #include <QMouseEvent> 0010 #include <QPainter> 0011 0012 ScribbleArea::ScribbleArea(QQuickItem* parent) 0013 : QQuickPaintedItem(parent) 0014 , scribbling(false) 0015 , myPenWidth(10) 0016 , myPenColor(QColor(242, 178, 0)) 0017 { 0018 setAcceptedMouseButtons(Qt::LeftButton);\ 0019 image = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied); 0020 image.fill(Qt::transparent); 0021 } 0022 0023 ScribbleArea::~ScribbleArea() 0024 { 0025 } 0026 0027 void ScribbleArea::clear() 0028 { 0029 image.fill(Qt::transparent); 0030 } 0031 0032 QColor ScribbleArea::color() const 0033 { 0034 return myPenColor; 0035 } 0036 0037 void ScribbleArea::setColor(const QColor& newColor) 0038 { 0039 myPenColor = newColor; 0040 emit colorChanged(); 0041 } 0042 0043 int ScribbleArea::penWidth() const 0044 { 0045 return myPenWidth; 0046 } 0047 0048 void ScribbleArea::setPenWidth(const int& newWidth) 0049 { 0050 myPenWidth = newWidth; 0051 emit penWidthChanged(); 0052 } 0053 0054 bool ScribbleArea::event(QEvent* event) 0055 { 0056 if(event->type() == QEvent::Resize) { 0057 image = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied); 0058 image.fill(Qt::transparent); 0059 } 0060 return QQuickPaintedItem::event(event); 0061 } 0062 0063 void ScribbleArea::mousePressEvent(QMouseEvent* event) 0064 { 0065 if (event->button() == Qt::LeftButton) { 0066 lastPoint = event->pos(); 0067 scribbling = true; 0068 emit paintingStarted(); 0069 } 0070 } 0071 0072 void ScribbleArea::mouseMoveEvent(QMouseEvent* event) 0073 { 0074 if ((event->buttons() & Qt::LeftButton) && scribbling) 0075 drawLineTo(event->pos()); 0076 } 0077 0078 void ScribbleArea::mouseReleaseEvent(QMouseEvent* event) 0079 { 0080 if (event->button() == Qt::LeftButton && scribbling) { 0081 drawLineTo(event->pos()); 0082 scribbling = false; 0083 emit paintingStopped(); 0084 } 0085 } 0086 0087 void ScribbleArea::paint(QPainter* painter) 0088 { 0089 painter->drawImage(boundingRect(), image); 0090 } 0091 0092 void ScribbleArea::drawLineTo(const QPointF& endPoint) 0093 { 0094 if(image.isNull()) { 0095 image = QImage(width(), height(), QImage::Format_ARGB32_Premultiplied); 0096 image.fill(Qt::transparent); 0097 } 0098 QPainter painter(&image); 0099 painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, 0100 Qt::RoundJoin)); 0101 painter.setRenderHint(QPainter::Antialiasing, true); 0102 painter.drawLine(lastPoint, endPoint); 0103 painter.end(); 0104 0105 int rad = (myPenWidth / 2) + 2; 0106 update(QRectF(lastPoint, endPoint).normalized() 0107 .adjusted(-rad, -rad, +rad, +rad).toRect()); 0108 lastPoint = endPoint; 0109 }