FaceAccess/Linguist/recoUi/recoUiScreensaver.cpp
2024-07-11 11:27:12 +08:00

289 lines
9.4 KiB
C++

#include "recoUiScreensaver.h"
#include <QDebug>
#include <QPixmap>
#include <QHBoxLayout>
#include "UiTools.h"
BgLoadThread::BgLoadThread(QObject* parent) : QObject (parent)
{
qDebug() << "BgLoadThread";
}
BgLoadThread::~BgLoadThread()
{
qDebug() << "~BgLoadThread";
}
void BgLoadThread::slotloadBGs(const QStringList& pics, const QSize& bgSize)
{
//qDebug() << "BgLoadThread::slotloadBGs";
for (int i=0; i<pics.size(); i++){
QPixmap bg(pics[i]);
if(!bg.isNull()){
//qDebug() << "load one background" << pics[i];
emit signalOneBGReady(ScalePixmap(bg, bgSize));
}
}
emit signalAllBGDone();
}
Screensaver::Screensaver(const QString& pic, QWidget *parent) : QWidget (parent)
{
QLabel* m_label_background = new QLabel(this);
m_label_background->setAlignment(Qt::AlignCenter);
m_label_background->resize(UiConfig::GetInstance()->getUiWidth(), UiConfig::GetInstance()->getUiHeight());
qDebug() << "screensaver:" << pic;
QPixmap bg(pic);
if(!bg.isNull()){
m_label_background->setPixmap(ScalePixmap(bg, QSize(UiConfig::GetInstance()->getUiWidth(), UiConfig::GetInstance()->getUiHeight())));
}
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(m_label_background, 100, Qt::AlignCenter);
layout->setMargin(0);
setLayout(layout);
resize(UiConfig::GetInstance()->getUiWidth(), UiConfig::GetInstance()->getUiHeight());
}
Screensaver::Screensaver(const QPixmap& pic, QWidget *parent) : QWidget (parent)
{
QLabel* m_label_background = new QLabel(this);
m_label_background->resize(UiConfig::GetInstance()->getUiWidth(), UiConfig::GetInstance()->getUiHeight());
m_label_background->setPixmap(pic);
m_label_background->setAlignment(Qt::AlignCenter);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(m_label_background, 100, Qt::AlignCenter);
layout->setMargin(0);
setLayout(layout);
resize(UiConfig::GetInstance()->getUiWidth(), UiConfig::GetInstance()->getUiHeight());
}
const QString recoUiScreensaver::AD_IMG[UiConfig::SUPPORT_SCREEN_SIZE_QUANTITY] = {
":/res/image/reco_ad_large.png",
":/res/image/reco_ad_large.png",
":/res/image/reco_ad_large.png",
":/res/image/reco_ad_large.png",
":/res/image/reco_ad_large.png"
};
const QString recoUiScreensaver::SCREENSAVER[UiConfig::SUPPORT_SCREEN_SIZE_QUANTITY][DEFAULT_SCREENSAVER_QUANTITY] = {
{":/res/image/screensaver-600-1024.jpg", ":/res/image/screensaver_720_1280_1.jpg", ":/res/image/screensaver_720_1280_2.jpg"},
{":/res/image/screensaver-800-1280.jpg", ":/res/image/screensaver_800_1280_1.jpg", ":/res/image/screensaver_800_1280_2.jpg"},
{":/res/image/screensaver-720-1280.jpg", ":/res/image/screensaver_720_1280_1.jpg", ":/res/image/screensaver_720_1280_2.jpg"},
{":/res/image/screensaver-800-1280.jpg", ":/res/image/screensaver_800_1280_1.jpg", ":/res/image/screensaver_800_1280_2.jpg"},
{":/res/image/screensaver-720-1280.jpg", ":/res/image/screensaver_720_1280_1.jpg", ":/res/image/screensaver_720_1280_2.jpg"}
};
recoUiScreensaver::recoUiScreensaver(int sourceType, QWidget *parent)
: WidgetWithBackstageInterface(parent), m_sourceType(sourceType)
{
qDebug() << "recoUiScreensaver()";
//setAttribute(Qt::WA_AcceptTouchEvents); //touch
//installEventFilter(this); //mouse click
/*
m_label_logo = new QLabel(this);
QPixmap pm(":/res/image/home_logo.png");
//pm = pm.scaled(50, 50, Qt::KeepAspectRatio);
m_label_logo->setPixmap(pm);
m_label_time = new QLabel(this);
QPalette palette(this->palette());
palette.setColor(QPalette::WindowText,Qt::white);
m_label_time->setPalette(palette);
QFont ft;
ft.setPointSize(30);
m_label_time->setFont(ft);
QHBoxLayout* hbLayout = new QHBoxLayout();
hbLayout->addWidget(m_label_logo, 1, Qt::AlignLeft | Qt::AlignTop);
hbLayout->addWidget(m_label_time, 1, Qt::AlignRight | Qt::AlignTop);
hbLayout->setMargin(40);
setAutoFillBackground(true);
//pm_logo.scaled(469, 700, Qt::IgnoreAspectRatio);
//pm_logo.scaledToHeight(350, Qt::FastTransformation);
palette.setBrush(QPalette::Background,QBrush(QPixmap(":/res/image/screensaver-600-1024.jpg")));
setPalette(palette);
setLayout(hbLayout);
*/
QLabel* blackBg = new QLabel(this);
blackBg->resize(UiConfig::GetInstance()->getUiWidth(), UiConfig::GetInstance()->getUiHeight());
blackBg->setStyleSheet("QLabel{background:#000000;}");
m_stack_scPage = new QStackedWidget(this);
QGridLayout* layout = new QGridLayout();
layout->addWidget(blackBg, 0, 0);
layout->addWidget(m_stack_scPage, 0, 0);
layout->setMargin(0);
setLayout(layout);
resize(UiConfig::GetInstance()->getUiWidth(), UiConfig::GetInstance()->getUiHeight());
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(slotTimer()));
connect(m_stack_scPage, SIGNAL(currentChanged(int)), this, SIGNAL(signalCurrentChange(int)));
}
recoUiScreensaver::~recoUiScreensaver()
{
qDebug() << "~recoUiScreensaver()";
if(m_thLoadBg){
m_thLoadBg->quit();
m_thLoadBg->wait();
}
}
bool recoUiScreensaver::eventFilter(QObject *watched, QEvent *event)
{
if( watched == this){
if(event->type() == QEvent::MouseButtonPress){
qDebug() << "recoUiScreensaver mouse press";
if(m_backstageIf){
qDebug() << "recoUiScreensaver wakeUpFromScreenSaverOrScreenOff";
m_backstageIf->wakeUpFromScreenSaverOrScreenOff();
}
}
}
else{
return QWidget::eventFilter(watched, event);
}
return false;
}
bool recoUiScreensaver::event(QEvent *event)
{
switch( event->type() )
{
case QEvent::TouchBegin:
{
qDebug() << "recoUiScreensaver touch begin";
if(m_backstageIf){
m_backstageIf->wakeUpFromScreenSaverOrScreenOff();
}
event->accept();
return true;
}
case QEvent::TouchUpdate:
{
event->accept();
return true;
}
case QEvent::TouchEnd:
{
qDebug() << "recoUiScreensaver touch end";
event->accept();
return true;
}
default:
break;
}
return QWidget::event(event);
}
void recoUiScreensaver::setBackstageUiinterface(BackstageInterfaceForUi *interface)
{
m_backstageIf = interface;
slotUpdateScreenSaver();
}
void recoUiScreensaver::slotUpdateScreenSaver()
{
qDebug() << "slotUpdateScreenSaver:" << m_sourceType;
if(m_isLoading){
qDebug() << "bg is loading...";
return;
}
stScreensaverCfg cfg;
if(0 == (0 == m_sourceType ? m_backstageIf->getScreenSaverConfig(cfg) : m_backstageIf->getRecoUiAdConfig(cfg))){
m_iTimerInterval = cfg.interval * 1000;
m_timer->start(m_iTimerInterval);
qDebug() << "getScreenSaverConfig successed, time interval:" << m_iTimerInterval;
m_ilastBgQty = m_stack_scPage->count();
qDebug() << "m_ilastBgQty:" << m_ilastBgQty;
qDebug() << "type:" << cfg.type << " qty:" << cfg.picQty;
QStringList pics;
QString picsCmp;
if(0 == cfg.type){
//m_stack_scPage->addWidget(new Screensaver(UiConfig::GetInstance()->getScreenSaver(), this));
if(0 == m_sourceType){
picsCmp = SCREENSAVER[UiConfig::GetInstance()->getScreenConfigIndex()][UiConfig::GetInstance()->defaultScreensaver()];
}
else{
picsCmp = AD_IMG[UiConfig::GetInstance()->getScreenConfigIndex()];
}
pics << picsCmp;
}
else if(1 == cfg.type){
for (int i=0; i<cfg.picQty; i++) {
picsCmp += cfg.pic[i];
pics << cfg.pic[i];
}
}
if(picsCmp == m_lastPics){
qDebug() << "same screensaver";
return;
}
m_isLoading = true;
m_lastPics = picsCmp;
startThread();
emit signalLoadBGs(pics, QSize(UiConfig::GetInstance()->getUiWidth(), UiConfig::GetInstance()->getUiHeight()));
}
}
void recoUiScreensaver::slotTimer()
{
//qDebug() << "slotTimer";
m_stack_scPage->setCurrentIndex(m_stack_scPage->currentIndex() < m_stack_scPage->count() - 1 ? m_stack_scPage->currentIndex() + 1 : 0);
}
void recoUiScreensaver::slotOneBGReady(const QPixmap& pic)
{
//qDebug() << "recoUiScreensaver::slotOneBGReady";
m_stack_scPage->addWidget(new Screensaver(pic, this));
}
void recoUiScreensaver::slotAllBGDone()
{
//qDebug() << "recoUiScreensaver::slotAllBGDone";
if(m_ilastBgQty){
for(int i=0; i<m_ilastBgQty; i++){
QWidget* widget = m_stack_scPage->widget(0);
m_stack_scPage->removeWidget(widget);
widget->deleteLater();
qDebug() << "delete bg:" << i;
}
}
if(m_stack_scPage->count() > 1){
m_timer->start(m_iTimerInterval);
}
else {
m_timer->stop();
}
m_isLoading = false;
}
void recoUiScreensaver::startThread()
{
if(m_thLoadBg){
return;
}
m_thLoadBg= new QThread();
m_oLoadBg = new BgLoadThread();
m_oLoadBg->moveToThread(m_thLoadBg);
connect(m_thLoadBg,&QThread::finished,m_thLoadBg,&QObject::deleteLater);
connect(m_thLoadBg,&QThread::finished,m_oLoadBg,&QObject::deleteLater);
connect(this,&recoUiScreensaver::signalLoadBGs,m_oLoadBg,&BgLoadThread::slotloadBGs);
connect(m_oLoadBg, SIGNAL(signalOneBGReady(const QPixmap&)), this, SLOT(slotOneBGReady(const QPixmap&)));
connect(m_oLoadBg, SIGNAL(signalAllBGDone()), this, SLOT(slotAllBGDone()));
m_thLoadBg->start();
}