From ebd3a13b807c6af2684b42d3912549caf7ef82aa Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Wed, 28 Feb 2018 14:44:06 +0100 Subject: [PATCH] Add a QPalette color role for placeholder texts This allow to customize easily placeholders in QLineEdit by example. Change-Id: I2bb379164376e1d88b42d6c86c2e5b8df99fbc56 Reviewed-by: Richard Moe Gustavsen --- src/gui/kernel/qpalette.cpp | 43 ++++++++++++++++++- src/gui/kernel/qpalette.h | 4 +- .../qdatastream/tst_qdatastream.cpp | 22 +++++----- .../auto/gui/kernel/qpalette/tst_qpalette.cpp | 3 +- 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/gui/kernel/qpalette.cpp b/src/gui/kernel/qpalette.cpp index 4905e51e01..9ccfb9b819 100644 --- a/src/gui/kernel/qpalette.cpp +++ b/src/gui/kernel/qpalette.cpp @@ -315,6 +315,21 @@ static void qt_palette_from_color(QPalette &pal, const QColor &button) \sa ColorRole, brush() */ +/*! + \fn const QBrush & QPalette::placeholderText() const + \since 5.12 + + Returns the placeholder text brush of the current color group. + + \note Before Qt 5.12, the placeholder text color was hard-coded in the code as + QPalette::text().color() where an alpha of 128 was applied. + We continue to support this behavior by default, unless you set your own brush. + One can get back the original placeholder color setting the special QBrush default + constructor as placeholder brush. + + \sa ColorRole, brush() +*/ + /*! \fn ColorGroup QPalette::currentColorGroup() const @@ -444,6 +459,9 @@ static void qt_palette_from_color(QPalette &pal, const QColor &button) of QPalette, because tool tips are not active windows. + \value PlaceholderText Used as the placeholder color for various text input widgets. + This enum value has been introduced in Qt 5.12 + \value Text The foreground color used with \c Base. This is usually the same as the \c WindowText, in which case it must provide good contrast with \c Window and \c Base. @@ -765,11 +783,32 @@ void QPalette::setBrush(ColorGroup cg, ColorRole cr, const QBrush &b) cg = Active; } + // For placeholder we want to continue to respect the original behavior, which is + // derivating the text color, but only if user has not yet set his own brush. + // We then use Qt::NoBrush as an inernal way to know if the brush is customized or not. + + // ### Qt 6 - remove this special case + // Part 1 - Restore initial color to the given color group + if (cr == PlaceholderText && b == QBrush()) { + QColor col = brush(Text).color(); + col.setAlpha(128); + setBrush(cg, PlaceholderText, QBrush(col, Qt::NoBrush)); + return; + } + if (d->br[cg][cr] != b) { detach(); d->br[cg][cr] = b; } data.resolve_mask |= (1<br[cg][PlaceholderText].style() == Qt::NoBrush) { + QColor col = brush(Text).color(); + col.setAlpha(128); + setBrush(cg, PlaceholderText, QBrush(col, Qt::NoBrush)); + } } /*! @@ -962,7 +1001,7 @@ QDataStream &operator<<(QDataStream &s, const QPalette &p) for (int i = 0; i < NumOldRoles; ++i) s << p.d->br[grp][oldRoles[i]].color(); } else { - int max = QPalette::ToolTipText + 1; + int max = (int)QPalette::NColorRoles; if (s.version() <= QDataStream::Qt_2_1) max = QPalette::HighlightedText + 1; else if (s.version() <= QDataStream::Qt_4_3) @@ -1159,7 +1198,7 @@ QDebug operator<<(QDebug dbg, const QPalette &p) {"WindowText", "Button", "Light", "Midlight", "Dark", "Mid", "Text", "BrightText", "ButtonText", "Base", "Window", "Shadow", "Highlight", "HighlightedText", "Link", "LinkVisited", "AlternateBase", "NoRole", - "ToolTipBase","ToolTipText" }; + "ToolTipBase","ToolTipText", "PlaceholderText" }; QDebugStateSaver saver(dbg); QDebug nospace = dbg.nospace(); const uint mask = p.resolve(); diff --git a/src/gui/kernel/qpalette.h b/src/gui/kernel/qpalette.h index 71f3d0c3b8..071eddbc4d 100644 --- a/src/gui/kernel/qpalette.h +++ b/src/gui/kernel/qpalette.h @@ -96,7 +96,8 @@ public: AlternateBase, NoRole, ToolTipBase, ToolTipText, - NColorRoles = ToolTipText + 1, + PlaceholderText, + NColorRoles = PlaceholderText + 1, Foreground = WindowText, Background = Window }; Q_ENUM(ColorRole) @@ -141,6 +142,7 @@ public: inline const QBrush &highlightedText() const { return brush(HighlightedText); } inline const QBrush &link() const { return brush(Link); } inline const QBrush &linkVisited() const { return brush(LinkVisited); } + inline const QBrush &placeholderText() const { return brush(PlaceholderText); } bool operator==(const QPalette &p) const; inline bool operator!=(const QPalette &p) const { return !(operator==(p)); } diff --git a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp index 14a2528cc6..c6faf8c7d5 100644 --- a/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp +++ b/tests/auto/corelib/serialization/qdatastream/tst_qdatastream.cpp @@ -260,16 +260,16 @@ static int NColorRoles[] = { QPalette::HighlightedText + 1, // Qt_4_0, Qt_4_1 QPalette::HighlightedText + 1, // Qt_4_2 QPalette::AlternateBase + 1, // Qt_4_3 - QPalette::ToolTipText + 1, // Qt_4_4 - QPalette::ToolTipText + 1, // Qt_4_5 - QPalette::ToolTipText + 1, // Qt_4_6 - QPalette::ToolTipText + 1, // Qt_5_0 - QPalette::ToolTipText + 1, // Qt_5_1 - QPalette::ToolTipText + 1, // Qt_5_2 - QPalette::ToolTipText + 1, // Qt_5_3 - QPalette::ToolTipText + 1, // Qt_5_4 - QPalette::ToolTipText + 1, // Qt_5_5 - QPalette::ToolTipText + 1, // Qt_5_6 + QPalette::PlaceholderText + 1, // Qt_4_4 + QPalette::PlaceholderText + 1, // Qt_4_5 + QPalette::PlaceholderText + 1, // Qt_4_6 + QPalette::PlaceholderText + 1, // Qt_5_0 + QPalette::PlaceholderText + 1, // Qt_5_1 + QPalette::PlaceholderText + 1, // Qt_5_2 + QPalette::PlaceholderText + 1, // Qt_5_3 + QPalette::PlaceholderText + 1, // Qt_5_4 + QPalette::PlaceholderText + 1, // Qt_5_5 + QPalette::PlaceholderText + 1, // Qt_5_6 0 // add the correct value for Qt_5_7 here later }; @@ -2139,7 +2139,7 @@ void tst_QDataStream::setVersion() */ // revise the test if new color roles or color groups are added - QVERIFY(QPalette::NColorRoles == QPalette::ToolTipText + 1); + QVERIFY(QPalette::NColorRoles == QPalette::PlaceholderText + 1); QCOMPARE(int(QPalette::NColorGroups), 3); QByteArray ba2; diff --git a/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp b/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp index ca6f677ba6..a0ac1b3631 100644 --- a/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp +++ b/tests/auto/gui/kernel/qpalette/tst_qpalette.cpp @@ -67,9 +67,10 @@ void tst_QPalette::roleValues_data() QTest::newRow("QPalette::NoRole") << int(QPalette::NoRole) << 17; QTest::newRow("QPalette::ToolTipBase") << int(QPalette::ToolTipBase) << 18; QTest::newRow("QPalette::ToolTipText") << int(QPalette::ToolTipText) << 19; + QTest::newRow("QPalette::PlaceholderText") << int(QPalette::PlaceholderText) << 20; // Change this value as you add more roles. - QTest::newRow("QPalette::NColorRoles") << int(QPalette::NColorRoles) << 20; + QTest::newRow("QPalette::NColorRoles") << int(QPalette::NColorRoles) << 21; } void tst_QPalette::roleValues()