qt/LedOK/wProgramManager/wEditProgram/wElement/ephoto.cpp

349 lines
14 KiB
C++
Raw Normal View History

2022-01-04 18:11:48 +08:00
#include "ephoto.h"
#include "ephotoattr.h"
#include "QThread"
2022-01-20 10:08:17 +08:00
ePhoto::ePhoto(QRectF rectInit,QSizeF DrawBoardSize,const QString &file, InteractiveType type, QGraphicsItem *parent) : eObject(rectInit,type, parent){
2022-01-04 18:11:48 +08:00
m_rectInit=rectInit;
m_DrawBoardSize=DrawBoardSize;
m_iType=eObject::Photo;
int iResult=onResChanged(file);
2022-01-20 10:08:17 +08:00
if(iResult!=2) {
2022-01-04 18:11:48 +08:00
setPlayArg(10, 1, 0, 0);
int x=rectInit.left();
int y=rectInit.top();
qreal ihbiw=static_cast<qreal>(m_photo.height())/ static_cast<qreal>(m_photo.width());
qreal iwbih=static_cast<qreal>(m_photo.width()) / static_cast<qreal>(m_photo.height());
qreal w = rectInit.width();
qreal h = floor(w*ihbiw );
2022-01-20 10:08:17 +08:00
if(x+w>DrawBoardSize.width()) {
2022-01-04 18:11:48 +08:00
x=0;
2022-01-20 10:08:17 +08:00
if(w>DrawBoardSize.width()) w=DrawBoardSize.width();
2022-01-04 18:11:48 +08:00
h=w*ihbiw;
}
2022-01-20 10:08:17 +08:00
if(y+h>DrawBoardSize.height()) {
2022-01-04 18:11:48 +08:00
y=0;
2022-01-20 10:08:17 +08:00
if(h>DrawBoardSize.height()) h=DrawBoardSize.height();
2022-01-04 18:11:48 +08:00
w=h*iwbih;
}
setGeometry(QRectF(x, y, w, h));
}
}
2022-01-20 10:08:17 +08:00
ePhoto::ePhoto(QRectF rectInit,const QString &file, InteractiveType type, QGraphicsItem *parent) : eObject(rectInit,type, parent) {
2022-01-04 18:11:48 +08:00
m_iType=eObject::Photo;
onResChanged(file);
setPlayArg(10, 1, 0, 0);
qreal x=rectInit.left();
qreal y=rectInit.top();
qreal w=rectInit.width();
qreal h=rectInit.height();
setGeometry(QRectF(x, y, w, h));
}
2022-01-20 10:08:17 +08:00
ePhoto::ePhoto(const QJsonObject &json, const QString &pRoot, InteractiveType type, QGraphicsItem *parent) : eObject(json["geometry"].toObject(), type, parent) {
m_iType = eObject::Photo;
2022-01-04 18:11:48 +08:00
QJsonDocument jRoot(json);
QString file0 = pRoot + MACRO_FENGEFU + jRoot["widget"]["file"].toString();
QString file1 = jRoot["widget"]["path"].toString() + MACRO_FENGEFU + jRoot["widget"]["file"].toString();
QString file2 = jRoot["widget"]["path"].toString() + MACRO_FENGEFU + jRoot["widget"]["yuanshi_file"].toString();
QString file3 = jRoot["widget"]["computer_pic_file"].toString() ;
QString file4 = jRoot["widget"]["path"].toString() + MACRO_FENGEFU +"card_"+ jRoot["widget"]["file"].toString();
QFile f0(file0);
QFile f1(file1);
QFile f2(file2);
QFile f3(file3);
QFile f4(file4);
QString file;
2022-01-20 10:08:17 +08:00
if(f0.exists()) file = file0;
else if(f1.exists()) file = file1;
else if(f2.exists()) file = file2;
else if(f3.exists()) file = file3;
else if(f4.exists()) file = file4;
2022-01-04 18:11:48 +08:00
else {
setGeometry(RECTF_INVALID);
return;
}
onResChanged(file);
setPlayArg(jRoot["play"]["playDuration"].toInt(),
jRoot["play"]["playTimes"].toInt(),
jRoot["play"]["enterStyle"].toInt(),
jRoot["play"]["enterDuration"].toInt());
m_attr.name = json["widget"]["file"].toString();
m_attr.yuanshi_name = json["widget"]["yuanshi_file"].toString();
m_attr.computer_pic_file = json["widget"]["computer_pic_file"].toString();
m_attr.path = json["widget"]["path"].toString();
m_attr.playDuration = json["play"]["playDuration"].toInt();
m_attr.playTimes = json["play"]["playTimes"].toInt();
m_attr.enterStyle = json["play"]["enterStyle"].toInt();
m_attr.enterDuration = json["play"]["enterDuration"].toInt();
}
2022-01-20 10:08:17 +08:00
void ePhoto::setElement(const QJsonObject &json, Data &attr){
2022-01-04 18:11:48 +08:00
attr.name = json["widget"]["file"].toString();
attr.yuanshi_name = json["widget"]["yuanshi_file"].toString();
attr.computer_pic_file = json["widget"]["computer_pic_file"].toString();
attr.path = json["widget"]["path"].toString();
attr.playDuration = json["play"]["playDuration"].toInt();
attr.playTimes = json["play"]["playTimes"].toInt();
attr.enterStyle = json["play"]["enterStyle"].toInt();
attr.enterDuration = json["play"]["enterDuration"].toInt();
}
2022-01-20 10:08:17 +08:00
void ePhoto::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
2022-01-04 18:11:48 +08:00
painter->save();
painter->drawImage(rect(), m_photo);
painter->restore();
2022-01-07 18:22:58 +08:00
eObject::paint(painter, option, widget);
2022-01-04 18:11:48 +08:00
}
2022-01-20 10:08:17 +08:00
void ePhoto:: deleteContent(){
2022-01-04 18:11:48 +08:00
QFile file(m_attr.path+"/card_"+m_attr.name);
if(file.exists())
file.remove();
QFile file2(m_attr.path+"/"+m_attr.name);
if(file2.exists())
file2.remove();
QFile file1(m_attr.path+"/"+m_attr.yuanshi_name);
if(file1.exists())
file1.remove();
}
2022-01-20 10:08:17 +08:00
QWidget* ePhoto::wAttr(){
2022-01-04 18:11:48 +08:00
QWidget *wObj = eObject::wAttr();
QWidget *w = wAttrElement();
static_cast<QBoxLayout*>(w->layout())->insertWidget(0, wObj);
return w;
}
2022-01-20 10:08:17 +08:00
QWidget* ePhoto::wAttrElement(){
2022-01-04 18:11:48 +08:00
ePhotoAttr *w = new ePhotoAttr(m_attr);
connect(w, SIGNAL(sResChanged(const QString &)), this, SLOT(onResChanged(const QString &)));
connect(w, SIGNAL(sAttrChanged(const ePhoto::Data &)), this, SLOT(onAttrChanged(const ePhoto::Data &)));
return w;
}
2022-01-20 10:08:17 +08:00
QStringList ePhoto::filesList() const{
QStringList list;
list.append(m_attr.path + MACRO_FENGEFU + m_attr.name);
return list;
}
bool ePhoto::save(const QString &pRoot){
2022-01-04 18:11:48 +08:00
QString fileOld = m_attr.computer_pic_file;
QString file0 = m_attr.path + PAGEDEL_SUFFIX + MACRO_FENGEFU + m_attr.name;
QString file1 = m_attr.path + MACRO_FENGEFU + m_attr.name;
QString file2 = m_attr.path + MACRO_FENGEFU + m_attr.yuanshi_name;
QString file3 = m_attr.path + PAGEDEL_SUFFIX + MACRO_FENGEFU + m_attr.yuanshi_name;
QFile fOld(fileOld);
QFile f0(file0);
QFile f1(file1);
QFile f2(file2);
QFile f3(file3);
QString old_file;
QString file_full=old_file.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
// QString strFileMD5=getFileMd5(file_full);
// m_attr.name=strFileMD5;
// QString new_file = pRoot + MACRO_FENGEFU + strFileMD5;
QString new_file = pRoot + MACRO_FENGEFU + m_attr.yuanshi_name;
2022-01-20 10:08:17 +08:00
if(fOld.exists()) old_file = fileOld;
else if(f0.exists()) old_file = file0;
else if(f3.exists()) old_file = file3;
else if(f1.exists()) old_file = file1;
else if(f2.exists()) old_file = file2;
else return false;
2022-01-04 18:11:48 +08:00
QFile old_f(old_file);
QFile new_f(new_file);
2022-01-20 10:08:17 +08:00
if(!new_f.exists()) {
2022-01-04 18:11:48 +08:00
old_f.copy(new_file);
QImage imageOlde(old_file);
2022-01-20 10:08:17 +08:00
qreal bili = width/(height==0 ? 1 : height);
if(imageOlde.height()!=0){
2022-01-04 18:11:48 +08:00
qreal ImageBili=imageOlde.width()/imageOlde.height();
//QMessageBox::warning(nullptr, "Tip", tr("2"));
2022-01-20 10:08:17 +08:00
if(ImageBili>=bili && ImageBili!=0) {
2022-01-04 18:11:48 +08:00
QImage fitimgpic=imageOlde.scaled(rect().width(),rect().width()/ImageBili,Qt::KeepAspectRatioByExpanding);
QString strControllerPicPathName=pRoot+MACRO_FENGEDANFU+"card_"+m_attr.name;
fitimgpic.save(strControllerPicPathName);
//QMessageBox::warning(nullptr, "Tip", tr("3"));
2022-01-20 10:08:17 +08:00
} else {
QImage fitimgpic= imageOlde.scaled(rect().height()*ImageBili,rect().height(),Qt::KeepAspectRatioByExpanding);
QString strControllerPicPathName=pRoot+MACRO_FENGEDANFU+"card_"+m_attr.name;
if(fitimgpic.save(strControllerPicPathName)==false){
2022-01-04 18:11:48 +08:00
// QMessageBox::warning(nullptr, "Tip", strControllerPicPathName+"\r\n"+old_file+"\r\n"+new_file);
2022-01-20 10:08:17 +08:00
old_f.copy(strControllerPicPathName);
}
2022-01-04 18:11:48 +08:00
}
}
2022-01-20 10:08:17 +08:00
} else {
2022-01-04 18:11:48 +08:00
QString strControllerPicPathName=pRoot+MACRO_FENGEDANFU+"card_"+m_attr.name;
QFile new_card_pic(strControllerPicPathName);
2022-01-20 10:08:17 +08:00
if(!new_card_pic.exists()) {
2022-01-04 18:11:48 +08:00
QImage imageOlde(old_file);
2022-01-20 10:08:17 +08:00
qreal bili = width/(height==0 ? 1 : height);
if(imageOlde.height()!=0){
2022-01-04 18:11:48 +08:00
qreal ImageBili=imageOlde.width()/imageOlde.height();
2022-01-20 10:08:17 +08:00
if(ImageBili>=bili && ImageBili!=0){
2022-01-04 18:11:48 +08:00
QImage fitimgpic=imageOlde.scaled(rect().width(),rect().width()/ImageBili,Qt::KeepAspectRatioByExpanding);
fitimgpic.save(strControllerPicPathName);
//QMessageBox::warning(nullptr, "Tip", tr("3"));
2022-01-20 10:08:17 +08:00
} else {
2022-01-04 18:11:48 +08:00
QImage fitimgpic= imageOlde.scaled(rect().height()*ImageBili,rect().height(),Qt::KeepAspectRatioByExpanding);
if(fitimgpic.save(strControllerPicPathName)==false)
{
// QMessageBox::warning(nullptr, "Tip", strControllerPicPathName+"\r\n"+old_file+"\r\n"+new_file);
old_f.copy(strControllerPicPathName);
}
}
}
}
}
m_attr.path = pRoot;
2022-01-20 10:08:17 +08:00
if(m_CropFlag && !m_photo.isNull()) { //如果图 剪切过
2022-01-04 18:11:48 +08:00
QString strNewPicPathName=m_attr.path+MACRO_FENGEDANFU+m_attr.yuanshi_name;
QString strControllerPicPathName=pRoot+MACRO_FENGEDANFU+"card_"+m_attr.name;
m_photo.save(strNewPicPathName);
m_photo.save(strControllerPicPathName);
}
2022-01-20 10:08:17 +08:00
return true;
2022-01-04 18:11:48 +08:00
}
QJsonObject ePhoto::elementJson() const
{
QJsonObject oRoot;
QJsonObject oWidget;
QJsonObject oPlay;
// Interior
oRoot["elementType"] = "Photo";
oRoot["elementTypeId"] = type();
oRoot["geometry"] = eObject::elementJson();
// Widget
oWidget["file"] = m_attr.name;
oWidget["computer_pic_file"] = m_attr.computer_pic_file;
oWidget["yuanshi_file"] = m_attr.yuanshi_name;
//oWidget["path"] = m_attr.path;
QString strtempPath= m_attr.path;
strtempPath.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
oWidget["path"] = strtempPath;
oRoot["widget"] = oWidget;
// Play
oPlay["playDuration"] = m_attr.playDuration;
oPlay["playTimes"] = m_attr.playTimes;
oPlay["enterStyle"] = m_attr.enterStyle;
oPlay["enterDuration"] = m_attr.enterDuration;
oRoot["play"] = oPlay;
return oRoot;
}
2022-01-20 10:08:17 +08:00
int ePhoto::onResChanged(const QString &filepath){
m_attr.computer_pic_file = filepath;
QFileInfo fileinfo(filepath);
m_attr.name = fileinfo.fileName();
m_attr.path = fileinfo.absolutePath();
m_attr.yuanshi_name = m_attr.name.contains("yuanshi_") ? m_attr.name : "yuanshi_"+m_attr.name;
if(fileinfo.exists()) {
m_photo = QImage(filepath);
if(m_photo.isNull()){
2022-01-04 18:11:48 +08:00
QString strAppPath=QApplication::applicationDirPath();
strAppPath.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
process = new QProcess(nullptr);
process->setWorkingDirectory(strAppPath);
2022-01-20 10:08:17 +08:00
2022-01-04 18:11:48 +08:00
QStringList args;
2022-01-20 10:08:17 +08:00
args<<"-i"<<filepath;
2022-01-04 18:11:48 +08:00
args<<"-vf"<<"scale=1920:-1";
QString strProgramItemPath = LoAppConfig::getInstance()->NProgramsLocation();
//原始节目保存的路径
2022-01-20 10:08:17 +08:00
m_strNewFileName=strProgramItemPath+"/"+fileinfo.fileName();
2022-01-04 18:11:48 +08:00
m_strNewFileName.replace(MACRO_DANYINFANXIEGAN, MACRO_DANYINXIEGAN);
args<<m_strNewFileName;
// QString strCmd="ffmpeg -i \""+file+" -vf “scale=1920:-1” output.jpg";
process->start("ffmpeg",args);
QFileInfo ff(m_strNewFileName);
int iReTryCount=0;
2022-01-20 10:08:17 +08:00
while(!ff.exists()) {
2022-01-04 18:11:48 +08:00
QThread::sleep(1);
iReTryCount++;
2022-01-20 10:08:17 +08:00
if(iReTryCount>10) break;
2022-01-04 18:11:48 +08:00
}
m_attr.computer_pic_file=m_strNewFileName;
QFileInfo fiNew(m_strNewFileName);
m_attr.name = fiNew.fileName();
2022-01-20 10:08:17 +08:00
if(!fiNew.fileName().contains("yuanshi_")) m_attr.yuanshi_name ="yuanshi_"+fiNew.fileName();
else m_attr.yuanshi_name =fiNew.fileName();
2022-01-04 18:11:48 +08:00
m_attr.path = fiNew.absolutePath();
m_photo = QImage(m_strNewFileName);
setPlayArg(10, 1, 0, 0);
int x=m_rectInit.left();
int y=m_rectInit.top();
qreal ihbiw=static_cast<qreal>(m_photo.height())/ static_cast<qreal>(m_photo.width());
qreal iwbih=static_cast<qreal>(m_photo.width()) / static_cast<qreal>(m_photo.height());
qreal w = m_rectInit.width();
qreal h = floor(w*ihbiw );
2022-01-20 10:08:17 +08:00
if(x+w>m_DrawBoardSize.width()){
2022-01-04 18:11:48 +08:00
x=0;
2022-01-20 10:08:17 +08:00
if(w>m_DrawBoardSize.width()) w=m_DrawBoardSize.width();
2022-01-04 18:11:48 +08:00
h=w*ihbiw;
}
2022-01-20 10:08:17 +08:00
if(y+h>m_DrawBoardSize.height()) {
2022-01-04 18:11:48 +08:00
y=0;
2022-01-20 10:08:17 +08:00
if(h>m_DrawBoardSize.height()) h=m_DrawBoardSize.height();
2022-01-04 18:11:48 +08:00
w=h*iwbih;
}
setGeometry(QRectF(x, y, w, h));
return 2;
}
2022-01-20 10:08:17 +08:00
} else {
QString strFileNew=fileinfo.path()+"/"+m_attr.yuanshi_name;
2022-01-04 18:11:48 +08:00
m_photo = QImage(strFileNew);
}
2022-01-20 10:08:17 +08:00
return 0;
2022-01-04 18:11:48 +08:00
}
2022-01-20 10:08:17 +08:00
void ePhoto::ConvertImage(int i,QProcess::ExitStatus){
2022-01-04 18:11:48 +08:00
Q_UNUSED(i);
delete process;
m_attr.computer_pic_file=m_strNewFileName;
QFileInfo fiNew(m_strNewFileName);
m_attr.name = fiNew.fileName();
2022-01-20 10:08:17 +08:00
if(!fiNew.fileName().contains("yuanshi_")) m_attr.yuanshi_name ="yuanshi_"+fiNew.fileName();
else m_attr.yuanshi_name =fiNew.fileName();
2022-01-04 18:11:48 +08:00
m_attr.path = fiNew.absolutePath();
m_photo = QImage(m_strNewFileName);
setPlayArg(10, 1, 0, 0);
int x=m_rectInit.left();
int y=m_rectInit.top();
qreal ihbiw=static_cast<qreal>(m_photo.height())/ static_cast<qreal>(m_photo.width());
qreal iwbih=static_cast<qreal>(m_photo.width()) / static_cast<qreal>(m_photo.height());
qreal w = m_rectInit.width();
qreal h = floor(w*ihbiw );
2022-01-20 10:08:17 +08:00
if(x+w>m_DrawBoardSize.width()){
2022-01-04 18:11:48 +08:00
x=0;
2022-01-20 10:08:17 +08:00
if(w>m_DrawBoardSize.width()) w=m_DrawBoardSize.width();
2022-01-04 18:11:48 +08:00
h=w*ihbiw;
}
2022-01-20 10:08:17 +08:00
if(y+h>m_DrawBoardSize.height()){
2022-01-04 18:11:48 +08:00
y=0;
2022-01-20 10:08:17 +08:00
if(h>m_DrawBoardSize.height()) h=m_DrawBoardSize.height();
2022-01-04 18:11:48 +08:00
w=h*iwbih;
}
setGeometry(QRectF(x, y, w, h));
}
void ePhoto::onAttrChanged(const ePhoto::Data &data)
{
setPlayArg(data.playDuration, data.playTimes, data.enterStyle, data.enterDuration);
}
void ePhoto::setPlayArg(int pd, int pt, int es, int ed)
{
m_attr.playDuration = pd;
m_attr.playTimes = pt;
m_attr.enterStyle = es;
m_attr.enterDuration = ed;
}
void ePhoto::CropCurImage(QRect rt)
{
stopElectment();
m_photo=m_photo.copy(rt);
m_CropFlag=true;
}