File indexing completed on 2024-05-19 04:25:09
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 handles are 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, int decorationThickness = 1); 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, int decorationThickness = 1); 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 ¢er, qreal radius); 0072 void drawHandleRect(const QPointF ¢er, qreal radius, QPoint offset); 0073 void fillHandleRect(const QPointF ¢er, 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 ¢er, qreal radius); 0079 0080 /** 0081 * Optimized version of the drawing method for drawing handles of 0082 * predefined size 0083 */ 0084 void drawHandleRect(const QPointF ¢er); 0085 0086 /** 0087 * Optimized version of the drawing method for drawing handles of 0088 * predefined size 0089 */ 0090 void drawHandleCircle(const QPointF ¢er); 0091 0092 /** 0093 * Optimized version of the drawing method for drawing handles of 0094 * predefined size 0095 */ 0096 void drawHandleSmallCircle(const QPointF ¢er); 0097 0098 /** 0099 * Draws a line in the style of a handle with \p width indicating the thickness. 0100 */ 0101 void drawHandleLine(const QLineF &line, qreal width = 1.0, QVector<qreal> dashPattern = {}, qreal dashOffset = 0.0); 0102 0103 /** 0104 * Draw a rotated handle representing the gradient handle 0105 */ 0106 void drawGradientHandle(const QPointF ¢er, qreal radius); 0107 0108 /** 0109 * Draw a rotated handle representing the gradient handle 0110 */ 0111 void drawGradientHandle(const QPointF ¢er); 0112 0113 /** 0114 * Draw a special handle representing the center of the gradient 0115 */ 0116 void drawGradientCrossHandle(const QPointF ¢er, qreal radius); 0117 0118 /** 0119 * Draw an arrow representing gradient position 0120 */ 0121 void drawGradientArrow(const QPointF &start, const QPointF &end, qreal radius); 0122 0123 /** 0124 * Draw a line showing the bounding box of the selection 0125 */ 0126 void drawRubberLine(const QPolygonF &poly); 0127 0128 /** 0129 * Draw a line connecting two points 0130 */ 0131 void drawConnectionLine(const QLineF &line); 0132 0133 /** 0134 * Draw a line connecting two points 0135 */ 0136 void drawConnectionLine(const QPointF &p1, const QPointF &p2); 0137 0138 /** 0139 * Draw an arbitrary path 0140 */ 0141 void drawPath(const QPainterPath &path); 0142 0143 /** 0144 * Draw an a given pixmap on the UI 0145 */ 0146 void drawPixmap(const QPixmap &pixmap, QPointF position, int size, QRectF sourceRect); 0147 0148 private: 0149 0150 /** 0151 * Draw a single arrow with the tip at position \p pos, directed from \p from, 0152 * of size \p radius. 0153 */ 0154 void drawArrow(const QPointF &pos, const QPointF &from, qreal radius); 0155 0156 void init(); 0157 0158 private: 0159 QPainter *m_painter; 0160 QTransform m_originalPainterTransform; 0161 QTransform m_painterTransform; 0162 qreal m_handleRadius; 0163 int m_decorationThickness; 0164 KisAlgebra2D::DecomposedMatrix m_decomposedMatrix; 0165 QTransform m_handleTransform; 0166 QPolygonF m_handlePolygon; 0167 KisHandleStyle m_handleStyle; 0168 }; 0169 0170 #endif // KISHANDLEPAINTERHELPER_H