Krita Source Code Documentation
Loading...
Searching...
No Matches
KisScreenColorSampler Class Reference

The KisScreenColorSampler class Based on the original QColorDialog's screen color picker, this class provides a button that can be used to activate a color sampler that can sample from anywhere on the screen. More...

#include <KisScreenColorSampler.h>

+ Inheritance diagram for KisScreenColorSampler:

Classes

struct  Private
 

Public Slots

void cancel ()
 
void sampleScreenColor ()
 
void setCurrentColor (KoColor c)
 
void setPerformRealColorSamplingOfCanvas (bool enable)
 

Signals

void sigNewColorHovered (KoColor c)
 
void sigNewColorSampled (KoColor c)
 
- Signals inherited from KisScreenColorSamplerBase
void sigNewColorSampled (KoColor)
 

Public Member Functions

KoColor currentColor ()
 
bool handleColorSamplingKeyPress (QKeyEvent *e)
 
bool handleColorSamplingMouseButtonRelease (QMouseEvent *e)
 
bool handleColorSamplingMouseMove (QMouseEvent *e)
 
 KisScreenColorSampler (bool showInfoLabel=false, QWidget *parent=0)
 
bool performRealColorSamplingOfCanvas () const
 
void updateIcons () override
 reloads icon(s) when theme is updated
 
 ~KisScreenColorSampler () override
 
- Public Member Functions inherited from KisScreenColorSamplerBase
 KisScreenColorSamplerBase (QWidget *parent=0)
 
virtual ~KisScreenColorSamplerBase ()
 

Static Public Member Functions

static KisScreenColorSampler * createScreenColorSampler (QWidget *parent=0)
 

Protected Member Functions

void changeEvent (QEvent *event) override
 

Private Slots

void colorPicked (const QColor &color)
 
void updateColorSampling ()
 

Private Member Functions

void continueUpdateColorSampling (const QPoint &globalPos)
 
KoColor grabScreenColor (const QPoint &p)
 
void releaseColorSampling ()
 
bool tryToUsePlatformIntegration ()
 
void updateColorLabelText (const QPoint &globalPos)
 

Private Attributes

const QScopedPointer< Private > m_d
 

Detailed Description

The KisScreenColorSampler class Based on the original QColorDialog's screen color picker, this class provides a button that can be used to activate a color sampler that can sample from anywhere on the screen.

Definition at line 23 of file KisScreenColorSampler.h.

Constructor & Destructor Documentation

◆ KisScreenColorSampler()

KisScreenColorSampler::KisScreenColorSampler ( bool showInfoLabel = false,
QWidget * parent = 0 )
explicit

Definition at line 60 of file KisScreenColorSampler.cpp.

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}
KisScreenColorSamplerBase(QWidget *parent=0)
const QScopedPointer< Private > m_d
void updateIcons() override
reloads icon(s) when theme is updated

References m_d, sampleScreenColor(), updateColorSampling(), and updateIcons().

◆ ~KisScreenColorSampler()

KisScreenColorSampler::~KisScreenColorSampler ( )
override

Definition at line 87 of file KisScreenColorSampler.cpp.

88{
89}

Member Function Documentation

◆ cancel

void KisScreenColorSampler::cancel ( )
slot

Definition at line 160 of file KisScreenColorSampler.cpp.

161{
163 setCurrentColor(m_d->beforeScreenColorSampling);
165}
void sigNewColorSampled(KoColor c)

References currentColor(), m_d, releaseColorSampling(), setCurrentColor(), and sigNewColorSampled().

◆ changeEvent()

void KisScreenColorSampler::changeEvent ( QEvent * event)
overrideprotected

Definition at line 278 of file KisScreenColorSampler.cpp.

279{
280 QWidget::changeEvent(e);
281}

◆ colorPicked

void KisScreenColorSampler::colorPicked ( const QColor & color)
privateslot

Definition at line 358 of file KisScreenColorSampler.cpp.

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}
@ PRIMARIES_ITU_R_BT_709_5
@ TRC_ITU_R_BT_470_6_SYSTEM_M
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,...

References currentColor(), KoColor::fromQColor(), KoColorSpaceRegistry::instance(), PRIMARIES_ITU_R_BT_709_5, KoColorSpaceRegistry::profileFor(), setCurrentColor(), KoColor::setProfile(), sigNewColorSampled(), and TRC_ITU_R_BT_470_6_SYSTEM_M.

◆ continueUpdateColorSampling()

void KisScreenColorSampler::continueUpdateColorSampling ( const QPoint & globalPos)
private

Definition at line 299 of file KisScreenColorSampler.cpp.

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}
void sigNewColorHovered(KoColor c)
void updateColorLabelText(const QPoint &globalPos)
KoColor grabScreenColor(const QPoint &p)

References currentColor(), grabScreenColor(), setCurrentColor(), sigNewColorHovered(), and updateColorLabelText().

◆ createScreenColorSampler()

static KisScreenColorSampler * KisScreenColorSampler::createScreenColorSampler ( QWidget * parent = 0)
inlinestatic

Definition at line 41 of file KisScreenColorSampler.h.

41{return new KisScreenColorSampler(parent);}
KisScreenColorSampler(bool showInfoLabel=false, QWidget *parent=0)

◆ currentColor()

KoColor KisScreenColorSampler::currentColor ( )

Definition at line 96 of file KisScreenColorSampler.cpp.

97{
98 return m_d->currentColor;
99}

References m_d.

◆ grabScreenColor()

KoColor KisScreenColorSampler::grabScreenColor ( const QPoint & p)
private

Definition at line 167 of file KisScreenColorSampler.cpp.

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}
const Params2D p
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
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
KisCanvas2 * canvasBase() const
Definition KisView.cpp:434
KisImageWSP image() const
Definition KisView.cpp:439
QPointer< KisDocument > document
Definition KisView.cpp:121
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
static QPoint ptToWrappedPt(QPoint pt, const QRect &wrapRect, WrapAroundAxis wrapAxis)

References KisImage::bounds(), KisView::canvasBase(), KisCanvas2::canvasWidget, KisImage::colorSpace(), KoColor::convertedTo(), KisCanvas2::coordinatesConverter, KisView::document, KoColor::fromQColor(), KisView::image(), KisPart::instance(), m_d, KoColor::opacityU8(), p, KisPaintDevice::pixel(), KisImage::projection(), KisWrappedRect::ptToWrappedPt(), KisCanvas2::referenceImagesDecoration(), KisCanvasDecoration::visible(), KisCoordinatesConverter::widgetToDocument(), KisCoordinatesConverter::widgetToImage(), KisImage::wrapAroundModeAxis(), and KisImage::wrapAroundModePermitted().

◆ handleColorSamplingKeyPress()

bool KisScreenColorSampler::handleColorSamplingKeyPress ( QKeyEvent * e)

Definition at line 244 of file KisScreenColorSampler.cpp.

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}

References cancel(), currentColor(), grabScreenColor(), releaseColorSampling(), setCurrentColor(), and sigNewColorSampled().

◆ handleColorSamplingMouseButtonRelease()

bool KisScreenColorSampler::handleColorSamplingMouseButtonRelease ( QMouseEvent * e)

Definition at line 236 of file KisScreenColorSampler.cpp.

237{
238 setCurrentColor(grabScreenColor(e->globalPos()));
241 return true;
242}

References currentColor(), grabScreenColor(), releaseColorSampling(), setCurrentColor(), and sigNewColorSampled().

◆ handleColorSamplingMouseMove()

bool KisScreenColorSampler::handleColorSamplingMouseMove ( QMouseEvent * e)

Definition at line 227 of file KisScreenColorSampler.cpp.

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}
void continueUpdateColorSampling(const QPoint &globalPos)

References continueUpdateColorSampling().

◆ performRealColorSamplingOfCanvas()

bool KisScreenColorSampler::performRealColorSamplingOfCanvas ( ) const

Definition at line 101 of file KisScreenColorSampler.cpp.

102{
103 return m_d->performRealColorSamplingOfCanvas;
104}

References m_d.

◆ releaseColorSampling()

void KisScreenColorSampler::releaseColorSampling ( )
private

Definition at line 257 of file KisScreenColorSampler.cpp.

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}
static KisGrabKeyboardFocusRecoveryWorkaround * instance()

References KisGrabKeyboardFocusRecoveryWorkaround::instance(), m_d, and KisGrabKeyboardFocusRecoveryWorkaround::recoverFocus().

◆ sampleScreenColor

void KisScreenColorSampler::sampleScreenColor ( )
slot

Definition at line 111 of file KisScreenColorSampler.cpp.

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}

References currentColor(), grabScreenColor(), m_d, setCurrentColor(), sigNewColorSampled(), tryToUsePlatformIntegration(), and updateColorLabelText().

◆ setCurrentColor

void KisScreenColorSampler::setCurrentColor ( KoColor c)
slot

Definition at line 155 of file KisScreenColorSampler.cpp.

156{
157 m_d->currentColor = c;
158}

References m_d.

◆ setPerformRealColorSamplingOfCanvas

void KisScreenColorSampler::setPerformRealColorSamplingOfCanvas ( bool enable)
slot

Definition at line 106 of file KisScreenColorSampler.cpp.

107{
108 m_d->performRealColorSamplingOfCanvas = enable;
109}

References m_d.

◆ sigNewColorHovered

void KisScreenColorSampler::sigNewColorHovered ( KoColor c)
signal

◆ sigNewColorSampled

void KisScreenColorSampler::sigNewColorSampled ( KoColor c)
signal

◆ tryToUsePlatformIntegration()

bool KisScreenColorSampler::tryToUsePlatformIntegration ( )
private

Definition at line 340 of file KisScreenColorSampler.cpp.

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}
void colorPicked(const QColor &color)

References colorPicked().

◆ updateColorLabelText()

void KisScreenColorSampler::updateColorLabelText ( const QPoint & globalPos)
private

Definition at line 216 of file KisScreenColorSampler.cpp.

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}
static QString toQString(const KoColor &color)
toQString create a user-visible string of the channel names and the channel values
Definition KoColor.cpp:656

References grabScreenColor(), m_d, and KoColor::toQString().

◆ updateColorSampling

void KisScreenColorSampler::updateColorSampling ( )
privateslot

Definition at line 283 of file KisScreenColorSampler.cpp.

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}

References continueUpdateColorSampling(), and m_d.

◆ updateIcons()

void KisScreenColorSampler::updateIcons ( )
overridevirtual

reloads icon(s) when theme is updated

Implements KisScreenColorSamplerBase.

Definition at line 91 of file KisScreenColorSampler.cpp.

92{
93 m_d->screenColorSamplerButton->setIcon(kisIcon("krita_tool_color_sampler"));
94}
#define kisIcon(name)
Definition kis_icon.h:26

References kisIcon, and m_d.

Member Data Documentation

◆ m_d

const QScopedPointer<Private> KisScreenColorSampler::m_d
private

Definition at line 59 of file KisScreenColorSampler.h.


The documentation for this class was generated from the following files: