#include "Manager.h" #include #ifdef HG_CMP_MSC #include #endif #include "lang/app_language.h" #include #include #include "base/HGInfo.h" Q_DECLARE_METATYPE(HGImage) extern Manager* g_manager; Manager::Manager() : QObject(nullptr) , m_progressUiCallback(std::function()) , m_progNotify(nullptr) , m_settingNotify(nullptr) , m_settingUiCallback(std::function()) , m_settingUi(nullptr) , m_progressUi(nullptr) { qRegisterMetaType("HGImage"); connect(this, SIGNAL(init()), this, SLOT(on_init())); connect(this, SIGNAL(createDeviceSelect(void *)), this, SLOT(on_createDeviceSelect(void *))); connect(this, SIGNAL(createSettingUi(void *)), this, SLOT(on_createSettingUi(void *))); connect(this, SIGNAL(createProgressUi(void *)), this, SLOT(on_createProgressUi(void *))); connect(this, SIGNAL(createMessageBoxUi(void *)), this, SLOT(on_createMessageBoxUi(void *))); connect(this, SIGNAL(createTwainSrcUi(void *)), this, SLOT(on_createTwainSrcUi(void *))); connect(this, SIGNAL(createSaneSrcUi(void *)), this, SLOT(on_createSaneSrcUi(void *))); connect(this, SIGNAL(createAbnormalImgUi(void *, HGImage)), this, SLOT(on_createAbnormalImgUi(void *, HGImage))); connect(this, SIGNAL(deleteSettingUi(void *)), this, SLOT(on_deleteSettingUi(void *))); connect(this, SIGNAL(deleteProgressUi(void *)), this, SLOT(on_deleteProgressUi(void *))); connect(this, SIGNAL(deleteMessageBoxUi(void *)), this, SLOT(on_deleteMessageBoxUi(void *))); emit init(); } Manager::~Manager() { QCoreApplication::removeTranslator(&m_translator); if (936 == m_langCode) QCoreApplication::removeTranslator(&m_translator_qt); } int Manager::showDeviceSelect(const std::vector& devs) { // param m_DeviceSelectDevs = devs; void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit createDeviceSelect(hStopEvent); #ifdef HG_CMP_MSC if (nullptr != hStopEvent) { msgLoop(hStopEvent); CloseHandle(hStopEvent); } #endif // ret return m_DeviceSelectResult; } int Manager::showSettingUi(SANE_Handle device, HWND parent, LPSANEAPI api, const char * devName, gb::scanner_cfg* cfg, bool with_scan, std::function callback, std::function* notify) { // param m_device = device; scanner_cfg_ = cfg; m_settingUiParent = (HGWindow)parent; memcpy(&m_api, api, sizeof(m_api)); m_devName = devName; m_with_scan = with_scan; m_settingUiCallback = callback; m_settingNotify = notify; void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit createSettingUi(hStopEvent); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "[Manager::showSettingUi] emit createSettingUi(ThreadId)"); #ifdef HG_CMP_MSC if (nullptr != hStopEvent) { msgLoop(hStopEvent); CloseHandle(hStopEvent); } #endif return 0; } int Manager::showProgressUi(HWND parent, std::function callback, std::function *notify) { // param m_progressUiparent = (HGWindow)parent; m_progressUiCallback = callback; m_progNotify = notify; void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit createProgressUi(hStopEvent); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "[Manager::showProgressUi] emit createProgressUi(ThreadId)"); #ifdef HG_CMP_MSC if (nullptr != hStopEvent) { msgLoop(hStopEvent); CloseHandle(hStopEvent); } #endif HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "[Manager::showProgressUi] Exit Message Loop"); return 0; } int Manager::showMessageBoxUi(HWND parent, int event, void *msg, int flag) { // param m_messageBoxUiParent = (HGWindow)parent; m_notifyEvent = event; m_message = (char*)msg; m_flag = flag; void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit createMessageBoxUi(hStopEvent); #ifdef HG_CMP_MSC if (nullptr != hStopEvent) { msgLoop(hStopEvent); CloseHandle(hStopEvent); } #endif return 0; } int Manager::showTwainSrcUi(const TW_IDENTITY *vds, HGUInt count, const char* defDevName, HGWindow parent, TW_IDENTITY *ds) { m_TwainSrcUiparent = parent; m_vds.clear(); for (int i = 0; i < count; ++i) { m_vds.push_back(vds[i]); } m_defDsName = defDevName; memset(&m_ds, 0, sizeof(TW_IDENTITY)); void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit createTwainSrcUi(hStopEvent); #ifdef HG_CMP_MSC if (nullptr != hStopEvent) { msgLoop(hStopEvent); CloseHandle(hStopEvent); } #endif // ret memcpy(ds, &m_ds, sizeof(TW_IDENTITY)); return 0; } int Manager::showSaneSrcUi(const char **manuNames, const char **sanePaths, HGWindow parent, HGDll *dll, SANEAPI* saneApi, char *manuName, unsigned int maxLen) { // param m_SaneSrcUiparent = parent; m_saneSource.clear(); const char **p1 = manuNames; const char **p2 = sanePaths; while (*p1 != NULL && *p2 != NULL) { std::pair pr; pr.first = *p1; pr.second = *p2; m_saneSource.push_back(pr); ++p1; ++p2; } m_saneDll = NULL; memset(&m_saneApi, 0, sizeof(SANEAPI)); m_saneManuName.clear(); void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit createSaneSrcUi(hStopEvent); #ifdef HG_CMP_MSC if (nullptr != hStopEvent) { msgLoop(hStopEvent); CloseHandle(hStopEvent); } #endif // ret *dll = m_saneDll; memcpy(saneApi, &m_saneApi, sizeof(SANEAPI)); strcpy(manuName, m_saneManuName.c_str()); return 0; } int Manager::showAbnormalImgUi(SANE_Image *img) { // param HGUInt imgType = 0; if (img->header.format == SANE_FRAME_GRAY) { if (1 == img->header.depth) imgType = HGBASE_IMGTYPE_BINARY; else if (8 == img->header.depth) imgType = HGBASE_IMGTYPE_GRAY; } else if (img->header.format == SANE_FRAME_RGB) { imgType = HGBASE_IMGTYPE_RGB; } HGImageInfo imgInfo = { (HGUInt)img->header.pixels_per_line, (HGUInt)img->header.lines, imgType, (HGUInt)img->header.bytes_per_line, HGBASE_IMGORIGIN_TOP }; HGImage image = NULL; HGBase_CreateImageWithData(img->data, &imgInfo, &image); void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit createAbnormalImgUi(hStopEvent, image); #ifdef HG_CMP_MSC if (nullptr != hStopEvent) { msgLoop(hStopEvent); CloseHandle(hStopEvent); } #endif // ret HGBase_DestroyImage(image); return m_abnormalImgResult; } void Manager::closeSettingUi() { void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit deleteSettingUi(hStopEvent); } void Manager::closeProgressUi() { void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit deleteProgressUi(hStopEvent); } void Manager::closeMessageBoxUi() { void *hStopEvent = nullptr; #ifdef HG_CMP_MSC if (qApp->thread() != QThread::currentThread()) { hStopEvent = ::CreateEvent(0, FALSE, FALSE, 0); } #endif emit deleteMessageBoxUi(hStopEvent); } void Manager::clear_functions(void) { m_settingUiCallback = std::function(); m_progressUiCallback = std::function(); m_progNotify = nullptr; m_settingNotify = nullptr; if (m_progressUi) m_progressUi->clear_callback(); if (m_settingUi) m_settingUi->clear_callback(); } void Manager::msgLoop(void *hEvent) { #ifdef HG_CMP_MSC assert(nullptr != hEvent); while (1) { MSG msg; while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } HANDLE arEvents[1] = { hEvent }; DWORD result = MsgWaitForMultipleObjectsEx(1, arEvents, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE); if (result == WAIT_OBJECT_0 + 1) // msg received { continue; } else if (result == WAIT_OBJECT_0) { break; } else if (result == WAIT_IO_COMPLETION) { break; } } #endif } void Manager::on_init() { m_langCode = lang_get_cur_code_page(); if (936 != m_langCode) { m_translator.load(":translation/TwainUI_zh_EN.qm"); } else { m_translator.load(":translation/TwainUI_zh_CN.qm"); m_translator_qt.load(":translation/qt_zh_CN.qm"); } QCoreApplication::installTranslator(&m_translator); if (936 == m_langCode) QCoreApplication::installTranslator(&m_translator_qt); } void Manager::on_createDeviceSelect(void *hEvent) { Dialog_device_select dlg(m_DeviceSelectDevs); dlg.exec(); m_DeviceSelectResult = dlg.getDevId(); #ifdef HG_CMP_MSC if (nullptr != hEvent) { SetEvent((HANDLE)hEvent); } #endif } void Manager::on_createSettingUi(void *hEvent) { QWidget *qParent = nullptr; #ifdef HG_CMP_MSC if (nullptr != m_settingUiParent) { QWinWidget *win = new QWinWidget(m_settingUiParent); win->showCentered(); qParent = win; } #else qParent = m_settingUiParent; #endif auto close_prog = [this](ui_result r) ->void { if(r != UI_RESULT_START_SCAN) m_settingUi = nullptr; m_settingUiCallback(r); }; if (m_settingUi != nullptr) delete m_settingUi; m_settingUi = new hg_settingdialog(this, m_device, &m_api, m_with_scan, m_devName.c_str(), scanner_cfg_, close_prog, m_settingNotify, qParent); m_settingUi->setModal(true); if (nullptr == m_settingUiParent) { m_settingUi->setWindowFlags(Qt::WindowStaysOnTopHint); } m_settingUi->show(); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "[Manager::on_createSettingUi] m_settingUi->show()"); #ifdef HG_CMP_MSC if (nullptr != hEvent) { SetEvent((HANDLE)hEvent); } #endif } void Manager::on_createProgressUi(void *hEvent) { if (nullptr != m_settingUi) m_progressUiparent = (HGWindow)m_settingUi->winId(); QWidget *qParent = nullptr; #ifdef HG_CMP_MSC if (nullptr != m_progressUiparent) { QWinWidget *win = new QWinWidget(m_progressUiparent); win->showCentered(); qParent = win; } #else qParent = m_progressUiparent; #endif auto close_prog = [&](ui_result r) ->void { if(m_progressUiCallback) m_progressUiCallback(r); if(r == UI_RESULT_CLOSE_NORMAL) m_progressUiCallback = std::function(); }; if (m_progressUi != nullptr) delete m_progressUi; m_progressUi = new Dialog_progress_ui(this, close_prog, m_progNotify, qParent); m_progressUi->setModal(true); m_progressUi->show(); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "[Manager::on_createProgressUi] m_progressUi->show()"); #ifdef HG_CMP_MSC if (nullptr != hEvent) { SetEvent((HANDLE)hEvent); } #endif } void Manager::on_createMessageBoxUi(void *hEvent) { QWidget *qParent = nullptr; #ifdef HG_CMP_MSC QWinWidget win(m_messageBoxUiParent); if (nullptr != m_messageBoxUiParent) { win.showCentered(); qParent = &win; } #else qParent = m_messageBoxUiParent; #endif QMessageBox dlg(QMessageBox::Critical, tr("Prompt"), QString::fromStdString(m_message), QMessageBox::Ok, qParent); dlg.setWindowFlags(Qt::SubWindow | Qt::Popup | Qt::WindowStaysOnTopHint); dlg.exec(); #ifdef HG_CMP_MSC if (nullptr != hEvent) { SetEvent((HANDLE)hEvent); } #endif } void Manager::on_createTwainSrcUi(void *hEvent) { QWidget *qParent = nullptr; #ifdef HG_CMP_MSC QWinWidget win(m_TwainSrcUiparent); if (nullptr != m_TwainSrcUiparent) { win.showCentered(); qParent = &win; } #else qParent = m_TwainSrcUiparent; #endif Dialog_Twain_Source_Select dlg(m_vds, m_defDsName, qParent); if (dlg.exec()) { dlg.GetIdentify(&m_ds); } #ifdef HG_CMP_MSC if (nullptr != hEvent) { SetEvent((HANDLE)hEvent); } #endif } void Manager::on_createSaneSrcUi(void *hEvent) { QWidget *qParent = nullptr; #ifdef HG_CMP_MSC QWinWidget win(m_SaneSrcUiparent); if (nullptr != m_SaneSrcUiparent) { win.showCentered(); qParent = &win; } #else qParent = m_SaneSrcUiparent; #endif Dialog_Source_Select dlg(m_saneSource, qParent); if (dlg.exec()) { m_saneManuName = dlg.GetManuName(); m_saneDll = dlg.GetDll(); dlg.GetSaneAPI(&m_saneApi); } #ifdef HG_CMP_MSC if (nullptr != hEvent) { SetEvent((HANDLE)hEvent); } #endif } void Manager::on_createAbnormalImgUi(void *hEvent, HGImage image) { Dialog_abnormal_img_select dlg(image, m_progressUi); dlg.exec(); m_abnormalImgResult = dlg.getUserSelect(); #ifdef HG_CMP_MSC if (nullptr != hEvent) { SetEvent((HANDLE)hEvent); } #endif } void Manager::on_deleteSettingUi(void *hEvent) { if (m_settingUi != nullptr) { m_settingUi->clear_callback(); delete m_settingUi; m_settingUi = nullptr; } } void Manager::on_deleteProgressUi(void *hEvent) { if (m_progressUi != nullptr) { m_progressUi->clear_callback(); delete m_progressUi; m_progressUi = nullptr; } } void Manager::on_deleteMessageBoxUi(void *hEvent) { }