#include "playwin.h" #include "digiclock.h" #include "eleanaclock.h" #include "eleborder.h" #include "elegif.h" #include "eleimg.h" #include "elemultipng.h" #include "elescroll.h" #include "eletimer.h" #include "elevideo.h" #include "gqt.h" #include "posdlg.h" #include #include #include #include #include #include #include #include PlayWin* PlayWin::self = nullptr; 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) { setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_QuitOnClose, false); setWindowFlag(Qt::FramelessWindowHint); setWindowFlag(Qt::WindowStaysOnTopHint); 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; Page *page; for(int p=0; psetGeometry(0, 0, width, height); page->setVisible(false); for(int ll=layers.size()-1; ll>=0; ll--) { QJsonObject layer = layers[ll].toObject(); QJsonArray srcMaps = layer["sources"].toArray(); QJsonValue border = layer["border"]; EleBorder *bdEle = nullptr; int bdWidth = 0, startTime = 0xffff, endTime = 0; if(! border.isNull()) { bdEle = new EleBorder(dir+"/"+border["img"].toString(), border["eff"].toString(), border["speed"].toInt(), page); bdWidth = bdEle->img.height(); } foreach(auto srcMap, srcMaps) { ele.type = srcMap["_type"].toString(); if(ele.type.isEmpty()) continue; ele.timeSpan = srcMap["timeSpan"].toInt()*1000; if(ele.timeSpan==0) continue; ele.x = srcMap["left"].toInt()+bdWidth; ele.y = srcMap["top"].toInt()+bdWidth; ele.w = srcMap["width"].toInt()-bdWidth-bdWidth; ele.h = srcMap["height"].toInt()-bdWidth-bdWidth; bool notAudio = ele.type!="Audio"; if((ele.w<=0 || ele.h<=0) && notAudio) continue; ele.startTime = srcMap["playTime"].toInt()*1000; if(startTime > ele.startTime) startTime = ele.startTime; ele.endTime = ele.startTime + ele.timeSpan; if(endTime < ele.endTime) endTime = ele.endTime; if(ele.endTime > page->timeSpan && notAudio) page->timeSpan = ele.endTime; ele.id = srcMap["id"].toString(); ele.wgt = nullptr; if(ele.type=="Image") { if(srcMap["mime"].toString().endsWith("gif")) ele.wgt = new EleGif(dir+"/"+ele.id, page); else ele.wgt = new EleImg(dir+"/"+ele.id, page); } else if(ele.type.startsWith("Environ")) { QJsonValue arrayPics = srcMap["arrayPics"]; for(int i=arrayPics.toArray().size()-1; i>=0; i--) if(arrayPics[i]["name"].toString() == "previewTmp") { if(srcMap["bSingleScroll"].toBool()) ele.wgt = new EleScroll(page, dir+"/" + arrayPics[i]["id"].toString(), 'l', srcMap["iScrollSpeed"].toDouble()); else ele.wgt = new EleScroll(page, dir+"/"+arrayPics[i]["id"].toString()); break; } } else if(ele.type=="MultiPng") { QJsonArray imgs = srcMap["arrayPics"].toArray(); if(imgs.isEmpty()) continue; if(imgs.size()==1 && imgs.at(0)["picDuration"].toInt()==0) ele.wgt = new EleScroll(page, dir+"/", imgs[0].toObject()); else ele.wgt = new EleMultiPng(dir+"/", imgs, page); } else if(ele.type=="DigitalClockNew") ele.wgt = new DigiClock(dir+"/", srcMap.toObject(), page); else if(ele.type=="AnalogClock") ele.wgt = new EleAnaClock(dir+"/"+ele.id, srcMap.toObject(), page); else if(ele.type=="Video" || ele.type=="Audio") { auto video = new EleVideo(dir+"/"+ele.id, page); auto vol = srcMap["vol"].toInt(100); if(vol<100) video->player->setVol(vol/100.0); ele.wgt = video; } else if(ele.type=="WebURL") { auto web = new QWebEngineView(page); web->load(QUrl(srcMap["url"].toString())); ele.wgt = web; } else if(ele.type=="Timer") ele.wgt = new EleTimer(srcMap.toObject(), page); else continue; if(ele.wgt==nullptr) continue; if(ele.startTime>0) ele.wgt->setVisible(false); ele.wgt->setGeometry(ele.x, ele.y, ele.w, ele.h); page->eles.append(ele); } if(bdEle!=nullptr && srcMaps.size()>0) { QJsonArray geometry = border["geometry"].toArray(); ele.x = geometry[0].toInt(); ele.y = geometry[1].toInt(); ele.w = geometry[2].toInt(); ele.h = geometry[3].toInt(); ele.startTime = startTime; ele.endTime = endTime; ele.timeSpan = endTime - startTime; ele.wgt = bdEle; if(ele.startTime>0) ele.wgt->setVisible(false); ele.wgt->setGeometry(ele.x, ele.y, ele.w, ele.h); page->eles.append(ele); } } if(page->timeSpan>0) pages.append(page); } setVisible(true); Page* page0 = pages[0]; EleBase* eleptr; for(int ee=0; eeeles.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)); } menu = new QMenu(this); auto act = menu->addAction(tr("Move to Top Left")); connect(act, &QAction::triggered, this, [this] { move(0,0); }); act = menu->addAction(tr("Set Position")); connect(act, &QAction::triggered, this, [this] { PosDlg dlg(this); dlg.exec(); }); act = menu->addAction(tr("Close")); connect(act, &QAction::triggered, this, [this] { if(self==this) self = nullptr; close(); }); } void PlayWin::sltNext() { if(isVisible()) { pages[cur]->setVisible(false); if(cur+2 > pages.size()) cur = 0; else cur++; Page* page = pages[cur]; if(timer!=nullptr) timer->inter = page->timeSpan; EleBase* ele; for(int ee=0; eeeles.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){ timerMap.remove(id); value.ele->setVisible(value.visible); } } void PlayWin::paintEvent(QPaintEvent *e){ if(timer==nullptr && isVisible()) { if(cur!=0) { pages[cur]->setVisible(false); cur = 0; } pages[cur]->setVisible(true); timer = new SyncTimer(pages[cur]->timeSpan); connect(timer, &SyncTimer::timeout, this, &PlayWin::sltNext, Qt::BlockingQueuedConnection); timer->start(); } QWidget::paintEvent(e); } void PlayWin::mousePressEvent(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { mPressRel = pos() - e->globalPos(); } } void PlayWin::mouseReleaseEvent(QMouseEvent *e) { if(e->button() == Qt::LeftButton) mPressRel.setX(INT_MAX); } void PlayWin::mouseMoveEvent(QMouseEvent *e) { if(e->buttons() & Qt::LeftButton) { if(mPressRel.x()==INT_MAX) return; move(mPressRel + e->globalPos()); } } void PlayWin::leaveEvent(QEvent *) { mPressRel.setX(INT_MAX); } void PlayWin::contextMenuEvent(QContextMenuEvent *event){ menu->exec(event->globalPos()); } void PlayWin::closeEvent(QCloseEvent *) { if(self==this) self = nullptr; }