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);
797 if (pt == m_pos || pix.isNull())
800 Q_ASSERT(pix.height());
801 Q_ASSERT(pix.width());
803 QRect r(m_pos, QSize(20, 20));
804 m_pos.setX(std::clamp(pt.x(), 0, pix.width() - 1));
805 m_pos.setY(std::clamp(pt.y(), 0, pix.height() - 1));
806 r = r.united(QRect(m_pos, QSize(20, 20)));
807 r.translate(contentsRect().x() - 9, contentsRect().y() - 9);
812 emit newCol(huePt(m_pos), satPt(m_pos));
819 setAttribute(Qt::WA_NoSystemBackground);
820 setFocusPolicy(Qt::StrongFocus);
821 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
824 pix = createColorsPixmap();
835 if (crossVisible != visible) {
836 crossVisible = visible;
843 return QSize(
pWidth + 2*frameWidth(),
pHeight + 2*frameWidth());
848 int nhue = qMin(qMax(0,h), 359);
849 int nsat = qMin(qMax(0,s), 255);
850 if (nhue == huePt(m_pos) && nsat == satPt(m_pos))
853 setCol(colPt(nhue, nsat),
false);
858 switch (event->key()) {
860 setCol(m_pos + QPoint(0, 1));
863 setCol(m_pos + QPoint(-1, 0));
866 setCol(m_pos + QPoint(1, 0));
869 setCol(m_pos + QPoint(0, -1));
872 QFrame::keyPressEvent(event);
879 QPoint p = m->position().toPoint() - contentsRect().topLeft();
880 if (m->buttons() == Qt::NoButton) {
889 QPoint p = m->position().toPoint() - contentsRect().topLeft();
897 QRect r = contentsRect();
899 p.drawPixmap(r.topLeft(), pix);
902 QPoint pt = m_pos + r.topLeft();
904 p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
905 p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
911 QFrame::resizeEvent(ev);
913 pix = createColorsPixmap();
915 const QSize &oldSize = ev->oldSize();
916 if (!oldSize.isValid())
921 const int hue = huePt(m_pos, oldSize);
922 const int sat = satPt(m_pos, oldSize);
928 int w = width() - frameWidth() * 2;
929 int h = height() - frameWidth() * 2;
930 QImage img(w, h, QImage::Format_RGB32);
932 uint *pixel = (uint *) img.scanLine(0);
933 for (y = 0; y < h; y++) {
934 const uint *end = pixel + w;
936 while (pixel < end) {
939 c.setHsv(huePt(p), satPt(p), 200);
945 return QPixmap::fromImage(img);
954 const QSignalBlocker blocker(
this);
955 QSpinBox::setValue(i);
971 {
return (colorDialog->options() & QColorDialog::ShowAlphaChannel) ? alphaEd->value() : 255; }
994 void showCurrentColor();
1016 QColorDialog *colorDialog;
1043#if QT_CONFIG(draganddrop)
1059 p.fillRect(contentsRect()&e->rect(), col);
1064 alphaLab->setVisible(b);
1065 alphaEd->setVisible(b);
1070 return alphaLab->isVisible();
1075 mousePressed =
true;
1076 pressPos = e->position().toPoint();
1081#if !QT_CONFIG(draganddrop)
1086 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
1087 QMimeData *mime =
new QMimeData;
1088 mime->setColorData(col);
1089 QPixmap pix(30, 20);
1092 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
1094 QDrag *drg =
new QDrag(
this);
1095 drg->setMimeData(mime);
1096 drg->setPixmap(pix);
1097 mousePressed =
false;
1098 drg->exec(Qt::CopyAction);
1103#if QT_CONFIG(draganddrop)
1134 mousePressed =
false;
1140 colorDialog = parent;
1142 curCol = qRgb(255, 255, 255);
1143 curQColor = Qt::white;
1145 gl =
new QGridLayout(
this);
1146 const int s = gl->spacing();
1147 gl->setContentsMargins(s, s, s, s);
1148 lab =
new QColorShowLabel(
this);
1150#ifdef QT_SMALL_COLORDIALOG
1151 lab->setMinimumHeight(60);
1153 lab->setMinimumWidth(60);
1157#if !defined(QT_SMALL_COLORDIALOG)
1158 gl->addWidget(lab, 0, 0, -1, 1);
1160 gl->addWidget(lab, 0, 0, 1, -1);
1162 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::newCol);
1163 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::setRgb);
1165 hEd =
new QColSpinBox(
this);
1166 hEd->setRange(0, 359);
1167 lblHue =
new QLabel(
this);
1168#ifndef QT_NO_SHORTCUT
1169 lblHue->setBuddy(hEd);
1171 lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1172#if !defined(QT_SMALL_COLORDIALOG)
1173 gl->addWidget(lblHue, 0, 1);
1174 gl->addWidget(hEd, 0, 2);
1176 gl->addWidget(lblHue, 1, 0);
1177 gl->addWidget(hEd, 2, 0);
1180 sEd =
new QColSpinBox(
this);
1181 lblSat =
new QLabel(
this);
1182#ifndef QT_NO_SHORTCUT
1183 lblSat->setBuddy(sEd);
1185 lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1186#if !defined(QT_SMALL_COLORDIALOG)
1187 gl->addWidget(lblSat, 1, 1);
1188 gl->addWidget(sEd, 1, 2);
1190 gl->addWidget(lblSat, 1, 1);
1191 gl->addWidget(sEd, 2, 1);
1194 vEd =
new QColSpinBox(
this);
1195 lblVal =
new QLabel(
this);
1196#ifndef QT_NO_SHORTCUT
1197 lblVal->setBuddy(vEd);
1199 lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1200#if !defined(QT_SMALL_COLORDIALOG)
1201 gl->addWidget(lblVal, 2, 1);
1202 gl->addWidget(vEd, 2, 2);
1204 gl->addWidget(lblVal, 1, 2);
1205 gl->addWidget(vEd, 2, 2);
1208 rEd =
new QColSpinBox(
this);
1209 lblRed =
new QLabel(
this);
1210#ifndef QT_NO_SHORTCUT
1211 lblRed->setBuddy(rEd);
1213 lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1214#if !defined(QT_SMALL_COLORDIALOG)
1215 gl->addWidget(lblRed, 0, 3);
1216 gl->addWidget(rEd, 0, 4);
1218 gl->addWidget(lblRed, 3, 0);
1219 gl->addWidget(rEd, 4, 0);
1222 gEd =
new QColSpinBox(
this);
1223 lblGreen =
new QLabel(
this);
1224#ifndef QT_NO_SHORTCUT
1225 lblGreen->setBuddy(gEd);
1227 lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1228#if !defined(QT_SMALL_COLORDIALOG)
1229 gl->addWidget(lblGreen, 1, 3);
1230 gl->addWidget(gEd, 1, 4);
1232 gl->addWidget(lblGreen, 3, 1);
1233 gl->addWidget(gEd, 4, 1);
1236 bEd =
new QColSpinBox(
this);
1237 lblBlue =
new QLabel(
this);
1238#ifndef QT_NO_SHORTCUT
1239 lblBlue->setBuddy(bEd);
1241 lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1242#if !defined(QT_SMALL_COLORDIALOG)
1243 gl->addWidget(lblBlue, 2, 3);
1244 gl->addWidget(bEd, 2, 4);
1246 gl->addWidget(lblBlue, 3, 2);
1247 gl->addWidget(bEd, 4, 2);
1250 alphaEd =
new QColSpinBox(
this);
1251 alphaLab =
new QLabel(
this);
1252#ifndef QT_NO_SHORTCUT
1253 alphaLab->setBuddy(alphaEd);
1255 alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1256#if !defined(QT_SMALL_COLORDIALOG)
1257 gl->addWidget(alphaLab, 3, 1, 1, 3);
1258 gl->addWidget(alphaEd, 3, 4);
1260 gl->addWidget(alphaLab, 1, 3, 3, 1);
1261 gl->addWidget(alphaEd, 4, 3);
1265 lblHtml =
new QLabel(
this);
1266 htEd =
new QLineEdit(
this);
1267 htEd->setObjectName(
"qt_colorname_lineedit");
1268#ifndef QT_NO_SHORTCUT
1269 lblHtml->setBuddy(htEd);
1272#if QT_CONFIG(regularexpression)
1273 QRegularExpression regExp(QStringLiteral(
"#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"));
1274 QRegularExpressionValidator *validator =
new QRegularExpressionValidator(regExp,
this);
1275 htEd->setValidator(validator);
1277 htEd->setReadOnly(
true);
1279 htEd->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
1281 lblHtml->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1282#if defined(QT_SMALL_COLORDIALOG)
1283 gl->addWidget(lblHtml, 5, 0);
1284 gl->addWidget(htEd, 5, 1, 1, 2);
1286 gl->addWidget(lblHtml, 5, 1);
1287 gl->addWidget(htEd, 5, 2, 1, 3);
1290 connect(hEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1291 connect(sEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1292 connect(vEd, &QSpinBox::valueChanged,
this, &
QColorShower::hsvEd);
1294 connect(rEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1295 connect(gEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1296 connect(bEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1297 connect(alphaEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1298 connect(htEd, &QLineEdit::textEdited,
this, &
QColorShower::htmlEd);
1313 if (nativeDialogInUse)
1314 return platformColorDialogHelper()->currentColor();
1315 return cs->currentQColor();
1320 lab->setColor(currentColor());
1327 curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
1329 rgb2hsv(currentColor(), hue, sat, val);
1335 htEd->setText(QColor(curCol).name());
1338 emit newCol(currentColor());
1344 rgbOriginal =
false;
1350 c.setHsv(hue, sat, val);
1357 htEd->setText(c.name());
1360 emit newCol(currentColor());
1366 QString t = htEd->text();
1370 if (!t.startsWith(u"#")) {
1372 QSignalBlocker blocker(htEd);
1376 QColor c = QColor::fromString(t);
1380 curCol = qRgba(c.red(), c.green(), c.blue(), currentAlpha());
1381 rgb2hsv(curCol, hue, sat, val);
1392 emit newCol(currentColor());
1401 rgb2hsv(currentColor(), hue, sat, val);
1411 htEd->setText(QColor(rgb).name());
1419 if (h < -1 || (uint)s > 255 || (uint)v > 255)
1422 rgbOriginal =
false;
1423 hue = h; val = v; sat = s;
1425 c.setHsv(hue, sat, val);
1436 htEd->setText(c.name());
1444 lblHue->setText(QColorDialog::tr(
"Hu&e:"));
1445 lblSat->setText(QColorDialog::tr(
"&Sat:"));
1446 lblVal->setText(QColorDialog::tr(
"&Val:"));
1447 lblRed->setText(QColorDialog::tr(
"&Red:"));
1448 lblGreen->setText(QColorDialog::tr(
"&Green:"));
1449 lblBlue->setText(QColorDialog::tr(
"Bl&ue:"));
1450 alphaLab->setText(QColorDialog::tr(
"A&lpha channel:"));
1451 lblHtml->setText(QColorDialog::tr(
"&HTML:"));
1456 QColor oldQColor(curQColor);
1457 curQColor.setRgba(qRgba(qRed(curCol), qGreen(curCol), qBlue(curCol), currentAlpha()));
1458 if (curQColor != oldQColor)
1459 emit currentColorChanged(curQColor);
1465 if (!nativeDialogInUse) {
1475 if (!nativeDialogInUse) {
1477 newColorTypedIn(rgb);
1485 if (
cs->curQColor != color) {
1486 cs->curQColor = color;
1487 emit q->currentColorChanged(color);
1500 QRgb color = col.rgb();
1503 const QRgb *standardColors = QColorDialogOptions::standardColors();
1505 const QRgb *match =
std::find(standardColors, standardColorsEnd, color);
1506 if (match != standardColorsEnd) {
1507 const int index =
int(match - standardColors);
1511 standard->setCurrent(row, column);
1512 standard->setSelected(row, column);
1513 standard->setFocus();
1519 const QRgb *customColors = QColorDialogOptions::customColors();
1521 const QRgb *match =
std::find(customColors, customColorsEnd, color);
1522 if (match != customColorsEnd) {
1523 const int index =
int(match - customColors);
1527 custom->setCurrent(row, column);
1528 custom->setSelected(row, column);
1538 QScreen *screen = QGuiApplication::screenAt(p);
1540 screen = QGuiApplication::primaryScreen();
1541 const QRect screenRect = screen->geometry();
1542 const QPixmap pixmap =
1543 screen->grabWindow(0, p.x() - screenRect.x(), p.y() - screenRect.y(), 1, 1);
1544 const QImage i = pixmap.toImage();
1545 return i.pixel(0, 0);
1551 if (!nativeDialogInUse) {
1553 rgb2hsv(rgb, h, s, v);
1567 setCurrentRgbColor(QColorDialogOptions::customColor(i));
1569 standard->setSelected(-1,-1);
1574 setCurrentRgbColor(QColorDialogOptions::standardColor(r + c * 6));
1576 custom->setSelected(-1,-1);
1583 auto *platformServices = QGuiApplicationPrivate::platformIntegration()->services();
1584 if (platformServices && platformServices->hasCapability(QPlatformServices::Capability::ColorPicking)) {
1585 if (
auto *colorPicker = platformServices->colorPicker(q->windowHandle())) {
1586 q->connect(colorPicker, &QPlatformServiceColorPicker::colorPicked, q,
1587 [q, colorPicker](
const QColor &color) {
1588 colorPicker->deleteLater();
1589 q->setCurrentColor(color);
1591 colorPicker->pickColor();
1596 if (!colorPickingEventFilter)
1597 colorPickingEventFilter =
new QColorPickingEventFilter(
this, q);
1598 q->installEventFilter(colorPickingEventFilter);
1599 QObject::connect(
qApp, &QGuiApplication::applicationStateChanged,
1600 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1602 beforeScreenColorPicking = cs->currentColor();
1604 q->grabMouse(Qt::CrossCursor);
1611 updateTimer->start(30);
1616 dummyTransparentWindow.show();
1620
1621
1622 q->setMouseTracking(
true);
1625 buttons->setDisabled(
true);
1628 const QPoint globalPos = QCursor::pos();
1629 q->setCurrentColor(grabScreenColor(globalPos));
1636 if (lblScreenColorInfo)
1637 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1639 .arg(globalPos.y()));
1646 q->removeEventFilter(colorPickingEventFilter);
1647 QObject::disconnect(
qApp, &QGuiApplication::applicationStateChanged,
1648 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1651 updateTimer->stop();
1652 dummyTransparentWindow.setVisible(
false);
1654 q->releaseKeyboard();
1655 q->setMouseTracking(
false);
1656 lblScreenColorInfo->setText(
"\n"_L1);
1658 buttons->setDisabled(
false);
1666 q->setSizeGripEnabled(
false);
1667 q->setWindowTitle(QColorDialog::tr(
"Select Color"));
1670 nativeDialogInUse = (platformColorDialogHelper() !=
nullptr);
1674 if (!nativeDialogInUse)
1678 dummyTransparentWindow.resize(1, 1);
1679 dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
1682 q->setCurrentColor(initial);
1688 QVBoxLayout *mainLay =
new QVBoxLayout(q);
1690 mainLay->setSizeConstraint(QLayout::SetFixedSize);
1692 QHBoxLayout *topLay =
new QHBoxLayout();
1693 mainLay->addLayout(topLay);
1697#if defined(QT_SMALL_COLORDIALOG)
1698 smallDisplay =
true;
1699 const int lumSpace = 20;
1703 smallDisplay = (QGuiApplication::primaryScreen()->virtualGeometry().width() < 480 || QGuiApplication::primaryScreen()->virtualGeometry().height() < 350);
1704 const int lumSpace = topLay->spacing() / 2;
1708 leftLay =
new QVBoxLayout;
1709 topLay->addLayout(leftLay);
1711 standard =
new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
1712 lblBasicColors =
new QLabel(q);
1713#ifndef QT_NO_SHORTCUT
1714 lblBasicColors->setBuddy(standard);
1716 QObjectPrivate::connect(standard, &QColorWell::selected,
1717 this, &QColorDialogPrivate::newStandard);
1718 leftLay->addWidget(lblBasicColors);
1719 leftLay->addWidget(standard);
1721#if !defined(QT_SMALL_COLORDIALOG)
1723 eyeDropperButton =
new QPushButton();
1724 leftLay->addWidget(eyeDropperButton);
1725 lblScreenColorInfo =
new QLabel(
"\n"_L1);
1726 leftLay->addWidget(lblScreenColorInfo);
1727 QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked,
1728 this, &QColorDialogPrivate::pickScreenColor);
1731 lblScreenColorInfo =
nullptr;
1735 leftLay->addStretch();
1737 custom =
new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
1738 custom->setAcceptDrops(
true);
1740 QObjectPrivate::connect(custom, &QColorWell::selected,
this, &QColorDialogPrivate::newCustom);
1741 QObjectPrivate::connect(custom, &QColorWell::currentChanged,
this, &QColorDialogPrivate::nextCustom);
1743 QObject::connect(custom, &QWellArray::colorChanged, q, [
this] (
int index, QRgb color) {
1744 QColorDialogOptions::setCustomColor(index, color);
1749 lblCustomColors =
new QLabel(q);
1750#ifndef QT_NO_SHORTCUT
1751 lblCustomColors->setBuddy(custom);
1753 leftLay->addWidget(lblCustomColors);
1754 leftLay->addWidget(custom);
1756 addCusBt =
new QPushButton(q);
1757 QObjectPrivate::connect(addCusBt, &QPushButton::clicked,
this, &QColorDialogPrivate::addCustom);
1758 leftLay->addWidget(addCusBt);
1761#if defined(QT_SMALL_COLORDIALOG)
1762 QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size();
1763 pWidth = pHeight = qMin(screenSize.width(), screenSize.height());
1765 if (screenSize.height() > screenSize.width())
1775 QVBoxLayout *rightLay =
new QVBoxLayout;
1776 topLay->addLayout(rightLay);
1778 QHBoxLayout *pickLay =
new QHBoxLayout;
1779 rightLay->addLayout(pickLay);
1781 QVBoxLayout *cLay =
new QVBoxLayout;
1782 pickLay->addLayout(cLay);
1783 cp =
new QColorPicker(q);
1785 cp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
1787#if defined(QT_SMALL_COLORDIALOG)
1790 cLay->addSpacing(lumSpace);
1791 cLay->addWidget(
cp);
1793 cLay->addSpacing(lumSpace);
1795 lp =
new QColorLuminancePicker(q);
1796#if defined(QT_SMALL_COLORDIALOG)
1799 lp->setFixedWidth(20);
1800 pickLay->addSpacing(10);
1801 pickLay->addWidget(
lp);
1802 pickLay->addStretch();
1805 QObject::connect(cp, &QColorPicker::newCol, lp, qOverload<
int,
int>(&QColorLuminancePicker::setCol));
1806 QObjectPrivate::connect(lp, &QColorLuminancePicker::newHsv,
this, &QColorDialogPrivate::newHsv);
1808 rightLay->addStretch();
1810 cs =
new QColorShower(q);
1811 pickLay->setContentsMargins(
cs->gl->contentsMargins());
1812 QObjectPrivate::connect(cs, &QColorShower::newCol,
1813 this, &QColorDialogPrivate::newColorTypedIn);
1814 QObject::connect(cs, &QColorShower::currentColorChanged,
1815 q, &QColorDialog::currentColorChanged);
1816#if defined(QT_SMALL_COLORDIALOG)
1817 topLay->addWidget(cs);
1819 rightLay->addWidget(
cs);
1821 leftLay->addSpacing(cs->gl->contentsMargins().right());
1824 buttons =
new QDialogButtonBox(q);
1825 mainLay->addWidget(buttons);
1827 ok = buttons->addButton(QDialogButtonBox::Ok);
1828 QObject::connect(ok, &QPushButton::clicked, q, &QColorDialog::accept);
1829 ok->setDefault(
true);
1830 cancel = buttons->addButton(QDialogButtonBox::Cancel);
1831 QObject::connect(cancel, &QPushButton::clicked, q, &QColorDialog::reject);
1834 updateTimer =
new QTimer(q);
1835 QObjectPrivate::connect(updateTimer, &QTimer::timeout,
1836 this, qOverload<>(&QColorDialogPrivate::updateColorPicking));
1843 QColorDialog *d = q_func();
1844 auto *colorDialogHelper =
static_cast<QPlatformColorDialogHelper*>(h);
1845 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::currentColorChanged,
1846 d, &QColorDialog::currentColorChanged);
1847 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::colorSelected,
1848 d, &QColorDialog::colorSelected);
1849 colorDialogHelper->setOptions(options);
1854 options->setWindowTitle(q_func()->windowTitle());
1859 QColorDialogOptions::setCustomColor(nextCust, cs->currentColor());
1862 nextCust = (nextCust+1) % QColorDialogOptions::customColorCount();
1867 if (nativeDialogInUse)
1871 lblBasicColors->setText(QColorDialog::tr(
"&Basic colors"));
1872 lblCustomColors->setText(QColorDialog::tr(
"&Custom colors"));
1873 addCusBt->setText(QColorDialog::tr(
"&Add to Custom Colors"));
1874#if !defined(QT_SMALL_COLORDIALOG)
1885 const auto integration = QGuiApplicationPrivate::platformIntegration();
1886 return integration->hasCapability(QPlatformIntegration::ScreenWindowGrabbing)
1887 || integration->services()->hasCapability(QPlatformServices::Capability::ColorPicking);
1894 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
1895 if (nativeDialogInUse)
1897 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
1898 || q->testAttribute(Qt::WA_DontShowOnScreen)
1899 || (options->options() & QColorDialog::DontUseNativeDialog)) {
1903 return strcmp(QColorDialog::staticMetaObject.className(), q->metaObject()->className()) == 0;
1907 Qt::Dialog | Qt::WindowTitleHint
1908 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
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
1945
1946
1947
1950
1951
1952QColorDialog::QColorDialog(QWidget *parent)
1953 : QColorDialog(QColor(Qt::white), parent)
1958
1959
1960
1961QColorDialog::QColorDialog(
const QColor &initial, QWidget *parent)
1962 : QDialog(*
new QColorDialogPrivate, parent, qcd_DefaultWindowFlags)
1970 if (nativeDialogInUse) {
1971 platformColorDialogHelper()->setCurrentColor(color);
1975 if (setColorMode & ShowColor) {
1976 setCurrentRgbColor(color.rgb());
1979 if (setColorMode & SelectColor)
1984
1985
1986
1988void QColorDialog::setCurrentColor(
const QColor &color)
1991 d->setCurrentColor(color);
1994QColor QColorDialog::currentColor()
const
1996 Q_D(
const QColorDialog);
1997 return d->currentQColor();
2001
2002
2003
2004
2005
2006
2007
2008QColor QColorDialog::selectedColor()
const
2010 Q_D(
const QColorDialog);
2011 return d->selectedQColor;
2015
2016
2017
2018
2019
2020void QColorDialog::setOption(ColorDialogOption option,
bool on)
2022 const QColorDialog::ColorDialogOptions previousOptions = options();
2023 if (!(previousOptions & option) != !on)
2024 setOptions(previousOptions ^ option);
2028
2029
2030
2031
2032
2033bool QColorDialog::testOption(ColorDialogOption option)
const
2035 Q_D(
const QColorDialog);
2036 return d->options->testOption(
static_cast<QColorDialogOptions::ColorDialogOption>(option));
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051void QColorDialog::setOptions(ColorDialogOptions options)
2055 if (QColorDialog::options() == options)
2058 d->options->setOptions(QColorDialogOptions::ColorDialogOptions(
int(options)));
2059 if ((options & DontUseNativeDialog) && d->nativeDialogInUse) {
2060 d->nativeDialogInUse =
false;
2063 if (!d->nativeDialogInUse) {
2064 d->buttons->setVisible(!(options & NoButtons));
2065 d->showAlpha(options & ShowAlphaChannel);
2066 if (d->eyeDropperButton)
2067 d->eyeDropperButton->setVisible(!(options & NoEyeDropperButton));
2071QColorDialog::ColorDialogOptions QColorDialog::options()
const
2073 Q_D(
const QColorDialog);
2074 return QColorDialog::ColorDialogOptions(
int(d->options->options()));
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2093
2094
2095
2096
2097
2098
2099
2102
2103
2104
2105
2106
2107
2108
2111
2112
2113
2114void QColorDialog::setVisible(
bool visible)
2117 QDialog::setVisible(visible);
2121
2122
2123
2124
2125
2126
2131 const auto q =
static_cast<QDialog *>(q_ptr);
2134 selectedQColor = QColor();
2136 if (nativeDialogInUse) {
2137 if (setNativeDialogVisible(visible)) {
2140 q->setAttribute(Qt::WA_DontShowOnScreen);
2141 }
else if (visible) {
2145 q->setAttribute(Qt::WA_DontShowOnScreen,
false);
2148 QDialogPrivate::setVisible(visible);
2152
2153
2154
2155
2156
2157void QColorDialog::open(QObject *receiver,
const char *member)
2160 connect(
this, SIGNAL(colorSelected(QColor)), receiver, member);
2161 d->receiverToDisconnectOnClose = receiver;
2162 d->memberToDisconnectOnClose = member;
2167
2168
2169
2170
2171
2172
2173
2174QColor QColorDialog::getColor(
const QColor &initial, QWidget *parent,
const QString &title,
2175 ColorDialogOptions options)
2177 QAutoPointer<QColorDialog> dlg(
new QColorDialog(parent));
2178 if (!title.isEmpty())
2179 dlg->setWindowTitle(title);
2180 dlg->setOptions(options);
2181 dlg->setCurrentColor(initial);
2187 return dlg->selectedColor();
2193
2194
2196QColorDialog::~QColorDialog()
2201
2202
2203void QColorDialog::changeEvent(QEvent *e)
2206 if (e->type() == QEvent::LanguageChange)
2207 d->retranslateStrings();
2208 QDialog::changeEvent(e);
2215 static QPoint lastGlobalPos;
2216 QPoint newGlobalPos = QCursor::pos();
2217 if (lastGlobalPos == newGlobalPos)
2219 lastGlobalPos = newGlobalPos;
2221 if (!q->rect().contains(q->mapFromGlobal(newGlobalPos))) {
2222 updateColorPicking(newGlobalPos);
2224 dummyTransparentWindow.setPosition(newGlobalPos);
2232 const QColor color = grabScreenColor(globalPos);
2235 setCurrentColor(color, ShowColor);
2244 updateColorPicking(e->globalPosition().toPoint());
2250 setCurrentColor(grabScreenColor(e->globalPosition().toPoint()), SetColorAll);
2258#if QT_CONFIG(shortcut)
2259 if (e->matches(QKeySequence::Cancel)) {
2260 releaseColorPicking();
2261 q->setCurrentColor(beforeScreenColorPicking);
2264 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
2265 q->setCurrentColor(grabScreenColor(QCursor::pos()));
2273
2274
2275
2276
2277
2278
2279void QColorDialog::done(
int result)
2282 if (result == Accepted) {
2283 d->selectedQColor = d->currentQColor();
2284 emit colorSelected(d->selectedQColor);
2286 d->selectedQColor = QColor();
2288 QDialog::done(result);
2289 if (d->receiverToDisconnectOnClose) {
2290 disconnect(
this, SIGNAL(colorSelected(QColor)),
2291 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
2292 d->receiverToDisconnectOnClose =
nullptr;
2294 d->memberToDisconnectOnClose.clear();
2299#include "qcolordialog.moc"
2300#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