kiran-qt5-integration/0011-fix-theme-Fix-the-use-of-QApplication-class-for-plat.patch

154 lines
5.7 KiB
Diff
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

From 09c229bb81e3fc77c4d80ed309ae4135b1ff7410 Mon Sep 17 00:00:00 2001
From: liuxinhao <liuxinhao@kylinsec.com.cn>
Date: Fri, 1 Dec 2023 15:48:32 +0800
Subject: [PATCH] fix(theme): Fix the use of QApplication class for platform
theme plugin errors
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 修复在平台主题插件中错误的使用QApplication类
---
platformtheme/kiran-theme.cpp | 50 +++++++++++++++++++++++++----------
platformtheme/kiran-theme.h | 1 +
2 files changed, 37 insertions(+), 14 deletions(-)
diff --git a/platformtheme/kiran-theme.cpp b/platformtheme/kiran-theme.cpp
index 86eb2e1..c1ca81f 100644
--- a/platformtheme/kiran-theme.cpp
+++ b/platformtheme/kiran-theme.cpp
@@ -14,8 +14,8 @@
#include "kiran-theme.h"
#include "kiran-appearance-monitor.h"
-#include "logging-category.h"
#include "kiran-integration-settings.h"
+#include "logging-category.h"
#include <private/qguiapplication_p.h>
#include <private/qiconloader_p.h>
@@ -27,6 +27,8 @@
#undef private
#include <QApplication>
+#include <QGuiApplication>
+
#include <QIcon>
#include <QLabel>
#include <QPixmap>
@@ -38,6 +40,10 @@
using namespace Kiran;
+// NOTE: QPlatformTheme插件是QGuiApplication中init_platform将会进行加载的
+// 1. 关于Application的调用QPlatformTheme中只能使用QGuiApplication。
+// 2. 若一定需要使用QApplication的相关方法得先判断Qt应用代码中是否使用的是QApplication
+
KiranTheme::KiranTheme(const QStringList& paramList)
: QGenericUnixTheme()
{
@@ -86,7 +92,7 @@ QVariant KiranTheme::themeHint(QPlatformTheme::ThemeHint hint) const
<< "/usr/share/icons"
<< "/usr/local/share/icons";
case StyleNames:
- return QStringList{"kiran","fusion"};
+ return QStringList{"kiran", "fusion"};
default:
break;
}
@@ -101,7 +107,7 @@ const QPalette* KiranTheme::palette(QPlatformTheme::Palette type) const
}
bool enable = KiranIntegrationSettings::appKiranStyleAvailable(qAppName());
- if( !enable )
+ if (!enable)
{
return QGenericUnixTheme::palette(type);
}
@@ -119,9 +125,9 @@ void KiranTheme::init()
m_scaleFactor = m_settingsMonitor->scaleFactor();
qDebug(kiranPlatformTheme) << "\tscale factor:" << m_scaleFactor;
- m_systemFont.setFamily(m_settingsMonitor->appFont().family());
- m_systemFont.setPointSize(m_settingsMonitor->appFont().pointSize());
- QApplication::setFont(m_systemFont);
+ updateAppFont(m_settingsMonitor->appFont().family(),
+ m_settingsMonitor->appFont().pointSize());
+
qDebug(kiranPlatformTheme) << "\tapplication font:" << m_settingsMonitor->appFont().family() << m_settingsMonitor->appFont().pointSize();
m_titleBarFont.setFamily(m_settingsMonitor->titleBarFont().family());
@@ -135,14 +141,31 @@ void KiranTheme::init()
QObject::connect(m_settingsMonitor, &KiranAppearanceMonitor::cursorThemeChanged, this, &KiranTheme::handleCursorThemeChanged);
// 不从KiranAppearanceMonitor接受主题变更事件修改为接受KiranPalette的主题变更信号能监听到系统主题变更以及应用程序手动指定主题
- //QObject::connect(m_settingsMonitor, &KiranAppearanceMonitor::gtkThemeChanged, this, &KiranTheme::handleThemeChanged);
- QObject::connect(StylePalette::instance(),&StylePalette::themeChanged,this,&KiranTheme::handleThemeChanged);
+ // QObject::connect(m_settingsMonitor, &KiranAppearanceMonitor::gtkThemeChanged, this, &KiranTheme::handleThemeChanged);
+ QObject::connect(StylePalette::instance(), &StylePalette::themeChanged, this, &KiranTheme::handleThemeChanged);
- QObject::connect(qApp, &QApplication::screenAdded, this, &KiranTheme::handleScreenAdded);
+ QObject::connect(qApp, &QGuiApplication::screenAdded, this, &KiranTheme::handleScreenAdded);
handleScaleFactorChanged(m_scaleFactor);
}
+void KiranTheme::updateAppFont(const QString& fontFamily, int pointSize)
+{
+ m_systemFont.setFamily(fontFamily);
+ m_systemFont.setPointSize(pointSize);
+
+ auto coreApp = QGuiApplication::instance();
+ if (!qobject_cast<QApplication*>(coreApp))
+ {
+ // FIXME: 使用QGuiApplication更新字体不全
+ QGuiApplication::setFont(m_systemFont);
+ }
+ else
+ {
+ QApplication::setFont(m_systemFont);
+ }
+}
+
const QFont* KiranTheme::font(QPlatformTheme::Font type) const
{
switch (type)
@@ -177,9 +200,8 @@ void KiranTheme::handleAppFontChanged()
<< m_settingsMonitor->appFont().family()
<< m_settingsMonitor->appFont().pointSize();
- m_systemFont.setFamily(m_settingsMonitor->appFont().family());
- m_systemFont.setPointSize(m_settingsMonitor->appFont().pointSize());
- QApplication::setFont(m_systemFont);
+ updateAppFont(m_settingsMonitor->appFont().family(),
+ m_settingsMonitor->appFont().pointSize());
}
void KiranTheme::handleTitleBarFontChanged()
@@ -295,8 +317,8 @@ void KiranTheme::handleScaleFactorChanged(int factor)
void KiranTheme::handleCursorThemeChanged()
{
// 强制让窗口更新光标
- QApplication::setOverrideCursor(QCursor());
- QApplication::restoreOverrideCursor();
+ QGuiApplication::setOverrideCursor(QCursor());
+ QGuiApplication::restoreOverrideCursor();
}
bool KiranTheme::enableRealTimeScaling()
diff --git a/platformtheme/kiran-theme.h b/platformtheme/kiran-theme.h
index 243789c..4ef9c49 100644
--- a/platformtheme/kiran-theme.h
+++ b/platformtheme/kiran-theme.h
@@ -41,6 +41,7 @@ public:
private:
void init();
+ void updateAppFont(const QString& fontFamily,int pointSize);
static bool enableRealTimeScaling();
private slots:
--
2.33.0