diff options
-rw-r--r-- | src/gui/painting/qcolor.cpp | 33 | ||||
-rw-r--r-- | src/gui/painting/qcolor.h | 16 | ||||
-rw-r--r-- | tests/auto/gui/painting/qcolor/tst_qcolor.cpp | 27 |
3 files changed, 63 insertions, 13 deletions
diff --git a/src/gui/painting/qcolor.cpp b/src/gui/painting/qcolor.cpp index 9e1785c11d..3cb81a657f 100644 --- a/src/gui/painting/qcolor.cpp +++ b/src/gui/painting/qcolor.cpp @@ -79,7 +79,7 @@ static inline int hex2int(char s) return h < 0 ? h : (h << 4) | h; } -static bool get_hex_rgb(const char *name, int len, QRgb *rgb) +static bool get_hex_rgb(const char *name, size_t len, QRgb *rgb) { if (name[0] != '#') return false; @@ -124,12 +124,12 @@ bool qt_get_hex_rgb(const char *name, QRgb *rgb) return get_hex_rgb(name, qstrlen(name), rgb); } -static bool get_hex_rgb(const QChar *str, int len, QRgb *rgb) +static bool get_hex_rgb(const QChar *str, size_t len, QRgb *rgb) { if (len > 13) return false; char tmp[16]; - for (int i = 0; i < len; ++i) + for (size_t i = 0; i < len; ++i) tmp[i] = str[i].toLatin1(); tmp[len] = 0; return get_hex_rgb(tmp, len, rgb); @@ -858,6 +858,7 @@ QString QColor::name(NameFormat format) const return QString(); } +#if QT_STRINGVIEW_LEVEL < 2 /*! Sets the RGB value of this QColor to \a name, which may be in one of these formats: @@ -883,6 +884,17 @@ QString QColor::name(NameFormat format) const void QColor::setNamedColor(const QString &name) { + setColorFromString(QStringView(name)); +} +#endif + +/*! + \overload + \since 5.10 +*/ + +void QColor::setNamedColor(QStringView name) +{ setColorFromString(name); } @@ -896,6 +908,7 @@ void QColor::setNamedColor(QLatin1String name) setColorFromString(name); } +#if QT_STRINGVIEW_LEVEL < 2 /*! \since 4.7 @@ -909,7 +922,17 @@ void QColor::setNamedColor(QLatin1String name) */ bool QColor::isValidColor(const QString &name) { - return !name.isEmpty() && QColor().setColorFromString(name); + return isValidColor(QStringView(name)); +} +#endif + +/*! + \overload + \since 5.10 +*/ +bool QColor::isValidColor(QStringView name) Q_DECL_NOTHROW +{ + return name.size() && QColor().setColorFromString(name); } /*! @@ -922,7 +945,7 @@ bool QColor::isValidColor(QLatin1String name) Q_DECL_NOTHROW } template <typename String> -bool QColor::setColorFromString(const String &name) +bool QColor::setColorFromString(String name) { if (!name.size()) { invalidate(); diff --git a/src/gui/painting/qcolor.h b/src/gui/painting/qcolor.h index 22fcbcb2a8..e254de6ad0 100644 --- a/src/gui/painting/qcolor.h +++ b/src/gui/painting/qcolor.h @@ -72,7 +72,10 @@ public: inline QColor(int r, int g, int b, int a = 255); QColor(QRgb rgb) Q_DECL_NOTHROW; QColor(QRgba64 rgba64) Q_DECL_NOTHROW; +#if QT_STRINGVIEW_LEVEL < 2 inline QColor(const QString& name); +#endif + explicit inline QColor(QStringView name); inline QColor(const char *aname) : QColor(QLatin1String(aname)) {} inline QColor(QLatin1String name); QColor(Spec spec) Q_DECL_NOTHROW; @@ -95,7 +98,10 @@ public: QString name() const; QString name(NameFormat format) const; +#if QT_STRINGVIEW_LEVEL < 2 void setNamedColor(const QString& name); +#endif + void setNamedColor(QStringView name); void setNamedColor(QLatin1String name); static QStringList colorNames(); @@ -221,14 +227,17 @@ public: operator QVariant() const; +#if QT_STRINGVIEW_LEVEL < 2 static bool isValidColor(const QString &name); +#endif + static bool isValidColor(QStringView) Q_DECL_NOTHROW; static bool isValidColor(QLatin1String) Q_DECL_NOTHROW; private: void invalidate() Q_DECL_NOTHROW; template <typename String> - bool setColorFromString(const String &name); + bool setColorFromString(String name); Spec cspec; union { @@ -280,8 +289,13 @@ inline QColor::QColor(int r, int g, int b, int a) inline QColor::QColor(QLatin1String aname) { setNamedColor(aname); } +inline QColor::QColor(QStringView aname) +{ setNamedColor(aname); } + +#if QT_STRINGVIEW_LEVEL < 2 inline QColor::QColor(const QString& aname) { setNamedColor(aname); } +#endif #if QT_VERSION < QT_VERSION_CHECK(6,0,0) inline QColor::QColor(const QColor &acolor) Q_DECL_NOTHROW diff --git a/tests/auto/gui/painting/qcolor/tst_qcolor.cpp b/tests/auto/gui/painting/qcolor/tst_qcolor.cpp index 1ce7e797fc..6809aea086 100644 --- a/tests/auto/gui/painting/qcolor/tst_qcolor.cpp +++ b/tests/auto/gui/painting/qcolor/tst_qcolor.cpp @@ -545,19 +545,32 @@ void tst_QColor::setNamedColor_data() QColor bySetNamedColor; \ bySetNamedColor.setNamedColor(expr); \ auto byCtor = QColor(expr); \ - QTest::newRow(e.name + QByteArrayLiteral(#expr)) \ + QTest::addRow("%s: %s", e.name, #expr) \ << byCtor << bySetNamedColor << expected; \ } while (0) \ /*end*/ - ROW(QLatin1String(e.name)); - ROW(QString(QLatin1String(e.name))); + const auto l1 = QLatin1String(e.name); + const auto l1UpperBA = QByteArray(e.name).toUpper(); + const auto l1Upper = QLatin1String(l1UpperBA); + const auto l1SpaceBA = QByteArray(e.name).insert(1, ' '); + const auto l1Space = QLatin1String(l1SpaceBA); + + const auto u16 = QString(l1); + const auto u16Upper = u16.toUpper(); + const auto u16Space = QString(u16).insert(1, ' '); + + ROW(l1); + ROW(u16); + ROW(QStringView(u16)); // name should be case insensitive - ROW(QLatin1String(QByteArray(e.name).toUpper())); - ROW(QString(e.name).toUpper()); + ROW(l1Upper); + ROW(u16Upper); + ROW(QStringView(u16Upper)); // spaces should be ignored - ROW(QLatin1String(QByteArray(e.name).insert(1, ' '))); - ROW(QString(e.name).insert(1, ' ')); + ROW(l1Space); + ROW(u16Space); + ROW(QStringView(u16Space)); #undef ROW } } |