File indexing completed on 2024-05-12 15:57:02

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KISHANDLEPAINTERHELPER_H
0008 #define KISHANDLEPAINTERHELPER_H
0009 
0010 #include "kritaglobal_export.h"
0011 #include "kis_algebra_2d.h"
0012 
0013 #include <QPainter>
0014 #include <KisHandleStyle.h>
0015 class QPainter;
0016 class KoShape;
0017 class KoViewConverter;
0018 
0019 /**
0020  * @brief The KisHandlePainterHelper class is a special helper for
0021  *        painting handles around objects. It ensures the handlesare painted
0022  *        with the same size and line width whatever transformation is setup
0023  *        in the painter. The handles will also be rotated/skewed if the object
0024  *        itself has these transformations.
0025  *
0026  *        On construction it resets QPainter transformation and on destruction
0027  *        recovers it back.
0028  *
0029  * Please consider using KoShape::createHandlePainterHelper instead of direct
0030  * construction of the helper. This factory method will also apply the
0031  * transformations needed for a shape.
0032  */
0033 
0034 class KRITAGLOBAL_EXPORT KisHandlePainterHelper
0035 {
0036 public:
0037 
0038     /**
0039      * Creates the helper, initializes all the internal transformations and
0040      * *resets* the transformation of the painter.
0041      */
0042     KisHandlePainterHelper(QPainter *_painter, qreal handleRadius = 0.0);
0043 
0044     /**
0045      * Creates the helper, initializes all the internal transformations and
0046      * *resets* the transformation of the painter. This override also adjusts the
0047      * transformation of the painter into the coordinate system of the shape
0048      */
0049     KisHandlePainterHelper(QPainter *_painter, const QTransform &originalPainterTransform, qreal handleRadius);
0050 
0051     /**
0052      * Move c-tor. Used to create and return the helper from functions by-value.
0053      */
0054     KisHandlePainterHelper(KisHandlePainterHelper &&rhs);
0055     KisHandlePainterHelper(KisHandlePainterHelper &rhs) = delete;
0056 
0057     /**
0058      * Restores the transformation of the painter
0059      */
0060     ~KisHandlePainterHelper();
0061 
0062     /**
0063      * Sets style used for painting the handles. Please use static methods of
0064      * KisHandleStyle to select predefined styles.
0065      */
0066     void setHandleStyle(const KisHandleStyle &style);
0067 
0068     /**
0069      * Draws a handle rect with a custom \p radius at position \p center
0070      */
0071     void drawHandleRect(const QPointF &center, qreal radius);
0072     void drawHandleRect(const QPointF &center, qreal radius, QPoint offset);
0073     void fillHandleRect(const QPointF &center, qreal radius, QColor fillColor, QPoint offset);
0074 
0075     /**
0076      * Draws a handle circle with a custom \p radius at position \p center
0077      */
0078     void drawHandleCircle(const QPointF &center, qreal radius);
0079 
0080     /**
0081      * Optimized version of the drawing method for drawing handles of
0082      * predefined size
0083      */
0084     void drawHandleRect(const QPointF &center);
0085 
0086     /**
0087      * Optimized version of the drawing method for drawing handles of
0088      * predefined size
0089      */
0090     void drawHandleCircle(const QPointF &center);
0091 
0092     /**
0093      * Optimized version of the drawing method for drawing handles of
0094      * predefined size
0095      */
0096     void drawHandleSmallCircle(const QPointF &center);
0097 
0098     /**
0099      * Draw a rotated handle representing the gradient handle
0100      */
0101     void drawGradientHandle(const QPointF &center, qreal radius);
0102 
0103     /**
0104      * Draw a rotated handle representing the gradient handle
0105      */
0106     void drawGradientHandle(const QPointF &center);
0107 
0108     /**
0109      * Draw a special handle representing the center of the gradient
0110      */
0111     void drawGradientCrossHandle(const QPointF &center, qreal radius);
0112 
0113     /**
0114      * Draw an arrow representing gradient position
0115      */
0116     void drawGradientArrow(const QPointF &start, const QPointF &end, qreal radius);
0117 
0118     /**
0119      * Draw a line showing the bounding box of the selection
0120      */
0121     void drawRubberLine(const QPolygonF &poly);
0122 
0123     /**
0124      * Draw a line connecting two points
0125      */
0126     void drawConnectionLine(const QLineF &line);
0127 
0128     /**
0129      * Draw a line connecting two points
0130      */
0131     void drawConnectionLine(const QPointF &p1, const QPointF &p2);
0132 
0133     /**
0134      * Draw an arbitrary path
0135      */
0136     void drawPath(const QPainterPath &path);
0137 
0138     /**
0139      * Draw an a given pixmap on the UI
0140      */
0141     void drawPixmap(const QPixmap &pixmap, QPointF position, int size, QRectF sourceRect);
0142 
0143 private:
0144 
0145     /**
0146      * Draw a single arrow with the tip at position \p pos, directed from \p from,
0147      * of size \p radius.
0148      */
0149     void drawArrow(const QPointF &pos, const QPointF &from, qreal radius);
0150 
0151     void init();
0152 
0153 private:
0154     QPainter *m_painter;
0155     QTransform m_originalPainterTransform;
0156     QTransform m_painterTransform;
0157     qreal m_handleRadius;
0158     KisAlgebra2D::DecomposedMatix m_decomposedMatrix;
0159     QTransform m_handleTransform;
0160     QPolygonF m_handlePolygon;
0161     KisHandleStyle m_handleStyle;
0162 };
0163 
0164 #endif // KISHANDLEPAINTERHELPER_H