qt/LedOK/player/elescroll.cpp

73 lines
2.5 KiB
C++
Raw Normal View History

2022-01-20 10:08:17 +08:00
#include "elescroll.h"
#include <QJsonObject>
#include <QPainter>
#include <complex>
2022-08-25 18:37:24 +08:00
EleScroll::EleScroll(QWidget *parent, QString dirPre, QJsonObject map) : QWidget{parent} {
2022-01-20 10:08:17 +08:00
img.load(dirPre + map["id"].toString());
QString effStr = map["effect"].toString();
if(effStr.isNull() || effStr=="no") return;
double effDurD = map["effectSpeed"].toInt()/2;
if(effDurD==0) return;
int idx = effStr.lastIndexOf(' ');
if(idx > -1) {
2022-08-25 18:37:24 +08:00
effect = effStr.at(idx+1).toLatin1();
2022-01-20 10:08:17 +08:00
if(effect=='l') end = -(img.width()-1);
else if(effect=='r') end = img.width()-1;
else if(effect=='t') end = -(img.height()-1);
else if(effect=='b') end = img.height()-1;
}
2022-08-25 18:37:24 +08:00
interval = ceil(effDurD/16.666666)*16.666666;
curAdd = ceil(interval/effDurD);
}
EleScroll::EleScroll(QWidget *parent, QString imgPath, char effect, double effDur) : QWidget{parent}, effect(effect) {
img.load(imgPath);
if(effect==0) return;
if(effDur==0) return;
if(effect=='l') end = -(img.width()-1);
else if(effect=='r') end = img.width()-1;
else if(effect=='t') end = -(img.height()-1);
else if(effect=='b') end = img.height()-1;
interval = ceil(effDur/16.666666)*16.666666;
curAdd = ceil(interval/effDur);
2022-01-20 10:08:17 +08:00
}
void EleScroll::paintEvent(QPaintEvent *) {
if(img.isNull()) return;
2022-08-25 18:37:24 +08:00
if(timerId==0 && effect!=0 && interval!=0) {
2022-01-20 10:08:17 +08:00
cur = 0;
2022-08-25 18:37:24 +08:00
timerId = startTimer(interval, Qt::PreciseTimer);
2022-01-20 10:08:17 +08:00
}
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
if(effect=='l') {
painter.drawPixmap(cur, 0, img);
painter.drawPixmap(cur+img.width(), 0, img);
} else if(effect=='r') {
painter.drawPixmap(cur, 0, img);
painter.drawPixmap(cur-img.width(), 0, img);
} else if(effect=='t') {
painter.drawPixmap(0, cur, img);
painter.drawPixmap(0, cur+img.height(), img);
} else if(effect=='b') {
painter.drawPixmap(0, cur, img);
painter.drawPixmap(0, cur-img.height(), img);
} else painter.drawPixmap(0, 0, img);
}
void EleScroll::timerEvent(QTimerEvent *) {
if(isVisible()) {
if(effect=='t' || effect=='l') {
if(cur <= end) cur = 0;
else cur-=curAdd;
} else if(effect=='b' || effect=='r') {
if(cur >= end) cur = 0;
else cur+=curAdd;
}
update();
} else if(timerId!=0) {
killTimer(timerId);
timerId = 0;
}
}