qt/LedOK/player/eleborder.cpp
2023-04-18 14:14:46 +08:00

77 lines
2.6 KiB
C++

#include "eleborder.h"
#include <QJsonObject>
#include <QPainter>
#include <QPainterPath>
#include <QTimerEvent>
EleBorder::EleBorder(QString path, QString eff, int speed, QWidget *parent) : QWidget{parent}, speed(speed) {
img.load(path);
if(eff.startsWith("ro")) this->eff = 'r';
else if(eff.startsWith("bl")) this->eff = 'b';
}
void EleBorder::paintEvent(QPaintEvent *) {
if(eff=='b' && off > 0) return;
if(timerId==0) {
if(eff=='r') timerId = startTimer(speed==1 ? 66 : (speed==2 ? 33 : 16), Qt::PreciseTimer);
else if(eff=='b') timerId = startTimer(speed==1 ? 500 : (speed==2 ? 250 : 66));
}
int bdWidth = img.height();
int halfBdWidth = (bdWidth+1)/2;
QBrush brush(img);
QTransform transTop = QTransform::fromTranslate(halfBdWidth+off, 0);
QTransform transRight = QTransform::fromTranslate(width() - bdWidth, halfBdWidth*3-width()+off);
transRight.rotate(90);
QTransform transBottom = QTransform::fromTranslate(halfBdWidth*3-height()-off, height() - bdWidth);
transBottom.rotate(180);
QTransform transLeft = QTransform::fromTranslate(0, halfBdWidth-off);
transLeft.rotate(270);
QPainter painter(this);
brush.setTransform(transTop);
QPainterPath path(QPointF(0, 0));
path.lineTo(width(), 0);
path.lineTo(width() - bdWidth, bdWidth);
path.lineTo(bdWidth, bdWidth);
path.closeSubpath();
painter.fillPath(path, brush);
brush.setTransform(transRight);
path = QPainterPath(QPointF(width(), 0));
path.lineTo(width(), height());
path.lineTo(width() - bdWidth, height() - bdWidth);
path.lineTo(width() - bdWidth, bdWidth);
path.closeSubpath();
painter.fillPath(path, brush);
brush.setTransform(transBottom);
path = QPainterPath(QPointF(width(), height()));
path.lineTo(0, height());
path.lineTo(bdWidth, height() - bdWidth);
path.lineTo(width() - bdWidth, height() - bdWidth);
path.closeSubpath();
painter.fillPath(path, brush);
brush.setTransform(transLeft);
path = QPainterPath(QPointF(0, height()));
path.lineTo(0, 0);
path.lineTo(bdWidth, bdWidth);
path.lineTo(bdWidth, height() - bdWidth);
path.closeSubpath();
painter.fillPath(path, brush);
}
void EleBorder::timerEvent(QTimerEvent *) {
if(isVisible()) {
if(eff=='r') {
if(off >= img.width() - 1) off = 0;
else off++;
} else off = off==0 ? 1 : 0;
update();
} else if(timerId!=0) {
killTimer(timerId);
timerId = 0;
}
}