diff --git a/src/main.cpp b/src/main.cpp index 7be71ec..c6fd4b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,8 @@ #include "xsteam_ui.h" #include #include "xsteam_base_ex.h" +#include "sui_base_config.h" +#include "sui_base_ex.h" int main(int argc, char *argv[]) { qputenv("QT_QPA_PLATFORM", "windows:fontengine=freetype"); @@ -14,6 +16,7 @@ int main(int argc, char *argv[]) return -1; } a.installTranslator(&qtBaseTranslator); + sui_init_config(); xsteam_init_opts(); xsteam_ui x; x.xsteam_init_layout(); diff --git a/src/sui.h b/src/sui.h index 23c688b..07e6eb5 100644 --- a/src/sui.h +++ b/src/sui.h @@ -1,7 +1,6 @@ #ifndef SUI_H #define SUI_H -#include "common.h" #include "sui_title.h" typedef enum { diff --git a/src/sui_base.h b/src/sui_base.h new file mode 100644 index 0000000..c1b3d4f --- /dev/null +++ b/src/sui_base.h @@ -0,0 +1,30 @@ +#ifndef SUI_BASE_H +#define SUI_BASE_H +//解耦自定义窗体设定和全局参数设定的变量 + +#include + +typedef struct { + //样式选择 + int style = 0; + //是否跟随系统样式 + int style_system=0; + //字体选择 + int font = 0; + //字体粗细 + int font_bold=0; +}SUI_CONFIG; + +//窗体全局变量 +extern SUI_CONFIG sui_config; +//字体表变量 +extern QStringList font_table; +//样式表变量 +extern QStringList style_table; +//sui文件目录 +extern QString sui_cur_dir; +//sui文件路径 +extern QString sui_con_file; +//sui文件名 +extern QString sui_con_name; +#endif \ No newline at end of file diff --git a/src/sui_base_config.h b/src/sui_base_config.h new file mode 100644 index 0000000..8177bff --- /dev/null +++ b/src/sui_base_config.h @@ -0,0 +1,8 @@ +#include "sui_base.h" + +QStringList font_table; +QStringList style_table; +SUI_CONFIG sui_config; +QString sui_cur_dir; +QString sui_con_file; +QString sui_con_name="/save/sui.json"; \ No newline at end of file diff --git a/src/sui_base_ex.cpp b/src/sui_base_ex.cpp new file mode 100644 index 0000000..d5e5b3b --- /dev/null +++ b/src/sui_base_ex.cpp @@ -0,0 +1,114 @@ +#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; + QFontDatabase database; + QString temp_family_name; + foreach (const QString &family, database.families(QFontDatabase::SimplifiedChinese)) + { + if (temp_family_name != family) + { + font_table.append(family); + temp_family_name = family; + } + } +} +void sui_init_style_table() +{ + // 读取软件自带style样式到style_table变量中 + QDir *styleDir = new QDir(":/res/qss/"); + style_table = styleDir->entryList(QDir::Files); +} +void sui_init_config() +{ + sui_init_font_table(); + sui_init_style_table(); + sui_json_to_data(); +} \ No newline at end of file diff --git a/src/sui_base_ex.h b/src/sui_base_ex.h new file mode 100644 index 0000000..ca58b62 --- /dev/null +++ b/src/sui_base_ex.h @@ -0,0 +1,19 @@ +#ifndef SUI_BASE_EX_H +#define SUI_BASE_EX_H + +#include +#include +#include "common.h" +#include "sui_base.h" +#include +#include +#include "3rdparty/json/json.hpp" +using json = nlohmann::json; +namespace fs = std::filesystem; +extern void sui_init_config(); +extern void sui_init_font_table(); +extern void sui_init_style_table(); +extern void sui_json_to_data(); +extern json sui_data_to_json(json); +extern void sui_save_json(); +#endif \ No newline at end of file diff --git a/src/sui_title.cpp b/src/sui_title.cpp index 366b686..8d32d51 100644 --- a/src/sui_title.cpp +++ b/src/sui_title.cpp @@ -10,7 +10,7 @@ sui_title::sui_title(QWidget *parent) setObjectName(tr("sui_title")); for (int i = 0; i <= m_btn_obj_str.count() - 1; i++) { - QPushButton *btn = new QPushButton(this); + QPushButton *btn=new QPushButton(this); btn->setAutoFillBackground(true); btn->setFlat(true); btn->setObjectName(m_btn_obj_str[i]); @@ -70,7 +70,7 @@ void sui_title::btn_ico_menu() QAction *s_Action = new QAction(sName, s_QGroup); s_Action->setObjectName(QString::number(i)); s_Action->setCheckable(true); - if (_data.x_config.style_system) + if (sui_config.style_system) { s_Action->setEnabled(false); } @@ -78,7 +78,7 @@ void sui_title::btn_ico_menu() { connect(s_Action, SIGNAL(triggered(bool)), this, SLOT(slots_change_style())); } - if (i == _data.x_config.style) + if (i == sui_config.style) { s_Action->setChecked(true); } @@ -91,11 +91,11 @@ void sui_title::btn_ico_menu() QAction *t_action = new QAction("跟随系统", t_group); t_action->setObjectName("track_system"); t_action->setCheckable(true); - if (_data.x_config.style_system) + if (sui_config.style_system) { t_action->setChecked(true); } - connect(t_action, SIGNAL(triggered(bool)), this, SLOT(slots_style_system(bool))); + connect(t_action, SIGNAL(triggered(bool)), this, SLOT(slots_track_style_off_on_sys())); s_menu->addAction(t_action); } // 字体 @@ -108,7 +108,7 @@ void sui_title::btn_ico_menu() QAction *f_Action = new QAction(font_table[i], f_QGroup); f_Action->setObjectName(QString::number(i)); f_Action->setCheckable(true); - if (i == _data.x_config.font) + if (i == sui_config.font) { f_Action->setChecked(true); } @@ -122,7 +122,7 @@ void sui_title::btn_ico_menu() QAction *b_Action = new QAction("字体加粗", b_QGroup); b_Action->setObjectName("font_bold"); b_Action->setCheckable(true); - if (_data.x_config.font_bold) + if (sui_config.font_bold) { b_Action->setChecked(true); } @@ -139,28 +139,28 @@ void sui_title::btn_ico_menu() s_menu = nullptr; f_menu = nullptr; } -void sui_title::slots_style_system(bool state) +void sui_title::slots_track_style_off_on_sys() { - if (_data.x_config.style_system==1) + if (sui_config.style_system==1) { - _data.x_config.style_system = 0; + sui_config.style_system = 0; m_style->state = 0; } else { - _data.x_config.style_system = 1; + sui_config.style_system = 1; m_style->state = 1; } } void sui_title::slots_change_font_bold(bool state) { - if (_data.x_config.font_bold) + if (sui_config.font_bold) { - _data.x_config.font_bold = 0; + sui_config.font_bold = 0; } else { - _data.x_config.font_bold = 1; + sui_config.font_bold = 1; } init_font_style(); } @@ -168,31 +168,37 @@ void sui_title::slots_change_font() { if (sender() != nullptr) { - _data.x_config.font = sender()->objectName().toInt(); + sui_config.font = sender()->objectName().toInt(); } init_font_style(); } void sui_title::slots_change_style() { - if (sender() != nullptr) + if (sender() != nullptr) { - _data.x_config.style = sender()->objectName().toInt(); + sui_config.style = sender()->objectName().toInt(); + } + init_font_style(); +} +void sui_title::slots_change_style_sys(bool state){ + sui_config.style=0; + if(state){ + sui_config.style=1; } init_font_style(); } - void sui_title::init_font_style() { QString style; QString qss_font = "QWidget { "; - qss_font.append("font-family: '" + font_table[_data.x_config.font] + "';"); - if (_data.x_config.font_bold) + qss_font.append("font-family: '" + font_table[sui_config.font] + "';"); + if (sui_config.font_bold) { qss_font.append("font-weight: bold;"); } qss_font.append("}"); - QString style_file = tr(":/res/qss/") + style_table[_data.x_config.style]; + QString style_file = tr(":/res/qss/") + style_table[sui_config.style]; QFile qss(style_file); if (qss.open(QFile::ReadOnly)) { @@ -202,6 +208,9 @@ void sui_title::init_font_style() qss.close(); QApplication *main_app = static_cast(QCoreApplication::instance()); main_app->setStyleSheet(style); + qDebug()<<"font-family:"<state = _data.x_config.style_system; m_thread = new QThread(); m_style->moveToThread(m_thread); - connect(m_style, SIGNAL(signals_sys_style_change(bool)), this, SLOT(slots_style_system(bool))); + m_style->state = sui_config.style_system; + connect(m_style, SIGNAL(signals_sys_style_change(bool)), this, SLOT(slots_change_style_sys(bool))); connect(m_thread, SIGNAL(started()), m_style, SLOT(slots_sys_style_thread())); m_thread->start(); } diff --git a/src/sui_title.h b/src/sui_title.h index 722daf8..e32a39a 100644 --- a/src/sui_title.h +++ b/src/sui_title.h @@ -18,7 +18,7 @@ #include #include #include -#include "xsteam_ex.h" +#include "sui_base_ex.h" #include "sui_sys_style.h" //QD_CLOSE->表示子窗口状态 @@ -60,11 +60,12 @@ private: QStringList m_btn_obj_str={"btn_opt","btn_min","btn_max","btn_close"}; QStringList m_btn_tips_str={"功能","最小化","最大化","关闭"}; public slots: - void slots_on_clicked(); + void slots_on_clicked(); void slots_change_font(); void slots_change_style(); + void slots_change_style_sys(bool); void slots_change_font_bold(bool); - void slots_style_system(bool); + void slots_track_style_off_on_sys(); signals: void signals_close(); }; diff --git a/src/xsteam_base.h b/src/xsteam_base.h index 327d63a..e870603 100644 --- a/src/xsteam_base.h +++ b/src/xsteam_base.h @@ -59,11 +59,7 @@ typedef struct } UidData; typedef struct -{ - int style = 0; - int style_system=0; - int font = 0; - int font_bold=0; +{ std::string steam_dir={}; std::string steamtools_dir={}; std::string steamtools_st_dir={}; @@ -103,8 +99,4 @@ extern QStringList ex_all_dlc_table_head_tips; extern QStringList ex_default_src_name; extern QStringList ex_default_src_url; - -extern QStringList font_table; - -extern QStringList style_table; #endif \ No newline at end of file diff --git a/src/xsteam_base_ex.h b/src/xsteam_base_ex.h index 7ef5b54..c8e80a6 100644 --- a/src/xsteam_base_ex.h +++ b/src/xsteam_base_ex.h @@ -29,10 +29,6 @@ QStringList ex_default_src_url = {QStringLiteral("https://github.com/ikun0014/Ma QStringLiteral("https://githubfast.com/ManifestHub/ManifestHub.git"), }; -QStringList font_table; - -QStringList style_table; - GloablData _data; #endif // STEAM_BASE_EX_H \ No newline at end of file diff --git a/src/xsteam_ex.cpp b/src/xsteam_ex.cpp index e955038..b767f40 100644 --- a/src/xsteam_ex.cpp +++ b/src/xsteam_ex.cpp @@ -20,17 +20,6 @@ void xsteam_init_opts() xsteam_branch_tag_data_read(x.src_name); } } - QFontDatabase database; - QString temp_family_name; - foreach (const QString &family, database.families(QFontDatabase::SimplifiedChinese)) - { - if(temp_family_name!=family){ - font_table.append(family); - temp_family_name=family; - } - } - QDir *styleDir = new QDir(":/res/qss/"); - style_table = styleDir->entryList(QDir::Files); } void xsteam_vdf_free_dir_file(UidData u_data) { @@ -350,10 +339,6 @@ void xsteam_data_unserialize() } } } - _data.x_config.style = j3.at("xsteam_sets").at("style"); - _data.x_config.style_system = j3.at("xsteam_sets").at("style_system"); - _data.x_config.font = j3.at("xsteam_sets").at("font"); - _data.x_config.font_bold = j3.at("xsteam_sets").at("font_bold"); _data.x_config.steam_dir = j3.at("xsteam_sets").at("steam_dir"); _data.x_config.steamtools_dir = j3.at("xsteam_sets").at("steamtools_dir"); _data.x_config.steamtools_st_dir = j3.at("xsteam_sets").at("steamtools_st_dir"); @@ -374,10 +359,6 @@ void xsteam_data_unserialize() } // 默认配置 - _data.x_config.font = 0; - _data.x_config.font_bold = 0; - _data.x_config.style = 1; - _data.x_config.style_system = 0; QSettings settings("HKEY_CURRENT_USER\\Software\\Valve\\Steam", QSettings::NativeFormat); _data.x_config.steam_dir = (settings.value("SteamPath", "").toString()).toStdString(); _data.x_config.steam_api_url = "https://store.steampowered.com/api/appdetails/?appids="; @@ -436,10 +417,6 @@ json xsteam_data_serialize(json jsonData) } } } - jsonData[toStr(xsteam_sets)][toStr(style)] = _data.x_config.style; - jsonData[toStr(xsteam_sets)][toStr(font)] = _data.x_config.font; - jsonData[toStr(xsteam_sets)][toStr(style_system)] = _data.x_config.style_system; - jsonData[toStr(xsteam_sets)][toStr(font_bold)] = _data.x_config.font_bold; jsonData[toStr(xsteam_sets)][toStr(steam_dir)] = _data.x_config.steam_dir; jsonData[toStr(xsteam_sets)][toStr(steamtools_dir)] = _data.x_config.steamtools_dir; jsonData[toStr(xsteam_sets)][toStr(steamtools_st_dir)] = _data.x_config.steamtools_st_dir; diff --git a/src/xsteam_ui.cpp b/src/xsteam_ui.cpp index f8b3e08..f5b42ec 100644 --- a/src/xsteam_ui.cpp +++ b/src/xsteam_ui.cpp @@ -4,6 +4,7 @@ xsteam_ui::xsteam_ui() } xsteam_ui::~xsteam_ui() { + xsteam_save_data_write(); } void xsteam_ui::xsteam_init_layout() {