diff options
Diffstat (limited to 'src/widgets/dialogs/qcolordialog.cpp')
-rw-r--r-- | src/widgets/dialogs/qcolordialog.cpp | 187 |
1 files changed, 105 insertions, 82 deletions
diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp index 8696797b64..1bb65e6c24 100644 --- a/src/widgets/dialogs/qcolordialog.cpp +++ b/src/widgets/dialogs/qcolordialog.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qcolordialog.h" @@ -75,17 +39,31 @@ #include "private/qdialog_p.h" +#include <qpa/qplatformintegration.h> +#include <qpa/qplatformservices.h> +#include <private/qguiapplication_p.h> + #include <algorithm> QT_BEGIN_NAMESPACE -namespace { +using namespace Qt::StringLiterals; + +namespace QtPrivate { class QColorLuminancePicker; class QColorPicker; class QColorShower; class QWellArray; +class QColorWell; class QColorPickingEventFilter; -} // unnamed namespace +} // namespace QtPrivate + +using QColorLuminancePicker = QtPrivate::QColorLuminancePicker; +using QColorPicker = QtPrivate::QColorPicker; +using QColorShower = QtPrivate::QColorShower; +using QWellArray = QtPrivate::QWellArray; +using QColorWell = QtPrivate::QColorWell; +using QColorPickingEventFilter = QtPrivate::QColorPickingEventFilter; class QColorDialogPrivate : public QDialogPrivate { @@ -122,6 +100,7 @@ public: void showAlpha(bool b); bool isAlphaVisible() const; void retranslateStrings(); + bool supportsColorPicking() const; void _q_addCustom(); void _q_setCustom(int index, QRgb color); @@ -179,7 +158,7 @@ private: //////////// QWellArray BEGIN -namespace { +namespace QtPrivate { class QWellArray : public QWidget { @@ -494,7 +473,7 @@ void QWellArray::keyPressEvent(QKeyEvent* e) return; } -} +} // namespace QtPrivate //////////// QWellArray END @@ -584,7 +563,7 @@ static inline void rgb2hsv(QRgb rgb, int &h, int &s, int &v) c.getHsv(&h, &s, &v); } -namespace { +namespace QtPrivate { class QColorWell : public QWellArray { @@ -734,9 +713,13 @@ private: bool crossVisible; }; +} // namespace QtPrivate + static int pWidth = 220; static int pHeight = 200; +namespace QtPrivate { + class QColorLuminancePicker : public QWidget { Q_OBJECT @@ -1197,8 +1180,8 @@ QColorShower::QColorShower(QColorDialog *parent) #else gl->addWidget(lab, 0, 0, 1, -1); #endif - connect(lab, SIGNAL(colorDropped(QRgb)), this, SIGNAL(newCol(QRgb))); - connect(lab, SIGNAL(colorDropped(QRgb)), this, SLOT(setRgb(QRgb))); + connect(lab, &QColorShowLabel::colorDropped, this, &QColorShower::newCol); + connect(lab, &QColorShowLabel::colorDropped, this, &QColorShower::setRgb); hEd = new QColSpinBox(this); hEd->setRange(0, 359); @@ -1302,12 +1285,13 @@ QColorShower::QColorShower(QColorDialog *parent) alphaLab->hide(); lblHtml = new QLabel(this); htEd = new QLineEdit(this); + htEd->setObjectName("qt_colorname_lineedit"); #ifndef QT_NO_SHORTCUT lblHtml->setBuddy(htEd); #endif #if QT_CONFIG(regularexpression) - QRegularExpression regExp(QStringLiteral("#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})")); + QRegularExpression regExp(QStringLiteral("#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})")); QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, this); htEd->setValidator(validator); #else @@ -1324,20 +1308,20 @@ QColorShower::QColorShower(QColorDialog *parent) gl->addWidget(htEd, 5, 2, 1, /*colspan=*/ 3); #endif - connect(hEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); - connect(sEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); - connect(vEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); + connect(hEd, &QSpinBox::valueChanged, this, &QColorShower::hsvEd); + connect(sEd, &QSpinBox::valueChanged, this, &QColorShower::hsvEd); + connect(vEd, &QSpinBox::valueChanged, this, &QColorShower::hsvEd); - connect(rEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); - connect(gEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); - connect(bEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); - connect(alphaEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); - connect(htEd, SIGNAL(textEdited(QString)), this, SLOT(htmlEd())); + connect(rEd, &QSpinBox::valueChanged, this, &QColorShower::rgbEd); + connect(gEd, &QSpinBox::valueChanged, this, &QColorShower::rgbEd); + connect(bEd, &QSpinBox::valueChanged, this, &QColorShower::rgbEd); + connect(alphaEd, &QSpinBox::valueChanged, this, &QColorShower::rgbEd); + connect(htEd, &QLineEdit::textChanged, this, &QColorShower::htmlEd); retranslateStrings(); } -} // unnamed namespace +} // namespace QtPrivate inline QRgb QColorDialogPrivate::currentColor() const { return cs->currentColor(); } inline int QColorDialogPrivate::currentAlpha() const { return cs->currentAlpha(); } @@ -1400,11 +1384,20 @@ void QColorShower::hsvEd() void QColorShower::htmlEd() { - QColor c; QString t = htEd->text(); - c.setNamedColor(t); + if (t.isEmpty()) + return; + + if (!t.startsWith(QStringLiteral("#"))) { + t = QStringLiteral("#") + t; + QSignalBlocker blocker(htEd); + htEd->setText(t); + } + + QColor c = QColor::fromString(t); if (!c.isValid()) return; + curCol = qRgba(c.red(), c.green(), c.blue(), currentAlpha()); rgb2hsv(curCol, hue, sat, val); @@ -1607,6 +1600,20 @@ void QColorDialogPrivate::_q_newStandard(int r, int c) void QColorDialogPrivate::_q_pickScreenColor() { Q_Q(QColorDialog); + + auto *platformServices = QGuiApplicationPrivate::platformIntegration()->services(); + if (platformServices->hasCapability(QPlatformServices::Capability::ColorPicking)) { + if (auto *colorPicker = platformServices->colorPicker(q->windowHandle())) { + q->connect(colorPicker, &QPlatformServiceColorPicker::colorPicked, q, + [q, colorPicker](const QColor &color) { + colorPicker->deleteLater(); + q->setCurrentColor(color); + }); + colorPicker->pickColor(); + return; + } + } + if (!colorPickingEventFilter) colorPickingEventFilter = new QColorPickingEventFilter(this, q); q->installEventFilter(colorPickingEventFilter); @@ -1635,16 +1642,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())); } @@ -1661,7 +1670,7 @@ void QColorDialogPrivate::releaseColorPicking() #endif q->releaseKeyboard(); q->setMouseTracking(false); - lblScreenColorInfo->setText(QLatin1String("\n")); + lblScreenColorInfo->setText("\n"_L1); addCusBt->setDisabled(false); buttons->setDisabled(false); screenColorPickerButton->setDisabled(false); @@ -1726,12 +1735,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("\n"_L1); + leftLay->addWidget(lblScreenColorInfo); + q->connect(screenColorPickerButton, SIGNAL(clicked()), SLOT(_q_pickScreenColor())); + } else { + screenColorPickerButton = nullptr; + lblScreenColorInfo = nullptr; + } #endif leftLay->addStretch(); @@ -1784,7 +1797,7 @@ void QColorDialogPrivate::initWidgets() pickLay->addLayout(cLay); cp = new QColorPicker(q); - cp->setFrameStyle(QFrame::Panel + QFrame::Sunken); + cp->setFrameStyle(QFrame::Panel | QFrame::Sunken); #if defined(QT_SMALL_COLORDIALOG) cp->hide(); @@ -1868,12 +1881,22 @@ 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 +{ + const auto integration = QGuiApplicationPrivate::platformIntegration(); + return integration->hasCapability(QPlatformIntegration::ScreenWindowGrabbing) + || integration->services()->hasCapability(QPlatformServices::Capability::ColorPicking); +} + bool QColorDialogPrivate::canBeNativeDialog() const { // Don't use Q_Q here! This function is called from ~QDialog, @@ -1887,12 +1910,10 @@ bool QColorDialogPrivate::canBeNativeDialog() const return false; } - QLatin1String staticName(QColorDialog::staticMetaObject.className()); - QLatin1String dynamicName(q->metaObject()->className()); - return (staticName == dynamicName); + return strcmp(QColorDialog::staticMetaObject.className(), q->metaObject()->className()) == 0; } -static const Qt::WindowFlags DefaultWindowFlags = +static const Qt::WindowFlags qcd_DefaultWindowFlags = Qt::Dialog | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint; @@ -1952,7 +1973,7 @@ QColorDialog::QColorDialog(QWidget *parent) \a initial color. */ QColorDialog::QColorDialog(const QColor &initial, QWidget *parent) - : QDialog(*new QColorDialogPrivate, parent, DefaultWindowFlags) + : QDialog(*new QColorDialogPrivate, parent, qcd_DefaultWindowFlags) { Q_D(QColorDialog); d->init(initial); @@ -2119,10 +2140,13 @@ void QColorDialog::setVisible(bool visible) d->selectedQColor = QColor(); if (d->nativeDialogInUse) { - d->setNativeDialogVisible(visible); - // Set WA_DontShowOnScreen so that QDialog::setVisible(visible) below - // updates the state correctly, but skips showing the non-native version: - setAttribute(Qt::WA_DontShowOnScreen); + if (d->setNativeDialogVisible(visible)) { + // Set WA_DontShowOnScreen so that QDialog::setVisible(visible) below + // updates the state correctly, but skips showing the non-native version: + setAttribute(Qt::WA_DontShowOnScreen); + } else { + d->initWidgets(); + } } else { setAttribute(Qt::WA_DontShowOnScreen, false); } @@ -2215,7 +2239,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) |