#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QSerialPortInfo>
 
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    serialPort(new QSerialPort),
    ymodemFileTransmit(new YmodemFileTransmit),
    ymodemFileReceive(new YmodemFileReceive)
{
    transmitButtonStatus = false;
    receiveButtonStatus  = false;
 
    ui->setupUi(this);
 
    QSerialPortInfo serialPortInfo;
 
    foreach(serialPortInfo, QSerialPortInfo::availablePorts())
    {
        ui->comPort->addItem(serialPortInfo.portName());
    }
 
    serialPort->setPortName("COM1");
    serialPort->setBaudRate(115200);
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setParity(QSerialPort::NoParity);
    serialPort->setFlowControl(QSerialPort::NoFlowControl);
 
    connect(ymodemFileTransmit, SIGNAL(transmitProgress(int)), this, SLOT(transmitProgress(int)));
    connect(ymodemFileReceive, SIGNAL(receiveProgress(int)), this, SLOT(receiveProgress(int)));
    connect(ymodemFileTransmit, SIGNAL(transmitStatus(YmodemFileTransmit::Status)), this, SLOT(transmitStatus(YmodemFileTransmit::Status)));
    connect(ymodemFileReceive, SIGNAL(receiveStatus(YmodemFileReceive::Status)), this, SLOT(receiveStatus(YmodemFileReceive::Status)));
}
 
Widget::~Widget()
{
    delete ui;
    delete serialPort;
    delete ymodemFileTransmit;
    delete ymodemFileReceive;
}
 
void Widget::on_comButton_clicked()
{
    static bool button_status = false;
 
    if(button_status == false)
    {
        serialPort->setPortName(ui->comPort->currentText());
        serialPort->setBaudRate(ui->comBaudRate->currentText().toInt());
 
        if(serialPort->open(QSerialPort::ReadWrite) == true)
        {
            button_status = true;
 
            ui->comPort->setDisabled(true);
            ui->comBaudRate->setDisabled(true);
            ui->comButton->setText(u8"关闭串口");
 
            ui->transmitBrowse->setEnabled(true);
            ui->receiveBrowse->setEnabled(true);
 
            if(ui->transmitPath->text().isEmpty() != true)
            {
                ui->transmitButton->setEnabled(true);
            }
 
            if(ui->receivePath->text().isEmpty() != true)
            {
                ui->receiveButton->setEnabled(true);
            }
        }
        else
        {
            QMessageBox::warning(this, u8"串口打开失败", u8"请检查串口是否已被占用！", u8"关闭");
        }
    }
    else
    {
        button_status = false;
 
        serialPort->close();
 
        ui->comPort->setEnabled(true);
        ui->comBaudRate->setEnabled(true);
        ui->comButton->setText(u8"打开串口");
 
        ui->transmitBrowse->setDisabled(true);
        ui->transmitButton->setDisabled(true);
 
        ui->receiveBrowse->setDisabled(true);
        ui->receiveButton->setDisabled(true);
    }
}
 
void Widget::on_transmitBrowse_clicked()
{
    ui->transmitPath->setText(QFileDialog::getOpenFileName(this, u8"打开文件", ".", u8"任意文件 (*.*)"));
 
    if(ui->transmitPath->text().isEmpty() != true)
    {
        ui->transmitButton->setEnabled(true);
    }
    else
    {
        ui->transmitButton->setDisabled(true);
    }
}
 
void Widget::on_receiveBrowse_clicked()
{
    ui->receivePath->setText(QFileDialog::getExistingDirectory(this, u8"选择目录", ".", QFileDialog::ShowDirsOnly));
 
    if(ui->receivePath->text().isEmpty() != true)
    {
        ui->receiveButton->setEnabled(true);
    }
    else
    {
        ui->receiveButton->setDisabled(true);
    }
}
 
void Widget::on_transmitButton_clicked()
{
    if(transmitButtonStatus == false)
    {
        serialPort->close();
 
        ymodemFileTransmit->setFileName(ui->transmitPath->text());
        ymodemFileTransmit->setPortName(ui->comPort->currentText());
        ymodemFileTransmit->setPortBaudRate(ui->comBaudRate->currentText().toInt());
 
        if(ymodemFileTransmit->startTransmit() == true)
        {
            transmitButtonStatus = true;
 
            ui->comButton->setDisabled(true);
 
            ui->receiveBrowse->setDisabled(true);
            ui->receiveButton->setDisabled(true);
 
            ui->transmitBrowse->setDisabled(true);
            ui->transmitButton->setText(u8"取消");
            ui->transmitProgress->setValue(0);
        }
        else
        {
            QMessageBox::warning(this, u8"失败", u8"文件发送失败！", u8"关闭");
        }
    }
    else
    {
        ymodemFileTransmit->stopTransmit();
    }
}
 
void Widget::on_receiveButton_clicked()
{
    if(receiveButtonStatus == false)
    {
        serialPort->close();
 
        ymodemFileReceive->setFilePath(ui->receivePath->text());
        ymodemFileReceive->setPortName(ui->comPort->currentText());
        ymodemFileReceive->setPortBaudRate(ui->comBaudRate->currentText().toInt());
 
        if(ymodemFileReceive->startReceive() == true)
        {
            receiveButtonStatus = true;
 
            ui->comButton->setDisabled(true);
 
            ui->transmitBrowse->setDisabled(true);
            ui->transmitButton->setDisabled(true);
 
            ui->receiveBrowse->setDisabled(true);
            ui->receiveButton->setText(u8"取消");
            ui->receiveProgress->setValue(0);
        }
        else
        {
            QMessageBox::warning(this, u8"失败", u8"文件接收失败！", u8"关闭");
        }
    }
    else
    {
        ymodemFileReceive->stopReceive();
    }
}
 
void Widget::transmitProgress(int progress)
{
    ui->transmitProgress->setValue(progress);
}
 
void Widget::receiveProgress(int progress)
{
    ui->receiveProgress->setValue(progress);
}
 
void Widget::transmitStatus(Ymodem::Status status)
{
    switch(status)
    {
        case YmodemFileTransmit::StatusEstablish:
        {
            break;
        }
 
        case YmodemFileTransmit::StatusTransmit:
        {
            break;
        }
 
        case YmodemFileTransmit::StatusFinish:
        {
            transmitButtonStatus = false;
 
            ui->comButton->setEnabled(true);
 
            ui->receiveBrowse->setEnabled(true);
 
            if(ui->receivePath->text().isEmpty() != true)
            {
                ui->receiveButton->setEnabled(true);
            }
 
            ui->transmitBrowse->setEnabled(true);
            ui->transmitButton->setText(u8"发送");
 
            QMessageBox::warning(this, u8"成功", u8"文件发送成功！", u8"关闭");
 
            break;
        }
 
        case YmodemFileTransmit::StatusAbort:
        {
            transmitButtonStatus = false;
 
            ui->comButton->setEnabled(true);
 
            ui->receiveBrowse->setEnabled(true);
 
            if(ui->receivePath->text().isEmpty() != true)
            {
                ui->receiveButton->setEnabled(true);
            }
 
            ui->transmitBrowse->setEnabled(true);
            ui->transmitButton->setText(u8"发送");
 
            QMessageBox::warning(this, u8"失败", u8"文件发送失败！", u8"关闭");
 
            break;
        }
 
        case YmodemFileTransmit::StatusTimeout:
        {
            transmitButtonStatus = false;
 
            ui->comButton->setEnabled(true);
 
            ui->receiveBrowse->setEnabled(true);
 
            if(ui->receivePath->text().isEmpty() != true)
            {
                ui->receiveButton->setEnabled(true);
            }
 
            ui->transmitBrowse->setEnabled(true);
            ui->transmitButton->setText(u8"发送");
 
            QMessageBox::warning(this, u8"失败", u8"文件发送失败！", u8"关闭");
 
            break;
        }
 
        default:
        {
            transmitButtonStatus = false;
 
            ui->comButton->setEnabled(true);
 
            ui->receiveBrowse->setEnabled(true);
 
            if(ui->receivePath->text().isEmpty() != true)
            {
                ui->receiveButton->setEnabled(true);
            }
 
            ui->transmitBrowse->setEnabled(true);
            ui->transmitButton->setText(u8"发送");
 
            QMessageBox::warning(this, u8"失败", u8"文件发送失败！", u8"关闭");
        }
    }
}
 
void Widget::receiveStatus(YmodemFileReceive::Status status)
{
    switch(status)
    {
        case YmodemFileReceive::StatusEstablish:
        {
            break;
        }
 
        case YmodemFileReceive::StatusTransmit:
        {
            break;
        }
 
        case YmodemFileReceive::StatusFinish:
        {
            receiveButtonStatus = false;
 
            ui->comButton->setEnabled(true);
 
            ui->transmitBrowse->setEnabled(true);
 
            if(ui->transmitPath->text().isEmpty() != true)
            {
                ui->transmitButton->setEnabled(true);
            }
 
            ui->receiveBrowse->setEnabled(true);
            ui->receiveButton->setText(u8"接收");
 
            QMessageBox::warning(this, u8"成功", u8"文件接收成功！", u8"关闭");
 
            break;
        }
 
        case YmodemFileReceive::StatusAbort:
        {
            receiveButtonStatus = false;
 
            ui->comButton->setEnabled(true);
 
            ui->transmitBrowse->setEnabled(true);
 
            if(ui->transmitPath->text().isEmpty() != true)
            {
                ui->transmitButton->setEnabled(true);
            }
 
            ui->receiveBrowse->setEnabled(true);
            ui->receiveButton->setText(u8"接收");
 
            QMessageBox::warning(this, u8"失败", u8"文件接收失败！", u8"关闭");
 
            break;
        }
 
        case YmodemFileReceive::StatusTimeout:
        {
            receiveButtonStatus = false;
 
            ui->comButton->setEnabled(true);
 
            ui->transmitBrowse->setEnabled(true);
 
            if(ui->transmitPath->text().isEmpty() != true)
            {
                ui->transmitButton->setEnabled(true);
            }
 
            ui->receiveBrowse->setEnabled(true);
            ui->receiveButton->setText(u8"接收");
 
            QMessageBox::warning(this, u8"失败", u8"文件接收失败！", u8"关闭");
 
            break;
        }
 
        default:
        {
            receiveButtonStatus = false;
 
            ui->comButton->setEnabled(true);
 
            ui->transmitBrowse->setEnabled(true);
 
            if(ui->transmitPath->text().isEmpty() != true)
            {
                ui->transmitButton->setEnabled(true);
            }
 
            ui->receiveBrowse->setEnabled(true);
            ui->receiveButton->setText(u8"接收");
 
            QMessageBox::warning(this, u8"失败", u8"文件接收失败！", u8"关闭");
        }
    }
}
