Skip to content

Commit

Permalink
Use screen to grab instead of deprecated grabWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Nov 29, 2017
1 parent 3b933f5 commit 656de62
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
31 changes: 25 additions & 6 deletions src/gui/qgscolorbutton.cpp
Expand Up @@ -33,6 +33,7 @@
#include <QStyle>
#include <QStyleOptionToolButton>
#include <QWidgetAction>
#include <QScreen>
#include <QLabel>
#include <QGridLayout>
#include <QPushButton>
Expand Down Expand Up @@ -245,10 +246,14 @@ void QgsColorButton::mouseMoveEvent( QMouseEvent *e )
{
//if left button depressed, sample color under cursor and temporarily update button color
//to give feedback to user
QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), e->globalPos().x(), e->globalPos().y(), 1, 1 );
QImage snappedImage = snappedPixmap.toImage();
QColor hoverColor = snappedImage.pixel( 0, 0 );
setButtonBackground( hoverColor );
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 );
}
}
e->accept();
return;
Expand Down Expand Up @@ -297,6 +302,7 @@ void QgsColorButton::stopPicking( QPointF eventPos, bool sampleColor )
releaseMouse();
releaseKeyboard();
unsetCursor();
setMouseTracking( false );
mPickingColor = false;

if ( !sampleColor )
Expand All @@ -306,7 +312,7 @@ void QgsColorButton::stopPicking( QPointF eventPos, bool sampleColor )
}

//grab snapshot of pixel under mouse cursor
QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), eventPos.x(), eventPos.y(), 1, 1 );
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 ) );
Expand Down Expand Up @@ -360,6 +366,18 @@ void QgsColorButton::dropEvent( QDropEvent *e )
}
}

QScreen *QgsColorButton::findScreenAt( const QPoint &pos )
{
for ( QScreen *screen : QGuiApplication::screens() )
{
if ( screen->geometry().contains( pos ) )
{
return screen;
}
}
return nullptr;
}

void QgsColorButton::setValidColor( const QColor &newColor )
{
if ( newColor.isValid() )
Expand Down Expand Up @@ -671,11 +689,12 @@ void QgsColorButton::pasteColor()

void QgsColorButton::activatePicker()
{
//pick color
//activate picker color
setCursor( QgsApplication::getThemeCursor( QgsApplication::Cursor::Sampler ) );
grabMouse();
grabKeyboard();
mPickingColor = true;
setMouseTracking( true );
}

QColor QgsColorButton::color() const
Expand Down
1 change: 1 addition & 0 deletions src/gui/qgscolorbutton.h
Expand Up @@ -415,6 +415,7 @@ class GUI_EXPORT QgsColorButton : public QToolButton

private:

static QScreen *findScreenAt( const QPoint &pos );
Behavior mBehavior = QgsColorButton::ShowDialog;
QString mColorDialogTitle;
QColor mColor;
Expand Down
25 changes: 23 additions & 2 deletions src/gui/qgscompoundcolorwidget.cpp
Expand Up @@ -27,6 +27,7 @@
#include <QMessageBox>
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QScreen>
#include <QInputDialog>
#include <QVBoxLayout>

Expand Down Expand Up @@ -542,6 +543,18 @@ void QgsCompoundColorWidget::mActionShowInButtons_toggled( bool state )
}
}

QScreen *QgsCompoundColorWidget::findScreenAt( const QPoint &pos )
{
for ( QScreen *screen : QGuiApplication::screens() )
{
if ( screen->geometry().contains( pos ) )
{
return screen;
}
}
return nullptr;
}

void QgsCompoundColorWidget::saveSettings()
{
//save changes to scheme
Expand Down Expand Up @@ -694,8 +707,16 @@ QColor QgsCompoundColorWidget::averageColor( const QImage &image ) const
QColor QgsCompoundColorWidget::sampleColor( QPoint point ) const
{
int sampleRadius = mSpinBoxRadius->value() - 1;
QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), point.x() - sampleRadius, point.y() - sampleRadius,
1 + sampleRadius * 2, 1 + sampleRadius * 2 );
QScreen *screen = findScreenAt( point );
if ( ! screen )
{
return QColor();
}
QPixmap snappedPixmap = screen->grabWindow( QApplication::desktop()->winId(),
point.x() - sampleRadius,
point.y() - sampleRadius,
1 + sampleRadius * 2,
1 + sampleRadius * 2 );
QImage snappedImage = snappedPixmap.toImage();
//scan all pixels and take average color
return averageColor( snappedImage );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgscompoundcolorwidget.h
Expand Up @@ -137,6 +137,8 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs

private:

static QScreen *findScreenAt( const QPoint &pos );

bool mAllowAlpha = true;

int mLastCustomColorIndex = 0;
Expand Down

0 comments on commit 656de62

Please sign in to comment.