账单记录器迁移。
This commit is contained in:
145
util.cpp
Normal file
145
util.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
#include "util.h"
|
||||
#include <QDir>
|
||||
#include <QUuid>
|
||||
#include <fstream>
|
||||
|
||||
static std::string gConfigPath;
|
||||
static std::string gWorkDir;
|
||||
static nlohmann::json gConfig;
|
||||
|
||||
void GenerateDefaultConfig(const std::string& path)
|
||||
{
|
||||
nlohmann::json config;
|
||||
config["MainWidth"] = 1000;
|
||||
config["MainHeight"] = 600;
|
||||
config["Editor"] = "Notepad";
|
||||
config["MediaDir"] = "./Media";
|
||||
config["CurCash"] = 0;
|
||||
config["CurSave"] = 0;
|
||||
std::ofstream configFile(path);
|
||||
configFile << config.dump(4);
|
||||
configFile.close();
|
||||
gConfig = config;
|
||||
}
|
||||
|
||||
Util::Util()
|
||||
{
|
||||
}
|
||||
|
||||
bool Util::Init()
|
||||
{
|
||||
gWorkDir = QDir::homePath().toStdString() + "/.config/SimpleAccount";
|
||||
if (!QDir(QString::fromStdString(gWorkDir)).exists()) {
|
||||
QDir().mkdir(QString::fromStdString(gWorkDir));
|
||||
}
|
||||
gConfigPath = gWorkDir + "/config.json";
|
||||
return LoadConfig();
|
||||
}
|
||||
|
||||
std::string Util::GetWorkDir()
|
||||
{
|
||||
return gWorkDir;
|
||||
}
|
||||
|
||||
std::string Util::GetMediaDir()
|
||||
{
|
||||
return gConfig["MediaDir"].get<std::string>();
|
||||
}
|
||||
|
||||
std::string Util::GetConfigPath()
|
||||
{
|
||||
return gConfigPath;
|
||||
}
|
||||
|
||||
std::pair<int, int> Util::GetMainSize()
|
||||
{
|
||||
return std::make_pair(gConfig["MainWidth"].get<int>(), gConfig["MainHeight"].get<int>());
|
||||
}
|
||||
|
||||
bool Util::SetMainSize(int w, int h)
|
||||
{
|
||||
LoadConfig();
|
||||
|
||||
gConfig["MainWidth"] = w;
|
||||
gConfig["MainHeight"] = h;
|
||||
|
||||
std::ofstream configFile(gConfigPath);
|
||||
if (!configFile.is_open()) {
|
||||
return false;
|
||||
}
|
||||
configFile << gConfig.dump(4);
|
||||
configFile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string Util::GetEditor()
|
||||
{
|
||||
return gConfig["Editor"].get<std::string>();
|
||||
}
|
||||
|
||||
QString Util::NewUUIDName(const QString& path)
|
||||
{
|
||||
QFileInfo fileInfo(path);
|
||||
QString originalSuffix = fileInfo.suffix();
|
||||
QString newName = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
||||
if (!originalSuffix.isEmpty()) {
|
||||
newName += "." + originalSuffix;
|
||||
}
|
||||
return newName;
|
||||
}
|
||||
|
||||
double Util::GetCurCash()
|
||||
{
|
||||
return gConfig["CurCash"].get<double>();
|
||||
}
|
||||
|
||||
double Util::GetCurSave()
|
||||
{
|
||||
return gConfig["CurSave"].get<double>();
|
||||
}
|
||||
|
||||
int Util::CashInt(double cash)
|
||||
{
|
||||
return static_cast<int32_t>(round(cash * 100));
|
||||
}
|
||||
|
||||
double Util::CashDouble(int cash)
|
||||
{
|
||||
return static_cast<double>(cash) / 100.0;
|
||||
}
|
||||
|
||||
bool Util::LoadConfig()
|
||||
{
|
||||
QFile file(QString::fromStdString(gConfigPath));
|
||||
if (file.exists()) {
|
||||
std::ifstream configFile(gConfigPath);
|
||||
if (!configFile.is_open()) {
|
||||
return false;
|
||||
}
|
||||
gConfig = nlohmann::json::parse(configFile);
|
||||
configFile.close();
|
||||
} else {
|
||||
GenerateDefaultConfig(gConfigPath);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
SharedData::~SharedData()
|
||||
{
|
||||
}
|
||||
|
||||
SharedData* SharedData::instance()
|
||||
{
|
||||
static SharedData data;
|
||||
return &data;
|
||||
}
|
||||
|
||||
void SharedData::ClearTt()
|
||||
{
|
||||
ttCashOut_ = 0;
|
||||
ttCashIn_ = 0;
|
||||
ttCreditIn_ = 0;
|
||||
ttCreditOut_ = 0;
|
||||
ttCreditCash_ = 0;
|
||||
ttCashPay_ = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user