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 KisScreenColorSamplercreateScreenColorSampler (QWidget *parent=0)
 

Protected Member Functions

void changeEvent (QEvent *event) override
 

Private Slots

void updateColorSampling ()
 

Private Member Functions

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

Private Attributes

const QScopedPointer< Privatem_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 59 of file KisScreenColorSampler.cpp.

60{
61 QVBoxLayout *layout = new QVBoxLayout(this);
62 m_d->screenColorSamplerButton = new QPushButton();
63
64 m_d->screenColorSamplerButton->setMinimumHeight(25);
65 layout->addWidget(m_d->screenColorSamplerButton);
66
67 if (showInfoLabel) {
68 m_d->lblScreenColorInfo = new QLabel(QLatin1String("\n"));
69 layout->addWidget(m_d->lblScreenColorInfo);
70 }
71
72 layout->setContentsMargins(0, 0, 0, 0);
73
74 connect(m_d->screenColorSamplerButton, SIGNAL(clicked()), SLOT(sampleScreenColor()));
75
77
78#ifdef Q_OS_WIN32
79 m_d->updateTimer = new QTimer(this);
80 m_d->dummyTransparentWindow.resize(1, 1);
81 m_d->dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
82 connect(m_d->updateTimer, SIGNAL(timeout()), SLOT(updateColorSampling()));
83#endif
84}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
KisScreenColorSamplerBase(QWidget *parent=0)
const QScopedPointer< Private > m_d
void updateIcons() override
reloads icon(s) when theme is updated

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

◆ ~KisScreenColorSampler()

KisScreenColorSampler::~KisScreenColorSampler ( )
override

Definition at line 86 of file KisScreenColorSampler.cpp.

87{
88}

Member Function Documentation

◆ cancel

void KisScreenColorSampler::cancel ( )
slot

Definition at line 153 of file KisScreenColorSampler.cpp.

154{
156 setCurrentColor(m_d->beforeScreenColorSampling);
158}
void sigNewColorSampled(KoColor c)

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

◆ changeEvent()

void KisScreenColorSampler::changeEvent ( QEvent * event)
overrideprotected

Definition at line 269 of file KisScreenColorSampler.cpp.

270{
271 QWidget::changeEvent(e);
272}

◆ continueUpdateColorSampling()

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

Definition at line 290 of file KisScreenColorSampler.cpp.

291{
292 const KoColor color = grabScreenColor(globalPos);
293 // QTBUG-39792, do not change standard, custom color selectors while moving as
294 // otherwise it is not possible to preselect a custom cell for assignment.
295 setCurrentColor(color);
297 updateColorLabelText(globalPos);
298}
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 95 of file KisScreenColorSampler.cpp.

96{
97 return m_d->currentColor;
98}

References m_d.

◆ grabScreenColor()

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

Definition at line 160 of file KisScreenColorSampler.cpp.

161{
162 // First check whether we're clicking on a Krita window for some real color sampling
163 if (m_d->performRealColorSamplingOfCanvas) {
164 Q_FOREACH(KisView *view, KisPart::instance()->views()) {
165 const KisCanvas2 *canvas = view->canvasBase();
166 const QWidget *canvasWidget = canvas->canvasWidget();
167 QPoint widgetPoint = canvasWidget->mapFromGlobal(p);
168
169 if (canvasWidget->visibleRegion().contains(widgetPoint)) {
170 KisImageWSP image = view->image();
171
172 if (image) {
173 QPointF imagePoint = canvas->coordinatesConverter()->widgetToImage(widgetPoint);
174 // sample from reference images first
175 KisSharedPtr<KisReferenceImagesLayer> referenceImageLayer = view->document()->referenceImagesLayer();
176
177 if (referenceImageLayer && canvas->referenceImagesDecoration()->visible()) {
178 QColor color = referenceImageLayer->getPixel(imagePoint);
179 if (color.isValid()) {
180 return KoColor(color, image->colorSpace());
181 }
182 }
183 if (image->wrapAroundModePermitted()) {
184 imagePoint = KisWrappedRect::ptToWrappedPt(imagePoint.toPoint(), image->bounds(), image->wrapAroundModeAxis());
185 }
186 KoColor sampledColor = KoColor();
187 image->projection()->pixel(imagePoint.x(), imagePoint.y(), &sampledColor);
188 return sampledColor;
189 }
190 }
191 }
192 }
193
194 KoColor col = KoColor();
195 QScreen *targetScreen = QGuiApplication::screenAt(p);
196
197 if (targetScreen) {
198 const QPoint screenPos = p - targetScreen->geometry().topLeft();
199
200 QImage grabImage = targetScreen->grabWindow(0, screenPos.x(), screenPos.y(), 1, 1).toImage();
201 col.fromQColor(QColor::fromRgb(grabImage.pixel(0, 0)));
202 }
203
204 return col;
205}
const Params2D p
KisReferenceImagesDecorationSP referenceImagesDecoration() const
KisCoordinatesConverter * coordinatesConverter
KisAbstractCanvasWidget * canvasWidget
_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:427
KisImageWSP image() const
Definition KisView.cpp:432
QPointer< KisDocument > document
Definition KisView.cpp:121
void fromQColor(const QColor &c)
Convenient function for converting from a QColor.
Definition KoColor.cpp:213
static QPoint ptToWrappedPt(QPoint pt, const QRect &wrapRect, WrapAroundAxis wrapAxis)

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

◆ handleColorSamplingKeyPress()

bool KisScreenColorSampler::handleColorSamplingKeyPress ( QKeyEvent * e)

Definition at line 235 of file KisScreenColorSampler.cpp.

236{
237 if (e->matches(QKeySequence::Cancel)) {
238 cancel();
239 } else if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
240 setCurrentColor(grabScreenColor(QCursor::pos()));
243 }
244 e->accept();
245 return true;
246}

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

◆ handleColorSamplingMouseButtonRelease()

bool KisScreenColorSampler::handleColorSamplingMouseButtonRelease ( QMouseEvent * e)

Definition at line 227 of file KisScreenColorSampler.cpp.

228{
229 setCurrentColor(grabScreenColor(e->globalPos()));
232 return true;
233}

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

◆ handleColorSamplingMouseMove()

bool KisScreenColorSampler::handleColorSamplingMouseMove ( QMouseEvent * e)

Definition at line 218 of file KisScreenColorSampler.cpp.

219{
220 // If the cross is visible the grabbed color will be black most of the times
221 //cp->setCrossVisible(!cp->geometry().contains(e->pos()));
222
223 continueUpdateColorSampling(e->globalPos());
224 return true;
225}
void continueUpdateColorSampling(const QPoint &globalPos)

References continueUpdateColorSampling().

◆ performRealColorSamplingOfCanvas()

bool KisScreenColorSampler::performRealColorSamplingOfCanvas ( ) const

Definition at line 100 of file KisScreenColorSampler.cpp.

101{
102 return m_d->performRealColorSamplingOfCanvas;
103}

References m_d.

◆ releaseColorSampling()

void KisScreenColorSampler::releaseColorSampling ( )
private

Definition at line 248 of file KisScreenColorSampler.cpp.

249{
250 // HACK: see class KisGrabKeyboardFocusRecoveryWorkaround
252
253 m_d->inputGrabberWidget->removeEventFilter(m_d->colorSamplingEventFilter);
254 m_d->inputGrabberWidget->releaseMouse();
255#ifdef Q_OS_WIN32
256 m_d->updateTimer->stop();
257 m_d->dummyTransparentWindow.setVisible(false);
258#endif
259 m_d->inputGrabberWidget->releaseKeyboard();
260 m_d->inputGrabberWidget->setMouseTracking(false);
261
262 if (m_d->lblScreenColorInfo) {
263 m_d->lblScreenColorInfo->setText(QLatin1String("\n"));
264 }
265
266 m_d->screenColorSamplerButton->setDisabled(false);
267}
static KisGrabKeyboardFocusRecoveryWorkaround * instance()

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

◆ sampleScreenColor

void KisScreenColorSampler::sampleScreenColor ( )
slot

Definition at line 110 of file KisScreenColorSampler.cpp.

111{
112 m_d->updateInputGrabberWidget();
113 if (m_d->inputGrabberWidget == nullptr) {
115 return;
116 }
117
118 if (!m_d->colorSamplingEventFilter) {
119 m_d->colorSamplingEventFilter = new KisScreenColorSamplingEventFilter(this, this);
120 }
121 m_d->inputGrabberWidget->installEventFilter(m_d->colorSamplingEventFilter);
122 // If user pushes Escape, the last color before sampling will be restored.
123 m_d->beforeScreenColorSampling = currentColor();
124 m_d->inputGrabberWidget->grabMouse(Qt::CrossCursor);
125
126#ifdef Q_OS_WIN32 // excludes WinCE and WinRT
127 // On Windows mouse tracking doesn't work over other processes's windows
128 m_d->updateTimer->start(30);
129
130 // HACK: Because mouse grabbing doesn't work across processes, we have to have a dummy,
131 // invisible window to catch the mouse click, otherwise we will click whatever we clicked
132 // and loose focus.
133 m_d->dummyTransparentWindow.show();
134#endif
135 m_d->inputGrabberWidget->grabKeyboard();
136 /* With setMouseTracking(true) the desired color can be more precisely sampled,
137 * and continuously pushing the mouse button is not necessary.
138 */
139 m_d->inputGrabberWidget->setMouseTracking(true);
140
141 m_d->screenColorSamplerButton->setDisabled(true);
142
143 const QPoint globalPos = QCursor::pos();
145 updateColorLabelText(globalPos);
146}

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

◆ setCurrentColor

void KisScreenColorSampler::setCurrentColor ( KoColor c)
slot

Definition at line 148 of file KisScreenColorSampler.cpp.

149{
150 m_d->currentColor = c;
151}

References m_d.

◆ setPerformRealColorSamplingOfCanvas

void KisScreenColorSampler::setPerformRealColorSamplingOfCanvas ( bool enable)
slot

Definition at line 105 of file KisScreenColorSampler.cpp.

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

References m_d.

◆ sigNewColorHovered

void KisScreenColorSampler::sigNewColorHovered ( KoColor c)
signal

◆ sigNewColorSampled

void KisScreenColorSampler::sigNewColorSampled ( KoColor c)
signal

◆ updateColorLabelText()

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

Definition at line 207 of file KisScreenColorSampler.cpp.

208{
209 if (m_d->lblScreenColorInfo) {
210 KoColor col = grabScreenColor(globalPos);
211 QString colname = KoColor::toQString(col);
212 QString location = QString::number(globalPos.x())+QString(", ")+QString::number(globalPos.y());
213 m_d->lblScreenColorInfo->setWordWrap(true);
214 m_d->lblScreenColorInfo->setText(location+QString(": ")+colname);
215 }
216}
static QString toQString(const KoColor &color)
toQString create a user-visible string of the channel names and the channel values
Definition KoColor.cpp:655

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

◆ updateColorSampling

void KisScreenColorSampler::updateColorSampling ( )
privateslot

Definition at line 274 of file KisScreenColorSampler.cpp.

275{
276 static QPoint lastGlobalPos;
277 QPoint newGlobalPos = QCursor::pos();
278 if (lastGlobalPos == newGlobalPos)
279 return;
280 lastGlobalPos = newGlobalPos;
281
282 if (!rect().contains(mapFromGlobal(newGlobalPos))) { // Inside the dialog mouse tracking works, handleColorSamplingMouseMove will be called
283 continueUpdateColorSampling(newGlobalPos);
284#ifdef Q_OS_WIN32
285 m_d->dummyTransparentWindow.setPosition(newGlobalPos);
286#endif
287 }
288}

References continueUpdateColorSampling(), and m_d.

◆ updateIcons()

void KisScreenColorSampler::updateIcons ( )
overridevirtual

reloads icon(s) when theme is updated

Implements KisScreenColorSamplerBase.

Definition at line 90 of file KisScreenColorSampler.cpp.

91{
92 m_d->screenColorSamplerButton->setIcon(kisIcon("krita_tool_color_sampler"));
93}
#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: