#include "RotatingLabel.h"

RotatingLabel::RotatingLabel(QWidget *parent) : QLabel(parent), m_angle(0) {
    setAlignment(Qt::AlignCenter);
}

void RotatingLabel::setPixmapWithAnimation(const QPixmap &pix, int durationMs) {
    m_pixmap = pix;
    updatePixmap(); // 初始显示

    // 创建动画
    QPropertyAnimation *anim = new QPropertyAnimation(this, "rotationAngle");
    anim->setDuration(durationMs);
    anim->setStartValue(0);
    anim->setEndValue(360); // 360度旋转
    anim->setLoopCount(-1);  // 无限循环
    anim->start();
}

void RotatingLabel::setRotationAngle(int angle) {
    m_angle = angle;
    updatePixmap();
}

void RotatingLabel::updatePixmap() {
    if (m_pixmap.isNull()) return;

    QTransform transform;
    transform.rotate(m_angle);
    QPixmap rotated = m_pixmap.transformed(transform, Qt::SmoothTransformation);

    // 保持图片居中
    QSize size = this->size();
    QPixmap centered(size);
    centered.fill(Qt::transparent);

    QPainter painter(&centered);
    painter.drawPixmap(
        (size.width() - rotated.width()) / 2,
        (size.height() - rotated.height()) / 2,
        rotated
        );

    QLabel::setPixmap(centered);
}
