#include "form_maininterface.h" #include "ui_form_maininterface.h" #include #include #include #include "base/HGBase.h" #include "imgfmt/HGImgFmt.h" #include "HGUIGlobal.h" #include "HGString.h" #include "form_texttips.h" #include "test_base.h" #include "hgscanner.h" #include "mainwindow.h" #include "dialog_excepdesc.h" #include "huagao/hgscanner_error.h" Form_mainInterface::Form_mainInterface(class MainWindow *mainwnd, SANE_Handle handle, HGPdtToolDbUserMgr pdtToolDbuserMgr, HGPdtToolDbDevice pdtToolDbDevice, const QString &serialNum, const QString &devType, const QString &devName, const QString &devFwNum) : QWidget(mainwnd) , ui(new Ui::Form_mainInterface) , m_mainwnd(mainwnd) , m_hg(nullptr) , m_handle(handle) , m_disconnect(false) , m_pdtToolDbuserMgr(pdtToolDbuserMgr) , m_pdtToolDbDevice(pdtToolDbDevice) , m_serialNum(serialNum) , m_devType(devType) , m_devName(devName) , m_devFwNum(devFwNum) , m_isTesting(false) { ui->setupUi(this); QFont ft; ft.setPointSize(15); this->setFont(ft); connect(this, SIGNAL(testResult(QString)), this, SLOT(on_testResult(QString)), Qt::QueuedConnection); connect(this, SIGNAL(testResultImg(void*)), this, SLOT(on_testResultImg(void*)), Qt::QueuedConnection); connect(this, SIGNAL(testDistortion(QString,void*)), this, SLOT(on_testDistortion(QString,void*)), Qt::QueuedConnection); connect(this, SIGNAL(testCorrectInfo(QString)), this, SLOT(on_testCorrectInfo(QString)), Qt::QueuedConnection); QString str; if (m_handle) { str = tr("Device has connected"); m_hg = new hgscanner(this, nullptr, m_handle); } else str = tr("Device hasnot connected"); ui->label_sn->setText(tr("Device type:") + m_devType + tr(" SerialNum:") + m_serialNum); ui->label_devStatus->setText(tr(" connect status:") + str); setMinimumWidth(500); ui->tableWidget->setMinimumWidth(400); ui->tableWidget->setMaximumWidth(400); m_textTips = new Form_textTips(); m_view = new HGImgView(); ui->stackedWidget->addWidget(m_textTips); ui->stackedWidget->addWidget(m_view); ui->pbtn_showImg->setToolTip(tr("load last image which uploaded last time")); initTableWidgetUi(); if(!m_devType.isEmpty()) HGPdtToolDb_SetDeviceType(m_pdtToolDbDevice, m_devType.toStdString().c_str()); if(!m_devFwNum.isEmpty()) HGPdtToolDb_SetDeviceVersion(m_pdtToolDbDevice, m_devFwNum.toStdString().c_str()); if(!m_devName.isEmpty()) HGPdtToolDb_SetDeviceName(m_pdtToolDbDevice, m_devName.toStdString().c_str()); } Form_mainInterface::~Form_mainInterface() { delete m_hg; m_hg = nullptr; sane_close(m_handle); m_handle = nullptr; HGPdtToolDb_CloseDevice(m_pdtToolDbDevice); m_pdtToolDbDevice = nullptr; delete ui; } bool Form_mainInterface::isTesting() { return m_isTesting; } QString Form_mainInterface::getSn() { return m_serialNum; } QString Form_mainInterface::getDevName() { return m_devName; } SANE_Handle Form_mainInterface::getDevHandle() { return m_handle; } hgscanner *Form_mainInterface::getScanner() { return m_hg; } void Form_mainInterface::setDevDisconnect() { QString str = tr("Device has disconnected"); ui->label_devStatus->setText(tr(" connect status:") + str); m_disconnect = true; } void Form_mainInterface::paintEvent(QPaintEvent *event) { (void)event; QPainter p(this); p.setPen(QColor("gray")); p.drawRect(0, 0, width() -1, height() -1); } void Form_mainInterface::on_testResult(QString text) { if (!text.isEmpty()) QMessageBox::information(this, tr("tips"), text); updateUiEnabled(true); m_isTesting = false; m_mainwnd->releaseTesting(); if (m_disconnect) { m_mainwnd->RemoveInterface(this); } } void Form_mainInterface::on_testResultImg(void *img) { HGImage image = (HGImage)img; ui->stackedWidget->setCurrentWidget(m_view); m_view->addImage(image); ui->pbtn_leftRotate->setVisible(true); ui->pbtn_rightRotate->setVisible(true); HGBase_DestroyImage(image); } void Form_mainInterface::on_testDistortion(QString name, void *data) { QString info = QString::fromStdWString((const wchar_t*)data); QMessageBox msg(QMessageBox::Question, name, info, QMessageBox::Yes | QMessageBox::No, this); msg.setButtonText(QMessageBox::Yes, tr("yes")); msg.setButtonText(QMessageBox::No, tr("no")); msg.exec(); if (msg.clickedButton() == msg.button(QMessageBox::Yes)) { // func_test_go(HGPDTTOOLDB_NAME_SINGLE_PAGE_TEST_1, L"null", m_hg); } else { updateUiEnabled(true); m_isTesting = false; m_mainwnd->releaseTesting(); if (m_disconnect) { m_mainwnd->RemoveInterface(this); } } } void Form_mainInterface::on_testCorrectInfo(QString info) { m_textTips->addContent(info); } void Form_mainInterface::on_pbtn_preStep_clicked() { int row = ui->tableWidget->currentRow(); if (row > 0) { ui->tableWidget->selectRow(row - 1); } } void Form_mainInterface::on_pbtn_nextStep_clicked() { int row = ui->tableWidget->currentRow(); if (row < ui->tableWidget->rowCount() - 1) { ui->tableWidget->selectRow(row + 1); } } void Form_mainInterface::on_pbtn_fail_clicked() { int row = ui->tableWidget->currentRow(); QTableWidgetItem *item = ui->tableWidget->item(row, 1); QString title = item->text(); Dialog_ExcepDesc dlg(this); if (dlg.exec()) { ui->tableWidget->setItem(row, 2, new QTableWidgetItem(getItemStatusStr(HGPDTTOOLDB_ENTRYSTATUS_NOTPASS))); ui->tableWidget->item(row, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui->tableWidget->item(row, 2)->setBackgroundColor(QColor(255,0,0)); QString str = dlg.getExcepDesc(); std::string name = m_map_title_name.value(title).name.toStdString(); HGPdtToolDb_SetDeviceEntryStatus(m_pdtToolDbDevice, name.c_str(), HGPDTTOOLDB_ENTRYSTATUS_NOTPASS); HGPdtToolDb_SetDeviceEntryExcepDesc(m_pdtToolDbDevice, name.c_str(), str.toStdString().c_str()); QTableWidgetItem *item2 = ui->tableWidget->item(row, 2); item2->setToolTip(str); } } void Form_mainInterface::on_pbtn_pass_clicked() { int row = ui->tableWidget->currentRow(); QTableWidgetItem *item = ui->tableWidget->item(row, 1); QString title = item->text(); std::string name = m_map_title_name.value(title).name.toStdString(); HGPdtToolDb_SetDeviceEntryStatus(m_pdtToolDbDevice, name.c_str(), HGPDTTOOLDB_ENTRYSTATUS_PASS); HGPdtToolDb_SetDeviceEntryExcepDesc(m_pdtToolDbDevice, name.c_str(), ""); ui->tableWidget->setItem(row, 2, new QTableWidgetItem(getItemStatusStr(HGPDTTOOLDB_ENTRYSTATUS_PASS))); ui->tableWidget->item(row, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui->tableWidget->item(row, 2)->setBackgroundColor(QColor(0,255,0)); QTableWidgetItem *item2 = ui->tableWidget->item(row, 2); item2->setToolTip(""); HGImage img = nullptr; m_view->getImage(&img); if (img != nullptr) { QString fileName = getCacheFileName(); HGResult ret = HGImgFmt_SaveImage(img, 0, 0, getStdString(fileName).c_str()); if(ret == HGBASE_ERR_OK) { int row = ui->tableWidget->currentRow(); QTableWidgetItem *item = ui->tableWidget->item(row, 1); QString title = item->text(); std::string name = m_map_title_name.value(title).name.toStdString(); HGPdtToolDb_ClearDeviceEntryImage(m_pdtToolDbDevice, name.c_str()); HGPdtToolDb_UploadDeviceEntryImage(m_pdtToolDbDevice, name.c_str(), m_mainwnd->m_ftpHost.toStdString().c_str(), m_mainwnd->m_ftpPort, fileName.toStdString().c_str()); QFile file(fileName); file.remove(); } } on_pbtn_nextStep_clicked(); } void Form_mainInterface::on_pbtn_stop_clicked() { m_mainwnd->RemoveInterface(this); } void Form_mainInterface::initTableWidgetUi() { ui->tableWidget->resizeRowsToContents(); ui->tableWidget->setColumnCount(3); ui->tableWidget->horizontalHeader()->setDefaultSectionSize(200); ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); QStringList header; header<< tr("ID") << tr("Test items") << tr("status"); ui->tableWidget->setHorizontalHeaderLabels(header); QFont font = ui->tableWidget->horizontalHeader()->font(); font.setBold(true); ui->tableWidget->horizontalHeader()->setFont(font); ui->tableWidget->horizontalHeader()->setStretchLastSection(true); ui->tableWidget->verticalHeader()->setDefaultSectionSize(10); ui->tableWidget->setFrameShape(QFrame::NoFrame); ui->tableWidget->setShowGrid(true); ui->tableWidget->verticalHeader()->setVisible(false); ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->tableWidget->horizontalHeader()->setFixedHeight(30); ui->tableWidget->clearContents(); ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); ui->tableWidget->setStyleSheet("selection-background-color:rgb(193,210,240)"); HGChar cfgPath[512] = {0}; HGBase_GetConfigPath(cfgPath, 512); HGBase_CreateDir(cfgPath); strcat(cfgPath, m_devType.toStdString().c_str()); strcat(cfgPath, ".json"); HGPdtToolDb_DownloadFile(m_pdtToolDbuserMgr, m_devType.toStdString().c_str(), cfgPath); AnalysisJson analysisJson(QString::fromStdString(cfgPath)); setGlobalCfg(analysisJson); std::vector list_jsonNode = analysisJson.GetNode(); int count = list_jsonNode.size(); ui->tableWidget->setRowCount(count); for(int i = 0; i < count; ++i) { AnalysisJson::json_node node = list_jsonNode[i]; m_map_title_name.insert(node.title, node); HGPdtToolDb_SetEntryNameCnStr(m_pdtToolDbuserMgr, node.name.toStdString().c_str(), node.title.toStdString().c_str()); ui->tableWidget->setItem(i, 0, new QTableWidgetItem(QString::number(i+1))); ui->tableWidget->item(i, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); ui->tableWidget->setItem(i, 1, new QTableWidgetItem(node.title)); ui->tableWidget->item(i, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); std::string name = node.name.toStdString(); HGUInt status = 0; HGPdtToolDb_GetDeviceEntryStatus(m_pdtToolDbDevice, name.c_str(), &status); QString statusStr = getItemStatusStr(status); ui->tableWidget->setItem(i, 2, new QTableWidgetItem(statusStr)); ui->tableWidget->item(i, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); if (HGPDTTOOLDB_ENTRYSTATUS_NOTPASS == status) { ui->tableWidget->item(i, 2)->setBackgroundColor(QColor(255,0,0)); } else if (HGPDTTOOLDB_ENTRYSTATUS_NOTSUPP == status) { ui->tableWidget->item(i, 2)->setBackgroundColor(QColor(255,255,0)); } else if (HGPDTTOOLDB_ENTRYSTATUS_PASS == status) { ui->tableWidget->item(i, 2)->setBackgroundColor(QColor(0,255,0)); } HGChar desc[512] = {0}; HGPdtToolDb_GetDeviceEntryExcepDesc(m_pdtToolDbDevice, name.c_str(), desc, 512); QTableWidgetItem *item = ui->tableWidget->item(i, 2); item->setToolTip(desc); } ui->tableWidget->selectRow(0); } QString Form_mainInterface::getItemStatusStr(HGUInt status) { static const QString statusStr[] = {tr("not test"), tr("not pass"), tr("not support"), tr("pass") }; return statusStr[status]; } QString Form_mainInterface::getCachePath() { HGChar cachePath[512]; HGBase_GetTmpPath(cachePath, 512); strcat(cachePath, "/Cache/"); return getStdFileName(StdStringToUtf8(cachePath).c_str()); } QString Form_mainInterface::getCacheFileName() { QString cachePath = getCachePath(); HGBase_CreateDir(getStdString(cachePath).c_str()); char uuid[256] = {0}; HGBase_GetUuid(uuid, 256); QString suffix = ".jpg"; QString fileName = getStdFileName(cachePath + uuid + suffix); return fileName; } void Form_mainInterface::updateUiEnabled(bool enable) { ui->tableWidget->setEnabled(enable); ui->pbtn_preStep->setEnabled(enable); ui->pbtn_nextStep->setEnabled(enable); ui->pbtn_pass->setEnabled(enable); ui->pbtn_fail->setEnabled(enable); ui->pbtn_start->setEnabled(enable); ui->pbtn_showImg->setEnabled(enable); ui->pbtn_leftRotate->setEnabled(enable); ui->pbtn_rightRotate->setEnabled(enable); ui->pbtn_stop->setEnabled(enable); } void Form_mainInterface::setGlobalCfg(AnalysisJson analysisJson) { assert(nullptr != m_hg); AnalysisJson::json_global jsonGlobal = analysisJson.GetGlobal(); LPWRITECFG p = new WRITECFG; VIDPID vidpid; vidpid.VID = jsonGlobal.vid_to; vidpid.PID = jsonGlobal.pid_to; int vidpidValue = vidpid.Value; p->cat = WRITE_CAT_VID_PID; p->val = (void*)&vidpidValue; func_test_go(WRITE_CFG_NAME, (const wchar_t*)p, m_hg); m_mainwnd->addTestingRef(); int speedMode = jsonGlobal.speed_mode ; p->cat = WRITE_CAT_SPEED; p->val = (void*)&speedMode; func_test_go(WRITE_CFG_NAME, (const wchar_t*)p, m_hg); m_mainwnd->addTestingRef(); int sleepTime = jsonGlobal.sleep_time; p->cat = WRITE_CAT_SLEEP; p->val = (void*)&sleepTime; func_test_go(WRITE_CFG_NAME, (const wchar_t*)p, m_hg); m_mainwnd->addTestingRef(); const wchar_t* sn = m_serialNum.toStdWString().c_str(); p->cat = WRITE_CAT_SERIALNUM; p->val = (void*)sn; func_test_go(WRITE_CFG_NAME, (const wchar_t*)p, m_hg); m_mainwnd->addTestingRef(); delete p; } void Form_mainInterface::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous) { (void)previous; ui->pbtn_showImg->setVisible(false); ui->pbtn_leftRotate->setVisible(false); ui->pbtn_rightRotate->setVisible(false); int row = current->row(); int count = ui->tableWidget->rowCount(); ui->pbtn_preStep->setEnabled(row != 0); ui->pbtn_nextStep->setEnabled(row != count- 1); QTableWidgetItem *item2 = ui->tableWidget->item(row, 1); QString title = item2->text(); if (nullptr == m_handle) { ui->pbtn_start->setVisible(false); } else { bool man = m_map_title_name.value(title).is_man; ui->pbtn_start->setEnabled(!man); } m_textTips->setViewContent(m_map_title_name.value(title).desc); ui->stackedWidget->setCurrentWidget(m_textTips); m_view->clearImage(); std::string name = m_map_title_name.value(title).name.toStdString(); HGUInt imageCount = 0; HGPdtToolDb_GetDeviceEntryImageCount(m_pdtToolDbDevice, name.c_str(), &imageCount); if (imageCount > 0) { ui->pbtn_showImg->setVisible(true); } } void Form_mainInterface::on_pbtn_start_clicked() { int row = ui->tableWidget->currentRow(); QTableWidgetItem *item = ui->tableWidget->item(row, 1); QString title = item->text(); QString name = m_map_title_name.value(title).name; if (m_hg != nullptr) func_test_go(name.toStdWString().c_str(), L"null", m_hg); updateUiEnabled(false); m_isTesting = true; m_mainwnd->addTestingRef(); } void Form_mainInterface::on_pbtn_showImg_clicked() { QString fileName = getCacheFileName(); int row = ui->tableWidget->currentRow(); QTableWidgetItem *item = ui->tableWidget->item(row, 1); QString title = item->text(); std::string name = m_map_title_name.value(title).name.toStdString(); HGResult ret = HGPdtToolDb_DownloadDeviceEntryImage(m_pdtToolDbDevice, name.c_str(), m_mainwnd->m_ftpHost.toStdString().c_str(), m_mainwnd->m_ftpPort, 0, fileName.toStdString().c_str()); if(ret == HGBASE_ERR_OK) { ui->pbtn_leftRotate->setVisible(true); ui->pbtn_rightRotate->setVisible(true); ui->stackedWidget->setCurrentWidget(m_view); HGImage img = nullptr; HGImgFmt_LoadImage(fileName.toStdString().c_str(), 0, 0, 0, 0, &img); m_view->addImage(img); QFile file(fileName); file.remove(); } else { QMessageBox::information(this, tr("tips"), tr("no image")); } } void Form_mainInterface::on_pbtn_leftRotate_clicked() { m_view->rotateLeft(); } void Form_mainInterface::on_pbtn_rightRotate_clicked() { m_view->rotateRight(); }