2023-04-04 03:17:09 +00:00
|
|
|
#include "dialog_device_scan.h"
|
2023-03-28 10:38:35 +00:00
|
|
|
#include "ui_dialog_device_scan.h"
|
2023-03-30 08:21:52 +00:00
|
|
|
#include "base/HGInc.h"
|
2023-03-31 08:39:02 +00:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QCloseEvent>
|
2023-03-28 10:38:35 +00:00
|
|
|
|
2023-04-03 09:13:53 +00:00
|
|
|
Dialog_Device_Scan::Dialog_Device_Scan(const SANEAPI* saneApi, SANE_Handle dev, const char *devName,
|
2023-05-23 06:37:34 +00:00
|
|
|
show_scan_ui_event_callback eventCallback, void *eventParam,
|
|
|
|
show_scan_ui_image_callback imageCallback, void *imageParam, QWidget *parent) :
|
2023-03-28 10:38:35 +00:00
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::Dialog_Device_Scan)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2023-03-29 10:04:59 +00:00
|
|
|
|
|
|
|
memcpy(&m_saneAPI, saneApi, sizeof(SANEAPI));
|
2023-03-28 10:38:35 +00:00
|
|
|
m_saneDev = dev;
|
2023-05-23 06:37:34 +00:00
|
|
|
m_eventCallback = eventCallback;
|
|
|
|
m_eventParam = eventParam;
|
|
|
|
m_imageCallback = imageCallback;
|
|
|
|
m_imageParam = imageParam;
|
2023-03-29 10:04:59 +00:00
|
|
|
m_buffer = NULL;
|
|
|
|
m_bufferSize = 0;
|
|
|
|
m_stopThread = HGFALSE;
|
|
|
|
m_thread = NULL;
|
2023-03-28 10:38:35 +00:00
|
|
|
|
2023-04-03 03:06:14 +00:00
|
|
|
setWindowTitle(QString::fromStdString(devName));
|
|
|
|
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
|
|
|
|
|
2023-03-29 10:04:59 +00:00
|
|
|
ui->pushButton_Continue->setEnabled(true);
|
2023-03-28 10:38:35 +00:00
|
|
|
ui->pushButton_Cancel->setEnabled(false);
|
2023-03-29 10:04:59 +00:00
|
|
|
ui->pushButton_Complete->setEnabled(true);
|
2023-03-28 10:38:35 +00:00
|
|
|
|
|
|
|
on_pushButton_Continue_clicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
Dialog_Device_Scan::~Dialog_Device_Scan()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2023-04-03 03:52:16 +00:00
|
|
|
void Dialog_Device_Scan::on_eventFunc(QString errInfo)
|
2023-03-28 10:38:35 +00:00
|
|
|
{
|
2023-04-03 03:52:16 +00:00
|
|
|
ui->listWidget->addItem(errInfo);
|
2023-04-03 03:06:14 +00:00
|
|
|
ui->listWidget->scrollToBottom();
|
2023-03-29 10:04:59 +00:00
|
|
|
|
|
|
|
m_stopThread = HGTRUE;
|
|
|
|
m_saneAPI.sane_cancel_api(m_saneDev);
|
|
|
|
HGBase_CloseThread(m_thread);
|
|
|
|
m_thread = NULL;
|
2023-03-28 10:38:35 +00:00
|
|
|
|
2023-03-29 10:04:59 +00:00
|
|
|
free(m_buffer);
|
|
|
|
m_buffer = NULL;
|
|
|
|
m_bufferSize = 0;
|
|
|
|
|
|
|
|
ui->pushButton_Continue->setEnabled(true);
|
|
|
|
ui->pushButton_Cancel->setEnabled(false);
|
|
|
|
ui->pushButton_Complete->setEnabled(true);
|
2023-03-28 10:38:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 10:04:59 +00:00
|
|
|
void Dialog_Device_Scan::on_pushButton_Cancel_clicked()
|
|
|
|
{
|
|
|
|
m_saneAPI.sane_cancel_api(m_saneDev);
|
2023-03-28 10:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog_Device_Scan::on_pushButton_Complete_clicked()
|
|
|
|
{
|
|
|
|
accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog_Device_Scan::on_pushButton_Continue_clicked()
|
|
|
|
{
|
2023-03-29 10:04:59 +00:00
|
|
|
SANE_Parameters params;
|
|
|
|
memset(¶ms, 0, sizeof(SANE_Parameters));
|
|
|
|
SANE_Status stat = m_saneAPI.sane_get_parameters_api(m_saneDev, ¶ms);
|
|
|
|
assert(SANE_STATUS_GOOD == stat);
|
|
|
|
|
2023-05-29 07:19:07 +00:00
|
|
|
m_bufferSize = params.bytes_per_line * params.lines;
|
2023-03-29 10:04:59 +00:00
|
|
|
m_buffer = (HGByte *)malloc(m_bufferSize);
|
|
|
|
if (NULL == m_buffer)
|
|
|
|
{
|
2023-04-03 03:06:14 +00:00
|
|
|
ui->listWidget->addItem(tr("Out of memory"));
|
|
|
|
ui->listWidget->scrollToBottom();
|
2023-03-29 10:04:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_stopThread = HGFALSE;
|
|
|
|
HGBase_OpenThread(ThreadFunc, this, &m_thread);
|
|
|
|
|
|
|
|
ui->pushButton_Continue->setEnabled(false);
|
|
|
|
ui->pushButton_Cancel->setEnabled(true);
|
|
|
|
ui->pushButton_Complete->setEnabled(false);
|
|
|
|
|
2023-04-03 03:06:14 +00:00
|
|
|
ui->listWidget->addItem(tr("Start scan"));
|
|
|
|
ui->listWidget->scrollToBottom();
|
2023-03-29 10:04:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 08:39:02 +00:00
|
|
|
void Dialog_Device_Scan::closeEvent(QCloseEvent *e)
|
|
|
|
{
|
2023-04-12 02:30:14 +00:00
|
|
|
if (nullptr != m_thread)
|
2023-03-31 08:39:02 +00:00
|
|
|
{
|
2023-04-03 03:06:14 +00:00
|
|
|
QMessageBox::warning(this, tr("Warning"), tr("Scanning in progress, closing not allowed"));
|
2023-03-31 08:39:02 +00:00
|
|
|
e->ignore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:04:59 +00:00
|
|
|
void HGAPI Dialog_Device_Scan::ThreadFunc(HGThread thread, HGPointer param)
|
|
|
|
{
|
|
|
|
Dialog_Device_Scan* p = (Dialog_Device_Scan*)param;
|
|
|
|
|
|
|
|
SANE_Status stat = p->m_saneAPI.sane_start_api(p->m_saneDev);
|
|
|
|
if (SANE_STATUS_GOOD != stat)
|
|
|
|
{
|
2023-04-04 08:36:19 +00:00
|
|
|
emit p->eventFunc(QString::fromUtf8(p->m_saneAPI.sane_strstatus_api(stat)));
|
2023-03-29 10:04:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-23 06:37:34 +00:00
|
|
|
if (nullptr != p->m_eventCallback)
|
|
|
|
{
|
|
|
|
p->m_eventCallback(SHOW_SCAN_UI_EVENT_WORKING, p->m_eventParam);
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:04:59 +00:00
|
|
|
while (!p->m_stopThread)
|
|
|
|
{
|
|
|
|
SANE_Parameters params;
|
|
|
|
memset(¶ms, 0, sizeof(SANE_Parameters));
|
|
|
|
SANE_Status stat1 = p->m_saneAPI.sane_get_parameters_api(p->m_saneDev, ¶ms);
|
|
|
|
|
|
|
|
SANE_Int readSize = 0;
|
|
|
|
SANE_Status stat2 = SANE_STATUS_GOOD;
|
|
|
|
while (readSize < p->m_bufferSize)
|
|
|
|
{
|
|
|
|
SANE_Int len = 0;
|
|
|
|
stat2 = p->m_saneAPI.sane_read_api(p->m_saneDev, p->m_buffer + readSize, p->m_bufferSize - readSize, &len);
|
|
|
|
readSize += len;
|
|
|
|
if (SANE_STATUS_GOOD != stat2)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SANE_STATUS_GOOD == stat2)
|
|
|
|
{
|
|
|
|
// m_bufferSize空间不够
|
2023-04-04 08:36:19 +00:00
|
|
|
emit p->eventFunc(QString::fromUtf8(p->m_saneAPI.sane_strstatus_api(SANE_STATUS_INVAL)));
|
2023-03-29 10:04:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (SANE_STATUS_EOF == stat2)
|
|
|
|
{
|
|
|
|
if (0 == readSize)
|
|
|
|
{
|
2023-04-03 03:52:16 +00:00
|
|
|
emit p->eventFunc(tr("Scan complete"));
|
2023-03-29 10:04:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (SANE_STATUS_GOOD != stat1 || readSize != params.bytes_per_line * params.lines)
|
|
|
|
{
|
2023-04-04 08:36:19 +00:00
|
|
|
emit p->eventFunc(QString::fromUtf8(p->m_saneAPI.sane_strstatus_api(SANE_STATUS_INVAL)));
|
2023-03-29 10:04:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (SANE_STATUS_CANCELLED == stat2)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-04 08:36:19 +00:00
|
|
|
emit p->eventFunc(QString::fromUtf8(p->m_saneAPI.sane_strstatus_api(stat2)));
|
2023-03-29 10:04:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-03-28 10:38:35 +00:00
|
|
|
|
2023-05-23 06:37:34 +00:00
|
|
|
if (nullptr != p->m_imageCallback)
|
|
|
|
{
|
|
|
|
HGUInt imgType = 0;
|
|
|
|
if (params.format == SANE_FRAME_GRAY)
|
|
|
|
{
|
|
|
|
if (1 == params.depth)
|
|
|
|
imgType = HGBASE_IMGTYPE_BINARY;
|
|
|
|
else if (8 == params.depth)
|
|
|
|
imgType = HGBASE_IMGTYPE_GRAY;
|
|
|
|
}
|
|
|
|
else if (params.format == SANE_FRAME_RGB)
|
|
|
|
{
|
|
|
|
imgType = HGBASE_IMGTYPE_RGB;
|
|
|
|
}
|
|
|
|
|
|
|
|
HGImageInfo imgInfo = { (HGUInt)params.pixels_per_line, (HGUInt)params.lines,
|
|
|
|
imgType, (HGUInt)params.bytes_per_line, HGBASE_IMGORIGIN_TOP };
|
|
|
|
HGImage img = NULL;
|
|
|
|
HGBase_CreateImageWithData((HGByte*)p->m_buffer, &imgInfo, &img);
|
|
|
|
if (NULL != img)
|
|
|
|
{
|
|
|
|
p->m_imageCallback(img, p->m_imageParam);
|
|
|
|
HGBase_DestroyImage(img);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nullptr != p->m_eventCallback)
|
|
|
|
{
|
|
|
|
p->m_eventCallback(SHOW_SCAN_UI_EVENT_SCANFINISHED, p->m_eventParam);
|
2023-03-29 10:04:59 +00:00
|
|
|
}
|
2023-03-28 10:38:35 +00:00
|
|
|
}
|