Skip to content

Commit

Permalink
Merge pull request #6356 from elpaso/bugfix-17721-color-picker
Browse files Browse the repository at this point in the history
[bugfix] Restore color picker from layer styling panel
  • Loading branch information
elpaso committed Feb 16, 2018
2 parents 0310c1d + f334961 commit 8628ba1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
47 changes: 23 additions & 24 deletions src/gui/qgscolorbutton.cpp
Expand Up @@ -231,20 +231,7 @@ void QgsColorButton::mouseMoveEvent( QMouseEvent *e )
{
if ( mPickingColor )
{
//currently in color picker mode
if ( e->buttons() & Qt::LeftButton )
{
//if left button depressed, sample color under cursor and temporarily update button color
//to give feedback to user
QScreen *screen = findScreenAt( e->globalPos() );
if ( screen )
{
QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(), e->globalPos().x(), e->globalPos().y(), 1, 1 );
QImage snappedImage = snappedPixmap.toImage();
QColor hoverColor = snappedImage.pixel( 0, 0 );
setButtonBackground( hoverColor );
}
}
setButtonBackground( sampleColor( e->globalPos() ) );
e->accept();
return;
}
Expand Down Expand Up @@ -286,26 +273,23 @@ void QgsColorButton::mouseReleaseEvent( QMouseEvent *e )
QToolButton::mouseReleaseEvent( e );
}

void QgsColorButton::stopPicking( QPointF eventPos, bool sampleColor )
void QgsColorButton::stopPicking( QPoint eventPos, bool samplingColor )
{
//release mouse and keyboard, and reset cursor
releaseMouse();
releaseKeyboard();
unsetCursor();
QgsApplication::restoreOverrideCursor();
setMouseTracking( false );
mPickingColor = false;

if ( !sampleColor )
if ( !samplingColor )
{
//not sampling color, nothing more to do
//not sampling color, restore old color
setButtonBackground( mCurrentColor );
return;
}

//grab snapshot of pixel under mouse cursor
QPixmap snappedPixmap = QApplication::desktop()->screen()->grab( QRect( eventPos.x(), eventPos.y(), 1, 1 ) );
QImage snappedImage = snappedPixmap.toImage();
//extract color from pixel and set color
setColor( snappedImage.pixel( 0, 0 ) );
setColor( sampleColor( eventPos ) );
addRecentColor( mColor );
}

Expand Down Expand Up @@ -356,6 +340,19 @@ void QgsColorButton::dropEvent( QDropEvent *e )
}
}

QColor QgsColorButton::sampleColor( QPoint point ) const
{

QScreen *screen = findScreenAt( point );
if ( ! screen )
{
return QColor();
}
QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(), point.x(), point.y(), 1, 1 );
QImage snappedImage = snappedPixmap.toImage();
return snappedImage.pixel( 0, 0 );
}

QScreen *QgsColorButton::findScreenAt( QPoint pos )
{
for ( QScreen *screen : QGuiApplication::screens() )
Expand Down Expand Up @@ -680,7 +677,9 @@ void QgsColorButton::pasteColor()
void QgsColorButton::activatePicker()
{
//activate picker color
setCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::Sampler ) );
// Store current color
mCurrentColor = mColor;
QApplication::setOverrideCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::Sampler ) );
grabMouse();
grabKeyboard();
mPickingColor = true;
Expand Down
11 changes: 9 additions & 2 deletions src/gui/qgscolorbutton.h
Expand Up @@ -399,6 +399,9 @@ class GUI_EXPORT QgsColorButton : public QToolButton

private:


QColor sampleColor( QPoint point ) const;

static QScreen *findScreenAt( QPoint pos );
Behavior mBehavior = QgsColorButton::ShowDialog;
QString mColorDialogTitle;
Expand All @@ -408,6 +411,10 @@ class GUI_EXPORT QgsColorButton : public QToolButton
QgsColorSchemeRegistry *mColorSchemeRegistry = nullptr;

QColor mDefaultColor;

//! Store current color when start picking
QColor mCurrentColor;

QString mContext;
bool mAllowOpacity = false;
bool mColorSet = false;
Expand Down Expand Up @@ -436,10 +443,10 @@ class GUI_EXPORT QgsColorButton : public QToolButton
/**
* Ends a color picking operation
* \param eventPos global position of pixel to sample color from
* \param sampleColor set to true to actually sample the color, false to just cancel
* \param samplingColor set to true to actually sample the color, false to just cancel
* the color picking operation
*/
void stopPicking( QPointF eventPos, bool sampleColor = true );
void stopPicking( QPoint eventPos, bool samplingColor = true );

/**
* Create a color icon for display in the drop-down menu
Expand Down

0 comments on commit 8628ba1

Please sign in to comment.