From ebd3a13b807c6af2684b42d3912549caf7ef82aa Mon Sep 17 00:00:00 2001 From: Filipe Azevedo Date: Wed, 28 Feb 2018 14:44:06 +0100 Subject: 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 +++- 2 files changed, 44 insertions(+), 3 deletions(-) (limited to 'src/gui/kernel') 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)); } -- cgit v1.2.3