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 <qpa/qplatformintegration.h>
#include <private/qguiapplication_p.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
@ -122,6 +125,7 @@ public:
void showAlpha(bool b);
bool isAlphaVisible() const;
void retranslateStrings();
bool supportsColorPicking() const;
void _q_addCustom();
void _q_setCustom(int index, QRgb color);
@ -1634,16 +1638,18 @@ void QColorDialogPrivate::_q_pickScreenColor()
addCusBt->setDisabled(true);
buttons->setDisabled(true);
screenColorPickerButton->setDisabled(true);
const QPoint globalPos = QCursor::pos();
q->setCurrentColor(grabScreenColor(globalPos));
updateColorLabelText(globalPos);
if (screenColorPickerButton) {
screenColorPickerButton->setDisabled(true);
const QPoint globalPos = QCursor::pos();
q->setCurrentColor(grabScreenColor(globalPos));
updateColorLabelText(globalPos);
}
}
void QColorDialogPrivate::updateColorLabelText(const QPoint &globalPos)
{
lblScreenColorInfo->setText(QColorDialog::tr("Cursor at %1, %2\nPress ESC to cancel")
if (lblScreenColorInfo)
lblScreenColorInfo->setText(QColorDialog::tr("Cursor at %1, %2\nPress ESC to cancel")
.arg(globalPos.x())
.arg(globalPos.y()));
}
@ -1725,12 +1731,16 @@ void QColorDialogPrivate::initWidgets()
leftLay->addWidget(standard);
#if !defined(QT_SMALL_COLORDIALOG)
// The screen color picker button
screenColorPickerButton = new QPushButton();
leftLay->addWidget(screenColorPickerButton);
lblScreenColorInfo = new QLabel(QLatin1String("\n"));
leftLay->addWidget(lblScreenColorInfo);
q->connect(screenColorPickerButton, SIGNAL(clicked()), SLOT(_q_pickScreenColor()));
if (supportsColorPicking()) {
screenColorPickerButton = new QPushButton();
leftLay->addWidget(screenColorPickerButton);
lblScreenColorInfo = new QLabel(QLatin1String("\n"));
leftLay->addWidget(lblScreenColorInfo);
q->connect(screenColorPickerButton, SIGNAL(clicked()), SLOT(_q_pickScreenColor()));
} else {
screenColorPickerButton = nullptr;
lblScreenColorInfo = nullptr;
}
#endif
leftLay->addStretch();
@ -1867,12 +1877,20 @@ void QColorDialogPrivate::retranslateStrings()
lblBasicColors->setText(QColorDialog::tr("&Basic colors"));
lblCustomColors->setText(QColorDialog::tr("&Custom colors"));
addCusBt->setText(QColorDialog::tr("&Add to Custom Colors"));
screenColorPickerButton->setText(QColorDialog::tr("&Pick Screen Color"));
#if !defined(QT_SMALL_COLORDIALOG)
if (screenColorPickerButton)
screenColorPickerButton->setText(QColorDialog::tr("&Pick Screen Color"));
#endif
}
cs->retranslateStrings();
}
bool QColorDialogPrivate::supportsColorPicking() const
{
return QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ScreenWindowGrabbing);
}
bool QColorDialogPrivate::canBeNativeDialog() const
{
// 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.
setCurrentColor(color, ShowColor);
updateColorLabelText(globalPos);
}
bool QColorDialogPrivate::handleColorPickingMouseMove(QMouseEvent *e)