qt/LedOK/player/playwin.cpp

141 lines
6.0 KiB
C++
Raw Normal View History

2022-01-20 10:08:17 +08:00
#include "playwin.h"
#include "eleanaclock.h"
#include "digiclock.h"
#include "eleimg.h"
#include "elegif.h"
#include "elescroll.h"
#include "elemultipng.h"
#include "elevideo.h"
#include "gqt.h"
#include <QJsonObject>
#include <QJsonArray>
#include <QMovie>
#include <QLabel>
#include <QThread>
Page::Page(QWidget *parent) : QWidget{parent} {
}
PlayWin::PlayWin(QString dir, int x, int y, int width, int height, const QJsonObject &aprog, QWidget *parent) : QWidget(parent, Qt::Window|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint) {
setAttribute(Qt::WA_DeleteOnClose);
setGeometry(x, y, width, height);
QPalette plt = palette();
plt.setColor(QPalette::Window, QColor(0,0,0));
setPalette(plt);
connect(this, &PlayWin::sigSetVisible, this, &PlayWin::sltSetVisible);
QJsonObject prog = aprog["task"].toObject();
QJsonArray pageMaps = prog["items"].toArray();
int pageCnt = pageMaps.size();
EleBase ele;
for(int p=0; p<pageCnt; p++) {
QJsonObject pageMap = pageMaps[p].toObject()["_program"].toObject();
QJsonArray layerMaps = pageMap["layers"].toArray();
Page *page = new Page(this);
page->setGeometry(0, 0, width, height);
page->setVisible(false);
page->name = pageMap["name"].toString();
page->dirPre = dir+"/"+page->name+"/";
for(int ll=0; ll<layerMaps.size(); ll++) {
QJsonObject layerMap = layerMaps[ll].toObject();
QJsonArray srcMaps = layerMap["sources"].toArray();
for(int ss=0; ss<srcMaps.size(); ss++) {
QJsonObject srcMap = srcMaps[ss].toObject();
ele.type = srcMap["_type"].toString();
if(ele.type.isEmpty()) continue;
ele.x = srcMap["left"].toInt();
ele.y = srcMap["top"].toInt();
ele.w = srcMap["width"].toInt();
ele.h = srcMap["height"].toInt();
if(ele.w==0 || ele.h==0) return;
ele.timeSpan = srcMap["timeSpan"].toInt()*1000;
if(ele.timeSpan==0) {
qDebug()<<page->name<<"-"<<ele.type<<"timeSpan==0";
continue;
}
ele.startTime = srcMap["playTime"].toInt()*1000;
ele.endTime = ele.startTime + ele.timeSpan;
if(ele.endTime > page->timeSpan) page->timeSpan = ele.endTime;
if(ele.startTime>0) ele.wgt->setVisible(false);
ele.id = srcMap["id"].toString();
ele.wgt = nullptr;
if(ele.type=="Image") {
if(srcMap["mime"].toString().endsWith("gif")) ele.wgt = new EleGif(page->dirPre+ele.id, page);
else ele.wgt = new EleImg(page->dirPre+ele.id, page);
} else if(ele.type.startsWith("Environ")) {
QJsonArray arrayPics = srcMap["arrayPics"].toArray();
for(int i=arrayPics.size()-1; i>=0; i--) if(arrayPics[i].toObject()["name"].toString() == "previewTmp") {
ele.wgt = new EleImg(page->dirPre + arrayPics[i].toObject()["id"].toString(), page);
break;
}
} else if(ele.type=="MultiPng") {
QJsonArray imgs = srcMap["arrayPics"].toArray();
if(imgs.isEmpty()) continue;
if(imgs.size()==1 && imgs[0].toObject()["picDuration"].toInt()==0) ele.wgt = new EleScroll(page->dirPre, imgs[0].toObject(), page);
else ele.wgt = new EleMultiPng(page->dirPre, imgs, page);
} else if(ele.type=="DigitalClockNew") ele.wgt = new DigiClock(page->dirPre, srcMap, page);
else if(ele.type=="AnalogClock") ele.wgt = new EleAnaClock(page->dirPre+ele.id, srcMap, page);
else if(ele.type=="Video") ele.wgt = new EleVideo(page->dirPre+ele.id, page);
else continue;
if(ele.wgt==nullptr) continue;
ele.wgt->setGeometry(ele.x, ele.y, ele.w, ele.h);
page->eles.append(ele);
}
}
}
setVisible(true);
Page* page0 = (Page*)children()[0];
EleBase* eleptr;
for(int ee=0; ee<page0->eles.size(); ee++) if((eleptr = &page0->eles[ee])->startTime > 0 || eleptr->endTime < page0->timeSpan) {
if(eleptr->startTime > 0) timerMap.insert(startTimer(eleptr->startTime), TimerValue(eleptr->wgt, true));
timerMap.insert(startTimer(eleptr->endTime), TimerValue(eleptr->wgt, false));
}
}
void PlayWin::sltNext() {
if(isVisible()) {
Page* page = (Page*)children()[cur];
page->setVisible(false);
if(cur+2 > children().size()) cur = 0;
else cur++;
page = (Page*)children()[cur];
if(timer!=nullptr) timer->inter = page->timeSpan;
EleBase* ele;
for(int ee=0; ee<page->eles.size(); ee++) if((ele = &page->eles[ee])->startTime > 0 || ele->endTime < page->timeSpan) {
if(ele->startTime > 0) timerMap.insert(startTimer(ele->startTime), TimerValue(ele->wgt, true));
else ele->wgt->setVisible(true);
timerMap.insert(startTimer(ele->endTime), TimerValue(ele->wgt, false));
}
page->setVisible(true);
} else if(timer!=nullptr) {
timer->stop();
timer = nullptr;
}
}
void PlayWin::timerEvent(QTimerEvent *e){
int id = e->timerId();
killTimer(id);
TimerValue value = timerMap[id];
if(value.ele!=nullptr){
qDebug()<<value.ele;
timerMap.remove(id);
value.ele->setVisible(value.visible);
}
}
void PlayWin::paintEvent(QPaintEvent *e){
if(timer==nullptr && isVisible()) {
if(cur!=0) {
((Page*)children()[cur])->setVisible(false);
cur = 0;
}
Page *page = (Page*)children()[cur];
page->setVisible(true);
timer = new SyncTimer(page->timeSpan);
connect(timer, &SyncTimer::timeout, this, &PlayWin::sltNext, Qt::BlockingQueuedConnection);
timer->start();
}
QWidget::paintEvent(e);
}