diff options
Diffstat (limited to 'src/widgets/dialogs/qcolordialog.cpp')
-rw-r--r-- | src/widgets/dialogs/qcolordialog.cpp | 333 |
1 files changed, 192 insertions, 141 deletions
diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp index 30c81f8c0e..22efecedc9 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,33 @@ #include "private/qdialog_p.h" +#include <qpa/qplatformintegration.h> +#include <qpa/qplatformservices.h> +#include <private/qguiapplication_p.h> + +#include <QtCore/qpointer.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,17 +102,18 @@ public: void showAlpha(bool b); bool isAlphaVisible() const; void retranslateStrings(); + bool supportsColorPicking() const; - void _q_addCustom(); + void addCustom(); void _q_setCustom(int index, QRgb color); - void _q_newHsv(int h, int s, int v); - void _q_newColorTypedIn(QRgb rgb); - void _q_nextCustom(int, int); - void _q_newCustom(int, int); - void _q_newStandard(int, int); - void _q_pickScreenColor(); - void _q_updateColorPicking(); + void newHsv(int h, int s, int v); + void newColorTypedIn(QRgb rgb); + void nextCustom(int, int); + void newCustom(int, int); + void newStandard(int, int); + void pickScreenColor(); + void updateColorPicking(); void updateColorLabelText(const QPoint &); void updateColorPicking(const QPoint &pos); void releaseColorPicking(); @@ -141,6 +122,7 @@ public: bool handleColorPickingKeyPress(QKeyEvent *e); bool canBeNativeDialog() const override; + void setVisible(bool visible) override; QWellArray *custom; QWellArray *standard; @@ -156,7 +138,7 @@ public: QPushButton *ok; QPushButton *cancel; QPushButton *addCusBt; - QPushButton *screenColorPickerButton; + QPushButton *eyeDropperButton = nullptr; QColor selectedQColor; int nextCust; bool smallDisplay; @@ -179,7 +161,7 @@ private: //////////// QWellArray BEGIN -namespace { +namespace QtPrivate { class QWellArray : public QWidget { @@ -343,7 +325,7 @@ void QWellArray::paintCell(QPainter* p, int row, int col, const QRect &rect) const QPalette & g = palette(); QStyleOptionFrame opt; opt.initFrom(this); - int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt); + int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, this); opt.lineWidth = dfw; opt.midLineWidth = 1; opt.rect = rect.adjusted(b, b, -b, -b); @@ -460,19 +442,19 @@ void QWellArray::keyPressEvent(QKeyEvent* e) { switch(e->key()) { // Look at the key code case Qt::Key_Left: // If 'left arrow'-key, - if(curCol > 0) // and cr't not in leftmost col + if (curCol > 0) // and cr't not in leftmost col setCurrent(curRow, curCol - 1); // set cr't to next left column break; case Qt::Key_Right: // Correspondingly... - if(curCol < numCols()-1) + if (curCol < numCols()-1) setCurrent(curRow, curCol + 1); break; case Qt::Key_Up: - if(curRow > 0) + if (curRow > 0) setCurrent(curRow - 1, curCol); break; case Qt::Key_Down: - if(curRow < numRows()-1) + if (curRow < numRows()-1) setCurrent(curRow + 1, curCol); break; #if 0 @@ -494,7 +476,7 @@ void QWellArray::keyPressEvent(QKeyEvent* e) return; } -} +} // namespace QtPrivate //////////// QWellArray END @@ -584,7 +566,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 +716,13 @@ private: bool crossVisible; }; +} // namespace QtPrivate + static int pWidth = 220; static int pHeight = 200; +namespace QtPrivate { + class QColorLuminancePicker : public QWidget { Q_OBJECT @@ -797,6 +783,10 @@ QColorLuminancePicker::~QColorLuminancePicker() void QColorLuminancePicker::mouseMoveEvent(QMouseEvent *m) { + if (m->buttons() == Qt::NoButton) { + m->ignore(); + return; + } setVal(y2val(m->position().toPoint().y())); } void QColorLuminancePicker::mousePressEvent(QMouseEvent *m) @@ -846,11 +836,10 @@ void QColorLuminancePicker::paintEvent(QPaintEvent *) qDrawShadePanel(&p, r, g, true); p.setPen(g.windowText().color()); p.setBrush(g.windowText()); - QPolygon a; - int y = val2y(val); - a.setPoints(3, w, y, w+5, y+5, w+5, y-5); p.eraseRect(w, 0, 5, height()); - p.drawPolygon(a); + const int y = val2y(val); + const std::array<QPoint, 3> points = {QPoint(w, y), QPoint(w + 5, y + 5), QPoint(w + 5, y - 5)}; + p.drawPolygon(points.data(), static_cast<int>(points.size())); } void QColorLuminancePicker::setCol(int h, int s , int v) @@ -931,6 +920,10 @@ void QColorPicker::setCol(int h, int s) void QColorPicker::mouseMoveEvent(QMouseEvent *m) { QPoint p = m->position().toPoint() - contentsRect().topLeft(); + if (m->buttons() == Qt::NoButton) { + m->ignore(); + return; + } setCol(p); emit newCol(hue, sat); } @@ -1197,8 +1190,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 +1295,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 +1318,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::textEdited, 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 +1394,20 @@ void QColorShower::hsvEd() void QColorShower::htmlEd() { - QColor c; QString t = htEd->text(); - c.setNamedColor(t); + if (t.isEmpty()) + return; + + if (!t.startsWith(u"#")) { + t.prepend(u"#"); + 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); @@ -1488,7 +1491,7 @@ void QColorShower::updateQColor() } //sets all widgets to display h,s,v -void QColorDialogPrivate::_q_newHsv(int h, int s, int v) +void QColorDialogPrivate::newHsv(int h, int s, int v) { if (!nativeDialogInUse) { cs->setHsv(h, s, v); @@ -1502,7 +1505,7 @@ void QColorDialogPrivate::setCurrentRgbColor(QRgb rgb) { if (!nativeDialogInUse) { cs->setRgb(rgb); - _q_newColorTypedIn(rgb); + newColorTypedIn(rgb); } } @@ -1535,7 +1538,7 @@ bool QColorDialogPrivate::selectColor(const QColor &col) const int index = int(match - standardColors); const int column = index / standardColorRows; const int row = index % standardColorRows; - _q_newStandard(row, column); + newStandard(row, column); standard->setCurrent(row, column); standard->setSelected(row, column); standard->setFocus(); @@ -1551,7 +1554,7 @@ bool QColorDialogPrivate::selectColor(const QColor &col) const int index = int(match - customColors); const int column = index / customColorRows; const int row = index % customColorRows; - _q_newCustom(row, column); + newCustom(row, column); custom->setCurrent(row, column); custom->setSelected(row, column); custom->setFocus(); @@ -1566,13 +1569,15 @@ QColor QColorDialogPrivate::grabScreenColor(const QPoint &p) QScreen *screen = QGuiApplication::screenAt(p); if (!screen) screen = QGuiApplication::primaryScreen(); - const QPixmap pixmap = screen->grabWindow(0, p.x(), p.y(), 1, 1); + const QRect screenRect = screen->geometry(); + const QPixmap pixmap = + screen->grabWindow(0, p.x() - screenRect.x(), p.y() - screenRect.y(), 1, 1); const QImage i = pixmap.toImage(); return i.pixel(0, 0); } //sets all widgets except cs to display rgb -void QColorDialogPrivate::_q_newColorTypedIn(QRgb rgb) +void QColorDialogPrivate::newColorTypedIn(QRgb rgb) { if (!nativeDialogInUse) { int h, s, v; @@ -1582,12 +1587,12 @@ void QColorDialogPrivate::_q_newColorTypedIn(QRgb rgb) } } -void QColorDialogPrivate::_q_nextCustom(int r, int c) +void QColorDialogPrivate::nextCustom(int r, int c) { nextCust = r + customColorRows * c; } -void QColorDialogPrivate::_q_newCustom(int r, int c) +void QColorDialogPrivate::newCustom(int r, int c) { const int i = r + customColorRows * c; setCurrentRgbColor(QColorDialogOptions::customColor(i)); @@ -1595,16 +1600,30 @@ void QColorDialogPrivate::_q_newCustom(int r, int c) standard->setSelected(-1,-1); } -void QColorDialogPrivate::_q_newStandard(int r, int c) +void QColorDialogPrivate::newStandard(int r, int c) { setCurrentRgbColor(QColorDialogOptions::standardColor(r + c * 6)); if (custom) custom->setSelected(-1,-1); } -void QColorDialogPrivate::_q_pickScreenColor() +void QColorDialogPrivate::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); @@ -1633,16 +1652,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 (eyeDropperButton) { + eyeDropperButton->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())); } @@ -1659,10 +1680,10 @@ 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); + eyeDropperButton->setDisabled(false); } void QColorDialogPrivate::init(const QColor &initial) @@ -1719,17 +1740,23 @@ void QColorDialogPrivate::initWidgets() #ifndef QT_NO_SHORTCUT lblBasicColors->setBuddy(standard); #endif - q->connect(standard, SIGNAL(selected(int,int)), SLOT(_q_newStandard(int,int))); + QObjectPrivate::connect(standard, &QColorWell::selected, + this, &QColorDialogPrivate::newStandard); leftLay->addWidget(lblBasicColors); 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()) { + eyeDropperButton = new QPushButton(); + leftLay->addWidget(eyeDropperButton); + lblScreenColorInfo = new QLabel("\n"_L1); + leftLay->addWidget(lblScreenColorInfo); + QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked, + this, &QColorDialogPrivate::pickScreenColor); + } else { + eyeDropperButton = nullptr; + lblScreenColorInfo = nullptr; + } #endif leftLay->addStretch(); @@ -1737,10 +1764,10 @@ void QColorDialogPrivate::initWidgets() custom = new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors()); custom->setAcceptDrops(true); - q->connect(custom, SIGNAL(selected(int,int)), SLOT(_q_newCustom(int,int))); - q->connect(custom, SIGNAL(currentChanged(int,int)), SLOT(_q_nextCustom(int,int))); + QObjectPrivate::connect(custom, &QColorWell::selected, this, &QColorDialogPrivate::newCustom); + QObjectPrivate::connect(custom, &QColorWell::currentChanged, this, &QColorDialogPrivate::nextCustom); - q->connect(custom, &QWellArray::colorChanged, [this] (int index, QRgb color) { + QObject::connect(custom, &QWellArray::colorChanged, q, [this] (int index, QRgb color) { QColorDialogOptions::setCustomColor(index, color); if (custom) custom->update(); @@ -1754,7 +1781,7 @@ void QColorDialogPrivate::initWidgets() leftLay->addWidget(custom); addCusBt = new QPushButton(q); - QObject::connect(addCusBt, SIGNAL(clicked()), q, SLOT(_q_addCustom())); + QObjectPrivate::connect(addCusBt, &QPushButton::clicked, this, &QColorDialogPrivate::addCustom); leftLay->addWidget(addCusBt); } else { // better color picker size for small displays @@ -1762,7 +1789,7 @@ void QColorDialogPrivate::initWidgets() QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size(); pWidth = pHeight = qMin(screenSize.width(), screenSize.height()); pHeight -= 20; - if(screenSize.height() > screenSize.width()) + if (screenSize.height() > screenSize.width()) pWidth -= 20; #else pWidth = 150; @@ -1782,7 +1809,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(); @@ -1802,16 +1829,17 @@ void QColorDialogPrivate::initWidgets() pickLay->addStretch(); #endif - QObject::connect(cp, SIGNAL(newCol(int,int)), lp, SLOT(setCol(int,int))); - QObject::connect(lp, SIGNAL(newHsv(int,int,int)), q, SLOT(_q_newHsv(int,int,int))); + QObject::connect(cp, &QColorPicker::newCol, lp, qOverload<int, int>(&QColorLuminancePicker::setCol)); + QObjectPrivate::connect(lp, &QColorLuminancePicker::newHsv, this, &QColorDialogPrivate::newHsv); rightLay->addStretch(); cs = new QColorShower(q); pickLay->setContentsMargins(cs->gl->contentsMargins()); - QObject::connect(cs, SIGNAL(newCol(QRgb)), q, SLOT(_q_newColorTypedIn(QRgb))); - QObject::connect(cs, SIGNAL(currentColorChanged(QColor)), - q, SIGNAL(currentColorChanged(QColor))); + QObjectPrivate::connect(cs, &QColorShower::newCol, + this, &QColorDialogPrivate::newColorTypedIn); + QObject::connect(cs, &QColorShower::currentColorChanged, + q, &QColorDialog::currentColorChanged); #if defined(QT_SMALL_COLORDIALOG) topLay->addWidget(cs); #else @@ -1824,14 +1852,15 @@ void QColorDialogPrivate::initWidgets() mainLay->addWidget(buttons); ok = buttons->addButton(QDialogButtonBox::Ok); - QObject::connect(ok, SIGNAL(clicked()), q, SLOT(accept())); + QObject::connect(ok, &QPushButton::clicked, q, &QColorDialog::accept); ok->setDefault(true); cancel = buttons->addButton(QDialogButtonBox::Cancel); - QObject::connect(cancel, SIGNAL(clicked()), q, SLOT(reject())); + QObject::connect(cancel, &QPushButton::clicked, q, &QColorDialog::reject); #ifdef Q_OS_WIN32 updateTimer = new QTimer(q); - QObject::connect(updateTimer, SIGNAL(timeout()), q, SLOT(_q_updateColorPicking())); + QObjectPrivate::connect(updateTimer, &QTimer::timeout, + this, qOverload<>(&QColorDialogPrivate::updateColorPicking)); #endif retranslateStrings(); } @@ -1839,9 +1868,12 @@ void QColorDialogPrivate::initWidgets() void QColorDialogPrivate::initHelper(QPlatformDialogHelper *h) { QColorDialog *d = q_func(); - QObject::connect(h, SIGNAL(currentColorChanged(QColor)), d, SIGNAL(currentColorChanged(QColor))); - QObject::connect(h, SIGNAL(colorSelected(QColor)), d, SIGNAL(colorSelected(QColor))); - static_cast<QPlatformColorDialogHelper *>(h)->setOptions(options); + auto *colorDialogHelper = static_cast<QPlatformColorDialogHelper*>(h); + QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::currentColorChanged, + d, &QColorDialog::currentColorChanged); + QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::colorSelected, + d, &QColorDialog::colorSelected); + colorDialogHelper->setOptions(options); } void QColorDialogPrivate::helperPrepareShow(QPlatformDialogHelper *) @@ -1849,7 +1881,7 @@ void QColorDialogPrivate::helperPrepareShow(QPlatformDialogHelper *) options->setWindowTitle(q_func()->windowTitle()); } -void QColorDialogPrivate::_q_addCustom() +void QColorDialogPrivate::addCustom() { QColorDialogOptions::setCustomColor(nextCust, cs->currentColor()); if (custom) @@ -1866,12 +1898,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 (eyeDropperButton) + eyeDropperButton->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, @@ -1885,12 +1927,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; @@ -1950,7 +1990,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); @@ -2056,6 +2096,8 @@ void QColorDialog::setOptions(ColorDialogOptions options) if (!d->nativeDialogInUse) { d->buttons->setVisible(!(options & NoButtons)); d->showAlpha(options & ShowAlphaChannel); + if (d->eyeDropperButton) + d->eyeDropperButton->setVisible(!(options & NoEyeDropperButton)); } } @@ -2075,6 +2117,7 @@ QColorDialog::ColorDialogOptions QColorDialog::options() const \value ShowAlphaChannel Allow the user to select the alpha component of a color. \value NoButtons Don't display \uicontrol{OK} and \uicontrol{Cancel} buttons. (Useful for "live dialogs".) + \value NoEyeDropperButton Hide the \uicontrol{Eye Dropper} button. This value was added in Qt 6.6. \value DontUseNativeDialog Use Qt's standard color dialog instead of the operating system native color dialog. @@ -2105,27 +2148,37 @@ QColorDialog::ColorDialogOptions QColorDialog::options() const */ void QColorDialog::setVisible(bool visible) { - Q_D(QColorDialog); + // will call QColorDialogPrivate::setVisible override + QDialog::setVisible(visible); +} - if (visible){ - if (testAttribute(Qt::WA_WState_ExplicitShowHide) && !testAttribute(Qt::WA_WState_Hidden)) - return; - } else if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden)) - return; +/*! + \internal + + The implementation of QColorDialog::setVisible() has to live here so that the call + to hide() in ~QDialog calls this function; it wouldn't call the override of + QDialog::setVisible(). +*/ +void QColorDialogPrivate::setVisible(bool visible) +{ + Q_Q(QColorDialog); if (visible) - d->selectedQColor = QColor(); + 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 (nativeDialogInUse) { + if (setNativeDialogVisible(visible)) { + // Set WA_DontShowOnScreen so that QDialog::setVisible(visible) below + // updates the state correctly, but skips showing the non-native version: + q->setAttribute(Qt::WA_DontShowOnScreen); + } else { + initWidgets(); + } } else { - setAttribute(Qt::WA_DontShowOnScreen, false); + q->setAttribute(Qt::WA_DontShowOnScreen, false); } - QDialog::setVisible(visible); + QDialogPrivate::setVisible(visible); } /*! @@ -2173,7 +2226,6 @@ QColor QColorDialog::getColor(const QColor &initial, QWidget *parent, const QStr QColorDialog::~QColorDialog() { - } /*! @@ -2187,7 +2239,7 @@ void QColorDialog::changeEvent(QEvent *e) QDialog::changeEvent(e); } -void QColorDialogPrivate::_q_updateColorPicking() +void QColorDialogPrivate::updateColorPicking() { #ifndef QT_NO_CURSOR Q_Q(QColorDialog); @@ -2213,7 +2265,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) |