code_production/app/HGProductionTool/mainwindow.cpp

535 lines
15 KiB
C++
Raw Normal View History

2022-12-14 06:39:22 +00:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
2022-12-22 02:48:06 +00:00
#include <QTimer>
2022-12-14 06:39:22 +00:00
#include "base/HGBase.h"
#include "HGUIGlobal.h"
#include "dialog_login.h"
#include "dialog_changepwd.h"
#include "dialog_accountmanage.h"
2022-12-14 12:28:25 +00:00
#include "dialog_uploadcfgfile.h"
2022-12-16 11:01:30 +00:00
#include "dialog_inputserialnum.h"
2022-12-14 06:39:22 +00:00
2022-12-27 08:25:47 +00:00
MainWindow::MainWindow(HGPdtToolDbUserMgr pdtToolDbuserMgr, const QString &ftpHost, unsigned short ftpPort, QWidget *parent)
2022-12-14 06:39:22 +00:00
: QMainWindow(parent)
, ui(new Ui::MainWindow)
2022-12-22 02:48:06 +00:00
, m_pdtToolDbuserMgr(pdtToolDbuserMgr)
2022-12-27 08:25:47 +00:00
, m_ftpHost(ftpHost)
, m_ftpPort(ftpPort)
2022-12-26 10:38:34 +00:00
, m_lock(nullptr)
2022-12-14 06:39:22 +00:00
, m_top_splitter(nullptr)
, m_bot_splitter(nullptr)
, m_isLogOut(false)
{
ui->setupUi(this);
2022-12-28 06:57:00 +00:00
QFont ft;
ft.setPointSize(15);
this->setFont(ft);
2022-12-14 06:39:22 +00:00
setWindowIcon(QIcon(":image/image_rsc/logo/logo.ico"));
2022-12-26 10:38:34 +00:00
HGBase_CreateLock(&m_lock);
2022-12-15 07:00:12 +00:00
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);
2022-12-14 06:39:22 +00:00
m_top_splitter = new QSplitter(Qt::Horizontal);
m_bot_splitter = new QSplitter(Qt::Horizontal);
2022-12-23 10:32:57 +00:00
m_top_splitter->setMinimumWidth(600);
m_bot_splitter->setMinimumWidth(600);
m_top_splitter->setChildrenCollapsible(false);
m_bot_splitter->setChildrenCollapsible(false);
2022-12-14 06:39:22 +00:00
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->addWidget(m_top_splitter);
vLayout->addWidget(m_bot_splitter);
2022-12-16 11:01:30 +00:00
vLayout->setStretch(0, 0);
2022-12-14 06:39:22 +00:00
vLayout->setStretch(1, 0);
this->centralWidget()->setLayout(vLayout);
2022-12-15 09:27:35 +00:00
2022-12-22 02:48:06 +00:00
QTimer *timer = new QTimer(this);
timer->start(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(on_update_userStatus()));
2022-12-15 09:27:35 +00:00
HGUInt userType = 0;
HGPdtToolDb_GetUserType(m_pdtToolDbuserMgr, &userType);
if (userType == HGPDTTOOLDB_USERTYPE_NORMAL)
2022-12-17 05:48:03 +00:00
{
2022-12-15 09:27:35 +00:00
ui->act_manage->setVisible(false);
2022-12-17 05:48:03 +00:00
ui->act_upload->setVisible(false);
}
2022-12-15 09:27:35 +00:00
HGChar userName[128];
HGPdtToolDb_GetUserName(m_pdtToolDbuserMgr, userName, 128);
setWindowTitle(userName);
2022-12-23 02:42:46 +00:00
showMaximized();
SANE_Int v = 0;
sane_init_ex(&v, sane_ex_callback, this);
2022-12-15 09:27:35 +00:00
}
2022-12-22 02:48:06 +00:00
MainWindow::~MainWindow()
2022-12-15 09:27:35 +00:00
{
2022-12-26 10:38:34 +00:00
HGBase_EnterLock(m_lock);
while (0 != m_top_splitter->count())
2022-12-22 02:48:06 +00:00
{
2022-12-26 10:38:34 +00:00
Form_mainInterface *interface = (Form_mainInterface *)m_top_splitter->widget(0);
2022-12-22 02:48:06 +00:00
delete interface;
}
2022-12-26 10:38:34 +00:00
while (0 != m_bot_splitter->count())
2022-12-22 02:48:06 +00:00
{
2022-12-26 10:38:34 +00:00
Form_mainInterface *interface = (Form_mainInterface *)m_bot_splitter->widget(0);
2022-12-22 02:48:06 +00:00
delete interface;
}
2022-12-26 10:38:34 +00:00
HGBase_LeaveLock(m_lock);
2022-12-22 02:48:06 +00:00
2022-12-23 10:32:57 +00:00
sane_exit();
2022-12-15 09:27:35 +00:00
2022-12-14 06:39:22 +00:00
HGPdtToolDb_DestroyUserMgr(m_pdtToolDbuserMgr);
m_pdtToolDbuserMgr = nullptr;
2022-12-22 02:48:06 +00:00
2022-12-26 10:38:34 +00:00
HGBase_DestroyLock(m_lock);
m_lock = nullptr;
2022-12-22 02:48:06 +00:00
delete ui;
}
2022-12-14 06:39:22 +00:00
bool MainWindow::isExitApp()
{
return !m_isLogOut;
}
2022-12-16 11:01:30 +00:00
void MainWindow::updateSplitter()
{
if (0 != m_top_splitter->count() && 0 == m_bot_splitter->count())
{
reinterpret_cast<QVBoxLayout*>(this->centralWidget()->layout())->setStretch(0, 1);
reinterpret_cast<QVBoxLayout*>(this->centralWidget()->layout())->setStretch(1, 0);
}
else if (0 == m_top_splitter->count() && 0 != m_bot_splitter->count())
{
reinterpret_cast<QVBoxLayout*>(this->centralWidget()->layout())->setStretch(0, 0);
reinterpret_cast<QVBoxLayout*>(this->centralWidget()->layout())->setStretch(1, 1);
}
else if (0 != m_top_splitter->count() && 0 != m_bot_splitter->count())
{
reinterpret_cast<QVBoxLayout*>(this->centralWidget()->layout())->setStretch(0, 1);
reinterpret_cast<QVBoxLayout*>(this->centralWidget()->layout())->setStretch(1, 1);
}
else
{
reinterpret_cast<QVBoxLayout*>(this->centralWidget()->layout())->setStretch(0, 0);
reinterpret_cast<QVBoxLayout*>(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 *interface = (Form_mainInterface *)m_top_splitter->widget(i);
if (sn == interface->getSn())
{
return interface;
}
}
for (int i = 0; i < m_bot_splitter->count(); ++i)
{
Form_mainInterface *interface = (Form_mainInterface *)m_bot_splitter->widget(i);
if (sn == interface->getSn())
{
return interface;
}
}
return nullptr;
}
2022-12-23 10:32:57 +00:00
Form_mainInterface *MainWindow::FindInterfaceByDevName(const QString &devName)
{
for (int i = 0; i < m_top_splitter->count(); ++i)
{
Form_mainInterface *interface = (Form_mainInterface *)m_top_splitter->widget(i);
if (devName == interface->getDevName())
{
return interface;
}
}
for (int i = 0; i < m_bot_splitter->count(); ++i)
{
Form_mainInterface *interface = (Form_mainInterface *)m_bot_splitter->widget(i);
if (devName == interface->getDevName())
{
return interface;
}
}
return nullptr;
}
2022-12-26 10:38:34 +00:00
Form_mainInterface* MainWindow::FindInterface(SANE_Handle handle)
{
Form_mainInterface *form = nullptr;
for (int i = 0; i < m_top_splitter->count(); ++i)
{
Form_mainInterface *interface = (Form_mainInterface *)m_top_splitter->widget(i);
if (handle == interface->getDevHandle())
{
form = interface;
break;
}
}
if (nullptr == form)
{
for (int i = 0; i < m_bot_splitter->count(); ++i)
{
Form_mainInterface *interface = (Form_mainInterface *)m_bot_splitter->widget(i);
if (handle == interface->getDevHandle())
{
form = interface;
break;
}
}
}
return form;
}
2022-12-16 11:01:30 +00:00
bool MainWindow::AddInterface(Form_mainInterface *interface)
{
assert(nullptr != interface);
2022-12-26 10:38:34 +00:00
HGBase_EnterLock(m_lock);
2022-12-16 11:01:30 +00:00
bool ret = false;
if (m_top_splitter->count() < 2)
{
m_top_splitter->addWidget(interface);
ret = true;
}
else if (m_bot_splitter->count() < 2)
{
m_bot_splitter->addWidget(interface);
ret = true;
}
2022-12-26 10:38:34 +00:00
HGBase_LeaveLock(m_lock);
2022-12-16 11:01:30 +00:00
updateSplitter();
return ret;
}
bool MainWindow::RemoveInterface(Form_mainInterface *interface)
{
assert(nullptr != interface);
2022-12-26 10:38:34 +00:00
HGBase_EnterLock(m_lock);
2022-12-16 11:01:30 +00:00
delete interface;
2022-12-26 10:38:34 +00:00
HGBase_LeaveLock(m_lock);
2022-12-16 11:01:30 +00:00
updateSplitter();
return true;
}
bool MainWindow::isTesting()
{
bool isTesting = false;
for (int i = 0; i < m_top_splitter->count(); ++i)
{
Form_mainInterface *interface = (Form_mainInterface *)m_top_splitter->widget(i);
if (interface->isTesting())
{
isTesting = true;
break;
}
}
if (!isTesting)
{
for (int i = 0; i < m_bot_splitter->count(); ++i)
{
Form_mainInterface *interface = (Form_mainInterface *)m_bot_splitter->widget(i);
if (interface->isTesting())
{
isTesting = true;
break;
}
}
}
return isTesting;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (isTesting())
{
QMessageBox::information(this, tr("tips"), tr("Is testing, do not close!"));
event->ignore();
}
}
2022-12-15 07:00:12 +00:00
int MainWindow::sane_ex_callback(SANE_Handle hdev, int code, void *data, unsigned int *len, void *param)
{
2022-12-26 08:24:20 +00:00
MainWindow* p = (MainWindow*)param;
2022-12-26 10:38:34 +00:00
if (SANE_EVENT_DEVICE_ARRIVED == code)
2022-12-26 08:24:20 +00:00
{
SANE_Device_Ex* sane_dev = (SANE_Device_Ex*)data;
emit p->sane_dev_arrive(sane_dev->name);
}
2022-12-26 10:38:34 +00:00
else if (SANE_EVENT_DEVICE_LEFT == code)
2022-12-26 08:24:20 +00:00
{
SANE_Device* sane_dev = (SANE_Device*)data;
emit p->sane_dev_remove(sane_dev->name);
}
2022-12-15 07:00:12 +00:00
2022-12-28 06:57:00 +00:00
if (code == SANE_EVENT_IMAGE_OK || code == SANE_EVENT_SCAN_FINISHED || code == SANE_EVENT_ERROR)
2022-12-26 08:24:20 +00:00
{
HGBase_EnterLock(p->m_lock);
Form_mainInterface* interface = p->FindInterface(hdev);
if (nullptr != interface)
{
if (nullptr != interface->getScanner() && nullptr != interface->getScanner()->cb_)
interface->getScanner()->cb_(hdev, code, data, len, interface->getScanner());
}
HGBase_LeaveLock(p->m_lock);
2022-12-26 10:38:34 +00:00
}
2022-12-26 08:24:20 +00:00
return 0;
2022-12-15 07:00:12 +00:00
}
std::vector<std::string> MainWindow::getDevices()
2022-12-15 03:00:46 +00:00
{
std::vector<std::string> 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;
}
2022-12-16 11:01:30 +00:00
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";
}
2022-12-23 10:32:57 +00: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());
}
2022-12-15 07:00:12 +00:00
void MainWindow::on_sane_dev_arrive(QString devName)
2022-12-15 03:00:46 +00:00
{
2022-12-16 11:01:30 +00:00
QMessageBox msg(QMessageBox::Question, tr("tips"),
tr("Are you sure to connect the new device"),
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))
{
SANE_Handle devHandle = nullptr;
sane_open(devName.toStdString().c_str(), &devHandle);
2022-12-15 03:00:46 +00:00
2022-12-16 11:01:30 +00:00
Dialog_InputSerialNum dlg(this, getDevSn(devHandle), getDevType(devHandle));
if (dlg.exec())
{
HGPdtToolDbDevice pdtToolDbDevice = nullptr;
HGPdtToolDb_OpenDevice(m_pdtToolDbuserMgr, dlg.GetSn().toStdString().c_str(), &pdtToolDbDevice);
2022-12-26 10:38:34 +00:00
Form_mainInterface *mainInterface = new Form_mainInterface(this, devHandle, m_pdtToolDbuserMgr, pdtToolDbDevice,
2022-12-23 10:32:57 +00:00
dlg.GetSn(), dlg.GetDevType(), devName, getDevFwNum(devHandle));
2022-12-16 11:01:30 +00:00
if (!AddInterface(mainInterface))
{
QMessageBox::information(this, tr("tips"), tr("cannot create more table"));
delete mainInterface;
}
}
else
{
2022-12-23 10:32:57 +00:00
sane_close(devHandle);
2022-12-16 11:01:30 +00:00
}
}
2022-12-15 07:00:12 +00:00
}
void MainWindow::on_sane_dev_remove(QString devName)
{
2022-12-23 10:32:57 +00:00
Form_mainInterface *interface = FindInterfaceByDevName(devName);
if (interface != nullptr)
{
QMessageBox::information(this, tr("tips"), tr("The device has disconnected"));
RemoveInterface(interface);
}
2022-12-15 03:00:46 +00:00
}
2022-12-15 07:00:12 +00:00
2022-12-22 02:48:06 +00:00
void MainWindow::on_update_userStatus()
2022-12-15 09:27:35 +00:00
{
QString tips;
2022-12-22 02:48:06 +00:00
HGUInt userStatus;
HGPdtToolDb_GetUserStatus(m_pdtToolDbuserMgr, &userStatus);
switch (userStatus)
2022-12-15 09:27:35 +00:00
{
2022-12-22 02:48:06 +00:00
case HGPDTTOOLDB_USERSTATUS_CONFLICT:
2022-12-15 09:27:35 +00:00
tips = tr("Account login elsewhere");
break;
2022-12-22 02:48:06 +00:00
case HGPDTTOOLDB_USERSTATUS_REFUSE:
2022-12-15 09:27:35 +00:00
tips = tr("You have been forced offline by the administrator account");
break;
2022-12-22 02:48:06 +00:00
case HGPDTTOOLDB_USERSTATUS_DBERROR:
2022-12-16 11:01:30 +00:00
tips = tr("Data base error");
break;
2022-12-15 09:27:35 +00:00
default:
break;
}
2022-12-22 02:48:06 +00:00
if (!tips.isEmpty())
{
if (!isTesting())
{
QMessageBox::information(this, (tr("tips")), tips);
m_isLogOut = true;
close();
}
2022-12-22 02:48:06 +00:00
}
2022-12-15 09:27:35 +00:00
}
2022-12-14 06:39:22 +00:00
void MainWindow::on_act_newDevice_triggered()
{
2022-12-16 11:01:30 +00:00
Dialog_InputSerialNum dlg(this, "", "");
if (dlg.exec())
2022-12-14 06:39:22 +00:00
{
2022-12-16 11:01:30 +00:00
HGPdtToolDbDevice pdtToolDbDevice = nullptr;
HGPdtToolDb_OpenDevice(m_pdtToolDbuserMgr, dlg.GetSn().toStdString().c_str(), &pdtToolDbDevice);
2022-12-14 06:39:22 +00:00
2022-12-26 10:38:34 +00:00
Form_mainInterface *mainInterface = new Form_mainInterface(this, nullptr, m_pdtToolDbuserMgr, pdtToolDbDevice, dlg.GetSn(), dlg.GetDevType(), nullptr, nullptr);
2022-12-16 11:01:30 +00:00
if (!AddInterface(mainInterface))
{
QMessageBox::information(this, tr("tips"), tr("cannot create more table"));
delete mainInterface;
}
2022-12-14 06:39:22 +00:00
}
}
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()
{
2022-12-16 11:01:30 +00:00
QString filename = QFileDialog::getSaveFileName(this, tr("save"), "test.xls", tr("xls(*.xls)"));
if (!filename.isEmpty())
{
2022-12-23 10:32:57 +00:00
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"));
}
2022-12-16 11:01:30 +00:00
}
2022-12-14 06:39:22 +00:00
}
void MainWindow::on_act_logOut_triggered()
{
if (isTesting())
{
QMessageBox::information(this, tr("tips"), tr("Is testing, do not close!"));
return;
}
2022-12-14 06:39:22 +00:00
m_isLogOut = true;
close();
}
void MainWindow::on_act_close_triggered()
{
if (isTesting())
{
QMessageBox::information(this, tr("tips"), tr("Is testing, do not close!"));
return;
}
2022-12-14 06:39:22 +00:00
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;
}
2022-12-14 12:28:25 +00:00
2022-12-16 11:01:30 +00:00
void MainWindow::on_act_upload_triggered()
{
Dialog_uploadCfgFile dlg(m_pdtToolDbuserMgr, this);
dlg.exec();
}