code_app/modules/twainui/Manager.cpp

377 lines
8.9 KiB
C++
Raw Normal View History

2023-05-10 12:30:07 +00:00
#include "Manager.h"
#include <QApplication>
#include <qwinwidget.hpp>
#include "lang/app_language.h"
2023-05-24 06:40:06 +00:00
extern Manager* g_manager;
2023-05-10 12:30:07 +00:00
Manager::Manager()
: QObject(nullptr)
2023-05-23 09:40:43 +00:00
, m_progressUiCallback(std::function<void(ui_result)>())
, m_notify(nullptr)
, m_settingUiCallback(std::function<void(ui_result)>())
, m_settingUi(nullptr)
, m_progressUi(nullptr)
, m_msgBoxUi(nullptr)
2023-05-10 12:30:07 +00:00
{
HGBase_CreateEvent(HGFALSE, HGFALSE, &m_event);
connect(this, SIGNAL(init()), this, SLOT(on_init()));
2023-05-19 10:41:49 +00:00
connect(this, SIGNAL(createDeviceSelect(bool)), this, SLOT(on_createDeviceSelect(bool)));
connect(this, SIGNAL(createSettingUi(bool)), this, SLOT(on_createSettingUi(bool)));
connect(this, SIGNAL(createProgressUi(bool)), this, SLOT(on_createProgressUi(bool)));
connect(this, SIGNAL(createMessageBoxUi(bool)), this, SLOT(on_createMessageBoxUi(bool)));
connect(this, SIGNAL(createTwainSrcUi(bool)), this, SLOT(on_createTwainSrcUi(bool)));
2023-05-10 12:30:07 +00:00
2023-05-24 06:40:06 +00:00
connect(this, SIGNAL(deleteDeviceSelectUi()), this, SLOT(on_deleteDeviceSelectUi()));
connect(this, SIGNAL(deleteSettingUi()), this, SLOT(on_deleteSettingUi()));
connect(this, SIGNAL(deleteProgressUi()), this, SLOT(on_deleteProgressUi()));
connect(this, SIGNAL(deleteMessageBoxUi()), this, SLOT(on_deleteMessageBoxUi()));
connect(this, SIGNAL(deleteManager()), this, SLOT(on_deleteManager()));
2023-05-10 12:30:07 +00:00
emit init();
}
Manager::~Manager()
{
QCoreApplication::removeTranslator(&m_translator);
if (20127 != m_langCode)
QCoreApplication::removeTranslator(&m_translator_qt);
HGBase_DestroyEvent(m_event);
2023-05-10 12:30:07 +00:00
}
2023-05-19 10:41:49 +00:00
int Manager::showDeviceSelect(bool qt, const std::vector<DEVQUEUI>& devs)
2023-05-10 12:30:07 +00:00
{
m_DeviceSelectDevs = devs;
m_DeviceSelectThreadId = GetCurrentThreadId();
2023-05-19 10:41:49 +00:00
emit createDeviceSelect(qt);
2023-05-10 12:30:07 +00:00
2023-05-19 10:41:49 +00:00
if (!qt)
2023-05-10 12:30:07 +00:00
{
2023-05-19 10:41:49 +00:00
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
2023-05-10 12:30:07 +00:00
}
return m_DeviceSelectResult;
}
2023-05-19 10:41:49 +00:00
int Manager::showSettingUi(bool qt, SANE_Handle device, HWND parent, LPSANEAPI api, const char *devName, bool with_scan, std::function<void (ui_result)> callback)
2023-05-10 12:30:07 +00:00
{
m_device = device;
m_settingUiParent = parent;
2023-05-10 12:30:07 +00:00
memcpy(&m_api, api, sizeof(m_api));
m_devName = devName;
m_with_scan = with_scan;
m_settingUiCallback = callback;
m_SettingUiThreadId = GetCurrentThreadId();
2023-05-19 10:41:49 +00:00
emit createSettingUi(qt);
2023-05-10 12:30:07 +00:00
return 0;
}
2023-05-19 10:41:49 +00:00
int Manager::showProgressUi(bool qt, HWND parent, std::function<void (ui_result)> callback, std::function<void (int, void *, int)> *notify)
2023-05-10 12:30:07 +00:00
{
2023-05-12 10:26:23 +00:00
m_progressUiparent = parent;
if (nullptr != m_settingUi)
m_progressUiparent = (HWND)m_settingUi->winId();
2023-05-10 12:30:07 +00:00
m_progressUiCallback = callback;
m_notify = notify;
2023-05-27 06:09:54 +00:00
m_ProgressUiThreadId = GetCurrentThreadId();
emit createProgressUi(qt);
2023-05-19 10:41:49 +00:00
if (!qt)
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
2023-05-16 02:57:46 +00:00
2023-05-10 12:30:07 +00:00
return 0;
}
2023-05-19 10:41:49 +00:00
int Manager::showMessageBoxUi(bool qt, HWND parent, int event, void *msg, int flag)
2023-05-10 12:30:07 +00:00
{
2023-05-19 10:41:49 +00:00
m_messageBoxUiParent = nullptr;
2023-05-10 12:30:07 +00:00
m_notifyEvent = event;
m_message = (char*)msg;
m_flag = flag;
m_MessageBoxUiThreadId = GetCurrentThreadId();
2023-05-19 10:41:49 +00:00
emit createMessageBoxUi(qt);
2023-05-10 12:30:07 +00:00
2023-05-25 10:41:40 +00:00
if (!qt)
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
2023-05-10 12:30:07 +00:00
return 0;
}
2023-05-31 01:05:33 +00:00
int Manager::showTwainSrcUi(bool qt, const TW_IDENTITY *vds, HGUInt count, const char* defDevName, HGWindow parent, TW_IDENTITY *ds)
{
2023-05-12 10:26:23 +00:00
m_twainSrcUiThreadId = GetCurrentThreadId();
m_TwainSrcUiparent = parent;
2023-05-30 11:11:24 +00:00
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));
2023-05-19 10:41:49 +00:00
emit createTwainSrcUi(qt);
2023-05-12 10:26:23 +00:00
2023-05-19 10:41:49 +00:00
if (!qt)
{
2023-05-19 10:41:49 +00:00
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
2023-05-12 10:26:23 +00:00
memcpy(ds, &m_ds, sizeof(TW_IDENTITY));
return 0;
}
2023-05-24 06:40:06 +00:00
void Manager::closeSettingUi()
{
emit deleteSettingUi();
}
void Manager::closeProgressUi()
{
emit deleteProgressUi();
}
void Manager::closeMessageBoxUi()
{
emit deleteMessageBoxUi();
}
void Manager::closeManager()
{
emit deleteManager();
}
void Manager::clear_functions(void)
{
m_settingUiCallback = std::function<void(ui_result)>();
m_progressUiCallback = std::function<void(ui_result)>();
m_notify = nullptr;
2023-05-24 10:00:21 +00:00
if (m_progressUi)
2023-05-24 06:40:06 +00:00
m_progressUi->clear_callback();
if (m_settingUi)
m_settingUi->clear_callback();
}
2023-05-10 12:30:07 +00:00
void Manager::on_init()
{
m_langCode = lang_get_cur_code_page();
if (20127 == 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 (20127 != m_langCode)
QCoreApplication::installTranslator(&m_translator_qt);
}
2023-05-19 10:41:49 +00:00
void Manager::on_createDeviceSelect(bool qt)
2023-05-10 12:30:07 +00:00
{
2023-05-24 10:00:21 +00:00
Dialog_device_select *dlg = new Dialog_device_select(m_DeviceSelectDevs);
dlg->exec();
m_DeviceSelectResult = dlg->getDevId();
2023-05-19 10:41:49 +00:00
if (!qt)
{
::PostThreadMessage(m_DeviceSelectThreadId, WM_QUIT, 0, 0);
}
2023-05-10 12:30:07 +00:00
}
2023-05-19 10:41:49 +00:00
void Manager::on_createSettingUi(bool qt)
2023-05-10 12:30:07 +00:00
{
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
2023-05-24 06:40:06 +00:00
auto close_prog = [this](ui_result r) ->void
{
if(r != UI_RESULT_START_SCAN)
m_settingUi = nullptr;
m_settingUiCallback(r);
};
if (m_settingUi == nullptr)
m_settingUi = new hg_settingdialog(this, m_device, &m_api, m_with_scan, m_devName.c_str(), close_prog, qParent);
2023-05-23 09:40:43 +00:00
m_settingUi->setModal(true);
m_settingUi->show();
2023-05-10 12:30:07 +00:00
}
2023-05-19 10:41:49 +00:00
void Manager::on_createProgressUi(bool qt)
2023-05-10 12:30:07 +00:00
{
QWidget *qParent = nullptr;
#ifdef HG_CMP_MSC
if (nullptr != m_progressUiparent)
{
QWinWidget *win = new QWinWidget(m_progressUiparent);
win->showCentered();
qParent = win;
}
2023-05-19 10:41:49 +00:00
2023-05-10 12:30:07 +00:00
#else
qParent = m_progressUiparent;
#endif
2023-05-24 06:40:06 +00:00
auto close_prog = [&](ui_result r) ->void
{
//m_progressUi = nullptr;
2023-05-24 06:40:06 +00:00
if(m_progressUiCallback)
m_progressUiCallback(r);
2023-05-24 10:00:21 +00:00
if(r == UI_RESULT_CLOSE_NORMAL)
m_progressUiCallback = std::function<void(ui_result)>();
2023-05-24 06:40:06 +00:00
};
if (m_progressUi == nullptr)
m_progressUi = new Dialog_progress_ui(this, close_prog, m_notify, qParent);
2023-05-23 09:40:43 +00:00
m_progressUi->setModal(true);
m_progressUi->show();
2023-05-19 10:41:49 +00:00
if (!qt)
{
::PostThreadMessage(m_ProgressUiThreadId, WM_QUIT, 0, 0);
}
2023-05-10 12:30:07 +00:00
}
2023-05-19 10:41:49 +00:00
void Manager::on_createMessageBoxUi(bool qt)
2023-05-10 12:30:07 +00:00
{
if (m_msgBoxUi != nullptr)
{
delete m_msgBoxUi;
m_msgBoxUi = nullptr;
}
2023-05-10 12:30:07 +00:00
QWidget *qParent = nullptr;
#ifdef HG_CMP_MSC
if (nullptr != m_messageBoxUiParent)
{
QWinWidget *win = new QWinWidget(m_messageBoxUiParent);
win->showCentered();
qParent = win;
}
#else
qParent = m_messageBoxUiParent;
#endif
assert(m_msgBoxUi == nullptr);
m_msgBoxUi = new QMessageBox(QMessageBox::Critical, tr("Prompt"), QString::fromStdString(m_message), QMessageBox::Ok, qParent);
2023-05-25 10:48:05 +00:00
m_msgBoxUi->setWindowFlags(Qt::SubWindow | Qt::Popup | Qt::WindowStaysOnTopHint);
2023-05-25 02:11:53 +00:00
//m_msgBoxUi->setModal(true);
m_msgBoxUi->exec();
delete m_msgBoxUi;
m_msgBoxUi = nullptr;
2023-05-25 10:41:40 +00:00
if (!qt)
{
::PostThreadMessage(m_MessageBoxUiThreadId, WM_QUIT, 0, 0);
}
2023-05-10 12:30:07 +00:00
}
2023-05-19 10:41:49 +00:00
void Manager::on_createTwainSrcUi(bool qt)
{
QWidget *qParent = nullptr;
#ifdef HG_CMP_MSC
2023-05-12 10:26:23 +00:00
QWinWidget win(m_TwainSrcUiparent);
if (nullptr != m_TwainSrcUiparent)
{
2023-05-12 10:26:23 +00:00
win.showCentered();
qParent = &win;
}
#else
qParent = m_TwainSrcUiparent;
#endif
2023-05-30 11:11:24 +00:00
Dialog_Twain_Source_Select *dlg = new Dialog_Twain_Source_Select (m_vds, m_defDsName, qParent);
2023-05-24 10:00:21 +00:00
if (dlg->exec())
{
2023-05-24 10:00:21 +00:00
dlg->GetIdentify(&m_ds);
}
2023-05-19 10:41:49 +00:00
if (!qt)
{
::PostThreadMessage(m_twainSrcUiThreadId, WM_QUIT, 0, 0);
}
}
2023-05-24 06:40:06 +00:00
void Manager::on_deleteSettingUi()
{
if (m_settingUi != nullptr)
{
m_settingUi->clear_callback();
delete m_settingUi;
m_settingUi = nullptr;
}
}
void Manager::on_deleteProgressUi()
{
if (m_progressUi != nullptr)
{
m_progressUi->clear_callback();
delete m_progressUi;
m_progressUi = nullptr;
}
}
void Manager::on_deleteMessageBoxUi()
{
if (m_msgBoxUi != nullptr)
{
delete m_msgBoxUi;
m_msgBoxUi = nullptr;
}
}
void Manager::on_deleteManager()
{
if (NULL != g_manager)
{
delete g_manager;
g_manager = nullptr;
}
QApplication::exit();
}