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);
165void QWellArray::paintEvent(QPaintEvent *e)
172 int colfirst = columnAt(cx);
173 int collast = columnAt(cx + cw);
174 int rowfirst = rowAt(cy);
175 int rowlast = rowAt(cy + ch);
177 if (isRightToLeft()) {
183 QPainter painter(
this);
184 QPainter *p = &painter;
185 QRect rect(0, 0, cellWidth(), cellHeight());
188 if (collast < 0 || collast >= ncols)
190 if (rowlast < 0 || rowlast >= nrows)
194 for (
int r = rowfirst; r <= rowlast; ++r) {
201 for (
int c = colfirst; c <= collast; ++c) {
203 int colp = columnX(c);
205 rect.translate(colp, rowp);
206 paintCell(p, r, c, rect);
207 rect.translate(-colp, -rowp);
212QWellArray::QWellArray(
int rows,
int cols, QWidget *parent)
214 ,nrows(rows), ncols(cols)
216 setFocusPolicy(Qt::StrongFocus);
225QSize QWellArray::sizeHint()
const
228 return gridSize().boundedTo(QSize(640, 480));
232void QWellArray::paintCell(QPainter* p,
int row,
int col,
const QRect &rect)
236 const QPalette & g = palette();
237 QStyleOptionFrame opt;
239 int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt,
this);
241 opt.midLineWidth = 1;
242 opt.rect = rect.adjusted(b, b, -b, -b);
244 opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
245 style()->drawPrimitive(QStyle::PE_Frame, &opt, p,
this);
248 if ((row == curRow) && (col == curCol)) {
250 QStyleOptionFocusRect opt;
253 opt.state = QStyle::State_None | QStyle::State_KeyboardFocusChange;
254 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p,
this);
257 paintCellContents(p, row, col, opt.rect.adjusted(dfw, dfw, -dfw, -dfw));
261
262
263void QWellArray::paintCellContents(QPainter *p,
int row,
int col,
const QRect &r)
267 p->fillRect(r, Qt::white);
268 p->setPen(Qt::black);
269 p->drawLine(r.topLeft(), r.bottomRight());
270 p->drawLine(r.topRight(), r.bottomLeft());
273void QWellArray::mousePressEvent(QMouseEvent *e)
276 QPoint pos = e->position().toPoint();
277 setCurrent(rowAt(pos.y()), columnAt(pos.x()));
280void QWellArray::mouseReleaseEvent(QMouseEvent * )
283 setSelected(curRow, curCol);
288
289
290
292void QWellArray::setCurrent(
int row,
int col)
294 if ((curRow == row) && (curCol == col))
297 if (row < 0 || col < 0 || row >= nrows || col >= ncols)
306 updateCell(oldRow, oldCol);
307 updateCell(curRow, curCol);
309 emit currentChanged(curRow, curCol);
310 sendAccessibleChildFocusEvent();
314
315
316
317
318
319void QWellArray::setSelected(
int row,
int col)
324 if (row < 0 || col < 0)
330 updateCell(oldRow, oldCol);
331 updateCell(selRow, selCol);
333 emit selected(row, col);
336 if (isVisible() && qobject_cast<QMenu*>(parentWidget()))
337 parentWidget()->close();
341void QWellArray::focusInEvent(QFocusEvent*)
343 updateCell(curRow, curCol);
344 emit currentChanged(curRow, curCol);
345 sendAccessibleChildFocusEvent();
349void QWellArray::focusOutEvent(QFocusEvent*)
351 updateCell(curRow, curCol);
354void QWellArray::keyPressEvent(QKeyEvent* e)
359 setCurrent(curRow, curCol - 1);
362 if (curCol < numCols()-1)
363 setCurrent(curRow, curCol + 1);
367 setCurrent(curRow - 1, curCol);
370 if (curRow < numRows()-1)
371 setCurrent(curRow + 1, curCol);
374 setSelected(curRow, curCol);
382void QWellArray::sendAccessibleChildFocusEvent()
384#if QT_CONFIG(accessibility)
385 if (!QAccessible::isActive())
388 if (hasFocus() && curRow >= 0 && curCol >= 0) {
389 const int itemIndex = index(curRow, curCol);
390 QAccessibleEvent event(
this, QAccessible::Focus);
391 event.setChild(itemIndex);
392 QAccessible::updateAccessibility(&event);
408 switch (event->type()) {
409 case QEvent::MouseMove:
411 case QEvent::MouseButtonRelease:
413 case QEvent::KeyPress:
423 if (state != Qt::ApplicationActive)
434
435
436
437int QColorDialog::customCount()
439 return QColorDialogOptions::customColorCount();
443
444
445QColor QColorDialog::customColor(
int index)
447 return QColor(QColorDialogOptions::customColor(index));
451
452
453
454
455
456
457void QColorDialog::setCustomColor(
int index, QColor color)
459 QColorDialogOptions::setCustomColor(index, color.rgba());
463
464
465
466
467QColor QColorDialog::standardColor(
int index)
469 return QColor(QColorDialogOptions::standardColor(index));
473
474
475
476
477
478
479void QColorDialog::setStandardColor(
int index, QColor color)
481 QColorDialogOptions::setStandardColor(index, color.rgba());
484static inline void rgb2hsv(QRgb rgb,
int &h,
int &s,
int &v)
488 c.getHsv(&h, &s, &v);
491void QColorWell::paintCellContents(QPainter *p,
int row,
int col,
const QRect &r)
493 int i = row + col*numRows();
494 p->fillRect(r, QColor(values[i]));
497void QColorWell::mousePressEvent(QMouseEvent *e)
499 oldCurrent = QPoint(selectedRow(), selectedColumn());
500 QWellArray::mousePressEvent(e);
502 pressPos = e->position().toPoint();
505void QColorWell::mouseMoveEvent(QMouseEvent *e)
507 QWellArray::mouseMoveEvent(e);
508#if QT_CONFIG(draganddrop)
511 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
512 setCurrent(oldCurrent.x(), oldCurrent.y());
513 int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
514 QColor col(values[i]);
515 QMimeData *mime =
new QMimeData;
516 mime->setColorData(col);
517 QPixmap pix(cellWidth(), cellHeight());
520 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
522 QDrag *drg =
new QDrag(
this);
523 drg->setMimeData(mime);
525 mousePressed =
false;
526 drg->exec(Qt::CopyAction);
531#if QT_CONFIG(draganddrop)
532void QColorWell::dragEnterEvent(QDragEnterEvent *e)
534 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
540void QColorWell::dragLeaveEvent(QDragLeaveEvent *)
543 parentWidget()->setFocus();
546void QColorWell::dragMoveEvent(QDragMoveEvent *e)
548 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) {
549 setCurrent(rowAt(e->position().toPoint().y()), columnAt(e->position().toPoint().x()));
556void QColorWell::dropEvent(QDropEvent *e)
558 QColor col = qvariant_cast<QColor>(e->mimeData()->colorData());
560 int i = rowAt(e->position().toPoint().y()) + columnAt(e->position().toPoint().x()) * numRows();
561 emit colorChanged(i, col.rgb());
570void QColorWell::mouseReleaseEvent(QMouseEvent *e)
574 QWellArray::mouseReleaseEvent(e);
575 mousePressed =
false;
605 QPixmap createColorsPixmap();
606 QPoint colPt(
int hue,
int sat);
607 int huePt(
const QPoint &pt,
const QSize &widgetSize);
608 int huePt(
const QPoint &pt) {
return huePt(pt, size()); }
609 int satPt(
const QPoint &pt,
const QSize &widgetSize);
610 int satPt(
const QPoint &pt) {
return satPt(pt, size()); }
611 void setCol(
const QPoint &pt,
bool notify =
true);
645 enum { foff = 3, coff = 4 };
660 int d = height() - 2*coff - 1;
661 return 255 - (y - coff)*255/d;
666 int d = height() - 2*coff - 1;
667 return coff + (255-v)*d/255;
673 hue = 100; val = 100; sat = 100;
675 setFocusPolicy(Qt::StrongFocus);
684 switch (event->key()) {
686 setVal(
std::clamp(val - 1, 0, 255));
689 setVal(
std::clamp(val + 1, 0, 255));
692 QWidget::keyPressEvent(event);
699 if (m->buttons() == Qt::NoButton) {
703 setVal(y2val(m->position().toPoint().y()));
707 setVal(y2val(m->position().toPoint().y()));
714 val = qMax(0, qMin(v,255));
717 emit newHsv(hue, sat, val);
724 emit newHsv(h, s, val);
731 QRect r(0, foff, w, height() - 2*foff);
732 int wi = r.width() - 2;
733 int hi = r.height() - 2;
734 if (pix.isNull() || pix.height() != hi || pix.width() != wi) {
735 QImage img(wi, hi, QImage::Format_RGB32);
737 uint *pixel = (uint *) img.scanLine(0);
738 for (y = 0; y < hi; y++) {
739 uint *end = pixel + wi;
740 std::fill(pixel, end, QColor::fromHsv(hue, sat, y2val(y + coff)).rgb());
743 pix = QPixmap::fromImage(img);
746 p.drawPixmap(1, coff, pix);
747 const QPalette &g = palette();
748 qDrawShadePanel(&p, r, g,
true);
749 p.setPen(g.windowText().color());
750 p.setBrush(g.windowText());
751 p.eraseRect(w, 0, 5, height());
752 const int y = val2y(val);
753 const std::array<QPoint, 3> points = {QPoint(w, y), QPoint(w + 5, y + 5), QPoint(w + 5, y - 5)};
754 p.drawPolygon(points.data(),
static_cast<
int>(points.size()));
768 QRect r = contentsRect();
769 return QPoint((360 - hue) * (r.width() - 1) / 360, (255 - sat) * (r.height() - 1) / 255);
774 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
775 return std::clamp(360 - pt.x() * 360 / (r.width() - 1), 0, 359);
780 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
781 return std::clamp(255 - pt.y() * 255 / (r.height() - 1), 0, 255);
786 if (pt == m_pos || pix.isNull())
789 Q_ASSERT(pix.height());
790 Q_ASSERT(pix.width());
792 QRect r(m_pos, QSize(20, 20));
793 m_pos.setX(std::clamp(pt.x(), 0, pix.width() - 1));
794 m_pos.setY(std::clamp(pt.y(), 0, pix.height() - 1));
795 r = r.united(QRect(m_pos, QSize(20, 20)));
796 r.translate(contentsRect().x() - 9, contentsRect().y() - 9);
801 emit newCol(huePt(m_pos), satPt(m_pos));
808 setAttribute(Qt::WA_NoSystemBackground);
809 setFocusPolicy(Qt::StrongFocus);
810 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
813 pix = createColorsPixmap();
824 if (crossVisible != visible) {
825 crossVisible = visible;
832 return QSize(
pWidth + 2*frameWidth(),
pHeight + 2*frameWidth());
837 int nhue = qMin(qMax(0,h), 359);
838 int nsat = qMin(qMax(0,s), 255);
839 if (nhue == huePt(m_pos) && nsat == satPt(m_pos))
842 setCol(colPt(nhue, nsat),
false);
847 switch (event->key()) {
849 setCol(m_pos + QPoint(0, 1));
852 setCol(m_pos + QPoint(-1, 0));
855 setCol(m_pos + QPoint(1, 0));
858 setCol(m_pos + QPoint(0, -1));
861 QFrame::keyPressEvent(event);
868 QPoint p = m->position().toPoint() - contentsRect().topLeft();
869 if (m->buttons() == Qt::NoButton) {
878 QPoint p = m->position().toPoint() - contentsRect().topLeft();
886 QRect r = contentsRect();
888 p.drawPixmap(r.topLeft(), pix);
891 QPoint pt = m_pos + r.topLeft();
893 p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
894 p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
900 QFrame::resizeEvent(ev);
902 pix = createColorsPixmap();
904 const QSize &oldSize = ev->oldSize();
905 if (!oldSize.isValid())
910 const int hue = huePt(m_pos, oldSize);
911 const int sat = satPt(m_pos, oldSize);
917 int w = width() - frameWidth() * 2;
918 int h = height() - frameWidth() * 2;
919 QImage img(w, h, QImage::Format_RGB32);
921 uint *pixel = (uint *) img.scanLine(0);
922 for (y = 0; y < h; y++) {
923 const uint *end = pixel + w;
925 while (pixel < end) {
928 c.setHsv(huePt(p), satPt(p), 200);
934 return QPixmap::fromImage(img);
943 const QSignalBlocker blocker(
this);
944 QSpinBox::setValue(i);
960 {
return (colorDialog->options() & QColorDialog::ShowAlphaChannel) ? alphaEd->value() : 255; }
983 void showCurrentColor();
1005 QColorDialog *colorDialog;
1032#if QT_CONFIG(draganddrop)
1048 p.fillRect(contentsRect()&e->rect(), col);
1053 alphaLab->setVisible(b);
1054 alphaEd->setVisible(b);
1059 return alphaLab->isVisible();
1064 mousePressed =
true;
1065 pressPos = e->position().toPoint();
1070#if !QT_CONFIG(draganddrop)
1075 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
1076 QMimeData *mime =
new QMimeData;
1077 mime->setColorData(col);
1078 QPixmap pix(30, 20);
1081 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
1083 QDrag *drg =
new QDrag(
this);
1084 drg->setMimeData(mime);
1085 drg->setPixmap(pix);
1086 mousePressed =
false;
1087 drg->exec(Qt::CopyAction);
1092#if QT_CONFIG(draganddrop)
1123 mousePressed =
false;
1129 colorDialog = parent;
1131 curCol = qRgb(255, 255, 255);
1132 curQColor = Qt::white;
1134 gl =
new QGridLayout(
this);
1135 const int s = gl->spacing();
1136 gl->setContentsMargins(s, s, s, s);
1137 lab =
new QColorShowLabel(
this);
1139#ifdef QT_SMALL_COLORDIALOG
1140 lab->setMinimumHeight(60);
1142 lab->setMinimumWidth(60);
1146#if !defined(QT_SMALL_COLORDIALOG)
1147 gl->addWidget(lab, 0, 0, -1, 1);
1149 gl->addWidget(lab, 0, 0, 1, -1);
1151 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::newCol);
1152 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::setRgb);
1154 hEd =
new QColSpinBox(
this);
1155 hEd->setRange(0, 359);
1156 lblHue =
new QLabel(
this);
1157#ifndef QT_NO_SHORTCUT
1158 lblHue->setBuddy(hEd);
1160 lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1161#if !defined(QT_SMALL_COLORDIALOG)
1162 gl->addWidget(lblHue, 0, 1);
1163 gl->addWidget(hEd, 0, 2);
1165 gl->addWidget(lblHue, 1, 0);
1166 gl->addWidget(hEd, 2, 0);
1169 sEd =
new QColSpinBox(
this);
1170 lblSat =
new QLabel(
this);
1171#ifndef QT_NO_SHORTCUT
1172 lblSat->setBuddy(sEd);
1174 lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1175#if !defined(QT_SMALL_COLORDIALOG)
1176 gl->addWidget(lblSat, 1, 1);
1177 gl->addWidget(sEd, 1, 2);
1179 gl->addWidget(lblSat, 1, 1);
1180 gl->addWidget(sEd, 2, 1);
1183 vEd =
new QColSpinBox(
this);
1184 lblVal =
new QLabel(
this);
1185#ifndef QT_NO_SHORTCUT
1186 lblVal->setBuddy(vEd);
1188 lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1189#if !defined(QT_SMALL_COLORDIALOG)
1190 gl->addWidget(lblVal, 2, 1);
1191 gl->addWidget(vEd, 2, 2);
1193 gl->addWidget(lblVal, 1, 2);
1194 gl->addWidget(vEd, 2, 2);
1197 rEd =
new QColSpinBox(
this);
1198 lblRed =
new QLabel(
this);
1199#ifndef QT_NO_SHORTCUT
1200 lblRed->setBuddy(rEd);
1202 lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1203#if !defined(QT_SMALL_COLORDIALOG)
1204 gl->addWidget(lblRed, 0, 3);
1205 gl->addWidget(rEd, 0, 4);
1207 gl->addWidget(lblRed, 3, 0);
1208 gl->addWidget(rEd, 4, 0);
1211 gEd =
new QColSpinBox(
this);
1212 lblGreen =
new QLabel(
this);
1213#ifndef QT_NO_SHORTCUT
1214 lblGreen->setBuddy(gEd);
1216 lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1217#if !defined(QT_SMALL_COLORDIALOG)
1218 gl->addWidget(lblGreen, 1, 3);
1219 gl->addWidget(gEd, 1, 4);
1221 gl->addWidget(lblGreen, 3, 1);
1222 gl->addWidget(gEd, 4, 1);
1225 bEd =
new QColSpinBox(
this);
1226 lblBlue =
new QLabel(
this);
1227#ifndef QT_NO_SHORTCUT
1228 lblBlue->setBuddy(bEd);
1230 lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1231#if !defined(QT_SMALL_COLORDIALOG)
1232 gl->addWidget(lblBlue, 2, 3);
1233 gl->addWidget(bEd, 2, 4);
1235 gl->addWidget(lblBlue, 3, 2);
1236 gl->addWidget(bEd, 4, 2);
1239 alphaEd =
new QColSpinBox(
this);
1240 alphaLab =
new QLabel(
this);
1241#ifndef QT_NO_SHORTCUT
1242 alphaLab->setBuddy(alphaEd);
1244 alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1245#if !defined(QT_SMALL_COLORDIALOG)
1246 gl->addWidget(alphaLab, 3, 1, 1, 3);
1247 gl->addWidget(alphaEd, 3, 4);
1249 gl->addWidget(alphaLab, 1, 3, 3, 1);
1250 gl->addWidget(alphaEd, 4, 3);
1254 lblHtml =
new QLabel(
this);
1255 htEd =
new QLineEdit(
this);
1256 htEd->setObjectName(
"qt_colorname_lineedit");
1257#ifndef QT_NO_SHORTCUT
1258 lblHtml->setBuddy(htEd);
1261#if QT_CONFIG(regularexpression)
1262 QRegularExpression regExp(QStringLiteral(
"#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"));
1263 QRegularExpressionValidator *validator =
new QRegularExpressionValidator(regExp,
this);
1264 htEd->setValidator(validator);
1266 htEd->setReadOnly(
true);
1268 htEd->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
1270 lblHtml->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1271#if defined(QT_SMALL_COLORDIALOG)
1272 gl->addWidget(lblHtml, 5, 0);
1273 gl->addWidget(htEd, 5, 1, 1, 2);
1275 gl->addWidget(lblHtml, 5, 1);
1276 gl->addWidget(htEd, 5, 2, 1, 3);
1279 connect(hEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1280 connect(sEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1281 connect(vEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1283 connect(rEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1284 connect(gEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1285 connect(bEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1286 connect(alphaEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1287 connect(htEd, &QLineEdit::textEdited,
this, &
QColorShower::htmlEd);
1302 if (nativeDialogInUse)
1303 return platformColorDialogHelper()->currentColor();
1304 return cs->currentQColor();
1309 lab->setColor(currentColor());
1316 curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
1318 rgb2hsv(currentColor(), hue, sat, val);
1324 htEd->setText(QColor(curCol).name());
1327 emit newCol(currentColor());
1333 rgbOriginal =
false;
1339 c.setHsv(hue, sat, val);
1346 htEd->setText(c.name());
1349 emit newCol(currentColor());
1355 QString t = htEd->text();
1359 if (!t.startsWith(u"#")) {
1361 QSignalBlocker blocker(htEd);
1365 QColor c = QColor::fromString(t);
1369 curCol = qRgba(c.red(), c.green(), c.blue(), currentAlpha());
1370 rgb2hsv(curCol, hue, sat, val);
1381 emit newCol(currentColor());
1390 rgb2hsv(currentColor(), hue, sat, val);
1400 htEd->setText(QColor(rgb).name());
1408 if (h < -1 || (uint)s > 255 || (uint)v > 255)
1411 rgbOriginal =
false;
1412 hue = h; val = v; sat = s;
1414 c.setHsv(hue, sat, val);
1425 htEd->setText(c.name());
1433 lblHue->setText(QColorDialog::tr(
"Hu&e:"));
1434 lblSat->setText(QColorDialog::tr(
"&Sat:"));
1435 lblVal->setText(QColorDialog::tr(
"&Val:"));
1436 lblRed->setText(QColorDialog::tr(
"&Red:"));
1437 lblGreen->setText(QColorDialog::tr(
"&Green:"));
1438 lblBlue->setText(QColorDialog::tr(
"Bl&ue:"));
1439 alphaLab->setText(QColorDialog::tr(
"A&lpha channel:"));
1440 lblHtml->setText(QColorDialog::tr(
"&HTML:"));
1445 QColor oldQColor(curQColor);
1446 curQColor.setRgba(qRgba(qRed(curCol), qGreen(curCol), qBlue(curCol), currentAlpha()));
1447 if (curQColor != oldQColor)
1448 emit currentColorChanged(curQColor);
1454 if (!nativeDialogInUse) {
1464 if (!nativeDialogInUse) {
1466 newColorTypedIn(rgb);
1474 if (
cs->curQColor != color) {
1475 cs->curQColor = color;
1476 emit q->currentColorChanged(color);
1489 QRgb color = col.rgb();
1492 const QRgb *standardColors = QColorDialogOptions::standardColors();
1494 const QRgb *match =
std::find(standardColors, standardColorsEnd, color);
1495 if (match != standardColorsEnd) {
1496 const int index =
int(match - standardColors);
1500 standard->setCurrent(row, column);
1501 standard->setSelected(row, column);
1502 standard->setFocus();
1508 const QRgb *customColors = QColorDialogOptions::customColors();
1510 const QRgb *match =
std::find(customColors, customColorsEnd, color);
1511 if (match != customColorsEnd) {
1512 const int index =
int(match - customColors);
1516 custom->setCurrent(row, column);
1517 custom->setSelected(row, column);
1527 QScreen *screen = QGuiApplication::screenAt(p);
1529 screen = QGuiApplication::primaryScreen();
1530 const QRect screenRect = screen->geometry();
1531 const QPixmap pixmap =
1532 screen->grabWindow(0, p.x() - screenRect.x(), p.y() - screenRect.y(), 1, 1);
1533 const QImage i = pixmap.toImage();
1534 return i.pixel(0, 0);
1540 if (!nativeDialogInUse) {
1542 rgb2hsv(rgb, h, s, v);
1556 setCurrentRgbColor(QColorDialogOptions::customColor(i));
1558 standard->setSelected(-1,-1);
1563 setCurrentRgbColor(QColorDialogOptions::standardColor(r + c * 6));
1565 custom->setSelected(-1,-1);
1572 auto *platformServices = QGuiApplicationPrivate::platformIntegration()->services();
1573 if (platformServices && platformServices->hasCapability(QPlatformServices::Capability::ColorPicking)) {
1574 if (
auto *colorPicker = platformServices->colorPicker(q->windowHandle())) {
1575 q->connect(colorPicker, &QPlatformServiceColorPicker::colorPicked, q,
1576 [q, colorPicker](
const QColor &color) {
1577 colorPicker->deleteLater();
1578 q->setCurrentColor(color);
1580 colorPicker->pickColor();
1585 if (!colorPickingEventFilter)
1586 colorPickingEventFilter =
new QColorPickingEventFilter(
this, q);
1587 q->installEventFilter(colorPickingEventFilter);
1588 QObject::connect(
qApp, &QGuiApplication::applicationStateChanged,
1589 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1591 beforeScreenColorPicking = cs->currentColor();
1593 q->grabMouse(Qt::CrossCursor);
1600 updateTimer->start(30);
1605 dummyTransparentWindow.show();
1609
1610
1611 q->setMouseTracking(
true);
1614 buttons->setDisabled(
true);
1617 const QPoint globalPos = QCursor::pos();
1618 q->setCurrentColor(grabScreenColor(globalPos));
1625 if (lblScreenColorInfo)
1626 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1628 .arg(globalPos.y()));
1635 q->removeEventFilter(colorPickingEventFilter);
1636 QObject::disconnect(
qApp, &QGuiApplication::applicationStateChanged,
1637 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1640 updateTimer->stop();
1641 dummyTransparentWindow.setVisible(
false);
1643 q->releaseKeyboard();
1644 q->setMouseTracking(
false);
1645 lblScreenColorInfo->setText(
"\n"_L1);
1647 buttons->setDisabled(
false);
1655 q->setSizeGripEnabled(
false);
1656 q->setWindowTitle(QColorDialog::tr(
"Select Color"));
1659 nativeDialogInUse = (platformColorDialogHelper() !=
nullptr);
1663 if (!nativeDialogInUse)
1667 dummyTransparentWindow.resize(1, 1);
1668 dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
1671 q->setCurrentColor(initial);
1677 QVBoxLayout *mainLay =
new QVBoxLayout(q);
1679 mainLay->setSizeConstraint(QLayout::SetFixedSize);
1681 QHBoxLayout *topLay =
new QHBoxLayout();
1682 mainLay->addLayout(topLay);
1686#if defined(QT_SMALL_COLORDIALOG)
1687 smallDisplay =
true;
1688 const int lumSpace = 20;
1692 smallDisplay = (QGuiApplication::primaryScreen()->virtualGeometry().width() < 480 || QGuiApplication::primaryScreen()->virtualGeometry().height() < 350);
1693 const int lumSpace = topLay->spacing() / 2;
1697 leftLay =
new QVBoxLayout;
1698 topLay->addLayout(leftLay);
1700 standard =
new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
1701 lblBasicColors =
new QLabel(q);
1702#ifndef QT_NO_SHORTCUT
1703 lblBasicColors->setBuddy(standard);
1705 QObjectPrivate::connect(standard, &QColorWell::selected,
1706 this, &QColorDialogPrivate::newStandard);
1707 leftLay->addWidget(lblBasicColors);
1708 leftLay->addWidget(standard);
1710#if !defined(QT_SMALL_COLORDIALOG)
1712 eyeDropperButton =
new QPushButton();
1713 leftLay->addWidget(eyeDropperButton);
1714 lblScreenColorInfo =
new QLabel(
"\n"_L1);
1715 leftLay->addWidget(lblScreenColorInfo);
1716 QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked,
1717 this, &QColorDialogPrivate::pickScreenColor);
1720 lblScreenColorInfo =
nullptr;
1724 leftLay->addStretch();
1726 custom =
new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
1727 custom->setAcceptDrops(
true);
1729 QObjectPrivate::connect(custom, &QColorWell::selected,
this, &QColorDialogPrivate::newCustom);
1730 QObjectPrivate::connect(custom, &QColorWell::currentChanged,
this, &QColorDialogPrivate::nextCustom);
1732 QObject::connect(custom, &QWellArray::colorChanged, q, [
this] (
int index, QRgb color) {
1733 QColorDialogOptions::setCustomColor(index, color);
1738 lblCustomColors =
new QLabel(q);
1739#ifndef QT_NO_SHORTCUT
1740 lblCustomColors->setBuddy(custom);
1742 leftLay->addWidget(lblCustomColors);
1743 leftLay->addWidget(custom);
1745 addCusBt =
new QPushButton(q);
1746 QObjectPrivate::connect(addCusBt, &QPushButton::clicked,
this, &QColorDialogPrivate::addCustom);
1747 leftLay->addWidget(addCusBt);
1750#if defined(QT_SMALL_COLORDIALOG)
1751 QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size();
1752 pWidth = pHeight = qMin(screenSize.width(), screenSize.height());
1754 if (screenSize.height() > screenSize.width())
1764 QVBoxLayout *rightLay =
new QVBoxLayout;
1765 topLay->addLayout(rightLay);
1767 QHBoxLayout *pickLay =
new QHBoxLayout;
1768 rightLay->addLayout(pickLay);
1770 QVBoxLayout *cLay =
new QVBoxLayout;
1771 pickLay->addLayout(cLay);
1772 cp =
new QColorPicker(q);
1774 cp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
1776#if defined(QT_SMALL_COLORDIALOG)
1779 cLay->addSpacing(lumSpace);
1780 cLay->addWidget(
cp);
1782 cLay->addSpacing(lumSpace);
1784 lp =
new QColorLuminancePicker(q);
1785#if defined(QT_SMALL_COLORDIALOG)
1788 lp->setFixedWidth(20);
1789 pickLay->addSpacing(10);
1790 pickLay->addWidget(
lp);
1791 pickLay->addStretch();
1794 QObject::connect(cp, &QColorPicker::newCol, lp, qOverload<
int,
int>(&QColorLuminancePicker::setCol));
1795 QObjectPrivate::connect(lp, &QColorLuminancePicker::newHsv,
this, &QColorDialogPrivate::newHsv);
1797 rightLay->addStretch();
1799 cs =
new QColorShower(q);
1800 pickLay->setContentsMargins(
cs->gl->contentsMargins());
1801 QObjectPrivate::connect(cs, &QColorShower::newCol,
1802 this, &QColorDialogPrivate::newColorTypedIn);
1803 QObject::connect(cs, &QColorShower::currentColorChanged,
1804 q, &QColorDialog::currentColorChanged);
1805#if defined(QT_SMALL_COLORDIALOG)
1806 topLay->addWidget(cs);
1808 rightLay->addWidget(
cs);
1810 leftLay->addSpacing(cs->gl->contentsMargins().right());
1813 buttons =
new QDialogButtonBox(q);
1814 mainLay->addWidget(buttons);
1816 ok = buttons->addButton(QDialogButtonBox::Ok);
1817 QObject::connect(ok, &QPushButton::clicked, q, &QColorDialog::accept);
1818 ok->setDefault(
true);
1819 cancel = buttons->addButton(QDialogButtonBox::Cancel);
1820 QObject::connect(cancel, &QPushButton::clicked, q, &QColorDialog::reject);
1823 updateTimer =
new QTimer(q);
1824 QObjectPrivate::connect(updateTimer, &QTimer::timeout,
1825 this, qOverload<>(&QColorDialogPrivate::updateColorPicking));
1832 QColorDialog *d = q_func();
1833 auto *colorDialogHelper =
static_cast<QPlatformColorDialogHelper*>(h);
1834 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::currentColorChanged,
1835 d, &QColorDialog::currentColorChanged);
1836 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::colorSelected,
1837 d, &QColorDialog::colorSelected);
1838 colorDialogHelper->setOptions(options);
1843 options->setWindowTitle(q_func()->windowTitle());
1848 QColorDialogOptions::setCustomColor(nextCust, cs->currentColor());
1851 nextCust = (nextCust+1) % QColorDialogOptions::customColorCount();
1856 if (nativeDialogInUse)
1860 lblBasicColors->setText(QColorDialog::tr(
"&Basic colors"));
1861 lblCustomColors->setText(QColorDialog::tr(
"&Custom colors"));
1862 addCusBt->setText(QColorDialog::tr(
"&Add to Custom Colors"));
1863#if !defined(QT_SMALL_COLORDIALOG)
1874 const auto integration = QGuiApplicationPrivate::platformIntegration();
1875 return integration->hasCapability(QPlatformIntegration::ScreenWindowGrabbing)
1876 || integration->services()->hasCapability(QPlatformServices::Capability::ColorPicking);
1883 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
1884 if (nativeDialogInUse)
1886 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
1887 || q->testAttribute(Qt::WA_DontShowOnScreen)
1888 || (options->options() & QColorDialog::DontUseNativeDialog)) {
1892 return strcmp(QColorDialog::staticMetaObject.className(), q->metaObject()->className()) == 0;
1896 Qt::Dialog | Qt::WindowTitleHint
1897 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
1900
1901
1902
1903
1904
1905
1906
1907
1908
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
1939
1940
1941QColorDialog::QColorDialog(QWidget *parent)
1942 : QColorDialog(QColor(Qt::white), parent)
1947
1948
1949
1950QColorDialog::QColorDialog(
const QColor &initial, QWidget *parent)
1951 : QDialog(*
new QColorDialogPrivate, parent, qcd_DefaultWindowFlags)
1959 if (nativeDialogInUse) {
1960 platformColorDialogHelper()->setCurrentColor(color);
1964 if (setColorMode & ShowColor) {
1965 setCurrentRgbColor(color.rgb());
1968 if (setColorMode & SelectColor)
1973
1974
1975
1977void QColorDialog::setCurrentColor(
const QColor &color)
1980 d->setCurrentColor(color);
1983QColor QColorDialog::currentColor()
const
1985 Q_D(
const QColorDialog);
1986 return d->currentQColor();
1990
1991
1992
1993
1994
1995
1996
1997QColor QColorDialog::selectedColor()
const
1999 Q_D(
const QColorDialog);
2000 return d->selectedQColor;
2004
2005
2006
2007
2008
2009void QColorDialog::setOption(ColorDialogOption option,
bool on)
2011 const QColorDialog::ColorDialogOptions previousOptions = options();
2012 if (!(previousOptions & option) != !on)
2013 setOptions(previousOptions ^ option);
2017
2018
2019
2020
2021
2022bool QColorDialog::testOption(ColorDialogOption option)
const
2024 Q_D(
const QColorDialog);
2025 return d->options->testOption(
static_cast<QColorDialogOptions::ColorDialogOption>(option));
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040void QColorDialog::setOptions(ColorDialogOptions options)
2044 if (QColorDialog::options() == options)
2047 d->options->setOptions(QColorDialogOptions::ColorDialogOptions(
int(options)));
2048 if ((options & DontUseNativeDialog) && d->nativeDialogInUse) {
2049 d->nativeDialogInUse =
false;
2052 if (!d->nativeDialogInUse) {
2053 d->buttons->setVisible(!(options & NoButtons));
2054 d->showAlpha(options & ShowAlphaChannel);
2055 if (d->eyeDropperButton)
2056 d->eyeDropperButton->setVisible(!(options & NoEyeDropperButton));
2060QColorDialog::ColorDialogOptions QColorDialog::options()
const
2062 Q_D(
const QColorDialog);
2063 return QColorDialog::ColorDialogOptions(
int(d->options->options()));
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2082
2083
2084
2085
2086
2087
2088
2091
2092
2093
2094
2095
2096
2097
2100
2101
2102
2103void QColorDialog::setVisible(
bool visible)
2106 QDialog::setVisible(visible);
2110
2111
2112
2113
2114
2115
2120 const auto q =
static_cast<QDialog *>(q_ptr);
2123 selectedQColor = QColor();
2125 if (nativeDialogInUse) {
2126 if (setNativeDialogVisible(visible)) {
2129 q->setAttribute(Qt::WA_DontShowOnScreen);
2130 }
else if (visible) {
2134 q->setAttribute(Qt::WA_DontShowOnScreen,
false);
2137 QDialogPrivate::setVisible(visible);
2141
2142
2143
2144
2145
2146void QColorDialog::open(QObject *receiver,
const char *member)
2149 connect(
this, SIGNAL(colorSelected(QColor)), receiver, member);
2150 d->receiverToDisconnectOnClose = receiver;
2151 d->memberToDisconnectOnClose = member;
2156
2157
2158
2159
2160
2161
2162
2163QColor QColorDialog::getColor(
const QColor &initial, QWidget *parent,
const QString &title,
2164 ColorDialogOptions options)
2166 QAutoPointer<QColorDialog> dlg(
new QColorDialog(parent));
2167 if (!title.isEmpty())
2168 dlg->setWindowTitle(title);
2169 dlg->setOptions(options);
2170 dlg->setCurrentColor(initial);
2176 return dlg->selectedColor();
2182
2183
2185QColorDialog::~QColorDialog()
2190
2191
2192void QColorDialog::changeEvent(QEvent *e)
2195 if (e->type() == QEvent::LanguageChange)
2196 d->retranslateStrings();
2197 QDialog::changeEvent(e);
2204 static QPoint lastGlobalPos;
2205 QPoint newGlobalPos = QCursor::pos();
2206 if (lastGlobalPos == newGlobalPos)
2208 lastGlobalPos = newGlobalPos;
2210 if (!q->rect().contains(q->mapFromGlobal(newGlobalPos))) {
2211 updateColorPicking(newGlobalPos);
2213 dummyTransparentWindow.setPosition(newGlobalPos);
2221 const QColor color = grabScreenColor(globalPos);
2224 setCurrentColor(color, ShowColor);
2233 updateColorPicking(e->globalPosition().toPoint());
2239 setCurrentColor(grabScreenColor(e->globalPosition().toPoint()), SetColorAll);
2247#if QT_CONFIG(shortcut)
2248 if (e->matches(QKeySequence::Cancel)) {
2249 releaseColorPicking();
2250 q->setCurrentColor(beforeScreenColorPicking);
2253 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
2254 q->setCurrentColor(grabScreenColor(QCursor::pos()));
2262
2263
2264
2265
2266
2267
2268void QColorDialog::done(
int result)
2271 if (result == Accepted) {
2272 d->selectedQColor = d->currentQColor();
2273 emit colorSelected(d->selectedQColor);
2275 d->selectedQColor = QColor();
2277 QDialog::done(result);
2278 if (d->receiverToDisconnectOnClose) {
2279 disconnect(
this, SIGNAL(colorSelected(QColor)),
2280 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
2281 d->receiverToDisconnectOnClose =
nullptr;
2283 d->memberToDisconnectOnClose.clear();
2288#include "qcolordialog.moc"
2289#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
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.
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