Krita Source Code Documentation
Loading...
Searching...
No Matches
KisScreenColorSampler.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 */
6
7#include <QScreen>
8#include <QGuiApplication>
9#include <QApplication>
10#include <QScreen>
11#include <QColor>
12#include <QVBoxLayout>
13#include <QLabel>
14#include <QPushButton>
15#include <QWindow>
16#include <QTimer>
17
18#include <kis_canvas2.h>
19
20#include "kis_shared_ptr.h"
21#include "kis_icon.h"
22#include "kis_image.h"
23#include "kis_wrapped_rect.h"
24#include "KisDocument.h"
25#include "KisPart.h"
30#include <KisPortingUtils.h>
31#include <KoColorProfileQuery.h>
32
34
36{
37 QPushButton *screenColorSamplerButton = 0;
38 QLabel *lblScreenColorInfo = 0;
39
42
44
46
47 QWidget *inputGrabberWidget {nullptr};
48
49#ifdef Q_OS_WIN32
50 QTimer *updateTimer = 0;
51 QWindow dummyTransparentWindow;
52#endif
53
55 {
56 inputGrabberWidget = qApp->activeWindow();
57 }
58};
59
60KisScreenColorSampler::KisScreenColorSampler(bool showInfoLabel, QWidget *parent) : KisScreenColorSamplerBase(parent), m_d(new Private)
61{
62 QVBoxLayout *layout = new QVBoxLayout(this);
63 m_d->screenColorSamplerButton = new QPushButton();
64
65 m_d->screenColorSamplerButton->setMinimumHeight(25);
66 layout->addWidget(m_d->screenColorSamplerButton);
67
68 if (showInfoLabel) {
69 m_d->lblScreenColorInfo = new QLabel(QLatin1String("\n"));
70 layout->addWidget(m_d->lblScreenColorInfo);
71 }
72
73 layout->setContentsMargins(0, 0, 0, 0);
74
75 connect(m_d->screenColorSamplerButton, SIGNAL(clicked()), SLOT(sampleScreenColor()));
76
78
79#ifdef Q_OS_WIN32
80 m_d->updateTimer = new QTimer(this);
81 m_d->dummyTransparentWindow.resize(1, 1);
82 m_d->dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
83 connect(m_d->updateTimer, SIGNAL(timeout()), SLOT(updateColorSampling()));
84#endif
85}
86
90
92{
93 m_d->screenColorSamplerButton->setIcon(kisIcon("krita_tool_color_sampler"));
94}
95
97{
98 return m_d->currentColor;
99}
100
102{
103 return m_d->performRealColorSamplingOfCanvas;
104}
105
107{
108 m_d->performRealColorSamplingOfCanvas = enable;
109}
110
112{
113#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
115 return;
116 }
117#endif
118
119 m_d->updateInputGrabberWidget();
120 if (m_d->inputGrabberWidget == nullptr) {
122 return;
123 }
124
125 if (!m_d->colorSamplingEventFilter) {
126 m_d->colorSamplingEventFilter = new KisScreenColorSamplingEventFilter(this, this);
127 }
128 m_d->inputGrabberWidget->installEventFilter(m_d->colorSamplingEventFilter);
129 // If user pushes Escape, the last color before sampling will be restored.
130 m_d->beforeScreenColorSampling = currentColor();
131 m_d->inputGrabberWidget->grabMouse(Qt::CrossCursor);
132
133#ifdef Q_OS_WIN32 // excludes WinCE and WinRT
134 // On Windows mouse tracking doesn't work over other processes's windows
135 m_d->updateTimer->start(30);
136
137 // HACK: Because mouse grabbing doesn't work across processes, we have to have a dummy,
138 // invisible window to catch the mouse click, otherwise we will click whatever we clicked
139 // and loose focus.
140 m_d->dummyTransparentWindow.show();
141#endif
142 m_d->inputGrabberWidget->grabKeyboard();
143 /* With setMouseTracking(true) the desired color can be more precisely sampled,
144 * and continuously pushing the mouse button is not necessary.
145 */
146 m_d->inputGrabberWidget->setMouseTracking(true);
147
148 m_d->screenColorSamplerButton->setDisabled(true);
149
150 const QPoint globalPos = QCursor::pos();
152 updateColorLabelText(globalPos);
153}
154
156{
157 m_d->currentColor = c;
158}
159
161{
163 setCurrentColor(m_d->beforeScreenColorSampling);
165}
166
168{
169 // First check whether we're clicking on a Krita window for some real color sampling
170 if (m_d->performRealColorSamplingOfCanvas) {
171 Q_FOREACH(KisView *view, KisPart::instance()->views()) {
172 const KisCanvas2 *canvas = view->canvasBase();
173 const QWidget *canvasWidget = canvas->canvasWidget();
174 QPoint widgetPoint = canvasWidget->mapFromGlobal(p);
175
176 if (canvasWidget->visibleRegion().contains(widgetPoint)) {
177 KisImageWSP image = view->image();
178
179 if (image) {
180 QPointF imagePoint = canvas->coordinatesConverter()->widgetToImage(widgetPoint);
181 // KisReferenceImagesLayer::getPixel use document coordinate
182 QPointF docPoint = canvas->coordinatesConverter()->widgetToDocument(widgetPoint);
183 // sample from reference images first
184 KisSharedPtr<KisReferenceImagesLayer> referenceImageLayer = view->document()->referenceImagesLayer();
185
186 if (referenceImageLayer && canvas->referenceImagesDecoration()->visible()) {
187 KoColor color = referenceImageLayer->getPixelForDocPoint(docPoint);
188 if (color.opacityU8() > 0) {
189 return color.convertedTo(image->colorSpace());
190 }
191 }
192 if (image->wrapAroundModePermitted()) {
193 imagePoint = KisWrappedRect::ptToWrappedPt(imagePoint.toPoint(), image->bounds(), image->wrapAroundModeAxis());
194 }
195 KoColor sampledColor = KoColor();
196 image->projection()->pixel(imagePoint.x(), imagePoint.y(), &sampledColor);
197 return sampledColor;
198 }
199 }
200 }
201 }
202
203 KoColor col = KoColor();
204 QScreen *targetScreen = QGuiApplication::screenAt(p);
205
206 if (targetScreen) {
207 const QPoint screenPos = p - targetScreen->geometry().topLeft();
208
209 QImage grabImage = targetScreen->grabWindow(0, screenPos.x(), screenPos.y(), 1, 1).toImage();
210 col.fromQColor(QColor::fromRgb(grabImage.pixel(0, 0)));
211 }
212
213 return col;
214}
215
217{
218 if (m_d->lblScreenColorInfo) {
219 KoColor col = grabScreenColor(globalPos);
220 QString colname = KoColor::toQString(col);
221 QString location = QString::number(globalPos.x())+QString(", ")+QString::number(globalPos.y());
222 m_d->lblScreenColorInfo->setWordWrap(true);
223 m_d->lblScreenColorInfo->setText(location+QString(": ")+colname);
224 }
225}
226
228{
229 // If the cross is visible the grabbed color will be black most of the times
230 //cp->setCrossVisible(!cp->geometry().contains(e->pos()));
231
232 continueUpdateColorSampling(e->globalPos());
233 return true;
234}
235
237{
238 setCurrentColor(grabScreenColor(e->globalPos()));
241 return true;
242}
243
245{
246 if (e->matches(QKeySequence::Cancel)) {
247 cancel();
248 } else if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
249 setCurrentColor(grabScreenColor(QCursor::pos()));
252 }
253 e->accept();
254 return true;
255}
256
258{
259 // HACK: see class KisGrabKeyboardFocusRecoveryWorkaround
261
262 m_d->inputGrabberWidget->removeEventFilter(m_d->colorSamplingEventFilter);
263 m_d->inputGrabberWidget->releaseMouse();
264#ifdef Q_OS_WIN32
265 m_d->updateTimer->stop();
266 m_d->dummyTransparentWindow.setVisible(false);
267#endif
268 m_d->inputGrabberWidget->releaseKeyboard();
269 m_d->inputGrabberWidget->setMouseTracking(false);
270
271 if (m_d->lblScreenColorInfo) {
272 m_d->lblScreenColorInfo->setText(QLatin1String("\n"));
273 }
274
275 m_d->screenColorSamplerButton->setDisabled(false);
276}
277
279{
280 QWidget::changeEvent(e);
281}
282
284{
285 static QPoint lastGlobalPos;
286 QPoint newGlobalPos = QCursor::pos();
287 if (lastGlobalPos == newGlobalPos)
288 return;
289 lastGlobalPos = newGlobalPos;
290
291 if (!rect().contains(mapFromGlobal(newGlobalPos))) { // Inside the dialog mouse tracking works, handleColorSamplingMouseMove will be called
292 continueUpdateColorSampling(newGlobalPos);
293#ifdef Q_OS_WIN32
294 m_d->dummyTransparentWindow.setPosition(newGlobalPos);
295#endif
296 }
297}
298
300{
301 const KoColor color = grabScreenColor(globalPos);
302 // QTBUG-39792, do not change standard, custom color selectors while moving as
303 // otherwise it is not possible to preselect a custom cell for assignment.
304 setCurrentColor(color);
306 updateColorLabelText(globalPos);
307}
308
309// Event filter to be installed on the dialog while in color-sampling mode.
311 : QObject(parent)
312 , m_w(w)
313{}
314
316{
317 switch (event->type()) {
318 case QEvent::MouseMove:
319 return m_w->handleColorSamplingMouseMove(static_cast<QMouseEvent *>(event));
320 case QEvent::MouseButtonRelease:
321 return m_w->handleColorSamplingMouseButtonRelease(static_cast<QMouseEvent *>(event));
322 case QEvent::KeyPress:
323 return m_w->handleColorSamplingKeyPress(static_cast<QKeyEvent *>(event));
324 default:
325 break;
326 }
327 return false;
328}
329
330// Register the color sampler factory with the internal color selector
333}
334
335#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
336#include <QtGui/private/qguiapplication_p.h>
337#include <QtGui/qpa/qplatformintegration.h>
338#include <qpa/qplatformservices.h>
339
341{
342 //Uses the screenshot portal on linux, since we can't just look at raw pixels anymore
343 auto *services = QGuiApplicationPrivate::platformIntegration()->services();
344
345 if (services && services->hasCapability(QPlatformServices::Capability::ColorPicking)) {
346 QPlatformServiceColorPicker *colorPicker = services->colorPicker(QGuiApplication::allWindows().first());
347
348 if (colorPicker) {
349 connect(colorPicker, SIGNAL(colorPicked(const QColor&)), this, SLOT(colorPicked(const QColor&)));
350
351 colorPicker->pickColor();
352 return true;
353 }
354 }
355 return false;
356}
357
358void KisScreenColorSampler::colorPicked(const QColor& color)
359{
360 KoColor col(Qt::white, KoColorSpaceRegistry::instance()->rgb8());
361 col.fromQColor(color);
362 if (qEnvironmentVariableIsSet("KRITA_USE_GAMMA22_FOR_COLOR_PICKING")) {
364 col.setProfile(profile);
365 }
366
367 setCurrentColor(col);
368
370}
371#endif
const Params2D p
KIS_DECLARE_STATIC_INITIALIZER
@ PRIMARIES_ITU_R_BT_709_5
@ TRC_ITU_R_BT_470_6_SYSTEM_M
KisReferenceImagesDecorationSP referenceImagesDecoration() const
KisCoordinatesConverter * coordinatesConverter
KisAbstractCanvasWidget * canvasWidget
_Private::Traits< T >::Result widgetToDocument(const T &obj) const
_Private::Traits< T >::Result widgetToImage(const T &obj) const
static void setScreenColorSamplerFactory(std::function< KisScreenColorSamplerBase *(QWidget *)> f)
static KisGrabKeyboardFocusRecoveryWorkaround * instance()
const KoColorSpace * colorSpace() const
WrapAroundAxis wrapAroundModeAxis() const
KisPaintDeviceSP projection() const
QRect bounds() const override
bool wrapAroundModePermitted() const
bool pixel(qint32 x, qint32 y, QColor *c) const
static KisPart * instance()
Definition KisPart.cpp:131
The KisScreenColorSampler class Based on the original QColorDialog's screen color picker,...
void sigNewColorHovered(KoColor c)
void changeEvent(QEvent *event) override
const QScopedPointer< Private > m_d
void colorPicked(const QColor &color)
void sigNewColorSampled(KoColor c)
void setPerformRealColorSamplingOfCanvas(bool enable)
void updateColorLabelText(const QPoint &globalPos)
KisScreenColorSampler(bool showInfoLabel=false, QWidget *parent=0)
void continueUpdateColorSampling(const QPoint &globalPos)
bool handleColorSamplingMouseMove(QMouseEvent *e)
KoColor grabScreenColor(const QPoint &p)
void updateIcons() override
reloads icon(s) when theme is updated
bool performRealColorSamplingOfCanvas() const
bool handleColorSamplingMouseButtonRelease(QMouseEvent *e)
bool handleColorSamplingKeyPress(QKeyEvent *e)
static KisScreenColorSampler * createScreenColorSampler(QWidget *parent=0)
bool eventFilter(QObject *, QEvent *event) override
KisScreenColorSamplingEventFilter(KisScreenColorSampler *w, QObject *parent=0)
KisCanvas2 * canvasBase() const
Definition KisView.cpp:434
KisImageWSP image() const
Definition KisView.cpp:439
QPointer< KisDocument > document
Definition KisView.cpp:121
static QString toQString(const KoColor &color)
toQString create a user-visible string of the channel names and the channel values
Definition KoColor.cpp:656
KoColor convertedTo(const KoColorSpace *cs, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags) const
Definition KoColor.cpp:163
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213
quint8 opacityU8() const
Definition KoColor.cpp:341
void setProfile(const KoColorProfile *profile)
assign new profile without converting pixel data
Definition KoColor.cpp:177
#define kisIcon(name)
Definition kis_icon.h:26
KisScreenColorSamplingEventFilter * colorSamplingEventFilter
static QPoint ptToWrappedPt(QPoint pt, const QRect &wrapRect, WrapAroundAxis wrapAxis)
The KoColorProfileQuery struct.
static KoColorSpaceRegistry * instance()
const KoColorProfile * profileFor(const KoColorProfileQuery &query, const bool generate=true) const
profileFor tries to find the profile that matches these characteristics, if no such profile is found,...