5#include "RabbitCommonDir.h"
10#include <QRegularExpression>
11#include <QLoggingCategory>
19static Q_LOGGING_CATEGORY(log,
"RabbitCommon.Style")
21CStyle::CStyle(QObject *parent) : QObject(parent)
23 qDebug(log) << Q_FUNC_INFO;
24 m_szDefaultFile = QString();
29 m_szDefaultIconTheme = QIcon::themeName();
30 if(m_szDefaultIconTheme.isEmpty()) {
31 m_szDefaultIconTheme =
"rabbit-black";
32 QIcon::setThemeName(m_szDefaultIconTheme);
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();
38 m_szDefaultFallbackIconTheme =
"rabbit-black";
39 QIcon::setFallbackThemeName(m_szDefaultFallbackIconTheme);
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"
47 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
48 <<
"Fallback search paths:" << QIcon::fallbackSearchPaths() <<
"\n"
53CStyle* CStyle::Instance()
55 static CStyle* p =
new CStyle();
56 if(!p) p =
new CStyle();
60void CStyle::SetFile(
const QString &file)
63 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
64 QSettings::IniFormat);
65 set.setValue(
"Style/File", m_szFile);
72 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
73 QSettings::IniFormat);
74 bool bIconTheme = set.value(
"Style/Icon/Theme/Enable",
true).toBool();
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());
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());
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"
102 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
103 <<
"Fallback search paths:" << QIcon::fallbackSearchPaths() <<
"\n"
108 QString szFile = set.value(
"Style/File", m_szFile).toString();
117 qApp->setStyleSheet(
"");
123 if(file.open(QFile::ReadOnly))
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())
135 QRegularExpression rePalette(
"#[0-9a-fA-F]+");
136 match = rePalette.match(matched);
139 QString paletteColor = match.captured(0);
140 qApp->setPalette(QPalette(QColor(paletteColor)));
146 int pos = rx.indexIn(stylesheet);
149 QString szPalette = rx.cap();
150 QRegExp rxPalette(
"#[0-9a-fA-F]+");
151 int pos = rxPalette.indexIn(stylesheet);
154 QString paletteColor = rxPalette.cap();
155 qApp->setPalette(QPalette(QColor(paletteColor)));
159 qApp->setStyleSheet(stylesheet);
164 qCritical(log) <<
"file open file fail:" << szFile;
170QString CStyle::GetDefaultStyle()
172 return m_szDefaultFile;
175QString CStyle::GetStyle()
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)
188 szPath =
"/usr/share/style";
190 if(!szFile.isEmpty()) {
191 QFileInfo fi(szFile);
192 szPath = fi.absoluteFilePath();
194 qDebug(log) <<
"Path:" << szPath;
195 QWidget* pParent =
dynamic_cast<QWidget*
>(this->parent());
196 szFile = QFileDialog::getOpenFileName(pParent, tr(
"Open sink"),
198 tr(
"Style files(*.qss *.css);; All files(*.*)"));
202QString CStyle::GetStyleFile()
204 if(!m_szFile.isEmpty())
205 if(!QFile::exists(m_szFile))
QString GetDirData(bool bReadOnly=false)
Get data directory.
int LoadStyle()
Load style from configure file.