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 #ifndef SCRIBBLEAREA_H
0023 #define SCRIBBLEAREA_H
0024 
0025 #include <QQuickPaintedItem>
0026 #include <QImage>
0027 
0028 class ScribbleArea : public QQuickPaintedItem
0029 {
0030     Q_OBJECT
0031     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
0032     Q_PROPERTY(int penWidth READ penWidth WRITE setPenWidth NOTIFY penWidthChanged)
0033 
0034 public:
0035     explicit ScribbleArea(QQuickItem* parent = 0);
0036     ~ScribbleArea() override;
0037 
0038     Q_INVOKABLE void clear();
0039 
0040     QColor color() const;
0041     void setColor(const QColor& newColor);
0042 
0043     int penWidth() const;
0044     void setPenWidth(const int& newWidth);
0045 
0046 Q_SIGNALS:
0047     void colorChanged();
0048     void penWidthChanged();
0049     void paintingStopped();
0050     void paintingStarted();
0051 
0052 protected:
0053     bool event(QEvent* event) override;
0054     void mousePressEvent(QMouseEvent *event) override;
0055     void mouseMoveEvent(QMouseEvent *event) override;
0056     void mouseReleaseEvent(QMouseEvent *event) override;
0057     void paint(QPainter* painter) override;
0058 
0059 private:
0060     void drawLineTo(const QPointF &endPoint);
0061     void resizeImage(QImage *image, const QSize &newSize);
0062 
0063     bool scribbling;
0064     int myPenWidth;
0065     QColor myPenColor;
0066     QImage image;
0067     QPointF lastPoint;
0068 };
0069 
0070 #endif // SCRIBBLEAREA_H