7#if QT_CONFIG(accessibility)
8#include "qaccessible.h"
14#if QT_CONFIG(draganddrop)
26#if QT_CONFIG(regularexpression)
27#include <qregularexpression.h>
29#if QT_CONFIG(settings)
44#include "private/qdialog_p.h"
45#include "private/qcolorwell_p.h"
47#include <qpa/qplatformintegration.h>
48#include <qpa/qplatformservices.h>
49#include <private/qguiapplication_p.h>
51#include <QtCore/qpointer.h>
57using namespace Qt::StringLiterals;
73 Q_DECLARE_PUBLIC(QColorDialog)
89 {
return static_cast<QPlatformColorDialogHelper *>(platformHelper()); }
91 void init(
const QColor &initial);
166void QWellArray::paintEvent(QPaintEvent *e)
173 int colfirst = columnAt(cx);
174 int collast = columnAt(cx + cw);
175 int rowfirst = rowAt(cy);
176 int rowlast = rowAt(cy + ch);
178 if (isRightToLeft()) {
184 QPainter painter(
this);
185 QPainter *p = &painter;
186 QRect rect(0, 0, cellWidth(), cellHeight());
189 if (collast < 0 || collast >= ncols)
191 if (rowlast < 0 || rowlast >= nrows)
195 for (
int r = rowfirst; r <= rowlast; ++r) {
202 for (
int c = colfirst; c <= collast; ++c) {
204 int colp = columnX(c);
206 rect.translate(colp, rowp);
207 paintCell(p, r, c, rect);
208 rect.translate(-colp, -rowp);
213QWellArray::QWellArray(
int rows,
int cols, QWidget *parent)
215 ,nrows(rows), ncols(cols)
217 setFocusPolicy(Qt::StrongFocus);
226QSize QWellArray::sizeHint()
const
229 return gridSize().boundedTo(QSize(640, 480));
233void QWellArray::paintCell(QPainter* p,
int row,
int col,
const QRect &rect)
237 const QPalette & g = palette();
238 QStyleOptionFrame opt;
240 int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt,
this);
242 opt.midLineWidth = 1;
243 opt.rect = rect.adjusted(b, b, -b, -b);
245 opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
246 style()->drawPrimitive(QStyle::PE_Frame, &opt, p,
this);
249 if ((row == curRow) && (col == curCol)) {
251 QStyleOptionFocusRect opt;
254 opt.state = QStyle::State_None | QStyle::State_KeyboardFocusChange;
255 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p,
this);
258 paintCellContents(p, row, col, opt.rect.adjusted(dfw, dfw, -dfw, -dfw));
262
263
264void QWellArray::paintCellContents(QPainter *p,
int row,
int col,
const QRect &r)
268 p->fillRect(r, Qt::white);
269 p->setPen(Qt::black);
270 p->drawLine(r.topLeft(), r.bottomRight());
271 p->drawLine(r.topRight(), r.bottomLeft());
274void QWellArray::mousePressEvent(QMouseEvent *e)
277 QPoint pos = e->position().toPoint();
278 setCurrent(rowAt(pos.y()), columnAt(pos.x()));
281void QWellArray::mouseReleaseEvent(QMouseEvent * )
284 setSelected(curRow, curCol);
289
290
291
293void QWellArray::setCurrent(
int row,
int col)
295 if ((curRow == row) && (curCol == col))
298 if (row < 0 || col < 0 || row >= nrows || col >= ncols)
307 updateCell(oldRow, oldCol);
308 updateCell(curRow, curCol);
310 emit currentChanged(curRow, curCol);
311 sendAccessibleChildFocusEvent();
315
316
317
318
319
320void QWellArray::setSelected(
int row,
int col)
325 if (row < 0 || col < 0)
331 updateCell(oldRow, oldCol);
332 updateCell(selRow, selCol);
334 emit selected(row, col);
337 if (isVisible() && qobject_cast<QMenu*>(parentWidget()))
338 parentWidget()->close();
342void QWellArray::focusInEvent(QFocusEvent*)
344 updateCell(curRow, curCol);
345 emit currentChanged(curRow, curCol);
346 sendAccessibleChildFocusEvent();
350void QWellArray::focusOutEvent(QFocusEvent*)
352 updateCell(curRow, curCol);
355void QWellArray::keyPressEvent(QKeyEvent* e)
360 setCurrent(curRow, curCol - 1);
363 if (curCol < numCols()-1)
364 setCurrent(curRow, curCol + 1);
368 setCurrent(curRow - 1, curCol);
371 if (curRow < numRows()-1)
372 setCurrent(curRow + 1, curCol);
375 setSelected(curRow, curCol);
383void QWellArray::sendAccessibleChildFocusEvent()
385#if QT_CONFIG(accessibility)
386 if (!QAccessible::isActive())
389 if (hasFocus() && curRow >= 0 && curCol >= 0) {
390 const int itemIndex = index(curRow, curCol);
391 QAccessibleEvent event(
this, QAccessible::Focus);
392 event.setChild(itemIndex);
393 QAccessible::updateAccessibility(&event);
409 switch (event->type()) {
410 case QEvent::MouseMove:
412 case QEvent::MouseButtonRelease:
414 case QEvent::KeyPress:
424 if (state != Qt::ApplicationActive)
435
436
437
438int QColorDialog::customCount()
440 return QColorDialogOptions::customColorCount();
444
445
446QColor QColorDialog::customColor(
int index)
448 return QColor(QColorDialogOptions::customColor(index));
452
453
454
455
456
457
458void QColorDialog::setCustomColor(
int index, QColor color)
460 QColorDialogOptions::setCustomColor(index, color.rgba());
464
465
466
467
468QColor QColorDialog::standardColor(
int index)
470 return QColor(QColorDialogOptions::standardColor(index));
474
475
476
477
478
479
480void QColorDialog::setStandardColor(
int index, QColor color)
482 QColorDialogOptions::setStandardColor(index, color.rgba());
485static inline void rgb2hsv(QRgb rgb,
int &h,
int &s,
int &v)
489 c.getHsv(&h, &s, &v);
492void QColorWell::paintCellContents(QPainter *p,
int row,
int col,
const QRect &r)
494 int i = row + col*numRows();
495 p->fillRect(r, QColor(values[i]));
498void QColorWell::mousePressEvent(QMouseEvent *e)
500 oldCurrent = QPoint(selectedRow(), selectedColumn());
501 QWellArray::mousePressEvent(e);
503 pressPos = e->position().toPoint();
506void QColorWell::mouseMoveEvent(QMouseEvent *e)
508 QWellArray::mouseMoveEvent(e);
509#if QT_CONFIG(draganddrop)
512 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
513 setCurrent(oldCurrent.x(), oldCurrent.y());
514 int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
515 QColor col(values[i]);
516 QMimeData *mime =
new QMimeData;
517 mime->setColorData(col);
518 QPixmap pix(cellWidth(), cellHeight());
521 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
523 QDrag *drg =
new QDrag(
this);
524 drg->setMimeData(mime);
526 mousePressed =
false;
527 drg->exec(Qt::CopyAction);
532#if QT_CONFIG(draganddrop)
533void QColorWell::dragEnterEvent(QDragEnterEvent *e)
535 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
541void QColorWell::dragLeaveEvent(QDragLeaveEvent *)
544 parentWidget()->setFocus();
547void QColorWell::dragMoveEvent(QDragMoveEvent *e)
549 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) {
550 setCurrent(rowAt(e->position().toPoint().y()), columnAt(e->position().toPoint().x()));
557void QColorWell::dropEvent(QDropEvent *e)
559 QColor col = qvariant_cast<QColor>(e->mimeData()->colorData());
561 int i = rowAt(e->position().toPoint().y()) + columnAt(e->position().toPoint().x()) * numRows();
562 emit colorChanged(i, col.rgb());
571void QColorWell::mouseReleaseEvent(QMouseEvent *e)
575 QWellArray::mouseReleaseEvent(e);
576 mousePressed =
false;
606 QPixmap createColorsPixmap();
607 QPoint colPt(
int hue,
int sat);
608 int huePt(
const QPoint &pt,
const QSize &widgetSize);
609 int huePt(
const QPoint &pt) {
return huePt(pt, size()); }
610 int satPt(
const QPoint &pt,
const QSize &widgetSize);
611 int satPt(
const QPoint &pt) {
return satPt(pt, size()); }
612 void setCol(
const QPoint &pt,
bool notify =
true);
646 enum { foff = 3, coff = 4 };
661 int d = height() - 2*coff - 1;
662 return 255 - (y - coff)*255/d;
667 int d = height() - 2*coff - 1;
668 return coff + (255-v)*d/255;
674 hue = 100; val = 100; sat = 100;
676 setFocusPolicy(Qt::StrongFocus);
685 switch (event->key()) {
687 setVal(
std::clamp(val - 1, 0, 255));
690 setVal(
std::clamp(val + 1, 0, 255));
693 QWidget::keyPressEvent(event);
700 if (m->buttons() == Qt::NoButton) {
704 setVal(y2val(m->position().toPoint().y()));
708 setVal(y2val(m->position().toPoint().y()));
715 val = qMax(0, qMin(v,255));
718 emit newHsv(hue, sat, val);
725 emit newHsv(h, s, val);
732 QRect r(0, foff, w, height() - 2*foff);
733 int wi = r.width() - 2;
734 int hi = r.height() - 2;
735 if (pix.isNull() || pix.height() != hi || pix.width() != wi) {
736 QImage img(wi, hi, QImage::Format_RGB32);
738 uint *pixel = (uint *) img.scanLine(0);
739 for (y = 0; y < hi; y++) {
740 uint *end = pixel + wi;
741 std::fill(pixel, end, QColor::fromHsv(hue, sat, y2val(y + coff)).rgb());
744 pix = QPixmap::fromImage(img);
747 p.drawPixmap(1, coff, pix);
748 const QPalette &g = palette();
749 qDrawShadePanel(&p, r, g,
true);
750 p.setPen(g.windowText().color());
751 p.setBrush(g.windowText());
752 p.eraseRect(w, 0, 5, height());
753 const int y = val2y(val);
754 const std::array<QPoint, 3> points = {QPoint(w, y), QPoint(w + 5, y + 5), QPoint(w + 5, y - 5)};
755 p.drawPolygon(points.data(),
static_cast<
int>(points.size()));
769 QRect r = contentsRect();
770 return QPoint((360 - hue) * (r.width() - 1) / 360, (255 - sat) * (r.height() - 1) / 255);
775 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
776 return std::clamp(360 - pt.x() * 360 / (r.width() - 1), 0, 359);
781 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
782 return std::clamp(255 - pt.y() * 255 / (r.height() - 1), 0, 255);
787 if (pt == m_pos || pix.isNull())
790 Q_ASSERT(pix.height());
791 Q_ASSERT(pix.width());
793 QRect r(m_pos, QSize(20, 20));
794 m_pos.setX(std::clamp(pt.x(), 0, pix.width() - 1));
795 m_pos.setY(std::clamp(pt.y(), 0, pix.height() - 1));
796 r = r.united(QRect(m_pos, QSize(20, 20)));
797 r.translate(contentsRect().x() - 9, contentsRect().y() - 9);
802 emit newCol(huePt(m_pos), satPt(m_pos));
809 setAttribute(Qt::WA_NoSystemBackground);
810 setFocusPolicy(Qt::StrongFocus);
811 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
814 pix = createColorsPixmap();
825 if (crossVisible != visible) {
826 crossVisible = visible;
833 return QSize(
pWidth + 2*frameWidth(),
pHeight + 2*frameWidth());
838 int nhue = qMin(qMax(0,h), 359);
839 int nsat = qMin(qMax(0,s), 255);
840 if (nhue == huePt(m_pos) && nsat == satPt(m_pos))
843 setCol(colPt(nhue, nsat),
false);
848 switch (event->key()) {
850 setCol(m_pos + QPoint(0, 1));
853 setCol(m_pos + QPoint(-1, 0));
856 setCol(m_pos + QPoint(1, 0));
859 setCol(m_pos + QPoint(0, -1));
862 QFrame::keyPressEvent(event);
869 QPoint p = m->position().toPoint() - contentsRect().topLeft();
870 if (m->buttons() == Qt::NoButton) {
879 QPoint p = m->position().toPoint() - contentsRect().topLeft();
887 QRect r = contentsRect();
889 p.drawPixmap(r.topLeft(), pix);
892 QPoint pt = m_pos + r.topLeft();
894 p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
895 p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
901 QFrame::resizeEvent(ev);
903 pix = createColorsPixmap();
905 const QSize &oldSize = ev->oldSize();
906 if (!oldSize.isValid())
911 const int hue = huePt(m_pos, oldSize);
912 const int sat = satPt(m_pos, oldSize);
918 int w = width() - frameWidth() * 2;
919 int h = height() - frameWidth() * 2;
920 QImage img(w, h, QImage::Format_RGB32);
922 uint *pixel = (uint *) img.scanLine(0);
923 for (y = 0; y < h; y++) {
924 const uint *end = pixel + w;
926 while (pixel < end) {
929 c.setHsv(huePt(p), satPt(p), 200);
935 return QPixmap::fromImage(img);
944 const QSignalBlocker blocker(
this);
945 QSpinBox::setValue(i);
961 {
return (colorDialog->options() & QColorDialog::ShowAlphaChannel) ? alphaEd->value() : 255; }
984 void showCurrentColor();
1006 QColorDialog *colorDialog;
1033#if QT_CONFIG(draganddrop)
1049 p.fillRect(contentsRect()&e->rect(), col);
1054 alphaLab->setVisible(b);
1055 alphaEd->setVisible(b);
1060 return alphaLab->isVisible();
1065 mousePressed =
true;
1066 pressPos = e->position().toPoint();
1071#if !QT_CONFIG(draganddrop)
1076 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
1077 QMimeData *mime =
new QMimeData;
1078 mime->setColorData(col);
1079 QPixmap pix(30, 20);
1082 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
1084 QDrag *drg =
new QDrag(
this);
1085 drg->setMimeData(mime);
1086 drg->setPixmap(pix);
1087 mousePressed =
false;
1088 drg->exec(Qt::CopyAction);
1093#if QT_CONFIG(draganddrop)
1124 mousePressed =
false;
1130 colorDialog = parent;
1132 curCol = qRgb(255, 255, 255);
1133 curQColor = Qt::white;
1135 gl =
new QGridLayout(
this);
1136 const int s = gl->spacing();
1137 gl->setContentsMargins(s, s, s, s);
1138 lab =
new QColorShowLabel(
this);
1140#ifdef QT_SMALL_COLORDIALOG
1141 lab->setMinimumHeight(60);
1143 lab->setMinimumWidth(60);
1147#if !defined(QT_SMALL_COLORDIALOG)
1148 gl->addWidget(lab, 0, 0, -1, 1);
1150 gl->addWidget(lab, 0, 0, 1, -1);
1152 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::newCol);
1153 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::setRgb);
1155 hEd =
new QColSpinBox(
this);
1156 hEd->setRange(0, 359);
1157 lblHue =
new QLabel(
this);
1158#ifndef QT_NO_SHORTCUT
1159 lblHue->setBuddy(hEd);
1161 lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1162#if !defined(QT_SMALL_COLORDIALOG)
1163 gl->addWidget(lblHue, 0, 1);
1164 gl->addWidget(hEd, 0, 2);
1166 gl->addWidget(lblHue, 1, 0);
1167 gl->addWidget(hEd, 2, 0);
1170 sEd =
new QColSpinBox(
this);
1171 lblSat =
new QLabel(
this);
1172#ifndef QT_NO_SHORTCUT
1173 lblSat->setBuddy(sEd);
1175 lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1176#if !defined(QT_SMALL_COLORDIALOG)
1177 gl->addWidget(lblSat, 1, 1);
1178 gl->addWidget(sEd, 1, 2);
1180 gl->addWidget(lblSat, 1, 1);
1181 gl->addWidget(sEd, 2, 1);
1184 vEd =
new QColSpinBox(
this);
1185 lblVal =
new QLabel(
this);
1186#ifndef QT_NO_SHORTCUT
1187 lblVal->setBuddy(vEd);
1189 lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1190#if !defined(QT_SMALL_COLORDIALOG)
1191 gl->addWidget(lblVal, 2, 1);
1192 gl->addWidget(vEd, 2, 2);
1194 gl->addWidget(lblVal, 1, 2);
1195 gl->addWidget(vEd, 2, 2);
1198 rEd =
new QColSpinBox(
this);
1199 lblRed =
new QLabel(
this);
1200#ifndef QT_NO_SHORTCUT
1201 lblRed->setBuddy(rEd);
1203 lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1204#if !defined(QT_SMALL_COLORDIALOG)
1205 gl->addWidget(lblRed, 0, 3);
1206 gl->addWidget(rEd, 0, 4);
1208 gl->addWidget(lblRed, 3, 0);
1209 gl->addWidget(rEd, 4, 0);
1212 gEd =
new QColSpinBox(
this);
1213 lblGreen =
new QLabel(
this);
1214#ifndef QT_NO_SHORTCUT
1215 lblGreen->setBuddy(gEd);
1217 lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1218#if !defined(QT_SMALL_COLORDIALOG)
1219 gl->addWidget(lblGreen, 1, 3);
1220 gl->addWidget(gEd, 1, 4);
1222 gl->addWidget(lblGreen, 3, 1);
1223 gl->addWidget(gEd, 4, 1);
1226 bEd =
new QColSpinBox(
this);
1227 lblBlue =
new QLabel(
this);
1228#ifndef QT_NO_SHORTCUT
1229 lblBlue->setBuddy(bEd);
1231 lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1232#if !defined(QT_SMALL_COLORDIALOG)
1233 gl->addWidget(lblBlue, 2, 3);
1234 gl->addWidget(bEd, 2, 4);
1236 gl->addWidget(lblBlue, 3, 2);
1237 gl->addWidget(bEd, 4, 2);
1240 alphaEd =
new QColSpinBox(
this);
1241 alphaLab =
new QLabel(
this);
1242#ifndef QT_NO_SHORTCUT
1243 alphaLab->setBuddy(alphaEd);
1245 alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1246#if !defined(QT_SMALL_COLORDIALOG)
1247 gl->addWidget(alphaLab, 3, 1, 1, 3);
1248 gl->addWidget(alphaEd, 3, 4);
1250 gl->addWidget(alphaLab, 1, 3, 3, 1);
1251 gl->addWidget(alphaEd, 4, 3);
1255 lblHtml =
new QLabel(
this);
1256 htEd =
new QLineEdit(
this);
1257 htEd->setObjectName(
"qt_colorname_lineedit");
1258#ifndef QT_NO_SHORTCUT
1259 lblHtml->setBuddy(htEd);
1262#if QT_CONFIG(regularexpression)
1263 QRegularExpression regExp(QStringLiteral(
"#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"));
1264 QRegularExpressionValidator *validator =
new QRegularExpressionValidator(regExp,
this);
1265 htEd->setValidator(validator);
1267 htEd->setReadOnly(
true);
1269 htEd->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
1271 lblHtml->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1272#if defined(QT_SMALL_COLORDIALOG)
1273 gl->addWidget(lblHtml, 5, 0);
1274 gl->addWidget(htEd, 5, 1, 1, 2);
1276 gl->addWidget(lblHtml, 5, 1);
1277 gl->addWidget(htEd, 5, 2, 1, 3);
1280 connect(hEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1281 connect(sEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1282 connect(vEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1284 connect(rEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1285 connect(gEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1286 connect(bEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1287 connect(alphaEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1288 connect(htEd, &QLineEdit::textEdited,
this, &
QColorShower::htmlEd);
1303 if (nativeDialogInUse)
1304 return platformColorDialogHelper()->currentColor();
1305 return cs->currentQColor();
1310 lab->setColor(currentColor());
1317 curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
1319 rgb2hsv(currentColor(), hue, sat, val);
1325 htEd->setText(QColor(curCol).name());
1328 emit newCol(currentColor());
1334 rgbOriginal =
false;
1340 c.setHsv(hue, sat, val);
1347 htEd->setText(c.name());
1350 emit newCol(currentColor());
1356 QString t = htEd->text();
1360 if (!t.startsWith(u"#")) {
1362 QSignalBlocker blocker(htEd);
1366 QColor c = QColor::fromString(t);
1370 curCol = qRgba(c.red(), c.green(), c.blue(), currentAlpha());
1371 rgb2hsv(curCol, hue, sat, val);
1382 emit newCol(currentColor());
1391 rgb2hsv(currentColor(), hue, sat, val);
1401 htEd->setText(QColor(rgb).name());
1409 if (h < -1 || (uint)s > 255 || (uint)v > 255)
1412 rgbOriginal =
false;
1413 hue = h; val = v; sat = s;
1415 c.setHsv(hue, sat, val);
1426 htEd->setText(c.name());
1434 lblHue->setText(QColorDialog::tr(
"Hu&e:"));
1435 lblSat->setText(QColorDialog::tr(
"&Sat:"));
1436 lblVal->setText(QColorDialog::tr(
"&Val:"));
1437 lblRed->setText(QColorDialog::tr(
"&Red:"));
1438 lblGreen->setText(QColorDialog::tr(
"&Green:"));
1439 lblBlue->setText(QColorDialog::tr(
"Bl&ue:"));
1440 alphaLab->setText(QColorDialog::tr(
"A&lpha channel:"));
1441 lblHtml->setText(QColorDialog::tr(
"&HTML:"));
1446 QColor oldQColor(curQColor);
1447 curQColor.setRgba(qRgba(qRed(curCol), qGreen(curCol), qBlue(curCol), currentAlpha()));
1448 if (curQColor != oldQColor)
1449 emit currentColorChanged(curQColor);
1455 if (!nativeDialogInUse) {
1465 if (!nativeDialogInUse) {
1467 newColorTypedIn(rgb);
1475 if (
cs->curQColor != color) {
1476 cs->curQColor = color;
1477 emit q->currentColorChanged(color);
1490 QRgb color = col.rgb();
1493 const QRgb *standardColors = QColorDialogOptions::standardColors();
1495 const QRgb *match =
std::find(standardColors, standardColorsEnd, color);
1496 if (match != standardColorsEnd) {
1497 const int index =
int(match - standardColors);
1501 standard->setCurrent(row, column);
1502 standard->setSelected(row, column);
1503 standard->setFocus();
1509 const QRgb *customColors = QColorDialogOptions::customColors();
1511 const QRgb *match =
std::find(customColors, customColorsEnd, color);
1512 if (match != customColorsEnd) {
1513 const int index =
int(match - customColors);
1517 custom->setCurrent(row, column);
1518 custom->setSelected(row, column);
1528 QScreen *screen = QGuiApplication::screenAt(p);
1530 screen = QGuiApplication::primaryScreen();
1531 const QRect screenRect = screen->geometry();
1532 const QPixmap pixmap =
1533 screen->grabWindow(0, p.x() - screenRect.x(), p.y() - screenRect.y(), 1, 1);
1534 const QImage i = pixmap.toImage();
1535 return i.pixel(0, 0);
1541 if (!nativeDialogInUse) {
1543 rgb2hsv(rgb, h, s, v);
1557 setCurrentRgbColor(QColorDialogOptions::customColor(i));
1559 standard->setSelected(-1,-1);
1564 setCurrentRgbColor(QColorDialogOptions::standardColor(r + c * 6));
1566 custom->setSelected(-1,-1);
1573 auto *platformServices = QGuiApplicationPrivate::platformIntegration()->services();
1574 if (platformServices && platformServices->hasCapability(QPlatformServices::Capability::ColorPicking)) {
1575 if (
auto *colorPicker = platformServices->colorPicker(q->windowHandle())) {
1576 q->connect(colorPicker, &QPlatformServiceColorPicker::colorPicked, q,
1577 [q, colorPicker](
const QColor &color) {
1578 colorPicker->deleteLater();
1579 q->setCurrentColor(color);
1581 colorPicker->pickColor();
1588 q->installEventFilter(colorPickingEventFilter);
1589 QObject::connect(
qApp, &QGuiApplication::applicationStateChanged,
1590 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1592 beforeScreenColorPicking = cs->currentColor();
1594 q->grabMouse(Qt::CrossCursor);
1601 updateTimer->start(30);
1606 dummyTransparentWindow.show();
1610
1611
1612 q->setMouseTracking(
true);
1615 buttons->setDisabled(
true);
1618 const QPoint globalPos = QCursor::pos();
1619 q->setCurrentColor(grabScreenColor(globalPos));
1626 if (!lblScreenColorInfo)
1630 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1631 .arg(QStringLiteral(
"-"))
1632 .arg(QStringLiteral(
"-")));
1634 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1636 .arg(globalPos.y()));
1644 q->removeEventFilter(colorPickingEventFilter);
1645 QObject::disconnect(
qApp, &QGuiApplication::applicationStateChanged,
1646 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1649 updateTimer->stop();
1650 dummyTransparentWindow.setVisible(
false);
1652 q->releaseKeyboard();
1653 q->setMouseTracking(
false);
1654 lblScreenColorInfo->setText(
"\n"_L1);
1656 buttons->setDisabled(
false);
1664 q->setSizeGripEnabled(
false);
1665 q->setWindowTitle(QColorDialog::tr(
"Select Color"));
1668 nativeDialogInUse = (platformColorDialogHelper() !=
nullptr);
1672 if (!nativeDialogInUse)
1676 dummyTransparentWindow.resize(1, 1);
1677 dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
1680 q->setCurrentColor(initial);
1686 QVBoxLayout *mainLay =
new QVBoxLayout(q);
1688 mainLay->setSizeConstraint(QLayout::SetFixedSize);
1690 QHBoxLayout *topLay =
new QHBoxLayout();
1691 mainLay->addLayout(topLay);
1695#if defined(QT_SMALL_COLORDIALOG)
1696 smallDisplay =
true;
1697 const int lumSpace = 20;
1701 smallDisplay = (QGuiApplication::primaryScreen()->virtualGeometry().width() < 480 || QGuiApplication::primaryScreen()->virtualGeometry().height() < 350);
1702 const int lumSpace = topLay->spacing() / 2;
1706 leftLay =
new QVBoxLayout;
1707 topLay->addLayout(leftLay);
1709 standard =
new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
1710 lblBasicColors =
new QLabel(q);
1711#ifndef QT_NO_SHORTCUT
1712 lblBasicColors->setBuddy(standard);
1714 QObjectPrivate::connect(standard, &QColorWell::selected,
1715 this, &QColorDialogPrivate::newStandard);
1716 leftLay->addWidget(lblBasicColors);
1717 leftLay->addWidget(standard);
1719#if !defined(QT_SMALL_COLORDIALOG)
1722 leftLay->addWidget(eyeDropperButton);
1723 lblScreenColorInfo =
new QLabel(
"\n"_L1);
1724 leftLay->addWidget(lblScreenColorInfo);
1725 QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked,
1726 this, &QColorDialogPrivate::pickScreenColor);
1729 lblScreenColorInfo =
nullptr;
1733 leftLay->addStretch();
1735 custom =
new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
1736 custom->setAcceptDrops(
true);
1738 QObjectPrivate::connect(custom, &QColorWell::selected,
this, &QColorDialogPrivate::newCustom);
1739 QObjectPrivate::connect(custom, &QColorWell::currentChanged,
this, &QColorDialogPrivate::nextCustom);
1741 QObject::connect(custom, &QWellArray::colorChanged, q, [
this] (
int index, QRgb color) {
1742 QColorDialogOptions::setCustomColor(index, color);
1747 lblCustomColors =
new QLabel(q);
1748#ifndef QT_NO_SHORTCUT
1749 lblCustomColors->setBuddy(custom);
1751 leftLay->addWidget(lblCustomColors);
1752 leftLay->addWidget(custom);
1755 QObjectPrivate::connect(addCusBt, &QPushButton::clicked,
this, &QColorDialogPrivate::addCustom);
1756 leftLay->addWidget(addCusBt);
1759#if defined(QT_SMALL_COLORDIALOG)
1760 QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size();
1761 pWidth = pHeight = qMin(screenSize.width(), screenSize.height());
1763 if (screenSize.height() > screenSize.width())
1773 QVBoxLayout *rightLay =
new QVBoxLayout;
1774 topLay->addLayout(rightLay);
1776 QHBoxLayout *pickLay =
new QHBoxLayout;
1777 rightLay->addLayout(pickLay);
1779 QVBoxLayout *cLay =
new QVBoxLayout;
1780 pickLay->addLayout(cLay);
1781 cp =
new QColorPicker(q);
1783 cp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
1785#if defined(QT_SMALL_COLORDIALOG)
1788 cLay->addSpacing(lumSpace);
1789 cLay->addWidget(
cp);
1791 cLay->addSpacing(lumSpace);
1793 lp =
new QColorLuminancePicker(q);
1794#if defined(QT_SMALL_COLORDIALOG)
1797 lp->setFixedWidth(20);
1798 pickLay->addSpacing(10);
1799 pickLay->addWidget(
lp);
1800 pickLay->addStretch();
1803 QObject::connect(cp, &QColorPicker::newCol, lp, qOverload<
int,
int>(&QColorLuminancePicker::setCol));
1804 QObjectPrivate::connect(lp, &QColorLuminancePicker::newHsv,
this, &QColorDialogPrivate::newHsv);
1806 rightLay->addStretch();
1808 cs =
new QColorShower(q);
1809 pickLay->setContentsMargins(
cs->gl->contentsMargins());
1810 QObjectPrivate::connect(cs, &QColorShower::newCol,
1811 this, &QColorDialogPrivate::newColorTypedIn);
1812 QObject::connect(cs, &QColorShower::currentColorChanged,
1813 q, &QColorDialog::currentColorChanged);
1814#if defined(QT_SMALL_COLORDIALOG)
1815 topLay->addWidget(cs);
1817 rightLay->addWidget(
cs);
1819 leftLay->addSpacing(cs->gl->contentsMargins().right());
1822 buttons =
new QDialogButtonBox(q);
1823 mainLay->addWidget(buttons);
1825 ok = buttons->addButton(QDialogButtonBox::Ok);
1826 QObject::connect(ok, &QPushButton::clicked, q, &QColorDialog::accept);
1827 ok->setDefault(
true);
1828 cancel = buttons->addButton(QDialogButtonBox::Cancel);
1829 QObject::connect(cancel, &QPushButton::clicked, q, &QColorDialog::reject);
1832 updateTimer =
new QTimer(q);
1833 QObjectPrivate::connect(updateTimer, &QTimer::timeout,
1834 this, qOverload<>(&QColorDialogPrivate::updateColorPicking));
1841 QColorDialog *d = q_func();
1842 auto *colorDialogHelper =
static_cast<QPlatformColorDialogHelper*>(h);
1843 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::currentColorChanged,
1844 d, &QColorDialog::currentColorChanged);
1845 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::colorSelected,
1846 d, &QColorDialog::colorSelected);
1847 colorDialogHelper->setOptions(options);
1852 options->setWindowTitle(q_func()->windowTitle());
1857 QColorDialogOptions::setCustomColor(nextCust, cs->currentColor());
1865 if (nativeDialogInUse)
1869 lblBasicColors->setText(QColorDialog::tr(
"&Basic colors"));
1870 lblCustomColors->setText(QColorDialog::tr(
"&Custom colors"));
1871 addCusBt->setText(QColorDialog::tr(
"&Add to Custom Colors"));
1872#if !defined(QT_SMALL_COLORDIALOG)
1883 const auto integration = QGuiApplicationPrivate::platformIntegration();
1884 return integration->hasCapability(QPlatformIntegration::ScreenWindowGrabbing)
1885 || integration->services()->hasCapability(QPlatformServices::Capability::ColorPicking);
1892 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
1893 if (nativeDialogInUse)
1895 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
1896 || q->testAttribute(Qt::WA_DontShowOnScreen)
1897 || (options->options() & QColorDialog::DontUseNativeDialog)) {
1901 return strcmp(QColorDialog::staticMetaObject.className(), q->metaObject()->className()) == 0;
1905 Qt::Dialog | Qt::WindowTitleHint
1906 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1948
1949
1950QColorDialog::QColorDialog(QWidget *parent)
1951 : QColorDialog(QColor(Qt::white), parent)
1956
1957
1958
1959QColorDialog::QColorDialog(
const QColor &initial, QWidget *parent)
1960 : QDialog(*
new QColorDialogPrivate, parent, qcd_DefaultWindowFlags)
1968 if (nativeDialogInUse) {
1969 platformColorDialogHelper()->setCurrentColor(color);
1973 if (setColorMode & ShowColor) {
1974 setCurrentRgbColor(color.rgb());
1977 if (setColorMode & SelectColor)
1982
1983
1984
1986void QColorDialog::setCurrentColor(
const QColor &color)
1989 d->setCurrentColor(color);
1992QColor QColorDialog::currentColor()
const
1994 Q_D(
const QColorDialog);
1995 return d->currentQColor();
1999
2000
2001
2002
2003
2004
2005
2006QColor QColorDialog::selectedColor()
const
2008 Q_D(
const QColorDialog);
2009 return d->selectedQColor;
2013
2014
2015
2016
2017
2018void QColorDialog::setOption(ColorDialogOption option,
bool on)
2021 d->optionsExplicitlySet =
true;
2023 const QColorDialog::ColorDialogOptions previousOptions = options();
2024 if (!(previousOptions & option) != !on)
2025 setOptions(previousOptions ^ option);
2029
2030
2031
2032
2033
2034bool QColorDialog::testOption(ColorDialogOption option)
const
2036 Q_D(
const QColorDialog);
2037 return d->options->testOption(
static_cast<QColorDialogOptions::ColorDialogOption>(option));
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052void QColorDialog::setOptions(ColorDialogOptions options)
2055 d->optionsExplicitlySet =
true;
2057 if (QColorDialog::options() == options)
2060 d->options->setOptions(QColorDialogOptions::ColorDialogOptions(
int(options)));
2061 if ((options & DontUseNativeDialog) && d->nativeDialogInUse) {
2062 d->nativeDialogInUse =
false;
2065 if (!d->nativeDialogInUse) {
2066 d->buttons->setVisible(!(options & NoButtons));
2067 d->showAlpha(options & ShowAlphaChannel);
2068 if (d->eyeDropperButton)
2069 d->eyeDropperButton->setVisible(!(options & NoEyeDropperButton));
2073QColorDialog::ColorDialogOptions QColorDialog::options()
const
2075 Q_D(
const QColorDialog);
2076 return QColorDialog::ColorDialogOptions(
int(d->options->options()));
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2095
2096
2097
2098
2099
2100
2101
2104
2105
2106
2107
2108
2109
2110
2113
2114
2115
2116void QColorDialog::setVisible(
bool visible)
2119 QDialog::setVisible(visible);
2123
2124
2125
2126
2127
2128
2133 const auto q =
static_cast<QDialog *>(q_ptr);
2136 selectedQColor = QColor();
2138 if (nativeDialogInUse) {
2139 if (setNativeDialogVisible(visible)) {
2142 q->setAttribute(Qt::WA_DontShowOnScreen);
2143 }
else if (visible) {
2147 q->setAttribute(Qt::WA_DontShowOnScreen,
false);
2150 QDialogPrivate::setVisible(visible);
2154
2155
2156
2157
2158
2159void QColorDialog::open(QObject *receiver,
const char *member)
2162 connect(
this, SIGNAL(colorSelected(QColor)), receiver, member);
2163 d->receiverToDisconnectOnClose = receiver;
2164 d->memberToDisconnectOnClose = member;
2169
2170
2171
2172
2173
2174
2175
2176QColor QColorDialog::getColor(
const QColor &initial, QWidget *parent,
const QString &title,
2177 ColorDialogOptions options)
2179 QAutoPointer<QColorDialog> dlg(
new QColorDialog(parent));
2180 if (!title.isEmpty())
2181 dlg->setWindowTitle(title);
2182 dlg->setOptions(options);
2183 dlg->setCurrentColor(initial);
2189 return dlg->selectedColor();
2195
2196
2198QColorDialog::~QColorDialog()
2203
2204
2205void QColorDialog::changeEvent(QEvent *e)
2208 if (e->type() == QEvent::LanguageChange)
2209 d->retranslateStrings();
2210 QDialog::changeEvent(e);
2214
2215
2216bool QColorDialog::event(QEvent *e)
2219 switch (e->type()) {
2220 case QEvent::Polish:
2221 if (!d->optionsExplicitlySet && metaObject() != &QColorDialog::staticMetaObject)
2222 setOption(QColorDialog::DontUseNativeDialog);
2227 return QDialog::event(e);
2234 static QPoint lastGlobalPos;
2235 QPoint newGlobalPos = QCursor::pos();
2236 if (lastGlobalPos == newGlobalPos)
2238 lastGlobalPos = newGlobalPos;
2240 if (!q->rect().contains(q->mapFromGlobal(newGlobalPos))) {
2241 updateColorPicking(newGlobalPos);
2243 dummyTransparentWindow.setPosition(newGlobalPos);
2251 const QColor color = grabScreenColor(globalPos);
2254 setCurrentColor(color, ShowColor);
2263 updateColorPicking(e->globalPosition().toPoint());
2269 setCurrentColor(grabScreenColor(e->globalPosition().toPoint()), SetColorAll);
2277#if QT_CONFIG(shortcut)
2278 if (e->matches(QKeySequence::Cancel)) {
2279 releaseColorPicking();
2280 q->setCurrentColor(beforeScreenColorPicking);
2283 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
2284 q->setCurrentColor(grabScreenColor(QCursor::pos()));
2292
2293
2294
2295
2296
2297
2298void QColorDialog::done(
int result)
2301 if (result == Accepted) {
2302 d->selectedQColor = d->currentQColor();
2303 emit colorSelected(d->selectedQColor);
2305 d->selectedQColor = QColor();
2307 QDialog::done(result);
2308 if (d->receiverToDisconnectOnClose) {
2309 disconnect(
this, SIGNAL(colorSelected(QColor)),
2310 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
2311 d->receiverToDisconnectOnClose =
nullptr;
2313 d->memberToDisconnectOnClose.clear();
2318#include "qcolordialog.moc"
2319#include "moc_qcolordialog.cpp"
void releaseColorPicking()
QColor grabScreenColor(const QPoint &p)
bool selectColor(const QColor &color)
void init(const QColor &initial)
void setCurrentAlpha(int a)
QColorPickingEventFilter * colorPickingEventFilter
bool handleColorPickingMouseButtonRelease(QMouseEvent *e)
QColor currentQColor() const
void updateColorPicking()
bool handleColorPickingMouseMove(QMouseEvent *e)
QLabel * lblScreenColorInfo
void _q_setCustom(int index, QRgb color)
QPushButton * eyeDropperButton
void updateColorLabelText(const QPoint &)
void setCurrentColor(const QColor &color, SetColorMode setColorMode=SetColorAll)
bool isAlphaVisible() const
void setCurrentQColor(const QColor &color)
bool canBeNativeDialog() const override
void setCurrentRgbColor(QRgb rgb)
void retranslateStrings()
QRgb currentColor() const
bool optionsExplicitlySet
void newColorTypedIn(QRgb rgb)
bool handleColorPickingKeyPress(QKeyEvent *e)
void updateColorPicking(const QPoint &pos)
QSharedPointer< QColorDialogOptions > options
void newStandard(int, int)
void setVisible(bool visible) override
QDialogButtonBox * buttons
bool supportsColorPicking() const
QPointer< QObject > receiverToDisconnectOnClose
QRgb beforeScreenColorPicking
virtual void initHelper(QPlatformDialogHelper *h) override
QByteArray memberToDisconnectOnClose
void newHsv(int h, int s, int v)
virtual void helperPrepareShow(QPlatformDialogHelper *h) override
QColorLuminancePicker * lp
QPlatformColorDialogHelper * platformColorDialogHelper() const
void nextCustom(int, int)
\inmodule QtCore\reentrant
QColSpinBox(QWidget *parent)
void paintEvent(QPaintEvent *) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
void keyPressEvent(QKeyEvent *event) override
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
void mouseMoveEvent(QMouseEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events ...
void setCol(int h, int s)
void mousePressEvent(QMouseEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
void paintEvent(QPaintEvent *) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
void mouseMoveEvent(QMouseEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events ...
QSize sizeHint() const override
void setCrossVisible(bool visible)
void resizeEvent(QResizeEvent *) override
This event handler can be reimplemented in a subclass to receive widget resize events which are passe...
void mousePressEvent(QMouseEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
void keyPressEvent(QKeyEvent *event) override
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
bool eventFilter(QObject *, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
void applicationStateChanged(Qt::ApplicationState state)
QColorPickingEventFilter(QColorDialogPrivate *dp, QObject *parent)
void mouseReleaseEvent(QMouseEvent *e) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse release even...
void mousePressEvent(QMouseEvent *e) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
void mouseMoveEvent(QMouseEvent *e) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse move events ...
void paintEvent(QPaintEvent *) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
void retranslateStrings()
QColor currentQColor() const
void setCurrentAlpha(int a)
bool isAlphaVisible() const
void setHsv(int h, int s, int v)
QRgb currentColor() const
void currentColorChanged(const QColor &color)
friend class QT_PREPEND_NAMESPACE(QUntypedBindable)
QtPrivate::QColorShower QColorShower
static void rgb2hsv(QRgb rgb, int &h, int &s, int &v)
QtPrivate::QColorPickingEventFilter QColorPickingEventFilter
QtPrivate::QColorLuminancePicker QColorLuminancePicker
QtPrivate::QColorPicker QColorPicker
static const Qt::WindowFlags qcd_DefaultWindowFlags