Krita Source Code Documentation
Loading...
Searching...
No Matches
KisGradientWidgetsUtils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021 Deif Lou <ginoba@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include <QPainterPath>
8
10
12
14{
15
16void paintGradientBox(QPainter &painter, const KoAbstractGradientSP gradient, const QRectF &rect)
17{
18 static KoCheckerBoardPainter checkerBoardPainter(4);
19
20 // Background
21 checkerBoardPainter.paint(painter, rect, rect.topLeft());
22 // Gradient
23 QImage image = gradient->generatePreview(rect.width(), rect.height());
24 if (!image.isNull()) {
25 painter.drawImage(rect.topLeft(), image);
26 }
27 // Border
28 painter.setPen(QColor(0, 0, 0, 192));
29 painter.drawRect(rect);
30}
31
32void paintStopHandle(QPainter &painter,
33 const QPointF &position,
34 const QSizeF &size,
35 bool isSelected, bool isHovered, bool hasFocus,
36 const QColor &highlightColor,
37 const StopHandleColor &color1,
38 const StopHandleColor &color2)
39{
40 painter.save();
41
42 QColor borderColor;
43 int borderWidth;
44 if (isSelected) {
45 borderColor = highlightColor;
46 borderColor.setAlpha(255);
47 borderWidth = hasFocus ? 2 : 1;
48 } else {
49 if (isHovered) {
50 borderColor = highlightColor;
51 borderColor.setAlpha(192);
52 } else {
53 borderColor = QColor(0, 0, 0, 128);
54 }
55 borderWidth = 1;
56 }
57 const QPointF alignedPosition(qRound(position.x() + 0.5) - 0.5, position.y());
58 const qreal halfWidth = size.width() * 0.5;
59 QPainterPath path(alignedPosition);
60 path.arcTo(
61 alignedPosition.x() - halfWidth,
62 alignedPosition.y() + size.height() - size.width(),
63 size.width(),
64 size.width(),
65 150,
66 240
67 );
68 path.closeSubpath();
69 // paint the "drop" handle
70 if (color1.type != None && color2.type != None && color1.color != color2.color) {
71 painter.setClipRect(QRectF(QPointF(alignedPosition.x() - halfWidth, alignedPosition.y()), QSizeF(halfWidth, size.height())));
72 painter.setPen(Qt::NoPen);
73 painter.setBrush(color1.color);
74 painter.drawPath(path);
75 painter.setClipRect(QRectF(alignedPosition, QSizeF(halfWidth, size.height())));
76 painter.setBrush(color2.color);
77 painter.drawPath(path);
78 painter.setClipping(false);
79 painter.setPen(QPen(borderColor, borderWidth));
80 painter.setBrush(Qt::NoBrush);
81 painter.drawPath(path);
82 } else {
83 painter.setPen(QPen(borderColor, borderWidth));
84 if (color1.type != None) {
85 // Only color 1 was specified
86 painter.setBrush(color1.color);
87 } else {
88 // Only color 2 was specified
89 painter.setBrush(color2.color);
90 }
91 painter.drawPath(path);
92 }
93 // paint the type indicator
94 constexpr QSizeF typeIndicatorSize(5.0, 5.0);
95 if (color1.type == Foreground || color1.type == Background) {
96 QPointF typeIndicatorPosition(
97 alignedPosition.x() - halfWidth - 1.0,
98 alignedPosition.y() + size.height() - typeIndicatorSize.height() + 1.0
99 );
100 if (color1.type == Foreground) {
101 painter.setPen(QPen(Qt::white, 1));
102 painter.setBrush(Qt::black);
103 } else if (color1.type == Background) {
104 painter.setPen(QPen(Qt::black, 1));
105 painter.setBrush(Qt::white);
106 }
107 painter.drawEllipse(QRectF(typeIndicatorPosition, typeIndicatorSize));
108 }
109 if (color2.type == Foreground || color2.type == Background) {
110 QPointF typeIndicatorPosition(
111 alignedPosition.x() + halfWidth - typeIndicatorSize.width() + 1.0,
112 alignedPosition.y() + size.height() - typeIndicatorSize.height() + 1.0
113 );
114 if (color2.type == Foreground) {
115 painter.setPen(QPen(Qt::white, 1));
116 painter.setBrush(Qt::black);
117 } else if (color2.type == Background) {
118 painter.setPen(QPen(Qt::black, 1));
119 painter.setBrush(Qt::white);
120 }
121 painter.drawEllipse(QRectF(typeIndicatorPosition, typeIndicatorSize));
122 }
123
124 painter.restore();
125}
126
127void paintMidPointHandle(QPainter &painter,
128 const QPointF &position,
129 qreal size,
130 bool isSelected, bool isHovered, bool hasFocus,
131 const QColor &borderColor,
132 const QColor &fillColor,
133 const QColor &highlightColor)
134{
135 painter.save();
136
137 QColor brushColor;
138 int penWidth;
139 if (isSelected) {
140 brushColor = highlightColor;
141 penWidth = hasFocus ? 2 : 1;
142 } else {
143 if (isHovered) {
144 brushColor = highlightColor;
145 brushColor.setAlpha(192);
146 } else {
147 brushColor = fillColor;
148 }
149 penWidth = 1;
150 }
151 const QPointF alignedPosition(qRound(position.x() + 0.5) - 0.5, qRound(position.y() + 0.5) - 0.5);
152 const qreal alignedSize = qRound(size);
153 const qreal handleSizeOverTwo = alignedSize * 0.5;
154 const QPointF points[4] =
155 {
156 QPointF(0.0, 0.0),
157 QPointF(-handleSizeOverTwo, handleSizeOverTwo),
158 QPointF(0.0, size),
159 QPointF(handleSizeOverTwo, handleSizeOverTwo)
160 };
161 painter.translate(alignedPosition);
162 painter.setPen(QPen(borderColor, penWidth));
163 painter.setBrush(brushColor);
164 painter.drawPolygon(points, 4);
165
166 painter.restore();
167}
168
169
179
181{
182 if (type == Foreground) {
184 } else if (type == Background) {
186 }
187 return COLOR_ENDPOINT;
188}
189
190}
KoGradientSegmentEndpointType
@ COLOR_ENDPOINT
@ BACKGROUND_TRANSPARENT_ENDPOINT
@ FOREGROUND_ENDPOINT
@ BACKGROUND_ENDPOINT
@ FOREGROUND_TRANSPARENT_ENDPOINT
void paint(QPainter &painter, const QRectF &rect, const QPointF &patternOrigin) const
void paintGradientBox(QPainter &painter, const KoAbstractGradientSP gradient, const QRectF &rect)
void paintStopHandle(QPainter &painter, const QPointF &position, const QSizeF &size, bool isSelected, bool isHovered, bool hasFocus, const QColor &highlightColor, const StopHandleColor &color1, const StopHandleColor &color2)
KisGradientWidgetsUtils::ColorType segmentEndPointTypeToColorType(KoGradientSegmentEndpointType type)
KoGradientSegmentEndpointType colorTypeToSegmentEndPointType(KisGradientWidgetsUtils::ColorType type, bool transparent)
void paintMidPointHandle(QPainter &painter, const QPointF &position, qreal size, bool isSelected, bool isHovered, bool hasFocus, const QColor &borderColor, const QColor &fillColor, const QColor &highlightColor)