summaryrefslogtreecommitdiffstats
path: root/src/gui
diff options
context:
space:
mode:
authorErik Verbruggen <erik.verbruggen@nokia.com>2010-03-01 14:17:42 (GMT)
committerErik Verbruggen <erik.verbruggen@nokia.com>2010-03-01 14:20:21 (GMT)
commit72da039e54a62bf3a481fefc753e0e50ba75df57 (patch)
tree8ca6e0e3797b84ac0e43b853f874be8cfe55027c /src/gui
parent244d4d5610ce288503a73d0aa86f257dce4e74e6 (diff)
downloadQt-72da039e54a62bf3a481fefc753e0e50ba75df57.zip
Qt-72da039e54a62bf3a481fefc753e0e50ba75df57.tar.gz
Qt-72da039e54a62bf3a481fefc753e0e50ba75df57.tar.bz2
Added static method isValidColor to QColor.
Reviewed-by: Gunnar Sletta
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/painting/qcolor.cpp28
-rw-r--r--src/gui/painting/qcolor.h3
2 files changed, 28 insertions, 3 deletions
diff --git a/src/gui/painting/qcolor.cpp b/src/gui/painting/qcolor.cpp
index d6d288e..1b43161 100644
--- a/src/gui/painting/qcolor.cpp
+++ b/src/gui/painting/qcolor.cpp
@@ -532,25 +532,46 @@ QString QColor::name() const
void QColor::setNamedColor(const QString &name)
{
+ if (!setColorFromString(name))
+ qWarning("QColor::setNamedColor: Unknown color name '%s'", name.toLatin1().constData());
+}
+
+/*!
+ Checks if the \a name is a valid color name. The algorithm used is the same as with
+ \a setNamedColor().
+
+ \return true if the color name is valid, false otherwise.
+
+ \sa setNamedColor()
+*/
+bool QColor::isValidColor(const QString &name)
+{
+ return QColor().setColorFromString(name);
+}
+
+bool QColor::setColorFromString(const QString &name)
+{
if (name.isEmpty()) {
invalidate();
- return;
+ return false;
}
if (name.startsWith(QLatin1Char('#'))) {
QRgb rgb;
if (qt_get_hex_rgb(name.constData(), name.length(), &rgb)) {
setRgb(rgb);
+ return true;
} else {
invalidate();
+ return false;
}
- return;
}
#ifndef QT_NO_COLORNAMES
QRgb rgb;
if (qt_get_named_rgb(name.constData(), name.length(), &rgb)) {
setRgba(rgb);
+ return true;
} else
#endif
{
@@ -561,11 +582,12 @@ void QColor::setNamedColor(const QString &name)
&& QX11Info::display()
&& XParseColor(QX11Info::display(), QX11Info::appColormap(), name.toLatin1().constData(), &result)) {
setRgb(result.red >> 8, result.green >> 8, result.blue >> 8);
+ return true;
} else
#endif
{
- qWarning("QColor::setNamedColor: Unknown color name '%s'", name.toLatin1().constData());
invalidate();
+ return false;
}
}
}
diff --git a/src/gui/painting/qcolor.h b/src/gui/painting/qcolor.h
index 332dc25..0ac828d 100644
--- a/src/gui/painting/qcolor.h
+++ b/src/gui/painting/qcolor.h
@@ -225,6 +225,8 @@ public:
QT3_SUPPORT uint pixel(int screen = -1) const;
#endif
+ static bool isValidColor(const QString &name);
+
private:
#ifndef QT3_SUPPORT
// do not allow a spec to be used as an alpha value
@@ -232,6 +234,7 @@ private:
#endif
void invalidate();
+ bool setColorFromString(const QString &name);
Spec cspec;
union {