From 72da039e54a62bf3a481fefc753e0e50ba75df57 Mon Sep 17 00:00:00 2001
From: Erik Verbruggen <erik.verbruggen@nokia.com>
Date: Mon, 1 Mar 2010 15:17:42 +0100
Subject: Added static method isValidColor to QColor.

Reviewed-by: Gunnar Sletta
---
 src/gui/painting/qcolor.cpp | 28 +++++++++++++++++++++++++---
 src/gui/painting/qcolor.h   |  3 +++
 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 {
-- 
cgit v0.12