From 24466d354219048b808393a094fcf80ae5c99e39 Mon Sep 17 00:00:00 2001 From: David Gil Date: Wed, 8 May 2013 19:05:32 +0200 Subject: [PATCH] Add web color QLineEdit to QColorDialog. This web color QLineEdit shows the hex number of the selected color. Besides, it can be edited and accepts 3 and 6 digit hex numbers. Change-Id: Idf629fbdc203fc099d446b31cbb49f2ff56be810 Reviewed-by: Friedemann Kleint Reviewed-by: Kevin Ottens Reviewed-by: Lars Knoll Reviewed-by: Gunnar Sletta --- src/widgets/dialogs/qcolordialog.cpp | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/widgets/dialogs/qcolordialog.cpp b/src/widgets/dialogs/qcolordialog.cpp index 5edf883e5a..e2f5950e4b 100644 --- a/src/widgets/dialogs/qcolordialog.cpp +++ b/src/widgets/dialogs/qcolordialog.cpp @@ -918,6 +918,8 @@ signals: private slots: void rgbEd(); void hsvEd(); + void htmlEd(); + private: void showCurrentColor(); int hue, sat, val; @@ -929,6 +931,7 @@ private: QLabel *lblRed; QLabel *lblGreen; QLabel *lblBlue; + QLabel *lblHtml; QColSpinBox *hEd; QColSpinBox *sEd; QColSpinBox *vEd; @@ -937,6 +940,7 @@ private: QColSpinBox *bEd; QColSpinBox *alphaEd; QLabel *alphaLab; + QLineEdit *htEd; QColorShowLabel *lab; bool rgbOriginal; QColorDialog *colorDialog; @@ -1228,6 +1232,19 @@ QColorShower::QColorShower(QColorDialog *parent) #endif alphaEd->hide(); alphaLab->hide(); + lblHtml = new QLabel(this); + htEd = new QLineEdit(this); +#ifndef QT_NO_SHORTCUT + lblHtml->setBuddy(htEd); +#endif + + QRegularExpression regExp(QStringLiteral("#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})")); + QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, this); + htEd->setValidator(validator); + + lblHtml->setAlignment(Qt::AlignRight|Qt::AlignVCenter); + gl->addWidget(lblHtml, 5, 1); + gl->addWidget(htEd, 5, 2); connect(hEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); connect(sEd, SIGNAL(valueChanged(int)), this, SLOT(hsvEd())); @@ -1237,6 +1254,7 @@ QColorShower::QColorShower(QColorDialog *parent) connect(gEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); connect(bEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); connect(alphaEd, SIGNAL(valueChanged(int)), this, SLOT(rgbEd())); + connect(htEd, SIGNAL(textEdited(QString)), this, SLOT(htmlEd())); retranslateStrings(); } @@ -1271,6 +1289,8 @@ void QColorShower::rgbEd() sEd->setValue(sat); vEd->setValue(val); + htEd->setText(QColor(curCol).name()); + showCurrentColor(); emit newCol(currentColor()); updateQColor(); @@ -1291,6 +1311,31 @@ void QColorShower::hsvEd() gEd->setValue(qGreen(currentColor())); bEd->setValue(qBlue(currentColor())); + htEd->setText(c.name()); + + showCurrentColor(); + emit newCol(currentColor()); + updateQColor(); +} + +void QColorShower::htmlEd() +{ + QColor c; + QString t = htEd->text(); + c.setNamedColor(t); + if (!c.isValid()) + return; + curCol = qRgba(c.red(), c.green(), c.blue(), currentAlpha()); + rgb2hsv(curCol, hue, sat, val); + + hEd->setValue(hue); + sEd->setValue(sat); + vEd->setValue(val); + + rEd->setValue(qRed(currentColor())); + gEd->setValue(qGreen(currentColor())); + bEd->setValue(qBlue(currentColor())); + showCurrentColor(); emit newCol(currentColor()); updateQColor(); @@ -1311,6 +1356,8 @@ void QColorShower::setRgb(QRgb rgb) gEd->setValue(qGreen(currentColor())); bEd->setValue(qBlue(currentColor())); + htEd->setText(QColor(rgb).name()); + showCurrentColor(); updateQColor(); } @@ -1334,6 +1381,8 @@ void QColorShower::setHsv(int h, int s, int v) gEd->setValue(qGreen(currentColor())); bEd->setValue(qBlue(currentColor())); + htEd->setText(c.name()); + showCurrentColor(); updateQColor(); } @@ -1347,6 +1396,7 @@ void QColorShower::retranslateStrings() lblGreen->setText(QColorDialog::tr("&Green:")); lblBlue->setText(QColorDialog::tr("Bl&ue:")); alphaLab->setText(QColorDialog::tr("A&lpha channel:")); + lblHtml->setText(QColorDialog::tr("&HTML:")); } void QColorShower::updateQColor()