Krita Source Code Documentation
Loading...
Searching...
No Matches
KisVisualDiamondSelectorShape.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 Mathias Wein <lynx.mw+kde@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
8
9#include <QPainter>
10#include <QRect>
11#include <QtMath>
12
13#include "kis_debug.h"
14#include "kis_global.h"
15
17 Dimensions dimension,
18 int channel1, int channel2,
19 int margin)
20 : KisVisualColorSelectorShape(parent, dimension, channel1, channel2),
21 m_margin(margin)
22{
23}
24
28
30{
31 // Diamond doesn't have a 1-dimensional mode
32}
33
35{
36 return geom;
37}
38
40{
41 return geom;
42}
43
45{
46 return geom;
47}
48
50{
51 // margin serves to render the cursor, and triangle is rendered 1px larger than its active area
52 qreal offset = m_margin + 1.0;
53
54 qreal y = ((1.0 - coordinate.y()) * (height() - 1 - 2 * offset)) + offset;
55
56 qreal triWidth = width() - 1 - 2 * offset;
57 qreal horizontalLineLength;
58 if (coordinate.y() < 0.5) {
59 horizontalLineLength = 2.0 * coordinate.y() * triWidth;
60 } else {
61 horizontalLineLength = 2.0 * (1.0 - coordinate.y()) * triWidth;
62 }
63 qreal horizontalLineStart = offset + 0.5 * (triWidth - horizontalLineLength);
64
65 qreal x = coordinate.x() * horizontalLineLength + horizontalLineStart;
66
67 return QPointF(x, y);
68}
69
71{
72 // margin serves to render the cursor, and triangle is rendered 1px larger than its active area
73 qreal offset = m_margin + 1.0;
74
75 qreal x = 0.5;
76 qreal y = qBound(0.0, 1.0 - (coordinate.y() - offset)/(height() - 1 - 2 * offset), 1.0);
77
78 qreal triWidth = width() - 1 - 2 * offset;
79 qreal horizontalLineLength;
80
81 if (y < 0.5) {
82 horizontalLineLength = 2.0 * y * triWidth;
83 } else {
84 horizontalLineLength = 2.0 * (1.0 - y) * triWidth;
85 }
86
87 if (horizontalLineLength > 1e-4) {
88 qreal horizontalLineStart = offset + 0.5 * (triWidth - horizontalLineLength);
89 x = qBound(0.0, (coordinate.x() - horizontalLineStart) / horizontalLineLength, 1.0);
90 }
91
92 return QPointF(x, y);
93}
94
96{
97 const int cursorWidth = qMax(2 * m_margin, 2);
98 QPolygon maskPoly;
99 maskPoly << QPoint(qFloor(0.5 * (width() - cursorWidth)), 0)
100 << QPoint(qCeil(0.5 * (width() + cursorWidth)), 0)
101 << QPoint(width(), qFloor(0.5 * height() - cursorWidth))
102 << QPoint(width(), qCeil(0.5 * height() + cursorWidth))
103 << QPoint(qCeil(0.5 * (width() + cursorWidth)), height())
104 << QPoint(qFloor(0.5 * (width() - cursorWidth)), height())
105 << QPoint(0, qCeil(0.5 * height() + cursorWidth))
106 << QPoint(0, qFloor(0.5 * height() - cursorWidth));
107
108 return QRegion(maskPoly);
109}
110
112{
113 // Hi-DPI aware rendering requires that we determine the device pixel dimension;
114 // actual widget size in device pixels is not accessible unfortunately, it might be 1px smaller...
115 const int deviceWidth = qCeil(width() * devicePixelRatioF());
116 const int deviceHeight = qCeil(height() * devicePixelRatioF());
117
118 QImage alphaMask(deviceWidth, deviceHeight, QImage::Format_Alpha8);
119 alphaMask.fill(0);
120 alphaMask.setDevicePixelRatio(devicePixelRatioF());
121 QPainter painter(&alphaMask);
122 painter.setRenderHint(QPainter::Antialiasing);
123 painter.setBrush(Qt::white);
124 painter.setPen(Qt::NoPen);
125 QPointF diamond[4] = {
126 QPointF(0.5 * width(), m_margin),
127 QPointF(m_margin, 0.5 * height()),
128 QPointF(0.5 * width(), height() - m_margin),
129 QPointF(width() - m_margin, 0.5 * height())
130 };
131 painter.drawConvexPolygon(diamond, 4);
132
133 return alphaMask;
134}
135
137{
140 QBrush fill(Qt::SolidPattern);
141
142 int cursorwidth = 5;
143
144 painter.setPen(Qt::white);
145 fill.setColor(Qt::white);
146 painter.setBrush(fill);
147 painter.drawEllipse(cursorPoint, cursorwidth, cursorwidth);
148 fill.setColor(col);
149 painter.setPen(Qt::black);
150 painter.setBrush(fill);
151 painter.drawEllipse(cursorPoint, cursorwidth-1.0, cursorwidth-1.0);
152}
The KisVisualColorSelectorShape class A 2d widget can represent at maximum 2 coordinates....
QPointF getCursorPosition() const
getCursorPosition
QColor getColorFromConverter(KoColor c)
getColorFromConverter
Dimensions
The Dimensions enum Whether or not the shape is single or two dimensional.
The KisVisualColorSelector class.
void drawCursor(QPainter &painter) override
QRect getSpaceForSquare(QRect geom) override
getSpaceForSquare
QImage renderAlphaMask() const override
render the alpha mask for the widget background the returned image is expected to be QImage::Format_A...
QPointF convertShapeCoordinateToWidgetCoordinate(QPointF coordinate) const override
convertShapeCoordinateToWidgetCoordinate
KisVisualDiamondSelectorShape(KisVisualColorSelector *parent, Dimensions dimension, int channel1, int channel2, int margin=5)
void setBorderWidth(int) override
setBorderWidth set the border of the single dimensional selector.
QPointF convertWidgetCoordinateToShapeCoordinate(QPointF coordinate) const override
convertWidgetCoordinateToShapeCoordinate Convert a coordinate in the widget's height/width to a shape...