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);
378
379
380
385 setSelected(curRow, curCol);
393void QWellArray::sendAccessibleChildFocusEvent()
395#if QT_CONFIG(accessibility)
396 if (!QAccessible::isActive())
399 if (hasFocus() && curRow >= 0 && curCol >= 0) {
400 const int itemIndex = index(curRow, curCol);
401 QAccessibleEvent event(
this, QAccessible::Focus);
402 event.setChild(itemIndex);
403 QAccessible::updateAccessibility(&event);
419 switch (event->type()) {
420 case QEvent::MouseMove:
422 case QEvent::MouseButtonRelease:
424 case QEvent::KeyPress:
434 if (state != Qt::ApplicationActive)
445
446
447
448int QColorDialog::customCount()
450 return QColorDialogOptions::customColorCount();
454
455
456QColor QColorDialog::customColor(
int index)
458 return QColor(QColorDialogOptions::customColor(index));
462
463
464
465
466
467
468void QColorDialog::setCustomColor(
int index, QColor color)
470 QColorDialogOptions::setCustomColor(index, color.rgba());
474
475
476
477
478QColor QColorDialog::standardColor(
int index)
480 return QColor(QColorDialogOptions::standardColor(index));
484
485
486
487
488
489
490void QColorDialog::setStandardColor(
int index, QColor color)
492 QColorDialogOptions::setStandardColor(index, color.rgba());
495static inline void rgb2hsv(QRgb rgb,
int &h,
int &s,
int &v)
499 c.getHsv(&h, &s, &v);
502void QColorWell::paintCellContents(QPainter *p,
int row,
int col,
const QRect &r)
504 int i = row + col*numRows();
505 p->fillRect(r, QColor(values[i]));
508void QColorWell::mousePressEvent(QMouseEvent *e)
510 oldCurrent = QPoint(selectedRow(), selectedColumn());
511 QWellArray::mousePressEvent(e);
513 pressPos = e->position().toPoint();
516void QColorWell::mouseMoveEvent(QMouseEvent *e)
518 QWellArray::mouseMoveEvent(e);
519#if QT_CONFIG(draganddrop)
522 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
523 setCurrent(oldCurrent.x(), oldCurrent.y());
524 int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
525 QColor col(values[i]);
526 QMimeData *mime =
new QMimeData;
527 mime->setColorData(col);
528 QPixmap pix(cellWidth(), cellHeight());
531 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
533 QDrag *drg =
new QDrag(
this);
534 drg->setMimeData(mime);
536 mousePressed =
false;
537 drg->exec(Qt::CopyAction);
542#if QT_CONFIG(draganddrop)
543void QColorWell::dragEnterEvent(QDragEnterEvent *e)
545 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid())
551void QColorWell::dragLeaveEvent(QDragLeaveEvent *)
554 parentWidget()->setFocus();
557void QColorWell::dragMoveEvent(QDragMoveEvent *e)
559 if (qvariant_cast<QColor>(e->mimeData()->colorData()).isValid()) {
560 setCurrent(rowAt(e->position().toPoint().y()), columnAt(e->position().toPoint().x()));
567void QColorWell::dropEvent(QDropEvent *e)
569 QColor col = qvariant_cast<QColor>(e->mimeData()->colorData());
571 int i = rowAt(e->position().toPoint().y()) + columnAt(e->position().toPoint().x()) * numRows();
572 emit colorChanged(i, col.rgb());
581void QColorWell::mouseReleaseEvent(QMouseEvent *e)
585 QWellArray::mouseReleaseEvent(e);
586 mousePressed =
false;
616 QPixmap createColorsPixmap();
617 QPoint colPt(
int hue,
int sat);
618 int huePt(
const QPoint &pt,
const QSize &widgetSize);
619 int huePt(
const QPoint &pt) {
return huePt(pt, size()); }
620 int satPt(
const QPoint &pt,
const QSize &widgetSize);
621 int satPt(
const QPoint &pt) {
return satPt(pt, size()); }
622 void setCol(
const QPoint &pt,
bool notify =
true);
656 enum { foff = 3, coff = 4 };
671 int d = height() - 2*coff - 1;
672 return 255 - (y - coff)*255/d;
677 int d = height() - 2*coff - 1;
678 return coff + (255-v)*d/255;
684 hue = 100; val = 100; sat = 100;
686 setFocusPolicy(Qt::StrongFocus);
695 switch (event->key()) {
697 setVal(
std::clamp(val - 1, 0, 255));
700 setVal(
std::clamp(val + 1, 0, 255));
703 QWidget::keyPressEvent(event);
710 if (m->buttons() == Qt::NoButton) {
714 setVal(y2val(m->position().toPoint().y()));
718 setVal(y2val(m->position().toPoint().y()));
725 val = qMax(0, qMin(v,255));
728 emit newHsv(hue, sat, val);
735 emit newHsv(h, s, val);
742 QRect r(0, foff, w, height() - 2*foff);
743 int wi = r.width() - 2;
744 int hi = r.height() - 2;
745 if (pix.isNull() || pix.height() != hi || pix.width() != wi) {
746 QImage img(wi, hi, QImage::Format_RGB32);
748 uint *pixel = (uint *) img.scanLine(0);
749 for (y = 0; y < hi; y++) {
750 uint *end = pixel + wi;
751 std::fill(pixel, end, QColor::fromHsv(hue, sat, y2val(y + coff)).rgb());
754 pix = QPixmap::fromImage(img);
757 p.drawPixmap(1, coff, pix);
758 const QPalette &g = palette();
759 qDrawShadePanel(&p, r, g,
true);
760 p.setPen(g.windowText().color());
761 p.setBrush(g.windowText());
762 p.eraseRect(w, 0, 5, height());
763 const int y = val2y(val);
764 const std::array<QPoint, 3> points = {QPoint(w, y), QPoint(w + 5, y + 5), QPoint(w + 5, y - 5)};
765 p.drawPolygon(points.data(),
static_cast<
int>(points.size()));
779 QRect r = contentsRect();
780 return QPoint((360 - hue) * (r.width() - 1) / 360, (255 - sat) * (r.height() - 1) / 255);
785 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
786 return std::clamp(360 - pt.x() * 360 / (r.width() - 1), 0, 359);
791 QRect r = QRect(QPoint(0, 0), widgetSize) - contentsMargins();
792 return std::clamp(255 - pt.y() * 255 / (r.height() - 1), 0, 255);
800 QRect r(m_pos, QSize(20, 20));
801 m_pos.setX(std::clamp(pt.x(), 0, pix.width() - 1));
802 m_pos.setY(std::clamp(pt.y(), 0, pix.height() - 1));
803 r = r.united(QRect(m_pos, QSize(20, 20)));
804 r.translate(contentsRect().x() - 9, contentsRect().y() - 9);
809 emit newCol(huePt(m_pos), satPt(m_pos));
816 setAttribute(Qt::WA_NoSystemBackground);
817 setFocusPolicy(Qt::StrongFocus);
818 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
821 pix = createColorsPixmap();
832 if (crossVisible != visible) {
833 crossVisible = visible;
840 return QSize(
pWidth + 2*frameWidth(),
pHeight + 2*frameWidth());
845 int nhue = qMin(qMax(0,h), 359);
846 int nsat = qMin(qMax(0,s), 255);
847 if (nhue == huePt(m_pos) && nsat == satPt(m_pos))
850 setCol(colPt(nhue, nsat),
false);
855 switch (event->key()) {
857 setCol(m_pos + QPoint(0, 1));
860 setCol(m_pos + QPoint(-1, 0));
863 setCol(m_pos + QPoint(1, 0));
866 setCol(m_pos + QPoint(0, -1));
869 QFrame::keyPressEvent(event);
876 QPoint p = m->position().toPoint() - contentsRect().topLeft();
877 if (m->buttons() == Qt::NoButton) {
886 QPoint p = m->position().toPoint() - contentsRect().topLeft();
894 QRect r = contentsRect();
896 p.drawPixmap(r.topLeft(), pix);
899 QPoint pt = m_pos + r.topLeft();
901 p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
902 p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
908 QFrame::resizeEvent(ev);
910 pix = createColorsPixmap();
912 const QSize &oldSize = ev->oldSize();
913 if (!oldSize.isValid())
918 const int hue = huePt(m_pos, oldSize);
919 const int sat = satPt(m_pos, oldSize);
925 int w = width() - frameWidth() * 2;
926 int h = height() - frameWidth() * 2;
927 QImage img(w, h, QImage::Format_RGB32);
929 uint *pixel = (uint *) img.scanLine(0);
930 for (y = 0; y < h; y++) {
931 const uint *end = pixel + w;
933 while (pixel < end) {
936 c.setHsv(huePt(p), satPt(p), 200);
942 return QPixmap::fromImage(img);
951 const QSignalBlocker blocker(
this);
952 QSpinBox::setValue(i);
968 {
return (colorDialog->options() & QColorDialog::ShowAlphaChannel) ? alphaEd->value() : 255; }
991 void showCurrentColor();
1013 QColorDialog *colorDialog;
1040#if QT_CONFIG(draganddrop)
1056 p.fillRect(contentsRect()&e->rect(), col);
1061 alphaLab->setVisible(b);
1062 alphaEd->setVisible(b);
1067 return alphaLab->isVisible();
1072 mousePressed =
true;
1073 pressPos = e->position().toPoint();
1078#if !QT_CONFIG(draganddrop)
1083 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
1084 QMimeData *mime =
new QMimeData;
1085 mime->setColorData(col);
1086 QPixmap pix(30, 20);
1089 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
1091 QDrag *drg =
new QDrag(
this);
1092 drg->setMimeData(mime);
1093 drg->setPixmap(pix);
1094 mousePressed =
false;
1095 drg->exec(Qt::CopyAction);
1100#if QT_CONFIG(draganddrop)
1131 mousePressed =
false;
1137 colorDialog = parent;
1139 curCol = qRgb(255, 255, 255);
1140 curQColor = Qt::white;
1142 gl =
new QGridLayout(
this);
1143 const int s = gl->spacing();
1144 gl->setContentsMargins(s, s, s, s);
1145 lab =
new QColorShowLabel(
this);
1147#ifdef QT_SMALL_COLORDIALOG
1148 lab->setMinimumHeight(60);
1150 lab->setMinimumWidth(60);
1154#if !defined(QT_SMALL_COLORDIALOG)
1155 gl->addWidget(lab, 0, 0, -1, 1);
1157 gl->addWidget(lab, 0, 0, 1, -1);
1159 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::newCol);
1160 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::setRgb);
1162 hEd =
new QColSpinBox(
this);
1163 hEd->setRange(0, 359);
1164 lblHue =
new QLabel(
this);
1165#ifndef QT_NO_SHORTCUT
1166 lblHue->setBuddy(hEd);
1168 lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1169#if !defined(QT_SMALL_COLORDIALOG)
1170 gl->addWidget(lblHue, 0, 1);
1171 gl->addWidget(hEd, 0, 2);
1173 gl->addWidget(lblHue, 1, 0);
1174 gl->addWidget(hEd, 2, 0);
1177 sEd =
new QColSpinBox(
this);
1178 lblSat =
new QLabel(
this);
1179#ifndef QT_NO_SHORTCUT
1180 lblSat->setBuddy(sEd);
1182 lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1183#if !defined(QT_SMALL_COLORDIALOG)
1184 gl->addWidget(lblSat, 1, 1);
1185 gl->addWidget(sEd, 1, 2);
1187 gl->addWidget(lblSat, 1, 1);
1188 gl->addWidget(sEd, 2, 1);
1191 vEd =
new QColSpinBox(
this);
1192 lblVal =
new QLabel(
this);
1193#ifndef QT_NO_SHORTCUT
1194 lblVal->setBuddy(vEd);
1196 lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1197#if !defined(QT_SMALL_COLORDIALOG)
1198 gl->addWidget(lblVal, 2, 1);
1199 gl->addWidget(vEd, 2, 2);
1201 gl->addWidget(lblVal, 1, 2);
1202 gl->addWidget(vEd, 2, 2);
1205 rEd =
new QColSpinBox(
this);
1206 lblRed =
new QLabel(
this);
1207#ifndef QT_NO_SHORTCUT
1208 lblRed->setBuddy(rEd);
1210 lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1211#if !defined(QT_SMALL_COLORDIALOG)
1212 gl->addWidget(lblRed, 0, 3);
1213 gl->addWidget(rEd, 0, 4);
1215 gl->addWidget(lblRed, 3, 0);
1216 gl->addWidget(rEd, 4, 0);
1219 gEd =
new QColSpinBox(
this);
1220 lblGreen =
new QLabel(
this);
1221#ifndef QT_NO_SHORTCUT
1222 lblGreen->setBuddy(gEd);
1224 lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1225#if !defined(QT_SMALL_COLORDIALOG)
1226 gl->addWidget(lblGreen, 1, 3);
1227 gl->addWidget(gEd, 1, 4);
1229 gl->addWidget(lblGreen, 3, 1);
1230 gl->addWidget(gEd, 4, 1);
1233 bEd =
new QColSpinBox(
this);
1234 lblBlue =
new QLabel(
this);
1235#ifndef QT_NO_SHORTCUT
1236 lblBlue->setBuddy(bEd);
1238 lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1239#if !defined(QT_SMALL_COLORDIALOG)
1240 gl->addWidget(lblBlue, 2, 3);
1241 gl->addWidget(bEd, 2, 4);
1243 gl->addWidget(lblBlue, 3, 2);
1244 gl->addWidget(bEd, 4, 2);
1247 alphaEd =
new QColSpinBox(
this);
1248 alphaLab =
new QLabel(
this);
1249#ifndef QT_NO_SHORTCUT
1250 alphaLab->setBuddy(alphaEd);
1252 alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1253#if !defined(QT_SMALL_COLORDIALOG)
1254 gl->addWidget(alphaLab, 3, 1, 1, 3);
1255 gl->addWidget(alphaEd, 3, 4);
1257 gl->addWidget(alphaLab, 1, 3, 3, 1);
1258 gl->addWidget(alphaEd, 4, 3);
1262 lblHtml =
new QLabel(
this);
1263 htEd =
new QLineEdit(
this);
1264 htEd->setObjectName(
"qt_colorname_lineedit");
1265#ifndef QT_NO_SHORTCUT
1266 lblHtml->setBuddy(htEd);
1269#if QT_CONFIG(regularexpression)
1270 QRegularExpression regExp(QStringLiteral(
"#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"));
1271 QRegularExpressionValidator *validator =
new QRegularExpressionValidator(regExp,
this);
1272 htEd->setValidator(validator);
1274 htEd->setReadOnly(
true);
1276 htEd->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
1278 lblHtml->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1279#if defined(QT_SMALL_COLORDIALOG)
1280 gl->addWidget(lblHtml, 5, 0);
1281 gl->addWidget(htEd, 5, 1, 1, 2);
1283 gl->addWidget(lblHtml, 5, 1);
1284 gl->addWidget(htEd, 5, 2, 1, 3);
1287 connect(hEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1288 connect(sEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1289 connect(vEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1291 connect(rEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1292 connect(gEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1293 connect(bEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1294 connect(alphaEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1295 connect(htEd, &QLineEdit::textEdited,
this, &
QColorShower::htmlEd);
1310 if (nativeDialogInUse)
1311 return platformColorDialogHelper()->currentColor();
1312 return cs->currentQColor();
1317 lab->setColor(currentColor());
1324 curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
1326 rgb2hsv(currentColor(), hue, sat, val);
1332 htEd->setText(QColor(curCol).name());
1335 emit newCol(currentColor());
1341 rgbOriginal =
false;
1347 c.setHsv(hue, sat, val);
1354 htEd->setText(c.name());
1357 emit newCol(currentColor());
1363 QString t = htEd->text();
1367 if (!t.startsWith(u"#")) {
1369 QSignalBlocker blocker(htEd);
1373 QColor c = QColor::fromString(t);
1377 curCol = qRgba(c.red(), c.green(), c.blue(), currentAlpha());
1378 rgb2hsv(curCol, hue, sat, val);
1389 emit newCol(currentColor());
1398 rgb2hsv(currentColor(), hue, sat, val);
1408 htEd->setText(QColor(rgb).name());
1416 if (h < -1 || (uint)s > 255 || (uint)v > 255)
1419 rgbOriginal =
false;
1420 hue = h; val = v; sat = s;
1422 c.setHsv(hue, sat, val);
1433 htEd->setText(c.name());
1441 lblHue->setText(QColorDialog::tr(
"Hu&e:"));
1442 lblSat->setText(QColorDialog::tr(
"&Sat:"));
1443 lblVal->setText(QColorDialog::tr(
"&Val:"));
1444 lblRed->setText(QColorDialog::tr(
"&Red:"));
1445 lblGreen->setText(QColorDialog::tr(
"&Green:"));
1446 lblBlue->setText(QColorDialog::tr(
"Bl&ue:"));
1447 alphaLab->setText(QColorDialog::tr(
"A&lpha channel:"));
1448 lblHtml->setText(QColorDialog::tr(
"&HTML:"));
1453 QColor oldQColor(curQColor);
1454 curQColor.setRgba(qRgba(qRed(curCol), qGreen(curCol), qBlue(curCol), currentAlpha()));
1455 if (curQColor != oldQColor)
1456 emit currentColorChanged(curQColor);
1462 if (!nativeDialogInUse) {
1472 if (!nativeDialogInUse) {
1474 newColorTypedIn(rgb);
1482 if (
cs->curQColor != color) {
1483 cs->curQColor = color;
1484 emit q->currentColorChanged(color);
1497 QRgb color = col.rgb();
1500 const QRgb *standardColors = QColorDialogOptions::standardColors();
1502 const QRgb *match =
std::find(standardColors, standardColorsEnd, color);
1503 if (match != standardColorsEnd) {
1504 const int index =
int(match - standardColors);
1508 standard->setCurrent(row, column);
1509 standard->setSelected(row, column);
1510 standard->setFocus();
1516 const QRgb *customColors = QColorDialogOptions::customColors();
1518 const QRgb *match =
std::find(customColors, customColorsEnd, color);
1519 if (match != customColorsEnd) {
1520 const int index =
int(match - customColors);
1524 custom->setCurrent(row, column);
1525 custom->setSelected(row, column);
1535 QScreen *screen = QGuiApplication::screenAt(p);
1537 screen = QGuiApplication::primaryScreen();
1538 const QRect screenRect = screen->geometry();
1539 const QPixmap pixmap =
1540 screen->grabWindow(0, p.x() - screenRect.x(), p.y() - screenRect.y(), 1, 1);
1541 const QImage i = pixmap.toImage();
1542 return i.pixel(0, 0);
1548 if (!nativeDialogInUse) {
1550 rgb2hsv(rgb, h, s, v);
1564 setCurrentRgbColor(QColorDialogOptions::customColor(i));
1566 standard->setSelected(-1,-1);
1571 setCurrentRgbColor(QColorDialogOptions::standardColor(r + c * 6));
1573 custom->setSelected(-1,-1);
1580 auto *platformServices = QGuiApplicationPrivate::platformIntegration()->services();
1581 if (platformServices && platformServices->hasCapability(QPlatformServices::Capability::ColorPicking)) {
1582 if (
auto *colorPicker = platformServices->colorPicker(q->windowHandle())) {
1583 q->connect(colorPicker, &QPlatformServiceColorPicker::colorPicked, q,
1584 [q, colorPicker](
const QColor &color) {
1585 colorPicker->deleteLater();
1586 q->setCurrentColor(color);
1588 colorPicker->pickColor();
1593 if (!colorPickingEventFilter)
1594 colorPickingEventFilter =
new QColorPickingEventFilter(
this, q);
1595 q->installEventFilter(colorPickingEventFilter);
1596 QObject::connect(
qApp, &QGuiApplication::applicationStateChanged,
1597 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1599 beforeScreenColorPicking = cs->currentColor();
1601 q->grabMouse(Qt::CrossCursor);
1608 updateTimer->start(30);
1613 dummyTransparentWindow.show();
1617
1618
1619 q->setMouseTracking(
true);
1622 buttons->setDisabled(
true);
1625 const QPoint globalPos = QCursor::pos();
1626 q->setCurrentColor(grabScreenColor(globalPos));
1633 if (lblScreenColorInfo)
1634 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1636 .arg(globalPos.y()));
1643 q->removeEventFilter(colorPickingEventFilter);
1644 QObject::disconnect(
qApp, &QGuiApplication::applicationStateChanged,
1645 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1648 updateTimer->stop();
1649 dummyTransparentWindow.setVisible(
false);
1651 q->releaseKeyboard();
1652 q->setMouseTracking(
false);
1653 lblScreenColorInfo->setText(
"\n"_L1);
1655 buttons->setDisabled(
false);
1663 q->setSizeGripEnabled(
false);
1664 q->setWindowTitle(QColorDialog::tr(
"Select Color"));
1667 nativeDialogInUse = (platformColorDialogHelper() !=
nullptr);
1671 if (!nativeDialogInUse)
1675 dummyTransparentWindow.resize(1, 1);
1676 dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
1679 q->setCurrentColor(initial);
1685 QVBoxLayout *mainLay =
new QVBoxLayout(q);
1687 mainLay->setSizeConstraint(QLayout::SetFixedSize);
1689 QHBoxLayout *topLay =
new QHBoxLayout();
1690 mainLay->addLayout(topLay);
1694#if defined(QT_SMALL_COLORDIALOG)
1695 smallDisplay =
true;
1696 const int lumSpace = 20;
1700 smallDisplay = (QGuiApplication::primaryScreen()->virtualGeometry().width() < 480 || QGuiApplication::primaryScreen()->virtualGeometry().height() < 350);
1701 const int lumSpace = topLay->spacing() / 2;
1705 leftLay =
new QVBoxLayout;
1706 topLay->addLayout(leftLay);
1708 standard =
new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
1709 lblBasicColors =
new QLabel(q);
1710#ifndef QT_NO_SHORTCUT
1711 lblBasicColors->setBuddy(standard);
1713 QObjectPrivate::connect(standard, &QColorWell::selected,
1714 this, &QColorDialogPrivate::newStandard);
1715 leftLay->addWidget(lblBasicColors);
1716 leftLay->addWidget(standard);
1718#if !defined(QT_SMALL_COLORDIALOG)
1720 eyeDropperButton =
new QPushButton();
1721 leftLay->addWidget(eyeDropperButton);
1722 lblScreenColorInfo =
new QLabel(
"\n"_L1);
1723 leftLay->addWidget(lblScreenColorInfo);
1724 QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked,
1725 this, &QColorDialogPrivate::pickScreenColor);
1728 lblScreenColorInfo =
nullptr;
1732 leftLay->addStretch();
1734 custom =
new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
1735 custom->setAcceptDrops(
true);
1737 QObjectPrivate::connect(custom, &QColorWell::selected,
this, &QColorDialogPrivate::newCustom);
1738 QObjectPrivate::connect(custom, &QColorWell::currentChanged,
this, &QColorDialogPrivate::nextCustom);
1740 QObject::connect(custom, &QWellArray::colorChanged, q, [
this] (
int index, QRgb color) {
1741 QColorDialogOptions::setCustomColor(index, color);
1746 lblCustomColors =
new QLabel(q);
1747#ifndef QT_NO_SHORTCUT
1748 lblCustomColors->setBuddy(custom);
1750 leftLay->addWidget(lblCustomColors);
1751 leftLay->addWidget(custom);
1753 addCusBt =
new QPushButton(q);
1754 QObjectPrivate::connect(addCusBt, &QPushButton::clicked,
this, &QColorDialogPrivate::addCustom);
1755 leftLay->addWidget(addCusBt);
1758#if defined(QT_SMALL_COLORDIALOG)
1759 QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size();
1760 pWidth = pHeight = qMin(screenSize.width(), screenSize.height());
1762 if (screenSize.height() > screenSize.width())
1772 QVBoxLayout *rightLay =
new QVBoxLayout;
1773 topLay->addLayout(rightLay);
1775 QHBoxLayout *pickLay =
new QHBoxLayout;
1776 rightLay->addLayout(pickLay);
1778 QVBoxLayout *cLay =
new QVBoxLayout;
1779 pickLay->addLayout(cLay);
1780 cp =
new QColorPicker(q);
1782 cp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
1784#if defined(QT_SMALL_COLORDIALOG)
1787 cLay->addSpacing(lumSpace);
1788 cLay->addWidget(
cp);
1790 cLay->addSpacing(lumSpace);
1792 lp =
new QColorLuminancePicker(q);
1793#if defined(QT_SMALL_COLORDIALOG)
1796 lp->setFixedWidth(20);
1797 pickLay->addSpacing(10);
1798 pickLay->addWidget(
lp);
1799 pickLay->addStretch();
1802 QObject::connect(cp, &QColorPicker::newCol, lp, qOverload<
int,
int>(&QColorLuminancePicker::setCol));
1803 QObjectPrivate::connect(lp, &QColorLuminancePicker::newHsv,
this, &QColorDialogPrivate::newHsv);
1805 rightLay->addStretch();
1807 cs =
new QColorShower(q);
1808 pickLay->setContentsMargins(
cs->gl->contentsMargins());
1809 QObjectPrivate::connect(cs, &QColorShower::newCol,
1810 this, &QColorDialogPrivate::newColorTypedIn);
1811 QObject::connect(cs, &QColorShower::currentColorChanged,
1812 q, &QColorDialog::currentColorChanged);
1813#if defined(QT_SMALL_COLORDIALOG)
1814 topLay->addWidget(cs);
1816 rightLay->addWidget(
cs);
1818 leftLay->addSpacing(cs->gl->contentsMargins().right());
1821 buttons =
new QDialogButtonBox(q);
1822 mainLay->addWidget(buttons);
1824 ok = buttons->addButton(QDialogButtonBox::Ok);
1825 QObject::connect(ok, &QPushButton::clicked, q, &QColorDialog::accept);
1826 ok->setDefault(
true);
1827 cancel = buttons->addButton(QDialogButtonBox::Cancel);
1828 QObject::connect(cancel, &QPushButton::clicked, q, &QColorDialog::reject);
1831 updateTimer =
new QTimer(q);
1832 QObjectPrivate::connect(updateTimer, &QTimer::timeout,
1833 this, qOverload<>(&QColorDialogPrivate::updateColorPicking));
1840 QColorDialog *d = q_func();
1841 auto *colorDialogHelper =
static_cast<QPlatformColorDialogHelper*>(h);
1842 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::currentColorChanged,
1843 d, &QColorDialog::currentColorChanged);
1844 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::colorSelected,
1845 d, &QColorDialog::colorSelected);
1846 colorDialogHelper->setOptions(options);
1851 options->setWindowTitle(q_func()->windowTitle());
1856 QColorDialogOptions::setCustomColor(nextCust, cs->currentColor());
1859 nextCust = (nextCust+1) % QColorDialogOptions::customColorCount();
1864 if (nativeDialogInUse)
1868 lblBasicColors->setText(QColorDialog::tr(
"&Basic colors"));
1869 lblCustomColors->setText(QColorDialog::tr(
"&Custom colors"));
1870 addCusBt->setText(QColorDialog::tr(
"&Add to Custom Colors"));
1871#if !defined(QT_SMALL_COLORDIALOG)
1882 const auto integration = QGuiApplicationPrivate::platformIntegration();
1883 return integration->hasCapability(QPlatformIntegration::ScreenWindowGrabbing)
1884 || integration->services()->hasCapability(QPlatformServices::Capability::ColorPicking);
1891 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
1892 if (nativeDialogInUse)
1894 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
1895 || q->testAttribute(Qt::WA_DontShowOnScreen)
1896 || (options->options() & QColorDialog::DontUseNativeDialog)) {
1900 return strcmp(QColorDialog::staticMetaObject.className(), q->metaObject()->className()) == 0;
1904 Qt::Dialog | Qt::WindowTitleHint
1905 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
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
1937
1938
1939
1940
1941
1942
1943
1944
1947
1948
1949QColorDialog::QColorDialog(QWidget *parent)
1950 : QColorDialog(QColor(Qt::white), parent)
1955
1956
1957
1958QColorDialog::QColorDialog(
const QColor &initial, QWidget *parent)
1959 : QDialog(*
new QColorDialogPrivate, parent, qcd_DefaultWindowFlags)
1967 if (nativeDialogInUse) {
1968 platformColorDialogHelper()->setCurrentColor(color);
1972 if (setColorMode & ShowColor) {
1973 setCurrentRgbColor(color.rgb());
1976 if (setColorMode & SelectColor)
1981
1982
1983
1985void QColorDialog::setCurrentColor(
const QColor &color)
1988 d->setCurrentColor(color);
1991QColor QColorDialog::currentColor()
const
1993 Q_D(
const QColorDialog);
1994 return d->currentQColor();
1998
1999
2000
2001
2002
2003
2004
2005QColor QColorDialog::selectedColor()
const
2007 Q_D(
const QColorDialog);
2008 return d->selectedQColor;
2012
2013
2014
2015
2016
2017void QColorDialog::setOption(ColorDialogOption option,
bool on)
2019 const QColorDialog::ColorDialogOptions previousOptions = options();
2020 if (!(previousOptions & option) != !on)
2021 setOptions(previousOptions ^ option);
2025
2026
2027
2028
2029
2030bool QColorDialog::testOption(ColorDialogOption option)
const
2032 Q_D(
const QColorDialog);
2033 return d->options->testOption(
static_cast<QColorDialogOptions::ColorDialogOption>(option));
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048void QColorDialog::setOptions(ColorDialogOptions options)
2052 if (QColorDialog::options() == options)
2055 d->options->setOptions(QColorDialogOptions::ColorDialogOptions(
int(options)));
2056 if ((options & DontUseNativeDialog) && d->nativeDialogInUse) {
2057 d->nativeDialogInUse =
false;
2060 if (!d->nativeDialogInUse) {
2061 d->buttons->setVisible(!(options & NoButtons));
2062 d->showAlpha(options & ShowAlphaChannel);
2063 if (d->eyeDropperButton)
2064 d->eyeDropperButton->setVisible(!(options & NoEyeDropperButton));
2068QColorDialog::ColorDialogOptions QColorDialog::options()
const
2070 Q_D(
const QColorDialog);
2071 return QColorDialog::ColorDialogOptions(
int(d->options->options()));
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2090
2091
2092
2093
2094
2095
2096
2099
2100
2101
2102
2103
2104
2105
2108
2109
2110
2111void QColorDialog::setVisible(
bool visible)
2114 QDialog::setVisible(visible);
2118
2119
2120
2121
2122
2123
2128 const auto q =
static_cast<QDialog *>(q_ptr);
2131 selectedQColor = QColor();
2133 if (nativeDialogInUse) {
2134 if (setNativeDialogVisible(visible)) {
2137 q->setAttribute(Qt::WA_DontShowOnScreen);
2138 }
else if (visible) {
2142 q->setAttribute(Qt::WA_DontShowOnScreen,
false);
2145 QDialogPrivate::setVisible(visible);
2149
2150
2151
2152
2153
2154void QColorDialog::open(QObject *receiver,
const char *member)
2157 connect(
this, SIGNAL(colorSelected(QColor)), receiver, member);
2158 d->receiverToDisconnectOnClose = receiver;
2159 d->memberToDisconnectOnClose = member;
2164
2165
2166
2167
2168
2169
2170
2171QColor QColorDialog::getColor(
const QColor &initial, QWidget *parent,
const QString &title,
2172 ColorDialogOptions options)
2174 QAutoPointer<QColorDialog> dlg(
new QColorDialog(parent));
2175 if (!title.isEmpty())
2176 dlg->setWindowTitle(title);
2177 dlg->setOptions(options);
2178 dlg->setCurrentColor(initial);
2184 return dlg->selectedColor();
2190
2191
2193QColorDialog::~QColorDialog()
2198
2199
2200void QColorDialog::changeEvent(QEvent *e)
2203 if (e->type() == QEvent::LanguageChange)
2204 d->retranslateStrings();
2205 QDialog::changeEvent(e);
2212 static QPoint lastGlobalPos;
2213 QPoint newGlobalPos = QCursor::pos();
2214 if (lastGlobalPos == newGlobalPos)
2216 lastGlobalPos = newGlobalPos;
2218 if (!q->rect().contains(q->mapFromGlobal(newGlobalPos))) {
2219 updateColorPicking(newGlobalPos);
2221 dummyTransparentWindow.setPosition(newGlobalPos);
2229 const QColor color = grabScreenColor(globalPos);
2232 setCurrentColor(color, ShowColor);
2241 updateColorPicking(e->globalPosition().toPoint());
2247 setCurrentColor(grabScreenColor(e->globalPosition().toPoint()), SetColorAll);
2255#if QT_CONFIG(shortcut)
2256 if (e->matches(QKeySequence::Cancel)) {
2257 releaseColorPicking();
2258 q->setCurrentColor(beforeScreenColorPicking);
2261 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
2262 q->setCurrentColor(grabScreenColor(QCursor::pos()));
2270
2271
2272
2273
2274
2275
2276void QColorDialog::done(
int result)
2279 if (result == Accepted) {
2280 d->selectedQColor = d->currentQColor();
2281 emit colorSelected(d->selectedQColor);
2283 d->selectedQColor = QColor();
2285 QDialog::done(result);
2286 if (d->receiverToDisconnectOnClose) {
2287 disconnect(
this, SIGNAL(colorSelected(QColor)),
2288 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
2289 d->receiverToDisconnectOnClose =
nullptr;
2291 d->memberToDisconnectOnClose.clear();
2296#include "qcolordialog.moc"
2297#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