2024-04-23 09:44:22 +00:00
|
|
|
#include "dialog_scan.h"
|
|
|
|
#include "ui_dialog_scan.h"
|
|
|
|
#include <QCloseEvent>
|
2024-04-24 06:57:44 +00:00
|
|
|
#include "mainwindow.h"
|
2024-04-23 09:44:22 +00:00
|
|
|
|
2024-04-24 06:57:44 +00:00
|
|
|
Dialog_Scan::Dialog_Scan(class MainWindow *mainWnd)
|
2024-04-24 07:08:43 +00:00
|
|
|
: QDialog(nullptr)
|
2024-04-23 09:44:22 +00:00
|
|
|
, ui(new Ui::Dialog_Scan)
|
2024-04-24 06:57:44 +00:00
|
|
|
, m_mainWnd(mainWnd)
|
2024-04-24 10:08:14 +00:00
|
|
|
, m_devHandle(nullptr)
|
2024-04-23 09:44:22 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
|
2024-04-24 10:08:14 +00:00
|
|
|
|
|
|
|
connect(this, SIGNAL(deviceArrive(QString)), this, SLOT(on_deviceArrive(QString)));
|
|
|
|
connect(this, SIGNAL(deviceRemove(QString)), this, SLOT(on_deviceRemove(QString)));
|
|
|
|
|
|
|
|
SANE_Int version_code = 0;
|
|
|
|
sane_init_ex(&version_code, sane_ex_callback, this);
|
2024-04-23 09:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Dialog_Scan::~Dialog_Scan()
|
|
|
|
{
|
2024-04-24 10:08:14 +00:00
|
|
|
if (NULL != m_devHandle)
|
|
|
|
{
|
|
|
|
sane_cancel(m_devHandle);
|
|
|
|
sane_close(m_devHandle);
|
|
|
|
m_devHandle = NULL;
|
|
|
|
m_devName.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
sane_exit();
|
2024-04-23 09:44:22 +00:00
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2024-04-24 10:08:14 +00:00
|
|
|
int Dialog_Scan::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param)
|
|
|
|
{
|
|
|
|
(void)hdev;
|
|
|
|
(void)len;
|
|
|
|
|
|
|
|
Dialog_Scan* p = (Dialog_Scan*)param;
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case SANE_EVENT_DEVICE_ARRIVED:
|
|
|
|
{
|
|
|
|
SANE_Device* sane_dev = (SANE_Device*)data;
|
|
|
|
emit p->deviceArrive(sane_dev->name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SANE_EVENT_DEVICE_LEFT:
|
|
|
|
{
|
|
|
|
SANE_Device* sane_dev = (SANE_Device*)data;
|
|
|
|
emit p->deviceRemove(sane_dev->name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-04-23 09:44:22 +00:00
|
|
|
void Dialog_Scan::closeEvent(QCloseEvent *e)
|
|
|
|
{
|
|
|
|
hide(); // 隐藏主窗口
|
|
|
|
e->ignore(); //忽略关闭事件,这样才不会关闭程序
|
|
|
|
}
|
2024-04-24 10:08:14 +00:00
|
|
|
|
|
|
|
void Dialog_Scan::on_deviceArrive(QString devName)
|
|
|
|
{
|
|
|
|
ui->comboBox->addItem(devName);
|
|
|
|
if (nullptr == m_devHandle)
|
|
|
|
{
|
|
|
|
SANE_Status status = sane_open(devName.toStdString().c_str(), &m_devHandle);
|
|
|
|
if (SANE_STATUS_GOOD == status)
|
|
|
|
{
|
|
|
|
m_devName = devName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog_Scan::on_deviceRemove(QString devName)
|
|
|
|
{
|
|
|
|
if (devName == m_devName)
|
|
|
|
{
|
|
|
|
assert(NULL != m_devHandle);
|
|
|
|
sane_cancel(m_devHandle);
|
|
|
|
sane_close(m_devHandle);
|
|
|
|
m_devHandle = NULL;
|
|
|
|
m_devName.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < ui->comboBox->count(); ++i)
|
|
|
|
{
|
|
|
|
if (ui->comboBox->itemText(i) == devName)
|
|
|
|
{
|
|
|
|
ui->comboBox->removeItem(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog_Scan::on_comboBox_currentIndexChanged(const QString &arg1)
|
|
|
|
{
|
|
|
|
if (NULL != m_devHandle)
|
|
|
|
{
|
|
|
|
sane_cancel(m_devHandle);
|
|
|
|
sane_close(m_devHandle);
|
|
|
|
m_devHandle = NULL;
|
|
|
|
m_devName.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
SANE_Status status = sane_open(arg1.toStdString().c_str(), &m_devHandle);
|
|
|
|
if (SANE_STATUS_GOOD == status)
|
|
|
|
{
|
|
|
|
m_devName = arg1;
|
|
|
|
}
|
|
|
|
}
|