2023-03-30 10:20:26 +00:00
|
|
|
#include "dialog_device_scan.hpp"
|
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 03:06:14 +00:00
|
|
|
Dialog_Device_Scan::Dialog_Device_Scan(SANEAPI* saneApi, SANE_Handle dev, const char *devName,
|
2023-03-29 10:04:59 +00:00
|
|
|
show_scan_ui_image_callback callback, void *callbackParam, 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-03-29 10:04:59 +00:00
|
|
|
m_callback = callback;
|
|
|
|
m_callbackParam = callbackParam;
|
|
|
|
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);
|
|
|
|
|
|
|
|
qRegisterMetaType<SANE_Parameters>("SANE_Parameters");
|
|
|
|
qRegisterMetaType<SANE_Byte *>("SANE_Byte *");
|
|
|
|
connect(this, &Dialog_Device_Scan::eventFunc, this, &Dialog_Device_Scan::on_eventFunc, Qt::QueuedConnection);
|
|
|
|
connect(this, &Dialog_Device_Scan::newImage, this, &Dialog_Device_Scan::on_newImage, Qt::QueuedConnection);
|
2023-03-28 10:38:35 +00:00
|
|
|
|
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-04-03 03:06:14 +00:00
|
|
|
void Dialog_Device_Scan::on_newImage(SANE_Parameters format, SANE_Byte *data)
|
2023-03-28 10:38:35 +00:00
|
|
|
{
|
2023-03-29 10:04:59 +00:00
|
|
|
if (NULL != m_callback)
|
2023-04-03 03:06:14 +00:00
|
|
|
m_callback(&format, data, m_callbackParam);
|
|
|
|
delete [] data;
|
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);
|
|
|
|
|
|
|
|
m_bufferSize = 5000 * 4000;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (NULL != m_thread)
|
|
|
|
{
|
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-03 03:52:16 +00:00
|
|
|
emit p->eventFunc(tr(p->m_saneAPI.sane_strstatus_api(stat)));
|
2023-03-29 10:04:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03 03:52:16 +00:00
|
|
|
emit p->eventFunc(tr(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-03 03:52:16 +00:00
|
|
|
emit p->eventFunc(tr(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-03 03:52:16 +00:00
|
|
|
emit p->eventFunc(tr(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-03-29 10:04:59 +00:00
|
|
|
SANE_Byte *data = new HGByte [readSize];
|
|
|
|
memcpy(data, p->m_buffer, readSize);
|
2023-04-03 03:06:14 +00:00
|
|
|
emit p->newImage(params, data);
|
2023-03-29 10:04:59 +00:00
|
|
|
}
|
2023-03-28 10:38:35 +00:00
|
|
|
}
|