2023-05-20 08:08:55 +00:00
|
|
|
#include "device_user.h"
|
2023-05-04 06:55:26 +00:00
|
|
|
#include <QMessageBox>
|
2023-05-20 08:08:55 +00:00
|
|
|
#include <QFileDialog>
|
2023-05-16 10:31:32 +00:00
|
|
|
#include "HGUIGlobal.h"
|
|
|
|
#include <thread>
|
2023-05-04 06:55:26 +00:00
|
|
|
|
|
|
|
#if defined(HG_CMP_MSC)
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
DeviceUserMgr::DeviceUserMgr(const QString &password, QWidget *wnd)
|
2023-05-04 06:55:26 +00:00
|
|
|
{
|
2023-05-16 10:31:32 +00:00
|
|
|
m_password = password;
|
2023-05-04 06:55:26 +00:00
|
|
|
m_wnd = wnd;
|
|
|
|
m_twainDSM = nullptr;
|
|
|
|
HGTwain_CreateDSM((HWND)m_wnd->winId(), &m_twainDSM);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceUserMgr::~DeviceUserMgr()
|
|
|
|
{
|
|
|
|
HGTwain_DestroyDSM(m_twainDSM);
|
|
|
|
m_twainDSM = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DeviceUser* DeviceUserMgr::OpenDeviceUser()
|
|
|
|
{
|
|
|
|
HGTwainDS ds = nullptr;
|
2023-08-15 07:28:23 +00:00
|
|
|
HGTwain_CreateSelectedDSEx(m_twainDSM, &ds);
|
2023-05-04 06:55:26 +00:00
|
|
|
if (nullptr == ds)
|
|
|
|
return nullptr;
|
2023-05-16 10:31:32 +00:00
|
|
|
return new DeviceUser(m_wnd, ds, m_password);
|
2023-05-04 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 11:11:24 +00:00
|
|
|
DeviceUser *DeviceUserMgr::OpenDefaultDeviceUser()
|
|
|
|
{
|
2023-06-01 01:54:25 +00:00
|
|
|
HGTwainDS ds = nullptr;
|
2023-08-15 07:28:23 +00:00
|
|
|
HGTwain_CreateDefaultDS(m_twainDSM, &ds);
|
2023-06-01 01:54:25 +00:00
|
|
|
if (nullptr == ds)
|
|
|
|
return nullptr;
|
|
|
|
return new DeviceUser(m_wnd, ds, m_password);
|
2023-05-30 11:11:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
DeviceUser::DeviceUser(QWidget *wnd, HGTwainDS ds, const QString &password)
|
2023-05-04 06:55:26 +00:00
|
|
|
{
|
|
|
|
m_wnd = wnd;
|
|
|
|
m_twainDS = ds;
|
2023-05-16 10:31:32 +00:00
|
|
|
m_password = password;
|
2023-05-04 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceUser::~DeviceUser()
|
|
|
|
{
|
|
|
|
HGTwain_CloseDS(m_twainDS);
|
2023-08-15 07:28:23 +00:00
|
|
|
HGTwain_DestroyDS(m_twainDS);
|
2023-05-04 06:55:26 +00:00
|
|
|
m_twainDS = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
HGResult DeviceUser::Open()
|
|
|
|
{
|
|
|
|
return HGTwain_OpenDS(m_twainDS);
|
|
|
|
}
|
|
|
|
|
2023-08-15 07:38:21 +00:00
|
|
|
HGResult DeviceUser::OpenDefault()
|
|
|
|
{
|
|
|
|
return HGTwain_OpenDS(m_twainDS);
|
|
|
|
}
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
HGResult DeviceUser::Close()
|
|
|
|
{
|
|
|
|
return HGTwain_CloseDS(m_twainDS);
|
|
|
|
}
|
|
|
|
|
2023-05-04 06:55:26 +00:00
|
|
|
QString DeviceUser::GetName()
|
|
|
|
{
|
2023-06-01 07:38:58 +00:00
|
|
|
HGChar devName[256] = {0};
|
2023-05-31 02:12:38 +00:00
|
|
|
HGTwain_GetDSDeviceName(m_twainDS, devName, 256);
|
2023-05-04 06:55:26 +00:00
|
|
|
return QString(devName);
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::ShowSettingDlg()
|
|
|
|
{
|
2023-05-20 08:08:55 +00:00
|
|
|
return HGTwain_EnableDSUIOnly(m_twainDS, (HWND)m_wnd->winId(), DSEventFunc, this);
|
2023-05-04 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::StartScan()
|
|
|
|
{
|
2023-05-20 08:08:55 +00:00
|
|
|
return HGTwain_EnableDS(m_twainDS, HGFALSE, (HWND)m_wnd->winId(), DSEventFunc, this, DSImageFunc, this);
|
2023-05-04 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 10:31:32 +00:00
|
|
|
HGResult DeviceUser::StartSingleScan()
|
|
|
|
{
|
2023-05-20 08:27:48 +00:00
|
|
|
return HGTwain_EnableDSWithSingleScan(m_twainDS, (HWND)m_wnd->winId(), DSEventFunc, this, DSImageFunc, this);
|
2023-05-16 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 06:55:26 +00:00
|
|
|
HGResult DeviceUser::GetDeviceCustomInfo(HGTwainDeviceCustomInfo *info)
|
|
|
|
{
|
|
|
|
return HGTwain_GetDSDeviceCustomInfo(m_twainDS, info);
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:31:32 +00:00
|
|
|
HGResult DeviceUser::Login()
|
|
|
|
{
|
|
|
|
return HGTwain_LoginDS(m_twainDS, "user", getStdString(m_password).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::Logout()
|
|
|
|
{
|
|
|
|
return HGTwain_LogoutDS(m_twainDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::ClearRollerCount()
|
|
|
|
{
|
|
|
|
return HGTwain_ClearDSRollerCount(m_twainDS);
|
|
|
|
}
|
|
|
|
|
2023-05-17 10:29:08 +00:00
|
|
|
QString DeviceUser::GetDriverLog()
|
2023-05-16 10:31:32 +00:00
|
|
|
{
|
2023-05-20 08:08:55 +00:00
|
|
|
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
|
2023-06-19 06:52:49 +00:00
|
|
|
std::string f = getStdFileName(fileName).toLocal8Bit().data();
|
|
|
|
HGResult ret = HGTwain_GetDSDriverLog(m_twainDS, f.c_str());
|
2023-05-17 10:29:08 +00:00
|
|
|
if (ret == HGBASE_ERR_OK)
|
2023-05-20 08:08:55 +00:00
|
|
|
return fileName;
|
2023-05-17 10:29:08 +00:00
|
|
|
|
2023-05-20 08:08:55 +00:00
|
|
|
return "Fail";
|
2023-05-16 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::ClearDriverLog()
|
|
|
|
{
|
|
|
|
return HGTwain_ClearDSDriverLog(m_twainDS);
|
|
|
|
}
|
|
|
|
|
2023-05-17 10:29:08 +00:00
|
|
|
QString DeviceUser::GetDeviceLog()
|
2023-05-16 10:31:32 +00:00
|
|
|
{
|
2023-05-20 08:08:55 +00:00
|
|
|
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
|
2023-06-19 06:52:49 +00:00
|
|
|
std::string f = getStdFileName(fileName).toLocal8Bit().data();
|
|
|
|
HGResult ret = HGTwain_GetDSDeviceLog(m_twainDS, f.c_str());
|
2023-05-17 10:29:08 +00:00
|
|
|
if (ret == HGBASE_ERR_OK)
|
2023-05-20 08:08:55 +00:00
|
|
|
return fileName;
|
2023-05-17 10:29:08 +00:00
|
|
|
|
2023-05-20 08:08:55 +00:00
|
|
|
return "Fail";
|
2023-05-16 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::ClearDeviceLog()
|
|
|
|
{
|
|
|
|
return HGTwain_ClearDSDeviceLog(m_twainDS);
|
|
|
|
}
|
|
|
|
|
2023-05-20 08:08:55 +00:00
|
|
|
void HGAPI DeviceUser::DSEventFunc(HGTwainDS ds, HGUInt event, HGPointer param)
|
2023-05-04 06:55:26 +00:00
|
|
|
{
|
|
|
|
DeviceUser* p = (DeviceUser*)param;
|
2023-06-01 06:57:39 +00:00
|
|
|
if (HGTWAIN_EVENT_TYPE_SCANFINISHED == event)
|
|
|
|
{
|
|
|
|
HGTwain_DisableDS(p->m_twainDS);
|
|
|
|
emit p->scanFinishEvent();
|
|
|
|
}
|
|
|
|
else if (HGTWAIN_EVENT_TYPE_WORKING == event)
|
|
|
|
{
|
|
|
|
emit p->scanWorkingEvent();
|
|
|
|
}
|
2023-05-04 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HGAPI DeviceUser::DSImageFunc(HGTwainDS ds, HGImage image, HGPointer param)
|
|
|
|
{
|
|
|
|
DeviceUser* p = (DeviceUser*)param;
|
2023-05-20 03:47:19 +00:00
|
|
|
emit p->newImage(image);
|
2023-05-16 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 06:55:26 +00:00
|
|
|
#else
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
DeviceUserMgr::DeviceUserMgr(const QString &password, QWidget *wnd)
|
2023-05-04 06:55:26 +00:00
|
|
|
{
|
|
|
|
m_wnd = wnd;
|
2023-05-16 10:31:32 +00:00
|
|
|
m_password = password;
|
2023-08-15 07:28:23 +00:00
|
|
|
m_saneMgr = nullptr;
|
2023-05-04 06:55:26 +00:00
|
|
|
HGSane_CreateManager(&m_saneMgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceUserMgr::~DeviceUserMgr()
|
|
|
|
{
|
|
|
|
HGSane_DestroyManager(m_saneMgr);
|
|
|
|
m_saneMgr = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DeviceUser* DeviceUserMgr::OpenDeviceUser()
|
|
|
|
{
|
|
|
|
HGSaneSource source = nullptr;
|
2023-07-31 08:56:59 +00:00
|
|
|
HGSane_OpenSource(m_saneMgr, 0, &source);
|
2023-05-04 06:55:26 +00:00
|
|
|
if (nullptr == source)
|
|
|
|
return nullptr;
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
return new DeviceUser(m_wnd, source, m_password);
|
2023-05-04 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 09:25:56 +00:00
|
|
|
DeviceUser *DeviceUserMgr::OpenDefaultDeviceUser()
|
|
|
|
{
|
|
|
|
HGSaneSource source = nullptr;
|
|
|
|
HGSane_OpenDefaultSource(m_saneMgr, &source);
|
|
|
|
if (nullptr == source)
|
|
|
|
return nullptr;
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
return new DeviceUser(m_wnd, source, m_password);
|
2023-06-03 09:25:56 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
DeviceUser::DeviceUser(QWidget *wnd, HGSaneSource source, const QString &password)
|
2023-05-04 06:55:26 +00:00
|
|
|
{
|
|
|
|
m_wnd = wnd;
|
|
|
|
m_source = source;
|
2023-08-15 07:28:23 +00:00
|
|
|
m_saneDev = nullptr;
|
2023-05-16 10:31:32 +00:00
|
|
|
m_password = password;
|
2023-05-04 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceUser::~DeviceUser()
|
|
|
|
{
|
|
|
|
HGSane_CloseDevice(m_saneDev);
|
|
|
|
m_saneDev = nullptr;
|
|
|
|
HGSane_CloseSource(m_source);
|
|
|
|
m_source = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
HGResult DeviceUser::Open()
|
|
|
|
{
|
|
|
|
if (nullptr != m_saneDev)
|
|
|
|
{
|
|
|
|
return HGSANE_ERR_FAIL;
|
|
|
|
}
|
|
|
|
|
2023-08-21 07:34:39 +00:00
|
|
|
HGUInt count = 0;
|
|
|
|
HGSane_GetDeviceCount(m_source, &count);
|
|
|
|
if (0 == count)
|
|
|
|
{
|
|
|
|
QMessageBox::information(m_wnd, tr("Prompt"), tr("Scanner not found!"));
|
|
|
|
return HGSANE_ERR_FAIL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return HGSane_OpenSelectedDevice(m_source, m_wnd, &m_saneDev);
|
|
|
|
}
|
2023-08-15 07:28:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 07:38:21 +00:00
|
|
|
HGResult DeviceUser::OpenDefault()
|
|
|
|
{
|
|
|
|
if (nullptr != m_saneDev)
|
|
|
|
{
|
|
|
|
return HGSANE_ERR_FAIL;
|
|
|
|
}
|
|
|
|
|
2023-08-21 07:34:39 +00:00
|
|
|
HGUInt count = 0;
|
|
|
|
HGSane_GetDeviceCount(m_source, &count);
|
|
|
|
if (0 == count)
|
|
|
|
{
|
|
|
|
QMessageBox::information(m_wnd, tr("Prompt"), tr("Scanner not found!"));
|
|
|
|
return HGSANE_ERR_FAIL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HGChar errInfo[256];
|
|
|
|
return HGSane_OpenDevice(m_source, 0, &m_saneDev, errInfo, 256);
|
|
|
|
}
|
2023-08-15 07:38:21 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 07:28:23 +00:00
|
|
|
HGResult DeviceUser::Close()
|
|
|
|
{
|
|
|
|
if (nullptr == m_saneDev)
|
|
|
|
{
|
|
|
|
return HGSANE_ERR_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
HGSane_CloseDevice(m_saneDev);
|
|
|
|
m_saneDev = nullptr;
|
|
|
|
return HGBASE_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2023-05-04 06:55:26 +00:00
|
|
|
QString DeviceUser::GetName()
|
|
|
|
{
|
|
|
|
HGChar devName[256];
|
2023-05-31 02:12:38 +00:00
|
|
|
HGSane_GetDeviceName(m_saneDev, devName, 256);
|
2023-05-04 06:55:26 +00:00
|
|
|
return QString(devName);
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::ShowSettingDlg()
|
|
|
|
{
|
|
|
|
return HGSane_ShowDeviceSettingDlg(m_saneDev, m_wnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::StartScan()
|
|
|
|
{
|
2023-05-23 06:52:45 +00:00
|
|
|
return HGSane_StartDevice(m_saneDev, m_wnd, DeviceEventFunc, this, DeviceImageFunc, this);
|
2023-05-04 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 10:06:05 +00:00
|
|
|
HGResult DeviceUser::StartSingleScan()
|
2023-05-16 10:31:32 +00:00
|
|
|
{
|
2023-05-23 06:52:45 +00:00
|
|
|
return HGSane_StartDeviceWithSingleScan(m_saneDev, m_wnd, DeviceEventFunc, this, DeviceImageFunc, this);
|
2023-05-16 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 10:06:05 +00:00
|
|
|
HGResult DeviceUser::GetDeviceCustomInfo(HGSaneDeviceCustomInfo *info)
|
2023-05-04 06:55:26 +00:00
|
|
|
{
|
|
|
|
return HGSane_GetDeviceCustomInfo(m_saneDev, info);
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:31:32 +00:00
|
|
|
HGResult DeviceUser::Login()
|
|
|
|
{
|
|
|
|
return HGSane_Login(m_saneDev, "user", getStdString(m_password).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::Logout()
|
|
|
|
{
|
|
|
|
return HGSane_Logout(m_saneDev);
|
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::ClearRollerCount()
|
|
|
|
{
|
|
|
|
return HGSane_ClearRollerCount(m_saneDev);
|
|
|
|
}
|
|
|
|
|
2023-06-13 07:24:58 +00:00
|
|
|
int DeviceUser::GetDeviceRollerLife()
|
|
|
|
{
|
|
|
|
int rollerLife = 0;
|
|
|
|
HGSane_GetDeviceRollerLife(m_saneDev, &rollerLife);
|
|
|
|
return rollerLife;
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:06:05 +00:00
|
|
|
QString DeviceUser::GetDriverLog()
|
2023-05-16 10:31:32 +00:00
|
|
|
{
|
2023-05-20 10:06:05 +00:00
|
|
|
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
|
2023-06-19 06:52:49 +00:00
|
|
|
std::string f = getStdFileName(fileName).toLocal8Bit().data();
|
|
|
|
HGResult ret = HGSane_GetDriverLog(m_saneDev, f.c_str());
|
2023-05-20 10:06:05 +00:00
|
|
|
if (ret == HGBASE_ERR_OK)
|
|
|
|
return fileName;
|
|
|
|
|
|
|
|
return "Fail";
|
2023-05-16 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::ClearDriverLog()
|
|
|
|
{
|
|
|
|
return HGSane_ClearDriverLog(m_saneDev);
|
|
|
|
}
|
|
|
|
|
2023-05-20 10:06:05 +00:00
|
|
|
QString DeviceUser::GetDeviceLog()
|
2023-05-16 10:31:32 +00:00
|
|
|
{
|
2023-05-20 10:06:05 +00:00
|
|
|
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
|
2023-06-19 06:52:49 +00:00
|
|
|
std::string f = getStdFileName(fileName).toLocal8Bit().data();
|
|
|
|
HGResult ret = HGSane_GetDeviceLog(m_saneDev, f.c_str());
|
2023-05-20 10:06:05 +00:00
|
|
|
if (ret == HGBASE_ERR_OK)
|
|
|
|
return fileName;
|
|
|
|
|
|
|
|
return "Fail";
|
2023-05-16 10:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HGResult DeviceUser::ClearDeviceLog()
|
|
|
|
{
|
|
|
|
return HGSane_ClearDeviceLog(m_saneDev);
|
|
|
|
}
|
|
|
|
|
2023-05-23 06:52:45 +00:00
|
|
|
void HGAPI DeviceUser::DeviceEventFunc(HGSaneDevice dev, HGUInt event, HGPointer param)
|
|
|
|
{
|
2023-06-03 09:25:56 +00:00
|
|
|
DeviceUser* p = (DeviceUser*)param;
|
|
|
|
if (HGSANE_EVENT_TYPE_SCANFINISHED == event)
|
|
|
|
{
|
|
|
|
emit p->scanFinishEvent();
|
|
|
|
}
|
|
|
|
else if (HGSANE_EVENT_TYPE_WORKING == event)
|
|
|
|
{
|
|
|
|
emit p->scanWorkingEvent();
|
|
|
|
}
|
2023-05-23 06:52:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 06:55:26 +00:00
|
|
|
void HGAPI DeviceUser::DeviceImageFunc(HGSaneDevice dev, HGImage image, HGPointer param)
|
|
|
|
{
|
|
|
|
DeviceUser* p = (DeviceUser*)param;
|
2023-05-20 03:47:19 +00:00
|
|
|
emit p->newImage(image);
|
2023-05-04 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|