qt/LedOK/program/gentmpthread.cpp

615 lines
25 KiB
C++
Raw Normal View History

2022-10-27 15:07:45 +08:00
#include "gentmpthread.h"
2024-08-07 18:18:37 +08:00
#include "main.h"
2022-08-25 18:37:24 +08:00
#include "tools.h"
2023-04-18 14:14:46 +08:00
#include "program/eenviron.h"
#include "program/evideo.h"
2022-08-25 18:37:24 +08:00
#include <QBuffer>
#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";
//清空目录
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();
2023-10-23 14:58:29 +08:00
QString error;
auto proJson = JFrom(data, &error).toObj();
if(! error.isEmpty()) {
emit onErr("Parse "+srcDir+"/pro.json Error: "+error);
2023-04-18 14:14:46 +08:00
return;
}
2022-08-25 18:37:24 +08:00
//扫描节目, 返回多个节目数组
2024-02-28 11:34:33 +08:00
auto pageNames = QDir(srcDir).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
2022-08-25 18:37:24 +08:00
//查询 order 属性, 将最上层的放在转换后 layers 的最前面
//一个 page.json 对应节目任务中的一个 items 里的 program
2023-10-23 14:58:29 +08:00
std::vector<JObj> pageJsons;
for(auto &pageName : pageNames) {
2022-08-25 18:37:24 +08:00
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-10-23 14:58:29 +08:00
auto pageJson = JFrom(data, &error).toObj();
if(error.isEmpty()) pageJsons.emplace_back(pageJson);
2022-01-04 18:11:48 +08:00
}
}
2023-10-23 14:58:29 +08:00
std::sort(pageJsons.begin(), pageJsons.end(), [](const JObj &a, const JObj &b) {
2022-01-04 18:11:48 +08:00
return a["order"].toInt() < b["order"].toInt();
});
2023-10-23 14:58:29 +08:00
JArray items;
2024-02-21 18:08:50 +08:00
for(auto &pageJson : pageJsons) {
2022-08-25 18:37:24 +08:00
srcPageDir = srcDir + "/" + pageJson["name"].toString();
2024-02-21 18:08:50 +08:00
items.append(cvtPage(pageJson));
2022-08-25 18:37:24 +08:00
}
2023-10-23 14:58:29 +08:00
JObj json;
2022-08-25 18:37:24 +08:00
json["_type"] = "PlayXixunTask";
2023-10-23 14:58:29 +08:00
json["task"] = JObj{
2022-08-25 18:37:24 +08:00
{"name", prog_name},
2024-02-21 18:08:50 +08:00
{"width", proJson["resolution"]["w"]},
{"height", proJson["resolution"]["h"]},
2024-08-19 16:10:49 +08:00
{"insert", proJson["isInsert"]},
2024-02-21 18:08:50 +08:00
{"partLengths", proJson["splitWidths"]},
{"isVertical", proJson["isVer"]},
2022-08-25 18:37:24 +08:00
{"items", items}
};
QFile program(dstDir + "/program");
if(program.open(QFile::WriteOnly)) {
2023-10-23 14:58:29 +08:00
program.write(JToBytes(json, "\t"));
2022-08-25 18:37:24 +08:00
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中的多个顺序元素为一个层上的时间轴上的素材
2024-02-21 18:08:50 +08:00
JObj GenTmpThread::cvtPage(const JObj &pageJson) {
2023-10-23 14:58:29 +08:00
auto audios = pageJson("audios").toArray();
2023-04-18 14:14:46 +08:00
auto sourceRepeat = pageJson["loop"].toBool();
2023-10-23 14:58:29 +08:00
JArray sources;
2022-08-25 18:37:24 +08:00
int start = 0;
2023-10-23 14:58:29 +08:00
for(auto &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;
2024-02-21 18:08:50 +08:00
auto file = audio["dir"].toString()+"/"+name;
2022-08-25 18:37:24 +08:00
QFileInfo srcInfo(file);
if(! srcInfo.isFile()) continue;
2024-02-21 18:08:50 +08:00
auto id = Tools::fileMd5(file);
2022-08-25 18:37:24 +08:00
if(id.isEmpty()) continue;
QFile::copy(file, dstDir+"/"+id);
2023-10-23 14:58:29 +08:00
JObj source;
2022-08-25 18:37:24 +08:00
source.insert("_type", "Audio");
source["id"] = id;
source["md5"] = id;
source["timeSpan"] = dur;
source["playTime"] = start;
source["vol"] = audio["vol"].toInt();
source["left"] = -1;
source["top"] = -1;
source["width"] = 1;
source["height"] = 1;
sources.append(source);
start += dur;
}
2023-10-23 14:58:29 +08:00
JArray layers;
if(! sources.empty()) layers.append(JObj{{"repeat", sourceRepeat}, {"sources", sources}});
2022-08-25 18:37:24 +08:00
auto elements = pageJson["elements"].toArray();
2024-02-21 18:08:50 +08:00
for(const auto &ele : elements) {
2023-10-23 14:58:29 +08:00
auto type = ele["elementType"].toString();
2022-08-25 18:37:24 +08:00
auto geometry = ele["geometry"];
2024-06-20 10:44:08 +08:00
bool isWin = type=="Window";
sources = isWin ? genSources(QString(), ele["elements"].toArray()) : genSources(type, JArray{ele});
auto startTime = isWin ? 0 : ele["startTime"].toInt();
for(auto &ss : sources) {
auto source = ss.toObj();
source["left"] = geometry["x"];
source["top"] = geometry["y"];
source["width"] = geometry["w"];
source["height"] = geometry["h"];
source["rotate"] = ele["rotate"];
2024-08-07 18:18:37 +08:00
source["opacity"] = ele["opacity"].toDouble(1);
2024-06-20 10:44:08 +08:00
source["playTime"] = startTime;
startTime += source["timeSpan"].toInt();
}
2023-10-23 14:58:29 +08:00
if(! sources.empty()) {
JObj layer{{"repeat", sourceRepeat}, {"sources", sources}};
2022-08-25 18:37:24 +08:00
auto border = ele["border"].toString();
if(! border.isEmpty()) {
2024-08-13 19:47:06 +08:00
auto bdSrc = QApplication::applicationDirPath()+"/borders/"+border;
2024-02-21 18:08:50 +08:00
auto id = Tools::fileMd5(bdSrc);
2022-08-25 18:37:24 +08:00
QFile::copy(bdSrc, dstDir+"/"+id);
auto borderSize = ele["borderSize"];
2023-10-23 14:58:29 +08:00
if(borderSize.isNull()){
2022-08-25 18:37:24 +08:00
QImage img(bdSrc);
2023-10-23 14:58:29 +08:00
borderSize = JArray{img.width(), img.height()};
2022-01-04 18:11:48 +08:00
}
2023-10-23 14:58:29 +08:00
layer["border"] = JObj{
2022-08-25 18:37:24 +08:00
{"img", id},
{"eff", ele["borderEff"]},
{"speed", ele["borderSpeed"]},
{"img_size", borderSize},
2024-08-07 18:18:37 +08:00
{"geometry", JArray{geometry["x"], geometry["y"], geometry["w"], geometry["h"]}},
{"rotate", isWin ? 0 : ele["rotate"]}
2023-10-23 14:58:29 +08:00
};
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
}
}
2023-10-23 14:58:29 +08:00
JArray schedules, plans = pageJson["plans"].toArray();
2022-08-25 18:37:24 +08:00
auto validDate = pageJson["validDate"];
bool isValid = validDate["isValid"].toBool();
2023-10-23 14:58:29 +08:00
if(plans.empty()) {
2022-08-25 18:37:24 +08:00
if(isValid) {
2023-10-23 14:58:29 +08:00
JObj schedule;
2022-08-25 18:37:24 +08:00
schedule["dateType"] = "Range";
schedule["startDate"] = validDate["start"];
schedule["endDate"] = validDate["end"];
schedule["timeType"] = "All";
schedule["filterType"] = "None";
2023-10-23 14:58:29 +08:00
schedule["weekFilter"] = JArray();
2022-08-25 18:37:24 +08:00
schedules.append(schedule);
2022-01-04 18:11:48 +08:00
}
2022-08-25 18:37:24 +08:00
} else {
2023-10-23 14:58:29 +08:00
for(auto &plan : plans) {
JObj schedule;
2022-08-25 18:37:24 +08:00
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;
2023-10-23 14:58:29 +08:00
schedule["filterType"] = weekly.toArray().empty() ? "None" : "Week";
2022-08-25 18:37:24 +08:00
schedules.append(schedule);
2022-01-04 18:11:48 +08:00
}
}
2023-10-23 14:58:29 +08:00
return JObj{
2024-02-21 18:08:50 +08:00
{"repeatTimes", pageJson["repeat"]},
2022-08-25 18:37:24 +08:00
{"schedules", schedules},
2023-10-23 14:58:29 +08:00
{"_program", JObj{
2022-08-25 18:37:24 +08:00
{"name", pageJson["name"].toString()},
{"layers", layers}
2024-02-21 18:08:50 +08:00
}}
2022-08-25 18:37:24 +08:00
};
2022-01-04 18:11:48 +08:00
}
2024-06-20 10:44:08 +08:00
JArray GenTmpThread::genSources(QString type, const JArray &eles) {
2024-02-21 18:08:50 +08:00
JArray sources;
auto needType = type.isEmpty();
for(const auto &ele : eles) {
JObj source;
if(needType) type = ele["elementType"].toString();
if(type=="Text") source = genText(ele, sources);
else if(type=="Image"||type=="Photo") source = genImage(ele);
2024-08-07 18:18:37 +08:00
else if(type=="Video"||type=="Movie") {
//genProg(ele, dstDir, mProgItem);
auto widget = ele["widget"];
if(widget.isNull()) widget = ele;
auto path = widget["path"].toString();
auto name = widget["file"].toString();
auto srcFile = path + "/" + name;
if(! QFileInfo(srcFile).isFile() && ! QFileInfo(srcFile = srcPageDir + "/" + name).isFile()) continue;
auto id = Tools::fileMd5(srcFile);
if(id.isEmpty()) continue;
QFile::copy(srcFile, dstDir+"/"+id);
source["_type"] = "Video";
source["id"] = id;
source["md5"] = id;
source["name"] = name;
auto play = ele["play"];
source["timeSpan"] = play.isNull() ? ele["duration"].toInt() * ele["playTimes"].toInt() : play["playDuration"].toInt() * play["playTimes"].toInt();
} else if(type=="Gif") source = convertGif(ele);
2024-02-21 18:08:50 +08:00
else if(type=="DClock") source = convertDClock(ele);
else if(type=="AClock") source = convertAClock(ele);
else if(type=="Temp") source = EEnviron::genProg(ele, dstDir, srcPageDir);
else if(type=="Web") source = convertWeb(ele);
else if(type=="Timer") source = convertTimer(ele);
2024-08-07 18:18:37 +08:00
else if(type=="Timer2") {
source["_type"] = "Countdown";
source["time"] = ele["targetTime"];
source["isUp"] = ele["isUp"];
source["html"] = ele["html"];
source["backColor"] = ele["backColor"];
}
2024-02-21 18:08:50 +08:00
if(! source.empty()) {
if(source["timeSpan"].isNull()) source["timeSpan"] = ele["duration"];
source["entryEffect"] = ele["entryEffect"];
source["exitEffect"] = ele["exitEffect"];
source["entryEffectTimeSpan"] = ele["entryDur"];
source["exitEffectTimeSpan"] = ele["exitDur"];
2024-06-20 10:44:08 +08:00
if(ele["hasBlink"].toBool()) source["blink"] = ele["blink"];
2024-08-07 18:18:37 +08:00
else if(ele["hasBreathe"].toBool()) source["breathe"] = ele["blink"];
2024-02-21 18:08:50 +08:00
sources.append(source);
}
}
return sources;
2022-01-04 18:11:48 +08:00
}
2024-02-21 18:08:50 +08:00
JObj GenTmpThread::genText(const JValue &ele, JArray &sources) {
auto widget = ele["widget"];
if(widget.isNull()) widget = ele;
auto play = ele["play"];
QString playMode, direction;
int speed;
if(play.isNull()) {
playMode = ele["playMode"].toString();
direction = ele["direction"].toString();
speed = ele["speed"].toInt();
} else {
QString playModes[]{"Flip", "Scroll", "Static"};
playMode = playModes[play["style"].toInt()];
auto rolling = play["rolling"];
QString ds[]{"left", "top", "right", "bottom"};
direction = ds[rolling["rollingStyle"].toInt()];
speed = 1000/rolling["rollingSpeed"].toInt(33);
2023-04-18 14:14:46 +08:00
}
2024-02-21 18:08:50 +08:00
auto filenames = widget["files"];
2023-10-23 14:58:29 +08:00
auto filePrefix = srcPageDir+"/"+widget["idDir"].toString()+"/";
2024-02-21 18:08:50 +08:00
auto isScroll = playMode=="Scroll";
if(isScroll) {
JObj source;
source["_type"] = "MultiPng";
source["playMode"] = playMode;
JArray arrayPics;
for(auto &filename : filenames) {
auto file = filePrefix + filename.toString();
QFile qFile(file);
if(! qFile.exists()) continue;
auto id = Tools::fileMd5(file);
qFile.copy(dstDir+"/"+id);
JObj arrayPic;
arrayPic["id"] = id;
if(direction=="left") arrayPic["effect"] = "right to left";
else if(direction=="top") arrayPic["effect"] = "bottom to top";
else if(direction=="right") arrayPic["effect"] = "left to right";
else if(direction=="bottom") arrayPic["effect"] = "top to bottom";
arrayPic["scrollSpeed"] = speed;
arrayPic["effectSpeed"] = 1000 / speed;
2022-08-25 18:37:24 +08:00
arrayPic["picDuration"] = 0;
2024-02-21 18:08:50 +08:00
arrayPics.append(arrayPic);
}
source["arrayPics"] = arrayPics;
return source;
} else {
auto duration = ele["duration"].toInt();
for(auto &filename : filenames) {
auto file = filePrefix + filename.toString();
QFile qFile(file);
if(! qFile.exists()) continue;
auto id = Tools::fileMd5(file);
qFile.copy(dstDir+"/"+id);
JObj source;
source["_type"] = "Image";
source["id"] = id;
source["md5"] = id;
source["timeSpan"] = duration;
source["entryEffect"] = ele["entryEffect"];
source["exitEffect"] = ele["exitEffect"];
source["entryEffectTimeSpan"] = ele["entryDur"];
source["exitEffectTimeSpan"] = ele["exitDur"];
2024-08-07 18:18:37 +08:00
if(ele["hasBlink"].toBool()) source["blink"] = ele["blink"];
else if(ele["hasBreathe"].toBool()) source["breathe"] = ele["blink"];
2024-02-21 18:08:50 +08:00
sources.append(source);
2022-01-04 18:11:48 +08:00
}
2024-02-21 18:08:50 +08:00
return JObj();
2022-01-04 18:11:48 +08:00
}
}
2024-08-07 18:18:37 +08:00
2024-02-21 18:08:50 +08:00
JObj GenTmpThread::genImage(const JValue &ele) {
2022-08-25 18:37:24 +08:00
auto widget = ele["widget"];
2024-02-21 18:08:50 +08:00
auto name = widget.isNull() ? ele["name"].toString() : widget["file"].toString();
auto srcFile = (widget.isNull() ? ele["dir"] : widget["path"]).toString() + "/" + name;
2022-08-25 18:37:24 +08:00
QFileInfo srcInfo(srcFile);
2023-10-23 14:58:29 +08:00
JObj source;
2022-10-27 15:07:45 +08:00
if(! srcInfo.isFile()) return source;
2023-08-01 11:42:41 +08:00
QImage img(srcFile);
auto geometry = ele["geometry"];
auto width = geometry["w"].toInt();
auto height = geometry["h"].toInt();
2024-02-21 18:08:50 +08:00
/*if(mProgItem->maxLen) {
2023-08-01 11:42:41 +08:00
auto scaled = img.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
2024-02-21 18:08:50 +08:00
QImage square(gProgItem->isVer ? gProgItem->mWidth*gProgItem->partLens.size() : gProgItem->maxLen, gProgItem->isVer ? gProgItem->maxLen : gProgItem->mHeight*gProgItem->partLens.size(), QImage::Format_ARGB32);
2023-08-01 11:42:41 +08:00
square.fill(0);
QPainter painter(&square);
QPointF pos(x, y);
2024-02-21 18:08:50 +08:00
auto end = (int)gProgItem->partLens.size();
if(gProgItem->isVer) {
painter.drawImage(pos, scaled, QRectF(0, 0, width, gProgItem->partLens[0]-pos.y()));
for(int i=1; i<end; i++) {
pos.ry() -= gProgItem->partLens[i-1];
pos.rx() += gProgItem->mWidth;
painter.drawImage(pos, scaled, QRectF(0, 0, width, gProgItem->partLens[i]-pos.y()));
}
} else {
painter.drawImage(pos, scaled, QRectF(0, 0, gProgItem->partLens[0]-pos.x(), height));
for(int i=1; i<end; i++) {
pos.rx() -= gProgItem->partLens[i-1];
pos.ry() += gProgItem->mHeight;
painter.drawImage(pos, scaled, QRectF(0, 0, gProgItem->partLens[i]-pos.x(), height));
}
2023-08-01 11:42:41 +08:00
}
QBuffer buf;
square.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;
2024-02-21 18:08:50 +08:00
} else */if(img.width() > width*2 && img.height() > height*2) {
2023-08-01 11:42:41 +08:00
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;
} else {
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["_type"] = "Image";
auto play = ele["play"];
2024-02-21 18:08:50 +08:00
source["timeSpan"] = play.isNull() ? ele["duration"] : play["playDuration"];
2022-08-25 18:37:24 +08:00
return source;
2022-01-04 18:11:48 +08:00
}
//转换图片
2024-02-21 18:08:50 +08:00
JObj GenTmpThread::convertGif(const JValue &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);
2023-10-23 14:58:29 +08:00
if(! srcInfo.isFile()) return JObj();
2022-08-25 18:37:24 +08:00
QString id = Tools::fileMd5(srcFile);
2023-10-23 14:58:29 +08:00
if(id.isEmpty()) return JObj();
2022-08-25 18:37:24 +08:00
QFile::copy(srcFile, dstDir+"/"+id);
2023-10-23 14:58:29 +08:00
JObj oRes;
2022-08-25 18:37:24 +08:00
oRes["_type"] = "Image";
oRes["id"] = id;
oRes["md5"] = id;
2024-02-21 18:08:50 +08:00
oRes["fileExt"] = srcInfo.suffix().toLower();
2022-08-25 18:37:24 +08:00
auto play = json["play"];
2024-02-21 18:08:50 +08:00
oRes["timeSpan"] = (play.isNull() ? json["duration"] : play["playDuration"]).toInt() * play["playTimes"].toInt(1);
2022-01-04 18:11:48 +08:00
return oRes;
}
2024-02-21 18:08:50 +08:00
JObj GenTmpThread::convertDClock(const JValue &json){
2023-10-23 14:58:29 +08:00
JObj oRes;
2022-08-25 18:37:24 +08:00
oRes["_type"] = "DigitalClockNew";
oRes["name"] = "DigitalClockNew";
auto widget = json["widget"];
2023-10-23 14:58:29 +08:00
oRes["timeZone"] = widget["timeZone"];
oRes["timezone"] = 8;//兼容旧播放器
2022-08-25 18:37:24 +08:00
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"];
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);
QFontMetrics metric(font);
2024-02-21 18:08:50 +08:00
oRes["spaceWidth"] = metric.horizontalAdvance(" ");
2022-08-25 18:37:24 +08:00
QColor color(textColor);
2023-10-23 14:58:29 +08:00
JArray imgs;
2024-08-07 18:18:37 +08:00
for(int i=0; i<=9; i++) Tools::saveImg2(dstDir, metric, font, color, imgs, QString::number(i), QString::number(i));
2022-08-25 18:37:24 +08:00
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["arrayPics"] = imgs;
2022-01-04 18:11:48 +08:00
return oRes;
}
2024-02-21 18:08:50 +08:00
JObj GenTmpThread::convertAClock(const JValue &json) {
2022-08-25 18:37:24 +08:00
auto widget = json["widget"];
2024-02-21 18:08:50 +08:00
if(widget.isNull()) widget = json;
2024-02-28 11:34:33 +08:00
auto srcFile = srcPageDir + "/" + widget["selfCreateDialName"].toString();
if(! QFileInfo::exists(srcFile)) {
srcFile = srcPageDir + "/" + widget["name"].toString();
if(! QFileInfo::exists(srcFile)) return JObj();
}
2022-08-25 18:37:24 +08:00
QString id = Tools::fileMd5(srcFile);
2024-02-28 11:34:33 +08:00
QFile(srcFile).copy(dstDir+"/"+id);
2023-10-23 14:58:29 +08:00
JObj oRes;
2022-08-25 18:37:24 +08:00
oRes["_type"] = "AnalogClock";
oRes["id"] = id;
oRes["md5"] = id;
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"];
2023-10-23 14:58:29 +08:00
oRes["scaleHourColor"] = color.isStr() ? color : Tools::int2Color(color.toInt()).name();
2022-08-25 18:37:24 +08:00
color = widget["minMarkColor"];
2023-10-23 14:58:29 +08:00
oRes["scaleMinColor"] = color.isStr() ? color : Tools::int2Color(color.toInt()).name();
2022-08-25 18:37:24 +08:00
color = widget["hourHandColor"];
2023-10-23 14:58:29 +08:00
oRes["pinHourColor"] = color.isStr() ? color : Tools::int2Color(color.toInt()).name();
2022-08-25 18:37:24 +08:00
color = widget["minHandColor"];
2023-10-23 14:58:29 +08:00
oRes["pinMinColor"] = color.isStr() ? color : Tools::int2Color(color.toInt()).name();
2022-08-25 18:37:24 +08:00
color = widget["secHandColor"];
2023-10-23 14:58:29 +08:00
oRes["pinSecColor"] = color.isStr() ? 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"];
2023-10-23 14:58:29 +08:00
oRes["timeZone"] = widget["timeZone"];
2022-01-04 18:11:48 +08:00
return oRes;
}
2024-02-21 18:08:50 +08:00
JObj GenTmpThread::convertWeb(const JValue &res) {
2023-10-23 14:58:29 +08:00
JObj dst;
2022-08-25 18:37:24 +08:00
dst["_type"] = "WebURL";
dst["name"] = "WebURL";
dst["url"] = res["url"];
2024-05-23 17:13:51 +08:00
dst["zoom"] = res["zoom"];
2024-08-07 18:18:37 +08:00
dst["refreshSec"] = res["refreshSec"];
2024-05-23 17:13:51 +08:00
dst["offX"] = res["offX"];
dst["offY"] = res["offY"];
2024-06-19 18:54:32 +08:00
dst["scaleX"] = res["scaleX"].toDouble(100)/100;
dst["scaleY"] = res["scaleY"].toDouble(100)/100;
2022-08-25 18:37:24 +08:00
return dst;
2022-01-04 18:11:48 +08:00
}
2024-02-21 18:08:50 +08:00
JObj GenTmpThread::convertTimer(const JValue &json) {
2024-08-07 18:18:37 +08:00
JObj src;
src["_type"] = "Timer";
src["name"] = "Timer";
src["targetTime"] = json["targetTime"];
src["isDown"] = json["isDown"];
src["hasDay"] = json["hasDay"];
src["hasHour"] = json["hasHour"];
src["hasMin"] = json["hasMin"];
src["hasSec"] = json["hasSec"];
2024-02-21 18:08:50 +08:00
auto isMultiline = json["isMultiline"].toBool();
2024-08-07 18:18:37 +08:00
src["isMultiline"] = isMultiline;
2022-08-25 18:37:24 +08:00
auto text = json["text"].toString();
2024-08-07 18:18:37 +08:00
src["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());
2024-08-07 18:18:37 +08:00
src["font"] = font.family();
src["fontSize"] = font.pixelSize();
src["fontBold"] = font.bold();
src["fontItalic"] = font.italic();
src["fontUnderline"] = font.underline();
2022-08-25 18:37:24 +08:00
auto textColor = json["textColor"].toString();
2024-08-07 18:18:37 +08:00
src["textColor"] = textColor;
src["backColor"] = json["backColor"];
2022-08-25 18:37:24 +08:00
font.setStyleStrategy(gTextAntialiasing ? QFont::PreferAntialias : QFont::NoAntialias);
QFontMetrics metric(font);
2024-08-07 18:18:37 +08:00
src["spaceWidth"] = metric.horizontalAdvance(" ");
2022-08-25 18:37:24 +08:00
QColor color(textColor);
2023-10-23 14:58:29 +08:00
JObj imgs;
2024-08-07 18:18:37 +08:00
for(int i=0; i<=9; i++) Tools::saveImg(dstDir, metric, font, color, imgs, QString::number(i), QString::number(i));
2022-08-25 18:37:24 +08:00
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()) {
2024-02-21 18:08:50 +08:00
QSize size;
if(isMultiline) {
auto innerW = json["innerW"].toInt();
auto innerH = json["innerH"].toInt();
auto rect = metric.boundingRect(0, 0, innerW, innerH, Qt::AlignCenter | Qt::TextWordWrap, text);
size = {qMin(rect.width(), innerW), qMin(rect.height(), innerH)};
} else size = {metric.horizontalAdvance(text), metric.lineSpacing()};
QImage img(size, QImage::Format_ARGB32);
2023-04-18 14:14:46 +08:00
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
}
2024-08-07 18:18:37 +08:00
src["imgs"] = imgs;
return src;
2022-01-04 18:11:48 +08:00
}