QtCanpool 3.3.0
载入中...
搜索中...
未找到
qxtheme.cpp
浏览该文件的文档.
1
5#include "qxtheme.h"
6
7#include <QtCore/QFile>
8
10
16#ifndef QX_THEME_STYLESHEET_PREFIX
17#define QX_THEME_STYLESHEET_PREFIX ":/qxribbon/res/stylesheets/"
18#endif
19
20namespace
21{
22
23struct ThemeInfo {
24 const char *id;
25 const char *name;
26 const char *fileName; // empty for Custom
27 bool dark;
28};
29
30const ThemeInfo kThemeInfo[] = {
31 {"light-yellow", "Light Yellow", "light_yellow.css", false}, // LightYellow
32 {"light-office2013", "Light Office 2013", "light_office2013.css", false}, // LightOffice2013
33 {"light-classic", "Light Classic", "light_classic.css", false}, // LightClassic
34 {"light-fancy", "Light Fancy", "light_fancy.css", false}, // LightFancy
35 {"dark-wps", "Dark WPS", "dark_wps.css", true}, // DarkWps
36 {"dark-officeplus", "Dark Office Plus", "dark_officeplus.css", true}, // DarkOfficePlus
37 {"custom", "Custom", "", false}, // Custom
38};
39
40const int kThemeCount = int(sizeof(kThemeInfo) / sizeof(kThemeInfo[0]));
41
43bool isValidTheme(Theme theme)
44{
45 const int index = int(theme);
46 return index >= 0 && index < kThemeCount;
47}
48
49const ThemeInfo *infoOf(Theme theme)
50{
51 return isValidTheme(theme) ? &kThemeInfo[int(theme)] : Q_NULLPTR;
52}
53
54} // namespace
55
56QString themeId(Theme theme)
57{
58 const ThemeInfo *info = infoOf(theme);
59 return info ? QString::fromLatin1(info->id) : QString();
60}
61
62QString themeName(Theme theme)
63{
64 const ThemeInfo *info = infoOf(theme);
65 return info ? QString::fromLatin1(info->name) : QString();
66}
67
68Theme themeFromId(const QString &id, Theme fallback)
69{
70 for (int i = 0; i < kThemeCount; ++i) {
71 if (id == QLatin1String(kThemeInfo[i].id)) {
72 return Theme(i);
73 }
74 }
75 return fallback;
76}
77
78bool isDarkTheme(Theme theme)
79{
80 const ThemeInfo *info = infoOf(theme);
81 return info ? info->dark : false;
82}
83
85{
86 const ThemeInfo *info = infoOf(theme);
87 if (!info || info->fileName[0] == '\0') {
88 return QString();
89 }
90 return QString::fromLatin1(QX_THEME_STYLESHEET_PREFIX) + QString::fromLatin1(info->fileName);
91}
92
93QString themeStyleSheet(Theme theme)
94{
95 const QString path = themeStyleSheetPath(theme);
96 if (path.isEmpty()) {
97 return QString();
98 }
99 QFile file(path);
100 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
101 return QString();
102 }
103 return QString::fromUtf8(file.readAll());
104}
105
bool isDarkTheme(Theme theme)
True for the themes that draw on a dark background.
Definition qxtheme.cpp:78
Theme themeFromId(const QString &id, Theme fallback)
Resolves an id produced by themeId(); fallback is returned for unknown ids.
Definition qxtheme.cpp:68
QString themeStyleSheet(Theme theme)
Content of the built-in style sheet, empty when the resource is missing.
Definition qxtheme.cpp:93
QString themeId(Theme theme)
Stable identifier written to the configuration.
Definition qxtheme.cpp:56
Theme
The themes understood by QxThemeManager.
Definition qxtheme.h:22
QString themeName(Theme theme)
Human readable name, suitable for a settings dialog.
Definition qxtheme.cpp:62
QString themeStyleSheetPath(Theme theme)
Resource path of the style sheet shipped with theme, empty for Custom.
Definition qxtheme.cpp:84
#define Q_NULLPTR
Copyleft (C) 2025 maminjie canpool@163.com SPDX-License-Identifier: MulanPSL-2.0
QFile file
Definition qxlogger.cpp:25
const char * fileName
Definition qxtheme.cpp:26
#define QX_THEME_STYLESHEET_PREFIX
Where the built-in style sheets are looked up.
Definition qxtheme.cpp:17
bool dark
Definition qxtheme.cpp:27
const char * name
Definition qxtheme.cpp:25
const char * id
Definition qxtheme.cpp:24
#define QX_THEME_BEGIN_NAMESPACE
#define QX_THEME_END_NAMESPACE