diff options
author | Fan PengCheng <fanpengcheng@uniontech.com> | 2022-02-23 13:04:22 +0800 |
---|---|---|
committer | Fan PengCheng <fanpengcheng@uniontech.com> | 2022-03-24 12:24:13 +0800 |
commit | 897845ca7f9e23400e9368efaf5dfbc5d0ffb67c (patch) | |
tree | 5c173e42bc6e43ca1289fa3964bb21ea11c3058d /src/widgets/dialogs | |
parent | e555069151f25d22a5b35cc3bd68a9620e021966 (diff) |
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>
Diffstat (limited to 'src/widgets/dialogs')
-rw-r--r-- | src/widgets/dialogs/qcolordialog.cpp | 45 |
1 files changed, 31 insertions, 14 deletions
diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp index a93c6fe629..6e39f76215 100644 --- a/src/widgets/dialogs/qcolordialog.cpp +++ b/src/widgets/dialogs/qcolordialog.cpp @@ -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) |