diff options
Diffstat (limited to 'src/widgets/dialogs/qcolordialog.cpp')
-rw-r--r-- | src/widgets/dialogs/qcolordialog.cpp | 160 |
1 files changed, 93 insertions, 67 deletions
diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp index 1bb65e6c24..22efecedc9 100644 --- a/src/widgets/dialogs/qcolordialog.cpp +++ b/src/widgets/dialogs/qcolordialog.cpp @@ -43,6 +43,8 @@ #include <qpa/qplatformservices.h> #include <private/qguiapplication_p.h> +#include <QtCore/qpointer.h> + #include <algorithm> QT_BEGIN_NAMESPACE @@ -102,16 +104,16 @@ public: 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(); @@ -120,6 +122,7 @@ public: bool handleColorPickingKeyPress(QKeyEvent *e); bool canBeNativeDialog() const override; + void setVisible(bool visible) override; QWellArray *custom; QWellArray *standard; @@ -135,7 +138,7 @@ public: QPushButton *ok; QPushButton *cancel; QPushButton *addCusBt; - QPushButton *screenColorPickerButton; + QPushButton *eyeDropperButton = nullptr; QColor selectedQColor; int nextCust; bool smallDisplay; @@ -322,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); @@ -780,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) @@ -829,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) @@ -914,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); } @@ -1316,7 +1326,7 @@ QColorShower::QColorShower(QColorDialog *parent) 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); + connect(htEd, &QLineEdit::textEdited, this, &QColorShower::htmlEd); retranslateStrings(); } @@ -1388,8 +1398,8 @@ void QColorShower::htmlEd() if (t.isEmpty()) return; - if (!t.startsWith(QStringLiteral("#"))) { - t = QStringLiteral("#") + t; + if (!t.startsWith(u"#")) { + t.prepend(u"#"); QSignalBlocker blocker(htEd); htEd->setText(t); } @@ -1481,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); @@ -1495,7 +1505,7 @@ void QColorDialogPrivate::setCurrentRgbColor(QRgb rgb) { if (!nativeDialogInUse) { cs->setRgb(rgb); - _q_newColorTypedIn(rgb); + newColorTypedIn(rgb); } } @@ -1528,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(); @@ -1544,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(); @@ -1567,7 +1577,7 @@ QColor QColorDialogPrivate::grabScreenColor(const QPoint &p) } //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; @@ -1577,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)); @@ -1590,14 +1600,14 @@ 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); @@ -1642,8 +1652,8 @@ void QColorDialogPrivate::_q_pickScreenColor() addCusBt->setDisabled(true); buttons->setDisabled(true); - if (screenColorPickerButton) { - screenColorPickerButton->setDisabled(true); + if (eyeDropperButton) { + eyeDropperButton->setDisabled(true); const QPoint globalPos = QCursor::pos(); q->setCurrentColor(grabScreenColor(globalPos)); updateColorLabelText(globalPos); @@ -1673,7 +1683,7 @@ void QColorDialogPrivate::releaseColorPicking() lblScreenColorInfo->setText("\n"_L1); addCusBt->setDisabled(false); buttons->setDisabled(false); - screenColorPickerButton->setDisabled(false); + eyeDropperButton->setDisabled(false); } void QColorDialogPrivate::init(const QColor &initial) @@ -1730,19 +1740,21 @@ 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) if (supportsColorPicking()) { - screenColorPickerButton = new QPushButton(); - leftLay->addWidget(screenColorPickerButton); + eyeDropperButton = new QPushButton(); + leftLay->addWidget(eyeDropperButton); lblScreenColorInfo = new QLabel("\n"_L1); leftLay->addWidget(lblScreenColorInfo); - q->connect(screenColorPickerButton, SIGNAL(clicked()), SLOT(_q_pickScreenColor())); + QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked, + this, &QColorDialogPrivate::pickScreenColor); } else { - screenColorPickerButton = nullptr; + eyeDropperButton = nullptr; lblScreenColorInfo = nullptr; } #endif @@ -1752,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(); @@ -1769,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 @@ -1817,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 @@ -1839,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(); } @@ -1854,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 *) @@ -1864,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) @@ -1882,8 +1899,8 @@ void QColorDialogPrivate::retranslateStrings() lblCustomColors->setText(QColorDialog::tr("&Custom colors")); addCusBt->setText(QColorDialog::tr("&Add to Custom Colors")); #if !defined(QT_SMALL_COLORDIALOG) - if (screenColorPickerButton) - screenColorPickerButton->setText(QColorDialog::tr("&Pick Screen Color")); + if (eyeDropperButton) + eyeDropperButton->setText(QColorDialog::tr("&Pick Screen Color")); #endif } @@ -2079,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)); } } @@ -2098,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. @@ -2128,30 +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) { - if (d->setNativeDialogVisible(visible)) { + 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: - setAttribute(Qt::WA_DontShowOnScreen); + q->setAttribute(Qt::WA_DontShowOnScreen); } else { - d->initWidgets(); + initWidgets(); } } else { - setAttribute(Qt::WA_DontShowOnScreen, false); + q->setAttribute(Qt::WA_DontShowOnScreen, false); } - QDialog::setVisible(visible); + QDialogPrivate::setVisible(visible); } /*! @@ -2199,7 +2226,6 @@ QColor QColorDialog::getColor(const QColor &initial, QWidget *parent, const QStr QColorDialog::~QColorDialog() { - } /*! @@ -2213,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); |