Skip to content

Commit 948452b

Browse files
committedFeb 15, 2018
[bugfix] Restore color picker from layer styling panel
Fixes #17721 Successfully tested on Windows 10 and Linux with Qt 5.10
1 parent 19ab0c8 commit 948452b

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed
 

‎src/gui/qgscolorbutton.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,7 @@ void QgsColorButton::mouseMoveEvent( QMouseEvent *e )
239239
QScreen *screen = findScreenAt( e->globalPos() );
240240
if ( screen )
241241
{
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() ) );
246243
}
247244
}
248245
e->accept();
@@ -286,26 +283,22 @@ void QgsColorButton::mouseReleaseEvent( QMouseEvent *e )
286283
QToolButton::mouseReleaseEvent( e );
287284
}
288285

289-
void QgsColorButton::stopPicking( QPointF eventPos, bool sampleColor )
286+
void QgsColorButton::stopPicking( QPoint eventPos, bool samplingColor )
290287
{
291288
//release mouse and keyboard, and reset cursor
292289
releaseMouse();
293290
releaseKeyboard();
294-
unsetCursor();
291+
QgsApplication::restoreOverrideCursor();
295292
setMouseTracking( false );
296293
mPickingColor = false;
297294

298-
if ( !sampleColor )
295+
if ( !samplingColor )
299296
{
300297
//not sampling color, nothing more to do
301298
return;
302299
}
303300

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 ) );
309302
addRecentColor( mColor );
310303
}
311304

@@ -356,6 +349,19 @@ void QgsColorButton::dropEvent( QDropEvent *e )
356349
}
357350
}
358351

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+
359365
QScreen *QgsColorButton::findScreenAt( QPoint pos )
360366
{
361367
for ( QScreen *screen : QGuiApplication::screens() )
@@ -680,7 +686,7 @@ void QgsColorButton::pasteColor()
680686
void QgsColorButton::activatePicker()
681687
{
682688
//activate picker color
683-
setCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::Sampler ) );
689+
QApplication::setOverrideCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::Sampler ) );
684690
grabMouse();
685691
grabKeyboard();
686692
mPickingColor = true;

‎src/gui/qgscolorbutton.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ class GUI_EXPORT QgsColorButton : public QToolButton
399399

400400
private:
401401

402+
403+
QColor sampleColor( QPoint point ) const;
404+
402405
static QScreen *findScreenAt( QPoint pos );
403406
Behavior mBehavior = QgsColorButton::ShowDialog;
404407
QString mColorDialogTitle;
@@ -436,10 +439,10 @@ class GUI_EXPORT QgsColorButton : public QToolButton
436439
/**
437440
* Ends a color picking operation
438441
* \param eventPos global position of pixel to sample color from
439-
* \param sampleColor set to true to actually sample the color, false to just cancel
442+
* \param samplingColor set to true to actually sample the color, false to just cancel
440443
* the color picking operation
441444
*/
442-
void stopPicking( QPointF eventPos, bool sampleColor = true );
445+
void stopPicking( QPoint eventPos, bool samplingColor = true );
443446

444447
/**
445448
* Create a color icon for display in the drop-down menu

0 commit comments

Comments
 (0)
Please sign in to comment.