134 lines
3.8 KiB
C++
134 lines
3.8 KiB
C++
#include "sui_base_ex.h"
|
|
|
|
void sui_json_to_data()
|
|
{
|
|
std::string json_str;
|
|
// 默认配置函数
|
|
auto default_config = [=]()
|
|
{
|
|
// 默认配置
|
|
sui_config.font = 0;
|
|
sui_config.font_bold = 0;
|
|
sui_config.style = 1;
|
|
sui_config.style_system = 0;
|
|
};
|
|
// 读取json文件函数
|
|
auto read_json = [=](QString _file, std::string &_str)
|
|
{
|
|
QFile json_file(_file);
|
|
if (!json_file.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
{
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
_str = json_file.readAll().toStdString();
|
|
}
|
|
json_file.close();
|
|
return true;
|
|
};
|
|
auto args_json = [=](json _data, SUI_CONFIG &_config)
|
|
{
|
|
_config.style = _data.at("sui").at("style");
|
|
_config.style_system = _data.at("sui").at("style_system");
|
|
_config.font = _data.at("sui").at("font");
|
|
_config.font_bold = _data.at("sui").at("font_bold");
|
|
};
|
|
// 检测文件是否存在
|
|
if (!QFile(sui_con_file).exists())
|
|
{
|
|
default_config();
|
|
return;
|
|
}
|
|
// 读取json文件内容
|
|
if (!read_json(sui_con_file, json_str))
|
|
{
|
|
default_config();
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
json data = json::parse(json_str);
|
|
if (data == nullptr)
|
|
{
|
|
default_config();
|
|
return;
|
|
}else{
|
|
args_json(data, sui_config);
|
|
}
|
|
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
// std::count << e.what() << '\n';
|
|
default_config();
|
|
}
|
|
}
|
|
json sui_data_to_json(json jsonData)
|
|
{
|
|
jsonData[toStr(sui)][toStr(style)] = sui_config.style;
|
|
jsonData[toStr(sui)][toStr(font)] = sui_config.font;
|
|
jsonData[toStr(sui)][toStr(style_system)] = sui_config.style_system;
|
|
jsonData[toStr(sui)][toStr(font_bold)] = sui_config.font_bold;
|
|
return jsonData;
|
|
}
|
|
void sui_save_json()
|
|
{
|
|
json jsonData = {};
|
|
jsonData = sui_data_to_json(jsonData);
|
|
fs::path json_path = sui_con_file.toStdString();
|
|
if (!fs::exists(json_path.parent_path()))
|
|
{
|
|
fs::create_directories(json_path.parent_path());
|
|
}
|
|
std::ofstream out(sui_con_file.toStdString(), std::ios::binary);
|
|
out << std::setw(4) << jsonData;
|
|
out.close();
|
|
}
|
|
void sui_init_font_table()
|
|
{
|
|
sui_cur_dir = QDir::currentPath();
|
|
sui_con_file = sui_cur_dir + sui_con_name;
|
|
sui_lang_dir = sui_cur_dir + "/lang/";
|
|
QFontDatabase database;
|
|
QString temp_family_name;
|
|
foreach (const QString &family, database.families(QFontDatabase::SimplifiedChinese))
|
|
{
|
|
if (temp_family_name != family)
|
|
{
|
|
sui_font_table.append(family);
|
|
temp_family_name = family;
|
|
}
|
|
}
|
|
}
|
|
void sui_init_style_table()
|
|
{
|
|
// 读取软件自带style样式到style_table变量中
|
|
QDir *styleDir = new QDir(":/res/qss/");
|
|
sui_style_table = styleDir->entryList(QDir::Files);
|
|
}
|
|
void sui_init_lang_table(){
|
|
try {
|
|
if (fs::exists(sui_lang_dir.toStdString()) && fs::is_directory(sui_lang_dir.toStdString())) {
|
|
for (const auto& entry : fs::directory_iterator(sui_lang_dir.toStdString())) {
|
|
if (fs::is_regular_file(entry.status())) {
|
|
std::cout << entry.path().filename() << std::endl;
|
|
//带后缀名entry.path().filename()
|
|
//不带后缀名entry.path().filename().replace_extension();
|
|
sui_lang_table.append(QString::fromStdString(entry.path().filename().replace_extension().string()));
|
|
}
|
|
}
|
|
} else {
|
|
std::cerr << "Path does not exist or is not a directory." << std::endl;
|
|
}
|
|
} catch (const fs::filesystem_error& e) {
|
|
std::cerr << e.what() << std::endl;
|
|
}
|
|
}
|
|
void sui_init_config()
|
|
{
|
|
sui_init_font_table();
|
|
sui_init_style_table();
|
|
sui_init_lang_table();
|
|
sui_json_to_data();
|
|
} |