RabbitCommon v2.2.6
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 <QDir>
12#include <QColor>
13#include <QPalette>
14#include <QIcon>
15
16namespace RabbitCommon {
17
18Q_LOGGING_CATEGORY(LoggerStyle, "RabbitCommon.Style")
19
20CStyle::CStyle(QObject *parent) : QObject(parent)
21{
22 m_szDefaultFile = QString(); /*RabbitCommon::CDir::Instance()->GetDirData(true)
23 + QDir::separator()
24 + "style" + QDir::separator()
25 + "white.qss"; //TODO: can modify default style //*/
26
27 m_szDefaultIconTheme = QIcon::themeName();
28 if(m_szDefaultIconTheme.isEmpty())
29 m_szDefaultIconTheme = "rabbit-black"; //TODO: can modify default icon theme
30
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"; //TODO: can modify default fallback icon theme
36#endif
37
38}
39
40CStyle* CStyle::Instance()
41{
42 static CStyle* p = new CStyle();
43 if(!p) p = new CStyle();
44 return p;
45}
46
47void CStyle::SetFile(const QString &file)
48{
49 m_szFile = file;
50 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
51 QSettings::IniFormat);
52 set.setValue("Style/File", m_szFile);
53 LoadStyle(m_szFile);
54}
55
57{
58 // Load icons theme
59 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
60 QSettings::IniFormat);
61 bool bIconTheme = set.value("Style/Icon/Theme/Enable", true).toBool();
62 if(bIconTheme) {
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());
70
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());
79#endif //QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
80
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"
85 #endif // QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
86 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
87 << "Fallback theme name:" << QIcon::fallbackThemeName() << "\n"
88 #endif //QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
89 ;
90 }
91
92 QString szFile = set.value("Style/File", m_szFile).toString();
93 return LoadStyle(szFile);
94}
95
96int CStyle::LoadStyle(const QString &szFile)
97{
98 m_szFile = szFile;
99 if(szFile.isEmpty())
100 {
101 qApp->setStyleSheet("");
102 //qApp->setPalette(QPalette(QColor(Qt::gray)));
103 }
104 else
105 {
106 QFile file(szFile);
107 if(file.open(QFile::ReadOnly))
108 {
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())
118 {
119 QRegularExpression rePalette("#[0-9a-fA-F]+");
120 match = rePalette.match(matched);
121 if(match.hasMatch())
122 {
123 QString paletteColor = match.captured(0);
124 qApp->setPalette(QPalette(QColor(paletteColor)));
125 }
126 }
127 }
128#else
129 QRegExp rx(pattern);
130 int pos = rx.indexIn(stylesheet);
131 if(pos > -1)
132 {
133 QString szPalette = rx.cap();
134 QRegExp rxPalette("#[0-9a-fA-F]+");
135 int pos = rxPalette.indexIn(stylesheet);
136 if(pos > -1)
137 {
138 QString paletteColor = rxPalette.cap();
139 qApp->setPalette(QPalette(QColor(paletteColor)));
140 }
141 }
142#endif
143 qApp->setStyleSheet(stylesheet);
144 file.close();
145 }
146 else
147 {
148 qCritical(LoggerStyle) << "file open file fail:" << szFile;
149 }
150 }
151 return 0;
152}
153
154QString CStyle::GetDefaultStyle()
155{
156 return m_szDefaultFile;
157}
158
159QString CStyle::GetStyle()
160{
161 QString szFile;
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)
167 + QDir::separator()
168 + "style";
169#ifdef Q_OS_LINUX
170 QDir d(szPath);
171 if(!d.exists())
172 szPath = "/usr/share/style";
173#endif
174 if(!szFile.isEmpty()) {
175 QFileInfo fi(szFile);
176 szPath = fi.absoluteFilePath();
177 }
178 qDebug(LoggerStyle) << "Path:" << szPath;
179 QWidget* pParent = dynamic_cast<QWidget*>(this->parent());
180 szFile = QFileDialog::getOpenFileName(pParent, tr("Open sink"),
181 szPath,
182 tr("Style files(*.qss *.css);; All files(*.*)"));
183 return szFile;
184}
185
186QString CStyle::GetStyleFile()
187{
188 if(!m_szFile.isEmpty())
189 if(!QFile::exists(m_szFile))
190 return "";
191 return m_szFile;
192}
193
194} //namespace RabbitCommon
QString GetDirData(bool bReadOnly=false)
Get data directory.
int LoadStyle()
Load style from configure file.
Definition Style.cpp:56