Skip to content
Permalink
Browse files

Use screen to grab instead of deprecated grabWidget

  • Loading branch information
elpaso committed Nov 29, 2017
1 parent 3b933f5 commit 656de62e70ade0bce07cda96c63719a0aef49f2a
Showing with 51 additions and 8 deletions.
  1. +25 −6 src/gui/qgscolorbutton.cpp
  2. +1 −0 src/gui/qgscolorbutton.h
  3. +23 −2 src/gui/qgscompoundcolorwidget.cpp
  4. +2 −0 src/gui/qgscompoundcolorwidget.h
@@ -33,6 +33,7 @@
#include <QStyle>
#include <QStyleOptionToolButton>
#include <QWidgetAction>
#include <QScreen>
#include <QLabel>
#include <QGridLayout>
#include <QPushButton>
@@ -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;
@@ -297,6 +302,7 @@ void QgsColorButton::stopPicking( QPointF eventPos, bool sampleColor )
releaseMouse();
releaseKeyboard();
unsetCursor();
setMouseTracking( false );
mPickingColor = false;

if ( !sampleColor )
@@ -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 ) );
@@ -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() )
@@ -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
@@ -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;
@@ -27,6 +27,7 @@
#include <QMessageBox>
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QScreen>
#include <QInputDialog>
#include <QVBoxLayout>

@@ -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
@@ -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 );
@@ -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;

0 comments on commit 656de62

Please sign in to comment.
You can’t perform that action at this time.