Qt: allow to use native styles

This commit is contained in:
Megamouse 2023-12-19 23:20:25 +01:00
parent 8b6fa32d12
commit 2eae0a9d3a
3 changed files with 51 additions and 0 deletions

View File

@ -36,6 +36,7 @@
#include <QFileInfo>
#include <QMessageBox>
#include <QTextDocument>
#include <QStyleFactory>
#include <clocale>
@ -746,14 +747,55 @@ void gui_application::OnChangeStyleSheetRequest()
const QString stylesheet_name = m_gui_settings->GetValue(gui::m_currentStylesheet).toString();
// Reset style to default before doing anything else, or we will get unexpected effects in custom stylesheets.
if (const QStringList styles = QStyleFactory::keys(); !styles.empty())
{
// The first style is the default style according to the Qt docs.
if (QStyle* style = QStyleFactory::create(styles.front()))
{
setStyle(style);
}
}
const auto match_native_style = [&stylesheet_name]() -> QString
{
// Search for "native (<style>)"
static const QRegularExpression expr(gui::NativeStylesheet + " \\((?<style>.*)\\)");
const QRegularExpressionMatch match = expr.match(stylesheet_name);
if (match.hasMatch())
{
return match.captured("style");
}
return {};
};
gui_log.notice("Changing stylesheet to '%s'", stylesheet_name);
if (stylesheet_name.isEmpty() || stylesheet_name == gui::DefaultStylesheet)
{
gui_log.notice("Using default stylesheet");
setStyleSheet(gui::stylesheets::default_style_sheet);
}
else if (stylesheet_name == gui::NoStylesheet)
{
gui_log.notice("Using empty style");
setStyleSheet("/* none */");
}
else if (const QString native_style = match_native_style(); !native_style.isEmpty())
{
if (QStyle* style = QStyleFactory::create(native_style))
{
gui_log.notice("Using native style '%s'", native_style);
setStyleSheet("/* none */");
setStyle(style);
}
else
{
gui_log.error("Failed to set stylesheet: Native style '%s' not available", native_style);
}
}
else
{
QString stylesheet_path;

View File

@ -89,6 +89,7 @@ namespace gui
const QString Settings = "CurrentSettings";
const QString DefaultStylesheet = "default";
const QString NoStylesheet = "none";
const QString NativeStylesheet = "native";
const QString main_window = "main_window";
const QString game_list = "GameList";

View File

@ -11,6 +11,7 @@
#include <QSpinBox>
#include <QTimer>
#include <QScreen>
#include <QStyleFactory>
#include "gui_settings.h"
#include "display_sleep_control.h"
@ -2412,6 +2413,13 @@ void settings_dialog::AddStylesheets()
ui->combo_stylesheets->clear();
ui->combo_stylesheets->addItem(tr("None", "Stylesheets"), gui::NoStylesheet);
for (const QString& key : QStyleFactory::keys())
{
// Variant value: "native (<style>)"
ui->combo_stylesheets->addItem(tr("Native (%0)", "Stylesheets").arg(key), QString("%0 (%1)").arg(gui::NativeStylesheet, key));
}
ui->combo_stylesheets->addItem(tr("Default (Bright)", "Stylesheets"), gui::DefaultStylesheet);
for (const QString& entry : m_gui_settings->GetStylesheetEntries())