@@ -239,10 +239,7 @@ void QgsColorButton::mouseMoveEvent( QMouseEvent *e )
239
239
QScreen *screen = findScreenAt ( e->globalPos () );
240
240
if ( screen )
241
241
{
242
- QPixmap snappedPixmap = screen->grabWindow ( QApplication::desktop ()->winId (), e->globalPos ().x (), e->globalPos ().y (), 1 , 1 );
243
- QImage snappedImage = snappedPixmap.toImage ();
244
- QColor hoverColor = snappedImage.pixel ( 0 , 0 );
245
- setButtonBackground ( hoverColor );
242
+ setButtonBackground ( sampleColor ( e->globalPos () ) );
246
243
}
247
244
}
248
245
e->accept ();
@@ -286,26 +283,22 @@ void QgsColorButton::mouseReleaseEvent( QMouseEvent *e )
286
283
QToolButton::mouseReleaseEvent ( e );
287
284
}
288
285
289
- void QgsColorButton::stopPicking ( QPointF eventPos, bool sampleColor )
286
+ void QgsColorButton::stopPicking ( QPoint eventPos, bool samplingColor )
290
287
{
291
288
// release mouse and keyboard, and reset cursor
292
289
releaseMouse ();
293
290
releaseKeyboard ();
294
- unsetCursor ();
291
+ QgsApplication::restoreOverrideCursor ();
295
292
setMouseTracking ( false );
296
293
mPickingColor = false ;
297
294
298
- if ( !sampleColor )
295
+ if ( !samplingColor )
299
296
{
300
297
// not sampling color, nothing more to do
301
298
return ;
302
299
}
303
300
304
- // grab snapshot of pixel under mouse cursor
305
- QPixmap snappedPixmap = QApplication::desktop ()->screen ()->grab ( QRect ( eventPos.x (), eventPos.y (), 1 , 1 ) );
306
- QImage snappedImage = snappedPixmap.toImage ();
307
- // extract color from pixel and set color
308
- setColor ( snappedImage.pixel ( 0 , 0 ) );
301
+ setColor ( sampleColor ( eventPos ) );
309
302
addRecentColor ( mColor );
310
303
}
311
304
@@ -356,6 +349,19 @@ void QgsColorButton::dropEvent( QDropEvent *e )
356
349
}
357
350
}
358
351
352
+ QColor QgsColorButton::sampleColor ( QPoint point ) const
353
+ {
354
+
355
+ QScreen *screen = findScreenAt ( point );
356
+ if ( ! screen )
357
+ {
358
+ return QColor ();
359
+ }
360
+ QPixmap snappedPixmap = screen->grabWindow ( QApplication::desktop ()->winId (), point.x (), point.y (), 1 , 1 );
361
+ QImage snappedImage = snappedPixmap.toImage ();
362
+ return snappedImage.pixel ( 0 , 0 );
363
+ }
364
+
359
365
QScreen *QgsColorButton::findScreenAt ( QPoint pos )
360
366
{
361
367
for ( QScreen *screen : QGuiApplication::screens () )
@@ -680,7 +686,7 @@ void QgsColorButton::pasteColor()
680
686
void QgsColorButton::activatePicker ()
681
687
{
682
688
// activate picker color
683
- setCursor ( QgsApplication::getThemeCursor ( QgsApplication::Cursor::Sampler ) );
689
+ QApplication::setOverrideCursor ( QgsApplication::getThemeCursor ( QgsApplication::Cursor::Sampler ) );
684
690
grabMouse ();
685
691
grabKeyboard ();
686
692
mPickingColor = true ;
0 commit comments