DPS/DataPlatform/common.cpp

205 lines
3.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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++)
{
//去掉前后空格
if ((a[i].simplified()).compare((b[i].simplified())) != 0)
{
return false;
}
}
return true;
}
bool CommonHelper::convertExcelboolValue(const QString& src, int& dst)
{
if (src.compare("") == 0)
{
dst = 1;
return true;
}
else if (src.compare("") == 0)
{
dst = 0;
return true;
}
else
{
return false;
}
}
QString CommonHelper::convertPropertyValue(const int src)
{
//只会有0.1值
if (src == 1)
{
return "";
}
else
{
return "";
}
}
//string转UTF8
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)
{
//如果excel数据项为空则转-1
if (src.isEmpty())
{
dst = -1;
}
else
{
int v = src.toInt();
dst = v < 0 ? -1 : v;
}
}
QString CommonHelper::convertFolderTypeToString(const int type)
{
if (type == 0)
{
return "分类容器";
}
else if (type == 1)
{
return "父数据类";
}
else if (type == 2)
{
return "临时数据模块";
}
return "无效类型";
}
QString CommonHelper::convertAttributeTypeTpString(const int type)
{
if (type == 1)
{
return "整型";
}
else if (type == 2)
{
return "字符型";
}
else if (type == 3)
{
return "实型";
}
return "无效类型";
}
QString CommonHelper::convertInterfaceTypeToStr(const int type)
{
if (type == 0)
{
return "void";
}
else if (type == 1)
{
return "static";
}
else if (type == 2)
{
return "virtual";
}
return "无效";
}
void CommonHelper::handleError(json& parameter, const char* error) {
LOG(INFO) << error;
parameter["error"] = "内部错误,详见日志文件";
}
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);
}
}