code_app/modules/saneui/dialog_device_scan.cpp

203 lines
5.8 KiB
C++

#include "dialog_device_scan.h"
#include "ui_dialog_device_scan.h"
#include "base/HGInc.h"
#include <QMessageBox>
#include <QCloseEvent>
Dialog_Device_Scan::Dialog_Device_Scan(const SANEAPI* saneApi, SANE_Handle dev, const char *devName,
show_scan_ui_event_callback eventCallback, void *eventParam,
show_scan_ui_image_callback imageCallback, void *imageParam, QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog_Device_Scan)
{
ui->setupUi(this);
memcpy(&m_saneAPI, saneApi, sizeof(SANEAPI));
m_saneDev = dev;
m_eventCallback = eventCallback;
m_eventParam = eventParam;
m_imageCallback = imageCallback;
m_imageParam = imageParam;
m_buffer = NULL;
m_bufferSize = 0;
m_stopThread = HGFALSE;
m_thread = NULL;
setWindowTitle(QString::fromStdString(devName));
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
ui->pushButton_Continue->setEnabled(true);
ui->pushButton_Cancel->setEnabled(false);
ui->pushButton_Complete->setEnabled(true);
on_pushButton_Continue_clicked();
}
Dialog_Device_Scan::~Dialog_Device_Scan()
{
delete ui;
}
void Dialog_Device_Scan::on_eventFunc(QString errInfo)
{
ui->listWidget->addItem(errInfo);
ui->listWidget->scrollToBottom();
m_stopThread = HGTRUE;
m_saneAPI.sane_cancel_api(m_saneDev);
HGBase_CloseThread(m_thread);
m_thread = NULL;
free(m_buffer);
m_buffer = NULL;
m_bufferSize = 0;
ui->pushButton_Continue->setEnabled(true);
ui->pushButton_Cancel->setEnabled(false);
ui->pushButton_Complete->setEnabled(true);
}
void Dialog_Device_Scan::on_pushButton_Cancel_clicked()
{
m_saneAPI.sane_cancel_api(m_saneDev);
}
void Dialog_Device_Scan::on_pushButton_Complete_clicked()
{
accept();
}
void Dialog_Device_Scan::on_pushButton_Continue_clicked()
{
SANE_Parameters params;
memset(&params, 0, sizeof(SANE_Parameters));
SANE_Status stat = m_saneAPI.sane_get_parameters_api(m_saneDev, &params);
assert(SANE_STATUS_GOOD == stat);
m_bufferSize = params.bytes_per_line * params.lines;
m_buffer = (HGByte *)malloc(m_bufferSize);
if (NULL == m_buffer)
{
ui->listWidget->addItem(tr("Out of memory"));
ui->listWidget->scrollToBottom();
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);
ui->listWidget->addItem(tr("Start scan"));
ui->listWidget->scrollToBottom();
}
void Dialog_Device_Scan::closeEvent(QCloseEvent *e)
{
if (nullptr != m_thread)
{
QMessageBox::warning(this, tr("Warning"), tr("Scanning in progress, closing not allowed"));
e->ignore();
return;
}
}
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)
{
emit p->eventFunc(QString::fromUtf8(p->m_saneAPI.sane_strstatus_api(stat)));
return;
}
if (nullptr != p->m_eventCallback)
{
p->m_eventCallback(SHOW_SCAN_UI_EVENT_WORKING, p->m_eventParam);
}
while (!p->m_stopThread)
{
SANE_Parameters params;
memset(&params, 0, sizeof(SANE_Parameters));
SANE_Status stat1 = p->m_saneAPI.sane_get_parameters_api(p->m_saneDev, &params);
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空间不够
emit p->eventFunc(QString::fromUtf8(p->m_saneAPI.sane_strstatus_api(SANE_STATUS_INVAL)));
break;
}
else if (SANE_STATUS_EOF == stat2)
{
if (0 == readSize)
{
emit p->eventFunc(tr("Scan complete"));
break;
}
else if (SANE_STATUS_GOOD != stat1 || readSize != params.bytes_per_line * params.lines)
{
emit p->eventFunc(QString::fromUtf8(p->m_saneAPI.sane_strstatus_api(SANE_STATUS_INVAL)));
break;
}
}
else if (SANE_STATUS_CANCELLED == stat2)
{
break;
}
else
{
emit p->eventFunc(QString::fromUtf8(p->m_saneAPI.sane_strstatus_api(stat2)));
break;
}
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);
}
}