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)
1660 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1661 .arg(QStringLiteral(
"-"))
1662 .arg(QStringLiteral(
"-")));
1664 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1666 .arg(globalPos.y()));
1674 q->removeEventFilter(colorPickingEventFilter);
1675 QObject::disconnect(
qApp, &QGuiApplication::applicationStateChanged,
1676 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1679 updateTimer->stop();
1680 dummyTransparentWindow.setVisible(
false);
1682 q->releaseKeyboard();
1683 q->setMouseTracking(
false);
1684 lblScreenColorInfo->setText(
"\n"_L1);
1686 buttons->setDisabled(
false);
1694 q->setSizeGripEnabled(
false);
1695 q->setWindowTitle(QColorDialog::tr(
"Select Color"));
1698 nativeDialogInUse = (platformColorDialogHelper() !=
nullptr);
1702 if (!nativeDialogInUse)
1706 dummyTransparentWindow.resize(1, 1);
1707 dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
1710 q->setCurrentColor(initial);
1716 QVBoxLayout *mainLay =
new QVBoxLayout(q);
1718 mainLay->setSizeConstraint(QLayout::SetFixedSize);
1720 QHBoxLayout *topLay =
new QHBoxLayout();
1721 mainLay->addLayout(topLay);
1725#if defined(QT_SMALL_COLORDIALOG)
1726 smallDisplay =
true;
1727 const int lumSpace = 20;
1731 smallDisplay = (QGuiApplication::primaryScreen()->virtualGeometry().width() < 480 || QGuiApplication::primaryScreen()->virtualGeometry().height() < 350);
1732 const int lumSpace = topLay->spacing() / 2;
1736 leftLay =
new QVBoxLayout;
1737 topLay->addLayout(leftLay);
1739 standard =
new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
1740 lblBasicColors =
new QLabel(q);
1741#ifndef QT_NO_SHORTCUT
1742 lblBasicColors->setBuddy(standard);
1744 QObjectPrivate::connect(standard, &QColorWell::selected,
1745 this, &QColorDialogPrivate::newStandard);
1746 leftLay->addWidget(lblBasicColors);
1747 leftLay->addWidget(standard);
1749#if !defined(QT_SMALL_COLORDIALOG)
1751 eyeDropperButton =
new QPushButton();
1752 leftLay->addWidget(eyeDropperButton);
1753 lblScreenColorInfo =
new QLabel(
"\n"_L1);
1754 leftLay->addWidget(lblScreenColorInfo);
1755 QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked,
1756 this, &QColorDialogPrivate::pickScreenColor);
1759 lblScreenColorInfo =
nullptr;
1763 leftLay->addStretch();
1765 custom =
new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
1766 custom->setAcceptDrops(
true);
1768 QObjectPrivate::connect(custom, &QColorWell::selected,
this, &QColorDialogPrivate::newCustom);
1769 QObjectPrivate::connect(custom, &QColorWell::currentChanged,
this, &QColorDialogPrivate::nextCustom);
1771 QObject::connect(custom, &QWellArray::colorChanged, q, [
this] (
int index, QRgb color) {
1772 QColorDialogOptions::setCustomColor(index, color);
1777 lblCustomColors =
new QLabel(q);
1778#ifndef QT_NO_SHORTCUT
1779 lblCustomColors->setBuddy(custom);
1781 leftLay->addWidget(lblCustomColors);
1782 leftLay->addWidget(custom);
1784 addCusBt =
new QPushButton(q);
1785 QObjectPrivate::connect(addCusBt, &QPushButton::clicked,
this, &QColorDialogPrivate::addCustom);
1786 leftLay->addWidget(addCusBt);
1789#if defined(QT_SMALL_COLORDIALOG)
1790 QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size();
1791 pWidth = pHeight = qMin(screenSize.width(), screenSize.height());
1793 if (screenSize.height() > screenSize.width())
1803 QVBoxLayout *rightLay =
new QVBoxLayout;
1804 topLay->addLayout(rightLay);
1806 QHBoxLayout *pickLay =
new QHBoxLayout;
1807 rightLay->addLayout(pickLay);
1809 QVBoxLayout *cLay =
new QVBoxLayout;
1810 pickLay->addLayout(cLay);
1811 cp =
new QColorPicker(q);
1813 cp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
1815#if defined(QT_SMALL_COLORDIALOG)
1818 cLay->addSpacing(lumSpace);
1819 cLay->addWidget(
cp);
1821 cLay->addSpacing(lumSpace);
1823 lp =
new QColorLuminancePicker(q);
1824#if defined(QT_SMALL_COLORDIALOG)
1827 lp->setFixedWidth(20);
1828 pickLay->addSpacing(10);
1829 pickLay->addWidget(
lp);
1830 pickLay->addStretch();
1833 QObject::connect(cp, &QColorPicker::newCol, lp, qOverload<
int,
int>(&QColorLuminancePicker::setCol));
1834 QObjectPrivate::connect(lp, &QColorLuminancePicker::newHsv,
this, &QColorDialogPrivate::newHsv);
1836 rightLay->addStretch();
1838 cs =
new QColorShower(q);
1839 pickLay->setContentsMargins(
cs->gl->contentsMargins());
1840 QObjectPrivate::connect(cs, &QColorShower::newCol,
1841 this, &QColorDialogPrivate::newColorTypedIn);
1842 QObject::connect(cs, &QColorShower::currentColorChanged,
1843 q, &QColorDialog::currentColorChanged);
1844#if defined(QT_SMALL_COLORDIALOG)
1845 topLay->addWidget(cs);
1847 rightLay->addWidget(
cs);
1849 leftLay->addSpacing(cs->gl->contentsMargins().right());
1852 buttons =
new QDialogButtonBox(q);
1853 mainLay->addWidget(buttons);
1855 ok = buttons->addButton(QDialogButtonBox::Ok);
1856 QObject::connect(ok, &QPushButton::clicked, q, &QColorDialog::accept);
1857 ok->setDefault(
true);
1858 cancel = buttons->addButton(QDialogButtonBox::Cancel);
1859 QObject::connect(cancel, &QPushButton::clicked, q, &QColorDialog::reject);
1862 updateTimer =
new QTimer(q);
1863 QObjectPrivate::connect(updateTimer, &QTimer::timeout,
1864 this, qOverload<>(&QColorDialogPrivate::updateColorPicking));
1871 QColorDialog *d = q_func();
1872 auto *colorDialogHelper =
static_cast<QPlatformColorDialogHelper*>(h);
1873 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::currentColorChanged,
1874 d, &QColorDialog::currentColorChanged);
1875 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::colorSelected,
1876 d, &QColorDialog::colorSelected);
1877 colorDialogHelper->setOptions(options);
1882 options->setWindowTitle(q_func()->windowTitle());
1887 QColorDialogOptions::setCustomColor(nextCust, cs->currentColor());
1890 nextCust = (nextCust+1) % QColorDialogOptions::customColorCount();
1895 if (nativeDialogInUse)
1899 lblBasicColors->setText(QColorDialog::tr(
"&Basic colors"));
1900 lblCustomColors->setText(QColorDialog::tr(
"&Custom colors"));
1901 addCusBt->setText(QColorDialog::tr(
"&Add to Custom Colors"));
1902#if !defined(QT_SMALL_COLORDIALOG)
1913 const auto integration = QGuiApplicationPrivate::platformIntegration();
1914 return integration->hasCapability(QPlatformIntegration::ScreenWindowGrabbing)
1915 || integration->services()->hasCapability(QPlatformServices::Capability::ColorPicking);
1922 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
1923 if (nativeDialogInUse)
1925 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
1926 || q->testAttribute(Qt::WA_DontShowOnScreen)
1927 || (options->options() & QColorDialog::DontUseNativeDialog)) {
1931 return strcmp(QColorDialog::staticMetaObject.className(), q->metaObject()->className()) == 0;
1935 Qt::Dialog | Qt::WindowTitleHint
1936 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
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
1968
1969
1970
1971
1972
1973
1974
1975
1978
1979
1980QColorDialog::QColorDialog(QWidget *parent)
1981 : QColorDialog(QColor(Qt::white), parent)
1983 new QColorPolishFilter(
this);
1987
1988
1989
1990QColorDialog::QColorDialog(
const QColor &initial, QWidget *parent)
1991 : QDialog(*
new QColorDialogPrivate, parent, qcd_DefaultWindowFlags)
1994 new QColorPolishFilter(
this);
2000 if (nativeDialogInUse) {
2001 platformColorDialogHelper()->setCurrentColor(color);
2005 if (setColorMode & ShowColor) {
2006 setCurrentRgbColor(color.rgb());
2009 if (setColorMode & SelectColor)
2014
2015
2016
2018void QColorDialog::setCurrentColor(
const QColor &color)
2021 d->setCurrentColor(color);
2024QColor QColorDialog::currentColor()
const
2026 Q_D(
const QColorDialog);
2027 return d->currentQColor();
2031
2032
2033
2034
2035
2036
2037
2038QColor QColorDialog::selectedColor()
const
2040 Q_D(
const QColorDialog);
2041 return d->selectedQColor;
2045
2046
2047
2048
2049
2050void QColorDialog::setOption(ColorDialogOption option,
bool on)
2053 d->optionsExplicitlySet =
true;
2055 const QColorDialog::ColorDialogOptions previousOptions = options();
2056 if (!(previousOptions & option) != !on)
2057 setOptions(previousOptions ^ option);
2061
2062
2063
2064
2065
2066bool QColorDialog::testOption(ColorDialogOption option)
const
2068 Q_D(
const QColorDialog);
2069 return d->options->testOption(
static_cast<QColorDialogOptions::ColorDialogOption>(option));
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084void QColorDialog::setOptions(ColorDialogOptions options)
2087 d->optionsExplicitlySet =
true;
2089 if (QColorDialog::options() == options)
2092 d->options->setOptions(QColorDialogOptions::ColorDialogOptions(
int(options)));
2093 if ((options & DontUseNativeDialog) && d->nativeDialogInUse) {
2094 d->nativeDialogInUse =
false;
2097 if (!d->nativeDialogInUse) {
2098 d->buttons->setVisible(!(options & NoButtons));
2099 d->showAlpha(options & ShowAlphaChannel);
2100 if (d->eyeDropperButton)
2101 d->eyeDropperButton->setVisible(!(options & NoEyeDropperButton));
2105QColorDialog::ColorDialogOptions QColorDialog::options()
const
2107 Q_D(
const QColorDialog);
2108 return QColorDialog::ColorDialogOptions(
int(d->options->options()));
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2127
2128
2129
2130
2131
2132
2133
2136
2137
2138
2139
2140
2141
2142
2145
2146
2147
2148void QColorDialog::setVisible(
bool visible)
2151 QDialog::setVisible(visible);
2155
2156
2157
2158
2159
2160
2165 const auto q =
static_cast<QDialog *>(q_ptr);
2168 selectedQColor = QColor();
2170 if (nativeDialogInUse) {
2171 if (setNativeDialogVisible(visible)) {
2174 q->setAttribute(Qt::WA_DontShowOnScreen);
2175 }
else if (visible) {
2179 q->setAttribute(Qt::WA_DontShowOnScreen,
false);
2182 QDialogPrivate::setVisible(visible);
2186
2187
2188
2189
2190
2191void QColorDialog::open(QObject *receiver,
const char *member)
2194 connect(
this, SIGNAL(colorSelected(QColor)), receiver, member);
2195 d->receiverToDisconnectOnClose = receiver;
2196 d->memberToDisconnectOnClose = member;
2201
2202
2203
2204
2205
2206
2207
2208QColor QColorDialog::getColor(
const QColor &initial, QWidget *parent,
const QString &title,
2209 ColorDialogOptions options)
2211 QAutoPointer<QColorDialog> dlg(
new QColorDialog(parent));
2212 if (!title.isEmpty())
2213 dlg->setWindowTitle(title);
2214 dlg->setOptions(options);
2215 dlg->setCurrentColor(initial);
2221 return dlg->selectedColor();
2227
2228
2230QColorDialog::~QColorDialog()
2235
2236
2237void QColorDialog::changeEvent(QEvent *e)
2240 if (e->type() == QEvent::LanguageChange)
2241 d->retranslateStrings();
2242 QDialog::changeEvent(e);
2249 static QPoint lastGlobalPos;
2250 QPoint newGlobalPos = QCursor::pos();
2251 if (lastGlobalPos == newGlobalPos)
2253 lastGlobalPos = newGlobalPos;
2255 if (!q->rect().contains(q->mapFromGlobal(newGlobalPos))) {
2256 updateColorPicking(newGlobalPos);
2258 dummyTransparentWindow.setPosition(newGlobalPos);
2266 const QColor color = grabScreenColor(globalPos);
2269 setCurrentColor(color, ShowColor);
2278 updateColorPicking(e->globalPosition().toPoint());
2284 setCurrentColor(grabScreenColor(e->globalPosition().toPoint()), SetColorAll);
2292#if QT_CONFIG(shortcut)
2293 if (e->matches(QKeySequence::Cancel)) {
2294 releaseColorPicking();
2295 q->setCurrentColor(beforeScreenColorPicking);
2298 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
2299 q->setCurrentColor(grabScreenColor(QCursor::pos()));
2307
2308
2309
2310
2311
2312
2313void QColorDialog::done(
int result)
2316 if (result == Accepted) {
2317 d->selectedQColor = d->currentQColor();
2318 emit colorSelected(d->selectedQColor);
2320 d->selectedQColor = QColor();
2322 QDialog::done(result);
2323 if (d->receiverToDisconnectOnClose) {
2324 disconnect(
this, SIGNAL(colorSelected(QColor)),
2325 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
2326 d->receiverToDisconnectOnClose =
nullptr;
2328 d->memberToDisconnectOnClose.clear();
2333#include "qcolordialog.moc"
2334#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