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
438
439
445 cd->installEventFilter(
this);
450 if (event->type() == QEvent::Polish ) {
451 Q_ASSERT(qobject_cast<QColorDialog *>(obj));
452 auto *cd =
static_cast<QColorDialog *>(obj);
453 auto *d =
static_cast<QColorDialogPrivate *>(QObjectPrivate::get(cd));
455 if (cd->metaObject() != &QColorDialog::staticMetaObject && !d->optionsExplicitlySet)
456 cd->setOption(QColorDialog::DontUseNativeDialog);
465
466
467
468int QColorDialog::customCount()
470 return QColorDialogOptions::customColorCount();
474
475
476QColor QColorDialog::customColor(
int index)
478 return QColor(QColorDialogOptions::customColor(index));
482
483
484
485
486
487
488void QColorDialog::setCustomColor(
int index, QColor color)
490 QColorDialogOptions::setCustomColor(index, color.rgba());
494
495
496
497
498QColor QColorDialog::standardColor(
int index)
500 return QColor(QColorDialogOptions::standardColor(index));
504
505
506
507
508
509
510void QColorDialog::setStandardColor(
int index, QColor color)
512 QColorDialogOptions::setStandardColor(index, color.rgba());
515static inline void rgb2hsv(QRgb rgb,
int &h,
int &s,
int &v)
519 c.getHsv(&h, &s, &v);
522void QColorWell::paintCellContents(QPainter *p,
int row,
int col,
const QRect &r)
524 int i = row + col*numRows();
525 p->fillRect(r, QColor(values[i]));
528void QColorWell::mousePressEvent(QMouseEvent *e)
530 oldCurrent = QPoint(selectedRow(), selectedColumn());
531 QWellArray::mousePressEvent(e);
533 pressPos = e->position().toPoint();
536void QColorWell::mouseMoveEvent(QMouseEvent *e)
538 QWellArray::mouseMoveEvent(e);
539#if QT_CONFIG(draganddrop)
542 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
543 setCurrent(oldCurrent.x(), oldCurrent.y());
544 int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
545 QColor col(values[i]);
546 QMimeData *mime =
new QMimeData;
547 mime->setColorData(col);
548 QPixmap pix(cellWidth(), cellHeight());
551 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
553 QDrag *drg =
new QDrag(
this);
554 drg->setMimeData(mime);
556 mousePressed =
false;
557 drg->exec(Qt::CopyAction);
562#if QT_CONFIG(draganddrop)
563void QColorWell::dragEnterEvent(QDragEnterEvent *e)
565 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
571void QColorWell::dragLeaveEvent(QDragLeaveEvent *)
574 parentWidget()->setFocus();
577void QColorWell::dragMoveEvent(QDragMoveEvent *e)
579 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) {
580 setCurrent(rowAt(e->position().toPoint().y()), columnAt(e->position().toPoint().x()));
587void QColorWell::dropEvent(QDropEvent *e)
589 QColor col = qvariant_cast<QColor>(e->mimeData()->colorData());
591 int i = rowAt(e->position().toPoint().y()) + columnAt(e->position().toPoint().x()) * numRows();
592 emit colorChanged(i, col.rgb());
601void QColorWell::mouseReleaseEvent(QMouseEvent *e)
605 QWellArray::mouseReleaseEvent(e);
606 mousePressed =
false;
636 QPixmap createColorsPixmap();
637 QPoint colPt(
int hue,
int sat);
638 int huePt(
const QPoint &pt,
const QSize &widgetSize);
639 int huePt(
const QPoint &pt) {
return huePt(pt, size()); }
640 int satPt(
const QPoint &pt,
const QSize &widgetSize);
641 int satPt(
const QPoint &pt) {
return satPt(pt, size()); }
642 void setCol(
const QPoint &pt,
bool notify =
true);
676 enum { foff = 3, coff = 4 };
691 int d = height() - 2*coff - 1;
692 return 255 - (y - coff)*255/d;
697 int d = height() - 2*coff - 1;
698 return coff + (255-v)*d/255;
704 hue = 100; val = 100; sat = 100;
706 setFocusPolicy(Qt::StrongFocus);
715 switch (event->key()) {
717 setVal(
std::clamp(val - 1, 0, 255));
720 setVal(
std::clamp(val + 1, 0, 255));
723 QWidget::keyPressEvent(event);
730 if (m->buttons() == Qt::NoButton) {
734 setVal(y2val(m->position().toPoint().y()));
738 setVal(y2val(m->position().toPoint().y()));
745 val = qMax(0, qMin(v,255));
748 emit newHsv(hue, sat, val);
755 emit newHsv(h, s, val);
762 QRect r(0, foff, w, height() - 2*foff);
763 int wi = r.width() - 2;
764 int hi = r.height() - 2;
765 if (pix.isNull() || pix.height() != hi || pix.width() != wi) {
766 QImage img(wi, hi, QImage::Format_RGB32);
768 uint *pixel = (uint *) img.scanLine(0);
769 for (y = 0; y < hi; y++) {
770 uint *end = pixel + wi;
771 std::fill(pixel, end, QColor::fromHsv(hue, sat, y2val(y + coff)).rgb());
774 pix = QPixmap::fromImage(img);
777 p.drawPixmap(1, coff, pix);
778 const QPalette &g = palette();
779 qDrawShadePanel(&p, r, g,
true);
780 p.setPen(g.windowText().color());
781 p.setBrush(g.windowText());
782 p.eraseRect(w, 0, 5, height());
783 const int y = val2y(val);
784 const std::array<QPoint, 3> points = {QPoint(w, y), QPoint(w + 5, y + 5), QPoint(w + 5, y - 5)};
785 p.drawPolygon(points.data(),
static_cast<
int>(points.size()));
799 QRect r = contentsRect();
800 return QPoint((360 - hue) * (r.width() - 1) / 360, (255 - sat) * (r.height() - 1) / 255);
805 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
806 return std::clamp(360 - pt.x() * 360 / (r.width() - 1), 0, 359);
811 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
812 return std::clamp(255 - pt.y() * 255 / (r.height() - 1), 0, 255);
817 if (pt == m_pos || pix.isNull())
820 Q_ASSERT(pix.height());
821 Q_ASSERT(pix.width());
823 QRect r(m_pos, QSize(20, 20));
824 m_pos.setX(std::clamp(pt.x(), 0, pix.width() - 1));
825 m_pos.setY(std::clamp(pt.y(), 0, pix.height() - 1));
826 r = r.united(QRect(m_pos, QSize(20, 20)));
827 r.translate(contentsRect().x() - 9, contentsRect().y() - 9);
832 emit newCol(huePt(m_pos), satPt(m_pos));
839 setAttribute(Qt::WA_NoSystemBackground);
840 setFocusPolicy(Qt::StrongFocus);
841 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
844 pix = createColorsPixmap();
855 if (crossVisible != visible) {
856 crossVisible = visible;
863 return QSize(
pWidth + 2*frameWidth(),
pHeight + 2*frameWidth());
868 int nhue = qMin(qMax(0,h), 359);
869 int nsat = qMin(qMax(0,s), 255);
870 if (nhue == huePt(m_pos) && nsat == satPt(m_pos))
873 setCol(colPt(nhue, nsat),
false);
878 switch (event->key()) {
880 setCol(m_pos + QPoint(0, 1));
883 setCol(m_pos + QPoint(-1, 0));
886 setCol(m_pos + QPoint(1, 0));
889 setCol(m_pos + QPoint(0, -1));
892 QFrame::keyPressEvent(event);
899 QPoint p = m->position().toPoint() - contentsRect().topLeft();
900 if (m->buttons() == Qt::NoButton) {
909 QPoint p = m->position().toPoint() - contentsRect().topLeft();
917 QRect r = contentsRect();
919 p.drawPixmap(r.topLeft(), pix);
922 QPoint pt = m_pos + r.topLeft();
924 p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
925 p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
931 QFrame::resizeEvent(ev);
933 pix = createColorsPixmap();
935 const QSize &oldSize = ev->oldSize();
936 if (!oldSize.isValid())
941 const int hue = huePt(m_pos, oldSize);
942 const int sat = satPt(m_pos, oldSize);
948 int w = width() - frameWidth() * 2;
949 int h = height() - frameWidth() * 2;
950 QImage img(w, h, QImage::Format_RGB32);
952 uint *pixel = (uint *) img.scanLine(0);
953 for (y = 0; y < h; y++) {
954 const uint *end = pixel + w;
956 while (pixel < end) {
959 c.setHsv(huePt(p), satPt(p), 200);
965 return QPixmap::fromImage(img);
974 const QSignalBlocker blocker(
this);
975 QSpinBox::setValue(i);
991 {
return (colorDialog->options() & QColorDialog::ShowAlphaChannel) ? alphaEd->value() : 255; }
1014 void showCurrentColor();
1036 QColorDialog *colorDialog;
1063#if QT_CONFIG(draganddrop)
1079 p.fillRect(contentsRect()&e->rect(), col);
1084 alphaLab->setVisible(b);
1085 alphaEd->setVisible(b);
1090 return alphaLab->isVisible();
1095 mousePressed =
true;
1096 pressPos = e->position().toPoint();
1101#if !QT_CONFIG(draganddrop)
1106 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
1107 QMimeData *mime =
new QMimeData;
1108 mime->setColorData(col);
1109 QPixmap pix(30, 20);
1112 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
1114 QDrag *drg =
new QDrag(
this);
1115 drg->setMimeData(mime);
1116 drg->setPixmap(pix);
1117 mousePressed =
false;
1118 drg->exec(Qt::CopyAction);
1123#if QT_CONFIG(draganddrop)
1154 mousePressed =
false;
1160 colorDialog = parent;
1162 curCol = qRgb(255, 255, 255);
1163 curQColor = Qt::white;
1165 gl =
new QGridLayout(
this);
1166 const int s = gl->spacing();
1167 gl->setContentsMargins(s, s, s, s);
1168 lab =
new QColorShowLabel(
this);
1170#ifdef QT_SMALL_COLORDIALOG
1171 lab->setMinimumHeight(60);
1173 lab->setMinimumWidth(60);
1177#if !defined(QT_SMALL_COLORDIALOG)
1178 gl->addWidget(lab, 0, 0, -1, 1);
1180 gl->addWidget(lab, 0, 0, 1, -1);
1182 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::newCol);
1183 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::setRgb);
1185 hEd =
new QColSpinBox(
this);
1186 hEd->setRange(0, 359);
1187 lblHue =
new QLabel(
this);
1188#ifndef QT_NO_SHORTCUT
1189 lblHue->setBuddy(hEd);
1191 lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1192#if !defined(QT_SMALL_COLORDIALOG)
1193 gl->addWidget(lblHue, 0, 1);
1194 gl->addWidget(hEd, 0, 2);
1196 gl->addWidget(lblHue, 1, 0);
1197 gl->addWidget(hEd, 2, 0);
1200 sEd =
new QColSpinBox(
this);
1201 lblSat =
new QLabel(
this);
1202#ifndef QT_NO_SHORTCUT
1203 lblSat->setBuddy(sEd);
1205 lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1206#if !defined(QT_SMALL_COLORDIALOG)
1207 gl->addWidget(lblSat, 1, 1);
1208 gl->addWidget(sEd, 1, 2);
1210 gl->addWidget(lblSat, 1, 1);
1211 gl->addWidget(sEd, 2, 1);
1214 vEd =
new QColSpinBox(
this);
1215 lblVal =
new QLabel(
this);
1216#ifndef QT_NO_SHORTCUT
1217 lblVal->setBuddy(vEd);
1219 lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1220#if !defined(QT_SMALL_COLORDIALOG)
1221 gl->addWidget(lblVal, 2, 1);
1222 gl->addWidget(vEd, 2, 2);
1224 gl->addWidget(lblVal, 1, 2);
1225 gl->addWidget(vEd, 2, 2);
1228 rEd =
new QColSpinBox(
this);
1229 lblRed =
new QLabel(
this);
1230#ifndef QT_NO_SHORTCUT
1231 lblRed->setBuddy(rEd);
1233 lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1234#if !defined(QT_SMALL_COLORDIALOG)
1235 gl->addWidget(lblRed, 0, 3);
1236 gl->addWidget(rEd, 0, 4);
1238 gl->addWidget(lblRed, 3, 0);
1239 gl->addWidget(rEd, 4, 0);
1242 gEd =
new QColSpinBox(
this);
1243 lblGreen =
new QLabel(
this);
1244#ifndef QT_NO_SHORTCUT
1245 lblGreen->setBuddy(gEd);
1247 lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1248#if !defined(QT_SMALL_COLORDIALOG)
1249 gl->addWidget(lblGreen, 1, 3);
1250 gl->addWidget(gEd, 1, 4);
1252 gl->addWidget(lblGreen, 3, 1);
1253 gl->addWidget(gEd, 4, 1);
1256 bEd =
new QColSpinBox(
this);
1257 lblBlue =
new QLabel(
this);
1258#ifndef QT_NO_SHORTCUT
1259 lblBlue->setBuddy(bEd);
1261 lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1262#if !defined(QT_SMALL_COLORDIALOG)
1263 gl->addWidget(lblBlue, 2, 3);
1264 gl->addWidget(bEd, 2, 4);
1266 gl->addWidget(lblBlue, 3, 2);
1267 gl->addWidget(bEd, 4, 2);
1270 alphaEd =
new QColSpinBox(
this);
1271 alphaLab =
new QLabel(
this);
1272#ifndef QT_NO_SHORTCUT
1273 alphaLab->setBuddy(alphaEd);
1275 alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1276#if !defined(QT_SMALL_COLORDIALOG)
1277 gl->addWidget(alphaLab, 3, 1, 1, 3);
1278 gl->addWidget(alphaEd, 3, 4);
1280 gl->addWidget(alphaLab, 1, 3, 3, 1);
1281 gl->addWidget(alphaEd, 4, 3);
1285 lblHtml =
new QLabel(
this);
1286 htEd =
new QLineEdit(
this);
1287 htEd->setObjectName(
"qt_colorname_lineedit");
1288#ifndef QT_NO_SHORTCUT
1289 lblHtml->setBuddy(htEd);
1292#if QT_CONFIG(regularexpression)
1293 QRegularExpression regExp(QStringLiteral(
"#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"));
1294 QRegularExpressionValidator *validator =
new QRegularExpressionValidator(regExp,
this);
1295 htEd->setValidator(validator);
1297 htEd->setReadOnly(
true);
1299 htEd->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
1301 lblHtml->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1302#if defined(QT_SMALL_COLORDIALOG)
1303 gl->addWidget(lblHtml, 5, 0);
1304 gl->addWidget(htEd, 5, 1, 1, 2);
1306 gl->addWidget(lblHtml, 5, 1);
1307 gl->addWidget(htEd, 5, 2, 1, 3);
1310 connect(hEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1311 connect(sEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1312 connect(vEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1314 connect(rEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1315 connect(gEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1316 connect(bEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1317 connect(alphaEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1318 connect(htEd, &QLineEdit::textEdited,
this, &
QColorShower::htmlEd);
1333 if (nativeDialogInUse)
1334 return platformColorDialogHelper()->currentColor();
1335 return cs->currentQColor();
1340 lab->setColor(currentColor());
1347 curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
1349 rgb2hsv(currentColor(), hue, sat, val);
1355 htEd->setText(QColor(curCol).name());
1358 emit newCol(currentColor());
1364 rgbOriginal =
false;
1370 c.setHsv(hue, sat, val);
1377 htEd->setText(c.name());
1380 emit newCol(currentColor());
1386 QString t = htEd->text();
1390 if (!t.startsWith(u"#")) {
1392 QSignalBlocker blocker(htEd);
1396 QColor c = QColor::fromString(t);
1400 curCol = qRgba(c.red(), c.green(), c.blue(), currentAlpha());
1401 rgb2hsv(curCol, hue, sat, val);
1412 emit newCol(currentColor());
1421 rgb2hsv(currentColor(), hue, sat, val);
1431 htEd->setText(QColor(rgb).name());
1439 if (h < -1 || (uint)s > 255 || (uint)v > 255)
1442 rgbOriginal =
false;
1443 hue = h; val = v; sat = s;
1445 c.setHsv(hue, sat, val);
1456 htEd->setText(c.name());
1464 lblHue->setText(QColorDialog::tr(
"Hu&e:"));
1465 lblSat->setText(QColorDialog::tr(
"&Sat:"));
1466 lblVal->setText(QColorDialog::tr(
"&Val:"));
1467 lblRed->setText(QColorDialog::tr(
"&Red:"));
1468 lblGreen->setText(QColorDialog::tr(
"&Green:"));
1469 lblBlue->setText(QColorDialog::tr(
"Bl&ue:"));
1470 alphaLab->setText(QColorDialog::tr(
"A&lpha channel:"));
1471 lblHtml->setText(QColorDialog::tr(
"&HTML:"));
1476 QColor oldQColor(curQColor);
1477 curQColor.setRgba(qRgba(qRed(curCol), qGreen(curCol), qBlue(curCol), currentAlpha()));
1478 if (curQColor != oldQColor)
1479 emit currentColorChanged(curQColor);
1485 if (!nativeDialogInUse) {
1495 if (!nativeDialogInUse) {
1497 newColorTypedIn(rgb);
1505 if (
cs->curQColor != color) {
1506 cs->curQColor = color;
1507 emit q->currentColorChanged(color);
1520 QRgb color = col.rgb();
1523 const QRgb *standardColors = QColorDialogOptions::standardColors();
1525 const QRgb *match =
std::find(standardColors, standardColorsEnd, color);
1526 if (match != standardColorsEnd) {
1527 const int index =
int(match - standardColors);
1531 standard->setCurrent(row, column);
1532 standard->setSelected(row, column);
1533 standard->setFocus();
1539 const QRgb *customColors = QColorDialogOptions::customColors();
1541 const QRgb *match =
std::find(customColors, customColorsEnd, color);
1542 if (match != customColorsEnd) {
1543 const int index =
int(match - customColors);
1547 custom->setCurrent(row, column);
1548 custom->setSelected(row, column);
1558 QScreen *screen = QGuiApplication::screenAt(p);
1560 screen = QGuiApplication::primaryScreen();
1561 const QRect screenRect = screen->geometry();
1562 const QPixmap pixmap =
1563 screen->grabWindow(0, p.x() - screenRect.x(), p.y() - screenRect.y(), 1, 1);
1564 const QImage i = pixmap.toImage();
1565 return i.pixel(0, 0);
1571 if (!nativeDialogInUse) {
1573 rgb2hsv(rgb, h, s, v);
1587 setCurrentRgbColor(QColorDialogOptions::customColor(i));
1589 standard->setSelected(-1,-1);
1594 setCurrentRgbColor(QColorDialogOptions::standardColor(r + c * 6));
1596 custom->setSelected(-1,-1);
1603 auto *platformServices = QGuiApplicationPrivate::platformIntegration()->services();
1604 if (platformServices && platformServices->hasCapability(QPlatformServices::Capability::ColorPicking)) {
1605 if (
auto *colorPicker = platformServices->colorPicker(q->windowHandle())) {
1606 q->connect(colorPicker, &QPlatformServiceColorPicker::colorPicked, q,
1607 [q, colorPicker](
const QColor &color) {
1608 colorPicker->deleteLater();
1609 q->setCurrentColor(color);
1611 colorPicker->pickColor();
1616 if (!colorPickingEventFilter)
1617 colorPickingEventFilter =
new QColorPickingEventFilter(
this, q);
1618 q->installEventFilter(colorPickingEventFilter);
1619 QObject::connect(
qApp, &QGuiApplication::applicationStateChanged,
1620 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1622 beforeScreenColorPicking = cs->currentColor();
1624 q->grabMouse(Qt::CrossCursor);
1631 updateTimer->start(30);
1636 dummyTransparentWindow.show();
1640
1641
1642 q->setMouseTracking(
true);
1645 buttons->setDisabled(
true);
1648 const QPoint globalPos = QCursor::pos();
1649 q->setCurrentColor(grabScreenColor(globalPos));
1656 if (lblScreenColorInfo)
1657 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1659 .arg(globalPos.y()));
1666 q->removeEventFilter(colorPickingEventFilter);
1667 QObject::disconnect(
qApp, &QGuiApplication::applicationStateChanged,
1668 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1671 updateTimer->stop();
1672 dummyTransparentWindow.setVisible(
false);
1674 q->releaseKeyboard();
1675 q->setMouseTracking(
false);
1676 lblScreenColorInfo->setText(
"\n"_L1);
1678 buttons->setDisabled(
false);
1686 q->setSizeGripEnabled(
false);
1687 q->setWindowTitle(QColorDialog::tr(
"Select Color"));
1690 nativeDialogInUse = (platformColorDialogHelper() !=
nullptr);
1694 if (!nativeDialogInUse)
1698 dummyTransparentWindow.resize(1, 1);
1699 dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
1702 q->setCurrentColor(initial);
1708 QVBoxLayout *mainLay =
new QVBoxLayout(q);
1710 mainLay->setSizeConstraint(QLayout::SetFixedSize);
1712 QHBoxLayout *topLay =
new QHBoxLayout();
1713 mainLay->addLayout(topLay);
1717#if defined(QT_SMALL_COLORDIALOG)
1718 smallDisplay =
true;
1719 const int lumSpace = 20;
1723 smallDisplay = (QGuiApplication::primaryScreen()->virtualGeometry().width() < 480 || QGuiApplication::primaryScreen()->virtualGeometry().height() < 350);
1724 const int lumSpace = topLay->spacing() / 2;
1728 leftLay =
new QVBoxLayout;
1729 topLay->addLayout(leftLay);
1731 standard =
new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
1732 lblBasicColors =
new QLabel(q);
1733#ifndef QT_NO_SHORTCUT
1734 lblBasicColors->setBuddy(standard);
1736 QObjectPrivate::connect(standard, &QColorWell::selected,
1737 this, &QColorDialogPrivate::newStandard);
1738 leftLay->addWidget(lblBasicColors);
1739 leftLay->addWidget(standard);
1741#if !defined(QT_SMALL_COLORDIALOG)
1743 eyeDropperButton =
new QPushButton();
1744 leftLay->addWidget(eyeDropperButton);
1745 lblScreenColorInfo =
new QLabel(
"\n"_L1);
1746 leftLay->addWidget(lblScreenColorInfo);
1747 QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked,
1748 this, &QColorDialogPrivate::pickScreenColor);
1751 lblScreenColorInfo =
nullptr;
1755 leftLay->addStretch();
1757 custom =
new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
1758 custom->setAcceptDrops(
true);
1760 QObjectPrivate::connect(custom, &QColorWell::selected,
this, &QColorDialogPrivate::newCustom);
1761 QObjectPrivate::connect(custom, &QColorWell::currentChanged,
this, &QColorDialogPrivate::nextCustom);
1763 QObject::connect(custom, &QWellArray::colorChanged, q, [
this] (
int index, QRgb color) {
1764 QColorDialogOptions::setCustomColor(index, color);
1769 lblCustomColors =
new QLabel(q);
1770#ifndef QT_NO_SHORTCUT
1771 lblCustomColors->setBuddy(custom);
1773 leftLay->addWidget(lblCustomColors);
1774 leftLay->addWidget(custom);
1776 addCusBt =
new QPushButton(q);
1777 QObjectPrivate::connect(addCusBt, &QPushButton::clicked,
this, &QColorDialogPrivate::addCustom);
1778 leftLay->addWidget(addCusBt);
1781#if defined(QT_SMALL_COLORDIALOG)
1782 QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size();
1783 pWidth = pHeight = qMin(screenSize.width(), screenSize.height());
1785 if (screenSize.height() > screenSize.width())
1795 QVBoxLayout *rightLay =
new QVBoxLayout;
1796 topLay->addLayout(rightLay);
1798 QHBoxLayout *pickLay =
new QHBoxLayout;
1799 rightLay->addLayout(pickLay);
1801 QVBoxLayout *cLay =
new QVBoxLayout;
1802 pickLay->addLayout(cLay);
1803 cp =
new QColorPicker(q);
1805 cp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
1807#if defined(QT_SMALL_COLORDIALOG)
1810 cLay->addSpacing(lumSpace);
1811 cLay->addWidget(
cp);
1813 cLay->addSpacing(lumSpace);
1815 lp =
new QColorLuminancePicker(q);
1816#if defined(QT_SMALL_COLORDIALOG)
1819 lp->setFixedWidth(20);
1820 pickLay->addSpacing(10);
1821 pickLay->addWidget(
lp);
1822 pickLay->addStretch();
1825 QObject::connect(cp, &QColorPicker::newCol, lp, qOverload<
int,
int>(&QColorLuminancePicker::setCol));
1826 QObjectPrivate::connect(lp, &QColorLuminancePicker::newHsv,
this, &QColorDialogPrivate::newHsv);
1828 rightLay->addStretch();
1830 cs =
new QColorShower(q);
1831 pickLay->setContentsMargins(
cs->gl->contentsMargins());
1832 QObjectPrivate::connect(cs, &QColorShower::newCol,
1833 this, &QColorDialogPrivate::newColorTypedIn);
1834 QObject::connect(cs, &QColorShower::currentColorChanged,
1835 q, &QColorDialog::currentColorChanged);
1836#if defined(QT_SMALL_COLORDIALOG)
1837 topLay->addWidget(cs);
1839 rightLay->addWidget(
cs);
1841 leftLay->addSpacing(cs->gl->contentsMargins().right());
1844 buttons =
new QDialogButtonBox(q);
1845 mainLay->addWidget(buttons);
1847 ok = buttons->addButton(QDialogButtonBox::Ok);
1848 QObject::connect(ok, &QPushButton::clicked, q, &QColorDialog::accept);
1849 ok->setDefault(
true);
1850 cancel = buttons->addButton(QDialogButtonBox::Cancel);
1851 QObject::connect(cancel, &QPushButton::clicked, q, &QColorDialog::reject);
1854 updateTimer =
new QTimer(q);
1855 QObjectPrivate::connect(updateTimer, &QTimer::timeout,
1856 this, qOverload<>(&QColorDialogPrivate::updateColorPicking));
1863 QColorDialog *d = q_func();
1864 auto *colorDialogHelper =
static_cast<QPlatformColorDialogHelper*>(h);
1865 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::currentColorChanged,
1866 d, &QColorDialog::currentColorChanged);
1867 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::colorSelected,
1868 d, &QColorDialog::colorSelected);
1869 colorDialogHelper->setOptions(options);
1874 options->setWindowTitle(q_func()->windowTitle());
1879 QColorDialogOptions::setCustomColor(nextCust, cs->currentColor());
1882 nextCust = (nextCust+1) % QColorDialogOptions::customColorCount();
1887 if (nativeDialogInUse)
1891 lblBasicColors->setText(QColorDialog::tr(
"&Basic colors"));
1892 lblCustomColors->setText(QColorDialog::tr(
"&Custom colors"));
1893 addCusBt->setText(QColorDialog::tr(
"&Add to Custom Colors"));
1894#if !defined(QT_SMALL_COLORDIALOG)
1905 const auto integration = QGuiApplicationPrivate::platformIntegration();
1906 return integration->hasCapability(QPlatformIntegration::ScreenWindowGrabbing)
1907 || integration->services()->hasCapability(QPlatformServices::Capability::ColorPicking);
1914 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
1915 if (nativeDialogInUse)
1917 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
1918 || q->testAttribute(Qt::WA_DontShowOnScreen)
1919 || (options->options() & QColorDialog::DontUseNativeDialog)) {
1923 return strcmp(QColorDialog::staticMetaObject.className(), q->metaObject()->className()) == 0;
1927 Qt::Dialog | Qt::WindowTitleHint
1928 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1970
1971
1972QColorDialog::QColorDialog(QWidget *parent)
1973 : QColorDialog(QColor(Qt::white), parent)
1975 new QColorPolishFilter(
this);
1979
1980
1981
1982QColorDialog::QColorDialog(
const QColor &initial, QWidget *parent)
1983 : QDialog(*
new QColorDialogPrivate, parent, qcd_DefaultWindowFlags)
1986 new QColorPolishFilter(
this);
1992 if (nativeDialogInUse) {
1993 platformColorDialogHelper()->setCurrentColor(color);
1997 if (setColorMode & ShowColor) {
1998 setCurrentRgbColor(color.rgb());
2001 if (setColorMode & SelectColor)
2006
2007
2008
2010void QColorDialog::setCurrentColor(
const QColor &color)
2013 d->setCurrentColor(color);
2016QColor QColorDialog::currentColor()
const
2018 Q_D(
const QColorDialog);
2019 return d->currentQColor();
2023
2024
2025
2026
2027
2028
2029
2030QColor QColorDialog::selectedColor()
const
2032 Q_D(
const QColorDialog);
2033 return d->selectedQColor;
2037
2038
2039
2040
2041
2042void QColorDialog::setOption(ColorDialogOption option,
bool on)
2045 d->optionsExplicitlySet =
true;
2047 const QColorDialog::ColorDialogOptions previousOptions = options();
2048 if (!(previousOptions & option) != !on)
2049 setOptions(previousOptions ^ option);
2053
2054
2055
2056
2057
2058bool QColorDialog::testOption(ColorDialogOption option)
const
2060 Q_D(
const QColorDialog);
2061 return d->options->testOption(
static_cast<QColorDialogOptions::ColorDialogOption>(option));
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076void QColorDialog::setOptions(ColorDialogOptions options)
2079 d->optionsExplicitlySet =
true;
2081 if (QColorDialog::options() == options)
2084 d->options->setOptions(QColorDialogOptions::ColorDialogOptions(
int(options)));
2085 if ((options & DontUseNativeDialog) && d->nativeDialogInUse) {
2086 d->nativeDialogInUse =
false;
2089 if (!d->nativeDialogInUse) {
2090 d->buttons->setVisible(!(options & NoButtons));
2091 d->showAlpha(options & ShowAlphaChannel);
2092 if (d->eyeDropperButton)
2093 d->eyeDropperButton->setVisible(!(options & NoEyeDropperButton));
2097QColorDialog::ColorDialogOptions QColorDialog::options()
const
2099 Q_D(
const QColorDialog);
2100 return QColorDialog::ColorDialogOptions(
int(d->options->options()));
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2119
2120
2121
2122
2123
2124
2125
2128
2129
2130
2131
2132
2133
2134
2137
2138
2139
2140void QColorDialog::setVisible(
bool visible)
2143 QDialog::setVisible(visible);
2147
2148
2149
2150
2151
2152
2157 const auto q =
static_cast<QDialog *>(q_ptr);
2160 selectedQColor = QColor();
2162 if (nativeDialogInUse) {
2163 if (setNativeDialogVisible(visible)) {
2166 q->setAttribute(Qt::WA_DontShowOnScreen);
2167 }
else if (visible) {
2171 q->setAttribute(Qt::WA_DontShowOnScreen,
false);
2174 QDialogPrivate::setVisible(visible);
2178
2179
2180
2181
2182
2183void QColorDialog::open(QObject *receiver,
const char *member)
2186 connect(
this, SIGNAL(colorSelected(QColor)), receiver, member);
2187 d->receiverToDisconnectOnClose = receiver;
2188 d->memberToDisconnectOnClose = member;
2193
2194
2195
2196
2197
2198
2199
2200QColor QColorDialog::getColor(
const QColor &initial, QWidget *parent,
const QString &title,
2201 ColorDialogOptions options)
2203 QAutoPointer<QColorDialog> dlg(
new QColorDialog(parent));
2204 if (!title.isEmpty())
2205 dlg->setWindowTitle(title);
2206 dlg->setOptions(options);
2207 dlg->setCurrentColor(initial);
2213 return dlg->selectedColor();
2219
2220
2222QColorDialog::~QColorDialog()
2227
2228
2229void QColorDialog::changeEvent(QEvent *e)
2232 if (e->type() == QEvent::LanguageChange)
2233 d->retranslateStrings();
2234 QDialog::changeEvent(e);
2241 static QPoint lastGlobalPos;
2242 QPoint newGlobalPos = QCursor::pos();
2243 if (lastGlobalPos == newGlobalPos)
2245 lastGlobalPos = newGlobalPos;
2247 if (!q->rect().contains(q->mapFromGlobal(newGlobalPos))) {
2248 updateColorPicking(newGlobalPos);
2250 dummyTransparentWindow.setPosition(newGlobalPos);
2258 const QColor color = grabScreenColor(globalPos);
2261 setCurrentColor(color, ShowColor);
2270 updateColorPicking(e->globalPosition().toPoint());
2276 setCurrentColor(grabScreenColor(e->globalPosition().toPoint()), SetColorAll);
2284#if QT_CONFIG(shortcut)
2285 if (e->matches(QKeySequence::Cancel)) {
2286 releaseColorPicking();
2287 q->setCurrentColor(beforeScreenColorPicking);
2290 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
2291 q->setCurrentColor(grabScreenColor(QCursor::pos()));
2299
2300
2301
2302
2303
2304
2305void QColorDialog::done(
int result)
2308 if (result == Accepted) {
2309 d->selectedQColor = d->currentQColor();
2310 emit colorSelected(d->selectedQColor);
2312 d->selectedQColor = QColor();
2314 QDialog::done(result);
2315 if (d->receiverToDisconnectOnClose) {
2316 disconnect(
this, SIGNAL(colorSelected(QColor)),
2317 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
2318 d->receiverToDisconnectOnClose =
nullptr;
2320 d->memberToDisconnectOnClose.clear();
2325#include "qcolordialog.moc"
2326#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)
bool eventFilter(QObject *obj, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
QColorPolishFilter(QColorDialog *cd)
\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.
QColor currentQColor() const
void setCurrentAlpha(int a)
bool isAlphaVisible() const
void setHsv(int h, int s, int v)
void retranslateStrings()
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