#include "PaperWidget.h"

#include <QApplication>
#include <QGraphicsDropShadowEffect>
#include <QPainter>

#include "qzxing/QZXing.h"

#include "ui_PaperWidget.h"

PaperWidget::PaperWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PaperWidget)
{
    ui->setupUi(this);
   // ui->widget->hide();

    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(this);
    effect->setOffset(0, 0);
    effect->setColor(QColor(68, 68, 68));
    effect->setBlurRadius(10);
    ui->widget_2->setGraphicsEffect(effect);
}

PaperWidget::~PaperWidget()
{
    delete ui;
}

void PaperWidget::SetBackground(const QString& path, const QString& url) {
    setAutoFillBackground(true);
    QPixmap px(path);
    //px_ = px;
    //if (!px.isNull()) {
    //    px_ = px.scaled(size(), Qt::KeepAspectRatio);
    //}
    QPalette pal = palette();
    pal.setBrush(QPalette::Window, QBrush(px.scaled(size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
    setPalette(pal);

    GeneratorQRcode(url);
}

void PaperWidget::GeneratorQRcode(const QString& url) {

    // 创建一个 QZXing 对象
    QZXing qzxing;

    // 生成二维码图像
    QImage qrImage = qzxing.encodeData(url);

    // 将二维码图像转换为 QPixmap
    QPixmap qrPixmap = QPixmap::fromImage(qrImage);
    qDebug() << __FUNCTION__ << ui->label_2->size();
    QPixmap pixmap = qrPixmap.scaled(QSize(220, 220), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_2->setPixmap(pixmap);
}

//void PaperWidget::paintEvent(QPaintEvent* e) {
//    if (px_.isNull()) {
//        QWidget::paintEvent(e);
//        return;
//    }
//
//    QPainter painter(this);
//    painter.drawPixmap((size().width() - px_.width())/ 2, 0, px_);
//
//    QWidget::paintEvent(e);
//}