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
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <QStyle> #include <QStyle>
#include <QStyleOptionToolButton> #include <QStyleOptionToolButton>
#include <QWidgetAction> #include <QWidgetAction>
#include <QScreen>
#include <QLabel> #include <QLabel>
#include <QGridLayout> #include <QGridLayout>
#include <QPushButton> #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 //if left button depressed, sample color under cursor and temporarily update button color
//to give feedback to user //to give feedback to user
QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), e->globalPos().x(), e->globalPos().y(), 1, 1 ); QScreen *screen = findScreenAt( e->globalPos() );
QImage snappedImage = snappedPixmap.toImage(); if ( screen )
QColor hoverColor = snappedImage.pixel( 0, 0 ); {
setButtonBackground( hoverColor ); 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(); e->accept();
return; return;
Expand Down Expand Up @@ -297,6 +302,7 @@ void QgsColorButton::stopPicking( QPointF eventPos, bool sampleColor )
releaseMouse(); releaseMouse();
releaseKeyboard(); releaseKeyboard();
unsetCursor(); unsetCursor();
setMouseTracking( false );
mPickingColor = false; mPickingColor = false;


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


//grab snapshot of pixel under mouse cursor //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(); QImage snappedImage = snappedPixmap.toImage();
//extract color from pixel and set color //extract color from pixel and set color
setColor( snappedImage.pixel( 0, 0 ) ); 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 ) void QgsColorButton::setValidColor( const QColor &newColor )
{ {
if ( newColor.isValid() ) if ( newColor.isValid() )
Expand Down Expand Up @@ -671,11 +689,12 @@ void QgsColorButton::pasteColor()


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


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


private: private:


static QScreen *findScreenAt( const QPoint &pos );
Behavior mBehavior = QgsColorButton::ShowDialog; Behavior mBehavior = QgsColorButton::ShowDialog;
QString mColorDialogTitle; QString mColorDialogTitle;
QColor mColor; QColor mColor;
Expand Down
25 changes: 23 additions & 2 deletions src/gui/qgscompoundcolorwidget.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <QMessageBox> #include <QMessageBox>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QMouseEvent> #include <QMouseEvent>
#include <QScreen>
#include <QInputDialog> #include <QInputDialog>
#include <QVBoxLayout> #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() void QgsCompoundColorWidget::saveSettings()
{ {
//save changes to scheme //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 QColor QgsCompoundColorWidget::sampleColor( QPoint point ) const
{ {
int sampleRadius = mSpinBoxRadius->value() - 1; int sampleRadius = mSpinBoxRadius->value() - 1;
QPixmap snappedPixmap = QPixmap::grabWindow( QApplication::desktop()->winId(), point.x() - sampleRadius, point.y() - sampleRadius, QScreen *screen = findScreenAt( point );
1 + sampleRadius * 2, 1 + sampleRadius * 2 ); 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(); QImage snappedImage = snappedPixmap.toImage();
//scan all pixels and take average color //scan all pixels and take average color
return averageColor( snappedImage ); return averageColor( snappedImage );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgscompoundcolorwidget.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class GUI_EXPORT QgsCompoundColorWidget : public QgsPanelWidget, private Ui::Qgs


private: private:


static QScreen *findScreenAt( const QPoint &pos );

bool mAllowAlpha = true; bool mAllowAlpha = true;


int mLastCustomColorIndex = 0; int mLastCustomColorIndex = 0;
Expand Down

0 comments on commit 656de62

Please sign in to comment.