#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include "base/HGBase.h" #include "HGUIGlobal.h" #include "dialog_login.h" #include "dialog_changepwd.h" #include "dialog_accountmanage.h" #include "dialog_uploadcfgfile.h" #include "dialog_inputserialnum.h" MainWindow::MainWindow(HGPdtToolDbUserMgr pdtToolDbuserMgr, Dialog_logIn::LogInType loginType, const QString &ftpHost, unsigned short ftpPort, QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , m_pdtToolDbuserMgr(pdtToolDbuserMgr) , m_ftpHost(ftpHost) , m_ftpPort(ftpPort) , m_lock(nullptr) , m_testingRef(0) , m_loginType(loginType) , m_formBurnMode(nullptr) , m_top_splitter(nullptr) , m_bot_splitter(nullptr) , m_isLogOut(false) , m_inputSnDlg(nullptr) { ui->setupUi(this); 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); if (m_loginType == Dialog_logIn::LogInType_Burn) { setWindowTitle(tr("production tool-burn station")); ui->act_newDevice->setVisible(false); ui->act_refreshDevice->setVisible(false); m_formBurnMode = new Form_BurnMode(this, this); QVBoxLayout *vLayout = new QVBoxLayout; vLayout->addWidget(m_formBurnMode); this->centralWidget()->setLayout(vLayout); show(); } else { ui->act_refreshDevice->setVisible(true); 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(); } QTimer *timer = new QTimer(this); timer->start(1000); connect(timer, SIGNAL(timeout()), this, SLOT(on_update_userStatus())); HGUInt userType = 0; HGPdtToolDb_GetUserType(m_pdtToolDbuserMgr, &userType); if (userType == HGPDTTOOLDB_USERTYPE_NORMAL) { ui->act_manage->setVisible(false); ui->act_upload->setVisible(false); } if (m_loginType == Dialog_logIn::LogInType_InitTest) setWindowTitle(tr("production tool-init test station")); else if (m_loginType == Dialog_logIn::LogInType_ImageTest) setWindowTitle(tr("production tool-image test station")); if (m_loginType == Dialog_logIn::LogInType_PressureTest) setWindowTitle(tr("production tool-pressure 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(); HGPdtToolDb_DestroyUserMgr(m_pdtToolDbuserMgr); m_pdtToolDbuserMgr = nullptr; 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; HGBase_LeaveLock(m_lock); updateSplitter(); return true; } void MainWindow::addTestingRef() { ++m_testingRef; ui->act_refreshDevice->setEnabled(false); ui->act_close->setEnabled(false); ui->act_manage->setEnabled(false); ui->act_logOut->setEnabled(false); ui->act_changePwd->setEnabled(false); ui->act_export->setEnabled(false); ui->act_upload->setEnabled(false); } void MainWindow::releaseTesting() { --m_testingRef; if (0 == m_testingRef) { ui->act_refreshDevice->setEnabled(true); ui->act_close->setEnabled(true); ui->act_manage->setEnabled(true); ui->act_logOut->setEnabled(true); ui->act_changePwd->setEnabled(true); ui->act_export->setEnabled(true); ui->act_upload->setEnabled(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); } if (code == SANE_EVENT_IMAGE_OK || code == SANE_EVENT_SCAN_FINISHED || code == SANE_EVENT_ERROR || code == SANE_EVENT_STATUS) { HGBase_EnterLock(p->m_lock); if (p->m_loginType != Dialog_logIn::LogInType_Burn) { 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()); } } HGBase_LeaveLock(p->m_lock); } 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) { if (m_loginType == Dialog_logIn::LogInType_Burn) { SANE_Handle devHandle = nullptr; sane_open(devName.toStdString().c_str(), &devHandle); if (nullptr != devHandle) { DeviceManager *devManager = new DeviceManager(this, m_pdtToolDbuserMgr, m_formBurnMode, devHandle, devName, getDevSn(devHandle), getDevType(devHandle)); m_formBurnMode->addDevice(devManager); } } else { QMessageBox msg(QMessageBox::Question, tr("tips"), tr("Confirm whether the device is reset"), QMessageBox::Yes | QMessageBox::No, this); msg.setButtonText(QMessageBox::Yes, tr("confirm")); msg.setButtonText(QMessageBox::No, tr("no")); msg.exec(); if (msg.clickedButton() == msg.button(QMessageBox::Yes)) { SANE_Handle devHandle = nullptr; sane_open(devName.toStdString().c_str(), &devHandle); if (nullptr != devHandle) { m_inputSnDlg = new Dialog_InputSerialNum(this, devName, m_pdtToolDbuserMgr, devHandle, getDevSn(devHandle), getDevType(devHandle)); if (m_inputSnDlg->exec()) { HGPdtToolDbDevice pdtToolDbDevice = nullptr; HGPdtToolDb_OpenDevice(m_pdtToolDbuserMgr, m_inputSnDlg->GetSn().toStdString().c_str(), &pdtToolDbDevice); Form_mainInterface *mainInterface = new Form_mainInterface(this, m_loginType, devHandle, m_pdtToolDbuserMgr, pdtToolDbDevice, m_inputSnDlg->GetSn(), m_inputSnDlg->GetDevType(), devName, getDevFwNum(devHandle)); if (!AddInterface(mainInterface)) { QMessageBox::information(this, tr("tips"), tr("cannot create more table")); delete mainInterface; } } else { sane_close(devHandle); } delete m_inputSnDlg; m_inputSnDlg = nullptr; } else { QMessageBox::information(this, tr("tips"), tr("Open device failed")); } } } } void MainWindow::on_sane_dev_remove(QString devName) { if (m_loginType == Dialog_logIn::LogInType_Burn) { m_formBurnMode->deleteDevice(devName); } else { 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_update_userStatus() { QString tips; HGUInt userStatus = 0; HGPdtToolDb_GetUserStatus(m_pdtToolDbuserMgr, &userStatus); switch (userStatus) { case HGPDTTOOLDB_USERSTATUS_CONFLICT: tips = tr("Account login elsewhere"); break; case HGPDTTOOLDB_USERSTATUS_REFUSE: tips = tr("You have been forced offline by the administrator account"); break; case HGPDTTOOLDB_USERSTATUS_DBERROR: tips = tr("Data base error"); break; default: break; } if (!tips.isEmpty()) { if (!isTesting()) { QMessageBox::information(this, (tr("tips")), tips); m_isLogOut = true; close(); } } } void MainWindow::on_act_newDevice_triggered() { m_inputSnDlg = new Dialog_InputSerialNum(this, "", nullptr, nullptr, "", ""); if (m_inputSnDlg->exec()) { HGPdtToolDbDevice pdtToolDbDevice = nullptr; HGPdtToolDb_OpenDevice(m_pdtToolDbuserMgr, m_inputSnDlg->GetSn().toStdString().c_str(), &pdtToolDbDevice); Form_mainInterface *mainInterface = new Form_mainInterface(this, m_loginType, nullptr, m_pdtToolDbuserMgr, pdtToolDbDevice, m_inputSnDlg->GetSn(), m_inputSnDlg->GetDevType(), nullptr, nullptr); if (!AddInterface(mainInterface)) { QMessageBox::information(this, tr("tips"), tr("cannot create more table")); delete mainInterface; } } delete m_inputSnDlg; m_inputSnDlg = nullptr; } void MainWindow::on_act_manage_triggered() { Dialog_accountManage dlg(m_pdtToolDbuserMgr, this); dlg.exec(); } void MainWindow::on_act_changePwd_triggered() { Dialog_changePwd dlg(m_pdtToolDbuserMgr, this); dlg.exec(); } void MainWindow::on_act_export_triggered() { QString filename = QFileDialog::getSaveFileName(this, tr("save"), "test.xls", tr("xls(*.xls)")); if (!filename.isEmpty()) { if (HGBASE_ERR_OK == HGPdtToolDb_Export(m_pdtToolDbuserMgr, getStdString(filename).c_str())) { QMessageBox::information(this, tr("tips"), tr("export succeed")); } else { QMessageBox::information(this, tr("tips"), tr("export failed")); } } } 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(); } QString MainWindow::getLogInfo(HGResult ret) { QString str; if (HGPDTTOOLDB_ERR_INVALID_USER == ret) { str = tr("Illegal user"); } else if (HGPDTTOOLDB_ERR_WRONG_PASSWORD == ret) { str = tr("Wrong password"); } else if (HGPDTTOOLDB_ERR_DATABASE == ret) { str = tr("Database error"); } else if (HGPDTTOOLDB_ERR_CONNECT == ret) { str = tr("connect error"); } else { str = tr("Failed"); } return str; } void MainWindow::on_act_upload_triggered() { Dialog_uploadCfgFile dlg(m_pdtToolDbuserMgr, this); dlg.exec(); } void MainWindow::on_act_refreshDevice_triggered() { std::vector name; name = getDevices(); for (int i = 0; i < name.size(); ++i) { SANE_Handle devHandle = nullptr; sane_open(name[i].c_str(), &devHandle); if (nullptr != devHandle) { m_inputSnDlg = new Dialog_InputSerialNum(this, name[i].c_str(), m_pdtToolDbuserMgr, devHandle, getDevSn(devHandle), getDevType(devHandle)); if (m_inputSnDlg->exec()) { HGPdtToolDbDevice pdtToolDbDevice = nullptr; HGPdtToolDb_OpenDevice(m_pdtToolDbuserMgr, m_inputSnDlg->GetSn().toStdString().c_str(), &pdtToolDbDevice); Form_mainInterface *mainInterface = new Form_mainInterface(this, m_loginType, devHandle, m_pdtToolDbuserMgr, pdtToolDbDevice, m_inputSnDlg->GetSn(), m_inputSnDlg->GetDevType(), name[i].c_str(), getDevFwNum(devHandle)); if (!AddInterface(mainInterface)) { QMessageBox::information(this, tr("tips"), tr("cannot create more table")); delete mainInterface; } } else { sane_close(devHandle); } delete m_inputSnDlg; m_inputSnDlg = nullptr; } } }