code_app/app/scanner2/device_user.cpp

295 lines
6.8 KiB
C++

#include "device_user.h"
#include <QMessageBox>
#include <QFileDialog>
#include "HGUIGlobal.h"
#include <thread>
#if defined(HG_CMP_MSC)
DeviceUserMgr::DeviceUserMgr(QString password, QWidget *wnd)
{
m_password = password;
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;
HGTwain_OpenSelectedDSEx(m_twainDSM, &ds);
if (nullptr == ds)
return nullptr;
return new DeviceUser(m_wnd, ds, m_password);
}
DeviceUser *DeviceUserMgr::OpenDefaultDeviceUser()
{
HGTwainDS ds = nullptr;
HGTwain_OpenDefaultDS(m_twainDSM, &ds);
if (nullptr == ds)
return nullptr;
return new DeviceUser(m_wnd, ds, m_password);
}
DeviceUser::DeviceUser(QWidget *wnd, HGTwainDS ds, QString password)
{
m_wnd = wnd;
m_twainDS = ds;
m_password = password;
}
DeviceUser::~DeviceUser()
{
HGTwain_CloseDS(m_twainDS);
m_twainDS = nullptr;
}
QString DeviceUser::GetName()
{
HGChar devName[256];
HGTwain_GetDSDeviceName(m_twainDS, devName, 256);
return QString(devName);
}
HGResult DeviceUser::ShowSettingDlg()
{
return HGTwain_EnableDSUIOnly(m_twainDS, (HWND)m_wnd->winId(), DSEventFunc, this);
}
HGResult DeviceUser::StartScan()
{
return HGTwain_EnableDS(m_twainDS, HGFALSE, (HWND)m_wnd->winId(), DSEventFunc, this, DSImageFunc, this);
}
HGResult DeviceUser::StartSingleScan()
{
return HGTwain_EnableDSWithSingleScan(m_twainDS, (HWND)m_wnd->winId(), DSEventFunc, this, DSImageFunc, this);
}
HGResult DeviceUser::GetDeviceCustomInfo(HGTwainDeviceCustomInfo *info)
{
return HGTwain_GetDSDeviceCustomInfo(m_twainDS, info);
}
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);
}
QString DeviceUser::GetDriverLog()
{
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
HGResult ret = HGTwain_GetDSDriverLog(m_twainDS, fileName.toStdString().c_str());
if (ret == HGBASE_ERR_OK)
return fileName;
return "Fail";
}
HGResult DeviceUser::ClearDriverLog()
{
return HGTwain_ClearDSDriverLog(m_twainDS);
}
QString DeviceUser::GetDeviceLog()
{
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
HGResult ret = HGTwain_GetDSDeviceLog(m_twainDS, fileName.toStdString().c_str());
if (ret == HGBASE_ERR_OK)
return fileName;
return "Fail";
}
HGResult DeviceUser::ClearDeviceLog()
{
return HGTwain_ClearDSDeviceLog(m_twainDS);
}
void HGAPI DeviceUser::DSEventFunc(HGTwainDS ds, HGUInt event, HGPointer param)
{
DeviceUser* p = (DeviceUser*)param;
emit p->scanEvent(event);
}
void HGAPI DeviceUser::DSImageFunc(HGTwainDS ds, HGImage image, HGPointer param)
{
DeviceUser* p = (DeviceUser*)param;
emit p->newImage(image);
}
#else
DeviceUserMgr::DeviceUserMgr(QString password, QWidget *wnd)
{
m_wnd = wnd;
m_saneMgr = nullptr;
m_password = password;
HGSane_CreateManager(&m_saneMgr);
}
DeviceUserMgr::~DeviceUserMgr()
{
HGSane_DestroyManager(m_saneMgr);
m_saneMgr = nullptr;
}
class DeviceUser* DeviceUserMgr::OpenDeviceUser()
{
HGSaneSource source = nullptr;
HGSane_OpenSelectedSource(m_saneMgr, m_wnd, &source);
if (nullptr == source)
return nullptr;
HGUInt devCount = 0;
HGSane_GetDeviceCount(source, &devCount);
if (0 == devCount)
{
HGSane_CloseSource(source);
QMessageBox::information(m_wnd, tr("tips"), tr("no device"));
return nullptr;
}
else if (1 == devCount)
{
HGChar errInfo[256];
HGSaneDevice dev = nullptr;
HGSane_OpenDevice(source, 0, &dev, errInfo, 256);
if (nullptr == dev)
{
HGSane_CloseSource(source);
QMessageBox::information(m_wnd, tr("tips"), QString::fromUtf8(errInfo));
return nullptr;
}
return new DeviceUser(m_wnd, source, dev, m_password);
}
HGSaneDevice dev = nullptr;
HGSane_OpenSelectedDevice(source, m_wnd, &dev);
if (nullptr == dev)
{
HGSane_CloseSource(source);
return nullptr;
}
return new DeviceUser(m_wnd, source, dev, m_password);
}
DeviceUser::DeviceUser(QWidget *wnd, HGSaneSource source, HGSaneDevice dev, QString password)
{
m_wnd = wnd;
m_source = source;
m_saneDev = dev;
m_password = password;
}
DeviceUser::~DeviceUser()
{
HGSane_CloseDevice(m_saneDev);
m_saneDev = nullptr;
HGSane_CloseSource(m_source);
m_source = nullptr;
}
QString DeviceUser::GetName()
{
HGChar devName[256];
HGSane_GetDeviceName(m_saneDev, devName, 256);
return QString(devName);
}
HGResult DeviceUser::ShowSettingDlg()
{
return HGSane_ShowDeviceSettingDlg(m_saneDev, m_wnd);
}
HGResult DeviceUser::StartScan()
{
return HGSane_StartDevice(m_saneDev, m_wnd, DeviceEventFunc, this, DeviceImageFunc, this);
}
HGResult DeviceUser::StartSingleScan()
{
return HGSane_StartDeviceWithSingleScan(m_saneDev, m_wnd, DeviceEventFunc, this, DeviceImageFunc, this);
}
HGResult DeviceUser::GetDeviceCustomInfo(HGSaneDeviceCustomInfo *info)
{
return HGSane_GetDeviceCustomInfo(m_saneDev, info);
}
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);
}
QString DeviceUser::GetDriverLog()
{
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
HGResult ret = HGSane_GetDriverLog(m_saneDev, fileName.toStdString().c_str());
if (ret == HGBASE_ERR_OK)
return fileName;
return "Fail";
}
HGResult DeviceUser::ClearDriverLog()
{
return HGSane_ClearDriverLog(m_saneDev);
}
QString DeviceUser::GetDeviceLog()
{
QString fileName = QFileDialog::getSaveFileName(m_wnd, tr("Select log file path"), ".", tr("text(*.txt)"));
HGResult ret = HGSane_GetDeviceLog(m_saneDev, fileName.toStdString().c_str());
if (ret == HGBASE_ERR_OK)
return fileName;
return "Fail";
}
HGResult DeviceUser::ClearDeviceLog()
{
return HGSane_ClearDeviceLog(m_saneDev);
}
void HGAPI DeviceUser::DeviceEventFunc(HGSaneDevice dev, HGUInt event, HGPointer param)
{
}
void HGAPI DeviceUser::DeviceImageFunc(HGSaneDevice dev, HGImage image, HGPointer param)
{
DeviceUser* p = (DeviceUser*)param;
emit p->newImage(image);
}
#endif