File indexing completed on 2025-01-19 03:58:00

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2008-05-19
0007  * Description : a widget to draw sketch.
0008  *
0009  * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2008-2010 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef DIGIKAM_SKETCH_WIDGET_H
0017 #define DIGIKAM_SKETCH_WIDGET_H
0018 
0019 // Qt includes
0020 
0021 #include <QWidget>
0022 #include <QXmlStreamReader>
0023 #include <QXmlStreamWriter>
0024 
0025 namespace Digikam
0026 {
0027 
0028 class SketchWidget : public QWidget
0029 {
0030     Q_OBJECT
0031 
0032 public:
0033 
0034     explicit SketchWidget(QWidget* const parent = nullptr);
0035     ~SketchWidget() override;
0036 
0037     QColor  penColor() const;
0038     int     penWidth() const;
0039     bool    isClear()  const;
0040 
0041     QImage  sketchImage() const;
0042     void    setSketchImage(const QImage& image);
0043 
0044     /**
0045      * This method return the drawing line history
0046      * as XML, to be stored in database as SAlbum data.
0047      */
0048     void sketchImageToXML(QXmlStreamWriter& writer);
0049     QString sketchImageToXML();
0050 
0051     /**
0052      * This method set sketch image using XML data based
0053      * on drawing line history.
0054      * Return true if data are imported successfully.
0055      */
0056     bool setSketchImageFromXML(QXmlStreamReader& reader);
0057     bool setSketchImageFromXML(const QString& xml);
0058 
0059 Q_SIGNALS:
0060 
0061     void signalSketchChanged(const QImage&);
0062     void signalPenSizeChanged(int);
0063     void signalPenColorChanged(const QColor&);
0064     void signalUndoRedoStateChanged(bool hasUndo, bool hasRedo);
0065 
0066 public Q_SLOTS:
0067 
0068     void setPenColor(const QColor& newColor);
0069     void setPenWidth(int newWidth);
0070     void slotClear();
0071     void slotUndo();
0072     void slotRedo();
0073 
0074 protected:
0075 
0076     void mousePressEvent(QMouseEvent*)      override;
0077     void mouseMoveEvent(QMouseEvent*)       override;
0078     void wheelEvent(QWheelEvent*)           override;
0079     void mouseReleaseEvent(QMouseEvent*)    override;
0080     void keyPressEvent(QKeyEvent*)          override;
0081     void keyReleaseEvent(QKeyEvent*)        override;
0082     void paintEvent(QPaintEvent*)           override;
0083 
0084 private:
0085 
0086     void updateDrawCursor();
0087     void replayEvents(int index);
0088     void drawLineTo(const QPoint& endPoint);
0089     void drawLineTo(int width, const QColor& color, const QPoint& start, const QPoint& end);
0090     void drawPath(int width, const QColor& color, const QPainterPath& path);
0091     void addPath(QXmlStreamReader& reader);
0092 /*
0093     QDomElement addXmlTextElement(QDomDocument& document, QDomElement& target,
0094                                   const QString& tag, const QString& text);
0095 */
0096 private:
0097 
0098     class Private;
0099     Private* const d;
0100 };
0101 
0102 } // namespace Digikam
0103 
0104 #endif // DIGIKAM_SKETCH_WIDGET_H