DPS/DataPlatform/common.cpp

205 lines
3.9 KiB
C++
Raw Normal View History

2025-06-23 10:41:33 +08:00
#pragma execution_character_set("utf-8")
#include "common.h"
//#include <QCoreApplication>
#include <QFileDialog>
#include <QDebug>
#include "easylogging++.h"
#include "Session.h"
#include "mainwindow.h"
CommonHelper::CommonHelper()
{
}
CommonHelper::~CommonHelper()
{
}
bool CommonHelper::intTobool(int b)
{
return b == 1 ? true : false;
}
bool CommonHelper::compareQStringlist(QStringList& a, QStringList& b)
{
if (a.size() != b.size())
{
return false;
}
for (int i = 0; i < a.size(); i++)
{
2025-06-23 18:01:09 +08:00
//去掉前后空格
2025-06-23 10:41:33 +08:00
if ((a[i].simplified()).compare((b[i].simplified())) != 0)
{
return false;
}
}
return true;
}
bool CommonHelper::convertExcelboolValue(const QString& src, int& dst)
{
2025-06-23 18:01:09 +08:00
if (src.compare("") == 0)
2025-06-23 10:41:33 +08:00
{
dst = 1;
return true;
}
2025-06-23 18:01:09 +08:00
else if (src.compare("") == 0)
2025-06-23 10:41:33 +08:00
{
dst = 0;
return true;
}
else
{
return false;
}
}
QString CommonHelper::convertPropertyValue(const int src)
{
2025-06-23 18:01:09 +08:00
//只会有0.1值
2025-06-23 10:41:33 +08:00
if (src == 1)
{
2025-06-23 18:01:09 +08:00
return "";
2025-06-23 10:41:33 +08:00
}
else
{
2025-06-23 18:01:09 +08:00
return "";
2025-06-23 10:41:33 +08:00
}
}
2025-06-23 18:01:09 +08:00
//string转UTF8
2025-06-23 10:41:33 +08:00
std::string CommonHelper::stringToUtf8(const std::string& str) {
return QString::fromLocal8Bit(str.c_str()).toUtf8().toStdString();
}
std::string CommonHelper::utf8ToStdString(const char* str) {
return QString::fromUtf8(str).toLocal8Bit().toStdString();
}
std::string CommonHelper::utf8ToStdString(const std::string& str) {
return QString::fromUtf8(str.c_str()).toLocal8Bit().toStdString();
}
const char* CommonHelper::utf8ToString(const char* str) {
return QString::fromUtf8(str).toLocal8Bit().constData();
}
QString CommonHelper::utf8ToQString(const char* str) {
return QString::fromUtf8(str);
}
QString CommonHelper::utf8ToQString(std::string&& str) {
return QString::fromUtf8(str.c_str());
}
QString CommonHelper::jsonToQString(nlohmann::json& j) {
return QString::fromUtf8(j.dump().c_str());
}
const char* CommonHelper::jsonToString(nlohmann::json& j) {
return QString::fromUtf8(j.dump().c_str()).toLocal8Bit().constData();
}
std::string CommonHelper::qstringToUtf8(const QString& str) {
return str.toUtf8().toStdString();
}
QString CommonHelper::stringToQstring(const std::string& str) {
return QString::fromLocal8Bit(str.c_str(), static_cast<int>(str.size()));
}
std::string CommonHelper::qstringToStdString(const QString& str) {
return str.toLocal8Bit().toStdString();
}
const char* CommonHelper::qstringToString(const QString& str) {
return str.toLocal8Bit();
}
void CommonHelper::convertExcelInt(const QString& src, int& dst)
{
2025-06-23 18:01:09 +08:00
//如果excel数据项为空则转-1
2025-06-23 10:41:33 +08:00
if (src.isEmpty())
{
dst = -1;
}
else
{
int v = src.toInt();
dst = v < 0 ? -1 : v;
}
}
QString CommonHelper::convertFolderTypeToString(const int type)
{
if (type == 0)
{
2025-06-23 18:01:09 +08:00
return "分类容器";
2025-06-23 10:41:33 +08:00
}
else if (type == 1)
{
2025-06-23 18:01:09 +08:00
return "父数据类";
2025-06-23 10:41:33 +08:00
}
else if (type == 2)
{
2025-06-23 18:01:09 +08:00
return "临时数据模块";
2025-06-23 10:41:33 +08:00
}
2025-06-23 18:01:09 +08:00
return "无效类型";
2025-06-23 10:41:33 +08:00
}
QString CommonHelper::convertAttributeTypeTpString(const int type)
{
if (type == 1)
{
2025-06-23 18:01:09 +08:00
return "整型";
2025-06-23 10:41:33 +08:00
}
else if (type == 2)
{
2025-06-23 18:01:09 +08:00
return "字符型";
2025-06-23 10:41:33 +08:00
}
else if (type == 3)
{
2025-06-23 18:01:09 +08:00
return "实型";
2025-06-23 10:41:33 +08:00
}
2025-06-23 18:01:09 +08:00
return "无效类型";
2025-06-23 10:41:33 +08:00
}
QString CommonHelper::convertInterfaceTypeToStr(const int type)
{
if (type == 0)
{
return "void";
}
else if (type == 1)
{
return "static";
}
else if (type == 2)
{
return "virtual";
}
2025-06-23 18:01:09 +08:00
return "无效";
2025-06-23 10:41:33 +08:00
}
void CommonHelper::handleError(json& parameter, const char* error) {
LOG(INFO) << error;
2025-06-23 18:01:09 +08:00
parameter["error"] = "内部错误,详见日志文件";
2025-06-23 10:41:33 +08:00
}
void CommonHelper::message(const QString& message, int type) {
Session::getSession()->parent()->message(message, type);
}
bool CommonHelper::checkDir(QString fullPath)
{
QDir dir(fullPath);
if (dir.exists())
{
return true;
}
else
{
return dir.mkdir(fullPath);
}
}
void CommonHelper::deleteFilesInFolder(const QString& folderPath)
{
QDir folder(folderPath);
QStringList files = folder.entryList(QDir::Files);
foreach(const QString & file, files) {
folder.remove(file);
}
}