RabbitCommon v2.3.4
Loading...
Searching...
No Matches
FrmStyle.cpp
1// Copyright Copyright (c) Kang Lin studio, All Rights Reserved
2// Author Kang Lin <kl222@126.com>
3
4// See: https://github.com/KangLin/Documents/blob/master/qt/theme.md
5// - Icon Theme Specification: https://specifications.freedesktop.org/icon-theme-spec/latest/
6// - Icon Naming Specification: https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
7// - Icon Theme Specification: https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html
8
9#include <QSettings>
10#include <QLoggingCategory>
11#include <QPalette>
12#include <QIcon>
13#include <QStyleHints>
14#include <QStyleFactory>
15#include <QApplication>
16#include <QMessageBox>
17
18#include "FrmStyle.h"
19#include "ui_FrmStyle.h"
20#include "Style.h"
21#include "RabbitCommonDir.h"
22
23static Q_LOGGING_CATEGORY(log, "RabbitCommon.Style")
24
25CFrmStyle::CFrmStyle(QWidget *parent, Qt::WindowFlags f) :
26 QWidget(parent, f),
27 ui(new Ui::CFrmStyle)
28{
29 qDebug(log) << Q_FUNC_INFO;
30 setAttribute(Qt::WA_DeleteOnClose, true);
31 ui->setupUi(this);
32
33 auto pStyle = RabbitCommon::CStyle::Instance();
34 foreach(auto szName, QStyleFactory::keys()) {
35 if(-1 == ui->cbStyleName->findData(szName))
36 ui->cbStyleName->addItem(szName, szName);
37 }
38 if(!QStyleFactory::keys().isEmpty()) {
39 int index = ui->cbStyleName->findData(pStyle->GetStyleName());
40 if(-1 < index)
41 ui->cbStyleName->setCurrentIndex(index);
42 }
43
44 ui->leStyleSheet->setText(pStyle->GetStyleSheetFile());
45
46 ui->lbIconThemeChanged->setVisible(false);
47 // Icon theme
48 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
49 QSettings::IniFormat);
50 bool bIconTheme = set.value("Style/Icon/Theme/Enable", true).toBool();
51 ui->gpIconTheme->setChecked(bIconTheme);
52
53 qDebug(log)
54 << "Icon theme name:" << QIcon::themeName() << "\n"
55 << "Icon theme search paths:" << QIcon::themeSearchPaths() << "\n"
56 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
57 << "Fallback theme name:" << QIcon::fallbackThemeName() << "\n"
58 #endif //QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
59 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
60 << "Fallback search paths:" << QIcon::fallbackSearchPaths() << "\n"
61 #endif // QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
62 ;
63 foreach(auto d, QIcon::themeSearchPaths())
64 {
65 QDir dir(d);
66 if(!dir.exists())
67 {
68 qDebug(log)
69 << "Theme folder isn't exists:" << d;
70 continue;
71 }
72
73 qInfo(log) << "Theme folder:" << dir.absolutePath();
74// #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) && !defined(Q_OS_WINDOWS)
75// if(RabbitCommon::CDir::Instance()->GetDirIcons() == d) continue;
76// #endif
77 foreach(auto themeName, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
78 {
79 qDebug(log) << "Theme path:" << dir.absolutePath()
80 << "Theme:" << themeName;
81 QFileInfo fi(dir.absolutePath() + QDir::separator()
82 + themeName + QDir::separator() + "index.theme");
83 if(fi.exists())
84 {
85 qDebug(log) << "Theme:" << themeName;
86 ui->cbIconTheme->addItem(themeName);
87 } else {
88 qCritical(log) << "index.theme doesn't exists: " << fi.fileName()
89 << "Theme:" << themeName;
90 }
91 }
92 }
93 if(!QIcon::themeName().isEmpty())
94 ui->cbIconTheme->setCurrentText(QIcon::themeName());
95
96#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) && !defined(Q_OS_WINDOWS)
97 QDir fallbackDir(RabbitCommon::CDir::Instance()->GetDirIcons());
98 QStringList lstFallback;
99 if(fallbackDir.exists())
100 {
101 foreach(auto themeName, fallbackDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
102 {
103 qDebug(log) << "fallback path:" << fallbackDir.absolutePath()
104 << "Theme:" << themeName;
105 QFileInfo fi(fallbackDir.absolutePath() + QDir::separator()
106 + themeName + QDir::separator() + "index.theme");
107 if(!fi.exists())
108 continue;
109 qDebug(log) << "fallback Theme:" << themeName;
110 ui->cbFallbackTheme->addItem(themeName);
111 }
112 }
113
114 if(!QIcon::fallbackThemeName().isEmpty())
115 {
116 ui->cbFallbackTheme->setCurrentText(QIcon::fallbackThemeName());
117 }
118 ui->gbFallbackTheme->setVisible(true);
119#else
120 ui->gbFallbackTheme->setVisible(false);
121#endif
122
123}
124
125CFrmStyle::~CFrmStyle()
126{
127 qDebug(log) << "CFrmStyle::~CFrmStyle()";
128 delete ui;
129}
130
131void CFrmStyle::on_pbOK_clicked()
132{
133#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
134 const auto colorScheme = QGuiApplication::styleHints()->colorScheme();
135 QString szColorScheme;
136 QRegularExpression re("[Bb]lack|[Dd]ark");
137 QRegularExpressionMatch match = re.match(ui->cbIconTheme->currentText());
138 if (Qt::ColorScheme::Dark == colorScheme && match.hasMatch()) {
139 szColorScheme = tr("Dark");
140 }
141 QRegularExpression reWhite("[Ww]hite|[L|l]ight");
142 match = reWhite.match(ui->cbIconTheme->currentText());
143 if (Qt::ColorScheme::Light == colorScheme && match.hasMatch()) {
144 szColorScheme = tr("Light");
145 }
146 if(!szColorScheme.isEmpty()) {
147 QString szInfo = tr("Current system theme is") + " \"" + szColorScheme + "\", " + tr("current select theme is")
148 + QString(" \"") + ui->cbIconTheme->currentText() + "\". \n";
149 szInfo += tr("it's almost impossible to find the icon because its color matches the current system theme.") + "\n"
150 + tr("Are you sure you want to modify it?");
151 auto ret = QMessageBox::information(nullptr, tr("Change theme"),
152 szInfo,
153 QMessageBox::Ok | QMessageBox::No,
154 QMessageBox::No);
155 if(QMessageBox::No == ret)
156 return;
157 }
158#endif // QT_VERSION
159
160 QSettings set(RabbitCommon::CDir::Instance()->GetFileUserConfigure(),
161 QSettings::IniFormat);
162 bool bIconTheme = ui->gpIconTheme->isChecked();
163 set.setValue("Style/Icon/Theme/Enable", bIconTheme);
164 if(bIconTheme) {
165 if(!ui->cbIconTheme->currentText().isEmpty())
166 QIcon::setThemeName(ui->cbIconTheme->currentText());
167 set.setValue("Style/Icon/Theme", QIcon::themeName());
168
169#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
170 if(!ui->cbFallbackTheme->currentText().isEmpty())
171 QIcon::setFallbackThemeName(ui->cbFallbackTheme->currentText());
172 set.setValue("Style/Icon/Theme/Fallback", QIcon::fallbackThemeName());
173#endif
174 }
175
176 auto pStyle = RabbitCommon::CStyle::Instance();
177 pStyle->SetStyleSheetFile(ui->leStyleSheet->text());
178 pStyle->SetStyleName(ui->cbStyleName->currentData().toString());
179 pStyle->ReLoadStyle();
180
181 close();
182}
183
184void CFrmStyle::on_pbCancel_clicked()
185{
186 close();
187}
188
189void CFrmStyle::on_pbBrowse_clicked()
190{
191 ui->leStyleSheet->setText(RabbitCommon::CStyle::Instance()->GetStyleSheet());
192}
193
194void CFrmStyle::on_pbDefault_clicked()
195{
196 ui->leStyleSheet->setText("");
197 if(ui->cbStyleName->count() > 0)
198 ui->cbStyleName->setCurrentIndex(0);
199
200 if(ui->gpIconTheme->isChecked()) {
201 ui->cbIconTheme->setCurrentText(
202 RabbitCommon::CStyle::Instance()->m_szDefaultIconTheme);
203 ui->cbFallbackTheme->setCurrentText(
204 RabbitCommon::CStyle::Instance()->m_szDefaultFallbackIconTheme);
205 }
206}
207
208void CFrmStyle::on_gpIconTheme_clicked()
209{
210 ui->lbIconThemeChanged->setVisible(true);
211}
Set style and icon theme form.
Definition FrmStyle.h:89