twain3/SaneUI/ScannerUI.cpp

148 lines
3.6 KiB
C++
Raw Normal View History

#include "ScannerUI.h"
#include <QApplication>
#include "indicator.h"
#include "scansettingdialog.h"
#include <QMessageBox>
#ifdef WIN32
#include <Windows.h>
#endif
int ScannerUI::argc = 0;
char** ScannerUI::argv = nullptr;
std::unique_ptr<QApplication> ScannerUI::application;
std::unique_ptr<ScanSettingDialog> ScannerUI::scanDialog;
#ifdef WIN32
std::unique_ptr<QWidget> ScannerUI::guiBridge;
#endif
std::unique_ptr<QWidget> ScannerUI::guiIndicator;
TwGlue ScannerUI::twglue = TwGlue(nullptr, nullptr);
ScannerUIPtr GUICreate::Instance()
{
return ScannerUIPtr(new ScannerUI());
}
ScannerUI::ScannerUI()
{
if (!qApp)
application = std::unique_ptr<QApplication>(new QApplication(argc, argv));
}
ScannerUI::~ScannerUI()
{
#ifdef WIN32
//if (modalfalg)
// ::EnableWindow(parent, true);
#endif // WIN32
application.reset();
}
void ScannerUI::showTwainUI(void* handle, bool uiOnly, Func f,std::string model,bool modelui)
{
QWidget* parent = nullptr;
// as a minimal source, we do not support GUI that just saves settings
#ifdef WIN32
// we use Qt, some Windows-Qt glue is necessary to set the parent window of our dialog
if (handle) {
guiBridge = std::unique_ptr<QWidget>(new QWidget);
HWND appWindow = static_cast<HWND>(handle);
HWND bridgeWindow = reinterpret_cast<HWND>(guiBridge->winId());
long bridgeFlags = GetWindowLong(bridgeWindow, GWL_STYLE);
SetWindowLong(bridgeWindow, GWL_STYLE, bridgeFlags | WS_CHILD);
HWND ch= SetParent(bridgeWindow, appWindow);
//if (modelui)
//{
// this->parent = appWindow;
// ::EnableWindow(appWindow, false);
//}
modalfalg = modelui;
if(f.IstopFunction)
guiIndicator.reset(new Indicator(f.IstopFunction));
}
parent = guiBridge.get();
#endif
//TwGlue glue = {f.IscanFunction, f.IcancelFunction };
//TwGlue glueUiOnly = { f.IconfirmFunction, f.IcancelFunction };
func = f;
//twglue = uiOnly ? glueUiOnly: glue;
scanDialog = std::unique_ptr<ScanSettingDialog>(new ScanSettingDialog(
f,
uiOnly ? ScanSettingDialog::UIonly : ScanSettingDialog::showUI,
model,
parent));
scanDialog->defaultjson = defaultjson;
#ifdef WIN32
//if (modalfalg)
// scanDialog->enableparent = [=] { if (this->parent) ::EnableWindow(this->parent, true); };
#endif // WIN32
scanDialog->show();
}
void ScannerUI::closeTwainUI()
{
guiIndicator.reset();
scanDialog.reset();
//guiBridge.reset();
}
void ScannerUI::postMessage()
{
if (static_cast<bool>(application)) {
// QApplication::processEvents(); - TODO: needs more investigation; results in freeze when attempting to scan using old DSM
QApplication::sendPostedEvents();
}
}
void ScannerUI::hideindicator()
{
if (guiIndicator.get())
guiIndicator->hide();
if (scanDialog.get())
scanDialog->EnableID_OKorID_Cancel(true);
}
void ScannerUI::showindicator()
{
if (guiIndicator.get())
guiIndicator->show();
if (scanDialog.get())
scanDialog->EnableID_OKorID_Cancel(false);
}
void ScannerUI::getjsonstring(std::string& str)
{
defaultjson=json::parse(str);
}
void ScannerUI::is_showindicator(bool enabel)
{
if (enabel)
{
if (!guiIndicator.get())
guiIndicator.reset(new Indicator(func.IstopFunction));
}
else
{
if (guiIndicator.get())
guiIndicator.reset();
}
}
void ScannerUI::setHandle(TwGlue twglue)
{
ScannerUI::twglue = twglue;
}
void ScannerUI::Message(std::string title, std::string msg, int type)
{
QMessageBox::warning(nullptr, title.c_str(), msg.c_str());
}