File indexing completed on 2024-12-22 04:13:14

0001 /* This file is part of the KDE project
0002  * Copyright 2010 (C) Boudewijn Rempt <boud@valdyas.org>
0003  * Copyright 2011 (C) Dmitry Kazakov <dimula73@gmail.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 #ifndef KIS_SCRATCH_PAD_H
0008 #define KIS_SCRATCH_PAD_H
0009 
0010 #include <QImage>
0011 #include <QWidget>
0012 #include <QRect>
0013 
0014 #include <KoColor.h>
0015 
0016 
0017 #include <brushengine/kis_paintop_preset.h>
0018 #include <kis_types.h>
0019 #include <kritaui_export.h>
0020 
0021 class QColor;
0022 
0023 class KoColorProfile;
0024 class KoPointerEvent;
0025 
0026 class KisCanvasResourceProvider;
0027 class KisUpdateScheduler;
0028 class KisUndoStore;
0029 class KisPostExecutionUndoAdapter;
0030 class KisScratchPadEventFilter;
0031 class KisPaintingInformationBuilder;
0032 class KisToolFreehandHelper;
0033 class KisNodeGraphListener;
0034 
0035 
0036 /**
0037  * A scratchpad is a painting canvas with only one zoomlevel and based on
0038  * a paint layer, not on a KisImage. It can have a blank, tiled background or
0039  * a gradient background.
0040  */
0041 class KRITAUI_EXPORT KisScratchPad : public QWidget
0042 {
0043 
0044     Q_OBJECT
0045 
0046 public:
0047     void setupScratchPad(KisCanvasResourceProvider* resourceProvider,
0048                          const QColor &defaultColor);
0049 
0050     KisScratchPad(QWidget *parent = 0);
0051     ~KisScratchPad() override;
0052 
0053     /// set the specified rect as the area taken for @see cutoutOverlay
0054     void setCutoutOverlayRect(const QRect&rc);
0055 
0056     /**
0057      * keep track of if our scratchpad is in paint, pan, or color sample mode
0058      * Set to true if there is a GUI controlling current mode
0059      * If this is false, the modes are only changed with various mouse click shortcuts
0060      */
0061     void setModeManually(bool value);
0062 
0063     /**
0064      * @brief change the mode explicitly to paint, mix, or pan
0065      * @param what mode to change it to
0066      */
0067     void setModeType(QString modeName);
0068 
0069     /**
0070      * @brief should the scratchpad zoom level stay in sync with canvas
0071      * @param should we link zoom level
0072      */
0073     void linkCanvasToZoomLevel(bool value);
0074 
0075 
0076     /// return the contents of the area under the cutoutOverlay rect
0077     QImage cutoutOverlay() const;
0078 
0079     // A callback for our own node graph listener
0080     void imageUpdated(const QRect &rect);
0081 
0082     // A callback for scratch pad default bounds
0083     QRect imageBounds() const;
0084 
0085 
0086     // Called by the event filter
0087     void pointerPress(KoPointerEvent *event);
0088     void pointerRelease(KoPointerEvent *event);
0089     void pointerMove(KoPointerEvent *event);
0090     void resetState();
0091 
0092 public Q_SLOTS:
0093     void fillDefault();
0094     void fillGradient();
0095     void fillBackground();
0096     void fillTransparent();
0097 
0098     void setFillColor(QColor newColor);
0099 
0100     /// Fill the area with what is on your current canvas
0101     void fillLayer();
0102 
0103 
0104     /**
0105      * Set the icon of the current preset
0106      */
0107     void setPresetImage(const QImage& image);
0108 
0109     /**
0110      * Paint the icon of the current preset inside the
0111      * cutout overlay
0112      *
0113      * \see setPresetImage
0114      */
0115     void paintPresetImage();
0116 
0117     /**
0118      * Paint the icon of a custom image that is being loaded
0119      *
0120      */
0121     void paintCustomImage(const QImage & loadedImage);
0122 
0123 
0124     void loadScratchpadImage(QImage image);
0125 
0126     QImage copyScratchpadImageData();
0127 
0128 private Q_SLOTS:
0129     void setOnScreenResolution(qreal scaleX, qreal scaleY);
0130     void setDisplayProfile(const KoColorProfile* colorProfile);
0131     void slotUpdateCanvas(const QRect &rect);
0132 
0133 Q_SIGNALS:
0134     void colorSelected(const KoColor& color);
0135     void sigUpdateCanvas(const QRect &rect);
0136 
0137 protected:
0138     void paintEvent ( QPaintEvent * event ) override;
0139 
0140 
0141 private:
0142     void beginStroke(KoPointerEvent *event);
0143     void doStroke(KoPointerEvent *event);
0144     void endStroke(KoPointerEvent *event);
0145 
0146     void beginPan(KoPointerEvent *event);
0147     void doPan(KoPointerEvent *event);
0148     void endPan(KoPointerEvent *event);
0149 
0150     void sample(KoPointerEvent *event);
0151 
0152     void updateTransformations();
0153 
0154     QTransform documentToWidget() const;
0155     QTransform widgetToDocument() const;
0156 
0157     friend class KisScratchPadPaintingInformationBuilder;
0158 
0159 private:
0160     enum Mode {
0161         PAINTING,
0162         HOVERING,
0163         PANNING,
0164         SAMPLING
0165     };
0166 
0167     Mode modeFromButton(Qt::MouseButton button) const;
0168 
0169 private:
0170 
0171     KoColor m_defaultColor;
0172     Mode m_toolMode;
0173     bool isModeManuallySet;
0174     bool isMouseDown;
0175     bool linkCanvasZoomLevel;
0176     KisPaintLayerSP m_paintLayer;
0177     const KoColorProfile* m_displayProfile;
0178     QCursor m_cursor;
0179     QCursor m_colorSamplerCursor;
0180     QRect m_cutoutOverlay;
0181     QBrush m_checkBrush;
0182     KisCanvasResourceProvider* m_resourceProvider;
0183 
0184     KisUpdateScheduler *m_updateScheduler;
0185     KisUndoStore *m_undoStore;
0186     KisPostExecutionUndoAdapter *m_undoAdapter;
0187     KisNodeGraphListener *m_nodeListener;
0188     KisScratchPadEventFilter *m_eventFilter;
0189 
0190     QScopedPointer<KisToolFreehandHelper> m_helper;
0191     KisPaintingInformationBuilder *m_infoBuilder;
0192 
0193     QTransform m_scaleTransform;
0194     QTransform m_translateTransform;
0195 
0196     QPointF m_panDocPoint;
0197     int m_scaleBorderWidth;
0198 
0199     QImage m_presetImage;
0200 };
0201 
0202 #endif // KIS_SCRATCH_PAD_H