#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include "base/HGBase.h" #include "huagao/brand.h" #include "huagao/hgscanner_error.h" #include "HGUIGlobal.h" #include "app_cfg.h" #include "dialog_login.h" #include "dialog_changepwd.h" #include "dialog_accountmanage.h" #include "dialog_uploadcfgfile.h" #include "dialog_inputserialnum.h" #include "dialog_hgmessagebox.h" MainWindow::MainWindow(const QString &accountName, Dialog_logIn::DeviceType deviceType, const QString &deviceConfig, Dialog_logIn::LogInType loginType, QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_lock(nullptr) , m_testingRef(0) , m_accountName(accountName) , m_deviceType(deviceType) , m_deviceConfig(deviceConfig) , m_loginType(loginType) , m_formBurnMode(nullptr) , m_top_splitter(nullptr) , m_bot_splitter(nullptr) , m_isLogOut(false) , m_burnDlg(nullptr) , m_burnForm(nullptr) , m_inputSnDlg(nullptr) , m_tmpDevType("") { ui->setupUi(this); ui->act_newDevice->setVisible(false); ui->act_close->setVisible(false); ui->act_manage->setVisible(false); ui->act_export->setVisible(false); ui->act_upload->setVisible(false); ui->act_openLog->setVisible(false); if (m_accountName == "admin") ui->act_openLog->setVisible(true); ui->act_refreshDevice->setShortcut(QKeySequence("F5")); QFont ft; ft.setPointSize(15); this->setFont(ft); setWindowIcon(QIcon(":image/image_rsc/logo/logo.ico")); HGBase_CreateLock(&m_lock); connect(this, SIGNAL(sane_dev_arrive(QString)), this, SLOT(on_sane_dev_arrive(QString)), Qt::QueuedConnection); connect(this, SIGNAL(sane_dev_remove(QString)), this, SLOT(on_sane_dev_remove(QString)), Qt::QueuedConnection); connect(this, &MainWindow::sane_create_interface, this, &MainWindow::on_sane_create_interface, Qt::QueuedConnection); if ((m_deviceType == Dialog_logIn::DeviceType_G300 || m_deviceType == Dialog_logIn::DeviceType_G400) && m_loginType == Dialog_logIn::LogInType_Station1) { m_formBurnMode = new Form_BurnMode(this, this); QVBoxLayout *vLayout = new QVBoxLayout; vLayout->addWidget(m_formBurnMode); this->centralWidget()->setLayout(vLayout); show(); } else { m_top_splitter = new QSplitter(Qt::Horizontal); m_bot_splitter = new QSplitter(Qt::Horizontal); m_top_splitter->setMinimumWidth(600); m_bot_splitter->setMinimumWidth(600); m_top_splitter->setChildrenCollapsible(false); m_bot_splitter->setChildrenCollapsible(false); QVBoxLayout *vLayout = new QVBoxLayout; vLayout->addWidget(m_top_splitter); vLayout->addWidget(m_bot_splitter); vLayout->setStretch(0, 0); vLayout->setStretch(1, 0); this->centralWidget()->setLayout(vLayout); showMaximized(); } if (m_deviceType == Dialog_logIn::DeviceType_G300 || m_deviceType == Dialog_logIn::DeviceType_G400) { if (m_loginType == Dialog_logIn::LogInType_Station1) setWindowTitle(tr("production tool-burn station")); else if (m_loginType == Dialog_logIn::LogInType_Station2) setWindowTitle(tr("production tool-init test station")); else if (m_loginType == Dialog_logIn::LogInType_Station3) setWindowTitle(tr("production tool-image test station")); } else { if (m_loginType == Dialog_logIn::LogInType_Station1) setWindowTitle(tr("production tool-init test station")); else if (m_loginType == Dialog_logIn::LogInType_Station2) setWindowTitle(tr("production tool-image test station")); else if (m_loginType == Dialog_logIn::LogInType_Station3) setWindowTitle(tr("production tool-finish test station")); } SANE_Int v = 0; sane_init_ex(&v, sane_ex_callback, this); } MainWindow::~MainWindow() { //HGBase_EnterLock(m_lock); if (m_top_splitter != nullptr) { while (0 != m_top_splitter->count()) { Form_mainInterface * mainInterface = (Form_mainInterface *)m_top_splitter->widget(0); delete mainInterface; } } if (m_top_splitter != nullptr) { while (0 != m_bot_splitter->count()) { Form_mainInterface * mainInterface = (Form_mainInterface *)m_bot_splitter->widget(0); delete mainInterface; } } //HGBase_LeaveLock(m_lock); sane_exit(); delete m_formBurnMode; m_formBurnMode = nullptr; HGBase_DestroyLock(m_lock); m_lock = nullptr; delete ui; } bool MainWindow::isExitApp() { return !m_isLogOut; } void MainWindow::updateSplitter() { if (0 != m_top_splitter->count() && 0 == m_bot_splitter->count()) { reinterpret_cast(this->centralWidget()->layout())->setStretch(0, 1); reinterpret_cast(this->centralWidget()->layout())->setStretch(1, 0); } else if (0 == m_top_splitter->count() && 0 != m_bot_splitter->count()) { reinterpret_cast(this->centralWidget()->layout())->setStretch(0, 0); reinterpret_cast(this->centralWidget()->layout())->setStretch(1, 1); } else if (0 != m_top_splitter->count() && 0 != m_bot_splitter->count()) { reinterpret_cast(this->centralWidget()->layout())->setStretch(0, 1); reinterpret_cast(this->centralWidget()->layout())->setStretch(1, 1); } else { reinterpret_cast(this->centralWidget()->layout())->setStretch(0, 0); reinterpret_cast(this->centralWidget()->layout())->setStretch(1, 0); } } Form_mainInterface* MainWindow::FindInterface(const QString &sn) { for (int i = 0; i < m_top_splitter->count(); ++i) { Form_mainInterface * mainInterface = (Form_mainInterface *)m_top_splitter->widget(i); if (sn == mainInterface->getSn()) { return mainInterface; } } for (int i = 0; i < m_bot_splitter->count(); ++i) { Form_mainInterface * mainInterface = (Form_mainInterface *)m_bot_splitter->widget(i); if (sn == mainInterface->getSn()) { return mainInterface; } } return nullptr; } Form_mainInterface *MainWindow::FindInterfaceByDevName(const QString &devName) { for (int i = 0; i < m_top_splitter->count(); ++i) { Form_mainInterface * mainInterface = (Form_mainInterface *)m_top_splitter->widget(i); if (devName == mainInterface->getDevName()) { return mainInterface; } } for (int i = 0; i < m_bot_splitter->count(); ++i) { Form_mainInterface * mainInterface = (Form_mainInterface *)m_bot_splitter->widget(i); if (devName == mainInterface->getDevName()) { return mainInterface; } } return nullptr; } Form_mainInterface* MainWindow::FindInterface(SANE_Handle handle) { Form_mainInterface *form = nullptr; for (int i = 0; i < m_top_splitter->count(); ++i) { Form_mainInterface * mainInterface = (Form_mainInterface *)m_top_splitter->widget(i); if (handle == mainInterface->getDevHandle()) { form = mainInterface; break; } } if (nullptr == form) { for (int i = 0; i < m_bot_splitter->count(); ++i) { Form_mainInterface * mainInterface = (Form_mainInterface *)m_bot_splitter->widget(i); if (handle == mainInterface->getDevHandle()) { form = mainInterface; break; } } } return form; } bool MainWindow::AddInterface(Form_mainInterface * mainInterface) { assert(nullptr != mainInterface); //HGBase_EnterLock(m_lock); bool ret = false; if (m_top_splitter->count() < 2) { m_top_splitter->addWidget(mainInterface); ret = true; } else if (m_bot_splitter->count() < 2) { m_bot_splitter->addWidget(mainInterface); ret = true; } //HGBase_LeaveLock(m_lock); updateSplitter(); return ret; } bool MainWindow::RemoveInterface(Form_mainInterface * mainInterface) { assert(nullptr != mainInterface); //HGBase_EnterLock(m_lock); delete mainInterface; mainInterface = nullptr; m_testingRef = 0; updateActionStatus(true); //HGBase_LeaveLock(m_lock); updateSplitter(); return true; } void MainWindow::updateActionStatus(bool enable) { ui->act_refreshDevice->setEnabled(enable); ui->act_close->setEnabled(enable); ui->act_manage->setEnabled(enable); ui->act_logOut->setEnabled(enable); ui->act_changePwd->setEnabled(enable); ui->act_export->setEnabled(enable); ui->act_upload->setEnabled(enable); } void MainWindow::addTestingRef() { ++m_testingRef; updateActionStatus(false); } void MainWindow::releaseTesting() { --m_testingRef; if (0 == m_testingRef) { updateActionStatus(true); } } void MainWindow::initTestingRef() { m_testingRef = 0; updateActionStatus(true); } bool MainWindow::isTesting() { return (m_testingRef != 0); } void MainWindow::closeEvent(QCloseEvent *event) { // if (isTesting()) // { // QMessageBox::information(this, tr("tips"), tr("Is testing, do not close!")); // event->ignore(); // } } int MainWindow::sane_ex_callback(SANE_Handle hdev, int code, void *data, unsigned int *len, void *param) { MainWindow* p = (MainWindow*)param; if (SANE_EVENT_DEVICE_ARRIVED == code) { SANE_Device_Ex* sane_dev = (SANE_Device_Ex*)data; emit p->sane_dev_arrive(sane_dev->name); } else if (SANE_EVENT_DEVICE_LEFT == code) { SANE_Device* sane_dev = (SANE_Device*)data; emit p->sane_dev_remove(sane_dev->name); } else if (SANE_EVENT_IMAGE_OK == code) { if (nullptr == p->m_formBurnMode) { Form_mainInterface* mainInterface = p->FindInterface(hdev); if (nullptr != mainInterface) { if (nullptr != mainInterface->getScanner() && nullptr != mainInterface->getScanner()->cb_) mainInterface->getScanner()->cb_(hdev, code, data, len, mainInterface->getScanner()); } } } else if (code == SANE_EVENT_SCAN_FINISHED || code == SANE_EVENT_ERROR || code == SANE_EVENT_STATUS) { SaneParams *saneParams = new SaneParams; saneParams->hdev = hdev; saneParams->code = code; char *content = new char[strlen((char*)data) + 1]; memcpy(content, data, strlen((char*)data) + 1); saneParams->data = content; saneParams->len = *len; if (code == SANE_EVENT_SCAN_FINISHED) { emit p->sane_create_interface(saneParams); } } return 0; } std::vector MainWindow::getDevices() { std::vector name; const SANE_Device** devs_list; SANE_Status status = SANE_STATUS_GOOD; if ((status = sane_get_devices(&devs_list, SANE_TRUE)) != SANE_STATUS_GOOD) { name.push_back(""); return name; } name.clear(); while (*devs_list != NULL) { name.push_back((*devs_list)->name); devs_list++; } return name; } QString MainWindow::getDevSn(SANE_Handle handle) { std::string serialNum; unsigned int serialNumLen = 0; if(SANE_STATUS_NO_MEM == sane_io_control(handle, IO_CTRL_CODE_GET_SERIAL, nullptr, &serialNumLen) && serialNumLen) { serialNum.resize(serialNumLen); sane_io_control(handle, IO_CTRL_CODE_GET_SERIAL, &serialNum[0], &serialNumLen); } return QString::fromStdString(serialNum.c_str()); } QString MainWindow::getDevType(SANE_Handle handle) { std::string firmWareNum; unsigned int firmWareNumLen = 0; if(SANE_STATUS_NO_MEM == sane_io_control(handle, IO_CTRL_CODE_GET_HARDWARE_VERSION, nullptr, &firmWareNumLen) && firmWareNumLen) { firmWareNum.resize(firmWareNumLen); sane_io_control(handle, IO_CTRL_CODE_GET_HARDWARE_VERSION, &firmWareNum[0], &firmWareNumLen); } QString str = QString::fromStdString(firmWareNum.c_str()); return str.left(2) + "00"; } QString MainWindow::getDevFwNum(SANE_Handle handle) { std::string firmWareNum; unsigned int firmWareNumLen = 0; if(SANE_STATUS_NO_MEM == sane_io_control(handle, IO_CTRL_CODE_GET_HARDWARE_VERSION, nullptr, &firmWareNumLen) && firmWareNumLen) { firmWareNum.resize(firmWareNumLen); sane_io_control(handle, IO_CTRL_CODE_GET_HARDWARE_VERSION, &firmWareNum[0], &firmWareNumLen); } return QString::fromStdString(firmWareNum.c_str()); } void MainWindow::on_sane_dev_arrive(QString devName) { } void MainWindow::on_sane_dev_remove(QString devName) { if (nullptr != m_formBurnMode) { m_formBurnMode->deleteDevice(devName); } else { if (nullptr != m_burnForm) { m_burnForm->deleteDevice(devName); } if (nullptr != m_inputSnDlg) { m_inputSnDlg->Invalid(devName); } Form_mainInterface * mainInterface = FindInterfaceByDevName(devName); if (mainInterface != nullptr) { mainInterface->setDevDisconnect(); if (!mainInterface->isTesting()) { RemoveInterface(mainInterface); } } } } void MainWindow::on_sane_create_interface(SaneParams *saneParams) { if (nullptr == m_formBurnMode) { Form_mainInterface* mainInterface = FindInterface(saneParams->hdev); if (nullptr != mainInterface) { if (nullptr != mainInterface->getScanner() && nullptr != mainInterface->getScanner()->cb_) { QString info = QString::fromStdString((char*)saneParams->data); if (info == QString(STATU_DESC_SCANNER_ERR_IMAGE_CORRECTION_FINISHED)) { int type = 0; func_test_correct(true, &type); if (type > 0) { mainInterface->checkDynamicCorrectItem(true, type); } } else //if (info == QString(STATU_DESC_SCANNER_ERR_IMAGE_CORRECTION_FAIL)) { int type = 0; func_test_correct(false, &type); if (type > 0) { mainInterface->checkDynamicCorrectItem(false, type); } } func_test_distortion(); mainInterface->getScanner()->cb_(saneParams->hdev, saneParams->code, saneParams->data, &saneParams->len, mainInterface->getScanner()); } } } delete[] (char*)saneParams->data; delete saneParams; saneParams = nullptr; } void MainWindow::on_burnmode_refresh_clicked() { if (nullptr != m_burnForm) { std::vector name; name = getDevices(); m_burnForm->clearDevice(); for (int i = 0; i < name.size(); ++i) { SANE_Handle devHandle = nullptr; sane_open(name[i].c_str(), &devHandle); if (nullptr != devHandle) { DeviceManager *devManager = new DeviceManager(this, m_burnForm, devHandle, m_deviceConfig, name[i].c_str(), getDevSn(devHandle)); m_burnForm->addDevice(devManager); } } } } void MainWindow::on_burnmode_ok_clicked() { if (nullptr != m_burnDlg) m_burnDlg->accept(); } void MainWindow::on_act_manage_triggered() { Dialog_accountManage dlg(this); dlg.exec(); } void MainWindow::on_act_changePwd_triggered() { Dialog_changePwd dlg(m_accountName, this); dlg.exec(); } void MainWindow::on_act_logOut_triggered() { if (isTesting()) { QMessageBox::information(this, tr("tips"), tr("Is testing, do not close!")); return; } m_isLogOut = true; close(); } void MainWindow::on_act_close_triggered() { if (isTesting()) { QMessageBox::information(this, tr("tips"), tr("Is testing, do not close!")); return; } close(); } void MainWindow::on_act_upload_triggered() { Dialog_uploadCfgFile dlg(this); dlg.exec(); } void MainWindow::on_act_refreshDevice_triggered() { std::vector name; name = getDevices(); if (nullptr != m_formBurnMode) { m_formBurnMode->clearDevice(); for (int i = 0; i < name.size(); ++i) { SANE_Handle devHandle = nullptr; sane_open(name[i].c_str(), &devHandle); if (nullptr != devHandle) { DeviceManager *devManager = new DeviceManager(this, m_formBurnMode, devHandle, m_deviceConfig, name[i].c_str(), getDevSn(devHandle)); m_formBurnMode->addDevice(devManager); } } return; } if (m_top_splitter->count() > 0 || m_bot_splitter->count() > 0) { return; } // G100和G200的初检工位 if (m_loginType == Dialog_logIn::LogInType_Station1) { m_burnDlg = new QDialog(this); m_burnDlg->setWindowTitle(tr("burn mode")); m_burnDlg->setFixedSize(800, 600); m_burnDlg->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint); QFont ft; ft.setPointSize(15); m_burnDlg->setFont(ft); QVBoxLayout *vLayout = new QVBoxLayout(m_burnDlg); m_burnForm = new Form_BurnMode(this, m_burnDlg); vLayout->addWidget(m_burnForm); QHBoxLayout *hLayout = new QHBoxLayout(m_burnDlg); hLayout->addStretch(); QPushButton *refreshBtn = new QPushButton(tr("Refresh(F5)")); refreshBtn->setFixedSize(186, 35); refreshBtn->setShortcut(QKeySequence("F5")); connect(refreshBtn, &QPushButton::clicked, this, &MainWindow::on_burnmode_refresh_clicked); QPushButton *okBtn = new QPushButton(tr("Start Init Test")); okBtn->setFixedSize(186, 35); connect(okBtn, &QPushButton::clicked, this, &MainWindow::on_burnmode_ok_clicked); hLayout->addWidget(refreshBtn); hLayout->addWidget(okBtn); vLayout->addLayout(hLayout); m_burnDlg->setLayout(vLayout); for (int i = 0; i < name.size(); ++i) { SANE_Handle devHandle = nullptr; sane_open(name[i].c_str(), &devHandle); if (nullptr != devHandle) { DeviceManager *devManager = new DeviceManager(this, m_burnForm, devHandle, m_deviceConfig, name[i].c_str(), getDevSn(devHandle)); m_burnForm->addDevice(devManager); } } bool ok = true; if (!m_burnDlg->exec()) ok = false; delete m_burnForm; m_burnForm = nullptr; delete m_burnDlg; m_burnDlg = nullptr; if (!ok) { return; } } for (int i = 0; i < name.size(); ++i) { SANE_Handle devHandle = nullptr; sane_open(name[i].c_str(), &devHandle); if (nullptr != devHandle) { bool canSetSN = false; if (m_deviceType == Dialog_logIn::DeviceType_G300 || m_deviceType == Dialog_logIn::DeviceType_G400) { if (m_loginType == Dialog_logIn::LogInType_Station2) canSetSN = true; } else { if (m_loginType == Dialog_logIn::LogInType_Station1) canSetSN = true; } m_inputSnDlg = new Dialog_InputSerialNum(this, name[i].c_str(), m_deviceConfig, m_accountName, devHandle, getDevSn(devHandle), getDevType(devHandle), canSetSN); if (m_inputSnDlg->exec()) { QString logInTypeStr; if (m_deviceType == Dialog_logIn::DeviceType_G300 || m_deviceType == Dialog_logIn::DeviceType_G400) { if (m_loginType == Dialog_logIn::LogInType_Station1) logInTypeStr = QStringLiteral("烧录工位"); else if (m_loginType == Dialog_logIn::LogInType_Station2) logInTypeStr = QStringLiteral("初检工位"); else if (m_loginType == Dialog_logIn::LogInType_Station3) logInTypeStr = QStringLiteral("图像测试工位"); } else { if (m_loginType == Dialog_logIn::LogInType_Station1) logInTypeStr = QStringLiteral("初检工位"); else if (m_loginType == Dialog_logIn::LogInType_Station2) logInTypeStr = QStringLiteral("图像测试工位"); else if (m_loginType == Dialog_logIn::LogInType_Station3) logInTypeStr = QStringLiteral("成品检验工位"); } Form_mainInterface *mainInterface = new Form_mainInterface(this, m_deviceConfig, m_accountName, devHandle, m_inputSnDlg->GetSn(), m_inputSnDlg->GetDevType(), name[i].c_str(), getDevFwNum(devHandle), logInTypeStr); if (!AddInterface(mainInterface)) { QMessageBox::information(this, tr("tips"), tr("cannot create more table")); delete mainInterface; } m_tmpDevType = getDevType(devHandle); } else { sane_close(devHandle); } delete m_inputSnDlg; m_inputSnDlg = nullptr; } } } void MainWindow::on_act_openLog_triggered() { HGChar logPath[512]; HGBase_GetDocumentsPath(logPath, 512); HGChar procName[512]; HGBase_GetProcessName(procName, 512); strcat(logPath, procName); strcat(logPath, "/Test_Log/"); QString defaultFileName = logPath + m_accountName + ".log"; QString fileName = getCfgValue(m_tmpDevType.toStdString().c_str(), "logPath", defaultFileName); QFileInfo fileInfo(getStdFileName(fileName)); auto pathDir = fileInfo.path(); QString strFilePath = "file:///" + pathDir; QDesktopServices::openUrl(QUrl(strFilePath)); } static bool isLeap(int year) { if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) return true; return false; } static int getDays(int year, int month) { if (2 == month) { if (isLeap(year)) return 29; else return 28; } if (4 == month || 6 == month || 9 == month || 11 == month) { return 30; } return 31; } static int getPatchVersion() { const char *buildDate = __DATE__; QDate date = QLocale(QLocale::English).toDate(QString(buildDate).replace(" ", " 0"), "MMM dd yyyy"); int year = date.year(); int day = date.day(); int month = date.month(); int ret = year % 100; ret *= 1000; for (int i = 1; i < month; ++i) { ret += getDays(year, i); } ret += day; return ret; } void MainWindow::on_act_about_triggered() { int mainVersion = VERSION_MAIN; int subVersion = VERSION_SUB; int buildVersion = 10000; int patchVersion = 0; patchVersion = getPatchVersion(); HGChar version[32] = { 0 }; sprintf(version, "%d.%d.%d.%d", mainVersion, subVersion, buildVersion, patchVersion); QString title = tr("about %1").arg(tr("Production tool")); QString content; content += tr("

DriverVer: %1

").arg(QString::fromLocal8Bit(version)); QMessageBox::about(this, title, content); }