2023-04-20 09:49:48 +00:00
|
|
|
#include "dialog_progress_ui.h"
|
|
|
|
#include "ui_dialog_progress_ui.h"
|
2023-04-23 01:25:52 +00:00
|
|
|
#include "lang/app_language.h"
|
2023-05-08 12:01:43 +00:00
|
|
|
#include "huagao/hgscanner_error.h"
|
2023-04-23 01:25:52 +00:00
|
|
|
#include <QCloseEvent>
|
2023-04-27 03:04:52 +00:00
|
|
|
#include <QTimer>
|
2023-05-11 13:07:30 +00:00
|
|
|
#include <thread>
|
2023-04-20 09:49:48 +00:00
|
|
|
|
|
|
|
Dialog_progress_ui *Dialog_progress_ui::m_param = nullptr;
|
|
|
|
|
|
|
|
Dialog_progress_ui::Dialog_progress_ui(std::function<void (ui_result)> callback, std::function<void (int, void *, int)> *notify, QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::Dialog_progress_ui)
|
2023-05-10 12:30:07 +00:00
|
|
|
, m_isScanning(false)
|
2023-04-20 09:49:48 +00:00
|
|
|
{
|
2023-04-23 01:25:52 +00:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose, true);
|
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
ui->setupUi(this);
|
2023-04-27 09:42:50 +00:00
|
|
|
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
|
2023-04-20 09:49:48 +00:00
|
|
|
|
2023-04-25 06:46:50 +00:00
|
|
|
ui->pbtn_cancelScan->setVisible(false);
|
|
|
|
ui->pbtn_close->setVisible(false);
|
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
m_callback = callback;
|
|
|
|
*notify = FuncNotify;
|
2023-05-10 12:30:07 +00:00
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
m_param = this;
|
|
|
|
|
2023-04-27 03:04:52 +00:00
|
|
|
m_timer = new QTimer(this);
|
|
|
|
|
|
|
|
connect(this, SIGNAL(scan_status(QString,bool)), this, SLOT(on_scan_status(QString,bool)), Qt::QueuedConnection);
|
2023-05-08 14:27:56 +00:00
|
|
|
connect(this, SIGNAL(scan_finish(int)), this, SLOT(on_scan_finish(int)), Qt::QueuedConnection);
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Dialog_progress_ui::~Dialog_progress_ui()
|
|
|
|
{
|
|
|
|
delete ui;
|
2023-05-10 12:30:07 +00:00
|
|
|
}
|
2023-04-23 01:25:52 +00:00
|
|
|
|
2023-05-10 12:30:07 +00:00
|
|
|
void Dialog_progress_ui::closeEvent(QCloseEvent *e)
|
|
|
|
{
|
|
|
|
if (m_isScanning)
|
|
|
|
{
|
|
|
|
if (e->spontaneous())
|
|
|
|
{
|
|
|
|
m_callback(UI_RESULT_CLOSE_CANCEL);
|
|
|
|
}
|
|
|
|
e->ignore();
|
|
|
|
}
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog_progress_ui::on_pbtn_cancelScan_clicked()
|
|
|
|
{
|
|
|
|
m_callback(UI_RESULT_CLOSE_CANCEL);
|
|
|
|
}
|
|
|
|
|
2023-05-11 13:07:30 +00:00
|
|
|
void notify_close(std::function<void (ui_result)> callback)
|
|
|
|
{
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
|
callback(UI_RESULT_CLOSE_NORMAL);
|
|
|
|
}
|
|
|
|
|
2023-04-25 06:46:50 +00:00
|
|
|
void Dialog_progress_ui::on_pbtn_close_clicked()
|
|
|
|
{
|
2023-04-27 03:04:52 +00:00
|
|
|
disconnect(m_timer, SIGNAL(timeout()), this, SLOT(on_pbtn_close_clicked()));
|
2023-05-11 13:07:30 +00:00
|
|
|
//CloseHandle(CreateThread(NULL, 0, notify_close, (void*)m_callback, 0, 0));
|
|
|
|
std::thread th_close(notify_close, m_callback);
|
|
|
|
th_close.detach();
|
|
|
|
// m_callback(UI_RESULT_CLOSE_NORMAL);
|
2023-04-25 06:46:50 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2023-04-27 03:04:52 +00:00
|
|
|
void Dialog_progress_ui::on_scan_status(QString finishInfo, bool isScanning)
|
2023-04-20 09:49:48 +00:00
|
|
|
{
|
2023-05-10 12:30:07 +00:00
|
|
|
m_isScanning = isScanning;
|
2023-04-20 09:49:48 +00:00
|
|
|
ui->label->setText(finishInfo);
|
2023-04-27 03:04:52 +00:00
|
|
|
ui->pbtn_cancelScan->setVisible(isScanning);
|
|
|
|
ui->pbtn_close->setVisible(!isScanning);
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 12:01:43 +00:00
|
|
|
void Dialog_progress_ui::on_scan_finish(int flag)
|
2023-04-25 06:46:50 +00:00
|
|
|
{
|
2023-05-08 14:27:56 +00:00
|
|
|
if (SCANNER_ERR_OK == flag || SCANNER_ERR_USER_CANCELED == flag)
|
2023-05-08 12:01:43 +00:00
|
|
|
{
|
|
|
|
m_timer->start(2000);
|
|
|
|
connect(m_timer, SIGNAL(timeout()), this, SLOT(on_pbtn_close_clicked()), Qt::QueuedConnection);
|
|
|
|
}
|
2023-04-25 06:46:50 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
void Dialog_progress_ui::FuncNotify(int event, void *msg, int flag)
|
|
|
|
{
|
|
|
|
Dialog_progress_ui *p = (Dialog_progress_ui *)m_param;
|
2023-05-10 12:30:07 +00:00
|
|
|
if (nullptr == p)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2023-04-20 09:49:48 +00:00
|
|
|
|
|
|
|
switch (event)
|
|
|
|
{
|
2023-04-25 06:46:50 +00:00
|
|
|
case SANE_EVENT_WORKING:
|
|
|
|
{
|
|
|
|
QString finishInfo((char*)msg);
|
2023-04-27 03:04:52 +00:00
|
|
|
emit p->scan_status(finishInfo, true);
|
2023-04-25 06:46:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-20 09:49:48 +00:00
|
|
|
case SANE_EVENT_SCAN_FINISHED:
|
|
|
|
{
|
2023-05-08 12:01:43 +00:00
|
|
|
emit p->scan_finish(flag);
|
2023-04-20 09:49:48 +00:00
|
|
|
QString finishInfo((char*)msg);
|
2023-04-27 03:04:52 +00:00
|
|
|
emit p->scan_status(finishInfo, false);
|
2023-04-20 09:49:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|