RabbitCommon v2.3.3
Loading...
Searching...
No Matches
Style.cpp
1// Copyright Copyright (c) Kang Lin studio, All Rights Reserved
2// Author Kang Lin <kl222@126.com>
3
4#include "Style.h"
5#include "RabbitCommonDir.h"
6
7#include <QSettings>
8#include <QDebug>
9#include <QApplication>
10#include <QRegularExpression>
11#include <QLoggingCategory>
12#include <QDir>
13#include <QColor>
14#include <QPalette>
15#include <QIcon>
16
17namespace RabbitCommon {
18
19static Q_LOGGING_CATEGORY(log, "RabbitCommon.Style")
20
21CStyle::CStyle(QObject *parent) : QObject(parent)
22{
23 qDebug(log) << Q_FUNC_INFO;
24 m_szDefaultFile = QString(); /*RabbitCommon::CDir::Instance()->GetDirData(true)
25 + QDir::separator()
26 + "style" + QDir::separator()
27 + "white.qss"; //TODO: can modify default style //*/
28
29 m_szDefaultIconTheme = QIcon::themeName();
30 if(m_szDefaultIconTheme.isEmpty()) {
31 m_szDefaultIconTheme = "rabbit-black"; //TODO: can modify default icon theme
32 QIcon::setThemeName(m_szDefaultIconTheme);
33 }
34#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) && !defined(Q_OS_WINDOWS)
35 if(!QIcon::fallbackThemeName().isEmpty())
36 qDebug(log) << "Old icon fallback theme:" << QIcon::fallbackThemeName();
37 //Note: the fallback icon theme must set "rabbit-*"
38 m_szDefaultFallbackIconTheme = "rabbit-black"; //TODO: can modify default fallback icon theme
39 QIcon::setFallbackThemeName(m_szDefaultFallbackIconTheme);
40#endif
41 qDebug(log)
42 << "Icon theme name:" << QIcon::themeName() << "\n"
43 << "Icon theme search paths:" << QIcon::themeSearchPaths() << "\n"
44 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
45 << "Fallback theme name:" << QIcon::fallbackThemeName() << "\n"
46 #endif //QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
47 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
48 << "Fallback search paths:" << QIcon::fallbackSearchPaths() << "\n"
49 #endif // QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
50 ;
51}
52
53CStyle* CStyle::Instance()
54{
55 static CStyle* p = new CStyle();
56 if(!p) p = new CStyle();
57 return p;
58}
59
60void CStyle::SetFile(const QString &file)
61{
62 m_szFile = file;
63 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
64 QSettings::IniFormat);
65 set.setValue("Style/File", m_szFile);
66 LoadStyle(m_szFile);
67}
68
70{
71 // Load icons theme
72 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
73 QSettings::IniFormat);
74 bool bIconTheme = set.value("Style/Icon/Theme/Enable", true).toBool();
75 if(bIconTheme) {
76 QIcon::setThemeSearchPaths(
77 QIcon::themeSearchPaths()
78 << RabbitCommon::CDir::Instance()->GetDirIcons(true));
79 QString szThemeName = QIcon::themeName();
80 if(szThemeName.isEmpty())
81 szThemeName = m_szDefaultIconTheme;
82 QIcon::setThemeName(set.value("Style/Icon/Theme",
83 szThemeName).toString());
84
85#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) && !defined(Q_OS_WINDOWS)
86 QIcon::setFallbackSearchPaths(
87 QIcon::fallbackSearchPaths()
88 << RabbitCommon::CDir::Instance()->GetDirIcons(true));
89 QString szFallbackThemeName = QIcon::fallbackThemeName();
90 if(szFallbackThemeName.isEmpty())
91 szFallbackThemeName = m_szDefaultFallbackIconTheme;
92 QIcon::setFallbackThemeName(set.value("Style/Icon/Theme/Fallback",
93 szFallbackThemeName).toString());
94#endif //QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
95
96 qDebug(log) << Q_FUNC_INFO << "\n"
97 << "Icon theme name:" << QIcon::themeName() << "\n"
98 << "Icon theme search paths:" << QIcon::themeSearchPaths() << "\n"
99 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
100 << "Fallback theme name:" << QIcon::fallbackThemeName() << "\n"
101 #endif //QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
102 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
103 << "Fallback search paths:" << QIcon::fallbackSearchPaths() << "\n"
104 #endif // QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
105 ;
106 }
107
108 QString szFile = set.value("Style/File", m_szFile).toString();
109 return LoadStyle(szFile);
110}
111
112int CStyle::LoadStyle(const QString &szFile)
113{
114 m_szFile = szFile;
115 if(szFile.isEmpty())
116 {
117 qApp->setStyleSheet("");
118 //qApp->setPalette(QPalette(QColor(Qt::gray)));
119 }
120 else
121 {
122 QFile file(szFile);
123 if(file.open(QFile::ReadOnly))
124 {
125 qInfo(log) << "Load style file:" << szFile;
126 QString stylesheet= file.readAll();
127 QString pattern("QPalette\\{background:#[0-9a-fA-F]+;\\}");
128#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
129 QRegularExpression re(pattern);
130 QRegularExpressionMatch match = re.match(stylesheet);
131 if (match.hasMatch()) {
132 QString matched = match.captured(0);
133 if(!matched.isEmpty())
134 {
135 QRegularExpression rePalette("#[0-9a-fA-F]+");
136 match = rePalette.match(matched);
137 if(match.hasMatch())
138 {
139 QString paletteColor = match.captured(0);
140 qApp->setPalette(QPalette(QColor(paletteColor)));
141 }
142 }
143 }
144#else
145 QRegExp rx(pattern);
146 int pos = rx.indexIn(stylesheet);
147 if(pos > -1)
148 {
149 QString szPalette = rx.cap();
150 QRegExp rxPalette("#[0-9a-fA-F]+");
151 int pos = rxPalette.indexIn(stylesheet);
152 if(pos > -1)
153 {
154 QString paletteColor = rxPalette.cap();
155 qApp->setPalette(QPalette(QColor(paletteColor)));
156 }
157 }
158#endif
159 qApp->setStyleSheet(stylesheet);
160 file.close();
161 }
162 else
163 {
164 qCritical(log) << "file open file fail:" << szFile;
165 }
166 }
167 return 0;
168}
169
170QString CStyle::GetDefaultStyle()
171{
172 return m_szDefaultFile;
173}
174
175QString CStyle::GetStyle()
176{
177 QString szFile;
178 szFile = m_szDefaultFile;
179 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
180 QSettings::IniFormat);
181 szFile = set.value("Style/File", szFile).toString();
182 QString szPath = RabbitCommon::CDir::Instance()->GetDirData(true)
183 + QDir::separator()
184 + "style";
185#ifdef Q_OS_LINUX
186 QDir d(szPath);
187 if(!d.exists())
188 szPath = "/usr/share/style";
189#endif
190 if(!szFile.isEmpty()) {
191 QFileInfo fi(szFile);
192 szPath = fi.absoluteFilePath();
193 }
194 qDebug(log) << "Path:" << szPath;
195 QWidget* pParent = dynamic_cast<QWidget*>(this->parent());
196 szFile = QFileDialog::getOpenFileName(pParent, tr("Open sink"),
197 szPath,
198 tr("Style files(*.qss *.css);; All files(*.*)"));
199 return szFile;
200}
201
202QString CStyle::GetStyleFile()
203{
204 if(!m_szFile.isEmpty())
205 if(!QFile::exists(m_szFile))
206 return "";
207 return m_szFile;
208}
209
210} //namespace RabbitCommon
QString GetDirData(bool bReadOnly=false)
Get data directory.
int LoadStyle()
Load style from configure file.
Definition Style.cpp:69