#include "printermanger.h"
#include <QPrinterInfo>

PrinterManger::PrinterManger(QObject *parent) : QObject(parent)
{

}


//void PrinterManger::setPrinterBoxName(const QString & name)
//{}

//void PrinterManger::setPrinterPackName(const QString & name)


void PrinterManger::setPrinter(const QSharedPointer<BaseBoxPrinter> box, const QSharedPointer<BasePackinfoPrinter> packInfo)
{
    _boxPrinter = box;
    _packPrinter = packInfo;
}

void PrinterManger::setPrinterName(const QString & name)
{
    _printerName = name;

    if(name.isEmpty()){
        _printerName = QPrinterInfo::defaultPrinterName();
    }
}

void PrinterManger::printerBox(QSharedPointer<BoxInfo> info)
{
    if(_boxPrinter.isNull()) return;
    auto image = _boxPrinter->doPrinter(info);
    if(image.isNull()) return;
    QPrinter printer(QPrinterInfo::printerInfo(_printerName));
    printer.setPageSize(QPrinter::Custom);
    printer.setPageSizeMM(_boxPrinter->printSize());
//    for(int i = 0; i < count; ++i) // TODO：
    {
        doPrinter(printer,image);
    }
}

void PrinterManger::printerBox1(QSharedPointer<BoxInfo> info)
{
    if(_boxPrinter.isNull()) return;
    auto image = _boxPrinter->doPrinter1(info);
    if(image.isNull()) return;
    QPrinter printer(QPrinterInfo::printerInfo(_printerName));
    printer.setPageSize(QPrinter::Custom);
    printer.setPageSizeMM(_boxPrinter->printSize());
//    for(int i = 0; i < count; ++i) // TODO：
    {
        doPrinter(printer,image);
    }
}

void PrinterManger::printerBoxModel(QSharedPointer<BoxInfo> info,int ModelIndex)
{
    if(_boxPrinter.isNull()) return;
    auto image = _boxPrinter->doPrinter(info,ModelIndex);
    if(image.isNull()) return;
    QPrinter printer(QPrinterInfo::printerInfo(_printerName));
    printer.setPageSize(QPrinter::Custom);
    printer.setPageSizeMM(_boxPrinter->printSize());
//    for(int i = 0; i < count; ++i) // TODO：
    {
        doPrinter(printer,image);
    }

}

void PrinterManger::doPrinter(QPrinter & printer,QImage & image)
{
//    QPainter painter(&printer);                         // 创建一个QPainter对象，并指定绘图设备为一个QPainter对象
//    QRect rect = painter.viewport();                     // 获得QPainter对象的视图矩形区域
//    QSize size(image.width(),image.height());                            // 获得图像的大小
    /* 按照图形的比例大小重新设置视图矩形区域 */
//    size.scale(rect.size(),Qt::KeepAspectRatio);
//    painter.setViewport(rect.x(),rect.y(),size.width()+50,size.height());
//        painter.setViewport(rect.x(),rect.y(),size.width(),size.height());
//    painter.setWindow(image.rect());                      // 设置QPainter窗口大小为图像的大小
//    painter.drawImage(0,0,image);                         // 打印图像
//    painter.end();
//    auto ed = printer.newPage();
//    pqInfo(log) << "printer  newPage:" << ed;

//        printer(QPrinterInfo::printerInfo(GlobalInfo::this_()->config().printerName));
        printer.setPageSize(QPrinter::Custom);
        printer.setPageSizeMM(QSizeF(100,70));

//        printer.setPrinterName();
        QPainter painter(&printer);                         // 创建一个QPainter对象，并指定绘图设备为一个QPainter对象
        QRect rect =painter.viewport();                     // 获得QPainter对象的视图矩形区域
        QSize size(image.width(),image.height());                            // 获得图像的大小
        /* 按照图形的比例大小重新设置视图矩形区域 */
        size.scale(rect.size(),Qt::KeepAspectRatio);
        painter.setViewport(rect.x(),rect.y(),size.width()+50,size.height());
//        painter.setViewport(rect.x(),rect.y(),size.width(),size.height());
        painter.setWindow(image.rect());                      // 设置QPainter窗口大小为图像的大小
        painter.drawImage(0,0,image);                         // 打印图像
        painter.end();
        auto ed = printer.newPage();
//        QLogInfo(log) << "printer  newPage:" << ed;
//        printer
}

void PrinterManger::printerPack(QSharedPointer<PackInfo> info)
{
    if(_packPrinter.isNull()) return;
    auto image = _packPrinter->doPrinter(info);
    if(image.isNull()) return;
    QPrinter printer(QPrinterInfo::printerInfo(_printerName));
    printer.setPageSize(QPrinter::Custom);
    printer.setPageSizeMM(_packPrinter->printSize());
//    for(int i = 0; i < count; ++i) // TODO：
    {
        doPrinter(printer,image);
    }
}

void PrinterManger::printerPackModel(QSharedPointer<PackInfo> info,int ModelIndex)
{
    if(_packPrinter.isNull()) return;
    auto image = _packPrinter->doPrinter(info,ModelIndex);
    if(image.isNull()) return;
    QPrinter printer(QPrinterInfo::printerInfo(_printerName));
    printer.setPageSize(QPrinter::Custom);
    printer.setPageSizeMM(_packPrinter->printSize());
//    for(int i = 0; i < count; ++i) // TODO：
    {
        doPrinter(printer,image);
    }
}
