qt/LedOK/program/gentmpthread.cpp

626 lines
25 KiB
C++
Raw Normal View History

2022-10-27 15:07:45 +08:00
#include "gentmpthread.h"
2022-08-25 18:37:24 +08:00
#include "cfg.h"
#include "globaldefine.h"
#include "tools.h"
2023-04-18 14:14:46 +08:00
#include "program/eenviron.h"
#include "program/etext.h"
#include "program/evideo.h"
2022-08-25 18:37:24 +08:00
#include <QBuffer>
#include <QJsonArray>
#include <QProcess>
2023-04-18 14:14:46 +08:00
#include <QMessageBox>
2023-05-15 16:06:10 +08:00
#include <QPainter>
2022-08-25 18:37:24 +08:00
2023-05-11 11:47:00 +08:00
GenTmpThread::GenTmpThread(ProgItem *progItem, const QString &prog_name, const QString &zip_file, const QString &password) : mProgItem(progItem), prog_name(prog_name), zip_file(zip_file), password(password) {
2023-04-18 14:14:46 +08:00
connect(this, &QThread::finished, this, &QThread::deleteLater);
2022-01-04 18:11:48 +08:00
}
2022-10-27 15:07:45 +08:00
void GenTmpThread::run() {
2022-08-25 18:37:24 +08:00
auto srcDir = programsDir() + "/" + prog_name;
dstDir = srcDir + "_tmp";
//清空目录
2022-01-04 18:11:48 +08:00
emit sProgress(tr("Preparing ..."), 0);
2023-04-18 14:14:46 +08:00
QDir progsDir(programsDir());
progsDir.remove(prog_name + "_tmp.zip");
2022-08-25 18:37:24 +08:00
QDir dstQDir(dstDir);
if(! dstQDir.exists() || dstQDir.removeRecursively()) {
int iReTryCount = 0;
2023-04-18 14:14:46 +08:00
while(!progsDir.mkdir(prog_name + "_tmp")) {
2022-08-25 18:37:24 +08:00
QThread::msleep(250);
2022-01-04 18:11:48 +08:00
iReTryCount++;
2022-08-25 18:37:24 +08:00
if(iReTryCount > 4) break;
2022-01-04 18:11:48 +08:00
}
}
2023-04-18 14:14:46 +08:00
QFile jsonFile(srcDir+"/pro.json");
if(! jsonFile.open(QIODevice::ReadOnly)) {
2023-04-28 16:02:14 +08:00
emit onErr("Can't open "+srcDir+"/pro.json");
2023-04-18 14:14:46 +08:00
return;
}
auto data = jsonFile.readAll();
jsonFile.close();
QJsonParseError error;
auto proJson = QJsonDocument::fromJson(data, &error);
if(error.error != QJsonParseError::NoError) {
2023-04-28 16:02:14 +08:00
emit onErr("Parse "+srcDir+"/pro.json Error: "+error.errorString());
2023-04-18 14:14:46 +08:00
return;
}
2022-08-25 18:37:24 +08:00
//扫描节目, 返回多个节目数组
2022-01-04 18:11:48 +08:00
emit sProgress(tr("Scan program ..."), 30);
2022-08-25 18:37:24 +08:00
QStringList pageNames = QDir(srcDir).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
//查询 order 属性, 将最上层的放在转换后 layers 的最前面
//一个 page.json 对应节目任务中的一个 items 里的 program
QList<QJsonDocument> pageJsons;
foreach(QString pageName, pageNames) {
QFile jsonFile(srcDir+"/"+pageName+"/page.json");
2023-04-18 14:14:46 +08:00
if(jsonFile.open(QIODevice::ReadOnly)) {
auto data = jsonFile.readAll();
2022-08-25 18:37:24 +08:00
jsonFile.close();
2023-04-18 14:14:46 +08:00
auto pageJson = QJsonDocument::fromJson(data, &error);
if(error.error == QJsonParseError::NoError) pageJsons.append(pageJson);
2022-01-04 18:11:48 +08:00
}
}
2022-08-25 18:37:24 +08:00
std::sort(pageJsons.begin(), pageJsons.end(), [](const QJsonDocument &a, const QJsonDocument &b) {
2022-01-04 18:11:48 +08:00
return a["order"].toInt() < b["order"].toInt();
});
2023-04-18 14:14:46 +08:00
2022-08-25 18:37:24 +08:00
QJsonArray items;
foreach(QJsonDocument pageJson, pageJsons) {
srcPageDir = srcDir + "/" + pageJson["name"].toString();
2023-04-18 14:14:46 +08:00
items.append(cvtPage(pageJson, proJson));
2022-08-25 18:37:24 +08:00
}
emit sProgress(tr("Create json ..."), 60);
QJsonObject json;
json["_type"] = "PlayXixunTask";
json["id"] = QUuid::createUuid().toString(QUuid::WithoutBraces);
json["preDownloadURL"] = "http://192.168.8.202:23412/file/download?id=";
json["notificationURL"] = "http://192.168.8.202:23412/test";
json["task"] = QJsonObject{
{"_id", QUuid::createUuid().toString(QUuid::WithoutBraces)},
{"name", prog_name},
{"cmdId", QUuid::createUuid().toString(QUuid::WithoutBraces)},
{"items", items}
};
QFile program(dstDir + "/program");
if(program.open(QFile::WriteOnly)) {
program.write(QJsonDocument(json).toJson());
program.close();
}
//如果是usb更新则生成压缩包网络发送则不需要
if(! zip_file.isEmpty()) {
2023-05-15 16:06:10 +08:00
#ifdef Q_OS_WIN
2022-08-25 18:37:24 +08:00
QStringList args{"a", zip_file, dstDir+"/*"};
if(! password.isEmpty()) args << "-p"+password;
QProcess::execute("7z.exe", args);
2023-04-28 16:02:14 +08:00
#else
2023-05-05 16:10:49 +08:00
QStringList args{"-r", zip_file};
if(! password.isEmpty()) args << "-P" << password;
args += QDir(dstDir).entryList(QDir::AllEntries | QDir::NoDotAndDotDot);
QProcess process;
process.setWorkingDirectory(dstDir);
process.start("zip", args);
process.waitForFinished();
2023-04-28 16:02:14 +08:00
#endif
2022-01-04 18:11:48 +08:00
}
}
2022-08-25 18:37:24 +08:00
//此处需要把幻灯片中的元素按层顺序排序再放入layers中每个元素对一个layer。ewindow中的多个顺序元素为一个层上的时间轴上的素材
2023-04-18 14:14:46 +08:00
QJsonObject GenTmpThread::cvtPage(const QJsonDocument &pageJson, const QJsonDocument &proJson) {
2022-08-25 18:37:24 +08:00
auto audios = pageJson["audios"].toArray();
2023-04-18 14:14:46 +08:00
auto sourceRepeat = pageJson["loop"].toBool();
2022-08-25 18:37:24 +08:00
QJsonArray sources;
int start = 0;
2023-04-19 14:42:06 +08:00
foreach(QJsonValue audio, audios) {
2022-08-25 18:37:24 +08:00
auto dur = audio["dur"].toInt();
if(dur==0) continue;
auto name = audio["name"].toString();
if(name.isEmpty()) continue;
QString file = audio["dir"].toString()+"/"+name;
QFileInfo srcInfo(file);
if(! srcInfo.isFile()) continue;
QString id = Tools::fileMd5(file);
if(id.isEmpty()) continue;
QFile::copy(file, dstDir+"/"+id);
QJsonObject source;
source.insert("_type", "Audio");
source["id"] = id;
source["md5"] = id;
source["name"] = name;
source["size"] = srcInfo.size();
source["fileExt"] = srcInfo.suffix().toLower();
source["timeSpan"] = dur;
source["playTime"] = start;
source["vol"] = audio["vol"].toInt();
source["enabled"] = true;
source["left"] = -1;
source["top"] = -1;
source["width"] = 1;
source["height"] = 1;
sources.append(source);
start += dur;
}
QJsonArray layers;
2023-04-18 14:14:46 +08:00
if(! sources.isEmpty()) layers.append(QJsonObject{{"repeat", sourceRepeat}, {"sources", sources}});
2022-08-25 18:37:24 +08:00
auto elements = pageJson["elements"].toArray();
foreach(auto ele, elements) {
QString type = ele["elementType"].toString();
auto geometry = ele["geometry"];
QJsonArray sources;
if(type=="Window") {
auto elements = ele["elements"].toArray();
auto left = geometry["x"];
auto top = geometry["y"];
auto width = geometry["w"];
auto height = geometry["h"];
QList<QJsonObject> eles;
foreach(auto value, elements) eles.append(value.toObject());
std::sort(eles.begin(), eles.end(), [](const QJsonObject &a, const QJsonObject &b) {
return a["geometry"]["order"].toInt() < b["geometry"]["order"].toInt();
});
int playTime = 0;
foreach(const auto ele, eles) {
QJsonObject source = cvtEle(ele["elementType"].toString(), ele);
if(source.isEmpty()) continue;
source["left"] = left;
source["top"] = top;
source["width"] = width;
source["height"] = height;
source["playTime"] = playTime;
playTime += source["timeSpan"].toInt();
sources.append(source);
2022-01-04 18:11:48 +08:00
}
2022-08-25 18:37:24 +08:00
} else {
QJsonObject source = cvtEle(type, ele.toObject());
if(! source.isEmpty()) {
2023-04-18 14:14:46 +08:00
if(mProgItem->mMaxWidth && (type=="Photo" || type=="Movie")) {
source["left"] = 0;
source["top"] = 0;
source["width"] = mProgItem->mMaxWidth;
source["height"] = mProgItem->mHeight * mProgItem->mSplitWidths.size();
} else {
2022-10-27 15:07:45 +08:00
source["left"] = geometry["x"];
source["top"] = geometry["y"];
source["width"] = geometry["w"];
source["height"] = geometry["h"];
}
2022-08-25 18:37:24 +08:00
source["playTime"] = 0;
sources.append(source);
2022-01-04 18:11:48 +08:00
}
}
2022-08-25 18:37:24 +08:00
if(! sources.isEmpty()) {
2023-04-18 14:14:46 +08:00
QJsonObject layer{{"repeat", sourceRepeat}, {"sources", sources}};
2022-08-25 18:37:24 +08:00
auto border = ele["border"].toString();
if(! border.isEmpty()) {
QString bdSrc = ":res/borders/"+border;
QString id = Tools::fileMd5(bdSrc);
QFile::copy(bdSrc, dstDir+"/"+id);
auto borderSize = ele["borderSize"];
if(borderSize.isUndefined() || borderSize.isNull()){
QImage img(bdSrc);
borderSize = QJsonArray{img.width(), img.height()};
2022-01-04 18:11:48 +08:00
}
2022-08-25 18:37:24 +08:00
layer.insert("border", QJsonObject{
{"img", id},
{"eff", ele["borderEff"]},
{"speed", ele["borderSpeed"]},
{"img_size", borderSize},
{"geometry", QJsonArray{geometry["x"], geometry["y"], geometry["w"], geometry["h"]}}
});
2022-01-04 18:11:48 +08:00
}
2022-08-25 18:37:24 +08:00
layers.append(layer);
2022-01-04 18:11:48 +08:00
}
}
2022-08-25 18:37:24 +08:00
QJsonArray schedules, plans = pageJson["plans"].toArray();
auto validDate = pageJson["validDate"];
bool isValid = validDate["isValid"].toBool();
if(plans.isEmpty()) {
if(isValid) {
QJsonObject schedule;
schedule["dateType"] = "Range";
schedule["startDate"] = validDate["start"];
schedule["endDate"] = validDate["end"];
schedule["timeType"] = "All";
schedule["filterType"] = "None";
schedule["monthFilter"] = QJsonArray();
schedule["weekFilter"] = QJsonArray();
schedules.append(schedule);
2022-01-04 18:11:48 +08:00
}
2022-08-25 18:37:24 +08:00
} else {
foreach(QJsonValue plan, plans) {
QJsonObject schedule;
if(isValid) {
schedule["dateType"] = "Range";
schedule["startDate"] = validDate["start"];
schedule["endDate"] = validDate["end"];
} else schedule["dateType"] = "All";
schedule["timeType"] = "Range";
schedule["startTime"] = plan["start"];
schedule["endTime"] = plan["end"];
auto weekly = plan["weekly"];
schedule["weekFilter"] = weekly;
schedule["filterType"] = weekly.toArray().isEmpty() ? "None" : "Week";
schedule["monthFilter"] = QJsonArray();
schedules.append(schedule);
2022-01-04 18:11:48 +08:00
}
}
2022-08-25 18:37:24 +08:00
return QJsonObject{
{"_id", QUuid::createUuid().toString(QUuid::WithoutBraces)},
{"priority", 0},
{"version", 0},
{"schedules", schedules},
{"_program", QJsonObject{
{"_id", QUuid::createUuid().toString(QUuid::WithoutBraces)},
{"totalSize", 0},
{"name", pageJson["name"].toString()},
2023-04-18 14:14:46 +08:00
{"width", proJson["resolution"]["w"]},
{"height", proJson["resolution"]["h"]},
{"splitWidths", proJson["splitWidths"]},
2022-08-25 18:37:24 +08:00
{"_company", "alahover"},
{"_department", "alahover"},
{"_role", "alahover"},
{"_user", "alahover"},
{"__v", 0},
{"dateCreated", pageJson["last_edit"]},
{"layers", layers}
}},
{"repeatTimes", pageJson["repeat"]}
};
2022-01-04 18:11:48 +08:00
}
2022-10-27 15:07:45 +08:00
QJsonObject GenTmpThread::cvtEle(const QString &type, const QJsonObject &ele) {
2022-08-25 18:37:24 +08:00
if(type=="Text") return convertText(ele);
else if(type=="Photo") return convertPhoto(ele);
2022-10-27 15:07:45 +08:00
else if(type=="Movie") return EVideo::genProg(ele, dstDir, mProgItem);
2022-08-25 18:37:24 +08:00
else if(type=="Gif") return convertGif(ele);
else if(type=="DClock") return convertDClock(ele);
else if(type=="AClock") return convertAClock(ele);
else if(type=="Temp") return EEnviron::genProg(ele, dstDir, srcPageDir);
else if(type=="Web") return convertWeb(ele);
else if(type=="Timer") return convertTimer(ele);
return QJsonObject();
2022-01-04 18:11:48 +08:00
}
2022-10-27 15:07:45 +08:00
QJsonObject GenTmpThread::convertText(const QJsonObject &json) {
2022-08-25 18:37:24 +08:00
EText::Data attr;
EText::setElement(json, attr);
2023-04-18 14:14:46 +08:00
QJsonObject source;
auto type = mProgItem->mMaxWidth ? "SplitText" : "MultiPng";
source["_type"] = type;
source["name"] = type;
source["id"] = res_id++;
2022-08-25 18:37:24 +08:00
auto widget = json["widget"];
QJsonArray srcFiles = widget["files"].toArray();
2023-04-18 14:14:46 +08:00
source["iPicCount"] = srcFiles.size();
if(attr.playMode==EText::Flip) {
source["playMode"] = "Flip";
source["timeSpan"] = attr.flip.pageDuration * srcFiles.size();
if(attr.flip.effectDuration >= attr.flip.pageDuration) attr.flip.effectDuration = attr.flip.pageDuration / 2;
} else if(attr.playMode==EText::Scroll) {
source["playMode"] = "Scroll";
source["timeSpan"] = attr.scroll.duration;
} else if(attr.playMode==EText::Static) {
source["playMode"] = "Static";
source["timeSpan"] = attr.duration;
}
2022-08-25 18:37:24 +08:00
QJsonArray arrayPics;
QString filePrefix = srcPageDir+"/"+widget["idDir"].toString()+"/";
2023-04-18 14:14:46 +08:00
for(int i=0; i<srcFiles.size(); i++) {
2022-08-25 18:37:24 +08:00
QString srcFile = filePrefix + srcFiles[i].toString();
QFile srcQFile(srcFile);
if(! srcQFile.exists()) continue;
QJsonObject arrayPic;
QString id = Tools::fileMd5(srcFile);
srcQFile.copy(dstDir+"/"+id);
arrayPic["id"] = id;
arrayPic["mime"] = "image/png";
2023-04-18 14:14:46 +08:00
if(attr.playMode==EText::Flip) {
arrayPic["effect"] = attr.flip.effect;
arrayPic["effectSpeed"] = attr.flip.effectDuration;
arrayPic["picDuration"] = attr.flip.pageDuration;
} else if(attr.playMode==EText::Scroll) {
if(attr.scroll.effect==0) arrayPic["effect"] = "right to left";
else if(attr.scroll.effect==1) arrayPic["effect"] = "bottom to top";
else if(attr.scroll.effect==2) arrayPic["effect"] = "left to right";
else if(attr.scroll.effect==3) arrayPic["effect"] = "top to bottom";
arrayPic["effectSpeed"] = attr.scroll.effectSpeed;
2022-08-25 18:37:24 +08:00
arrayPic["picDuration"] = 0;
2023-04-18 14:14:46 +08:00
} else if(attr.playMode==EText::Static) {
arrayPic["effect"] = "no";
arrayPic["effectSpeed"] = 0;
arrayPic["picDuration"] = attr.duration;
2022-01-04 18:11:48 +08:00
}
2022-08-25 18:37:24 +08:00
arrayPics.append(arrayPic);
2022-01-04 18:11:48 +08:00
}
2023-04-18 14:14:46 +08:00
source["arrayPics"] = arrayPics;
return source;
2022-01-04 18:11:48 +08:00
}
//转换图片
2022-10-27 15:07:45 +08:00
QJsonObject GenTmpThread::convertPhoto(const QJsonObject &ele){
2022-08-25 18:37:24 +08:00
auto widget = ele["widget"];
auto name = widget["file"].toString();
QString srcFile = widget["path"].toString() + "/" + name;
QFileInfo srcInfo(srcFile);
QJsonObject source;
2022-10-27 15:07:45 +08:00
if(! srcInfo.isFile()) return source;
2023-04-18 14:14:46 +08:00
if(mProgItem->mMaxWidth) {
2022-10-27 15:07:45 +08:00
srcFile += "-square.png";
2022-08-25 18:37:24 +08:00
auto md5 = Tools::fileMd5(srcFile);
if(md5.isEmpty()) return source;
QFile::copy(srcFile, dstDir+"/"+md5);
source["id"] = md5;
source["md5"] = md5;
source["size"] = srcInfo.size();
source["name"] = name;
2022-10-27 15:07:45 +08:00
source["fileExt"] = "png";
source["mime"] = "image/png";
} else {
QImage img(srcFile);
auto geometry = ele["geometry"];
int width = geometry["w"].toInt();
int height = geometry["h"].toInt();
if(img.width() > width*2 && img.height() > height*2) {
QBuffer buf;
img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).save(&buf, "PNG");
QCryptographicHash cryptoHash(QCryptographicHash::Md5);
cryptoHash.addData(buf.data());
auto md5 = QString::fromLatin1(cryptoHash.result().toHex());
QFile file(dstDir+"/"+md5);
if(! file.open(QFile::WriteOnly)) return source;
file.write(buf.data());
file.close();
source["id"] = md5;
source["md5"] = md5;
source["size"] = buf.size();
source["name"] = srcInfo.baseName()+".png";
source["fileExt"] = "png";
source["mime"] = "image/png";
} else {
auto md5 = Tools::fileMd5(srcFile);
if(md5.isEmpty()) return source;
QFile::copy(srcFile, dstDir+"/"+md5);
source["id"] = md5;
source["md5"] = md5;
source["size"] = srcInfo.size();
source["name"] = name;
QString sufLower = srcInfo.suffix().toLower();
source["fileExt"] = sufLower;
source["mime"] = "image/"+(sufLower=="jpg" ? "jpeg" : sufLower);
}
2022-08-25 18:37:24 +08:00
}
source["_type"] = "Image";
auto play = ele["play"];
source["timeSpan"] = play["playDuration"];
source["enabled"] = true;
source["enabledBy"] = "";
source["oldFilePath"] = "";
static const int effSize = 13;
static const int effs[effSize] = {0,1,3,4,5,6,7,9,10,11,12,17,18};
int enterStyle = play["enterStyle"].toInt();
source["entryEffect"] = enterStyle < effSize ? effs[enterStyle] : 0;
source["exitEffect"] = 0;
source["entryEffectTimeSpan"] = play["enterDuration"];
source["exitEffectTimeSpan"] = 0;
return source;
2022-01-04 18:11:48 +08:00
}
//转换图片
2022-10-27 15:07:45 +08:00
QJsonObject GenTmpThread::convertGif(const QJsonObject &json) {
2022-08-25 18:37:24 +08:00
auto widget = json["widget"];
auto path = widget["path"].toString();
auto name = widget["file"].toString();
QString srcFile = path + "/" + name;
QFileInfo srcInfo(srcFile);
if(! srcInfo.isFile()) return QJsonObject();
QString id = Tools::fileMd5(srcFile);
if(id.isEmpty()) return QJsonObject();
QFile::copy(srcFile, dstDir+"/"+id);
2022-01-04 18:11:48 +08:00
QJsonObject oRes;
2022-08-25 18:37:24 +08:00
oRes["_type"] = "Image";
oRes["id"] = id;
oRes["md5"] = id;
oRes["name"] = name;
oRes["size"] = srcInfo.size();
QString suffix = srcInfo.suffix().toLower();
oRes["fileExt"] = suffix;
oRes["mime"] = "image/gif";
auto play = json["play"];
oRes["timeSpan"] = play["playDuration"].toInt() * play["playTimes"].toInt();
oRes["enabled"] = true;
oRes["entryEffect"] = "None";
oRes["exitEffect"] = "None";
oRes["entryEffectTimeSpan"] = 0;
oRes["exitEffectTimeSpan"] = 0;
2022-01-04 18:11:48 +08:00
return oRes;
}
2022-10-27 15:07:45 +08:00
QJsonObject GenTmpThread::convertDClock(const QJsonObject &json){
2022-01-04 18:11:48 +08:00
QJsonObject oRes;
2022-08-25 18:37:24 +08:00
oRes["_type"] = "DigitalClockNew";
oRes["name"] = "DigitalClockNew";
oRes["id"] = "";
oRes["timeSpan"] = json["play"]["duration"];
oRes["timezone"] = 8;
auto widget = json["widget"];
oRes["year"] = widget["year"];
oRes["month"] = widget["month"];
oRes["day"] = widget["day"];
oRes["hour"] = widget["hour"];
oRes["min"] = widget["min"];
oRes["sec"] = widget["sec"];
oRes["weekly"] = widget["weekly"];
oRes["fullYear"] = widget["fullYear"];
oRes["hour12"] = widget["12Hour"];
oRes["AmPm"] = widget["AmPm"];
oRes["dateStyle"] = widget["dateStyle"];
oRes["timeStyle"] = widget["timeStyle"];
oRes["multiline"] = widget["multiline"];
oRes["fontSize"] = widget["font"]["size"];
oRes["entryEffect"] = "None";
oRes["exitEffect"] = "None";
oRes["entryEffectTimeSpan"] = 0;
oRes["exitEffectTimeSpan"] = 0;
auto fontVal = widget["font"];
auto textColor = Tools::int2Color(fontVal["color"].toInt());
2023-04-27 15:06:24 +08:00
QFont font(fontVal["family"].toString());
font.setPixelSize(fontVal["size"].toInt());
2022-08-25 18:37:24 +08:00
font.setBold(fontVal["bold"].toBool());
font.setItalic(fontVal["italics"].toBool());
font.setUnderline(fontVal["underline"].toBool());
font.setStyleStrategy(gTextAntialiasing ? QFont::PreferAntialias : QFont::NoAntialias);
QFontMetricsF metricF(font);
oRes["spaceWidth"] = metricF.horizontalAdvance(" ");
QFontMetrics metric(font);
QColor color(textColor);
QJsonArray imgs;
for(auto str : str0_9) Tools::saveImg2(dstDir, metric, font, color, imgs, str, str);
Tools::saveImg2(dstDir, metric, font, color, imgs, tr("MON"), "MON");
Tools::saveImg2(dstDir, metric, font, color, imgs, tr("TUE"), "TUE");
Tools::saveImg2(dstDir, metric, font, color, imgs, tr("WED"), "WED");
Tools::saveImg2(dstDir, metric, font, color, imgs, tr("THU"), "THU");
Tools::saveImg2(dstDir, metric, font, color, imgs, tr("FRI"), "FRI");
Tools::saveImg2(dstDir, metric, font, color, imgs, tr("SAT"), "SAT");
Tools::saveImg2(dstDir, metric, font, color, imgs, tr("SUN"), "SUN");
Tools::saveImg2(dstDir, metric, font, color, imgs, tr("AM"), "AM");
Tools::saveImg2(dstDir, metric, font, color, imgs, tr("PM"), "PM");
Tools::saveImg2(dstDir, metric, font, color, imgs, "", "YEAR");
Tools::saveImg2(dstDir, metric, font, color, imgs, "", "MONTH");
Tools::saveImg2(dstDir, metric, font, color, imgs, "", "DAY");
Tools::saveImg2(dstDir, metric, font, color, imgs, ":", "maohao");
Tools::saveImg2(dstDir, metric, font, color, imgs, "/", "xiegang");
Tools::saveImg2(dstDir, metric, font, color, imgs, "-", "hengxian");
oRes["iPicCount"] = imgs.size();
oRes["arrayPics"] = imgs;
2022-01-04 18:11:48 +08:00
return oRes;
}
2022-10-27 15:07:45 +08:00
QJsonObject GenTmpThread::convertAClock(const QJsonObject &json) {
2022-08-25 18:37:24 +08:00
auto widget = json["widget"];
QString srcFile = srcPageDir + "/" + widget["selfCreateDialName"].toString();
QFile srcQFile(srcFile);
if(! srcQFile.exists()) return QJsonObject();
QString id = Tools::fileMd5(srcFile);
srcQFile.copy(dstDir+"/"+id);
2022-01-04 18:11:48 +08:00
QJsonObject oRes;
2022-08-25 18:37:24 +08:00
oRes["_type"] = "AnalogClock";
oRes["id"] = id;
oRes["md5"] = id;
oRes["mime"] = "image/png";
oRes["name"] = "001";
oRes["timeSpan"] = json["play"]["duration"];
2023-04-18 14:14:46 +08:00
oRes["shade"] = 0;//表盘形状
oRes["opacity"] = 1;//透明度
oRes["showBg"] = false;//是否显示背景色
oRes["bgColor"] = 0;
oRes["showHourScale"] = false;//是否显示时针
2022-08-25 18:37:24 +08:00
auto color = widget["hourMarkColor"];
oRes["scaleHourColor"] = color.isString() ? color : Tools::int2Color(color.toInt()).name();
color = widget["minMarkColor"];
oRes["scaleMinColor"] = color.isString() ? color : Tools::int2Color(color.toInt()).name();
color = widget["hourHandColor"];
oRes["pinHourColor"] = color.isString() ? color : Tools::int2Color(color.toInt()).name();
color = widget["minHandColor"];
oRes["pinMinColor"] = color.isString() ? color : Tools::int2Color(color.toInt()).name();
color = widget["secHandColor"];
oRes["pinSecColor"] = color.isString() ? color : Tools::int2Color(color.toInt()).name();
2023-04-18 14:14:46 +08:00
oRes["pinHourLen"] = widget["hhLen"].toInt();
oRes["pinMinLen"] = widget["mhLen"].toInt();
oRes["pinSecLen"] = widget["shLen"].toInt();
oRes["pinHourWidth"] = widget["hhWidth"].toInt();
oRes["pinMinWidth"] = widget["mhWidth"].toInt();
oRes["pinSecWidth"] = widget["shWidth"].toInt();
2022-08-25 18:37:24 +08:00
oRes["showMinScale"] = false;
oRes["scaleStyle"] = 0;
oRes["showScaleNum"] = false;
oRes["pinStyle"] = 1;
2023-04-18 14:14:46 +08:00
oRes["showSecond"] = widget["showSecHand"];
2022-01-04 18:11:48 +08:00
//下同Video
2022-08-25 18:37:24 +08:00
oRes["entryEffect"] = "None";
oRes["exitEffect"] = "None";
oRes["entryEffectTimeSpan"] = 0;
oRes["exitEffectTimeSpan"] = 0 ;
2022-01-04 18:11:48 +08:00
return oRes;
}
2022-10-27 15:07:45 +08:00
QJsonObject GenTmpThread::convertWeb(const QJsonObject &res) {
2022-08-25 18:37:24 +08:00
QJsonObject dst;
dst["_type"] = "WebURL";
dst["id"] = "";
dst["name"] = "WebURL";
dst["url"] = res["url"];
dst["timeSpan"] = res["duration"];
return dst;
2022-01-04 18:11:48 +08:00
}
2022-10-27 15:07:45 +08:00
QJsonObject GenTmpThread::convertTimer(const QJsonObject &json) {
2022-01-04 18:11:48 +08:00
QJsonObject oRes;
2022-08-25 18:37:24 +08:00
oRes["_type"] = "Timer";
oRes["name"] = "Timer";
oRes["id"] = "";
oRes["targetTime"] = json["targetTime"];
oRes["isDown"] = json["isDown"];
oRes["hasDay"] = json["hasDay"];
oRes["hasHour"] = json["hasHour"];
oRes["hasMin"] = json["hasMin"];
oRes["hasSec"] = json["hasSec"];
oRes["isMultiline"] = json["isMultiline"];
auto text = json["text"].toString();
oRes["text"] = text;
2023-04-27 15:06:24 +08:00
QFont font(json["font"].toString());
font.setPixelSize(json["fontSize"].toInt());
2022-08-25 18:37:24 +08:00
font.setBold(json["fontBold"].toBool());
font.setItalic(json["fontItalic"].toBool());
font.setUnderline(json["fontUnderline"].toBool());
oRes["font"] = font.family();
2023-04-23 10:04:49 +08:00
oRes["fontSize"] = font.pixelSize();
2022-08-25 18:37:24 +08:00
oRes["fontBold"] = font.bold();
oRes["fontItalic"] = font.italic();
oRes["fontUnderline"] = font.underline();
auto textColor = json["textColor"].toString();
oRes["textColor"] = textColor;
oRes["backColor"] = json["backColor"];
oRes["timeSpan"] = json["duration"];
font.setStyleStrategy(gTextAntialiasing ? QFont::PreferAntialias : QFont::NoAntialias);
QFontMetricsF metricF(font);
oRes["spaceWidth"] = metricF.horizontalAdvance(" ");
QFontMetrics metric(font);
QColor color(textColor);
QJsonObject imgs;
for(auto str : str0_9) Tools::saveImg(dstDir, metric, font, color, imgs, str, str);
Tools::saveImg(dstDir, metric, font, color, imgs, tr("day"), "day");
Tools::saveImg(dstDir, metric, font, color, imgs, tr("hour"), "hour");
Tools::saveImg(dstDir, metric, font, color, imgs, tr("min"), "min");
Tools::saveImg(dstDir, metric, font, color, imgs, tr("sec"), "sec");
2023-04-18 14:14:46 +08:00
if(! text.isEmpty()) {
auto innerW = json["innerW"].toInt();
auto innerH = json["innerH"].toInt();
auto rect = metric.boundingRect(0, 0, innerW, innerH, Qt::AlignCenter | Qt::TextWordWrap, text);
QImage img(qMin(rect.width(), innerW), qMin(rect.height(), innerH), QImage::Format_ARGB32);
img.fill(Qt::transparent);
{
QPainter painter(&img);
painter.setFont(font);
painter.setPen(color);
painter.drawText(QRectF(0, 0, img.width(), img.height()), text, QTextOption(Qt::AlignCenter));
2022-01-04 18:11:48 +08:00
}
2023-04-18 14:14:46 +08:00
QByteArray data;
QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
if(img.save(&buffer, "PNG")) {
QCryptographicHash cryptoHash(QCryptographicHash::Md5);
cryptoHash.addData(data);
auto md5 = QString::fromLatin1(cryptoHash.result().toHex());
QFile file(dstDir+"/"+md5);
if(file.open(QFile::WriteOnly)) {
file.write(data);
file.close();
imgs.insert("text", md5);
} else emit onErr("convertTimer file.open false");
} else emit onErr("convertTimer img.save false");
2022-01-04 18:11:48 +08:00
}
2022-08-25 18:37:24 +08:00
oRes["imgs"] = imgs;
2022-01-04 18:11:48 +08:00
return oRes;
}