11#if QT_CONFIG(draganddrop)
23#if QT_CONFIG(regularexpression)
24#include <qregularexpression.h>
26#if QT_CONFIG(settings)
41#include "private/qdialog_p.h"
42#include "private/qcolorwell_p.h"
44#include <qpa/qplatformintegration.h>
45#include <qpa/qplatformservices.h>
46#include <private/qguiapplication_p.h>
48#include <QtCore/qpointer.h>
54using namespace Qt::StringLiterals;
70 Q_DECLARE_PUBLIC(QColorDialog)
86 {
return static_cast<QPlatformColorDialogHelper *>(platformHelper()); }
88 void init(
const QColor &initial);
162void QWellArray::paintEvent(QPaintEvent *e)
169 int colfirst = columnAt(cx);
170 int collast = columnAt(cx + cw);
171 int rowfirst = rowAt(cy);
172 int rowlast = rowAt(cy + ch);
174 if (isRightToLeft()) {
180 QPainter painter(
this);
181 QPainter *p = &painter;
182 QRect rect(0, 0, cellWidth(), cellHeight());
185 if (collast < 0 || collast >= ncols)
187 if (rowlast < 0 || rowlast >= nrows)
191 for (
int r = rowfirst; r <= rowlast; ++r) {
198 for (
int c = colfirst; c <= collast; ++c) {
200 int colp = columnX(c);
202 rect.translate(colp, rowp);
203 paintCell(p, r, c, rect);
204 rect.translate(-colp, -rowp);
209QWellArray::QWellArray(
int rows,
int cols, QWidget *parent)
211 ,nrows(rows), ncols(cols)
213 setFocusPolicy(Qt::StrongFocus);
222QSize QWellArray::sizeHint()
const
225 return gridSize().boundedTo(QSize(640, 480));
229void QWellArray::paintCell(QPainter* p,
int row,
int col,
const QRect &rect)
233 const QPalette & g = palette();
234 QStyleOptionFrame opt;
236 int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt,
this);
238 opt.midLineWidth = 1;
239 opt.rect = rect.adjusted(b, b, -b, -b);
241 opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
242 style()->drawPrimitive(QStyle::PE_Frame, &opt, p,
this);
245 if ((row == curRow) && (col == curCol)) {
247 QStyleOptionFocusRect opt;
250 opt.state = QStyle::State_None | QStyle::State_KeyboardFocusChange;
251 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p,
this);
254 paintCellContents(p, row, col, opt.rect.adjusted(dfw, dfw, -dfw, -dfw));
258
259
260void QWellArray::paintCellContents(QPainter *p,
int row,
int col,
const QRect &r)
264 p->fillRect(r, Qt::white);
265 p->setPen(Qt::black);
266 p->drawLine(r.topLeft(), r.bottomRight());
267 p->drawLine(r.topRight(), r.bottomLeft());
270void QWellArray::mousePressEvent(QMouseEvent *e)
273 QPoint pos = e->position().toPoint();
274 setCurrent(rowAt(pos.y()), columnAt(pos.x()));
277void QWellArray::mouseReleaseEvent(QMouseEvent * )
280 setSelected(curRow, curCol);
285
286
287
289void QWellArray::setCurrent(
int row,
int col)
291 if ((curRow == row) && (curCol == col))
294 if (row < 0 || col < 0)
303 updateCell(oldRow, oldCol);
304 updateCell(curRow, curCol);
306 emit currentChanged(curRow, curCol);
310
311
312
313
314
315void QWellArray::setSelected(
int row,
int col)
320 if (row < 0 || col < 0)
326 updateCell(oldRow, oldCol);
327 updateCell(selRow, selCol);
329 emit selected(row, col);
332 if (isVisible() && qobject_cast<QMenu*>(parentWidget()))
333 parentWidget()->close();
337void QWellArray::focusInEvent(QFocusEvent*)
339 updateCell(curRow, curCol);
340 emit currentChanged(curRow, curCol);
344void QWellArray::focusOutEvent(QFocusEvent*)
346 updateCell(curRow, curCol);
349void QWellArray::keyPressEvent(QKeyEvent* e)
354 setCurrent(curRow, curCol - 1);
357 if (curCol < numCols()-1)
358 setCurrent(curRow, curCol + 1);
362 setCurrent(curRow - 1, curCol);
365 if (curRow < numRows()-1)
366 setCurrent(curRow + 1, curCol);
373
374
375
380 setSelected(curRow, curCol);
399 switch (event->type()) {
400 case QEvent::MouseMove:
402 case QEvent::MouseButtonRelease:
404 case QEvent::KeyPress:
414 if (state != Qt::ApplicationActive)
425
426
427
428int QColorDialog::customCount()
430 return QColorDialogOptions::customColorCount();
434
435
436QColor QColorDialog::customColor(
int index)
438 return QColor(QColorDialogOptions::customColor(index));
442
443
444
445
446
447
448void QColorDialog::setCustomColor(
int index, QColor color)
450 QColorDialogOptions::setCustomColor(index, color.rgba());
454
455
456
457
458QColor QColorDialog::standardColor(
int index)
460 return QColor(QColorDialogOptions::standardColor(index));
464
465
466
467
468
469
470void QColorDialog::setStandardColor(
int index, QColor color)
472 QColorDialogOptions::setStandardColor(index, color.rgba());
475static inline void rgb2hsv(QRgb rgb,
int &h,
int &s,
int &v)
479 c.getHsv(&h, &s, &v);
482void QColorWell::paintCellContents(QPainter *p,
int row,
int col,
const QRect &r)
484 int i = row + col*numRows();
485 p->fillRect(r, QColor(values[i]));
488void QColorWell::mousePressEvent(QMouseEvent *e)
490 oldCurrent = QPoint(selectedRow(), selectedColumn());
491 QWellArray::mousePressEvent(e);
493 pressPos = e->position().toPoint();
496void QColorWell::mouseMoveEvent(QMouseEvent *e)
498 QWellArray::mouseMoveEvent(e);
499#if QT_CONFIG(draganddrop)
502 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
503 setCurrent(oldCurrent.x(), oldCurrent.y());
504 int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
505 QColor col(values[i]);
506 QMimeData *mime =
new QMimeData;
507 mime->setColorData(col);
508 QPixmap pix(cellWidth(), cellHeight());
511 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
513 QDrag *drg =
new QDrag(
this);
514 drg->setMimeData(mime);
516 mousePressed =
false;
517 drg->exec(Qt::CopyAction);
522#if QT_CONFIG(draganddrop)
523void QColorWell::dragEnterEvent(QDragEnterEvent *e)
525 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
531void QColorWell::dragLeaveEvent(QDragLeaveEvent *)
534 parentWidget()->setFocus();
537void QColorWell::dragMoveEvent(QDragMoveEvent *e)
539 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) {
540 setCurrent(rowAt(e->position().toPoint().y()), columnAt(e->position().toPoint().x()));
547void QColorWell::dropEvent(QDropEvent *e)
549 QColor col = qvariant_cast<QColor>(e->mimeData()->colorData());
551 int i = rowAt(e->position().toPoint().y()) + columnAt(e->position().toPoint().x()) * numRows();
552 emit colorChanged(i, col.rgb());
561void QColorWell::mouseReleaseEvent(QMouseEvent *e)
565 QWellArray::mouseReleaseEvent(e);
566 mousePressed =
false;
596 QPixmap createColorsPixmap();
597 QPoint colPt(
int hue,
int sat);
598 int huePt(
const QPoint &pt,
const QSize &widgetSize);
599 int huePt(
const QPoint &pt) {
return huePt(pt, size()); }
600 int satPt(
const QPoint &pt,
const QSize &widgetSize);
601 int satPt(
const QPoint &pt) {
return satPt(pt, size()); }
602 void setCol(
const QPoint &pt,
bool notify =
true);
636 enum { foff = 3, coff = 4 };
651 int d = height() - 2*coff - 1;
652 return 255 - (y - coff)*255/d;
657 int d = height() - 2*coff - 1;
658 return coff + (255-v)*d/255;
664 hue = 100; val = 100; sat = 100;
667 setFocusPolicy(Qt::StrongFocus);
677 switch (event->key()) {
679 setVal(
std::clamp(val - 1, 0, 255));
682 setVal(
std::clamp(val + 1, 0, 255));
685 QWidget::keyPressEvent(event);
692 if (m->buttons() == Qt::NoButton) {
696 setVal(y2val(m->position().toPoint().y()));
700 setVal(y2val(m->position().toPoint().y()));
707 val = qMax(0, qMin(v,255));
708 delete pix; pix=
nullptr;
710 emit newHsv(hue, sat, val);
717 emit newHsv(h, s, val);
724 QRect r(0, foff, w, height() - 2*foff);
725 int wi = r.width() - 2;
726 int hi = r.height() - 2;
727 if (!pix || pix->height() != hi || pix->width() != wi) {
729 QImage img(wi, hi, QImage::Format_RGB32);
731 uint *pixel = (uint *) img.scanLine(0);
732 for (y = 0; y < hi; y++) {
733 uint *end = pixel + wi;
734 std::fill(pixel, end, QColor::fromHsv(hue, sat, y2val(y + coff)).rgb());
737 pix =
new QPixmap(QPixmap::fromImage(img));
740 p.drawPixmap(1, coff, *pix);
741 const QPalette &g = palette();
742 qDrawShadePanel(&p, r, g,
true);
743 p.setPen(g.windowText().color());
744 p.setBrush(g.windowText());
745 p.eraseRect(w, 0, 5, height());
746 const int y = val2y(val);
747 const std::array<QPoint, 3> points = {QPoint(w, y), QPoint(w + 5, y + 5), QPoint(w + 5, y - 5)};
748 p.drawPolygon(points.data(),
static_cast<
int>(points.size()));
756 delete pix; pix=
nullptr;
762 QRect r = contentsRect();
763 return QPoint((360 - hue) * (r.width() - 1) / 360, (255 - sat) * (r.height() - 1) / 255);
768 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
769 return std::clamp(360 - pt.x() * 360 / (r.width() - 1), 0, 359);
774 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
775 return std::clamp(255 - pt.y() * 255 / (r.height() - 1), 0, 255);
783 QRect r(m_pos, QSize(20, 20));
784 m_pos.setX(std::clamp(pt.x(), 0, pix.width() - 1));
785 m_pos.setY(std::clamp(pt.y(), 0, pix.height() - 1));
786 r = r.united(QRect(m_pos, QSize(20, 20)));
787 r.translate(contentsRect().x() - 9, contentsRect().y() - 9);
792 emit newCol(huePt(m_pos), satPt(m_pos));
799 setAttribute(Qt::WA_NoSystemBackground);
800 setFocusPolicy(Qt::StrongFocus);
801 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
804 pix = createColorsPixmap();
815 if (crossVisible != visible) {
816 crossVisible = visible;
823 return QSize(
pWidth + 2*frameWidth(),
pHeight + 2*frameWidth());
828 int nhue = qMin(qMax(0,h), 359);
829 int nsat = qMin(qMax(0,s), 255);
830 if (nhue == huePt(m_pos) && nsat == satPt(m_pos))
833 setCol(colPt(nhue, nsat),
false);
838 switch (event->key()) {
840 setCol(m_pos + QPoint(0, 1));
843 setCol(m_pos + QPoint(-1, 0));
846 setCol(m_pos + QPoint(1, 0));
849 setCol(m_pos + QPoint(0, -1));
852 QFrame::keyPressEvent(event);
859 QPoint p = m->position().toPoint() - contentsRect().topLeft();
860 if (m->buttons() == Qt::NoButton) {
869 QPoint p = m->position().toPoint() - contentsRect().topLeft();
877 QRect r = contentsRect();
879 p.drawPixmap(r.topLeft(), pix);
882 QPoint pt = m_pos + r.topLeft();
884 p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
885 p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
891 QFrame::resizeEvent(ev);
893 pix = createColorsPixmap();
895 const QSize &oldSize = ev->oldSize();
896 if (!oldSize.isValid())
901 const int hue = huePt(m_pos, oldSize);
902 const int sat = satPt(m_pos, oldSize);
908 int w = width() - frameWidth() * 2;
909 int h = height() - frameWidth() * 2;
910 QImage img(w, h, QImage::Format_RGB32);
912 uint *pixel = (uint *) img.scanLine(0);
913 for (y = 0; y < h; y++) {
914 const uint *end = pixel + w;
916 while (pixel < end) {
919 c.setHsv(huePt(p), satPt(p), 200);
925 return QPixmap::fromImage(img);
934 const QSignalBlocker blocker(
this);
935 QSpinBox::setValue(i);
951 {
return (colorDialog->options() & QColorDialog::ShowAlphaChannel) ? alphaEd->value() : 255; }
974 void showCurrentColor();
996 QColorDialog *colorDialog;
1023#if QT_CONFIG(draganddrop)
1039 p.fillRect(contentsRect()&e->rect(), col);
1044 alphaLab->setVisible(b);
1045 alphaEd->setVisible(b);
1050 return alphaLab->isVisible();
1055 mousePressed =
true;
1056 pressPos = e->position().toPoint();
1061#if !QT_CONFIG(draganddrop)
1066 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
1067 QMimeData *mime =
new QMimeData;
1068 mime->setColorData(col);
1069 QPixmap pix(30, 20);
1072 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
1074 QDrag *drg =
new QDrag(
this);
1075 drg->setMimeData(mime);
1076 drg->setPixmap(pix);
1077 mousePressed =
false;
1078 drg->exec(Qt::CopyAction);
1083#if QT_CONFIG(draganddrop)
1114 mousePressed =
false;
1120 colorDialog = parent;
1122 curCol = qRgb(255, 255, 255);
1123 curQColor = Qt::white;
1125 gl =
new QGridLayout(
this);
1126 const int s = gl->spacing();
1127 gl->setContentsMargins(s, s, s, s);
1128 lab =
new QColorShowLabel(
this);
1130#ifdef QT_SMALL_COLORDIALOG
1131 lab->setMinimumHeight(60);
1133 lab->setMinimumWidth(60);
1137#if !defined(QT_SMALL_COLORDIALOG)
1138 gl->addWidget(lab, 0, 0, -1, 1);
1140 gl->addWidget(lab, 0, 0, 1, -1);
1142 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::newCol);
1143 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::setRgb);
1145 hEd =
new QColSpinBox(
this);
1146 hEd->setRange(0, 359);
1147 lblHue =
new QLabel(
this);
1148#ifndef QT_NO_SHORTCUT
1149 lblHue->setBuddy(hEd);
1151 lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1152#if !defined(QT_SMALL_COLORDIALOG)
1153 gl->addWidget(lblHue, 0, 1);
1154 gl->addWidget(hEd, 0, 2);
1156 gl->addWidget(lblHue, 1, 0);
1157 gl->addWidget(hEd, 2, 0);
1160 sEd =
new QColSpinBox(
this);
1161 lblSat =
new QLabel(
this);
1162#ifndef QT_NO_SHORTCUT
1163 lblSat->setBuddy(sEd);
1165 lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1166#if !defined(QT_SMALL_COLORDIALOG)
1167 gl->addWidget(lblSat, 1, 1);
1168 gl->addWidget(sEd, 1, 2);
1170 gl->addWidget(lblSat, 1, 1);
1171 gl->addWidget(sEd, 2, 1);
1174 vEd =
new QColSpinBox(
this);
1175 lblVal =
new QLabel(
this);
1176#ifndef QT_NO_SHORTCUT
1177 lblVal->setBuddy(vEd);
1179 lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1180#if !defined(QT_SMALL_COLORDIALOG)
1181 gl->addWidget(lblVal, 2, 1);
1182 gl->addWidget(vEd, 2, 2);
1184 gl->addWidget(lblVal, 1, 2);
1185 gl->addWidget(vEd, 2, 2);
1188 rEd =
new QColSpinBox(
this);
1189 lblRed =
new QLabel(
this);
1190#ifndef QT_NO_SHORTCUT
1191 lblRed->setBuddy(rEd);
1193 lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1194#if !defined(QT_SMALL_COLORDIALOG)
1195 gl->addWidget(lblRed, 0, 3);
1196 gl->addWidget(rEd, 0, 4);
1198 gl->addWidget(lblRed, 3, 0);
1199 gl->addWidget(rEd, 4, 0);
1202 gEd =
new QColSpinBox(
this);
1203 lblGreen =
new QLabel(
this);
1204#ifndef QT_NO_SHORTCUT
1205 lblGreen->setBuddy(gEd);
1207 lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1208#if !defined(QT_SMALL_COLORDIALOG)
1209 gl->addWidget(lblGreen, 1, 3);
1210 gl->addWidget(gEd, 1, 4);
1212 gl->addWidget(lblGreen, 3, 1);
1213 gl->addWidget(gEd, 4, 1);
1216 bEd =
new QColSpinBox(
this);
1217 lblBlue =
new QLabel(
this);
1218#ifndef QT_NO_SHORTCUT
1219 lblBlue->setBuddy(bEd);
1221 lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1222#if !defined(QT_SMALL_COLORDIALOG)
1223 gl->addWidget(lblBlue, 2, 3);
1224 gl->addWidget(bEd, 2, 4);
1226 gl->addWidget(lblBlue, 3, 2);
1227 gl->addWidget(bEd, 4, 2);
1230 alphaEd =
new QColSpinBox(
this);
1231 alphaLab =
new QLabel(
this);
1232#ifndef QT_NO_SHORTCUT
1233 alphaLab->setBuddy(alphaEd);
1235 alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1236#if !defined(QT_SMALL_COLORDIALOG)
1237 gl->addWidget(alphaLab, 3, 1, 1, 3);
1238 gl->addWidget(alphaEd, 3, 4);
1240 gl->addWidget(alphaLab, 1, 3, 3, 1);
1241 gl->addWidget(alphaEd, 4, 3);
1245 lblHtml =
new QLabel(
this);
1246 htEd =
new QLineEdit(
this);
1247 htEd->setObjectName(
"qt_colorname_lineedit");
1248#ifndef QT_NO_SHORTCUT
1249 lblHtml->setBuddy(htEd);
1252#if QT_CONFIG(regularexpression)
1253 QRegularExpression regExp(QStringLiteral(
"#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"));
1254 QRegularExpressionValidator *validator =
new QRegularExpressionValidator(regExp,
this);
1255 htEd->setValidator(validator);
1257 htEd->setReadOnly(
true);
1259 htEd->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
1261 lblHtml->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1262#if defined(QT_SMALL_COLORDIALOG)
1263 gl->addWidget(lblHtml, 5, 0);
1264 gl->addWidget(htEd, 5, 1, 1, 2);
1266 gl->addWidget(lblHtml, 5, 1);
1267 gl->addWidget(htEd, 5, 2, 1, 3);
1270 connect(hEd, &QSpinBox::valueChanged,
this, &QColorShower::hsvEd);
1271 connect(sEd, &QSpinBox::valueChanged,
this, &QColorShower::hsvEd);
1272 connect(vEd, &QSpinBox::valueChanged,
this, &QColorShower::hsvEd);
1274 connect(rEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1275 connect(gEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1276 connect(bEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1277 connect(alphaEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1278 connect(htEd, &QLineEdit::textEdited,
this, &QColorShower::htmlEd);
1293 if (nativeDialogInUse)
1294 return platformColorDialogHelper()->currentColor();
1295 return cs->currentQColor();
1300 lab->setColor(currentColor());
1307 curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
1309 rgb2hsv(currentColor(), hue, sat, val);
1315 htEd->setText(QColor(curCol).name());
1318 emit newCol(currentColor());
1324 rgbOriginal =
false;
1330 c.setHsv(hue, sat, val);
1333 rEd->setValue(qRed(currentColor()));
1334 gEd->setValue(qGreen(currentColor()));
1335 bEd->setValue(qBlue(currentColor()));
1337 htEd->setText(c.name());
1340 emit newCol(currentColor());
1346 QString t = htEd->text();
1350 if (!t.startsWith(u"#")) {
1352 QSignalBlocker blocker(htEd);
1356 QColor c = QColor::fromString(t);
1360 curCol = qRgba(c.red(), c.green(), c.blue(), currentAlpha());
1361 rgb2hsv(curCol, hue, sat, val);
1367 rEd->setValue(qRed(currentColor()));
1368 gEd->setValue(qGreen(currentColor()));
1369 bEd->setValue(qBlue(currentColor()));
1372 emit newCol(currentColor());
1381 rgb2hsv(currentColor(), hue, sat, val);
1387 rEd->setValue(qRed(currentColor()));
1388 gEd->setValue(qGreen(currentColor()));
1389 bEd->setValue(qBlue(currentColor()));
1391 htEd->setText(QColor(rgb).name());
1399 if (h < -1 || (uint)s > 255 || (uint)v > 255)
1402 rgbOriginal =
false;
1403 hue = h; val = v; sat = s;
1405 c.setHsv(hue, sat, val);
1412 rEd->setValue(qRed(currentColor()));
1413 gEd->setValue(qGreen(currentColor()));
1414 bEd->setValue(qBlue(currentColor()));
1416 htEd->setText(c.name());
1424 lblHue->setText(QColorDialog::tr(
"Hu&e:"));
1425 lblSat->setText(QColorDialog::tr(
"&Sat:"));
1426 lblVal->setText(QColorDialog::tr(
"&Val:"));
1427 lblRed->setText(QColorDialog::tr(
"&Red:"));
1428 lblGreen->setText(QColorDialog::tr(
"&Green:"));
1429 lblBlue->setText(QColorDialog::tr(
"Bl&ue:"));
1430 alphaLab->setText(QColorDialog::tr(
"A&lpha channel:"));
1431 lblHtml->setText(QColorDialog::tr(
"&HTML:"));
1436 QColor oldQColor(curQColor);
1437 curQColor.setRgba(qRgba(qRed(curCol), qGreen(curCol), qBlue(curCol), currentAlpha()));
1438 if (curQColor != oldQColor)
1439 emit currentColorChanged(curQColor);
1445 if (!nativeDialogInUse) {
1455 if (!nativeDialogInUse) {
1457 newColorTypedIn(rgb);
1465 if (
cs->curQColor != color) {
1466 cs->curQColor = color;
1467 emit q->currentColorChanged(color);
1480 QRgb color = col.rgb();
1483 const QRgb *standardColors = QColorDialogOptions::standardColors();
1485 const QRgb *match =
std::find(standardColors, standardColorsEnd, color);
1486 if (match != standardColorsEnd) {
1487 const int index =
int(match - standardColors);
1491 standard->setCurrent(row, column);
1492 standard->setSelected(row, column);
1493 standard->setFocus();
1499 const QRgb *customColors = QColorDialogOptions::customColors();
1501 const QRgb *match =
std::find(customColors, customColorsEnd, color);
1502 if (match != customColorsEnd) {
1503 const int index =
int(match - customColors);
1507 custom->setCurrent(row, column);
1508 custom->setSelected(row, column);
1518 QScreen *screen = QGuiApplication::screenAt(p);
1520 screen = QGuiApplication::primaryScreen();
1521 const QRect screenRect = screen->geometry();
1522 const QPixmap pixmap =
1523 screen->grabWindow(0, p.x() - screenRect.x(), p.y() - screenRect.y(), 1, 1);
1524 const QImage i = pixmap.toImage();
1525 return i.pixel(0, 0);
1531 if (!nativeDialogInUse) {
1533 rgb2hsv(rgb, h, s, v);
1547 setCurrentRgbColor(QColorDialogOptions::customColor(i));
1549 standard->setSelected(-1,-1);
1554 setCurrentRgbColor(QColorDialogOptions::standardColor(r + c * 6));
1556 custom->setSelected(-1,-1);
1563 auto *platformServices = QGuiApplicationPrivate::platformIntegration()->services();
1564 if (platformServices && platformServices->hasCapability(QPlatformServices::Capability::ColorPicking)) {
1565 if (
auto *colorPicker = platformServices->colorPicker(q->windowHandle())) {
1566 q->connect(colorPicker, &QPlatformServiceColorPicker::colorPicked, q,
1567 [q, colorPicker](
const QColor &color) {
1568 colorPicker->deleteLater();
1569 q->setCurrentColor(color);
1571 colorPicker->pickColor();
1576 if (!colorPickingEventFilter)
1577 colorPickingEventFilter =
new QColorPickingEventFilter(
this, q);
1578 q->installEventFilter(colorPickingEventFilter);
1579 QObject::connect(
qApp, &QGuiApplication::applicationStateChanged,
1580 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1582 beforeScreenColorPicking = cs->currentColor();
1584 q->grabMouse(Qt::CrossCursor);
1591 updateTimer->start(30);
1596 dummyTransparentWindow.show();
1600
1601
1602 q->setMouseTracking(
true);
1605 buttons->setDisabled(
true);
1608 const QPoint globalPos = QCursor::pos();
1609 q->setCurrentColor(grabScreenColor(globalPos));
1616 if (lblScreenColorInfo)
1617 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1619 .arg(globalPos.y()));
1626 q->removeEventFilter(colorPickingEventFilter);
1627 QObject::disconnect(
qApp, &QGuiApplication::applicationStateChanged,
1628 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1631 updateTimer->stop();
1632 dummyTransparentWindow.setVisible(
false);
1634 q->releaseKeyboard();
1635 q->setMouseTracking(
false);
1636 lblScreenColorInfo->setText(
"\n"_L1);
1638 buttons->setDisabled(
false);
1646 q->setSizeGripEnabled(
false);
1647 q->setWindowTitle(QColorDialog::tr(
"Select Color"));
1650 nativeDialogInUse = (platformColorDialogHelper() !=
nullptr);
1654 if (!nativeDialogInUse)
1658 dummyTransparentWindow.resize(1, 1);
1659 dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
1662 q->setCurrentColor(initial);
1668 QVBoxLayout *mainLay =
new QVBoxLayout(q);
1670 mainLay->setSizeConstraint(QLayout::SetFixedSize);
1672 QHBoxLayout *topLay =
new QHBoxLayout();
1673 mainLay->addLayout(topLay);
1677#if defined(QT_SMALL_COLORDIALOG)
1678 smallDisplay =
true;
1679 const int lumSpace = 20;
1683 smallDisplay = (QGuiApplication::primaryScreen()->virtualGeometry().width() < 480 || QGuiApplication::primaryScreen()->virtualGeometry().height() < 350);
1684 const int lumSpace = topLay->spacing() / 2;
1688 leftLay =
new QVBoxLayout;
1689 topLay->addLayout(leftLay);
1691 standard =
new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
1692 lblBasicColors =
new QLabel(q);
1693#ifndef QT_NO_SHORTCUT
1694 lblBasicColors->setBuddy(standard);
1696 QObjectPrivate::connect(standard, &QColorWell::selected,
1697 this, &QColorDialogPrivate::newStandard);
1698 leftLay->addWidget(lblBasicColors);
1699 leftLay->addWidget(standard);
1701#if !defined(QT_SMALL_COLORDIALOG)
1703 eyeDropperButton =
new QPushButton();
1704 leftLay->addWidget(eyeDropperButton);
1705 lblScreenColorInfo =
new QLabel(
"\n"_L1);
1706 leftLay->addWidget(lblScreenColorInfo);
1707 QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked,
1708 this, &QColorDialogPrivate::pickScreenColor);
1711 lblScreenColorInfo =
nullptr;
1715 leftLay->addStretch();
1717 custom =
new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
1718 custom->setAcceptDrops(
true);
1720 QObjectPrivate::connect(custom, &QColorWell::selected,
this, &QColorDialogPrivate::newCustom);
1721 QObjectPrivate::connect(custom, &QColorWell::currentChanged,
this, &QColorDialogPrivate::nextCustom);
1723 QObject::connect(custom, &QWellArray::colorChanged, q, [
this] (
int index, QRgb color) {
1724 QColorDialogOptions::setCustomColor(index, color);
1729 lblCustomColors =
new QLabel(q);
1730#ifndef QT_NO_SHORTCUT
1731 lblCustomColors->setBuddy(custom);
1733 leftLay->addWidget(lblCustomColors);
1734 leftLay->addWidget(custom);
1736 addCusBt =
new QPushButton(q);
1737 QObjectPrivate::connect(addCusBt, &QPushButton::clicked,
this, &QColorDialogPrivate::addCustom);
1738 leftLay->addWidget(addCusBt);
1741#if defined(QT_SMALL_COLORDIALOG)
1742 QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size();
1743 pWidth = pHeight = qMin(screenSize.width(), screenSize.height());
1745 if (screenSize.height() > screenSize.width())
1755 QVBoxLayout *rightLay =
new QVBoxLayout;
1756 topLay->addLayout(rightLay);
1758 QHBoxLayout *pickLay =
new QHBoxLayout;
1759 rightLay->addLayout(pickLay);
1761 QVBoxLayout *cLay =
new QVBoxLayout;
1762 pickLay->addLayout(cLay);
1763 cp =
new QColorPicker(q);
1765 cp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
1767#if defined(QT_SMALL_COLORDIALOG)
1770 cLay->addSpacing(lumSpace);
1771 cLay->addWidget(
cp);
1773 cLay->addSpacing(lumSpace);
1775 lp =
new QColorLuminancePicker(q);
1776#if defined(QT_SMALL_COLORDIALOG)
1779 lp->setFixedWidth(20);
1780 pickLay->addSpacing(10);
1781 pickLay->addWidget(
lp);
1782 pickLay->addStretch();
1785 QObject::connect(cp, &QColorPicker::newCol, lp, qOverload<
int,
int>(&QColorLuminancePicker::setCol));
1786 QObjectPrivate::connect(lp, &QColorLuminancePicker::newHsv,
this, &QColorDialogPrivate::newHsv);
1788 rightLay->addStretch();
1790 cs =
new QColorShower(q);
1791 pickLay->setContentsMargins(
cs->gl->contentsMargins());
1792 QObjectPrivate::connect(cs, &QColorShower::newCol,
1793 this, &QColorDialogPrivate::newColorTypedIn);
1794 QObject::connect(cs, &QColorShower::currentColorChanged,
1795 q, &QColorDialog::currentColorChanged);
1796#if defined(QT_SMALL_COLORDIALOG)
1797 topLay->addWidget(cs);
1799 rightLay->addWidget(
cs);
1801 leftLay->addSpacing(cs->gl->contentsMargins().right());
1804 buttons =
new QDialogButtonBox(q);
1805 mainLay->addWidget(buttons);
1807 ok = buttons->addButton(QDialogButtonBox::Ok);
1808 QObject::connect(ok, &QPushButton::clicked, q, &QColorDialog::accept);
1809 ok->setDefault(
true);
1810 cancel = buttons->addButton(QDialogButtonBox::Cancel);
1811 QObject::connect(cancel, &QPushButton::clicked, q, &QColorDialog::reject);
1814 updateTimer =
new QTimer(q);
1815 QObjectPrivate::connect(updateTimer, &QTimer::timeout,
1816 this, qOverload<>(&QColorDialogPrivate::updateColorPicking));
1823 QColorDialog *d = q_func();
1824 auto *colorDialogHelper =
static_cast<QPlatformColorDialogHelper*>(h);
1825 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::currentColorChanged,
1826 d, &QColorDialog::currentColorChanged);
1827 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::colorSelected,
1828 d, &QColorDialog::colorSelected);
1829 colorDialogHelper->setOptions(options);
1834 options->setWindowTitle(q_func()->windowTitle());
1839 QColorDialogOptions::setCustomColor(nextCust, cs->currentColor());
1842 nextCust = (nextCust+1) % QColorDialogOptions::customColorCount();
1847 if (nativeDialogInUse)
1851 lblBasicColors->setText(QColorDialog::tr(
"&Basic colors"));
1852 lblCustomColors->setText(QColorDialog::tr(
"&Custom colors"));
1853 addCusBt->setText(QColorDialog::tr(
"&Add to Custom Colors"));
1854#if !defined(QT_SMALL_COLORDIALOG)
1855 if (eyeDropperButton)
1856 eyeDropperButton->setText(QColorDialog::tr(
"&Pick Screen Color"));
1865 const auto integration = QGuiApplicationPrivate::platformIntegration();
1866 return integration->hasCapability(QPlatformIntegration::ScreenWindowGrabbing)
1867 || integration->services()->hasCapability(QPlatformServices::Capability::ColorPicking);
1874 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
1875 if (nativeDialogInUse)
1877 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
1878 || q->testAttribute(Qt::WA_DontShowOnScreen)
1879 || (options->options() & QColorDialog::DontUseNativeDialog)) {
1883 return strcmp(QColorDialog::staticMetaObject.className(), q->metaObject()->className()) == 0;
1887 Qt::Dialog | Qt::WindowTitleHint
1888 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
1891
1892
1893
1894
1895
1896
1897
1898
1899
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
1930
1931
1932QColorDialog::QColorDialog(QWidget *parent)
1933 : QColorDialog(QColor(Qt::white), parent)
1938
1939
1940
1941QColorDialog::QColorDialog(
const QColor &initial, QWidget *parent)
1942 : QDialog(*
new QColorDialogPrivate, parent, qcd_DefaultWindowFlags)
1950 if (nativeDialogInUse) {
1951 platformColorDialogHelper()->setCurrentColor(color);
1955 if (setColorMode & ShowColor) {
1956 setCurrentRgbColor(color.rgb());
1959 if (setColorMode & SelectColor)
1964
1965
1966
1968void QColorDialog::setCurrentColor(
const QColor &color)
1971 d->setCurrentColor(color);
1974QColor QColorDialog::currentColor()
const
1976 Q_D(
const QColorDialog);
1977 return d->currentQColor();
1981
1982
1983
1984
1985
1986
1987
1988QColor QColorDialog::selectedColor()
const
1990 Q_D(
const QColorDialog);
1991 return d->selectedQColor;
1995
1996
1997
1998
1999
2000void QColorDialog::setOption(ColorDialogOption option,
bool on)
2002 const QColorDialog::ColorDialogOptions previousOptions = options();
2003 if (!(previousOptions & option) != !on)
2004 setOptions(previousOptions ^ option);
2008
2009
2010
2011
2012
2013bool QColorDialog::testOption(ColorDialogOption option)
const
2015 Q_D(
const QColorDialog);
2016 return d->options->testOption(
static_cast<QColorDialogOptions::ColorDialogOption>(option));
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031void QColorDialog::setOptions(ColorDialogOptions options)
2035 if (QColorDialog::options() == options)
2038 d->options->setOptions(QColorDialogOptions::ColorDialogOptions(
int(options)));
2039 if ((options & DontUseNativeDialog) && d->nativeDialogInUse) {
2040 d->nativeDialogInUse =
false;
2043 if (!d->nativeDialogInUse) {
2044 d->buttons->setVisible(!(options & NoButtons));
2045 d->showAlpha(options & ShowAlphaChannel);
2046 if (d->eyeDropperButton)
2047 d->eyeDropperButton->setVisible(!(options & NoEyeDropperButton));
2051QColorDialog::ColorDialogOptions QColorDialog::options()
const
2053 Q_D(
const QColorDialog);
2054 return QColorDialog::ColorDialogOptions(
int(d->options->options()));
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2073
2074
2075
2076
2077
2078
2079
2082
2083
2084
2085
2086
2087
2088
2091
2092
2093
2094void QColorDialog::setVisible(
bool visible)
2097 QDialog::setVisible(visible);
2101
2102
2103
2104
2105
2106
2111 const auto q =
static_cast<QDialog *>(q_ptr);
2114 selectedQColor = QColor();
2116 if (nativeDialogInUse) {
2117 if (setNativeDialogVisible(visible)) {
2120 q->setAttribute(Qt::WA_DontShowOnScreen);
2121 }
else if (visible) {
2125 q->setAttribute(Qt::WA_DontShowOnScreen,
false);
2128 QDialogPrivate::setVisible(visible);
2132
2133
2134
2135
2136
2137void QColorDialog::open(QObject *receiver,
const char *member)
2140 connect(
this, SIGNAL(colorSelected(QColor)), receiver, member);
2141 d->receiverToDisconnectOnClose = receiver;
2142 d->memberToDisconnectOnClose = member;
2147
2148
2149
2150
2151
2152
2153
2154QColor QColorDialog::getColor(
const QColor &initial, QWidget *parent,
const QString &title,
2155 ColorDialogOptions options)
2157 QAutoPointer<QColorDialog> dlg(
new QColorDialog(parent));
2158 if (!title.isEmpty())
2159 dlg->setWindowTitle(title);
2160 dlg->setOptions(options);
2161 dlg->setCurrentColor(initial);
2167 return dlg->selectedColor();
2173
2174
2176QColorDialog::~QColorDialog()
2181
2182
2183void QColorDialog::changeEvent(QEvent *e)
2186 if (e->type() == QEvent::LanguageChange)
2187 d->retranslateStrings();
2188 QDialog::changeEvent(e);
2195 static QPoint lastGlobalPos;
2196 QPoint newGlobalPos = QCursor::pos();
2197 if (lastGlobalPos == newGlobalPos)
2199 lastGlobalPos = newGlobalPos;
2201 if (!q->rect().contains(q->mapFromGlobal(newGlobalPos))) {
2202 updateColorPicking(newGlobalPos);
2204 dummyTransparentWindow.setPosition(newGlobalPos);
2212 const QColor color = grabScreenColor(globalPos);
2215 setCurrentColor(color, ShowColor);
2224 updateColorPicking(e->globalPosition().toPoint());
2230 setCurrentColor(grabScreenColor(e->globalPosition().toPoint()), SetColorAll);
2238#if QT_CONFIG(shortcut)
2239 if (e->matches(QKeySequence::Cancel)) {
2240 releaseColorPicking();
2241 q->setCurrentColor(beforeScreenColorPicking);
2244 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
2245 q->setCurrentColor(grabScreenColor(QCursor::pos()));
2253
2254
2255
2256
2257
2258
2259void QColorDialog::done(
int result)
2262 if (result == Accepted) {
2263 d->selectedQColor = d->currentQColor();
2264 emit colorSelected(d->selectedQColor);
2266 d->selectedQColor = QColor();
2268 QDialog::done(result);
2269 if (d->receiverToDisconnectOnClose) {
2270 disconnect(
this, SIGNAL(colorSelected(QColor)),
2271 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
2272 d->receiverToDisconnectOnClose =
nullptr;
2274 d->memberToDisconnectOnClose.clear();
2279#include "qcolordialog.moc"
2280#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