widgets: Disable "Pick color" in QColorDialog when unavailable

On Wayland, you can't read screen content, so the "pick color"
feature does not do anything. In cases such as these, we should
detect that the platform does not support the feature and hide
or disable the button.

Fixes: QTBUG-101145
Change-Id: I192ca2aac78b3df5352b4dc71100b93f69dc5efb
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Fan PengCheng 2022-02-23 13:04:22 +08:00
parent e555069151
commit 897845ca7f

View File

@ -75,6 +75,9 @@
#include "private/qdialog_p.h" #include "private/qdialog_p.h"
#include <qpa/qplatformintegration.h>
#include <private/qguiapplication_p.h>
#include <algorithm> #include <algorithm>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -122,6 +125,7 @@ public:
void showAlpha(bool b); void showAlpha(bool b);
bool isAlphaVisible() const; bool isAlphaVisible() const;
void retranslateStrings(); void retranslateStrings();
bool supportsColorPicking() const;
void _q_addCustom(); void _q_addCustom();
void _q_setCustom(int index, QRgb color); void _q_setCustom(int index, QRgb color);
@ -1634,15 +1638,17 @@ void QColorDialogPrivate::_q_pickScreenColor()
addCusBt->setDisabled(true); addCusBt->setDisabled(true);
buttons->setDisabled(true); buttons->setDisabled(true);
if (screenColorPickerButton) {
screenColorPickerButton->setDisabled(true); screenColorPickerButton->setDisabled(true);
const QPoint globalPos = QCursor::pos(); const QPoint globalPos = QCursor::pos();
q->setCurrentColor(grabScreenColor(globalPos)); q->setCurrentColor(grabScreenColor(globalPos));
updateColorLabelText(globalPos); updateColorLabelText(globalPos);
}
} }
void QColorDialogPrivate::updateColorLabelText(const QPoint &globalPos) void QColorDialogPrivate::updateColorLabelText(const QPoint &globalPos)
{ {
if (lblScreenColorInfo)
lblScreenColorInfo->setText(QColorDialog::tr("Cursor at %1, %2\nPress ESC to cancel") lblScreenColorInfo->setText(QColorDialog::tr("Cursor at %1, %2\nPress ESC to cancel")
.arg(globalPos.x()) .arg(globalPos.x())
.arg(globalPos.y())); .arg(globalPos.y()));
@ -1725,12 +1731,16 @@ void QColorDialogPrivate::initWidgets()
leftLay->addWidget(standard); leftLay->addWidget(standard);
#if !defined(QT_SMALL_COLORDIALOG) #if !defined(QT_SMALL_COLORDIALOG)
// The screen color picker button if (supportsColorPicking()) {
screenColorPickerButton = new QPushButton(); screenColorPickerButton = new QPushButton();
leftLay->addWidget(screenColorPickerButton); leftLay->addWidget(screenColorPickerButton);
lblScreenColorInfo = new QLabel(QLatin1String("\n")); lblScreenColorInfo = new QLabel(QLatin1String("\n"));
leftLay->addWidget(lblScreenColorInfo); leftLay->addWidget(lblScreenColorInfo);
q->connect(screenColorPickerButton, SIGNAL(clicked()), SLOT(_q_pickScreenColor())); q->connect(screenColorPickerButton, SIGNAL(clicked()), SLOT(_q_pickScreenColor()));
} else {
screenColorPickerButton = nullptr;
lblScreenColorInfo = nullptr;
}
#endif #endif
leftLay->addStretch(); leftLay->addStretch();
@ -1867,12 +1877,20 @@ void QColorDialogPrivate::retranslateStrings()
lblBasicColors->setText(QColorDialog::tr("&Basic colors")); lblBasicColors->setText(QColorDialog::tr("&Basic colors"));
lblCustomColors->setText(QColorDialog::tr("&Custom colors")); lblCustomColors->setText(QColorDialog::tr("&Custom colors"));
addCusBt->setText(QColorDialog::tr("&Add to Custom Colors")); addCusBt->setText(QColorDialog::tr("&Add to Custom Colors"));
#if !defined(QT_SMALL_COLORDIALOG)
if (screenColorPickerButton)
screenColorPickerButton->setText(QColorDialog::tr("&Pick Screen Color")); screenColorPickerButton->setText(QColorDialog::tr("&Pick Screen Color"));
#endif
} }
cs->retranslateStrings(); cs->retranslateStrings();
} }
bool QColorDialogPrivate::supportsColorPicking() const
{
return QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ScreenWindowGrabbing);
}
bool QColorDialogPrivate::canBeNativeDialog() const bool QColorDialogPrivate::canBeNativeDialog() const
{ {
// Don't use Q_Q here! This function is called from ~QDialog, // Don't use Q_Q here! This function is called from ~QDialog,
@ -2214,7 +2232,6 @@ void QColorDialogPrivate::updateColorPicking(const QPoint &globalPos)
// otherwise it is not possible to pre-select a custom cell for assignment. // otherwise it is not possible to pre-select a custom cell for assignment.
setCurrentColor(color, ShowColor); setCurrentColor(color, ShowColor);
updateColorLabelText(globalPos); updateColorLabelText(globalPos);
} }
bool QColorDialogPrivate::handleColorPickingMouseMove(QMouseEvent *e) bool QColorDialogPrivate::handleColorPickingMouseMove(QMouseEvent *e)