Move methods to sample screen pixels to QgsGui

This commit is contained in:
Nyall Dawson 2019-09-23 13:49:21 +10:00
parent d498eed0a5
commit 2ff91d24a9
5 changed files with 59 additions and 30 deletions

View File

@ -145,6 +145,20 @@ Returns the platform's HIG flags.
~QgsGui();
static QColor sampleColor( QPoint point );
%Docstring
Samples the color on screen at the specified global ``point`` (pixel).
.. versionadded:: 3.10
%End
static QScreen *findScreenAt( QPoint point );
%Docstring
Returns the screen at the given global ``point`` (pixel).
.. versionadded:: 3.10
%End
private:
QgsGui( const QgsGui &other );
};

View File

@ -24,6 +24,7 @@
#include "qgssettings.h"
#include "qgsproject.h"
#include "qgsguiutils.h"
#include "qgsgui.h"
#include <QPainter>
#include <QMouseEvent>
@ -282,7 +283,7 @@ void QgsColorButton::mouseMoveEvent( QMouseEvent *e )
{
if ( mPickingColor )
{
setButtonBackground( sampleColor( e->globalPos() ) );
setButtonBackground( QgsGui::sampleColor( e->globalPos() ) );
e->accept();
return;
}
@ -343,7 +344,7 @@ void QgsColorButton::stopPicking( QPoint eventPos, bool samplingColor )
return;
}
setColor( sampleColor( eventPos ) );
setColor( QgsGui::sampleColor( eventPos ) );
addRecentColor( mColor );
}
@ -424,30 +425,6 @@ 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() )
{
if ( screen->geometry().contains( pos ) )
{
return screen;
}
}
return nullptr;
}
void QgsColorButton::setValidColor( const QColor &newColor )
{
if ( newColor.isValid() )

View File

@ -453,10 +453,6 @@ class GUI_EXPORT QgsColorButton : public QToolButton
private:
QColor sampleColor( QPoint point ) const;
static QScreen *findScreenAt( QPoint pos );
Behavior mBehavior = QgsColorButton::ShowDialog;
QString mColorDialogTitle;
QColor mColor;

View File

@ -50,6 +50,9 @@
#include "qgsproviderguiregistry.h"
#include "qgsprojectstorageguiregistry.h"
#include <QScreen>
#include <QDesktopWidget>
QgsGui *QgsGui::instance()
{
static QgsGui *sInstance( new QgsGui() );
@ -165,6 +168,31 @@ QgsGui::~QgsGui()
delete mProviderGuiRegistry;
}
QColor QgsGui::sampleColor( QPoint point )
{
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 *QgsGui::findScreenAt( QPoint point )
{
const QList< QScreen * > screens = QGuiApplication::screens();
for ( QScreen *screen : screens )
{
if ( screen->geometry().contains( point ) )
{
return screen;
}
}
return nullptr;
}
QgsGui::QgsGui()
{
#ifdef Q_OS_MAC

View File

@ -178,6 +178,20 @@ class GUI_EXPORT QgsGui : public QObject
~QgsGui();
/**
* Samples the color on screen at the specified global \a point (pixel).
*
* \since QGIS 3.10
*/
static QColor sampleColor( QPoint point );
/**
* Returns the screen at the given global \a point (pixel).
*
* \since QGIS 3.10
*/
static QScreen *findScreenAt( QPoint point );
private:
QgsGui();