diff options
Diffstat (limited to 'src/widgets/dialogs/qcolordialog.cpp')
-rw-r--r-- | src/widgets/dialogs/qcolordialog.cpp | 452 |
1 files changed, 238 insertions, 214 deletions
diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp index 4c4e6e23ba..22efecedc9 100644 --- a/src/widgets/dialogs/qcolordialog.cpp +++ b/src/widgets/dialogs/qcolordialog.cpp @@ -1,47 +1,9 @@ -/**************************************************************************** -** -** 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" #include "qapplication.h" -#include "qdesktopwidget.h" -#include <private/qdesktopwidget_p.h> #include "qdrawutil.h" #include "qevent.h" #include "qimage.h" @@ -57,6 +19,9 @@ #include "qpainter.h" #include "qpixmap.h" #include "qpushbutton.h" +#if QT_CONFIG(regularexpression) +#include <qregularexpression.h> +#endif #if QT_CONFIG(settings) #include "qsettings.h" #endif @@ -74,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 { @@ -121,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(); @@ -140,6 +122,7 @@ public: bool handleColorPickingKeyPress(QKeyEvent *e); bool canBeNativeDialog() const override; + void setVisible(bool visible) override; QWellArray *custom; QWellArray *standard; @@ -155,7 +138,7 @@ public: QPushButton *ok; QPushButton *cancel; QPushButton *addCusBt; - QPushButton *screenColorPickerButton; + QPushButton *eyeDropperButton = nullptr; QColor selectedQColor; int nextCust; bool smallDisplay; @@ -178,7 +161,7 @@ private: //////////// QWellArray BEGIN -namespace { +namespace QtPrivate { class QWellArray : public QWidget { @@ -187,7 +170,7 @@ class QWellArray : public QWidget Q_PROPERTY(int selectedRow READ selectedRow) public: - QWellArray(int rows, int cols, QWidget* parent=0); + QWellArray(int rows, int cols, QWidget* parent=nullptr); ~QWellArray() {} QString cellContent(int row, int col) const; @@ -341,7 +324,8 @@ void QWellArray::paintCell(QPainter* p, int row, int col, const QRect &rect) const QPalette & g = palette(); QStyleOptionFrame opt; - int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + opt.initFrom(this); + int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt, this); opt.lineWidth = dfw; opt.midLineWidth = 1; opt.rect = rect.adjusted(b, b, -b, -b); @@ -378,7 +362,7 @@ void QWellArray::paintCellContents(QPainter *p, int row, int col, const QRect &r void QWellArray::mousePressEvent(QMouseEvent *e) { // The current cell marker is set to the cell the mouse is pressed in - QPoint pos = e->pos(); + QPoint pos = e->position().toPoint(); setCurrent(rowAt(pos.y()), columnAt(pos.x())); } @@ -458,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 @@ -492,14 +476,14 @@ void QWellArray::keyPressEvent(QKeyEvent* e) return; } -} +} // namespace QtPrivate //////////// QWellArray END // Event filter to be installed on the dialog while in color-picking mode. class QColorPickingEventFilter : public QObject { public: - explicit QColorPickingEventFilter(QColorDialogPrivate *dp, QObject *parent = 0) : QObject(parent), m_dp(dp) {} + explicit QColorPickingEventFilter(QColorDialogPrivate *dp, QObject *parent) : QObject(parent), m_dp(dp) {} bool eventFilter(QObject *, QEvent *event) override { @@ -582,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 { @@ -622,7 +606,7 @@ void QColorWell::mousePressEvent(QMouseEvent *e) oldCurrent = QPoint(selectedRow(), selectedColumn()); QWellArray::mousePressEvent(e); mousePressed = true; - pressPos = e->pos(); + pressPos = e->position().toPoint(); } void QColorWell::mouseMoveEvent(QMouseEvent *e) @@ -631,7 +615,7 @@ void QColorWell::mouseMoveEvent(QMouseEvent *e) #if QT_CONFIG(draganddrop) if (!mousePressed) return; - if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) { + if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) { setCurrent(oldCurrent.x(), oldCurrent.y()); int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows(); QColor col(values[i]); @@ -646,7 +630,7 @@ void QColorWell::mouseMoveEvent(QMouseEvent *e) drg->setMimeData(mime); drg->setPixmap(pix); mousePressed = false; - drg->start(); + drg->exec(Qt::CopyAction); } #endif } @@ -669,7 +653,7 @@ void QColorWell::dragLeaveEvent(QDragLeaveEvent *) void QColorWell::dragMoveEvent(QDragMoveEvent *e) { if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) { - setCurrent(rowAt(e->pos().y()), columnAt(e->pos().x())); + setCurrent(rowAt(e->position().toPoint().y()), columnAt(e->position().toPoint().x())); e->accept(); } else { e->ignore(); @@ -680,7 +664,7 @@ void QColorWell::dropEvent(QDropEvent *e) { QColor col = qvariant_cast<QColor>(e->mimeData()->colorData()); if (col.isValid()) { - int i = rowAt(e->pos().y()) + columnAt(e->pos().x()) * numRows(); + int i = rowAt(e->position().toPoint().y()) + columnAt(e->position().toPoint().x()) * numRows(); emit colorChanged(i, col.rgb()); e->accept(); } else { @@ -732,14 +716,18 @@ private: bool crossVisible; }; +} // namespace QtPrivate + static int pWidth = 220; static int pHeight = 200; +namespace QtPrivate { + class QColorLuminancePicker : public QWidget { Q_OBJECT public: - QColorLuminancePicker(QWidget* parent=0); + QColorLuminancePicker(QWidget* parent=nullptr); ~QColorLuminancePicker(); public slots: @@ -784,7 +772,7 @@ QColorLuminancePicker::QColorLuminancePicker(QWidget* parent) :QWidget(parent) { hue = 100; val = 100; sat = 100; - pix = 0; + pix = nullptr; // setAttribute(WA_NoErase, true); } @@ -795,11 +783,15 @@ QColorLuminancePicker::~QColorLuminancePicker() void QColorLuminancePicker::mouseMoveEvent(QMouseEvent *m) { - setVal(y2val(m->y())); + if (m->buttons() == Qt::NoButton) { + m->ignore(); + return; + } + setVal(y2val(m->position().toPoint().y())); } void QColorLuminancePicker::mousePressEvent(QMouseEvent *m) { - setVal(y2val(m->y())); + setVal(y2val(m->position().toPoint().y())); } void QColorLuminancePicker::setVal(int v) @@ -807,7 +799,7 @@ void QColorLuminancePicker::setVal(int v) if (val == v) return; val = qMax(0, qMin(v,255)); - delete pix; pix=0; + delete pix; pix=nullptr; repaint(); emit newHsv(hue, sat, val); } @@ -842,13 +834,12 @@ void QColorLuminancePicker::paintEvent(QPaintEvent *) p.drawPixmap(1, coff, *pix); const QPalette &g = palette(); qDrawShadePanel(&p, r, g, true); - p.setPen(g.foreground().color()); - p.setBrush(g.foreground()); - QPolygon a; - int y = val2y(val); - a.setPoints(3, w, y, w+5, y+5, w+5, y-5); + p.setPen(g.windowText().color()); + p.setBrush(g.windowText()); 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) @@ -856,7 +847,7 @@ void QColorLuminancePicker::setCol(int h, int s , int v) val = v; hue = h; sat = s; - delete pix; pix=0; + delete pix; pix=nullptr; repaint(); } @@ -928,14 +919,18 @@ void QColorPicker::setCol(int h, int s) void QColorPicker::mouseMoveEvent(QMouseEvent *m) { - QPoint p = m->pos() - contentsRect().topLeft(); + QPoint p = m->position().toPoint() - contentsRect().topLeft(); + if (m->buttons() == Qt::NoButton) { + m->ignore(); + return; + } setCol(p); emit newCol(hue, sat); } void QColorPicker::mousePressEvent(QMouseEvent *m) { - QPoint p = m->pos() - contentsRect().topLeft(); + QPoint p = m->position().toPoint() - contentsRect().topLeft(); setCol(p); emit newCol(hue, sat); } @@ -1109,7 +1104,7 @@ inline bool QColorShower::isAlphaVisible() const void QColorShowLabel::mousePressEvent(QMouseEvent *e) { mousePressed = true; - pressPos = e->pos(); + pressPos = e->position().toPoint(); } void QColorShowLabel::mouseMoveEvent(QMouseEvent *e) @@ -1119,7 +1114,7 @@ void QColorShowLabel::mouseMoveEvent(QMouseEvent *e) #else if (!mousePressed) return; - if ((pressPos - e->pos()).manhattanLength() > QApplication::startDragDistance()) { + if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) { QMimeData *mime = new QMimeData; mime->setColorData(col); QPixmap pix(30, 20); @@ -1131,7 +1126,7 @@ void QColorShowLabel::mouseMoveEvent(QMouseEvent *e) drg->setMimeData(mime); drg->setPixmap(pix); mousePressed = false; - drg->start(); + drg->exec(Qt::CopyAction); } #endif } @@ -1179,7 +1174,8 @@ QColorShower::QColorShower(QColorDialog *parent) curQColor = Qt::white; gl = new QGridLayout(this); - gl->setMargin(gl->spacing()); + const int s = gl->spacing(); + gl->setContentsMargins(s, s, s, s); lab = new QColorShowLabel(this); #ifdef QT_SMALL_COLORDIALOG @@ -1194,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); @@ -1299,18 +1295,15 @@ 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); -#elif !defined(QT_NO_REGEXP) - QRegExp regExp(QStringLiteral("#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})")); - QRegExpValidator *validator = new QRegExpValidator(regExp, this); - htEd->setValidator(validator); #else htEd->setReadOnly(true); #endif @@ -1325,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(); } @@ -1401,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); @@ -1489,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); @@ -1503,7 +1505,7 @@ void QColorDialogPrivate::setCurrentRgbColor(QRgb rgb) { if (!nativeDialogInUse) { cs->setRgb(rgb); - _q_newColorTypedIn(rgb); + newColorTypedIn(rgb); } } @@ -1536,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(); @@ -1552,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(); @@ -1564,14 +1566,18 @@ bool QColorDialogPrivate::selectColor(const QColor &col) QColor QColorDialogPrivate::grabScreenColor(const QPoint &p) { - const QDesktopWidget *desktop = QApplication::desktop(); - const QPixmap pixmap = QGuiApplication::primaryScreen()->grabWindow(desktop->winId(), p.x(), p.y(), 1, 1); - QImage i = pixmap.toImage(); + QScreen *screen = QGuiApplication::screenAt(p); + if (!screen) + screen = QGuiApplication::primaryScreen(); + 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; @@ -1581,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)); @@ -1594,18 +1600,32 @@ 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); + colorPickingEventFilter = new QColorPickingEventFilter(this, q); q->installEventFilter(colorPickingEventFilter); // If user pushes Escape, the last color before picking will be restored. beforeScreenColorPicking = cs->currentColor(); @@ -1615,7 +1635,7 @@ void QColorDialogPrivate::_q_pickScreenColor() q->grabMouse(); #endif -#ifdef Q_OS_WIN32 // excludes WinCE and WinRT +#ifdef Q_OS_WIN32 // On Windows mouse tracking doesn't work over other processes's windows updateTimer->start(30); @@ -1632,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())); } @@ -1658,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) @@ -1672,8 +1694,8 @@ void QColorDialogPrivate::init(const QColor &initial) q->setWindowTitle(QColorDialog::tr("Select Color")); // default: use the native dialog if possible. Can be overridden in setOptions() - nativeDialogInUse = (platformColorDialogHelper() != 0); - colorPickingEventFilter = 0; + nativeDialogInUse = (platformColorDialogHelper() != nullptr); + colorPickingEventFilter = nullptr; nextCust = 0; if (!nativeDialogInUse) @@ -1697,7 +1719,7 @@ void QColorDialogPrivate::initWidgets() QHBoxLayout *topLay = new QHBoxLayout(); mainLay->addLayout(topLay); - leftLay = 0; + leftLay = nullptr; #if defined(QT_SMALL_COLORDIALOG) smallDisplay = true; @@ -1705,7 +1727,7 @@ void QColorDialogPrivate::initWidgets() #else // small displays (e.g. PDAs) cannot fit the full color dialog, // so just use the color picker. - smallDisplay = (QDesktopWidgetPrivate::width() < 480 || QDesktopWidgetPrivate::height() < 350); + smallDisplay = (QGuiApplication::primaryScreen()->virtualGeometry().width() < 480 || QGuiApplication::primaryScreen()->virtualGeometry().height() < 350); const int lumSpace = topLay->spacing() / 2; #endif @@ -1718,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(); @@ -1736,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(); @@ -1753,22 +1781,22 @@ 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 #if defined(QT_SMALL_COLORDIALOG) - QSize screenSize = QDesktopWidgetPrivate::availableGeometry(QCursor::pos()).size(); + 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; pHeight = 100; #endif - custom = 0; - standard = 0; + custom = nullptr; + standard = nullptr; } QVBoxLayout *rightLay = new QVBoxLayout; @@ -1781,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(); @@ -1801,36 +1829,38 @@ 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->setMargin(cs->gl->margin()); - QObject::connect(cs, SIGNAL(newCol(QRgb)), q, SLOT(_q_newColorTypedIn(QRgb))); - QObject::connect(cs, SIGNAL(currentColorChanged(QColor)), - q, SIGNAL(currentColorChanged(QColor))); + pickLay->setContentsMargins(cs->gl->contentsMargins()); + 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 rightLay->addWidget(cs); if (leftLay) - leftLay->addSpacing(cs->gl->margin()); + leftLay->addSpacing(cs->gl->contentsMargins().right()); #endif buttons = new QDialogButtonBox(q); 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(); } @@ -1838,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 *) @@ -1848,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) @@ -1865,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, @@ -1884,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; @@ -1949,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); @@ -2055,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)); } } @@ -2074,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. @@ -2104,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); } /*! @@ -2166,41 +2220,12 @@ QColor QColorDialog::getColor(const QColor &initial, QWidget *parent, const QStr return dlg.selectedColor(); } -#if QT_DEPRECATED_SINCE(5, 12) -/*! - \obsolete - - Pops up a modal color dialog to allow the user to choose a color - and an alpha channel (transparency) value. The color+alpha is - initially set to \a initial. The dialog is a child of \a parent. - - If \a ok is non-null, \e {*ok} is set to true if the user clicked - \uicontrol{OK}, and to false if the user clicked Cancel. - - If the user clicks Cancel, the \a initial value is returned. - - Use QColorDialog::getColor() instead, passing the - QColorDialog::ShowAlphaChannel option. -*/ - -QRgb QColorDialog::getRgba(QRgb initial, bool *ok, QWidget *parent) -{ - const QColor color = getColor(QColor::fromRgba(initial), parent, QString(), - ShowAlphaChannel); - QRgb result = color.isValid() ? color.rgba() : initial; - if (ok) - *ok = color.isValid(); - return result; -} -#endif - /*! Destroys the color dialog. */ QColorDialog::~QColorDialog() { - } /*! @@ -2214,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); @@ -2240,21 +2265,20 @@ 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) { // If the cross is visible the grabbed color will be black most of the times - cp->setCrossVisible(!cp->geometry().contains(e->pos())); + cp->setCrossVisible(!cp->geometry().contains(e->position().toPoint())); - updateColorPicking(e->globalPos()); + updateColorPicking(e->globalPosition().toPoint()); return true; } bool QColorDialogPrivate::handleColorPickingMouseButtonRelease(QMouseEvent *e) { - setCurrentColor(grabScreenColor(e->globalPos()), SetColorAll); + setCurrentColor(grabScreenColor(e->globalPosition().toPoint()), SetColorAll); releaseColorPicking(); return true; } @@ -2296,7 +2320,7 @@ void QColorDialog::done(int result) if (d->receiverToDisconnectOnClose) { disconnect(this, SIGNAL(colorSelected(QColor)), d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose); - d->receiverToDisconnectOnClose = 0; + d->receiverToDisconnectOnClose = nullptr; } d->memberToDisconnectOnClose.clear(); } |