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"
43#include <qpa/qplatformintegration.h>
44#include <qpa/qplatformservices.h>
45#include <private/qguiapplication_p.h>
47#include <QtCore/qpointer.h>
53using namespace Qt::StringLiterals;
73 Q_DECLARE_PUBLIC(QColorDialog)
89 {
return static_cast<QPlatformColorDialogHelper *>(platformHelper()); }
91 void init(
const QColor &initial);
170 Q_PROPERTY(
int selectedColumn READ selectedColumn)
193 {
return y / cellh; }
196 {
if (isRightToLeft())
return ncols - (x / cellw) - 1;
return x / cellw; }
199 {
return cellh * row; }
202 {
if (isRightToLeft())
return cellw * (ncols - column - 1);
return cellw * column; }
211 {
return QRect(0, 0, cellw, cellh); }
214 {
return QSize(ncols * cellw, nrows * cellh); }
219 if (row >= 0 && row < nrows && column >= 0 && column < ncols)
224 inline void updateCell(
int row,
int column) { update(cellGeometry(row, column)); }
267 if (isRightToLeft()) {
275 QRect rect(0, 0, cellWidth(), cellHeight());
278 if (collast < 0 || collast >= ncols)
280 if (rowlast < 0 || rowlast >= nrows)
284 for (
int r = rowfirst; r <= rowlast; ++r) {
291 for (
int c = colfirst; c <= collast; ++c) {
295 rect.translate(colp, rowp);
297 rect.translate(-colp, -rowp);
304 ,nrows(rows), ncols(cols)
306 setFocusPolicy(Qt::StrongFocus);
318 return gridSize().boundedTo(QSize(640, 480));
326 const QPalette & g = palette();
329 int dfw = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &opt,
this);
331 opt.midLineWidth = 1;
332 opt.rect = rect.adjusted(b, b, -b, -b);
334 opt.state = QStyle::State_Enabled | QStyle::State_Sunken;
335 style()->drawPrimitive(QStyle::PE_Frame, &opt, p,
this);
338 if ((row == curRow) && (col == curCol)) {
343 opt.state = QStyle::State_None | QStyle::State_KeyboardFocusChange;
344 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, p,
this);
347 paintCellContents(p, row, col, opt.rect.adjusted(dfw, dfw, -dfw, -dfw));
351
352
357 p->fillRect(r, Qt::white);
358 p->setPen(Qt::black);
359 p->drawLine(r.topLeft(), r.bottomRight());
360 p->drawLine(r.topRight(), r.bottomLeft());
366 QPoint pos = e->position().toPoint();
378
379
380
384 if ((curRow == row) && (curCol == col))
387 if (row < 0 || col < 0)
399 emit currentChanged(curRow, curCol);
403
404
405
406
407
413 if (row < 0 || col < 0)
422 emit selected(row, col);
425 if (isVisible() && qobject_cast<QMenu*>(parentWidget()))
426 parentWidget()->close();
433 emit currentChanged(curRow, curCol);
491 switch (event->type()) {
492 case QEvent::MouseMove:
494 case QEvent::MouseButtonRelease:
496 case QEvent::KeyPress:
506 if (state != Qt::ApplicationActive)
517
518
519
520int QColorDialog::customCount()
522 return QColorDialogOptions::customColorCount();
526
527
528QColor QColorDialog::customColor(
int index)
530 return QColor(QColorDialogOptions::customColor(index));
534
535
536
537
538
539
540void QColorDialog::setCustomColor(
int index, QColor color)
542 QColorDialogOptions::setCustomColor(index, color.rgba());
546
547
548
549
550QColor QColorDialog::standardColor(
int index)
552 return QColor(QColorDialogOptions::standardColor(index));
556
557
558
559
560
561
562void QColorDialog::setStandardColor(
int index, QColor color)
564 QColorDialogOptions::setStandardColor(index, color.rgba());
567static inline void rgb2hsv(QRgb rgb,
int &h,
int &s,
int &v)
571 c.getHsv(&h, &s, &v);
581 { setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); }
588#if QT_CONFIG(draganddrop)
606 p->fillRect(r, QColor(values[i]));
611 oldCurrent = QPoint(selectedRow(), selectedColumn());
614 pressPos = e->position().toPoint();
619 QWellArray::mouseMoveEvent(e);
620#if QT_CONFIG(draganddrop)
623 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
624 setCurrent(oldCurrent.x(), oldCurrent.y());
625 int i = rowAt(pressPos.y()) + columnAt(pressPos.x()) * numRows();
626 QColor col(values[i]);
627 QMimeData *mime =
new QMimeData;
628 mime->setColorData(col);
629 QPixmap pix(cellWidth(), cellHeight());
632 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
634 QDrag *drg =
new QDrag(
this);
635 drg->setMimeData(mime);
637 mousePressed =
false;
638 drg->exec(Qt::CopyAction);
643#if QT_CONFIG(draganddrop)
687 mousePressed =
false;
716 int huePt(
const QPoint &pt);
717 int satPt(
const QPoint &pt);
718 void setCol(
const QPoint &pt);
751 enum { foff = 3, coff = 4 };
766 int d = height() - 2*coff - 1;
767 return 255 - (y - coff)*255/d;
772 int d = height() - 2*coff - 1;
773 return coff + (255-v)*d/255;
779 hue = 100; val = 100; sat = 100;
791 if (m->buttons() == Qt::NoButton) {
795 setVal(y2val(m->position().toPoint().y()));
799 setVal(y2val(m->position().toPoint().y()));
806 val = qMax(0, qMin(v,255));
807 delete pix; pix=
nullptr;
809 emit newHsv(hue, sat, val);
816 emit newHsv(h, s, val);
823 QRect r(0, foff, w, height() - 2*foff);
824 int wi = r.width() - 2;
825 int hi = r.height() - 2;
826 if (!pix || pix->height() != hi || pix->width() != wi) {
828 QImage img(wi, hi, QImage::Format_RGB32);
830 uint *pixel = (uint *) img.scanLine(0);
831 for (y = 0; y < hi; y++) {
832 uint *end = pixel + wi;
833 std::fill(pixel, end, QColor::fromHsv(hue, sat, y2val(y + coff)).rgb());
836 pix =
new QPixmap(QPixmap::fromImage(img));
839 p.drawPixmap(1, coff, *pix);
840 const QPalette &g = palette();
841 qDrawShadePanel(&p, r, g,
true);
842 p.setPen(g.windowText().color());
843 p.setBrush(g.windowText());
844 p.eraseRect(w, 0, 5, height());
845 const int y = val2y(val);
846 const std::array<QPoint, 3> points = {QPoint(w, y), QPoint(w + 5, y + 5), QPoint(w + 5, y - 5)};
847 p.drawPolygon(points.data(),
static_cast<
int>(points.size()));
855 delete pix; pix=
nullptr;
861 QRect r = contentsRect();
862 return QPoint((360 - hue) * (r.width() - 1) / 360, (255 - sat) * (r.height() - 1) / 255);
867 QRect r = contentsRect();
868 return 360 - pt.x() * 360 / (r.width() - 1);
873 QRect r = contentsRect();
874 return 255 - pt.y() * 255 / (r.height() - 1);
879 setCol(huePt(pt), satPt(pt));
889 setAttribute(Qt::WA_NoSystemBackground);
890 setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed) );
899 if (crossVisible != visible) {
900 crossVisible = visible;
907 return QSize(
pWidth + 2*frameWidth(),
pHeight + 2*frameWidth());
912 int nhue = qMin(qMax(0,h), 359);
913 int nsat = qMin(qMax(0,s), 255);
914 if (nhue == hue && nsat == sat)
917 QRect r(colPt(), QSize(20,20));
918 hue = nhue; sat = nsat;
919 r = r.united(QRect(colPt(), QSize(20,20)));
920 r.translate(contentsRect().x()-9, contentsRect().y()-9);
927 QPoint p = m->position().toPoint() - contentsRect().topLeft();
928 if (m->buttons() == Qt::NoButton) {
933 emit newCol(hue, sat);
938 QPoint p = m->position().toPoint() - contentsRect().topLeft();
940 emit newCol(hue, sat);
947 QRect r = contentsRect();
949 p.drawPixmap(r.topLeft(), pix);
952 QPoint pt = colPt() + r.topLeft();
954 p.fillRect(pt.x()-9, pt.y(), 20, 2, Qt::black);
955 p.fillRect(pt.x(), pt.y()-9, 2, 20, Qt::black);
961 QFrame::resizeEvent(ev);
963 int w = width() - frameWidth() * 2;
964 int h = height() - frameWidth() * 2;
965 QImage img(w, h, QImage::Format_RGB32);
967 uint *pixel = (uint *) img.scanLine(0);
968 for (y = 0; y < h; y++) {
969 const uint *end = pixel + w;
971 while (pixel < end) {
974 c.setHsv(huePt(p), satPt(p), 200);
980 pix = QPixmap::fromImage(img);
990 const QSignalBlocker blocker(
this);
991 QSpinBox::setValue(i);
1007 {
return (colorDialog->options() & QColorDialog::ShowAlphaChannel) ? alphaEd->value() : 255; }
1030 void showCurrentColor();
1052 QColorDialog *colorDialog;
1079#if QT_CONFIG(draganddrop)
1095 p.fillRect(contentsRect()&e->rect(), col);
1100 alphaLab->setVisible(b);
1101 alphaEd->setVisible(b);
1106 return alphaLab->isVisible();
1111 mousePressed =
true;
1112 pressPos = e->position().toPoint();
1117#if !QT_CONFIG(draganddrop)
1122 if ((pressPos - e->position().toPoint()).manhattanLength() > QApplication::startDragDistance()) {
1123 QMimeData *mime =
new QMimeData;
1124 mime->setColorData(col);
1125 QPixmap pix(30, 20);
1128 p.drawRect(0, 0, pix.width() - 1, pix.height() - 1);
1130 QDrag *drg =
new QDrag(
this);
1131 drg->setMimeData(mime);
1132 drg->setPixmap(pix);
1133 mousePressed =
false;
1134 drg->exec(Qt::CopyAction);
1139#if QT_CONFIG(draganddrop)
1170 mousePressed =
false;
1176 colorDialog = parent;
1178 curCol = qRgb(255, 255, 255);
1179 curQColor = Qt::white;
1181 gl =
new QGridLayout(
this);
1182 const int s = gl->spacing();
1183 gl->setContentsMargins(s, s, s, s);
1184 lab =
new QColorShowLabel(
this);
1186#ifdef QT_SMALL_COLORDIALOG
1187 lab->setMinimumHeight(60);
1189 lab->setMinimumWidth(60);
1193#if !defined(QT_SMALL_COLORDIALOG)
1194 gl->addWidget(lab, 0, 0, -1, 1);
1196 gl->addWidget(lab, 0, 0, 1, -1);
1198 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::newCol);
1199 connect(lab, &QColorShowLabel::colorDropped,
this, &QColorShower::setRgb);
1201 hEd =
new QColSpinBox(
this);
1202 hEd->setRange(0, 359);
1203 lblHue =
new QLabel(
this);
1204#ifndef QT_NO_SHORTCUT
1205 lblHue->setBuddy(hEd);
1207 lblHue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1208#if !defined(QT_SMALL_COLORDIALOG)
1209 gl->addWidget(lblHue, 0, 1);
1210 gl->addWidget(hEd, 0, 2);
1212 gl->addWidget(lblHue, 1, 0);
1213 gl->addWidget(hEd, 2, 0);
1216 sEd =
new QColSpinBox(
this);
1217 lblSat =
new QLabel(
this);
1218#ifndef QT_NO_SHORTCUT
1219 lblSat->setBuddy(sEd);
1221 lblSat->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1222#if !defined(QT_SMALL_COLORDIALOG)
1223 gl->addWidget(lblSat, 1, 1);
1224 gl->addWidget(sEd, 1, 2);
1226 gl->addWidget(lblSat, 1, 1);
1227 gl->addWidget(sEd, 2, 1);
1230 vEd =
new QColSpinBox(
this);
1231 lblVal =
new QLabel(
this);
1232#ifndef QT_NO_SHORTCUT
1233 lblVal->setBuddy(vEd);
1235 lblVal->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1236#if !defined(QT_SMALL_COLORDIALOG)
1237 gl->addWidget(lblVal, 2, 1);
1238 gl->addWidget(vEd, 2, 2);
1240 gl->addWidget(lblVal, 1, 2);
1241 gl->addWidget(vEd, 2, 2);
1244 rEd =
new QColSpinBox(
this);
1245 lblRed =
new QLabel(
this);
1246#ifndef QT_NO_SHORTCUT
1247 lblRed->setBuddy(rEd);
1249 lblRed->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1250#if !defined(QT_SMALL_COLORDIALOG)
1251 gl->addWidget(lblRed, 0, 3);
1252 gl->addWidget(rEd, 0, 4);
1254 gl->addWidget(lblRed, 3, 0);
1255 gl->addWidget(rEd, 4, 0);
1258 gEd =
new QColSpinBox(
this);
1259 lblGreen =
new QLabel(
this);
1260#ifndef QT_NO_SHORTCUT
1261 lblGreen->setBuddy(gEd);
1263 lblGreen->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1264#if !defined(QT_SMALL_COLORDIALOG)
1265 gl->addWidget(lblGreen, 1, 3);
1266 gl->addWidget(gEd, 1, 4);
1268 gl->addWidget(lblGreen, 3, 1);
1269 gl->addWidget(gEd, 4, 1);
1272 bEd =
new QColSpinBox(
this);
1273 lblBlue =
new QLabel(
this);
1274#ifndef QT_NO_SHORTCUT
1275 lblBlue->setBuddy(bEd);
1277 lblBlue->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1278#if !defined(QT_SMALL_COLORDIALOG)
1279 gl->addWidget(lblBlue, 2, 3);
1280 gl->addWidget(bEd, 2, 4);
1282 gl->addWidget(lblBlue, 3, 2);
1283 gl->addWidget(bEd, 4, 2);
1286 alphaEd =
new QColSpinBox(
this);
1287 alphaLab =
new QLabel(
this);
1288#ifndef QT_NO_SHORTCUT
1289 alphaLab->setBuddy(alphaEd);
1291 alphaLab->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1292#if !defined(QT_SMALL_COLORDIALOG)
1293 gl->addWidget(alphaLab, 3, 1, 1, 3);
1294 gl->addWidget(alphaEd, 3, 4);
1296 gl->addWidget(alphaLab, 1, 3, 3, 1);
1297 gl->addWidget(alphaEd, 4, 3);
1301 lblHtml =
new QLabel(
this);
1302 htEd =
new QLineEdit(
this);
1303 htEd->setObjectName(
"qt_colorname_lineedit");
1304#ifndef QT_NO_SHORTCUT
1305 lblHtml->setBuddy(htEd);
1308#if QT_CONFIG(regularexpression)
1309 QRegularExpression regExp(QStringLiteral(
"#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})"));
1310 QRegularExpressionValidator *validator =
new QRegularExpressionValidator(regExp,
this);
1311 htEd->setValidator(validator);
1313 htEd->setReadOnly(
true);
1315 htEd->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
1317 lblHtml->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
1318#if defined(QT_SMALL_COLORDIALOG)
1319 gl->addWidget(lblHtml, 5, 0);
1320 gl->addWidget(htEd, 5, 1, 1, 2);
1322 gl->addWidget(lblHtml, 5, 1);
1323 gl->addWidget(htEd, 5, 2, 1, 3);
1326 connect(hEd, &QSpinBox::valueChanged,
this, &QColorShower::hsvEd);
1327 connect(sEd, &QSpinBox::valueChanged,
this, &QColorShower::hsvEd);
1328 connect(vEd, &QSpinBox::valueChanged,
this, &QColorShower::hsvEd);
1330 connect(rEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1331 connect(gEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1332 connect(bEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1333 connect(alphaEd, &QSpinBox::valueChanged,
this, &QColorShower::rgbEd);
1334 connect(htEd, &QLineEdit::textEdited,
this, &QColorShower::htmlEd);
1349 if (nativeDialogInUse)
1350 return platformColorDialogHelper()->currentColor();
1351 return cs->currentQColor();
1356 lab->setColor(currentColor());
1363 curCol = qRgba(rEd->value(), gEd->value(), bEd->value(), currentAlpha());
1365 rgb2hsv(currentColor(), hue, sat, val);
1371 htEd->setText(QColor(curCol).name());
1374 emit newCol(currentColor());
1380 rgbOriginal =
false;
1386 c.setHsv(hue, sat, val);
1389 rEd->setValue(qRed(currentColor()));
1390 gEd->setValue(qGreen(currentColor()));
1391 bEd->setValue(qBlue(currentColor()));
1393 htEd->setText(c.name());
1396 emit newCol(currentColor());
1402 QString t = htEd->text();
1406 if (!t.startsWith(u"#")) {
1408 QSignalBlocker blocker(htEd);
1412 QColor c = QColor::fromString(t);
1416 curCol = qRgba(c.red(), c.green(), c.blue(), currentAlpha());
1417 rgb2hsv(curCol, hue, sat, val);
1423 rEd->setValue(qRed(currentColor()));
1424 gEd->setValue(qGreen(currentColor()));
1425 bEd->setValue(qBlue(currentColor()));
1428 emit newCol(currentColor());
1437 rgb2hsv(currentColor(), hue, sat, val);
1443 rEd->setValue(qRed(currentColor()));
1444 gEd->setValue(qGreen(currentColor()));
1445 bEd->setValue(qBlue(currentColor()));
1447 htEd->setText(QColor(rgb).name());
1455 if (h < -1 || (uint)s > 255 || (uint)v > 255)
1458 rgbOriginal =
false;
1459 hue = h; val = v; sat = s;
1461 c.setHsv(hue, sat, val);
1468 rEd->setValue(qRed(currentColor()));
1469 gEd->setValue(qGreen(currentColor()));
1470 bEd->setValue(qBlue(currentColor()));
1472 htEd->setText(c.name());
1480 lblHue->setText(QColorDialog::tr(
"Hu&e:"));
1481 lblSat->setText(QColorDialog::tr(
"&Sat:"));
1482 lblVal->setText(QColorDialog::tr(
"&Val:"));
1483 lblRed->setText(QColorDialog::tr(
"&Red:"));
1484 lblGreen->setText(QColorDialog::tr(
"&Green:"));
1485 lblBlue->setText(QColorDialog::tr(
"Bl&ue:"));
1486 alphaLab->setText(QColorDialog::tr(
"A&lpha channel:"));
1487 lblHtml->setText(QColorDialog::tr(
"&HTML:"));
1492 QColor oldQColor(curQColor);
1493 curQColor.setRgba(qRgba(qRed(curCol), qGreen(curCol), qBlue(curCol), currentAlpha()));
1494 if (curQColor != oldQColor)
1495 emit currentColorChanged(curQColor);
1501 if (!nativeDialogInUse) {
1511 if (!nativeDialogInUse) {
1513 newColorTypedIn(rgb);
1521 if (
cs->curQColor != color) {
1522 cs->curQColor = color;
1523 emit q->currentColorChanged(color);
1536 QRgb color = col.rgb();
1539 const QRgb *standardColors = QColorDialogOptions::standardColors();
1541 const QRgb *match =
std::find(standardColors, standardColorsEnd, color);
1542 if (match != standardColorsEnd) {
1543 const int index =
int(match - standardColors);
1555 const QRgb *customColors = QColorDialogOptions::customColors();
1557 const QRgb *match =
std::find(customColors, customColorsEnd, color);
1558 if (match != customColorsEnd) {
1559 const int index =
int(match - customColors);
1574 QScreen *screen = QGuiApplication::screenAt(p);
1576 screen = QGuiApplication::primaryScreen();
1577 const QRect screenRect = screen->geometry();
1578 const QPixmap pixmap =
1579 screen->grabWindow(0, p.x() - screenRect.x(), p.y() - screenRect.y(), 1, 1);
1580 const QImage i = pixmap.toImage();
1581 return i.pixel(0, 0);
1587 if (!nativeDialogInUse) {
1589 rgb2hsv(rgb, h, s, v);
1603 setCurrentRgbColor(QColorDialogOptions::customColor(i));
1610 setCurrentRgbColor(QColorDialogOptions::standardColor(r + c * 6));
1619 auto *platformServices = QGuiApplicationPrivate::platformIntegration()->services();
1620 if (platformServices && platformServices->hasCapability(QPlatformServices::Capability::ColorPicking)) {
1621 if (
auto *colorPicker = platformServices->colorPicker(q->windowHandle())) {
1622 q->connect(colorPicker, &QPlatformServiceColorPicker::colorPicked, q,
1623 [q, colorPicker](
const QColor &color) {
1624 colorPicker->deleteLater();
1625 q->setCurrentColor(color);
1627 colorPicker->pickColor();
1632 if (!colorPickingEventFilter)
1633 colorPickingEventFilter =
new QColorPickingEventFilter(
this, q);
1634 q->installEventFilter(colorPickingEventFilter);
1635 QObject::connect(
qApp, &QGuiApplication::applicationStateChanged,
1636 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1638 beforeScreenColorPicking = cs->currentColor();
1640 q->grabMouse(Qt::CrossCursor);
1647 updateTimer->start(30);
1652 dummyTransparentWindow.show();
1656
1657
1658 q->setMouseTracking(
true);
1661 buttons->setDisabled(
true);
1664 const QPoint globalPos = QCursor::pos();
1665 q->setCurrentColor(grabScreenColor(globalPos));
1672 if (lblScreenColorInfo)
1673 lblScreenColorInfo->setText(QColorDialog::tr(
"Cursor at %1, %2\nPress ESC to cancel")
1675 .arg(globalPos.y()));
1682 q->removeEventFilter(colorPickingEventFilter);
1683 QObject::disconnect(
qApp, &QGuiApplication::applicationStateChanged,
1684 colorPickingEventFilter, &QColorPickingEventFilter::applicationStateChanged);
1687 updateTimer->stop();
1688 dummyTransparentWindow.setVisible(
false);
1690 q->releaseKeyboard();
1691 q->setMouseTracking(
false);
1692 lblScreenColorInfo->setText(
"\n"_L1);
1694 buttons->setDisabled(
false);
1702 q->setSizeGripEnabled(
false);
1703 q->setWindowTitle(QColorDialog::tr(
"Select Color"));
1706 nativeDialogInUse = (platformColorDialogHelper() !=
nullptr);
1710 if (!nativeDialogInUse)
1714 dummyTransparentWindow.resize(1, 1);
1715 dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
1718 q->setCurrentColor(initial);
1724 QVBoxLayout *mainLay =
new QVBoxLayout(q);
1726 mainLay->setSizeConstraint(QLayout::SetFixedSize);
1728 QHBoxLayout *topLay =
new QHBoxLayout();
1729 mainLay->addLayout(topLay);
1733#if defined(QT_SMALL_COLORDIALOG)
1734 smallDisplay =
true;
1735 const int lumSpace = 20;
1739 smallDisplay = (QGuiApplication::primaryScreen()->virtualGeometry().width() < 480 || QGuiApplication::primaryScreen()->virtualGeometry().height() < 350);
1740 const int lumSpace = topLay->spacing() / 2;
1744 leftLay =
new QVBoxLayout;
1745 topLay->addLayout(leftLay);
1747 standard =
new QColorWell(q, standardColorRows, colorColumns, QColorDialogOptions::standardColors());
1748 lblBasicColors =
new QLabel(q);
1749#ifndef QT_NO_SHORTCUT
1750 lblBasicColors->setBuddy(standard);
1752 QObjectPrivate::connect(standard, &QColorWell::selected,
1753 this, &QColorDialogPrivate::newStandard);
1754 leftLay->addWidget(lblBasicColors);
1755 leftLay->addWidget(standard);
1757#if !defined(QT_SMALL_COLORDIALOG)
1759 eyeDropperButton =
new QPushButton();
1760 leftLay->addWidget(eyeDropperButton);
1761 lblScreenColorInfo =
new QLabel(
"\n"_L1);
1762 leftLay->addWidget(lblScreenColorInfo);
1763 QObjectPrivate::connect(eyeDropperButton, &QPushButton::clicked,
1764 this, &QColorDialogPrivate::pickScreenColor);
1767 lblScreenColorInfo =
nullptr;
1771 leftLay->addStretch();
1773 custom =
new QColorWell(q, customColorRows, colorColumns, QColorDialogOptions::customColors());
1774 custom->setAcceptDrops(
true);
1776 QObjectPrivate::connect(custom, &QColorWell::selected,
this, &QColorDialogPrivate::newCustom);
1777 QObjectPrivate::connect(custom, &QColorWell::currentChanged,
this, &QColorDialogPrivate::nextCustom);
1779 QObject::connect(custom, &QWellArray::colorChanged, q, [
this] (
int index, QRgb color) {
1780 QColorDialogOptions::setCustomColor(index, color);
1785 lblCustomColors =
new QLabel(q);
1786#ifndef QT_NO_SHORTCUT
1787 lblCustomColors->setBuddy(custom);
1789 leftLay->addWidget(lblCustomColors);
1790 leftLay->addWidget(custom);
1792 addCusBt =
new QPushButton(q);
1793 QObjectPrivate::connect(addCusBt, &QPushButton::clicked,
this, &QColorDialogPrivate::addCustom);
1794 leftLay->addWidget(addCusBt);
1797#if defined(QT_SMALL_COLORDIALOG)
1798 QSize screenSize = QGuiApplication::screenAt(QCursor::pos())->availableGeometry().size();
1799 pWidth = pHeight = qMin(screenSize.width(), screenSize.height());
1801 if (screenSize.height() > screenSize.width())
1811 QVBoxLayout *rightLay =
new QVBoxLayout;
1812 topLay->addLayout(rightLay);
1814 QHBoxLayout *pickLay =
new QHBoxLayout;
1815 rightLay->addLayout(pickLay);
1817 QVBoxLayout *cLay =
new QVBoxLayout;
1818 pickLay->addLayout(cLay);
1819 cp =
new QColorPicker(q);
1821 cp->setFrameStyle(QFrame::Panel | QFrame::Sunken);
1823#if defined(QT_SMALL_COLORDIALOG)
1826 cLay->addSpacing(lumSpace);
1827 cLay->addWidget(
cp);
1829 cLay->addSpacing(lumSpace);
1831 lp =
new QColorLuminancePicker(q);
1832#if defined(QT_SMALL_COLORDIALOG)
1835 lp->setFixedWidth(20);
1836 pickLay->addSpacing(10);
1837 pickLay->addWidget(
lp);
1838 pickLay->addStretch();
1841 QObject::connect(cp, &QColorPicker::newCol, lp, qOverload<
int,
int>(&QColorLuminancePicker::setCol));
1842 QObjectPrivate::connect(lp, &QColorLuminancePicker::newHsv,
this, &QColorDialogPrivate::newHsv);
1844 rightLay->addStretch();
1846 cs =
new QColorShower(q);
1847 pickLay->setContentsMargins(
cs->gl->contentsMargins());
1848 QObjectPrivate::connect(cs, &QColorShower::newCol,
1849 this, &QColorDialogPrivate::newColorTypedIn);
1850 QObject::connect(cs, &QColorShower::currentColorChanged,
1851 q, &QColorDialog::currentColorChanged);
1852#if defined(QT_SMALL_COLORDIALOG)
1853 topLay->addWidget(cs);
1855 rightLay->addWidget(
cs);
1857 leftLay->addSpacing(cs->gl->contentsMargins().right());
1860 buttons =
new QDialogButtonBox(q);
1861 mainLay->addWidget(buttons);
1863 ok = buttons->addButton(QDialogButtonBox::Ok);
1864 QObject::connect(ok, &QPushButton::clicked, q, &QColorDialog::accept);
1865 ok->setDefault(
true);
1866 cancel = buttons->addButton(QDialogButtonBox::Cancel);
1867 QObject::connect(cancel, &QPushButton::clicked, q, &QColorDialog::reject);
1870 updateTimer =
new QTimer(q);
1871 QObjectPrivate::connect(updateTimer, &QTimer::timeout,
1872 this, qOverload<>(&QColorDialogPrivate::updateColorPicking));
1879 QColorDialog *d = q_func();
1880 auto *colorDialogHelper =
static_cast<QPlatformColorDialogHelper*>(h);
1881 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::currentColorChanged,
1882 d, &QColorDialog::currentColorChanged);
1883 QObject::connect(colorDialogHelper, &QPlatformColorDialogHelper::colorSelected,
1884 d, &QColorDialog::colorSelected);
1885 colorDialogHelper->setOptions(options);
1890 options->setWindowTitle(q_func()->windowTitle());
1895 QColorDialogOptions::setCustomColor(nextCust, cs->currentColor());
1898 nextCust = (nextCust+1) % QColorDialogOptions::customColorCount();
1903 if (nativeDialogInUse)
1907 lblBasicColors->setText(QColorDialog::tr(
"&Basic colors"));
1908 lblCustomColors->setText(QColorDialog::tr(
"&Custom colors"));
1909 addCusBt->setText(QColorDialog::tr(
"&Add to Custom Colors"));
1910#if !defined(QT_SMALL_COLORDIALOG)
1911 if (eyeDropperButton)
1912 eyeDropperButton->setText(QColorDialog::tr(
"&Pick Screen Color"));
1921 const auto integration = QGuiApplicationPrivate::platformIntegration();
1922 return integration->hasCapability(QPlatformIntegration::ScreenWindowGrabbing)
1923 || integration->services()->hasCapability(QPlatformServices::Capability::ColorPicking);
1930 const QDialog *
const q =
static_cast<
const QDialog*>(q_ptr);
1931 if (nativeDialogInUse)
1933 if (QCoreApplication::testAttribute(Qt::AA_DontUseNativeDialogs)
1934 || q->testAttribute(Qt::WA_DontShowOnScreen)
1935 || (options->options() & QColorDialog::DontUseNativeDialog)) {
1939 return strcmp(QColorDialog::staticMetaObject.className(), q->metaObject()->className()) == 0;
1943 Qt::Dialog | Qt::WindowTitleHint
1944 | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1986
1987
1988QColorDialog::QColorDialog(QWidget *parent)
1989 : QColorDialog(QColor(Qt::white), parent)
1994
1995
1996
1997QColorDialog::QColorDialog(
const QColor &initial, QWidget *parent)
1998 : QDialog(*
new QColorDialogPrivate, parent, qcd_DefaultWindowFlags)
2006 if (nativeDialogInUse) {
2007 platformColorDialogHelper()->setCurrentColor(color);
2011 if (setColorMode & ShowColor) {
2012 setCurrentRgbColor(color.rgb());
2015 if (setColorMode & SelectColor)
2020
2021
2022
2024void QColorDialog::setCurrentColor(
const QColor &color)
2027 d->setCurrentColor(color);
2030QColor QColorDialog::currentColor()
const
2032 Q_D(
const QColorDialog);
2033 return d->currentQColor();
2037
2038
2039
2040
2041
2042
2043
2044QColor QColorDialog::selectedColor()
const
2046 Q_D(
const QColorDialog);
2047 return d->selectedQColor;
2051
2052
2053
2054
2055
2056void QColorDialog::setOption(ColorDialogOption option,
bool on)
2058 const QColorDialog::ColorDialogOptions previousOptions = options();
2059 if (!(previousOptions & option) != !on)
2060 setOptions(previousOptions ^ option);
2064
2065
2066
2067
2068
2069bool QColorDialog::testOption(ColorDialogOption option)
const
2071 Q_D(
const QColorDialog);
2072 return d->options->testOption(
static_cast<QColorDialogOptions::ColorDialogOption>(option));
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087void QColorDialog::setOptions(ColorDialogOptions options)
2091 if (QColorDialog::options() == options)
2094 d->options->setOptions(QColorDialogOptions::ColorDialogOptions(
int(options)));
2095 if ((options & DontUseNativeDialog) && d->nativeDialogInUse) {
2096 d->nativeDialogInUse =
false;
2099 if (!d->nativeDialogInUse) {
2100 d->buttons->setVisible(!(options & NoButtons));
2101 d->showAlpha(options & ShowAlphaChannel);
2102 if (d->eyeDropperButton)
2103 d->eyeDropperButton->setVisible(!(options & NoEyeDropperButton));
2107QColorDialog::ColorDialogOptions QColorDialog::options()
const
2109 Q_D(
const QColorDialog);
2110 return QColorDialog::ColorDialogOptions(
int(d->options->options()));
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2129
2130
2131
2132
2133
2134
2135
2138
2139
2140
2141
2142
2143
2144
2147
2148
2149
2150void QColorDialog::setVisible(
bool visible)
2153 QDialog::setVisible(visible);
2157
2158
2159
2160
2161
2162
2167 const auto q =
static_cast<QDialog *>(q_ptr);
2170 selectedQColor = QColor();
2172 if (nativeDialogInUse) {
2173 if (setNativeDialogVisible(visible)) {
2176 q->setAttribute(Qt::WA_DontShowOnScreen);
2177 }
else if (visible) {
2181 q->setAttribute(Qt::WA_DontShowOnScreen,
false);
2184 QDialogPrivate::setVisible(visible);
2188
2189
2190
2191
2192
2193void QColorDialog::open(QObject *receiver,
const char *member)
2196 connect(
this, SIGNAL(colorSelected(QColor)), receiver, member);
2197 d->receiverToDisconnectOnClose = receiver;
2198 d->memberToDisconnectOnClose = member;
2203
2204
2205
2206
2207
2208
2209
2210QColor QColorDialog::getColor(
const QColor &initial, QWidget *parent,
const QString &title,
2211 ColorDialogOptions options)
2213 QAutoPointer<QColorDialog> dlg(
new QColorDialog(parent));
2214 if (!title.isEmpty())
2215 dlg->setWindowTitle(title);
2216 dlg->setOptions(options);
2217 dlg->setCurrentColor(initial);
2223 return dlg->selectedColor();
2229
2230
2232QColorDialog::~QColorDialog()
2237
2238
2239void QColorDialog::changeEvent(QEvent *e)
2242 if (e->type() == QEvent::LanguageChange)
2243 d->retranslateStrings();
2244 QDialog::changeEvent(e);
2251 static QPoint lastGlobalPos;
2252 QPoint newGlobalPos = QCursor::pos();
2253 if (lastGlobalPos == newGlobalPos)
2255 lastGlobalPos = newGlobalPos;
2257 if (!q->rect().contains(q->mapFromGlobal(newGlobalPos))) {
2258 updateColorPicking(newGlobalPos);
2260 dummyTransparentWindow.setPosition(newGlobalPos);
2268 const QColor color = grabScreenColor(globalPos);
2271 setCurrentColor(color, ShowColor);
2280 updateColorPicking(e->globalPosition().toPoint());
2286 setCurrentColor(grabScreenColor(e->globalPosition().toPoint()), SetColorAll);
2294#if QT_CONFIG(shortcut)
2295 if (e->matches(QKeySequence::Cancel)) {
2296 releaseColorPicking();
2297 q->setCurrentColor(beforeScreenColorPicking);
2300 if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
2301 q->setCurrentColor(grabScreenColor(QCursor::pos()));
2309
2310
2311
2312
2313
2314
2315void QColorDialog::done(
int result)
2318 if (result == Accepted) {
2319 d->selectedQColor = d->currentQColor();
2320 emit colorSelected(d->selectedQColor);
2322 d->selectedQColor = QColor();
2324 QDialog::done(result);
2325 if (d->receiverToDisconnectOnClose) {
2326 disconnect(
this, SIGNAL(colorSelected(QColor)),
2327 d->receiverToDisconnectOnClose, d->memberToDisconnectOnClose);
2328 d->receiverToDisconnectOnClose =
nullptr;
2330 d->memberToDisconnectOnClose.clear();
2335#include "qcolordialog.moc"
2336#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
\variable QStyleOption::palette
\variable QStyleOptionFocusRect::backgroundColor
QColSpinBox(QWidget *parent)
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 ...
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...
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)
QColorWell(QWidget *parent, int r, int c, const QRgb *vals)
void paintCellContents(QPainter *, int row, int col, const QRect &) override
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 mouseReleaseEvent(QMouseEvent *e) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse release even...
friend class QT_PREPEND_NAMESPACE(QUntypedBindable)
void mouseReleaseEvent(QMouseEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse release even...
void focusOutEvent(QFocusEvent *) override
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus lost) f...
void currentChanged(int row, int col)
int columnAt(int x) const
void updateCell(int row, int column)
void colorChanged(int index, QRgb color)
void paintEvent(QPaintEvent *) override
This event handler can be reimplemented in a subclass to receive paint events passed in event.
void mousePressEvent(QMouseEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events...
void focusInEvent(QFocusEvent *) override
This event handler can be reimplemented in a subclass to receive keyboard focus events (focus receive...
virtual void setCurrent(int row, int col)
virtual void setSelected(int row, int col)
QRect cellGeometry(int row, int column)
virtual void paintCell(QPainter *, int row, int col, const QRect &)
int columnX(int column) const
QSize sizeHint() const override
virtual void paintCellContents(QPainter *, int row, int col, const QRect &)
int selectedColumn() const
void keyPressEvent(QKeyEvent *) override
This event handler, for event event, can be reimplemented in a subclass to receive key press events f...
QString cellContent(int row, int col) const
QtPrivate::QColorShower QColorShower
static void rgb2hsv(QRgb rgb, int &h, int &s, int &v)
QtPrivate::QColorWell QColorWell
QtPrivate::QWellArray QWellArray
QtPrivate::QColorPickingEventFilter QColorPickingEventFilter
QtPrivate::QColorLuminancePicker QColorLuminancePicker
QtPrivate::QColorPicker QColorPicker
static const Qt::WindowFlags qcd_DefaultWindowFlags