summaryrefslogtreecommitdiffstats
path: root/src/gui/painting/qcolor.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2016-08-19 09:48:14 +0200
committerMarc Mutz <marc.mutz@kdab.com>2016-09-23 04:39:41 +0000
commit0889e9da20b8f016795ec2e7352b1e2f1678098d (patch)
treef311b708338554057dec9e5ba758c8cb18ac2c07 /src/gui/painting/qcolor.cpp
parentceb3c2ea621a5c5542eb6d127625a934b865e869 (diff)
QColor: provide QLatin1String overloads of functions taking QString
The inefficiency of QColor(const char*) came to light by a recent refactoring which showed that the existing char* overload of qt_get_hex_rgb() was never called. So, provide a QLatin1String interface for named colors that allows user code to reach that internal function without converting to QString first. Change-Id: I74df7b570ef28c00e35ca4adf46c4b7c7e9994b3 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Anton Kudryavtsev <a.kudryavtsev@netris.ru>
Diffstat (limited to 'src/gui/painting/qcolor.cpp')
-rw-r--r--src/gui/painting/qcolor.cpp44
1 files changed, 32 insertions, 12 deletions
diff --git a/src/gui/painting/qcolor.cpp b/src/gui/painting/qcolor.cpp
index 5f971599d2..05790c4504 100644
--- a/src/gui/painting/qcolor.cpp
+++ b/src/gui/painting/qcolor.cpp
@@ -323,7 +323,6 @@ static bool get_named_rgb_no_space(const char *name_no_space, QRgb *rgb)
return false;
}
-#ifdef QCOLOR_THIS_IS_CURRENTLY_UNUSED_BUT_WILL_BE_USED_SOON
static bool get_named_rgb(const char *name, int len, QRgb* rgb)
{
if (len > 255)
@@ -338,7 +337,6 @@ static bool get_named_rgb(const char *name, int len, QRgb* rgb)
return get_named_rgb_no_space(name_no_space, rgb);
}
-#endif
static bool get_named_rgb(const QChar *name, int len, QRgb *rgb)
{
@@ -796,12 +794,14 @@ QColor::QColor(Spec spec) Q_DECL_NOTHROW
/*!
\fn QColor::QColor(const char *name)
+ \overload
+ \sa setNamedColor(), name(), isValid()
+*/
- Constructs a named color in the same way as setNamedColor() using
- the given \a name.
-
- The color is left invalid if the \a name cannot be parsed.
-
+/*!
+ \fn QColor::QColor(QLatin1String name)
+ \overload
+ \since 5.8
\sa setNamedColor(), name(), isValid()
*/
@@ -882,6 +882,16 @@ void QColor::setNamedColor(const QString &name)
}
/*!
+ \overload
+ \since 5.8
+*/
+
+void QColor::setNamedColor(QLatin1String name)
+{
+ setColorFromString(name);
+}
+
+/*!
\since 4.7
Returns \c true if the \a name is a valid color name and can
@@ -897,16 +907,26 @@ bool QColor::isValidColor(const QString &name)
return !name.isEmpty() && QColor().setColorFromString(name);
}
-bool QColor::setColorFromString(const QString &name)
+/*!
+ \overload
+ \since 5.8
+*/
+bool QColor::isValidColor(QLatin1String name) Q_DECL_NOTHROW
+{
+ return name.size() && QColor().setColorFromString(name);
+}
+
+template <typename String>
+bool QColor::setColorFromString(const String &name)
{
- if (name.isEmpty()) {
+ if (!name.size()) {
invalidate();
return true;
}
- if (name.startsWith(QLatin1Char('#'))) {
+ if (name[0] == QLatin1Char('#')) {
QRgb rgba;
- if (get_hex_rgb(name.constData(), name.length(), &rgba)) {
+ if (get_hex_rgb(name.data(), name.size(), &rgba)) {
setRgba(rgba);
return true;
} else {
@@ -917,7 +937,7 @@ bool QColor::setColorFromString(const QString &name)
#ifndef QT_NO_COLORNAMES
QRgb rgb;
- if (get_named_rgb(name.constData(), name.length(), &rgb)) {
+ if (get_named_rgb(name.data(), name.size(), &rgb)) {
setRgba(rgb);
return true;
} else