5#include "RabbitCommonDir.h"
10#include <QRegularExpression>
18Q_LOGGING_CATEGORY(LoggerStyle,
"RabbitCommon.Style")
20CStyle::CStyle(QObject *parent) : QObject(parent)
22 m_szDefaultFile = QString();
27 m_szDefaultIconTheme = QIcon::themeName();
28 if(m_szDefaultIconTheme.isEmpty())
29 m_szDefaultIconTheme =
"rabbit-black";
31#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) && !defined(Q_OS_WINDOWS)
32 if(!QIcon::fallbackThemeName().isEmpty())
33 m_szDefaultFallbackIconTheme = QIcon::fallbackThemeName();
34 if(m_szDefaultFallbackIconTheme.isEmpty())
35 m_szDefaultFallbackIconTheme =
"rabbit-black";
40CStyle* CStyle::Instance()
42 static CStyle* p =
new CStyle();
43 if(!p) p =
new CStyle();
47void CStyle::SetFile(
const QString &file)
50 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
51 QSettings::IniFormat);
52 set.setValue(
"Style/File", m_szFile);
59 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
60 QSettings::IniFormat);
61 bool bIconTheme = set.value(
"Style/Icon/Theme/Enable",
true).toBool();
63 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths()
64 << RabbitCommon::CDir::Instance()->GetDirIcons(
true));
65 QString szThemeName = QIcon::themeName();
66 if(szThemeName.isEmpty())
67 szThemeName = m_szDefaultIconTheme;
68 QIcon::setThemeName(set.value(
"Style/Icon/Theme",
69 szThemeName).toString());
71#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) && !defined(Q_OS_WINDOWS)
72 QIcon::setFallbackSearchPaths(QIcon::fallbackSearchPaths()
73 << RabbitCommon::CDir::Instance()->GetDirIcons(
true));
74 QString szFallbackThemeName = QIcon::fallbackThemeName();
75 if(szFallbackThemeName.isEmpty())
76 szFallbackThemeName = m_szDefaultFallbackIconTheme;
77 QIcon::setFallbackThemeName(set.value(
"Style/Icon/Theme/Fallback",
78 szFallbackThemeName).toString());
81 qDebug(LoggerStyle) <<
"Icon theme search paths:" << QIcon::themeSearchPaths() <<
"\n"
82 <<
"Icon theme name:" << QIcon::themeName() <<
"\n"
83 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
84 <<
"Fallback search paths:" << QIcon::fallbackSearchPaths() <<
"\n"
86 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
87 <<
"Fallback theme name:" << QIcon::fallbackThemeName() <<
"\n"
92 QString szFile = set.value(
"Style/File", m_szFile).toString();
101 qApp->setStyleSheet(
"");
107 if(file.open(QFile::ReadOnly))
109 qInfo(LoggerStyle) <<
"Load style file:" << szFile;
110 QString stylesheet= file.readAll();
111 QString pattern(
"QPalette\\{background:#[0-9a-fA-F]+;\\}");
112#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
113 QRegularExpression re(pattern);
114 QRegularExpressionMatch match = re.match(stylesheet);
115 if (match.hasMatch()) {
116 QString matched = match.captured(0);
117 if(!matched.isEmpty())
119 QRegularExpression rePalette(
"#[0-9a-fA-F]+");
120 match = rePalette.match(matched);
123 QString paletteColor = match.captured(0);
124 qApp->setPalette(QPalette(QColor(paletteColor)));
130 int pos = rx.indexIn(stylesheet);
133 QString szPalette = rx.cap();
134 QRegExp rxPalette(
"#[0-9a-fA-F]+");
135 int pos = rxPalette.indexIn(stylesheet);
138 QString paletteColor = rxPalette.cap();
139 qApp->setPalette(QPalette(QColor(paletteColor)));
143 qApp->setStyleSheet(stylesheet);
148 qCritical(LoggerStyle) <<
"file open file fail:" << szFile;
154QString CStyle::GetDefaultStyle()
156 return m_szDefaultFile;
159QString CStyle::GetStyle()
162 szFile = m_szDefaultFile;
163 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
164 QSettings::IniFormat);
165 szFile = set.value(
"Style/File", szFile).toString();
166 QString szPath = RabbitCommon::CDir::Instance()->
GetDirData(
true)
172 szPath =
"/usr/share/style";
174 if(!szFile.isEmpty()) {
175 QFileInfo fi(szFile);
176 szPath = fi.absoluteFilePath();
178 qDebug(LoggerStyle) <<
"Path:" << szPath;
179 QWidget* pParent =
dynamic_cast<QWidget*
>(this->parent());
180 szFile = QFileDialog::getOpenFileName(pParent, tr(
"Open sink"),
182 tr(
"Style files(*.qss *.css);; All files(*.*)"));
186QString CStyle::GetStyleFile()
188 if(!m_szFile.isEmpty())
189 if(!QFile::exists(m_szFile))
QString GetDirData(bool bReadOnly=false)
Get data directory.
int LoadStyle()
Load style from configure file.