Changeset View
Changeset View
Standalone View
Standalone View
plugins/dockers/smallcolorselector/KisClickableGLImageWidget.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2019 Dmitry Kazakov <dimula73@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "KisClickableGLImageWidget.h" | ||||
20 | | ||||
21 | #include <QMouseEvent> | ||||
22 | #include <QPainter> | ||||
23 | #include "kis_algebra_2d.h" | ||||
24 | #include <kis_debug.h> | ||||
25 | | ||||
26 | KisClickableGLImageWidget::KisClickableGLImageWidget(QWidget *parent) | ||||
27 | : KisGLImageWidget(parent) | ||||
28 | { | ||||
29 | } | ||||
30 | | ||||
31 | KisClickableGLImageWidget::KisClickableGLImageWidget(QSurfaceFormat::ColorSpace colorSpace, QWidget *parent) | ||||
32 | : KisGLImageWidget(colorSpace, parent) | ||||
33 | { | ||||
34 | } | ||||
35 | | ||||
36 | KisClickableGLImageWidget::~KisClickableGLImageWidget() | ||||
37 | { | ||||
38 | } | ||||
39 | | ||||
40 | void KisClickableGLImageWidget::setHandlePaintingStrategy(HandlePaintingStrategy *strategy) | ||||
41 | { | ||||
42 | m_handleStrategy.reset(strategy); | ||||
43 | } | ||||
44 | | ||||
45 | void KisClickableGLImageWidget::setUseHandleOpacity(bool value) | ||||
46 | { | ||||
47 | m_useHandleOpacity = value; | ||||
48 | update(); | ||||
49 | } | ||||
50 | | ||||
51 | QPointF KisClickableGLImageWidget::normalizedPos() const | ||||
52 | { | ||||
53 | return m_normalizedClickPoint; | ||||
54 | } | ||||
55 | | ||||
56 | void KisClickableGLImageWidget::setNormalizedPos(const QPointF &pos, bool update) | ||||
57 | { | ||||
58 | m_normalizedClickPoint = KisAlgebra2D::clampPoint(pos, QRectF(0,0,1.0,1.0)); | ||||
59 | if (update) { | ||||
60 | this->update(); | ||||
61 | } | ||||
62 | } | ||||
63 | | ||||
64 | void KisClickableGLImageWidget::paintEvent(QPaintEvent *event) | ||||
65 | { | ||||
66 | KisGLImageWidget::paintEvent(event); | ||||
67 | | ||||
68 | if (m_handleStrategy) { | ||||
69 | QPainter p(this); | ||||
70 | m_handleStrategy->drawHandle(&p, m_normalizedClickPoint, rect(), m_useHandleOpacity); | ||||
71 | } | ||||
72 | } | ||||
73 | | ||||
74 | void KisClickableGLImageWidget::mousePressEvent(QMouseEvent *event) | ||||
75 | { | ||||
76 | KisGLImageWidget::mousePressEvent(event); | ||||
77 | | ||||
78 | if (!event->isAccepted()) { | ||||
79 | event->accept(); | ||||
80 | m_normalizedClickPoint = normalizePoint(event->localPos()); | ||||
81 | emit selected(m_normalizedClickPoint); | ||||
82 | | ||||
83 | if (m_handleStrategy) { | ||||
84 | update(); | ||||
85 | } | ||||
86 | } | ||||
87 | } | ||||
88 | | ||||
89 | void KisClickableGLImageWidget::mouseReleaseEvent(QMouseEvent *event) | ||||
90 | { | ||||
91 | KisGLImageWidget::mouseReleaseEvent(event); | ||||
92 | | ||||
93 | if (!event->isAccepted()) { | ||||
94 | event->accept(); | ||||
95 | m_normalizedClickPoint = normalizePoint(event->localPos()); | ||||
96 | emit selected(m_normalizedClickPoint); | ||||
97 | | ||||
98 | if (m_handleStrategy) { | ||||
99 | update(); | ||||
100 | } | ||||
101 | } | ||||
102 | } | ||||
103 | | ||||
104 | void KisClickableGLImageWidget::mouseMoveEvent(QMouseEvent *event) | ||||
105 | { | ||||
106 | KisGLImageWidget::mouseMoveEvent(event); | ||||
107 | | ||||
108 | if (!event->isAccepted()) { | ||||
109 | event->accept(); | ||||
110 | m_normalizedClickPoint = normalizePoint(event->localPos()); | ||||
111 | emit selected(m_normalizedClickPoint); | ||||
112 | | ||||
113 | if (m_handleStrategy) { | ||||
114 | update(); | ||||
115 | } | ||||
116 | } | ||||
117 | } | ||||
118 | | ||||
119 | QPointF KisClickableGLImageWidget::normalizePoint(const QPointF &pos) const | ||||
120 | { | ||||
121 | const QPointF croppedPoint = KisAlgebra2D::clampPoint(pos, rect()); | ||||
122 | return QPointF(croppedPoint.x() / width(), croppedPoint.y() / height()); | ||||
123 | } | ||||
124 | | ||||
125 | | ||||
126 | namespace { | ||||
127 | QPen outerHandlePen(bool useOpacity) { | ||||
128 | // opacity works inexpectedly in HDR mode, so let the user switch it off | ||||
129 | return QPen(QColor(0, 0, 0, useOpacity ? 180 : 255), 0); | ||||
130 | } | ||||
131 | QPen innerHandlePen(bool useOpacity) { | ||||
132 | // opacity works inexpectedly in HDR mode, so let the user switch it off | ||||
133 | return QPen(QColor(255, 255, 255, useOpacity ? 180 : 255), 0); | ||||
134 | } | ||||
135 | } | ||||
136 | | ||||
137 | void KisClickableGLImageWidget::VerticalLineHandleStrategy::drawHandle(QPainter *p, const QPointF &normalizedPoint, const QRect &rect, bool useOpacity) | ||||
138 | { | ||||
139 | const QPointF pos = KisAlgebra2D::relativeToAbsolute(normalizedPoint, rect); | ||||
140 | const int x = std::floor(pos.x()); | ||||
141 | | ||||
142 | p->setPen(outerHandlePen(useOpacity)); | ||||
143 | p->drawLine(x, rect.top(), x, rect.bottom()); | ||||
144 | p->setPen(innerHandlePen(useOpacity)); | ||||
145 | p->drawLine(x + 1, rect.top(), x + 1, rect.bottom()); | ||||
146 | } | ||||
147 | | ||||
148 | void KisClickableGLImageWidget::CircularHandleStrategy::drawHandle(QPainter *p, const QPointF &normalizedPoint, const QRect &rect, bool useOpacity) | ||||
149 | { | ||||
150 | const QPointF pos = KisAlgebra2D::relativeToAbsolute(normalizedPoint, rect); | ||||
151 | | ||||
152 | p->setRenderHint(QPainter::Antialiasing); | ||||
153 | p->setPen(outerHandlePen(useOpacity)); | ||||
154 | p->drawEllipse(pos, 5, 5); | ||||
155 | | ||||
156 | p->setPen(innerHandlePen(useOpacity)); | ||||
157 | p->drawEllipse(pos, 4, 4); | ||||
158 | } |