sui从xsteam彻底分离,sui框架的设置单独存放在save/sui.json文件内,彻底从data.json中分离出来.是的后续窗体功能和软件功能分离实现,提高整体软件性能

This commit is contained in:
JackLee_CN 2024-12-03 19:09:32 +08:00
parent 9e4ce3fe4e
commit 9e987b6ffb
12 changed files with 216 additions and 66 deletions

View File

@ -2,6 +2,8 @@
#include "xsteam_ui.h" #include "xsteam_ui.h"
#include <QTranslator> #include <QTranslator>
#include "xsteam_base_ex.h" #include "xsteam_base_ex.h"
#include "sui_base_config.h"
#include "sui_base_ex.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
qputenv("QT_QPA_PLATFORM", "windows:fontengine=freetype"); qputenv("QT_QPA_PLATFORM", "windows:fontengine=freetype");
@ -14,6 +16,7 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
a.installTranslator(&qtBaseTranslator); a.installTranslator(&qtBaseTranslator);
sui_init_config();
xsteam_init_opts(); xsteam_init_opts();
xsteam_ui x; xsteam_ui x;
x.xsteam_init_layout(); x.xsteam_init_layout();

View File

@ -1,7 +1,6 @@
#ifndef SUI_H #ifndef SUI_H
#define SUI_H #define SUI_H
#include "common.h"
#include "sui_title.h" #include "sui_title.h"
typedef enum { typedef enum {

30
src/sui_base.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef SUI_BASE_H
#define SUI_BASE_H
//解耦自定义窗体设定和全局参数设定的变量
#include <QStringList>
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

8
src/sui_base_config.h Normal file
View File

@ -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";

114
src/sui_base_ex.cpp Normal file
View File

@ -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();
}

19
src/sui_base_ex.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef SUI_BASE_EX_H
#define SUI_BASE_EX_H
#include <QFontDatabase>
#include <QDir>
#include "common.h"
#include "sui_base.h"
#include <iostream>
#include <filesystem>
#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

View File

@ -10,7 +10,7 @@ sui_title::sui_title(QWidget *parent)
setObjectName(tr("sui_title")); setObjectName(tr("sui_title"));
for (int i = 0; i <= m_btn_obj_str.count() - 1; i++) 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->setAutoFillBackground(true);
btn->setFlat(true); btn->setFlat(true);
btn->setObjectName(m_btn_obj_str[i]); 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); QAction *s_Action = new QAction(sName, s_QGroup);
s_Action->setObjectName(QString::number(i)); s_Action->setObjectName(QString::number(i));
s_Action->setCheckable(true); s_Action->setCheckable(true);
if (_data.x_config.style_system) if (sui_config.style_system)
{ {
s_Action->setEnabled(false); 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())); 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); s_Action->setChecked(true);
} }
@ -91,11 +91,11 @@ void sui_title::btn_ico_menu()
QAction *t_action = new QAction("跟随系统", t_group); QAction *t_action = new QAction("跟随系统", t_group);
t_action->setObjectName("track_system"); t_action->setObjectName("track_system");
t_action->setCheckable(true); t_action->setCheckable(true);
if (_data.x_config.style_system) if (sui_config.style_system)
{ {
t_action->setChecked(true); 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); 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); QAction *f_Action = new QAction(font_table[i], f_QGroup);
f_Action->setObjectName(QString::number(i)); f_Action->setObjectName(QString::number(i));
f_Action->setCheckable(true); f_Action->setCheckable(true);
if (i == _data.x_config.font) if (i == sui_config.font)
{ {
f_Action->setChecked(true); f_Action->setChecked(true);
} }
@ -122,7 +122,7 @@ void sui_title::btn_ico_menu()
QAction *b_Action = new QAction("字体加粗", b_QGroup); QAction *b_Action = new QAction("字体加粗", b_QGroup);
b_Action->setObjectName("font_bold"); b_Action->setObjectName("font_bold");
b_Action->setCheckable(true); b_Action->setCheckable(true);
if (_data.x_config.font_bold) if (sui_config.font_bold)
{ {
b_Action->setChecked(true); b_Action->setChecked(true);
} }
@ -139,28 +139,28 @@ void sui_title::btn_ico_menu()
s_menu = nullptr; s_menu = nullptr;
f_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; m_style->state = 0;
} }
else else
{ {
_data.x_config.style_system = 1; sui_config.style_system = 1;
m_style->state = 1; m_style->state = 1;
} }
} }
void sui_title::slots_change_font_bold(bool state) 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 else
{ {
_data.x_config.font_bold = 1; sui_config.font_bold = 1;
} }
init_font_style(); init_font_style();
} }
@ -168,31 +168,37 @@ void sui_title::slots_change_font()
{ {
if (sender() != nullptr) if (sender() != nullptr)
{ {
_data.x_config.font = sender()->objectName().toInt(); sui_config.font = sender()->objectName().toInt();
} }
init_font_style(); init_font_style();
} }
void sui_title::slots_change_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(); init_font_style();
} }
void sui_title::init_font_style() void sui_title::init_font_style()
{ {
QString style; QString style;
QString qss_font = "QWidget { "; QString qss_font = "QWidget { ";
qss_font.append("font-family: '" + font_table[_data.x_config.font] + "';"); qss_font.append("font-family: '" + font_table[sui_config.font] + "';");
if (_data.x_config.font_bold) if (sui_config.font_bold)
{ {
qss_font.append("font-weight: bold;"); qss_font.append("font-weight: bold;");
} }
qss_font.append("}"); 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); QFile qss(style_file);
if (qss.open(QFile::ReadOnly)) if (qss.open(QFile::ReadOnly))
{ {
@ -202,6 +208,9 @@ void sui_title::init_font_style()
qss.close(); qss.close();
QApplication *main_app = static_cast<QApplication *>(QCoreApplication::instance()); QApplication *main_app = static_cast<QApplication *>(QCoreApplication::instance());
main_app->setStyleSheet(style); main_app->setStyleSheet(style);
qDebug()<<"font-family:"<<font_table[sui_config.font];
qDebug()<<"font-bold:"<<sui_config.font_bold;
qDebug()<<"style:"<<font_table[sui_config.font];
} }
// 双击标题栏进行界面的最大化/还原 // 双击标题栏进行界面的最大化/还原
@ -234,7 +243,8 @@ void sui_title::slots_on_clicked()
} }
else if (m_type == QD_TYPE::QD_EXIT) else if (m_type == QD_TYPE::QD_EXIT)
{ {
xsteam_save_data_write(); //保存窗体设置
sui_save_json();
QApplication::quit(); QApplication::quit();
} }
} }
@ -267,14 +277,14 @@ void sui_title::up_maximize()
void sui_title::set_type(QD_TYPE _type) void sui_title::set_type(QD_TYPE _type)
{ {
m_type = _type; m_type = _type;
if (m_type == QD_TYPE::QD_EXIT) if (m_type==QD_TYPE::QD_EXIT)
{ {
// 启动系统主题监听线程 // 启动系统主题监听线程
m_style = new sui_sys_style(); m_style = new sui_sys_style();
m_style->state = _data.x_config.style_system;
m_thread = new QThread(); m_thread = new QThread();
m_style->moveToThread(m_thread); 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())); connect(m_thread, SIGNAL(started()), m_style, SLOT(slots_sys_style_thread()));
m_thread->start(); m_thread->start();
} }

View File

@ -18,7 +18,7 @@
#include <QFontDatabase> #include <QFontDatabase>
#include <QStyleFactory> #include <QStyleFactory>
#include <QtConcurrent/QtConcurrent> #include <QtConcurrent/QtConcurrent>
#include "xsteam_ex.h" #include "sui_base_ex.h"
#include "sui_sys_style.h" #include "sui_sys_style.h"
//QD_CLOSE->表示子窗口状态 //QD_CLOSE->表示子窗口状态
@ -60,11 +60,12 @@ private:
QStringList m_btn_obj_str={"btn_opt","btn_min","btn_max","btn_close"}; QStringList m_btn_obj_str={"btn_opt","btn_min","btn_max","btn_close"};
QStringList m_btn_tips_str={"功能","最小化","最大化","关闭"}; QStringList m_btn_tips_str={"功能","最小化","最大化","关闭"};
public slots: public slots:
void slots_on_clicked(); void slots_on_clicked();
void slots_change_font(); void slots_change_font();
void slots_change_style(); void slots_change_style();
void slots_change_style_sys(bool);
void slots_change_font_bold(bool); void slots_change_font_bold(bool);
void slots_style_system(bool); void slots_track_style_off_on_sys();
signals: signals:
void signals_close(); void signals_close();
}; };

View File

@ -59,11 +59,7 @@ typedef struct
} UidData; } UidData;
typedef struct typedef struct
{ {
int style = 0;
int style_system=0;
int font = 0;
int font_bold=0;
std::string steam_dir={}; std::string steam_dir={};
std::string steamtools_dir={}; std::string steamtools_dir={};
std::string steamtools_st_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_name;
extern QStringList ex_default_src_url; extern QStringList ex_default_src_url;
extern QStringList font_table;
extern QStringList style_table;
#endif #endif

View File

@ -29,10 +29,6 @@ QStringList ex_default_src_url = {QStringLiteral("https://github.com/ikun0014/Ma
QStringLiteral("https://githubfast.com/ManifestHub/ManifestHub.git"), QStringLiteral("https://githubfast.com/ManifestHub/ManifestHub.git"),
}; };
QStringList font_table;
QStringList style_table;
GloablData _data; GloablData _data;
#endif // STEAM_BASE_EX_H #endif // STEAM_BASE_EX_H

View File

@ -20,17 +20,6 @@ void xsteam_init_opts()
xsteam_branch_tag_data_read(x.src_name); 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) 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.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_dir = j3.at("xsteam_sets").at("steamtools_dir");
_data.x_config.steamtools_st_dir = j3.at("xsteam_sets").at("steamtools_st_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); QSettings settings("HKEY_CURRENT_USER\\Software\\Valve\\Steam", QSettings::NativeFormat);
_data.x_config.steam_dir = (settings.value("SteamPath", "").toString()).toStdString(); _data.x_config.steam_dir = (settings.value("SteamPath", "").toString()).toStdString();
_data.x_config.steam_api_url = "https://store.steampowered.com/api/appdetails/?appids="; _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(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_dir)] = _data.x_config.steamtools_dir;
jsonData[toStr(xsteam_sets)][toStr(steamtools_st_dir)] = _data.x_config.steamtools_st_dir; jsonData[toStr(xsteam_sets)][toStr(steamtools_st_dir)] = _data.x_config.steamtools_st_dir;

View File

@ -4,6 +4,7 @@ xsteam_ui::xsteam_ui()
} }
xsteam_ui::~xsteam_ui() xsteam_ui::~xsteam_ui()
{ {
xsteam_save_data_write();
} }
void xsteam_ui::xsteam_init_layout() void xsteam_ui::xsteam_init_layout()
{ {