Merge pull request #9678 from Filoppi/tooltips_style

Make all Qt ToolTips look similar to BalloonTips
This commit is contained in:
JMC47
2021-05-17 16:40:49 -04:00
committed by GitHub
4 changed files with 60 additions and 48 deletions

View File

@ -10,6 +10,7 @@
#include <QFileInfo>
#include <QFontDatabase>
#include <QSize>
#include <QWidget>
#include "AudioCommon/AudioCommon.h"
@ -45,8 +46,6 @@ Settings::Settings()
g_controller_interface.RegisterDevicesChangedCallback(
[this] { QueueOnObject(this, [this] { emit DevicesChanged(); }); });
SetCurrentUserStyle(GetCurrentUserStyle());
}
Settings::~Settings() = default;
@ -80,10 +79,13 @@ QString Settings::GetCurrentUserStyle() const
return QFileInfo(GetQSettings().value(QStringLiteral("userstyle/path")).toString()).fileName();
}
// Calling this before the main window has been created breaks the style of some widgets on
// Windows 10/Qt 5.15.0. But only if we set a stylesheet that isn't an empty string.
void Settings::SetCurrentUserStyle(const QString& stylesheet_name)
{
QString stylesheet_contents;
// If we haven't found one, we continue with an empty (default) style
if (!stylesheet_name.isEmpty() && AreUserStylesEnabled())
{
// Load custom user stylesheet
@ -94,6 +96,26 @@ void Settings::SetCurrentUserStyle(const QString& stylesheet_name)
stylesheet_contents = QString::fromUtf8(stylesheet.readAll().data());
}
// Define tooltips style if not already defined
if (!stylesheet_contents.contains(QStringLiteral("QToolTip"), Qt::CaseSensitive))
{
const QPalette& palette = qApp->palette();
QColor window_color;
QColor text_color;
QColor unused_text_emphasis_color;
QColor border_color;
GetToolTipStyle(window_color, text_color, unused_text_emphasis_color, border_color, palette,
palette);
const auto tooltip_stylesheet =
QStringLiteral("QToolTip { background-color: #%1; color: #%2; padding: 8px; "
"border: 1px; border-style: solid; border-color: #%3; }")
.arg(window_color.rgba(), 0, 16)
.arg(text_color.rgba(), 0, 16)
.arg(border_color.rgba(), 0, 16);
stylesheet_contents.append(QStringLiteral("%1").arg(tooltip_stylesheet));
}
qApp->setStyleSheet(stylesheet_contents);
GetQSettings().setValue(QStringLiteral("userstyle/name"), stylesheet_name);
@ -109,6 +131,32 @@ void Settings::SetUserStylesEnabled(bool enabled)
GetQSettings().setValue(QStringLiteral("userstyle/enabled"), enabled);
}
void Settings::GetToolTipStyle(QColor& window_color, QColor& text_color,
QColor& emphasis_text_color, QColor& border_color,
const QPalette& palette, const QPalette& high_contrast_palette) const
{
const auto theme_window_color = palette.color(QPalette::Base);
const auto theme_window_hsv = theme_window_color.toHsv();
const auto brightness = theme_window_hsv.value();
const bool brightness_over_threshold = brightness > 128;
const QColor emphasis_text_color_1 = Qt::yellow;
const QColor emphasis_text_color_2 = QColor(QStringLiteral("#0090ff")); // ~light blue
if (Config::Get(Config::MAIN_USE_HIGH_CONTRAST_TOOLTIPS))
{
window_color = brightness_over_threshold ? QColor(72, 72, 72) : Qt::white;
text_color = brightness_over_threshold ? Qt::white : Qt::black;
emphasis_text_color = brightness_over_threshold ? emphasis_text_color_1 : emphasis_text_color_2;
border_color = high_contrast_palette.color(QPalette::Window).darker(160);
}
else
{
window_color = palette.color(QPalette::Window);
text_color = palette.color(QPalette::Text);
emphasis_text_color = brightness_over_threshold ? emphasis_text_color_2 : emphasis_text_color_1;
border_color = palette.color(QPalette::Text);
}
}
QStringList Settings::GetPaths() const
{
QStringList list;