tmp

#include "mainwindow.h"
#include "ui_mainwindow.h"

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

    //设置坐标系
    ui->qcustomplot->addGraph();
    //ui->qcustomplot->setInteractions(QCP::iRangeDrag|QCP::iRangeZoom);
    ui->qcustomplot->xAxis2->setPadding(0);
    ui->qcustomplot->xAxis->setLabel("V");
    ui->qcustomplot->yAxis->setLabel("P");
    ui->qcustomplot->yAxis->setAutoTicks(true);
    ui->qcustomplot->yAxis->setAutoTickStep(true);
    ui->qcustomplot->yAxis->setAutoSubTicks(true);
    ui->qcustomplot->xAxis2->setVisible(true);
    ui->qcustomplot->xAxis2->setTickLabels(false);
    ui->qcustomplot->yAxis2->setVisible(true);
    ui->qcustomplot->yAxis2->setTickLabels(false);
    ui->qcustomplot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));
    connect(ui->qcustomplot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->qcustomplot->xAxis2, SLOT(setRange(QCPRange)));
    connect(ui->qcustomplot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->qcustomplot->yAxis2, SLOT(setRange(QCPRange)));



    //设置按钮风格
    const QList<QPushButton*> allButtons = this->findChildren<QPushButton *>();
    foreach (QPushButton *button, allButtons)
        button->setStyleSheet(QString::fromLocal8Bit("*{font-size:14px;color:#FFFFFF;background-color:#009966;font-family:\"微软雅黑\";} \n *::disabled{color:white;background-color:gray;}"));

    //实例化KCV600并自动连接
    m_pKCV600 = new KCV_600(this);
    m_pKCV600->ConnectDevice();

    //实例化DP700并自动连接打开显示
    m_pDP700 = new DP700(this);
    m_pDP700->ConnectDevice();
    m_pDP700->OpenCloseDevice(true);
    m_pDP700->OpenDisplay(true);


    //实例化数据表model
    m_pDefaultModel = new QSqlTableModel;
    m_pAutoScanModel = new QSqlTableModel;
    m_pTableView = new QTableView;
    QString defaultTable("measure_default");
    connectModelView(m_pDefaultModel,m_pTableView,defaultTable);
    ui->tableViewLayout->addWidget(m_pTableView);

    connect(ui->lineEdit_5,SIGNAL(returnPressed()),this,SLOT(on_pushButton_7_clicked()));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete m_pDefaultModel;
    delete m_pTableView;
    delete m_pAutoScanModel;
}

bool MainWindow::insertData(QSqlTableModel *model, double vol, double cap, double cur,int row)
{

    QDateTime currentTime = QDateTime::currentDateTime();
    QString date = currentTime.toString("yyyy-MM-dd");
    QString time = currentTime.toString("hh:mm:ss");
    QSqlRecord record = model->record();
    record.setValue("voltage",vol);
    record.setValue("capacity",cap);
    record.setValue("current",cur);
    record.setValue("date",date);
    record.setValue("time",time);
    record.setValue("name",ui->lineEdit->text());
    record.setValue("temp",ui->lineEdit_2->text());
    record.setValue("No",ui->LabelSpinBox->text());
    record.setValue("hum",ui->lineEdit_4->text());

    if(!model->insertRecord(row,record))
    {
        qDebug() <<model->lastError().text();
        return false;
    }
    if(model->submitAll())
        qDebug() << "submitAll success";
    else
        qDebug() << "submitAll failed";
    return true;
}

void MainWindow::connectModelView(QSqlTableModel *model, QTableView *view,QString table)
{
    model->setTable(table);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();


    //设置表格头
    model->setHeaderData(0,Qt::Horizontal,QString::fromLocal8Bit("电压"));
    model->setHeaderData(1,Qt::Horizontal,QString::fromLocal8Bit("电容"));
    model->setHeaderData(2,Qt::Horizontal,QString::fromLocal8Bit("电流"));
    model->setHeaderData(3,Qt::Horizontal,QString::fromLocal8Bit("日期"));
    model->setHeaderData(4,Qt::Horizontal,QString::fromLocal8Bit("时间"));
    model->setHeaderData(5,Qt::Horizontal,QString::fromLocal8Bit("名称"));
    model->setHeaderData(6,Qt::Horizontal,QString::fromLocal8Bit("温度"));
    model->setHeaderData(7,Qt::Horizontal,QString::fromLocal8Bit("编号"));
    model->setHeaderData(8,Qt::Horizontal,QString::fromLocal8Bit("湿度"));

    //实例化数据表view
    view->setModel(model);
    view->setEditTriggers(QTableView::NoEditTriggers);
    view->setSelectionBehavior(QAbstractItemView::SelectRows);
    view->hideColumn(5);
    view->hideColumn(6);
    view->hideColumn(7);
    view->hideColumn(8);
    view->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Stretch);
    view->horizontalHeader()->setSectionResizeMode(1,QHeaderView::Stretch);
    view->horizontalHeader()->setSectionResizeMode(2,QHeaderView::Stretch);
    view->horizontalHeader()->setSectionResizeMode(3,QHeaderView::Stretch);
    view->horizontalHeader()->setSectionResizeMode(4,QHeaderView::Stretch);
}

bool MainWindow::on_pushButton_7_clicked()
{
    ui->qcustomplot->graph(0)->clearData();
    if(ui->LabelSpinBox->text() == "0")
    {
        QMessageBox question(QMessageBox::NoIcon,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("样品编号为默认值,是否在不填写编号的情况下测量"));
        question.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
        question.setButtonText(QMessageBox::Yes,QString::fromLocal8Bit("确定"));
        question.setButtonText(QMessageBox::No,QString::fromLocal8Bit("取消"));
        if(question.exec() == QMessageBox::No)
            return false;
    }
    QSqlQuery query;
    bool success = query.exec(QString("create table measure_%1(voltage double,capacity double,current double,date varchar(20),time varchar(20),name varchar(20),temp varchar(20),no varchar(20),hum varchar(20))").arg(ui->LabelSpinBox->text()));
    if(success)
        qDebug() << "table create success";
    else
    {
        qDebug() << "table create fail";
        qDebug() <<query.lastError().text();
        if(query.lastError().text().mid(0,5) == "table")
        {
            QMessageBox question(QMessageBox::NoIcon,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("当前编号测量表格已存在,是否替换"));
            question.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
            question.setButtonText(QMessageBox::Yes,QString::fromLocal8Bit("确定"));
            question.setButtonText(QMessageBox::No,QString::fromLocal8Bit("取消"));
            if(question.exec() == QMessageBox::No)
                return false;
        }
    }

    //建立model-view模型
    QString table("measure_");
    table += ui->LabelSpinBox->text();
    connectModelView(m_pAutoScanModel,m_pTableView,table);


    //发送指令并返回数据,其中DP700发送后检查电压值是否设定正确,KCV600发送后等待1s读取数据
    QVector<double> buff(20);
    double voltage = ui->lineEdit_5->text().toDouble();
    if(voltage <0 || voltage>35)
    {
        QMessageBox::warning(this,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("电压范围为0-35V"));
        return false;
    }
    if(!m_pDP700->SetVoltage(voltage))
        return false;
    General::Wait(2);
    buff = m_pKCV600->GetCurrentValue();
    if(!insertData(m_pAutoScanModel,buff.at(0),buff.at(1),buff.at(2)))
    {
        QMessageBox::warning(this,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("插入数据失败,请检查是否正确填写数据信息"));
        return false;
    }

    //绘制曲线图
    ui->qcustomplot->graph(0)->addData(buff.at(0),buff.at(1));
    ui->qcustomplot->graph(0)->rescaleAxes(true);
    ui->qcustomplot->replot();
    buff.clear();
    return true;
    QCPGraph *graph = ui->qcustomplot->graph(0);
    graph->addData(buff.at(0),buff.at(1));
    graph->rescaleValueAxis(true);
    ui->qcustomplot->yAxis->setOffset(-(ui->qcustomplot->width()/2-15));
    ui->qcustomplot->replot();
    buff.clear();
    return true;
}

/*************************************************
Function:       // 按钮槽函数
Description:    // 电容电压自动扫描测量及绘图
Return:         // 无
*************************************************/
bool MainWindow::on_pushButton_9_clicked()
{

/*****************************************************数据准备部分**********************************************************/
    ui->qcustomplot->graph(0)->clearData();

    /*用于当前的绘图数据,并不保存到数据库*/
    QVector<double> lowKey,lowValue;
    QVector<double> highKey,highValue;
    double minValue = ui->comboBox_2->text().toDouble();
    double maxValue = ui->comboBox->text().toDouble();
    double stepValue = ui->comboBox_4->text().toDouble();

    if(ui->comboBox_4->text().toDouble() == 0)
    {
        QMessageBox::warning(this,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("电压步进值为零"));
        return false;
    }
    if(ui->comboBox_4->text().toDouble() >= ui->comboBox->text().toDouble()||ui->comboBox_4->text().toDouble() >= ui->comboBox_2->text().toDouble())
    {
        QMessageBox::warning(this,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("电压步进值大于或等于电压设定值"));
        return false;
    }

/**********************************************************数据库部分*****************************************************************/
    QSqlQuery query;
    bool success = query.exec(QString("create table measure_%1(voltage double,capacity double,current double,date varchar(20),time varchar(20),name varchar(20),temp varchar(20),no varchar(20),hum varchar(20))").arg(ui->LabelSpinBox->text()));
    if(success)
        qDebug() << "table create success";
    else
    {
        qDebug() << "table create fail";
        qDebug() <<query.lastError().text();
        if(query.lastError().text().mid(0,5) == "table")
        {
            QMessageBox question(QMessageBox::NoIcon,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("当前编号测量表格已存在,是否替换"));
            question.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
            question.setButtonText(QMessageBox::Yes,QString::fromLocal8Bit("确定"));
            question.setButtonText(QMessageBox::No,QString::fromLocal8Bit("取消"));
            if(question.exec() == QMessageBox::No)
                return false;
            else
            {
                bool success = query.exec(QString("drop table measure_%1").arg(ui->LabelSpinBox->text()));
                if(success)
                    qDebug() << "table delete success";
                else
                {
                    qDebug() << "table delete fail";
                    qDebug() << query.lastError().text();
                }
                success = query.exec(QString("create table measure_%1(voltage double,capacity double,current double,date varchar(20),time varchar(20),name varchar(20),temp varchar(20),no varchar(20),hum varchar(20))").arg(ui->LabelSpinBox->text()));
                if(success)
                    qDebug() << "table create success";
                else
                {
                    qDebug() << "table create fail";
                    qDebug() << query.lastError().text();
                }
            }
        }
    }

    /*建立model-view模型*/
    QString table("measure_");
    table += ui->LabelSpinBox->text();
    connectModelView(m_pAutoScanModel,m_pTableView,table);

/**********************************************************测量逻辑部分*****************************************************************/
    //正向电压自动扫描
    for(double voltage=0;voltage<=maxValue+0.03;voltage+=stepValue)
    {
        QVector<double> buff(20);
        //发送电压指令
        if(!m_pDP700->SetVoltage(voltage))return false;
        General::Wait(2);
        buff = m_pKCV600->GetCurrentValue();
        if(buff.empty())break;
        //插入数据,若插入失败则跳出当前循环
        if(!insertData(m_pAutoScanModel,buff.at(0),buff.at(1),buff.at(2)))
        {
            QMessageBox::warning(this,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("插入数据失败,请确认数据库未被其他程序占用及数据填写完整"));
            return false;
        }

        //当发送数据,读取数据,插入数据到数据库中均正常时,开始绘图并自动调整坐标轴比例
        lowKey.append(buff.at(0));
        lowValue.append(buff.at(1));
        QCPGraph *graph = ui->qcustomplot->graph(0);
        graph->addData(buff.at(0),buff.at(1));
        graph->rescaleAxes();
        ui->qcustomplot->replot();
        buff.clear();
    }

    //打开继电器
    if(!m_pKCV600->OpenRelay())
        return false;

    //反向电压自动扫描
    for(double voltage=0;voltage<=minValue+0.03;voltage+=stepValue)
    {
        QCPGraph *graph = ui->qcustomplot->graph(0);
        if(voltage == 0)
        {
            QSqlRecord record = m_pAutoScanModel->record(0);
            highKey.append(0);
            highValue.append(record.value("capacity").toDouble());
            continue;
        }
        QVector<double> buff(20);
        //发送电压指令
        if(!m_pDP700->SetVoltage(voltage))
            return false;
        General::Wait(2);
        //读取数据,若读取失败则数据为空,并跳出该次循环
        buff = m_pKCV600->GetCurrentValue();
        if(buff.empty())
            break;
        //插入数据,若插入失败则跳出当前循环
        if(!insertData(m_pAutoScanModel,buff.at(0)*-1,buff.at(1),buff.at(2)))
        {
            QMessageBox::warning(this,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("插入数据失败,请检查是否正确填写数据信息"));
            return false;
        }

        //当发送数据,读取数据,插入数据到数据库中均正常时,开始绘图并自动调整坐标轴比例
        highKey.append(buff.at(0)*-1);
        highValue.append(buff.at(1));
        graph->addData(buff.at(0)*-1,buff.at(1));
        graph->rescaleAxes();
        ui->qcustomplot->replot();
        buff.clear();
    }

    if(!m_pKCV600->CloseRelay())
        return false;
    else
        m_pDP700->SetVoltage(0);
/*********************************************************************************************************************/


/******************************************坐标轴显示处理***************************************************************/
    //第一步:获取正向电容的最大值和反向电容的最大值,将其设为y轴的显示范围,并确定缩放比例
    ui->qcustomplot->graph(0)->clearData();
    double lowMaxValue = General::QVectorDoubleMax(lowValue);
    double highMaxValue = General::QVectorDoubleMax(highValue);
    double scale = lowMaxValue/highMaxValue;
    //第二步:确定y轴的显示范围为正向电容的2.1倍,确定tickstep为正向电容最大值的1/4
    ui->qcustomplot->yAxis->setRange(0,lowMaxValue*2.1);
    ui->qcustomplot->yAxis->setAutoSubTicks(false);
    ui->qcustomplot->yAxis->setAutoTickStep(false);
    ui->qcustomplot->yAxis->setTickStep(lowMaxValue/4);
    ui->qcustomplot->yAxis->setSubTickCount(4);
    ui->qcustomplot->xAxis->setRange(minValue,maxValue);
    ui->qcustomplot->yAxis->setAutoTickLabels(false);
    //第三步:修改y轴的label
    QVector<QString> labels;
    labels << "" << QString::number((int)(lowMaxValue/4)) << QString::number((int)(lowMaxValue/2)) <<QString::number((int)(lowMaxValue*3/4)) <<QString::number((int)lowMaxValue)<<QString::number((int)(highMaxValue/4))<<QString::number((int)(highMaxValue/2))<<QString::number((int)(highMaxValue*3/4))<<QString::number((int)highMaxValue);
    //第四步:将反向电压的数据按缩放比例进行压缩
    for(auto ivec = highValue.begin()+1;ivec!= highValue.end();ivec++)
        *ivec = *ivec*scale+lowMaxValue;
    ui->qcustomplot->graph(0)->addData(lowKey,lowValue);
    ui->qcustomplot->graph(0)->addData(highKey,highValue);
    ui->qcustomplot->yAxis->setOffset(-(ui->qcustomplot->width()/2-14));
    ui->qcustomplot->replot();
    /*****************************************************************************************************************/
    QMessageBox::information(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("扫描完毕"));

    return true;
}

//void MainWindow::resizeEvent(QResizeEvent *event)
//{
//    if(event->type() == QEvent::Resize)
//    {
//        ui->qcustomplot->yAxis->setOffset(-(ui->qcustomplot->width()/2-15));
//        ui->qcustomplot->replot();
//    }

//}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值