#include "settingForm.h"
#include "ui_settingForm.h"
#include "preDefine.h"
#include "DTools/configManger.h"
#include <QPrinterInfo>
#include <QSound>
#include "fmPrinter.h"
#include <QListView>

SettingForm::SettingForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingForm)
{
    ui->setupUi(this);

    // 初始化打印机选项
    QString lfprinter = ConfigManger::GetInstance().GetlfPrinterName();
    QString qtprinter = ConfigManger::GetInstance().GetqtPrinterName();
    QString rcprinter = ConfigManger::GetInstance().GetrcPrinterName();
    QStringList printerList = QPrinterInfo::availablePrinterNames();
    printerList.append("禁用此打印机");
    for(int i=0; i<printerList.count(); i++)
    {
        QString name = printerList[i];
        ui->settingCbxlfPrinter->addItem(name);
        ui->settingCbxqtPrinter->addItem(name);
        ui->settingCbxrcPrinter->addItem(name);
        if(!name.compare(lfprinter))
        {
            ui->settingCbxlfPrinter->setCurrentIndex(i);
        }
        if(!name.compare(qtprinter))
        {
            ui->settingCbxqtPrinter->setCurrentIndex(i);
        }
        if(!name.compare(rcprinter))
        {
            ui->settingCbxrcPrinter->setCurrentIndex(i);
        }
    }
//    ui->settingCbxlfPrinter->addItem("禁用此打印机");
//    ui->settingCbxqtPrinter->addItem("禁用此打印机");
//    ui->settingCbxrcPrinter->addItem("禁用此打印机");
    ui->settingCbxlfPrinter->setView(new QListView());
    ui->settingCbxrcPrinter->setView(new QListView());
    ui->settingCbxqtPrinter->setView(new QListView());
    _Init();
}

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

void SettingForm::_Init()
{
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setModal(true);
}

//void SettingForm::on_settingCbxPrinter_currentIndexChanged(const QString &text)
//{
//    ConfigManger::GetInstance().SetPrinterName(text);
//}

void SettingForm::on_settingBtnSoundTest_clicked()
{
    QSound::play(QString("%1/wav/msg.wav").arg(QApplication::applicationDirPath()));
}

void SettingForm::on_settingBtnqtPrintTest_clicked()
{
    QString testStr("这是一个前台打印测试页看到此此页说明打印正常!\r\n\r\n\r\n");
    FmPrinter::GetInstance().TestPrint(ConfigManger::GetInstance().GetqtPrinterName(), testStr);
}

void SettingForm::on_settingBtnlfPrintTest_clicked()
{
    QString testStr("这是一个冷房打印测试页看到此此页说明打印正常!\r\n\r\n\r\n");
    FmPrinter::GetInstance().TestPrint(ConfigManger::GetInstance().GetlfPrinterName(), testStr);
}

void SettingForm::on_settingBtnrcPrintTest_clicked()
{
    QString testStr("这是一个热厨打印测试页看到此此页说明打印正常!\r\n\r\n\r\n");
    FmPrinter::GetInstance().TestPrint(ConfigManger::GetInstance().GetrcPrinterName(), testStr);
}

void SettingForm::on_settingCbxqtPrinter_activated(const QString &text)
{
     ConfigManger::GetInstance().SetqtPrinterName(text);
}

void SettingForm::on_settingCbxlfPrinter_activated(const QString &text)
{
     ConfigManger::GetInstance().SetlfPrinterName(text);
}

void SettingForm::on_settingCbxrcPrinter_activated(const QString &text)
{
     ConfigManger::GetInstance().SetrcPrinterName(text);
}
