Krita Source Code Documentation
Loading...
Searching...
No Matches
KisVisualTriangleSelectorShape.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Wolthera van Hovell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
7
8#include <QColor>
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 //qDebug() << "creating KisVisualTriangleSelectorShape" << this;
24}
25
27{
28 //qDebug() << "deleting KisVisualTriangleSelectorShape" << this;
29}
30
32{
33 // triangle doesn't have a 1-dimensional mode
34}
35
37{
38 return geom;
39}
40
42{
43 return geom;
44}
45
47{
48 return geom;
49}
50
52{
53 // margin serves to render the cursor, and triangle is rendered 1px larger than its active area
54 qreal offset = m_margin + 1.0;
55
56 qreal y = (coordinate.y() * (height() - 1 - 2 * offset)) + offset;
57
58 qreal triWidth = width() - 1 - 2 * offset;
59 qreal horizontalLineLength = coordinate.y() * triWidth;
60 qreal horizontalLineStart = offset + 0.5 * (triWidth - horizontalLineLength);
61
62 qreal x = coordinate.x() * horizontalLineLength + horizontalLineStart;
63
64 return QPointF(x, y);
65}
66
68{
69 // margin serves to render the cursor, and triangle is rendered 1px larger than its active area
70 qreal offset = m_margin + 1.0;
71
72 qreal x = 0.5;
73 qreal y = qBound(0.0, (coordinate.y() - offset)/(height() - 1 - 2 * offset), 1.0);
74
75 if (y > 0) {
76 qreal triWidth = width() - 1 - 2 * offset;
77 qreal horizontalLineLength = y * triWidth;
78 qreal horizontalLineStart = offset + 0.5 * (triWidth - horizontalLineLength);
79
80 x = qBound(0.0, (coordinate.x() - horizontalLineStart) / horizontalLineLength, 1.0);
81 }
82
83 return QPointF(x, y);
84}
85
87{
88 const int cursorWidth = qMax(2 * m_margin, 2);
89 QPolygon maskPoly;
90 maskPoly << QPoint(qFloor(0.5 * (width() - cursorWidth)), 0)
91 << QPoint(qCeil(0.5 * (width() + cursorWidth)), 0)
92 << QPoint(width(), height() - cursorWidth)
93 << QPoint(width(), height())
94 << QPoint(0, height())
95 << QPoint(0, height() - cursorWidth);
96
97 return QRegion(maskPoly);
98}
99
101{
102 // Hi-DPI aware rendering requires that we determine the device pixel dimension;
103 // actual widget size in device pixels is not accessible unfortunately, it might be 1px smaller...
104 const int deviceWidth = qCeil(width() * devicePixelRatioF());
105 const int deviceHeight = qCeil(height() * devicePixelRatioF());
106
107 QImage alphaMask(deviceWidth, deviceHeight, QImage::Format_Alpha8);
108 alphaMask.fill(0);
109 alphaMask.setDevicePixelRatio(devicePixelRatioF());
110 QPainter painter(&alphaMask);
111 painter.setRenderHint(QPainter::Antialiasing);
112 painter.setBrush(Qt::white);
113 painter.setPen(Qt::NoPen);
114 QPointF triangle[3] = {
115 QPointF(0.5 * width(), m_margin),
116 QPointF(m_margin, height() - m_margin),
117 QPointF(width() - m_margin, height() - m_margin),
118 };
119 painter.drawConvexPolygon(triangle, 3);
120
121 return alphaMask;
122}
123
125{
126 //qDebug() << this << "KisVisualTriangleSelectorShape::drawCursor: image needs update" << imagesNeedUpdate();
129 QBrush fill(Qt::SolidPattern);
130
131 int cursorwidth = 5;
132
133 painter.setPen(Qt::white);
134 fill.setColor(Qt::white);
135 painter.setBrush(fill);
136 painter.drawEllipse(cursorPoint, cursorwidth, cursorwidth);
137 fill.setColor(col);
138 painter.setPen(Qt::black);
139 painter.setBrush(fill);
140 painter.drawEllipse(cursorPoint, cursorwidth-1.0, cursorwidth-1.0);
141}
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.
QImage renderAlphaMask() const override
render the alpha mask for the widget background the returned image is expected to be QImage::Format_A...
void drawCursor(QPainter &painter) override
KisVisualTriangleSelectorShape(KisVisualColorSelector *parent, Dimensions dimension, int channel1, int channel2, int margin=5)
QPointF convertWidgetCoordinateToShapeCoordinate(QPointF coordinate) const override
convertWidgetCoordinateToShapeCoordinate Convert a coordinate in the widget's height/width to a shape...
void setBorderWidth(int) override
setBorderWidth set the border of the single dimensional selector.
QRect getSpaceForSquare(QRect geom) override
getSpaceForSquare
QPointF convertShapeCoordinateToWidgetCoordinate(QPointF coordinate) const override
convertShapeCoordinateToWidgetCoordinate